Spelling fixes.
[bpt/emacs.git] / lisp / org / org-agenda.el
CommitLineData
b349f79f 1;;; org-agenda.el --- Dynamic task and appointment lists for Org
20908596 2
cbd20947 3;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
20908596
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
6;; Keywords: outlines, hypermedia, calendar, wp
7;; Homepage: http://orgmode.org
3ab2c837 8;; Version: 7.7
20908596
CD
9;;
10;; This file is part of GNU Emacs.
11;;
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
20908596
CD
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25;;
26;;; Commentary:
27
28;; This file contains the code for creating and using the Agenda for Org-mode.
29
30;;; Code:
31
32(require 'org)
33(eval-when-compile
86fbb8ca 34 (require 'cl))
20908596 35
b349f79f 36(declare-function diary-add-to-list "diary-lib"
20908596
CD
37 (date string specifier &optional marker globcolor literal))
38(declare-function calendar-absolute-from-iso "cal-iso" (date))
39(declare-function calendar-astro-date-string "cal-julian" (&optional date))
40(declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
20908596
CD
41(declare-function calendar-chinese-date-string "cal-china" (&optional date))
42(declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
43(declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
44(declare-function calendar-french-date-string "cal-french" (&optional date))
45(declare-function calendar-goto-date "cal-move" (date))
46(declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
47(declare-function calendar-islamic-date-string "cal-islam" (&optional date))
48(declare-function calendar-iso-date-string "cal-iso" (&optional date))
f6aafbed 49(declare-function calendar-iso-from-absolute "cal-iso" (date))
20908596
CD
50(declare-function calendar-julian-date-string "cal-julian" (&optional date))
51(declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
52(declare-function calendar-persian-date-string "cal-persia" (&optional date))
68a1b090
GM
53(declare-function org-datetree-find-date-create "org-datetree"
54 (date &optional keep-restriction))
20908596 55(declare-function org-columns-quit "org-colview" ())
8bfe682a
CD
56(declare-function diary-date-display-form "diary-lib" (&optional type))
57(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
58(declare-function org-habit-insert-consistency-graphs
59 "org-habit" (&optional line))
60(declare-function org-is-habit-p "org-habit" (&optional pom))
61(declare-function org-habit-parse-todo "org-habit" (&optional pom))
68a1b090 62(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
3ab2c837 63
20908596 64(defvar calendar-mode-map)
afe98dfa 65(defvar org-clock-current-task) ; defined in org-clock.el
8d642074 66(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
8bfe682a
CD
67(defvar org-habit-show-habits)
68(defvar org-habit-show-habits-only-for-today)
20908596
CD
69
70;; Defined somewhere in this file, but used before definition.
71(defvar org-agenda-buffer-name)
72(defvar org-agenda-overriding-header)
8d642074 73(defvar org-agenda-title-append nil)
20908596
CD
74(defvar entry)
75(defvar date)
76(defvar org-agenda-undo-list)
77(defvar org-agenda-pending-undo-list)
78(defvar original-date) ; dynamically scoped, calendar.el does scope this
79
80(defcustom org-agenda-confirm-kill 1
81 "When set, remote killing from the agenda buffer needs confirmation.
82When t, a confirmation is always needed. When a number N, confirmation is
83only needed when the text to be killed contains more than N non-white lines."
84 :group 'org-agenda
85 :type '(choice
86 (const :tag "Never" nil)
87 (const :tag "Always" t)
c8d0cf5c 88 (integer :tag "When more than N lines")))
20908596
CD
89
90(defcustom org-agenda-compact-blocks nil
ed21c5c8 91 "Non-nil means make the block agenda more compact.
3ab2c837
BG
92This is done globally by leaving out lines like the agenda span
93name and week number or the separator lines."
20908596
CD
94 :group 'org-agenda
95 :type 'boolean)
96
0bd48b37
CD
97(defcustom org-agenda-block-separator ?=
98 "The separator between blocks in the agenda.
99If this is a string, it will be used as the separator, with a newline added.
3ab2c837
BG
100If it is a character, it will be repeated to fill the window width.
101If nil the separator is disabled. In `org-agenda-custom-commands' this
102addresses the separator between the current and the previous block."
0bd48b37
CD
103 :group 'org-agenda
104 :type '(choice
3ab2c837 105 (const :tag "Disabled" nil)
0bd48b37
CD
106 (character)
107 (string)))
108
20908596
CD
109(defgroup org-agenda-export nil
110 "Options concerning exporting agenda views in Org-mode."
111 :tag "Org Agenda Export"
112 :group 'org-agenda)
113
114(defcustom org-agenda-with-colors t
ed21c5c8 115 "Non-nil means use colors in agenda views."
20908596
CD
116 :group 'org-agenda-export
117 :type 'boolean)
118
119(defcustom org-agenda-exporter-settings nil
120 "Alist of variable/value pairs that should be active during agenda export.
c8d0cf5c
CD
121This is a good place to set options for ps-print and for htmlize.
122Note that the way this is implemented, the values will be evaluated
123before assigned to the variables. So make sure to quote values you do
124*not* want evaluated, for example
125
126 (setq org-agenda-exporter-settings
127 '((ps-print-color-p 'black-white)))"
20908596
CD
128 :group 'org-agenda-export
129 :type '(repeat
130 (list
131 (variable)
132 (sexp :tag "Value"))))
133
c8d0cf5c
CD
134(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
135 "Hook run in temporary buffer before writing it to an export file.
136A useful function is `org-agenda-add-entry-text'."
137 :group 'org-agenda-export
138 :type 'hook
139 :options '(org-agenda-add-entry-text))
140
141(defcustom org-agenda-add-entry-text-maxlines 0
142 "Maximum number of entry text lines to be added to agenda.
143This is only relevant when `org-agenda-add-entry-text' is part of
144`org-agenda-before-write-hook', which it is by default.
145When this is 0, nothing will happen. When it is greater than 0, it
146specifies the maximum number of lines that will be added for each entry
54a0dee5
CD
147that is listed in the agenda view.
148
149Note that this variable is not used during display, only when exporting
86fbb8ca
CD
150the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
151and `org-agenda-entry-text-maxlines'."
c8d0cf5c
CD
152 :group 'org-agenda
153 :type 'integer)
154
155(defcustom org-agenda-add-entry-text-descriptive-links t
ed21c5c8 156 "Non-nil means export org-links as descriptive links in agenda added text.
c8d0cf5c
CD
157This variable applies to the text added to the agenda when
158`org-agenda-add-entry-text-maxlines' is larger than 0.
7877f373 159When this variable is nil, the URL will (also) be shown."
c8d0cf5c
CD
160 :group 'org-agenda
161 :type 'boolean)
162
20908596
CD
163(defcustom org-agenda-export-html-style ""
164 "The style specification for exported HTML Agenda files.
165If this variable contains a string, it will replace the default <style>
166section as produced by `htmlize'.
167Since there are different ways of setting style information, this variable
168needs to contain the full HTML structure to provide a style, including the
169surrounding HTML tags. The style specifications should include definitions
170the fonts used by the agenda, here is an example:
171
172 <style type=\"text/css\">
173 p { font-weight: normal; color: gray; }
174 .org-agenda-structure {
175 font-size: 110%;
176 color: #003399;
177 font-weight: 600;
178 }
179 .org-todo {
180 color: #cc6666;
181 font-weight: bold;
182 }
c8d0cf5c
CD
183 .org-agenda-done {
184 color: #339933;
185 }
20908596
CD
186 .org-done {
187 color: #339933;
188 }
189 .title { text-align: center; }
190 .todo, .deadline { color: red; }
191 .done { color: green; }
192 </style>
193
194or, if you want to keep the style in a file,
195
196 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
197
198As the value of this option simply gets inserted into the HTML <head> header,
199you can \"misuse\" it to also add other text to the header. However,
200<style>...</style> is required, if not present the variable will be ignored."
201 :group 'org-agenda-export
202 :group 'org-export-html
203 :type 'string)
204
86fbb8ca
CD
205(defcustom org-agenda-persistent-filter nil
206 "When set, keep filters from one agenda view to the next."
207 :group 'org-agenda
208 :type 'boolean)
209
20908596
CD
210(defgroup org-agenda-custom-commands nil
211 "Options concerning agenda views in Org-mode."
212 :tag "Org Agenda Custom Commands"
213 :group 'org-agenda)
214
215(defconst org-sorting-choice
216 '(choice
217 (const time-up) (const time-down)
218 (const category-keep) (const category-up) (const category-down)
219 (const tag-down) (const tag-up)
220 (const priority-up) (const priority-down)
621f83e4 221 (const todo-state-up) (const todo-state-down)
c8d0cf5c 222 (const effort-up) (const effort-down)
8bfe682a 223 (const habit-up) (const habit-down)
86fbb8ca 224 (const alpha-up) (const alpha-down)
c8d0cf5c 225 (const user-defined-up) (const user-defined-down))
20908596
CD
226 "Sorting choices.")
227
228(defconst org-agenda-custom-commands-local-options
229 `(repeat :tag "Local settings for this command. Remember to quote values"
230 (choice :tag "Setting"
c8d0cf5c
CD
231 (list :tag "Heading for this block"
232 (const org-agenda-overriding-header)
233 (string :tag "Headline"))
20908596
CD
234 (list :tag "Files to be searched"
235 (const org-agenda-files)
236 (list
237 (const :format "" quote)
c8d0cf5c 238 (repeat (file))))
20908596
CD
239 (list :tag "Sorting strategy"
240 (const org-agenda-sorting-strategy)
241 (list
242 (const :format "" quote)
243 (repeat
244 ,org-sorting-choice)))
245 (list :tag "Prefix format"
246 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
247 (string))
248 (list :tag "Number of days in agenda"
acedf35c
CD
249 (const org-agenda-span)
250 (choice (const :tag "Day" 'day)
251 (const :tag "Week" 'week)
252 (const :tag "Month" 'month)
253 (const :tag "Year" 'year)
254 (integer :tag "Custom")))
20908596
CD
255 (list :tag "Fixed starting date"
256 (const org-agenda-start-day)
257 (string :value "2007-11-01"))
258 (list :tag "Start on day of week"
259 (const org-agenda-start-on-weekday)
260 (choice :value 1
261 (const :tag "Today" nil)
c8d0cf5c 262 (integer :tag "Weekday No.")))
20908596
CD
263 (list :tag "Include data from diary"
264 (const org-agenda-include-diary)
265 (boolean))
266 (list :tag "Deadline Warning days"
267 (const org-deadline-warning-days)
268 (integer :value 1))
c8d0cf5c
CD
269 (list :tag "Tags filter preset"
270 (const org-agenda-filter-preset)
271 (list
272 (const :format "" quote)
273 (repeat
274 (string :tag "+tag or -tag"))))
ed21c5c8
CD
275 (list :tag "Set daily/weekly entry types"
276 (const org-agenda-entry-types)
277 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
278 (const :deadline)
279 (const :scheduled)
280 (const :timestamp)
281 (const :sexp)))
20908596
CD
282 (list :tag "Standard skipping condition"
283 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
284 (const org-agenda-skip-function)
285 (list
286 (const :format "" quote)
287 (list
288 (choice
33306645 289 :tag "Skipping range"
20908596
CD
290 (const :tag "Skip entry" org-agenda-skip-entry-if)
291 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
292 (repeat :inline t :tag "Conditions for skipping"
293 (choice
294 :tag "Condition type"
295 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
296 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
ed21c5c8
CD
297 (list :tag "TODO state is" :inline t
298 (const 'todo)
299 (choice
300 (const :tag "any not-done state" 'todo)
301 (const :tag "any done state" 'done)
302 (const :tag "any state" 'any)
303 (list :tag "Keyword list"
304 (const :format "" quote)
305 (repeat (string :tag "Keyword")))))
306 (list :tag "TODO state is not" :inline t
307 (const 'nottodo)
308 (choice
309 (const :tag "any not-done state" 'todo)
310 (const :tag "any done state" 'done)
311 (const :tag "any state" 'any)
312 (list :tag "Keyword list"
313 (const :format "" quote)
314 (repeat (string :tag "Keyword")))))
20908596
CD
315 (const :tag "scheduled" 'scheduled)
316 (const :tag "not scheduled" 'notscheduled)
317 (const :tag "deadline" 'deadline)
c8d0cf5c
CD
318 (const :tag "no deadline" 'notdeadline)
319 (const :tag "timestamp" 'timestamp)
320 (const :tag "no timestamp" 'nottimestamp))))))
20908596
CD
321 (list :tag "Non-standard skipping condition"
322 :value (org-agenda-skip-function)
2c3ad40d 323 (const org-agenda-skip-function)
c8d0cf5c
CD
324 (sexp :tag "Function or form (quoted!)"))
325 (list :tag "Any variable"
326 (variable :tag "Variable")
327 (sexp :tag "Value (sexp)"))))
20908596
CD
328 "Selection of examples for agenda command settings.
329This will be spliced into the custom type of
330`org-agenda-custom-commands'.")
331
332
333(defcustom org-agenda-custom-commands nil
334 "Custom commands for the agenda.
335These commands will be offered on the splash screen displayed by the
336agenda dispatcher \\[org-agenda]. Each entry is a list like this:
337
338 (key desc type match settings files)
339
340key The key (one or more characters as a string) to be associated
341 with the command.
7877f373 342desc A description of the command; when omitted or nil, a default
20908596
CD
343 description is built using MATCH.
344type The command type, any of the following symbols:
345 agenda The daily/weekly agenda.
346 todo Entries with a specific TODO keyword, in all agenda files.
347 search Entries containing search words entry or headline.
348 tags Tags/Property/TODO match in all agenda files.
349 tags-todo Tags/P/T match in all agenda files, TODO entries only.
350 todo-tree Sparse tree of specific TODO keyword in *current* file.
351 tags-tree Sparse tree with all tags matches in *current* file.
352 occur-tree Occur sparse tree for *current* file.
353 ... A user-defined function.
354match What to search for:
355 - a single keyword for TODO keyword searches
356 - a tags match expression for tags searches
7877f373 357 - a word search expression for text searches
20908596
CD
358 - a regular expression for occur searches
359 For all other commands, this should be the empty string.
360settings A list of option settings, similar to that in a let form, so like
361 this: ((opt1 val1) (opt2 val2) ...). The values will be
362 evaluated at the moment of execution, so quote them when needed.
363files A list of files file to write the produced agenda buffer to
364 with the command `org-store-agenda-views'.
365 If a file name ends in \".html\", an HTML version of the buffer
7877f373 366 is written out. If it ends in \".ps\", a PostScript version is
33306645 367 produced. Otherwise, only the plain text is written to the file.
20908596
CD
368
369You can also define a set of commands, to create a composite agenda buffer.
370In this case, an entry looks like this:
371
372 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
373
374where
375
376desc A description string to be displayed in the dispatcher menu.
377cmd An agenda command, similar to the above. However, tree commands
378 are no allowed, but instead you can get agenda and global todo list.
379 So valid commands for a set are:
380 (agenda \"\" settings)
381 (alltodo \"\" settings)
382 (stuck \"\" settings)
383 (todo \"match\" settings files)
384 (search \"match\" settings files)
385 (tags \"match\" settings files)
386 (tags-todo \"match\" settings files)
387
388Each command can carry a list of options, and another set of options can be
389given for the whole set of commands. Individual command options take
390precedence over the general options.
391
392When using several characters as key to a command, the first characters
393are prefix commands. For the dispatcher to display useful information, you
394should provide a description for the prefix, like
395
396 (setq org-agenda-custom-commands
397 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
398 (\"hl\" tags \"+HOME+Lisa\")
399 (\"hp\" tags \"+HOME+Peter\")
400 (\"hk\" tags \"+HOME+Kim\")))"
401 :group 'org-agenda-custom-commands
402 :type `(repeat
403 (choice :value ("x" "Describe command here" tags "" nil)
404 (list :tag "Single command"
405 (string :tag "Access Key(s) ")
406 (option (string :tag "Description"))
407 (choice
408 (const :tag "Agenda" agenda)
409 (const :tag "TODO list" alltodo)
410 (const :tag "Search words" search)
411 (const :tag "Stuck projects" stuck)
c8d0cf5c
CD
412 (const :tag "Tags/Property match (all agenda files)" tags)
413 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
20908596
CD
414 (const :tag "TODO keyword search (all agenda files)" todo)
415 (const :tag "Tags sparse tree (current buffer)" tags-tree)
416 (const :tag "TODO keyword tree (current buffer)" todo-tree)
417 (const :tag "Occur tree (current buffer)" occur-tree)
418 (sexp :tag "Other, user-defined function"))
419 (string :tag "Match (only for some commands)")
420 ,org-agenda-custom-commands-local-options
421 (option (repeat :tag "Export" (file :tag "Export to"))))
422 (list :tag "Command series, all agenda files"
423 (string :tag "Access Key(s)")
424 (string :tag "Description ")
425 (repeat :tag "Component"
426 (choice
427 (list :tag "Agenda"
428 (const :format "" agenda)
429 (const :tag "" :format "" "")
430 ,org-agenda-custom-commands-local-options)
431 (list :tag "TODO list (all keywords)"
432 (const :format "" alltodo)
433 (const :tag "" :format "" "")
434 ,org-agenda-custom-commands-local-options)
435 (list :tag "Search words"
436 (const :format "" search)
437 (string :tag "Match")
438 ,org-agenda-custom-commands-local-options)
439 (list :tag "Stuck projects"
440 (const :format "" stuck)
441 (const :tag "" :format "" "")
442 ,org-agenda-custom-commands-local-options)
443 (list :tag "Tags search"
444 (const :format "" tags)
445 (string :tag "Match")
446 ,org-agenda-custom-commands-local-options)
447 (list :tag "Tags search, TODO entries only"
448 (const :format "" tags-todo)
449 (string :tag "Match")
450 ,org-agenda-custom-commands-local-options)
451 (list :tag "TODO keyword search"
452 (const :format "" todo)
453 (string :tag "Match")
454 ,org-agenda-custom-commands-local-options)
455 (list :tag "Other, user-defined function"
456 (symbol :tag "function")
457 (string :tag "Match")
458 ,org-agenda-custom-commands-local-options)))
459
460 (repeat :tag "Settings for entire command set"
461 (list (variable :tag "Any variable")
462 (sexp :tag "Value")))
463 (option (repeat :tag "Export" (file :tag "Export to"))))
464 (cons :tag "Prefix key documentation"
465 (string :tag "Access Key(s)")
466 (string :tag "Description ")))))
467
468(defcustom org-agenda-query-register ?o
469 "The register holding the current query string.
33306645 470The purpose of this is that if you construct a query string interactively,
20908596
CD
471you can then use it to define a custom command."
472 :group 'org-agenda-custom-commands
473 :type 'character)
474
475(defcustom org-stuck-projects
476 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
477 "How to identify stuck projects.
478This is a list of four items:
c8d0cf5c
CD
4791. A tags/todo/property matcher string that is used to identify a project.
480 See the manual for a description of tag and property searches.
20908596
CD
481 The entire tree below a headline matched by this is considered one project.
4822. A list of TODO keywords identifying non-stuck projects.
483 If the project subtree contains any headline with one of these todo
484 keywords, the project is considered to be not stuck. If you specify
485 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
4863. A list of tags identifying non-stuck projects.
487 If the project subtree contains any headline with one of these tags,
488 the project is considered to be not stuck. If you specify \"*\" as
c8d0cf5c
CD
489 a tag, any tag will mark the project unstuck. Note that this is about
490 the explicit presence of a tag somewhere in the subtree, inherited
491 tags to not count here. If inherited tags make a project not stuck,
492 use \"-TAG\" in the tags part of the matcher under (1.) above.
20908596
CD
4934. An arbitrary regular expression matching non-stuck projects.
494
c8d0cf5c
CD
495If the project turns out to be not stuck, search continues also in the
496subtree to see if any of the subtasks have project status.
497
498See also the variable `org-tags-match-list-sublevels' which applies
499to projects matched by this search as well.
500
20908596
CD
501After defining this variable, you may use \\[org-agenda-list-stuck-projects]
502or `C-c a #' to produce the list."
503 :group 'org-agenda-custom-commands
504 :type '(list
505 (string :tag "Tags/TODO match to identify a project")
506 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
507 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
c8d0cf5c 508 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
20908596 509
71d35b24
CD
510(defcustom org-agenda-filter-effort-default-operator "<"
511 "The default operator for effort estimate filtering.
93b62de8 512If you select an effort estimate limit without first pressing an operator,
71d35b24
CD
513this one will be used."
514 :group 'org-agenda-custom-commands
515 :type '(choice (const :tag "less or equal" "<")
516 (const :tag "greater or equal"">")
517 (const :tag "equal" "=")))
20908596
CD
518
519(defgroup org-agenda-skip nil
520 "Options concerning skipping parts of agenda files."
521 :tag "Org Agenda Skip"
522 :group 'org-agenda)
3ab2c837
BG
523
524(defcustom org-agenda-skip-function-global nil
525 "Function to be called at each match during agenda construction.
526If this function returns nil, the current match should not be skipped.
527If the function decided to skip an agenda match, is must return the
528buffer position from which the search should be continued.
529This may also be a Lisp form, which will be evaluated.
530
531This variable will be applied to every agenda match, including
532tags/property searches and TODO lists. So try to make the test function
533do its checking as efficiently as possible. To implement a skipping
534condition just for specific agenda commands, use the variable
535`org-agenda-skip-function' which can be set in the options section
536of custom agenda commands."
537 :group 'org-agenda-skip
538 :type 'sexp)
539
0bd48b37
CD
540(defgroup org-agenda-daily/weekly nil
541 "Options concerning the daily/weekly agenda."
542 :tag "Org Agenda Daily/Weekly"
543 :group 'org-agenda)
544(defgroup org-agenda-todo-list nil
545 "Options concerning the global todo list agenda view."
546 :tag "Org Agenda Todo List"
547 :group 'org-agenda)
548(defgroup org-agenda-match-view nil
549 "Options concerning the general tags/property/todo match agenda view."
550 :tag "Org Agenda Match View"
551 :group 'org-agenda)
8bfe682a
CD
552(defgroup org-agenda-search-view nil
553 "Options concerning the general tags/property/todo match agenda view."
554 :tag "Org Agenda Match View"
555 :group 'org-agenda)
20908596 556
2c3ad40d 557(defvar org-agenda-archives-mode nil
ed21c5c8 558 "Non-nil means the agenda will include archived items.
2c3ad40d
CD
559If this is the symbol `trees', trees in the selected agenda scope
560that are marked with the ARCHIVE tag will be included anyway. When this is
561t, also all archive files associated with the current selection of agenda
562files will be included.")
563
b349f79f 564(defcustom org-agenda-skip-comment-trees t
ed21c5c8 565 "Non-nil means skip trees that start with the COMMENT keyword.
33306645 566When nil, these trees are also scanned by agenda commands."
b349f79f
CD
567 :group 'org-agenda-skip
568 :type 'boolean)
569
20908596 570(defcustom org-agenda-todo-list-sublevels t
ed21c5c8 571 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
20908596
CD
572When nil, the sublevels of a TODO entry are not checked, resulting in
573potentially much shorter TODO lists."
574 :group 'org-agenda-skip
0bd48b37 575 :group 'org-agenda-todo-list
20908596
CD
576 :type 'boolean)
577
578(defcustom org-agenda-todo-ignore-with-date nil
ed21c5c8 579 "Non-nil means don't show entries with a date in the global todo list.
20908596
CD
580You can use this if you prefer to mark mere appointments with a TODO keyword,
581but don't want them to show up in the TODO list.
582When this is set, it also covers deadlines and scheduled items, the settings
583of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
c8d0cf5c
CD
584will be ignored.
585See also the variable `org-agenda-tags-todo-honor-ignore-options'."
20908596 586 :group 'org-agenda-skip
0bd48b37 587 :group 'org-agenda-todo-list
20908596
CD
588 :type 'boolean)
589
acedf35c
CD
590(defcustom org-agenda-todo-ignore-timestamp nil
591 "Non-nil means don't show entries with a timestamp.
592This applies when creating the global todo list.
593Valid values are:
594
595past Don't show entries for today or in the past.
596
597future Don't show entries with a timestamp in the future.
598 The idea behind this is that if it has a future
599 timestamp, you don't want to think about it until the
600 date.
601
602all Don't show any entries with a timestamp in the global todo list.
603 The idea behind this is that by setting a timestamp, you
604 have already \"taken care\" of this item.
605
3ab2c837
BG
606This variable can also have an integer as a value. If positive (N),
607todos with a timestamp N or more days in the future will be ignored. If
608negative (-N), todos with a timestamp N or more days in the past will be
609ignored. If 0, todos with a timestamp either today or in the future will
610be ignored. For example, a value of -1 will exclude todos with a
611timestamp in the past (yesterday or earlier), while a value of 7 will
612exclude todos with a timestamp a week or more in the future.
613
acedf35c
CD
614See also `org-agenda-todo-ignore-with-date'.
615See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
616to make his option also apply to the tags-todo list."
617 :group 'org-agenda-skip
618 :group 'org-agenda-todo-list
619 :type '(choice
620 (const :tag "Ignore future timestamp todos" future)
621 (const :tag "Ignore past or present timestamp todos" past)
622 (const :tag "Ignore all timestamp todos" all)
3ab2c837
BG
623 (const :tag "Show timestamp todos" nil)
624 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
acedf35c 625
20908596 626(defcustom org-agenda-todo-ignore-scheduled nil
ed21c5c8
CD
627 "Non-nil means, ignore some scheduled TODO items when making TODO list.
628This applies when creating the global todo list.
629Valid values are:
630
631past Don't show entries scheduled today or in the past.
632
633future Don't show entries scheduled in the future.
634 The idea behind this is that by scheduling it, you don't want to
635 think about it until the scheduled date.
636
637all Don't show any scheduled entries in the global todo list.
638 The idea behind this is that by scheduling it, you have already
639 \"taken care\" of this item.
640
641t Same as `all', for backward compatibility.
642
3ab2c837
BG
643This variable can also have an integer as a value. See
644`org-agenda-todo-ignore-timestamp' for more details.
645
c8d0cf5c 646See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
647See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
648to make his option also apply to the tags-todo list."
20908596 649 :group 'org-agenda-skip
0bd48b37 650 :group 'org-agenda-todo-list
ed21c5c8
CD
651 :type '(choice
652 (const :tag "Ignore future-scheduled todos" future)
653 (const :tag "Ignore past- or present-scheduled todos" past)
654 (const :tag "Ignore all scheduled todos" all)
655 (const :tag "Ignore all scheduled todos (compatibility)" t)
3ab2c837
BG
656 (const :tag "Show scheduled todos" nil)
657 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
20908596
CD
658
659(defcustom org-agenda-todo-ignore-deadlines nil
ed21c5c8
CD
660 "Non-nil means ignore some deadlined TODO items when making TODO list.
661There are different motivations for using different values, please think
662carefully when configuring this variable.
663
86fbb8ca 664This applies when creating the global todo list.
ed21c5c8
CD
665Valid values are:
666
667near Don't show near deadline entries. A deadline is near when it is
668 closer than `org-deadline-warning-days' days. The idea behind this
669 is that such items will appear in the agenda anyway.
670
671far Don't show TODO entries where a deadline has been defined, but
672 the deadline is not near. This is useful if you don't want to
673 use the todo list to figure out what to do now.
674
675past Don't show entries with a deadline timestamp for today or in the past.
676
677future Don't show entries with a deadline timestamp in the future, not even
678 when they become `near' ones. Use it with caution.
679
680all Ignore all TODO entries that do have a deadline.
681
682t Same as `near', for backward compatibility.
683
3ab2c837
BG
684This variable can also have an integer as a value. See
685`org-agenda-todo-ignore-timestamp' for more details.
686
c8d0cf5c 687See also `org-agenda-todo-ignore-with-date'.
ed21c5c8
CD
688See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
689to make his option also apply to the tags-todo list."
20908596 690 :group 'org-agenda-skip
0bd48b37 691 :group 'org-agenda-todo-list
ed21c5c8
CD
692 :type '(choice
693 (const :tag "Ignore near deadlines" near)
694 (const :tag "Ignore near deadlines (compatibility)" t)
695 (const :tag "Ignore far deadlines" far)
696 (const :tag "Ignore all TODOs with a deadlines" all)
3ab2c837
BG
697 (const :tag "Show all TODOs, even if they have a deadline" nil)
698 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
0bd48b37
CD
699
700(defcustom org-agenda-tags-todo-honor-ignore-options nil
ed21c5c8 701 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
0bd48b37
CD
702The variables
703 `org-agenda-todo-ignore-with-date',
acedf35c
CD
704 `org-agenda-todo-ignore-timestamp',
705 `org-agenda-todo-ignore-scheduled',
0bd48b37
CD
706 `org-agenda-todo-ignore-deadlines'
707make the global TODO list skip entries that have time stamps of certain
708kinds. If this option is set, the same options will also apply for the
709tags-todo search, which is the general tags/property matcher
710restricted to unfinished TODO entries only."
711 :group 'org-agenda-skip
712 :group 'org-agenda-todo-list
713 :group 'org-agenda-match-view
20908596
CD
714 :type 'boolean)
715
716(defcustom org-agenda-skip-scheduled-if-done nil
717 "Non-nil means don't show scheduled items in agenda when they are done.
718This is relevant for the daily/weekly agenda, not for the TODO list. And
719it applies only to the actual date of the scheduling. Warnings about
720an item with a past scheduling dates are always turned off when the item
721is DONE."
722 :group 'org-agenda-skip
0bd48b37 723 :group 'org-agenda-daily/weekly
20908596
CD
724 :type 'boolean)
725
54a0dee5
CD
726(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
727 "Non-nil means skip scheduling line if same entry shows because of deadline.
728In the agenda of today, an entry can show up multiple times because
729it is both scheduled and has a nearby deadline, and maybe a plain time
730stamp as well.
731When this variable is t, then only the deadline is shown and the fact that
732the entry is scheduled today or was scheduled previously is not shown.
733When this variable is nil, the entry will be shown several times. When
734the variable is the symbol `not-today', then skip scheduled previously,
735but not scheduled today."
736 :group 'org-agenda-skip
737 :group 'org-agenda-daily/weekly
738 :type '(choice
739 (const :tag "Never" nil)
740 (const :tag "Always" t)
741 (const :tag "Not when scheduled today" not-today)))
742
20908596 743(defcustom org-agenda-skip-deadline-if-done nil
33306645 744 "Non-nil means don't show deadlines when the corresponding item is done.
20908596
CD
745When nil, the deadline is still shown and should give you a happy feeling.
746This is relevant for the daily/weekly agenda. And it applied only to the
33306645 747actually date of the deadline. Warnings about approaching and past-due
20908596
CD
748deadlines are always turned off when the item is DONE."
749 :group 'org-agenda-skip
0bd48b37 750 :group 'org-agenda-daily/weekly
20908596
CD
751 :type 'boolean)
752
ed21c5c8
CD
753(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
754 "Non-nil means skip deadline prewarning when entry is also scheduled.
755This will apply on all days where a prewarning for the deadline would
756be shown, but not at the day when the entry is actually due. On that day,
757the deadline will be shown anyway.
758This variable may be set to nil, t, or a number which will then give
759the number of days before the actual deadline when the prewarnings
760should resume.
761This can be used in a workflow where the first showing of the deadline will
762trigger you to schedule it, and then you don't want to be reminded of it
763because you will take care of it on the day when scheduled."
764 :group 'org-agenda-skip
765 :group 'org-agenda-daily/weekly
766 :type '(choice
fe3c5669 767 (const :tag "Always show prewarning" nil)
ed21c5c8
CD
768 (const :tag "Remove prewarning if entry is scheduled" t)
769 (integer :tag "Restart prewarning N days before deadline")))
770
c8d0cf5c
CD
771(defcustom org-agenda-skip-additional-timestamps-same-entry t
772 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
773When non-nil, after the search for timestamps has matched once in an
774entry, the rest of the entry will not be searched."
775 :group 'org-agenda-skip
776 :type 'boolean)
777
20908596
CD
778(defcustom org-agenda-skip-timestamp-if-done nil
779 "Non-nil means don't select item by timestamp or -range if it is DONE."
780 :group 'org-agenda-skip
0bd48b37 781 :group 'org-agenda-daily/weekly
20908596
CD
782 :type 'boolean)
783
d6685abc 784(defcustom org-agenda-dim-blocked-tasks t
ed21c5c8 785 "Non-nil means dim blocked tasks in the agenda display.
c8d0cf5c
CD
786This causes some overhead during agenda construction, but if you
787have turned on `org-enforce-todo-dependencies',
788`org-enforce-todo-checkbox-dependencies', or any other blocking
789mechanism, this will create useful feedback in the agenda.
790
8bfe682a 791Instead of t, this variable can also have the value `invisible'.
c8d0cf5c
CD
792Then blocked tasks will be invisible and only become visible when
793they become unblocked. An exemption to this behavior is when a task is
794blocked because of unchecked checkboxes below it. Since checkboxes do
795not show up in the agenda views, making this task invisible you remove any
796trace from agenda views that there is something to do. Therefore, a task
797that is blocked because of checkboxes will never be made invisible, it
798will only be dimmed."
d6685abc
CD
799 :group 'org-agenda-daily/weekly
800 :group 'org-agenda-todo-list
801 :type '(choice
802 (const :tag "Do not dim" nil)
803 (const :tag "Dim to a grey face" t)
8bfe682a 804 (const :tag "Make invisible" invisible)))
d6685abc 805
20908596 806(defcustom org-timeline-show-empty-dates 3
ed21c5c8 807 "Non-nil means `org-timeline' also shows dates without an entry.
20908596
CD
808When nil, only the days which actually have entries are shown.
809When t, all days between the first and the last date are shown.
810When an integer, show also empty dates, but if there is a gap of more than
811N days, just insert a special line indicating the size of the gap."
812 :group 'org-agenda-skip
813 :type '(choice
814 (const :tag "None" nil)
815 (const :tag "All" t)
c8d0cf5c 816 (integer :tag "at most")))
20908596 817
20908596
CD
818(defgroup org-agenda-startup nil
819 "Options concerning initial settings in the Agenda in Org Mode."
820 :tag "Org Agenda Startup"
821 :group 'org-agenda)
822
afe98dfa 823(defcustom org-agenda-menu-show-matcher t
3ab2c837 824 "Non-nil means show the match string in the agenda dispatcher menu.
afe98dfa
CD
825When nil, the matcher string is not shown, but is put into the help-echo
826property so than moving the mouse over the command shows it.
827Setting it to nil is good if matcher strings are very long and/or if
3ab2c837 828you want to use two-column display (see `org-agenda-menu-two-column')."
afe98dfa
CD
829 :group 'org-agenda
830 :type 'boolean)
831
832(defcustom org-agenda-menu-two-column nil
833 "Non-nil means, use two columns to show custom commands in the dispatcher.
834If you use this, you probably want to set `org-agenda-menu-show-matcher'
835to nil."
836 :group 'org-agenda
837 :type 'boolean)
838
20908596
CD
839(defcustom org-finalize-agenda-hook nil
840 "Hook run just before displaying an agenda buffer."
841 :group 'org-agenda-startup
842 :type 'hook)
843
844(defcustom org-agenda-mouse-1-follows-link nil
ed21c5c8 845 "Non-nil means mouse-1 on a link will follow the link in the agenda.
20908596
CD
846A longer mouse click will still set point. Does not work on XEmacs.
847Needs to be set before org.el is loaded."
848 :group 'org-agenda-startup
849 :type 'boolean)
850
851(defcustom org-agenda-start-with-follow-mode nil
86fbb8ca 852 "The initial value of follow mode in a newly created agenda window."
20908596
CD
853 :group 'org-agenda-startup
854 :type 'boolean)
855
1bcdebed 856(defcustom org-agenda-show-outline-path t
ed21c5c8 857 "Non-nil means show outline path in echo area after line motion."
1bcdebed
CD
858 :group 'org-agenda-startup
859 :type 'boolean)
860
54a0dee5
CD
861(defcustom org-agenda-start-with-entry-text-mode nil
862 "The initial value of entry-text-mode in a newly created agenda window."
863 :group 'org-agenda-startup
864 :type 'boolean)
865
866(defcustom org-agenda-entry-text-maxlines 5
8bfe682a 867 "Number of text lines to be added when `E' is pressed in the agenda.
54a0dee5
CD
868
869Note that this variable only used during agenda display. Add add entry text
870when exporting the agenda, configure the variable
871`org-agenda-add-entry-ext-maxlines'."
872 :group 'org-agenda
873 :type 'integer)
874
8d642074
CD
875(defcustom org-agenda-entry-text-exclude-regexps nil
876 "List of regular expressions to clean up entry text.
877The complete matches of all regular expressions in this list will be
878removed from entry text before it is shown in the agenda."
879 :group 'org-agenda
880 :type '(repeat (regexp)))
881
882(defvar org-agenda-entry-text-cleanup-hook nil
883 "Hook that is run after basic cleanup of entry text to be shown in agenda.
884This cleanup is done in a temporary buffer, so the function may inspect and
885change the entire buffer.
886Some default stuff like drawers and scheduling/deadline dates will already
887have been removed when this is called, as will any matches for regular
888expressions listed in `org-agenda-entry-text-exclude-regexps'.")
889
20908596 890(defvar org-agenda-include-inactive-timestamps nil
ed21c5c8 891 "Non-nil means include inactive time stamps in agenda and timeline.")
20908596
CD
892
893(defgroup org-agenda-windows nil
894 "Options concerning the windows used by the Agenda in Org Mode."
895 :tag "Org Agenda Windows"
896 :group 'org-agenda)
897
898(defcustom org-agenda-window-setup 'reorganize-frame
899 "How the agenda buffer should be displayed.
900Possible values for this option are:
901
902current-window Show agenda in the current window, keeping all other windows.
20908596
CD
903other-window Use `switch-to-buffer-other-window' to display agenda.
904reorganize-frame Show only two windows on the current frame, the current
905 window and the agenda.
8d642074
CD
906other-frame Use `switch-to-buffer-other-frame' to display agenda.
907 Also, when exiting the agenda, kill that frame.
20908596
CD
908See also the variable `org-agenda-restore-windows-after-quit'."
909 :group 'org-agenda-windows
910 :type '(choice
911 (const current-window)
912 (const other-frame)
913 (const other-window)
914 (const reorganize-frame)))
915
916(defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
917 "The min and max height of the agenda window as a fraction of frame height.
918The value of the variable is a cons cell with two numbers between 0 and 1.
919It only matters if `org-agenda-window-setup' is `reorganize-frame'."
920 :group 'org-agenda-windows
921 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
922
923(defcustom org-agenda-restore-windows-after-quit nil
3ab2c837 924 "Non-nil means restore window configuration upon exiting agenda.
20908596
CD
925Before the window configuration is changed for displaying the agenda,
926the current status is recorded. When the agenda is exited with
927`q' or `x' and this option is set, the old state is restored. If
928`org-agenda-window-setup' is `other-frame', the value of this
baf0cb84 929option will be ignored."
20908596
CD
930 :group 'org-agenda-windows
931 :type 'boolean)
932
acedf35c
CD
933(defcustom org-agenda-ndays nil
934 "Number of days to include in overview display.
c8d0cf5c 935Should be 1 or 7.
acedf35c
CD
936Obsolete, see `org-agenda-span'."
937 :group 'org-agenda-daily/weekly
938 :type 'integer)
939
940(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
941
942(defcustom org-agenda-span 'week
943 "Number of days to include in overview display.
944Can be day, week, month, year, or any number of days.
c8d0cf5c 945Custom commands can set this variable in the options section."
20908596 946 :group 'org-agenda-daily/weekly
acedf35c
CD
947 :type '(choice (const :tag "Day" day)
948 (const :tag "Week" week)
949 (const :tag "Month" month)
950 (const :tag "Year" year)
951 (integer :tag "Custom")))
20908596
CD
952
953(defcustom org-agenda-start-on-weekday 1
ed21c5c8 954 "Non-nil means start the overview always on the specified weekday.
20908596 9550 denotes Sunday, 1 denotes Monday etc.
c8d0cf5c
CD
956When nil, always start on the current day.
957Custom commands can set this variable in the options section."
20908596
CD
958 :group 'org-agenda-daily/weekly
959 :type '(choice (const :tag "Today" nil)
c8d0cf5c 960 (integer :tag "Weekday No.")))
20908596
CD
961
962(defcustom org-agenda-show-all-dates t
ed21c5c8 963 "Non-nil means `org-agenda' shows every day in the selected range.
20908596
CD
964When nil, only the days which actually have entries are shown."
965 :group 'org-agenda-daily/weekly
966 :type 'boolean)
967
968(defcustom org-agenda-format-date 'org-agenda-format-date-aligned
969 "Format string for displaying dates in the agenda.
970Used by the daily/weekly agenda and by the timeline. This should be
971a format string understood by `format-time-string', or a function returning
972the formatted date as a string. The function must take a single argument,
973a calendar-style date list like (month day year)."
974 :group 'org-agenda-daily/weekly
975 :type '(choice
976 (string :tag "Format string")
977 (function :tag "Function")))
978
979(defun org-agenda-format-date-aligned (date)
980 "Format a date string for display in the daily/weekly agenda, or timeline.
981This function makes sure that dates are aligned for easy reading."
982 (require 'cal-iso)
983 (let* ((dayname (calendar-day-name date))
984 (day (cadr date))
985 (day-of-week (calendar-day-of-week date))
986 (month (car date))
987 (monthname (calendar-month-name month))
988 (year (nth 2 date))
989 (iso-week (org-days-to-iso-week
990 (calendar-absolute-from-gregorian date)))
991 (weekyear (cond ((and (= month 1) (>= iso-week 52))
992 (1- year))
993 ((and (= month 12) (<= iso-week 1))
994 (1+ year))
995 (t year)))
996 (weekstring (if (= day-of-week 1)
997 (format " W%02d" iso-week)
998 "")))
999 (format "%-10s %2d %s %4d%s"
1000 dayname day monthname year weekstring)))
1001
ed21c5c8
CD
1002(defcustom org-agenda-time-leading-zero nil
1003 "Non-nil means use leading zero for military times in agenda.
1004For example, 9:30am would become 09:30 rather than 9:30."
1005 :group 'org-agenda-daily/weekly
1006 :type 'boolean)
1007
acedf35c
CD
1008(defcustom org-agenda-timegrid-use-ampm nil
1009 "When set, show AM/PM style timestamps on the timegrid."
1010 :group 'org-agenda
1011 :type 'boolean)
1012
1013(defun org-agenda-time-of-day-to-ampm (time)
1014 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1015 (let* ((hour-number (string-to-number (substring time 0 -3)))
1016 (minute (substring time -2))
1017 (ampm "am"))
1018 (cond
1019 ((equal hour-number 12)
1020 (setq ampm "pm"))
1021 ((> hour-number 12)
1022 (setq ampm "pm")
1023 (setq hour-number (- hour-number 12))))
1024 (concat
1025 (if org-agenda-time-leading-zero
1026 (format "%02d" hour-number)
1027 (format "%02s" (number-to-string hour-number)))
1028 ":" minute ampm)))
1029
1030(defun org-agenda-time-of-day-to-ampm-maybe (time)
1031 "Conditionally convert TIME to AM/PM format
1032based on `org-agenda-timegrid-use-ampm'"
1033 (if org-agenda-timegrid-use-ampm
1034 (org-agenda-time-of-day-to-ampm time)
1035 time))
1036
20908596
CD
1037(defcustom org-agenda-weekend-days '(6 0)
1038 "Which days are weekend?
1039These days get the special face `org-agenda-date-weekend' in the agenda
1040and timeline buffers."
1041 :group 'org-agenda-daily/weekly
1042 :type '(set :greedy t
1043 (const :tag "Monday" 1)
1044 (const :tag "Tuesday" 2)
1045 (const :tag "Wednesday" 3)
1046 (const :tag "Thursday" 4)
1047 (const :tag "Friday" 5)
1048 (const :tag "Saturday" 6)
1049 (const :tag "Sunday" 0)))
1050
1051(defcustom org-agenda-include-diary nil
c8d0cf5c
CD
1052 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1053Custom commands can set this variable in the options section."
20908596
CD
1054 :group 'org-agenda-daily/weekly
1055 :type 'boolean)
1056
ed21c5c8
CD
1057(defcustom org-agenda-include-deadlines t
1058 "If non-nil, include entries within their deadline warning period.
1059Custom commands can set this variable in the options section."
1060 :group 'org-agenda-daily/weekly
1061 :type 'boolean)
1062
20908596
CD
1063(defcustom org-agenda-include-all-todo nil
1064 "Set means weekly/daily agenda will always contain all TODO entries.
1065The TODO entries will be listed at the top of the agenda, before
0bd48b37
CD
1066the entries for specific days.
1067This option is deprecated, it is better to define a block agenda instead."
20908596
CD
1068 :group 'org-agenda-daily/weekly
1069 :type 'boolean)
1070
1071(defcustom org-agenda-repeating-timestamp-show-all t
ed21c5c8 1072 "Non-nil means show all occurrences of a repeating stamp in the agenda.
3ab2c837
BG
1073When set to a list of strings, only show occurrences of repeating
1074stamps for these TODO keywords. When nil, only one occurrence is
1075shown, either today or the nearest into the future."
20908596 1076 :group 'org-agenda-daily/weekly
3ab2c837
BG
1077 :type '(choice
1078 (const :tag "Show repeating stamps" t)
1079 (repeat :tag "Show repeating stamps for these TODO keywords"
1080 (string :tag "TODO Keyword"))
1081 (const :tag "Don't show repeating stamps" nil)))
20908596
CD
1082
1083(defcustom org-scheduled-past-days 10000
1084 "No. of days to continue listing scheduled items that are not marked DONE.
1085When an item is scheduled on a date, it shows up in the agenda on this
1086day and will be listed until it is marked done for the number of days
1087given here."
1088 :group 'org-agenda-daily/weekly
c8d0cf5c 1089 :type 'integer)
20908596 1090
93b62de8
CD
1091(defcustom org-agenda-log-mode-items '(closed clock)
1092 "List of items that should be shown in agenda log mode.
1093This list may contain the following symbols:
1094
1095 closed Show entries that have been closed on that day.
1096 clock Show entries that have received clocked time on that day.
c8d0cf5c
CD
1097 state Show all logged state changes.
1098Note that instead of changing this variable, you can also press `C-u l' in
1099the agenda to display all available LOG items temporarily."
93b62de8
CD
1100 :group 'org-agenda-daily/weekly
1101 :type '(set :greedy t (const closed) (const clock) (const state)))
1102
3ab2c837
BG
1103(defcustom org-agenda-clock-consistency-checks
1104 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1105 :gap-ok-around ("4:00")
1106 :default-face ((:background "DarkRed") (:foreground "white"))
1107 :overlap-face nil :gap-face nil :no-end-time-face nil
1108 :long-face nil :short-face nil)
1109 "This is a property list, with the following keys:
1110
1111:max-duration Mark clocking chunks that are longer than this time.
1112 This is a time string like \"HH:MM\", or the number
1113 of minutes as an integer.
1114
1115:min-duration Mark clocking chunks that are shorter that this.
1116 This is a time string like \"HH:MM\", or the number
1117 of minutes as an integer.
1118
1119:max-gap Mark gaps between clocking chunks that are longer than
1120 this duration. A number of minutes, or a string
1121 like \"HH:MM\".
1122
1123:gap-ok-around List of times during the day which are usually not working
1124 times. When a gap is detected, but the gap contains any
1125 of these times, the gap is *not* reported. For example,
1126 if this is (\"4:00\" \"13:00\") then gaps that contain
1127 4:00 in the morning (i.e. the night) and 13:00
1128 (i.e. a typical lunch time) do not cause a warning.
1129 You should have at least one time during the night in this
1130 list, or otherwise the first task each morning will trigger
1131 a warning because it follows a long gap.
1132
1133Furthermore, the following properties can be used to define faces for
1134issue display.
1135
1136:default-face the default face, if the specific face is undefined
1137:overlap-face face for overlapping clocks
1138:gap-face face for gaps between clocks
1139:no-end-time-face face for incomplete clocks
1140:long-face face for clock intervals that are too long
1141:short-face face for clock intervals that are too short"
1142 :group 'org-agenda-daily/weekly
1143 :group 'org-clock
1144 :type 'plist)
1145
c8d0cf5c 1146(defcustom org-agenda-log-mode-add-notes t
ed21c5c8 1147 "Non-nil means add first line of notes to log entries in agenda views.
c8d0cf5c
CD
1148If a log item like a state change or a clock entry is associated with
1149notes, the first line of these notes will be added to the entry in the
1150agenda display."
1151 :group 'org-agenda-daily/weekly
1152 :type 'boolean)
1153
1154(defcustom org-agenda-start-with-log-mode nil
1155 "The initial value of log-mode in a newly created agenda window."
1156 :group 'org-agenda-startup
1157 :group 'org-agenda-daily/weekly
1158 :type 'boolean)
1159
20908596
CD
1160(defcustom org-agenda-start-with-clockreport-mode nil
1161 "The initial value of clockreport-mode in a newly created agenda window."
1162 :group 'org-agenda-startup
1163 :group 'org-agenda-daily/weekly
1164 :type 'boolean)
1165
1166(defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1167 "Property list with parameters for the clocktable in clockreport mode.
1168This is the display mode that shows a clock table in the daily/weekly
1169agenda, the properties for this dynamic block can be set here.
1170The usual clocktable parameters are allowed here, but you cannot set
1171the properties :name, :tstart, :tend, :block, and :scope - these will
1172be overwritten to make sure the content accurately reflects the
1173current display in the agenda."
1174 :group 'org-agenda-daily/weekly
1175 :type 'plist)
1176
ed21c5c8
CD
1177(defcustom org-agenda-search-view-always-boolean nil
1178 "Non-nil means the search string is interpreted as individual parts.
1179
1180The search string for search view can either be interpreted as a phrase,
1181or as a list of snippets that define a boolean search for a number of
1182strings.
1183
1184When this is non-nil, the string will be split on whitespace, and each
1185snippet will be searched individually, and all must match in order to
1186select an entry. A snippet is then a single string of non-white
1187characters, or a string in double quotes, or a regexp in {} braces.
86fbb8ca 1188If a snippet is preceded by \"-\", the snippet must *not* match.
ed21c5c8
CD
1189\"+\" is syntactic sugar for positive selection. Each snippet may
1190be found as a full word or a partial word, but see the variable
1191`org-agenda-search-view-force-full-words'.
1192
1193When this is nil, search will look for the entire search phrase as one,
1194with each space character matching any amount of whitespace, including
1195line breaks.
1196
1197Even when this is nil, you can still switch to Boolean search dynamically
86fbb8ca 1198by preceding the first snippet with \"+\" or \"-\". If the first snippet
ed21c5c8
CD
1199is a regexp marked with braces like \"{abc}\", this will also switch to
1200boolean search."
1201 :group 'org-agenda-search-view
1202 :type 'boolean)
1203
1204(if (fboundp 'defvaralias)
1205 (defvaralias 'org-agenda-search-view-search-words-only
1206 'org-agenda-search-view-always-boolean))
1207
1208(defcustom org-agenda-search-view-force-full-words nil
86fbb8ca 1209 "Non-nil means, search words must be matches as complete words.
ed21c5c8 1210When nil, they may also match part of a word."
8bfe682a
CD
1211 :group 'org-agenda-search-view
1212 :type 'boolean)
20908596
CD
1213
1214(defgroup org-agenda-time-grid nil
1215 "Options concerning the time grid in the Org-mode Agenda."
1216 :tag "Org Agenda Time Grid"
1217 :group 'org-agenda)
1218
c8d0cf5c 1219(defcustom org-agenda-search-headline-for-time t
ed21c5c8 1220 "Non-nil means search headline for a time-of-day.
c8d0cf5c
CD
1221If the headline contains a time-of-day in one format or another, it will
1222be used to sort the entry into the time sequence of items for a day.
1223Some people have time stamps in the headline that refer to the creation
1224time or so, and then this produces an unwanted side effect. If this is
1225the case for your, use this variable to turn off searching the headline
1226for a time."
1227 :group 'org-agenda-time-grid
1228 :type 'boolean)
1229
20908596 1230(defcustom org-agenda-use-time-grid t
ed21c5c8 1231 "Non-nil means show a time grid in the agenda schedule.
20908596
CD
1232A time grid is a set of lines for specific times (like every two hours between
12338:00 and 20:00). The items scheduled for a day at specific times are
1234sorted in between these lines.
1235For details about when the grid will be shown, and what it will look like, see
1236the variable `org-agenda-time-grid'."
1237 :group 'org-agenda-time-grid
1238 :type 'boolean)
1239
1240(defcustom org-agenda-time-grid
1241 '((daily today require-timed)
1242 "----------------"
1243 (800 1000 1200 1400 1600 1800 2000))
1244
1245 "The settings for time grid for agenda display.
1246This is a list of three items. The first item is again a list. It contains
1247symbols specifying conditions when the grid should be displayed:
1248
1249 daily if the agenda shows a single day
1250 weekly if the agenda shows an entire week
1251 today show grid on current date, independent of daily/weekly display
1252 require-timed show grid only if at least one item has a time specification
1253
b349f79f 1254The second item is a string which will be placed behind the grid time.
20908596
CD
1255
1256The third item is a list of integers, indicating the times that should have
1257a grid line."
1258 :group 'org-agenda-time-grid
1259 :type
1260 '(list
1261 (set :greedy t :tag "Grid Display Options"
1262 (const :tag "Show grid in single day agenda display" daily)
1263 (const :tag "Show grid in weekly agenda display" weekly)
1264 (const :tag "Always show grid for today" today)
1265 (const :tag "Show grid only if any timed entries are present"
1266 require-timed)
1267 (const :tag "Skip grid times already present in an entry"
1268 remove-match))
1269 (string :tag "Grid String")
1270 (repeat :tag "Grid Times" (integer :tag "Time"))))
1271
3ab2c837
BG
1272(defcustom org-agenda-show-current-time-in-grid t
1273 "Non-nil means show the current time in the time grid."
1274 :group 'org-agenda-time-grid
1275 :type 'boolean)
1276
1277(defcustom org-agenda-current-time-string
1278 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1279 "The string for the current time marker in the agenda."
1280 :group 'org-agenda-time-grid
1281 :type 'string)
1282
20908596
CD
1283(defgroup org-agenda-sorting nil
1284 "Options concerning sorting in the Org-mode Agenda."
1285 :tag "Org Agenda Sorting"
1286 :group 'org-agenda)
1287
1288(defcustom org-agenda-sorting-strategy
8bfe682a
CD
1289 '((agenda habit-down time-up priority-down category-keep)
1290 (todo priority-down category-keep)
1291 (tags priority-down category-keep)
20908596
CD
1292 (search category-keep))
1293 "Sorting structure for the agenda items of a single day.
1294This is a list of symbols which will be used in sequence to determine
1295if an entry should be listed before another entry. The following
1296symbols are recognized:
1297
c8d0cf5c
CD
1298time-up Put entries with time-of-day indications first, early first
1299time-down Put entries with time-of-day indications first, late first
1300category-keep Keep the default order of categories, corresponding to the
1301 sequence in `org-agenda-files'.
1302category-up Sort alphabetically by category, A-Z.
1303category-down Sort alphabetically by category, Z-A.
1304tag-up Sort alphabetically by last tag, A-Z.
1305tag-down Sort alphabetically by last tag, Z-A.
1306priority-up Sort numerically by priority, high priority last.
1307priority-down Sort numerically by priority, high priority first.
1308todo-state-up Sort by todo state, tasks that are done last.
1309todo-state-down Sort by todo state, tasks that are done first.
1310effort-up Sort numerically by estimated effort, high effort last.
1311effort-down Sort numerically by estimated effort, high effort first.
1312user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1313user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
8bfe682a
CD
1314habit-up Put entries that are habits first
1315habit-down Put entries that are habits last
86fbb8ca
CD
1316alpha-up Sort headlines alphabetically
1317alpha-down Sort headlines alphabetically, reversed
20908596
CD
1318
1319The different possibilities will be tried in sequence, and testing stops
1320if one comparison returns a \"not-equal\". For example, the default
1321 '(time-up category-keep priority-down)
1322means: Pull out all entries having a specified time of day and sort them,
1323in order to make a time schedule for the current day the first thing in the
1324agenda listing for the day. Of the entries without a time indication, keep
1325the grouped in categories, don't sort the categories, but keep them in
1326the sequence given in `org-agenda-files'. Within each category sort by
1327priority.
1328
1329Leaving out `category-keep' would mean that items will be sorted across
1330categories by priority.
1331
1332Instead of a single list, this can also be a set of list for specific
1333contents, with a context symbol in the car of the list, any of
8bfe682a 1334`agenda', `todo', `tags', `search' for the corresponding agenda views.
c8d0cf5c
CD
1335
1336Custom commands can bind this variable in the options section."
20908596
CD
1337 :group 'org-agenda-sorting
1338 :type `(choice
1339 (repeat :tag "General" ,org-sorting-choice)
1340 (list :tag "Individually"
1341 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1342 (repeat ,org-sorting-choice))
1343 (cons (const :tag "Strategy for TODO lists" todo)
1344 (repeat ,org-sorting-choice))
1345 (cons (const :tag "Strategy for Tags matches" tags)
8bfe682a
CD
1346 (repeat ,org-sorting-choice))
1347 (cons (const :tag "Strategy for search matches" search)
20908596
CD
1348 (repeat ,org-sorting-choice)))))
1349
c8d0cf5c
CD
1350(defcustom org-agenda-cmp-user-defined nil
1351 "A function to define the comparison `user-defined'.
1352This function must receive two arguments, agenda entry a and b.
1353If a>b, return +1. If a<b, return -1. If they are equal as seen by
1354the user comparison, return nil.
1355When this is defined, you can make `user-defined-up' and `user-defined-down'
1356part of an agenda sorting strategy."
1357 :group 'org-agenda-sorting
1358 :type 'symbol)
1359
20908596 1360(defcustom org-sort-agenda-notime-is-late t
ed21c5c8 1361 "Non-nil means items without time are considered late.
20908596
CD
1362This is only relevant for sorting. When t, items which have no explicit
1363time like 15:30 will be considered as 99:01, i.e. later than any items which
1364do have a time. When nil, the default time is before 0:00. You can use this
1365option to decide if the schedule for today should come before or after timeless
1366agenda entries."
1367 :group 'org-agenda-sorting
1368 :type 'boolean)
1369
1370(defcustom org-sort-agenda-noeffort-is-high t
ed21c5c8 1371 "Non-nil means items without effort estimate are sorted as high effort.
c8d0cf5c
CD
1372This also applies when filtering an agenda view with respect to the
1373< or > effort operator. Then, tasks with no effort defined will be treated
1374as tasks with high effort.
20908596
CD
1375When nil, such items are sorted as 0 minutes effort."
1376 :group 'org-agenda-sorting
1377 :type 'boolean)
1378
1379(defgroup org-agenda-line-format nil
1380 "Options concerning the entry prefix in the Org-mode agenda display."
1381 :tag "Org Agenda Line Format"
1382 :group 'org-agenda)
1383
1384(defcustom org-agenda-prefix-format
acedf35c 1385 '((agenda . " %i %-12:c%?-12t% s")
20908596 1386 (timeline . " % s")
acedf35c
CD
1387 (todo . " %i %-12:c")
1388 (tags . " %i %-12:c")
1389 (search . " %i %-12:c"))
20908596 1390 "Format specifications for the prefix of items in the agenda views.
fe3c5669
PE
1391An alist with five entries, each for the different agenda types. The
1392keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
3ab2c837
BG
1393The values are format strings.
1394
20908596
CD
1395This format works similar to a printf format, with the following meaning:
1396
fe3c5669 1397 %c the category of the item, \"Diary\" for entries from the diary,
3ab2c837
BG
1398 or as given by the CATEGORY keyword or derived from the file name
1399 %i the icon category of the item, see `org-agenda-category-icon-alist'
1400 %T the last tag of the item (ignore inherited tags, which come first)
1401 %t the HH:MM time-of-day specification if one applies to the entry
20908596 1402 %s Scheduling/Deadline information, a short string
3ab2c837
BG
1403 %(expression) Eval EXPRESSION and replace the control string
1404 by the result
20908596
CD
1405
1406All specifiers work basically like the standard `%s' of printf, but may
3ab2c837
BG
1407contain two additional characters: a question mark just after the `%'
1408and a whitespace/punctuation character just before the final letter.
20908596
CD
1409
1410If the first character after `%' is a question mark, the entire field
fe3c5669
PE
1411will only be included if the corresponding value applies to the current
1412entry. This is useful for fields which should have fixed width when
1413present, but zero width when absent. For example, \"%?-12t\" will
1414result in a 12 character time field if a time of the day is specified,
3ab2c837 1415but will completely disappear in entries which do not contain a time.
20908596
CD
1416
1417If there is punctuation or whitespace character just before the final
1418format letter, this character will be appended to the field value if
1419the value is not empty. For example, the format \"%-12:c\" leads to
1420\"Diary: \" if the category is \"Diary\". If the category were be
8bfe682a 1421empty, no additional colon would be inserted.
20908596 1422
fe3c5669 1423The default value for the agenda sublist is \" %-12:c%?-12t% s\",
3ab2c837
BG
1424which means:
1425
20908596 1426- Indent the line with two space characters
3ab2c837 1427- Give the category a 12 chars wide field, padded with whitespace on
20908596
CD
1428 the right (because of `-'). Append a colon if there is a category
1429 (because of `:').
1430- If there is a time-of-day, put it into a 12 chars wide field. If no
1431 time, don't put in an empty field, just skip it (because of '?').
3ab2c837 1432- Finally, put the scheduling information.
20908596
CD
1433
1434See also the variables `org-agenda-remove-times-when-in-prefix' and
c8d0cf5c
CD
1435`org-agenda-remove-tags'.
1436
1437Custom commands can set this variable in the options section."
20908596
CD
1438 :type '(choice
1439 (string :tag "General format")
1440 (list :greedy t :tag "View dependent"
1441 (cons (const agenda) (string :tag "Format"))
1442 (cons (const timeline) (string :tag "Format"))
1443 (cons (const todo) (string :tag "Format"))
1444 (cons (const tags) (string :tag "Format"))
1445 (cons (const search) (string :tag "Format"))))
1446 :group 'org-agenda-line-format)
1447
1448(defvar org-prefix-format-compiled nil
1449 "The compiled version of the most recently used prefix format.
1450See the variable `org-agenda-prefix-format'.")
1451
1452(defcustom org-agenda-todo-keyword-format "%-1s"
1453 "Format for the TODO keyword in agenda lines.
1454Set this to something like \"%-12s\" if you want all TODO keywords
1455to occupy a fixed space in the agenda display."
1456 :group 'org-agenda-line-format
1457 :type 'string)
1458
ce4fdcb9
CD
1459(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1460 "Text preceding timerange entries in the agenda view.
1461This is a list with two strings. The first applies when the range
1462is entirely on one day. The second applies if the range spans several days.
1463The strings may have two \"%d\" format specifiers which will be filled
1464with the sequence number of the days, and the total number of days in the
1465range, respectively."
1466 :group 'org-agenda-line-format
1467 :type '(list
1468 (string :tag "Deadline today ")
1469 (choice :tag "Deadline relative"
1470 (string :tag "Format string")
1471 (function))))
1472
20908596 1473(defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
86fbb8ca 1474 "Text preceding scheduled items in the agenda view.
20908596
CD
1475This is a list with two strings. The first applies when the item is
1476scheduled on the current day. The second applies when it has been scheduled
b349f79f
CD
1477previously, it may contain a %d indicating that this is the nth time that
1478this item is scheduled, due to automatic rescheduling of unfinished items
1479for the following day. So this number is one larger than the number of days
1480that passed since this item was scheduled first."
20908596
CD
1481 :group 'org-agenda-line-format
1482 :type '(list
1483 (string :tag "Scheduled today ")
1484 (string :tag "Scheduled previously")))
1485
ed21c5c8 1486(defcustom org-agenda-inactive-leader "["
86fbb8ca 1487 "Text preceding item pulled into the agenda by inactive time stamps.
ed21c5c8
CD
1488These entries are added to the agenda when pressing \"[\"."
1489 :group 'org-agenda-line-format
1490 :type '(list
1491 (string :tag "Scheduled today ")
1492 (string :tag "Scheduled previously")))
1493
20908596 1494(defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
86fbb8ca 1495 "Text preceding deadline items in the agenda view.
20908596
CD
1496This is a list with two strings. The first applies when the item has its
1497deadline on the current day. The second applies when it is in the past or
1498in the future, it may contain %d to capture how many days away the deadline
1499is (was)."
1500 :group 'org-agenda-line-format
1501 :type '(list
1502 (string :tag "Deadline today ")
1503 (choice :tag "Deadline relative"
1504 (string :tag "Format string")
1505 (function))))
1506
1507(defcustom org-agenda-remove-times-when-in-prefix t
ed21c5c8 1508 "Non-nil means remove duplicate time specifications in agenda items.
20908596
CD
1509When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1510time-of-day specification in a headline or diary entry is extracted and
1511placed into the prefix. If this option is non-nil, the original specification
1512\(a timestamp or -range, or just a plain time(range) specification like
151311:30-4pm) will be removed for agenda display. This makes the agenda less
1514cluttered.
1515The option can be t or nil. It may also be the symbol `beg', indicating
86fbb8ca 1516that the time should only be removed when it is located at the beginning of
20908596
CD
1517the headline/diary entry."
1518 :group 'org-agenda-line-format
1519 :type '(choice
1520 (const :tag "Always" t)
1521 (const :tag "Never" nil)
1522 (const :tag "When at beginning of entry" beg)))
1523
86fbb8ca
CD
1524(defcustom org-agenda-remove-timeranges-from-blocks nil
1525 "Non-nil means remove time ranges specifications in agenda
1526items that span on several days."
1527 :group 'org-agenda-line-format
1528 :type 'boolean)
20908596
CD
1529
1530(defcustom org-agenda-default-appointment-duration nil
1531 "Default duration for appointments that only have a starting time.
1532When nil, no duration is specified in such cases.
1533When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1534 :group 'org-agenda-line-format
1535 :type '(choice
1536 (integer :tag "Minutes")
1537 (const :tag "No default duration")))
1538
ff4be292 1539(defcustom org-agenda-show-inherited-tags t
ed21c5c8 1540 "Non-nil means show inherited tags in each agenda line."
ff4be292
CD
1541 :group 'org-agenda-line-format
1542 :type 'boolean)
20908596 1543
5dec9555
CD
1544(defcustom org-agenda-hide-tags-regexp nil
1545 "Regular expression used to filter away specific tags in agenda views.
1546This means that these tags will be present, but not be shown in the agenda
86fbb8ca 1547line. Secondary filtering will still work on the hidden tags.
afe98dfa 1548Nil means don't hide any tags."
5dec9555
CD
1549 :group 'org-agenda-line-format
1550 :type '(choice
1551 (const :tag "Hide none" nil)
1552 (string :tag "Regexp ")))
1553
20908596 1554(defcustom org-agenda-remove-tags nil
ed21c5c8 1555 "Non-nil means remove the tags from the headline copy in the agenda.
20908596
CD
1556When this is the symbol `prefix', only remove tags when
1557`org-agenda-prefix-format' contains a `%T' specifier."
1558 :group 'org-agenda-line-format
1559 :type '(choice
1560 (const :tag "Always" t)
1561 (const :tag "Never" nil)
1562 (const :tag "When prefix format contains %T" prefix)))
1563
1564(if (fboundp 'defvaralias)
1565 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1566 'org-agenda-remove-tags))
1567
5ace2fe5 1568(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
20908596
CD
1569 "Shift tags in agenda items to this column.
1570If this number is positive, it specifies the column. If it is negative,
1571it means that the tags should be flushright to that column. For example,
1572-80 works well for a normal 80 character screen."
1573 :group 'org-agenda-line-format
1574 :type 'integer)
1575
1576(if (fboundp 'defvaralias)
1577 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1578
c8d0cf5c 1579(defcustom org-agenda-fontify-priorities 'cookies
ed21c5c8 1580 "Non-nil means highlight low and high priorities in agenda.
20908596 1581When t, the highest priority entries are bold, lowest priority italic.
86fbb8ca 1582However, settings in `org-priority-faces' will overrule these faces.
c8d0cf5c
CD
1583When this variable is the symbol `cookies', only fontify the
1584cookies, not the entire task.
621f83e4
CD
1585This may also be an association list of priority faces, whose
1586keys are the character values of `org-highest-priority',
1587`org-default-priority', and `org-lowest-priority' (the default values
ed21c5c8
CD
1588are ?A, ?B, and ?C, respectively). The face may be a named face, a
1589color as a string, or a list like `(:background \"Red\")'.
1590If it is a color, the variable `org-faces-easy-properties'
1591determines if it is a foreground or a background color."
20908596
CD
1592 :group 'org-agenda-line-format
1593 :type '(choice
1594 (const :tag "Never" nil)
1595 (const :tag "Defaults" t)
c8d0cf5c 1596 (const :tag "Cookies only" cookies)
20908596
CD
1597 (repeat :tag "Specify"
1598 (list (character :tag "Priority" :value ?A)
ed21c5c8
CD
1599 (choice :tag "Face "
1600 (string :tag "Color")
1601 (sexp :tag "Face"))))))
20908596 1602
acedf35c
CD
1603(defcustom org-agenda-day-face-function nil
1604 "Function called to determine what face should be used to display a day.
1605The only argument passed to that function is the day. It should
1606returns a face, or nil if does not want to specify a face and let
1607the normal rules apply."
1608 :group 'org-agenda-line-format
1609 :type 'function)
1610
1611(defcustom org-agenda-category-icon-alist nil
1612 "Alist of category icon to be displayed in agenda views.
1613
1614Each entry should have the following format:
1615
1616 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1617
1618Where CATEGORY-REGEXP is a regexp matching the categories where
1619the icon should be displayed.
1620FILE-OR-DATA either a file path or a string containing image data.
1621
e1dbe924 1622The other fields can be omitted safely if not needed:
acedf35c
CD
1623TYPE indicates the image type.
1624DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1625image data.
1626PROPS are additional image attributes to assign to the image,
1627like, e.g. `:ascent center'.
1628
1629 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1630
1631If you want to set the display properties yourself, just put a
1632list as second element:
1633
1634 (CATEGORY-REGEXP (MY PROPERTY LIST))
1635
1636For example, to display a 16px horizontal space for Emacs
1637category, you can use:
1638
1639 (\"Emacs\" '(space . (:width (16))))"
1640 :group 'org-agenda-line-format
1641 :type '(alist :key-type (string :tag "Regexp matching category")
1642 :value-type (choice (list :tag "Icon"
1643 (string :tag "File or data")
1644 (symbol :tag "Type")
1645 (boolean :tag "Data?")
1646 (repeat :tag "Extra image properties" :inline t symbol))
1647 (list :tag "Display properties" sexp))))
1648
20908596
CD
1649(defgroup org-agenda-column-view nil
1650 "Options concerning column view in the agenda."
1651 :tag "Org Agenda Column View"
1652 :group 'org-agenda)
1653
1654(defcustom org-agenda-columns-show-summaries t
ed21c5c8 1655 "Non-nil means show summaries for columns displayed in the agenda view."
20908596
CD
1656 :group 'org-agenda-column-view
1657 :type 'boolean)
1658
1659(defcustom org-agenda-columns-compute-summary-properties t
ed21c5c8 1660 "Non-nil means recompute all summary properties before column view.
20908596
CD
1661When column view in the agenda is listing properties that have a summary
1662operator, it can go to all relevant buffers and recompute the summaries
1663there. This can mean overhead for the agenda column view, but is necessary
1664to have thing up to date.
1665As a special case, a CLOCKSUM property also makes sure that the clock
1666computations are current."
1667 :group 'org-agenda-column-view
1668 :type 'boolean)
1669
1670(defcustom org-agenda-columns-add-appointments-to-effort-sum nil
ed21c5c8 1671 "Non-nil means the duration of an appointment will add to day effort.
20908596
CD
1672The property to which appointment durations will be added is the one given
1673in the option `org-effort-property'. If an appointment does not have
1674an end time, `org-agenda-default-appointment-duration' will be used. If that
1675is not set, an appointment without end time will not contribute to the time
1676estimate."
1677 :group 'org-agenda-column-view
1678 :type 'boolean)
1679
8bfe682a
CD
1680(defcustom org-agenda-auto-exclude-function nil
1681 "A function called with a tag to decide if it is filtered on '/ RET'.
1682The sole argument to the function, which is called once for each
1683possible tag, is a string giving the name of the tag. The
1684function should return either nil if the tag should be included
ed21c5c8
CD
1685as normal, or \"-<TAG>\" to exclude the tag.
1686Note that for the purpose of tag filtering, only the lower-case version of
1687all tags will be considered, so that this function will only ever see
1688the lower-case version of all tags."
8bfe682a
CD
1689 :group 'org-agenda
1690 :type 'function)
1691
3ab2c837
BG
1692(defcustom org-agenda-bulk-custom-functions nil
1693 "Alist of characters and custom functions for bulk actions.
1694For example, this value makes those two functions available:
1695
1696 '((?R set-category)
1697 (?C bulk-cut))
1698
1699With selected entries in an agenda buffer, `B R' will call
fe3c5669 1700the custom function `set-category' on the selected entries.
3ab2c837
BG
1701Note that functions in this alist don't need to be quoted."
1702 :type 'alist
1703 :group 'org-agenda)
1704
20908596
CD
1705(eval-when-compile
1706 (require 'cl))
1707(require 'org)
1708
afe98dfa
CD
1709(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1710 "Execute BODY with point at location given by `org-hd-marker' property.
1711If STRING is non-nil, the text property will be fetched from position 0
1712in that string. If STRING is nil, it will be fetched from the beginning
1713of the current line."
1714 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1715 'org-hd-marker string)))
1716 (with-current-buffer (marker-buffer marker)
1717 (save-excursion
1718 (goto-char marker)
1719 ,@body))))
1720
20908596
CD
1721(defun org-add-agenda-custom-command (entry)
1722 "Replace or add a command in `org-agenda-custom-commands'.
1723This is mostly for hacking and trying a new command - once the command
1724works you probably want to add it to `org-agenda-custom-commands' for good."
1725 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1726 (if ass
1727 (setcdr ass (cdr entry))
1728 (push entry org-agenda-custom-commands))))
1729
1730;;; Define the Org-agenda-mode
1731
1732(defvar org-agenda-mode-map (make-sparse-keymap)
1733 "Keymap for `org-agenda-mode'.")
8bfe682a
CD
1734(if (fboundp 'defvaralias)
1735 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
20908596
CD
1736
1737(defvar org-agenda-menu) ; defined later in this file.
c8d0cf5c 1738(defvar org-agenda-restrict) ; defined later in this file.
20908596 1739(defvar org-agenda-follow-mode nil)
54a0dee5 1740(defvar org-agenda-entry-text-mode nil)
20908596
CD
1741(defvar org-agenda-clockreport-mode nil)
1742(defvar org-agenda-show-log nil)
1743(defvar org-agenda-redo-command nil)
1744(defvar org-agenda-query-string nil)
0bd48b37 1745(defvar org-agenda-mode-hook nil
86fbb8ca 1746 "Hook for `org-agenda-mode', run after the mode is turned on.")
20908596
CD
1747(defvar org-agenda-type nil)
1748(defvar org-agenda-force-single-file nil)
c8d0cf5c 1749(defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
20908596
CD
1750
1751(defun org-agenda-mode ()
1752 "Mode for time-sorted view on action items in Org-mode files.
1753
1754The following commands are available:
1755
1756\\{org-agenda-mode-map}"
1757 (interactive)
1758 (kill-all-local-variables)
1759 (setq org-agenda-undo-list nil
c8d0cf5c
CD
1760 org-agenda-pending-undo-list nil
1761 org-agenda-bulk-marked-entries nil)
20908596
CD
1762 (setq major-mode 'org-agenda-mode)
1763 ;; Keep global-font-lock-mode from turning on font-lock-mode
1764 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1765 (setq mode-name "Org-Agenda")
1766 (use-local-map org-agenda-mode-map)
1767 (easy-menu-add org-agenda-menu)
1768 (if org-startup-truncated (setq truncate-lines t))
54a0dee5 1769 (org-set-local 'line-move-visual nil)
20908596
CD
1770 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1771 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1772 ;; Make sure properties are removed when copying text
1773 (when (boundp 'buffer-substring-filters)
1774 (org-set-local 'buffer-substring-filters
1775 (cons (lambda (x)
1776 (set-text-properties 0 (length x) nil x) x)
1777 buffer-substring-filters)))
1778 (unless org-agenda-keep-modes
1779 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
54a0dee5 1780 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
20908596 1781 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
c8d0cf5c
CD
1782 org-agenda-show-log org-agenda-start-with-log-mode))
1783
20908596
CD
1784 (easy-menu-change
1785 '("Agenda") "Agenda Files"
1786 (append
1787 (list
1788 (vector
1789 (if (get 'org-agenda-files 'org-restrict)
1790 "Restricted to single file"
1791 "Edit File List")
1792 '(org-edit-agenda-file-list)
1793 (not (get 'org-agenda-files 'org-restrict)))
1794 "--")
1795 (mapcar 'org-file-menu-entry (org-agenda-files))))
1796 (org-agenda-set-mode-name)
1797 (apply
1798 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1799 (list 'org-agenda-mode-hook)))
1800
1801(substitute-key-definition 'undo 'org-agenda-undo
1802 org-agenda-mode-map global-map)
1803(org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1804(org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1805(org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1806(org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
c8d0cf5c
CD
1807(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1808(org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
3ab2c837 1809(org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
c8d0cf5c
CD
1810(org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1811(org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
3ab2c837 1812(org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
c8d0cf5c
CD
1813(org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1814(org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
8bfe682a
CD
1815(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1816(org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1817(org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
54a0dee5 1818(org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
8bfe682a 1819(org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
20908596 1820(org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
20908596 1821(org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
8bfe682a
CD
1822(org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1823(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1824(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
20908596
CD
1825(org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1826(org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1827(org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
20908596
CD
1828(org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1829(org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
54a0dee5 1830(org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
20908596 1831(org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
8bfe682a 1832(org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
20908596 1833(org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
71d35b24 1834(org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
20908596
CD
1835(org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1836(org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1837(org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1838(org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
20908596
CD
1839(org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1840(org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1841(org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
b349f79f
CD
1842(org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1843(org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
c8d0cf5c
CD
1844(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1845(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1846(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1847(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
20908596
CD
1848
1849(org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1850(org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1851(org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1852(let ((l '(1 2 3 4 5 6 7 8 9 0)))
1853 (while l (org-defkey org-agenda-mode-map
1854 (int-to-string (pop l)) 'digit-argument)))
1855
54a0dee5 1856(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
20908596 1857(org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
54a0dee5 1858(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
20908596 1859(org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
c8d0cf5c 1860(org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
20908596 1861(org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
ed21c5c8 1862(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
20908596
CD
1863(org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1864(org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1865(org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
54a0dee5
CD
1866(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1867(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1868(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1869 'org-clock-modify-effort-estimate)
1870(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
20908596
CD
1871(org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1872(org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1873(org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
20908596 1874(org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
c8d0cf5c 1875(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
20908596
CD
1876(org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1877(org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
8bfe682a
CD
1878(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1879(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1880(substitute-key-definition 'next-line 'org-agenda-next-line
1881 org-agenda-mode-map global-map)
1882(substitute-key-definition 'previous-line 'org-agenda-previous-line
1883 org-agenda-mode-map global-map)
621f83e4 1884(org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
20908596
CD
1885(org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1886(org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1887(org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1888(org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1889(org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1890(org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1891(org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1892(org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1893(org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1894(org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1895(org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1896(org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1897(org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1898(org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1899(org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1900(org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1901(org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1902(org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
afe98dfa 1903(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
20908596
CD
1904(org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1905(org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1906(org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1907(org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1908(org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1909(org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
54a0dee5
CD
1910(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1911(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
20908596 1912(org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
c8d0cf5c 1913(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20908596
CD
1914
1915(org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1916(org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1917(org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1918(org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
621f83e4 1919(org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
71d35b24 1920(org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
c8d0cf5c 1921(org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
8d642074
CD
1922(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1923(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1924(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
20908596 1925
86fbb8ca
CD
1926(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1927(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
20908596 1928(when org-agenda-mouse-1-follows-link
8bfe682a 1929 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
20908596
CD
1930(easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1931 '("Agenda"
1932 ("Agenda Files")
1933 "--"
8d642074
CD
1934 ("Agenda Dates"
1935 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1936 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1937 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1938 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1939 "--"
1940 ("View"
1941 ["Day View" org-agenda-day-view
1942 :active (org-agenda-check-type nil 'agenda)
acedf35c 1943 :style radio :selected (eq org-agenda-current-span 'day)
8d642074
CD
1944 :keys "v d (or just d)"]
1945 ["Week View" org-agenda-week-view
1946 :active (org-agenda-check-type nil 'agenda)
acedf35c 1947 :style radio :selected (eq org-agenda-current-span 'week)
8d642074
CD
1948 :keys "v w (or just w)"]
1949 ["Month View" org-agenda-month-view
1950 :active (org-agenda-check-type nil 'agenda)
acedf35c 1951 :style radio :selected (eq org-agenda-current-span 'month)
8d642074
CD
1952 :keys "v m"]
1953 ["Year View" org-agenda-year-view
1954 :active (org-agenda-check-type nil 'agenda)
acedf35c 1955 :style radio :selected (eq org-agenda-current-span 'year)
8d642074
CD
1956 :keys "v y"]
1957 "--"
1958 ["Include Diary" org-agenda-toggle-diary
1959 :style toggle :selected org-agenda-include-diary
1960 :active (org-agenda-check-type nil 'agenda)]
ed21c5c8
CD
1961 ["Include Deadlines" org-agenda-toggle-deadlines
1962 :style toggle :selected org-agenda-include-deadlines
1963 :active (org-agenda-check-type nil 'agenda)]
8d642074
CD
1964 ["Use Time Grid" org-agenda-toggle-time-grid
1965 :style toggle :selected org-agenda-use-time-grid
1966 :active (org-agenda-check-type nil 'agenda)]
1967 "--"
1968 ["Show clock report" org-agenda-clockreport-mode
1969 :style toggle :selected org-agenda-clockreport-mode
1970 :active (org-agenda-check-type nil 'agenda)]
1971 ["Show some entry text" org-agenda-entry-text-mode
1972 :style toggle :selected org-agenda-entry-text-mode
1973 :active t]
1974 "--"
1975 ["Show Logbook entries" org-agenda-log-mode
1976 :style toggle :selected org-agenda-show-log
1977 :active (org-agenda-check-type nil 'agenda 'timeline)
1978 :keys "v l (or just l)"]
1979 ["Include archived trees" org-agenda-archives-mode
1980 :style toggle :selected org-agenda-archives-mode :active t
1981 :keys "v a"]
1982 ["Include archive files" (org-agenda-archives-mode t)
1983 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1984 :keys "v A"]
1985 "--"
1986 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1987 ["Write view to file" org-write-agenda t]
1988 ["Rebuild buffer" org-agenda-redo t]
1989 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1990 "--"
1991 ["Show original entry" org-agenda-show t]
20908596
CD
1992 ["Go To (other window)" org-agenda-goto t]
1993 ["Go To (this window)" org-agenda-switch-to t]
1994 ["Follow Mode" org-agenda-follow-mode
1995 :style toggle :selected org-agenda-follow-mode :active t]
8d642074 1996; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
20908596 1997 "--"
8d642074
CD
1998 ("TODO"
1999 ["Cycle TODO" org-agenda-todo t]
2000 ["Next TODO set" org-agenda-todo-nextset t]
2001 ["Previous TODO set" org-agenda-todo-previousset t]
2002 ["Add note" org-agenda-add-note t])
2003 ("Archive/Refile/Delete"
8bfe682a
CD
2004 ["Archive default" org-agenda-archive-default t]
2005 ["Archive default" org-agenda-archive-default-with-confirmation t]
20908596
CD
2006 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2007 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
c8d0cf5c 2008 ["Archive subtree" org-agenda-archive t]
8d642074
CD
2009 "--"
2010 ["Refile" org-agenda-refile t]
2011 "--"
2012 ["Delete subtree" org-agenda-kill t])
c8d0cf5c 2013 ("Bulk action"
8d642074 2014 ["Mark entry" org-agenda-bulk-mark t]
3ab2c837 2015 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
8d642074 2016 ["Unmark entry" org-agenda-bulk-unmark t]
c8d0cf5c 2017 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
3ab2c837 2018 ["Act on all marked" org-agenda-bulk-action t]
c8d0cf5c 2019 "--"
20908596
CD
2020 ("Tags and Properties"
2021 ["Show all Tags" org-agenda-show-tags t]
2022 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2023 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2024 "--"
2025 ["Column View" org-columns t])
8d642074 2026 ("Deadline/Schedule"
20908596
CD
2027 ["Schedule" org-agenda-schedule t]
2028 ["Set Deadline" org-agenda-deadline t]
2029 "--"
b349f79f
CD
2030 ["Mark item" org-agenda-action :active t :keys "k m"]
2031 ["Show mark item" org-agenda-action :active t :keys "k v"]
2032 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2033 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2034 "--"
20908596
CD
2035 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2036 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
c8d0cf5c
CD
2037 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2038 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2039 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2040 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
20908596 2041 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
54a0dee5 2042 ("Clock and Effort"
20908596
CD
2043 ["Clock in" org-agenda-clock-in t]
2044 ["Clock out" org-agenda-clock-out t]
2045 ["Clock cancel" org-agenda-clock-cancel t]
54a0dee5
CD
2046 ["Goto running clock" org-clock-goto t]
2047 "--"
2048 ["Set Effort" org-agenda-set-effort t]
2049 ["Change clocked effort" org-clock-modify-effort-estimate
2050 (org-clock-is-active)])
20908596
CD
2051 ("Priority"
2052 ["Set Priority" org-agenda-priority t]
2053 ["Increase Priority" org-agenda-priority-up t]
2054 ["Decrease Priority" org-agenda-priority-down t]
2055 ["Show Priority" org-agenda-show-priority t])
2056 ("Calendar/Diary"
2057 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2058 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2059 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2060 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2061 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2062 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2063 "--"
8d642074 2064 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
20908596 2065 "--"
8d642074 2066 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2c3ad40d 2067 "--"
8d642074
CD
2068 ("MobileOrg"
2069 ["Push Files and Views" org-mobile-push t]
2070 ["Get Captured and Flagged" org-mobile-pull t]
2071 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2072 ["Show note / unflag" org-agenda-show-the-flagging-note t]
c8d0cf5c 2073 "--"
8d642074 2074 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
20908596
CD
2075 "--"
2076 ["Quit" org-agenda-quit t]
2077 ["Exit and Release Buffers" org-agenda-exit t]
2078 ))
2079
2080;;; Agenda undo
2081
2082(defvar org-agenda-allow-remote-undo t
ed21c5c8 2083 "Non-nil means allow remote undo from the agenda buffer.")
20908596
CD
2084(defvar org-agenda-undo-list nil
2085 "List of undoable operations in the agenda since last refresh.")
2086(defvar org-agenda-undo-has-started-in nil
2087 "Buffers that have already seen `undo-start' in the current undo sequence.")
2088(defvar org-agenda-pending-undo-list nil
33306645 2089 "In a series of undo commands, this is the list of remaining undo items.")
20908596 2090
20908596
CD
2091(defun org-agenda-undo ()
2092 "Undo a remote editing step in the agenda.
2093This undoes changes both in the agenda buffer and in the remote buffer
2094that have been changed along."
2095 (interactive)
2096 (or org-agenda-allow-remote-undo
f924a367 2097 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
20908596
CD
2098 (if (not (eq this-command last-command))
2099 (setq org-agenda-undo-has-started-in nil
2100 org-agenda-pending-undo-list org-agenda-undo-list))
2101 (if (not org-agenda-pending-undo-list)
2102 (error "No further undo information"))
2103 (let* ((entry (pop org-agenda-pending-undo-list))
2104 buf line cmd rembuf)
2105 (setq cmd (pop entry) line (pop entry))
2106 (setq rembuf (nth 2 entry))
2107 (org-with-remote-undo rembuf
2108 (while (bufferp (setq buf (pop entry)))
2109 (if (pop entry)
2110 (with-current-buffer buf
2111 (let ((last-undo-buffer buf)
2112 (inhibit-read-only t))
2113 (unless (memq buf org-agenda-undo-has-started-in)
2114 (push buf org-agenda-undo-has-started-in)
2115 (make-local-variable 'pending-undo-list)
2116 (undo-start))
2117 (while (and pending-undo-list
2118 (listp pending-undo-list)
2119 (not (car pending-undo-list)))
2120 (pop pending-undo-list))
2121 (undo-more 1))))))
54a0dee5 2122 (org-goto-line line)
20908596
CD
2123 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2124
2125(defun org-verify-change-for-undo (l1 l2)
2126 "Verify that a real change occurred between the undo lists L1 and L2."
2127 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2128 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2129 (not (eq l1 l2)))
2130
2131;;; Agenda dispatch
2132
2133(defvar org-agenda-restrict nil)
2134(defvar org-agenda-restrict-begin (make-marker))
2135(defvar org-agenda-restrict-end (make-marker))
2136(defvar org-agenda-last-dispatch-buffer nil)
2137(defvar org-agenda-overriding-restriction nil)
2138
2139;;;###autoload
c8d0cf5c 2140(defun org-agenda (&optional arg keys restriction)
20908596
CD
2141 "Dispatch agenda commands to collect entries to the agenda buffer.
2142Prompts for a command to execute. Any prefix arg will be passed
2143on to the selected command. The default selections are:
2144
2145a Call `org-agenda-list' to display the agenda for current day or week.
2146t Call `org-todo-list' to display the global todo list.
2147T Call `org-todo-list' to display the global todo list, select only
2148 entries with a specific TODO keyword (the user gets a prompt).
2149m Call `org-tags-view' to display headlines with tags matching
2150 a condition (the user is prompted for the condition).
2151M Like `m', but select only TODO entries, no ordinary headlines.
2152L Create a timeline for the current buffer.
2153e Export views to associated files.
c8d0cf5c 2154s Search entries for keywords.
8bfe682a 2155/ Multi occur across all agenda files and also files listed
c8d0cf5c
CD
2156 in `org-agenda-text-search-extra-files'.
2157< Restrict agenda commands to buffer, subtree, or region.
2158 Press several times to get the desired effect.
2159> Remove a previous restriction.
2160# List \"stuck\" projects.
2161! Configure what \"stuck\" means.
2162C Configure custom agenda commands.
20908596
CD
2163
2164More commands can be added by configuring the variable
2165`org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2166searches can be pre-defined in this way.
2167
2168If the current buffer is in Org-mode and visiting a file, you can also
2169first press `<' once to indicate that the agenda should be temporarily
2170\(until the next use of \\[org-agenda]) restricted to the current file.
2171Pressing `<' twice means to restrict to the current subtree or region
2172\(if active)."
2173 (interactive "P")
2174 (catch 'exit
2175 (let* ((prefix-descriptions nil)
54a0dee5
CD
2176 (org-agenda-window-setup (if (equal (buffer-name)
2177 org-agenda-buffer-name)
2178 'current-window
2179 org-agenda-window-setup))
20908596
CD
2180 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2181 (org-agenda-custom-commands
2182 ;; normalize different versions
2183 (delq nil
2184 (mapcar
2185 (lambda (x)
2186 (cond ((stringp (cdr x))
2187 (push x prefix-descriptions)
2188 nil)
2189 ((stringp (nth 1 x)) x)
2190 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2191 (t (cons (car x) (cons "" (cdr x))))))
2192 org-agenda-custom-commands)))
2193 (buf (current-buffer))
2194 (bfn (buffer-file-name (buffer-base-buffer)))
2195 entry key type match lprops ans)
8d642074 2196 ;; Turn off restriction unless there is an overriding one,
20908596 2197 (unless org-agenda-overriding-restriction
8bfe682a 2198 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
8d642074
CD
2199 ;; There is a request to keep the file list in place
2200 (put 'org-agenda-files 'org-restrict nil))
20908596
CD
2201 (setq org-agenda-restrict nil)
2202 (move-marker org-agenda-restrict-begin nil)
2203 (move-marker org-agenda-restrict-end nil))
2204 ;; Delete old local properties
2205 (put 'org-agenda-redo-command 'org-lprops nil)
3ab2c837
BG
2206 ;; Delete previously set last-arguments
2207 (put 'org-agenda-redo-command 'last-args nil)
20908596
CD
2208 ;; Remember where this call originated
2209 (setq org-agenda-last-dispatch-buffer (current-buffer))
2210 (unless keys
2211 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2212 keys (car ans)
2213 restriction (cdr ans)))
8bfe682a 2214 ;; Establish the restriction, if any
20908596
CD
2215 (when (and (not org-agenda-overriding-restriction) restriction)
2216 (put 'org-agenda-files 'org-restrict (list bfn))
2217 (cond
2218 ((eq restriction 'region)
2219 (setq org-agenda-restrict t)
2220 (move-marker org-agenda-restrict-begin (region-beginning))
2221 (move-marker org-agenda-restrict-end (region-end)))
2222 ((eq restriction 'subtree)
2223 (save-excursion
2224 (setq org-agenda-restrict t)
2225 (org-back-to-heading t)
2226 (move-marker org-agenda-restrict-begin (point))
2227 (move-marker org-agenda-restrict-end
2228 (progn (org-end-of-subtree t)))))))
2229
20908596
CD
2230 ;; For example the todo list should not need it (but does...)
2231 (cond
2232 ((setq entry (assoc keys org-agenda-custom-commands))
2233 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2234 (progn
8bfe682a
CD
2235 (setq type (nth 2 entry) match (eval (nth 3 entry))
2236 lprops (nth 4 entry))
20908596
CD
2237 (put 'org-agenda-redo-command 'org-lprops lprops)
2238 (cond
2239 ((eq type 'agenda)
2240 (org-let lprops '(org-agenda-list current-prefix-arg)))
2241 ((eq type 'alltodo)
2242 (org-let lprops '(org-todo-list current-prefix-arg)))
2243 ((eq type 'search)
2244 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2245 ((eq type 'stuck)
2246 (org-let lprops '(org-agenda-list-stuck-projects
2247 current-prefix-arg)))
2248 ((eq type 'tags)
2249 (org-let lprops '(org-tags-view current-prefix-arg match)))
2250 ((eq type 'tags-todo)
2251 (org-let lprops '(org-tags-view '(4) match)))
2252 ((eq type 'todo)
2253 (org-let lprops '(org-todo-list match)))
2254 ((eq type 'tags-tree)
2255 (org-check-for-org-mode)
c8d0cf5c 2256 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
20908596
CD
2257 ((eq type 'todo-tree)
2258 (org-check-for-org-mode)
2259 (org-let lprops
3ab2c837 2260 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
20908596
CD
2261 (regexp-quote match) "\\>"))))
2262 ((eq type 'occur-tree)
2263 (org-check-for-org-mode)
2264 (org-let lprops '(org-occur match)))
2265 ((functionp type)
2266 (org-let lprops '(funcall type match)))
2267 ((fboundp type)
2268 (org-let lprops '(funcall type match)))
2269 (t (error "Invalid custom agenda command type %s" type))))
3ab2c837 2270 (org-agenda-run-series (nth 1 entry) (cddr entry))))
20908596
CD
2271 ((equal keys "C")
2272 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2273 (customize-variable 'org-agenda-custom-commands))
2274 ((equal keys "a") (call-interactively 'org-agenda-list))
2275 ((equal keys "s") (call-interactively 'org-search-view))
2276 ((equal keys "t") (call-interactively 'org-todo-list))
2277 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2278 ((equal keys "m") (call-interactively 'org-tags-view))
2279 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2280 ((equal keys "e") (call-interactively 'org-store-agenda-views))
8d642074
CD
2281 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2282 (org-add-hook
2283 'post-command-hook
2284 (lambda ()
2285 (unless (current-message)
2286 (let* ((m (org-agenda-get-any-marker))
2287 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2288 (when note
2289 (message (concat
2290 "FLAGGING-NOTE ([?] for more info): "
2291 (org-add-props
2292 (replace-regexp-in-string
2293 "\\\\n" "//"
2294 (copy-sequence note))
2295 nil 'face 'org-warning)))))))
2296 t t))
20908596
CD
2297 ((equal keys "L")
2298 (unless (org-mode-p)
2299 (error "This is not an Org-mode file"))
2300 (unless restriction
2301 (put 'org-agenda-files 'org-restrict (list bfn))
2302 (org-call-with-arg 'org-timeline arg)))
2303 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2304 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2305 ((equal keys "!") (customize-variable 'org-stuck-projects))
2306 (t (error "Invalid agenda key"))))))
2307
3ab2c837
BG
2308(defun org-agenda-append-agenda ()
2309 "Append another agenda view to the current one.
2310This function allows interactive building of block agendas.
2311Agenda views are separated by `org-agenda-block-separator'."
2312 (interactive)
2313 (unless (string= (buffer-name) org-agenda-buffer-name)
2314 (error "Can only append from within agenda buffer"))
2315 (let ((org-agenda-multi t))
2316 (org-agenda)
2317 (widen)))
2318
20908596
CD
2319(defun org-agenda-normalize-custom-commands (cmds)
2320 (delq nil
2321 (mapcar
2322 (lambda (x)
2323 (cond ((stringp (cdr x)) nil)
2324 ((stringp (nth 1 x)) x)
2325 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2326 (t (cons (car x) (cons "" (cdr x))))))
2327 cmds)))
2328
2329(defun org-agenda-get-restriction-and-command (prefix-descriptions)
2330 "The user interface for selecting an agenda command."
2331 (catch 'exit
2332 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2333 (restrict-ok (and bfn (org-mode-p)))
2334 (region-p (org-region-active-p))
2335 (custom org-agenda-custom-commands)
2336 (selstring "")
2337 restriction second-time
afe98dfa
CD
2338 c entry key type match prefixes rmheader header-end custom1 desc
2339 line lines left right n n1)
20908596
CD
2340 (save-window-excursion
2341 (delete-other-windows)
2342 (org-switch-to-buffer-other-window " *Agenda Commands*")
2343 (erase-buffer)
2344 (insert (eval-when-compile
2345 (let ((header
2346"
2c3ad40d 2347Press key for an agenda command: < Buffer, subtree/region restriction
20908596
CD
2348-------------------------------- > Remove restriction
2349a Agenda for current week or day e Export agenda views
2350t List of all TODO entries T Entries with special TODO kwd
621f83e4 2351m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
20908596
CD
2352L Timeline for current buffer # List stuck projects (!=configure)
2353s Search for keywords C Configure custom agenda commands
8d642074 2354/ Multi-occur ? Find :FLAGGED: entries
20908596
CD
2355")
2356 (start 0))
2357 (while (string-match
2358 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2359 header start)
2360 (setq start (match-end 0))
2361 (add-text-properties (match-beginning 2) (match-end 2)
2362 '(face bold) header))
2363 header)))
2364 (setq header-end (move-marker (make-marker) (point)))
2365 (while t
2366 (setq custom1 custom)
2367 (when (eq rmheader t)
54a0dee5 2368 (org-goto-line 1)
20908596
CD
2369 (re-search-forward ":" nil t)
2370 (delete-region (match-end 0) (point-at-eol))
2371 (forward-char 1)
2372 (looking-at "-+")
2373 (delete-region (match-end 0) (point-at-eol))
2374 (move-marker header-end (match-end 0)))
2375 (goto-char header-end)
2376 (delete-region (point) (point-max))
afe98dfa
CD
2377
2378 ;; Produce all the lines that describe custom commands and prefixes
2379 (setq lines nil)
20908596
CD
2380 (while (setq entry (pop custom1))
2381 (setq key (car entry) desc (nth 1 entry)
54a0dee5
CD
2382 type (nth 2 entry)
2383 match (nth 3 entry))
20908596
CD
2384 (if (> (length key) 1)
2385 (add-to-list 'prefixes (string-to-char key))
afe98dfa
CD
2386 (setq line
2387 (format
2388 "%-4s%-14s"
2389 (org-add-props (copy-sequence key)
2390 '(face bold))
2391 (cond
2392 ((string-match "\\S-" desc) desc)
2393 ((eq type 'agenda) "Agenda for current week or day")
2394 ((eq type 'alltodo) "List of all TODO entries")
2395 ((eq type 'search) "Word search")
2396 ((eq type 'stuck) "List of stuck projects")
2397 ((eq type 'todo) "TODO keyword")
2398 ((eq type 'tags) "Tags query")
2399 ((eq type 'tags-todo) "Tags (TODO)")
2400 ((eq type 'tags-tree) "Tags tree")
2401 ((eq type 'todo-tree) "TODO kwd tree")
2402 ((eq type 'occur-tree) "Occur tree")
2403 ((functionp type) (if (symbolp type)
2404 (symbol-name type)
2405 "Lambda expression"))
2406 (t "???"))))
2407 (if org-agenda-menu-show-matcher
2408 (setq line
2409 (concat line ": "
2410 (cond
2411 ((stringp match)
2412 (setq match (copy-sequence match))
2413 (org-add-props match nil 'face 'org-warning))
2414 (match
2415 (format "set of %d commands" (length match)))
2416 (t ""))))
2417 (if (org-string-nw-p match)
2418 (add-text-properties
2419 0 (length line) (list 'help-echo
2420 (concat "Matcher: "match)) line)))
2421 (push line lines)))
2422 (setq lines (nreverse lines))
20908596
CD
2423 (when prefixes
2424 (mapc (lambda (x)
afe98dfa
CD
2425 (push
2426 (format "%s %s"
20908596 2427 (org-add-props (char-to-string x)
afe98dfa
CD
2428 nil 'face 'bold)
2429 (or (cdr (assoc (concat selstring
2430 (char-to-string x))
20908596 2431 prefix-descriptions))
afe98dfa
CD
2432 "Prefix key"))
2433 lines))
20908596 2434 prefixes))
afe98dfa
CD
2435
2436 ;; Check if we should display in two columns
2437 (if org-agenda-menu-two-column
2438 (progn
2439 (setq n (length lines)
2440 n1 (+ (/ n 2) (mod n 2))
2441 right (nthcdr n1 lines)
2442 left (copy-sequence lines))
2443 (setcdr (nthcdr (1- n1) left) nil))
2444 (setq left lines right nil))
2445 (while left
2446 (insert "\n" (pop left))
2447 (when right
2448 (if (< (current-column) 40)
2449 (move-to-column 40 t)
2450 (insert " "))
2451 (insert (pop right))))
2452
2453 ;; Make the window the right size
20908596 2454 (goto-char (point-min))
93b62de8
CD
2455 (if second-time
2456 (if (not (pos-visible-in-window-p (point-max)))
2457 (org-fit-window-to-buffer))
2458 (setq second-time t)
2459 (org-fit-window-to-buffer))
afe98dfa
CD
2460
2461 ;; Ask for selection
20908596
CD
2462 (message "Press key for agenda command%s:"
2463 (if (or restrict-ok org-agenda-overriding-restriction)
2464 (if org-agenda-overriding-restriction
2465 " (restriction lock active)"
2466 (if restriction
2467 (format " (restricted to %s)" restriction)
2468 " (unrestricted)"))
2469 ""))
2470 (setq c (read-char-exclusive))
2471 (message "")
2472 (cond
2473 ((assoc (char-to-string c) custom)
2474 (setq selstring (concat selstring (char-to-string c)))
2475 (throw 'exit (cons selstring restriction)))
2476 ((memq c prefixes)
2477 (setq selstring (concat selstring (char-to-string c))
2478 prefixes nil
2479 rmheader (or rmheader t)
2480 custom (delq nil (mapcar
2481 (lambda (x)
2482 (if (or (= (length (car x)) 1)
2483 (/= (string-to-char (car x)) c))
2484 nil
2485 (cons (substring (car x) 1) (cdr x))))
2486 custom))))
2487 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2488 (message "Restriction is only possible in Org-mode buffers")
2489 (ding) (sit-for 1))
2490 ((eq c ?1)
2491 (org-agenda-remove-restriction-lock 'noupdate)
2492 (setq restriction 'buffer))
2493 ((eq c ?0)
2494 (org-agenda-remove-restriction-lock 'noupdate)
2495 (setq restriction (if region-p 'region 'subtree)))
2496 ((eq c ?<)
2497 (org-agenda-remove-restriction-lock 'noupdate)
2498 (setq restriction
2499 (cond
2500 ((eq restriction 'buffer)
2501 (if region-p 'region 'subtree))
2502 ((memq restriction '(subtree region))
2503 nil)
2504 (t 'buffer))))
2505 ((eq c ?>)
2506 (org-agenda-remove-restriction-lock 'noupdate)
2507 (setq restriction nil))
8d642074 2508 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
20908596
CD
2509 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2510 ((and (> (length selstring) 0) (eq c ?\d))
2511 (delete-window)
2512 (org-agenda-get-restriction-and-command prefix-descriptions))
2513
2514 ((equal c ?q) (error "Abort"))
2515 (t (error "Invalid key %c" c))))))))
2516
3ab2c837
BG
2517(defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2518(defvar org-agenda-last-arguments nil
2519 "The arguments of the previous call to `org-agenda'.")
2520(defun org-agenda-run-series (name series)
c8d0cf5c 2521 (org-let (nth 1 series) '(org-prepare-agenda name))
20908596 2522 (let* ((org-agenda-multi t)
3ab2c837
BG
2523 (redo (list 'org-agenda-run-series name (list 'quote series)))
2524 (org-agenda-overriding-arguments
2525 (or org-agenda-overriding-arguments
2526 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2527 (get 'org-agenda-redo-command 'last-args))))
20908596
CD
2528 (cmds (car series))
2529 (gprops (nth 1 series))
2530 match ;; The byte compiler incorrectly complains about this. Keep it!
2531 cmd type lprops)
2532 (while (setq cmd (pop cmds))
8bfe682a 2533 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
20908596
CD
2534 (cond
2535 ((eq type 'agenda)
2536 (org-let2 gprops lprops
2537 '(call-interactively 'org-agenda-list)))
2538 ((eq type 'alltodo)
2539 (org-let2 gprops lprops
2540 '(call-interactively 'org-todo-list)))
2541 ((eq type 'search)
2542 (org-let2 gprops lprops
2543 '(org-search-view current-prefix-arg match nil)))
2544 ((eq type 'stuck)
2545 (org-let2 gprops lprops
2546 '(call-interactively 'org-agenda-list-stuck-projects)))
2547 ((eq type 'tags)
2548 (org-let2 gprops lprops
2549 '(org-tags-view current-prefix-arg match)))
2550 ((eq type 'tags-todo)
2551 (org-let2 gprops lprops
2552 '(org-tags-view '(4) match)))
2553 ((eq type 'todo)
2554 (org-let2 gprops lprops
2555 '(org-todo-list match)))
2556 ((fboundp type)
2557 (org-let2 gprops lprops
2558 '(funcall type match)))
2559 (t (error "Invalid type in command series"))))
2560 (widen)
2561 (setq org-agenda-redo-command redo)
3ab2c837 2562 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
20908596 2563 (goto-char (point-min)))
c8d0cf5c 2564 (org-fit-agenda-window)
0bd48b37 2565 (org-let (nth 1 series) '(org-finalize-agenda)))
20908596
CD
2566
2567;;;###autoload
2568(defmacro org-batch-agenda (cmd-key &rest parameters)
2569 "Run an agenda command in batch mode and send the result to STDOUT.
2570If CMD-KEY is a string of length 1, it is used as a key in
2571`org-agenda-custom-commands' and triggers this command. If it is a
2572longer string it is used as a tags/todo match string.
86fbb8ca 2573Parameters are alternating variable names and values that will be bound
20908596
CD
2574before running the agenda command."
2575 (let (pars)
2576 (while parameters
2577 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2578 (if (> (length cmd-key) 2)
2579 (eval (list 'let (nreverse pars)
2580 (list 'org-tags-view nil cmd-key)))
2581 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2582 (set-buffer org-agenda-buffer-name)
2583 (princ (org-encode-for-stdout (buffer-string)))))
2584
8bfe682a
CD
2585;(defun org-encode-for-stdout (string)
2586; (if (fboundp 'encode-coding-string)
2587; (encode-coding-string string buffer-file-coding-system)
2588; string))
2589
20908596 2590(defun org-encode-for-stdout (string)
8bfe682a 2591 string)
20908596
CD
2592
2593(defvar org-agenda-info nil)
2594
2595;;;###autoload
2596(defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2597 "Run an agenda command in batch mode and send the result to STDOUT.
2598If CMD-KEY is a string of length 1, it is used as a key in
2599`org-agenda-custom-commands' and triggers this command. If it is a
2600longer string it is used as a tags/todo match string.
86fbb8ca 2601Parameters are alternating variable names and values that will be bound
20908596
CD
2602before running the agenda command.
2603
2604The output gives a line for each selected agenda item. Each
2605item is a list of comma-separated values, like this:
2606
2607category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2608
2609category The category of the item
2610head The headline, without TODO kwd, TAGS and PRIORITY
2611type The type of the agenda entry, can be
2612 todo selected in TODO match
2613 tagsmatch selected in tags match
2614 diary imported from diary
2615 deadline a deadline on given date
2616 scheduled scheduled on given date
2617 timestamp entry has timestamp on given date
2618 closed entry was closed on given date
2619 upcoming-deadline warning about deadline
2620 past-scheduled forwarded scheduled item
2621 block entry has date block including g. date
2622todo The todo keyword, if any
2623tags All tags including inherited ones, separated by colons
2624date The relevant date, like 2007-2-14
2625time The time, like 15:00-16:50
2626extra Sting with extra planning info
2627priority-l The priority letter if any was given
2628priority-n The computed numerical priority
2629agenda-day The day in the agenda where this is listed"
2630
2631 (let (pars)
2632 (while parameters
2633 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2634 (push (list 'org-agenda-remove-tags t) pars)
2635 (if (> (length cmd-key) 2)
2636 (eval (list 'let (nreverse pars)
2637 (list 'org-tags-view nil cmd-key)))
2638 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2639 (set-buffer org-agenda-buffer-name)
2640 (let* ((lines (org-split-string (buffer-string) "\n"))
2641 line)
2642 (while (setq line (pop lines))
2643 (catch 'next
2644 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2645 (setq org-agenda-info
2646 (org-fix-agenda-info (text-properties-at 0 line)))
2647 (princ
2648 (org-encode-for-stdout
2649 (mapconcat 'org-agenda-export-csv-mapper
ed21c5c8 2650 '(org-category txt type todo tags date time extra
20908596
CD
2651 priority-letter priority agenda-day)
2652 ",")))
2653 (princ "\n"))))))
2654
2655(defun org-fix-agenda-info (props)
86fbb8ca
CD
2656 "Make sure all properties on an agenda item have a canonical form.
2657This ensures the export commands can easily use it."
20908596
CD
2658 (let (tmp re)
2659 (when (setq tmp (plist-get props 'tags))
2660 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2661 (when (setq tmp (plist-get props 'date))
2662 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2663 (let ((calendar-date-display-form '(year "-" month "-" day)))
2664 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2665
2666 (setq tmp (calendar-date-string tmp)))
2667 (setq props (plist-put props 'date tmp)))
2668 (when (setq tmp (plist-get props 'day))
2669 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2670 (let ((calendar-date-display-form '(year "-" month "-" day)))
2671 (setq tmp (calendar-date-string tmp)))
2672 (setq props (plist-put props 'day tmp))
2673 (setq props (plist-put props 'agenda-day tmp)))
2674 (when (setq tmp (plist-get props 'txt))
2675 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2676 (plist-put props 'priority-letter (match-string 1 tmp))
2677 (setq tmp (replace-match "" t t tmp)))
2678 (when (and (setq re (plist-get props 'org-todo-regexp))
2679 (setq re (concat "\\`\\.*" re " ?"))
2680 (string-match re tmp))
2681 (plist-put props 'todo (match-string 1 tmp))
2682 (setq tmp (replace-match "" t t tmp)))
2683 (plist-put props 'txt tmp)))
2684 props)
2685
2686(defun org-agenda-export-csv-mapper (prop)
2687 (let ((res (plist-get org-agenda-info prop)))
2688 (setq res
2689 (cond
2690 ((not res) "")
2691 ((stringp res) res)
2692 (t (prin1-to-string res))))
2693 (while (string-match "," res)
2694 (setq res (replace-match ";" t t res)))
2695 (org-trim res)))
2696
2697
2698;;;###autoload
2699(defun org-store-agenda-views (&rest parameters)
2700 (interactive)
2701 (eval (list 'org-batch-store-agenda-views)))
2702
2703;; FIXME, why is this a macro?????
2704;;;###autoload
2705(defmacro org-batch-store-agenda-views (&rest parameters)
2706 "Run all custom agenda commands that have a file argument."
2707 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2708 (pop-up-frames nil)
2709 (dir default-directory)
2c3ad40d 2710 pars cmd thiscmdkey files opts cmd-or-set)
20908596
CD
2711 (while parameters
2712 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2713 (setq pars (reverse pars))
2714 (save-window-excursion
2715 (while cmds
2716 (setq cmd (pop cmds)
2717 thiscmdkey (car cmd)
2c3ad40d
CD
2718 cmd-or-set (nth 2 cmd)
2719 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2720 files (nth (if (listp cmd-or-set) 4 5) cmd))
20908596
CD
2721 (if (stringp files) (setq files (list files)))
2722 (when files
2723 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2724 (list 'org-agenda nil thiscmdkey)))
2725 (set-buffer org-agenda-buffer-name)
2726 (while files
2727 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2728 (list 'org-write-agenda
c8d0cf5c 2729 (expand-file-name (pop files) dir) nil t))))
20908596
CD
2730 (and (get-buffer org-agenda-buffer-name)
2731 (kill-buffer org-agenda-buffer-name)))))))
2732
8d642074
CD
2733(defun org-agenda-mark-header-line (pos)
2734 "Mark the line at POS as an agenda structure header."
2735 (save-excursion
2736 (goto-char pos)
2737 (put-text-property (point-at-bol) (point-at-eol)
2738 'org-agenda-structural-header t)
2739 (when org-agenda-title-append
2740 (put-text-property (point-at-bol) (point-at-eol)
2741 'org-agenda-title-append org-agenda-title-append))))
2742
8bfe682a 2743(defvar org-mobile-creating-agendas)
c8d0cf5c 2744(defun org-write-agenda (file &optional open nosettings)
20908596
CD
2745 "Write the current buffer (an agenda view) as a file.
2746Depending on the extension of the file name, plain text (.txt),
7877f373
JB
2747HTML (.html or .htm) or PostScript (.ps) is produced.
2748If the extension is .ics, run iCalendar export over all files used
20908596
CD
2749to construct the agenda and limit the export to entries listed in the
2750agenda now.
8bfe682a 2751With prefix argument OPEN, open the new file immediately.
20908596
CD
2752If NOSETTINGS is given, do not scope the settings of
2753`org-agenda-exporter-settings' into the export commands. This is used when
2754the settings have already been scoped and we do not wish to overrule other,
2755higher priority settings."
c8d0cf5c 2756 (interactive "FWrite agenda to file: \nP")
20908596
CD
2757 (if (not (file-writable-p file))
2758 (error "Cannot write agenda to file %s" file))
20908596 2759 (org-let (if nosettings nil org-agenda-exporter-settings)
afe98dfa 2760 '(save-excursion
20908596 2761 (save-window-excursion
93b62de8 2762 (org-agenda-mark-filtered-text)
8bfe682a 2763 (let ((bs (copy-sequence (buffer-string))) beg)
93b62de8
CD
2764 (org-agenda-unmark-filtered-text)
2765 (with-temp-buffer
afe98dfa
CD
2766 (rename-buffer "Agenda View" t)
2767 (set-buffer-modified-p nil)
20908596 2768 (insert bs)
93b62de8
CD
2769 (org-agenda-remove-marked-text 'org-filtered)
2770 (while (setq beg (text-property-any (point-min) (point-max)
2771 'org-filtered t))
2772 (delete-region
2773 beg (or (next-single-property-change beg 'org-filtered)
2774 (point-max))))
c8d0cf5c 2775 (run-hooks 'org-agenda-before-write-hook)
93b62de8 2776 (cond
8bfe682a
CD
2777 ((org-bound-and-true-p org-mobile-creating-agendas)
2778 (org-mobile-write-agenda-for-mobile file))
93b62de8 2779 ((string-match "\\.html?\\'" file)
afe98dfa 2780 (require 'htmlize)
93b62de8 2781 (set-buffer (htmlize-buffer (current-buffer)))
ff4be292 2782
93b62de8
CD
2783 (when (and org-agenda-export-html-style
2784 (string-match "<style>" org-agenda-export-html-style))
2785 ;; replace <style> section with org-agenda-export-html-style
2786 (goto-char (point-min))
2787 (kill-region (- (search-forward "<style") 6)
2788 (search-forward "</style>"))
2789 (insert org-agenda-export-html-style))
2790 (write-file file)
2791 (kill-buffer (current-buffer))
2792 (message "HTML written to %s" file))
2793 ((string-match "\\.ps\\'" file)
c8d0cf5c 2794 (require 'ps-print)
afe98dfa 2795 (ps-print-buffer-with-faces file)
7877f373 2796 (message "PostScript written to %s" file))
c8d0cf5c
CD
2797 ((string-match "\\.pdf\\'" file)
2798 (require 'ps-print)
afe98dfa
CD
2799 (ps-print-buffer-with-faces
2800 (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c
CD
2801 (call-process "ps2pdf" nil nil nil
2802 (expand-file-name
2803 (concat (file-name-sans-extension file) ".ps"))
2804 (expand-file-name file))
afe98dfa 2805 (delete-file (concat (file-name-sans-extension file) ".ps"))
c8d0cf5c 2806 (message "PDF written to %s" file))
93b62de8 2807 ((string-match "\\.ics\\'" file)
c8d0cf5c 2808 (require 'org-icalendar)
93b62de8
CD
2809 (let ((org-agenda-marker-table
2810 (org-create-marker-find-array
2811 (org-agenda-collect-markers)))
2812 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2813 (org-combined-agenda-icalendar-file file))
2814 (apply 'org-export-icalendar 'combine
2815 (org-agenda-files nil 'ifmode))))
2816 (t
2817 (let ((bs (buffer-string)))
2818 (find-file file)
2819 (erase-buffer)
2820 (insert bs)
2821 (save-buffer 0)
2822 (kill-buffer (current-buffer))
2823 (message "Plain text written to %s" file))))))))
c8d0cf5c
CD
2824 (set-buffer org-agenda-buffer-name))
2825 (when open (org-open-file file)))
2826
93b62de8
CD
2827(defvar org-agenda-filter-overlays nil)
2828
2829(defun org-agenda-mark-filtered-text ()
2830 "Mark all text hidden by filtering with a text property."
2831 (let ((inhibit-read-only t))
2832 (mapc
2833 (lambda (o)
86fbb8ca 2834 (when (equal (overlay-buffer o) (current-buffer))
93b62de8 2835 (put-text-property
86fbb8ca 2836 (overlay-start o) (overlay-end o)
93b62de8
CD
2837 'org-filtered t)))
2838 org-agenda-filter-overlays)))
2839
2840(defun org-agenda-unmark-filtered-text ()
2841 "Remove the filtering text property."
2842 (let ((inhibit-read-only t))
2843 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2844
2845(defun org-agenda-remove-marked-text (property &optional value)
2846 "Delete all text marked with VALUE of PROPERTY.
2847VALUE defaults to t."
2848 (let (beg)
2849 (setq value (or value t))
2850 (while (setq beg (text-property-any (point-min) (point-max)
2851 property value))
2852 (delete-region
2853 beg (or (next-single-property-change beg 'org-filtered)
2854 (point-max))))))
20908596 2855
c8d0cf5c
CD
2856(defun org-agenda-add-entry-text ()
2857 "Add entry text to agenda lines.
2858This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2859entry text following headings shown in the agenda.
2860Drawers will be excluded, also the line with scheduling/deadline info."
8bfe682a
CD
2861 (when (and (> org-agenda-add-entry-text-maxlines 0)
2862 (not (org-bound-and-true-p org-mobile-creating-agendas)))
54a0dee5 2863 (let (m txt)
c8d0cf5c
CD
2864 (goto-char (point-min))
2865 (while (not (eobp))
8d642074 2866 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
c8d0cf5c 2867 (beginning-of-line 2)
54a0dee5 2868 (setq txt (org-agenda-get-some-entry-text
8d642074 2869 m org-agenda-add-entry-text-maxlines " > "))
c8d0cf5c 2870 (end-of-line 1)
afe98dfa
CD
2871 (if (string-match "\\S-" txt)
2872 (insert "\n" txt)
2873 (or (eobp) (forward-char 1))))))))
c8d0cf5c 2874
8d642074
CD
2875(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2876 &rest keep)
54a0dee5 2877 "Extract entry text from MARKER, at most N-LINES lines.
8d642074
CD
2878This will ignore drawers etc, just get the text.
2879If INDENT is given, prefix every line with this string. If KEEP is
8bfe682a 2880given, it is a list of symbols, defining stuff that should not be
8d642074 2881removed from the entry content. Currently only `planning' is allowed here."
54a0dee5
CD
2882 (let (txt drawer-re kwd-time-re ind)
2883 (save-excursion
2884 (with-current-buffer (marker-buffer marker)
2885 (if (not (org-mode-p))
2886 (setq txt "")
2887 (save-excursion
2888 (save-restriction
2889 (widen)
2890 (goto-char marker)
8d642074 2891 (end-of-line 1)
54a0dee5 2892 (setq txt (buffer-substring
8d642074 2893 (min (1+ (point)) (point-max))
54a0dee5
CD
2894 (progn (outline-next-heading) (point)))
2895 drawer-re org-drawer-regexp
2896 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2897 ".*\n?"))
2898 (with-temp-buffer
2899 (insert txt)
2900 (when org-agenda-add-entry-text-descriptive-links
2901 (goto-char (point-min))
2902 (while (org-activate-bracket-links (point-max))
2903 (add-text-properties (match-beginning 0) (match-end 0)
2904 '(face org-link))))
2905 (goto-char (point-min))
2906 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2907 (set-text-properties (match-beginning 0) (match-end 0)
2908 nil))
2909 (goto-char (point-min))
2910 (while (re-search-forward drawer-re nil t)
2911 (delete-region
2912 (match-beginning 0)
2913 (progn (re-search-forward
2914 "^[ \t]*:END:.*\n?" nil 'move)
2915 (point))))
8d642074
CD
2916 (unless (member 'planning keep)
2917 (goto-char (point-min))
2918 (while (re-search-forward kwd-time-re nil t)
2919 (replace-match "")))
54a0dee5 2920 (goto-char (point-min))
8d642074
CD
2921 (when org-agenda-entry-text-exclude-regexps
2922 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2923 (while (setq re (pop re-list))
2924 (goto-char (point-min))
2925 (while (re-search-forward re nil t)
2926 (replace-match "")))))
2927 (goto-char (point-max))
2928 (skip-chars-backward " \t\n")
2929 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2930
2931 ;; find and remove min common indentation
54a0dee5
CD
2932 (goto-char (point-min))
2933 (untabify (point-min) (point-max))
2934 (setq ind (org-get-indentation))
2935 (while (not (eobp))
2936 (unless (looking-at "[ \t]*$")
2937 (setq ind (min ind (org-get-indentation))))
2938 (beginning-of-line 2))
2939 (goto-char (point-min))
2940 (while (not (eobp))
2941 (unless (looking-at "[ \t]*$")
2942 (move-to-column ind)
2943 (delete-region (point-at-bol) (point)))
2944 (beginning-of-line 2))
8d642074
CD
2945
2946 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2947
54a0dee5 2948 (goto-char (point-min))
8d642074
CD
2949 (when indent
2950 (while (and (not (eobp)) (re-search-forward "^" nil t))
2951 (replace-match indent t t)))
54a0dee5
CD
2952 (goto-char (point-min))
2953 (while (looking-at "[ \t]*\n") (replace-match ""))
2954 (goto-char (point-max))
2955 (when (> (org-current-line)
2956 n-lines)
2957 (org-goto-line (1+ n-lines))
2958 (backward-char 1))
2959 (setq txt (buffer-substring (point-min) (point)))))))))
2960 txt))
2961
20908596
CD
2962(defun org-agenda-collect-markers ()
2963 "Collect the markers pointing to entries in the agenda buffer."
2964 (let (m markers)
2965 (save-excursion
2966 (goto-char (point-min))
2967 (while (not (eobp))
8d642074
CD
2968 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2969 (org-get-at-bol 'org-marker)))
20908596
CD
2970 (push m markers))
2971 (beginning-of-line 2)))
2972 (nreverse markers)))
2973
2974(defun org-create-marker-find-array (marker-list)
58179cce 2975 "Create an alist of files names with all marker positions in that file."
20908596
CD
2976 (let (f tbl m a p)
2977 (while (setq m (pop marker-list))
2978 (setq p (marker-position m)
2979 f (buffer-file-name (or (buffer-base-buffer
2980 (marker-buffer m))
2981 (marker-buffer m))))
2982 (if (setq a (assoc f tbl))
2983 (push (marker-position m) (cdr a))
2984 (push (list f p) tbl)))
2985 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2986 tbl)))
2987
33306645 2988(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
20908596
CD
2989(defun org-check-agenda-marker-table ()
2990 "Check of the current entry is on the marker list."
2991 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2992 a)
2993 (and (setq a (assoc file org-agenda-marker-table))
2994 (save-match-data
2995 (save-excursion
2996 (org-back-to-heading t)
2997 (member (point) (cdr a)))))))
2998
2999(defun org-check-for-org-mode ()
7877f373 3000 "Make sure current buffer is in Org-mode. Error if not."
20908596 3001 (or (org-mode-p)
f924a367 3002 (error "Cannot execute org-mode agenda command on buffer in %s"
20908596
CD
3003 major-mode)))
3004
3005(defun org-fit-agenda-window ()
3006 "Fit the window to the buffer size."
3007 (and (memq org-agenda-window-setup '(reorganize-frame))
3008 (fboundp 'fit-window-to-buffer)
93b62de8 3009 (org-fit-window-to-buffer
20908596
CD
3010 nil
3011 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3012 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3013
3014;;; Agenda prepare and finalize
3015
33306645 3016(defvar org-agenda-multi nil) ; dynamically scoped
20908596
CD
3017(defvar org-agenda-buffer-name "*Org Agenda*")
3018(defvar org-pre-agenda-window-conf nil)
3019(defvar org-agenda-columns-active nil)
3020(defvar org-agenda-name nil)
71d35b24 3021(defvar org-agenda-filter nil)
acedf35c 3022(defvar org-agenda-filter-while-redo nil)
c8d0cf5c
CD
3023(defvar org-agenda-filter-preset nil
3024 "A preset of the tags filter used for secondary agenda filtering.
86fbb8ca 3025This must be a list of strings, each string must be a single tag preceded
c8d0cf5c
CD
3026by \"+\" or \"-\".
3027This variable should not be set directly, but agenda custom commands can
afe98dfa
CD
3028bind it in the options section. The preset filter is a global property of
3029the entire agenda view. In a block agenda, it will not work reliably to
3030define a filter for one of the individual blocks. You need to set it in
3031the global options and expect it to be applied to the entire view.")
c8d0cf5c 3032
20908596
CD
3033(defun org-prepare-agenda (&optional name)
3034 (setq org-todo-keywords-for-agenda nil)
3035 (setq org-done-keywords-for-agenda nil)
8d642074 3036 (setq org-drawers-for-agenda nil)
86fbb8ca
CD
3037 (unless org-agenda-persistent-filter
3038 (setq org-agenda-filter nil))
c8d0cf5c 3039 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
20908596
CD
3040 (if org-agenda-multi
3041 (progn
3042 (setq buffer-read-only nil)
3043 (goto-char (point-max))
3ab2c837
BG
3044 (unless (or (bobp) org-agenda-compact-blocks
3045 (not org-agenda-block-separator))
0bd48b37
CD
3046 (insert "\n"
3047 (if (stringp org-agenda-block-separator)
3048 org-agenda-block-separator
3049 (make-string (window-width) org-agenda-block-separator))
3050 "\n"))
20908596
CD
3051 (narrow-to-region (point) (point-max)))
3052 (org-agenda-reset-markers)
3053 (setq org-agenda-contributing-files nil)
3054 (setq org-agenda-columns-active nil)
2c3ad40d 3055 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
20908596
CD
3056 (setq org-todo-keywords-for-agenda
3057 (org-uniquify org-todo-keywords-for-agenda))
3058 (setq org-done-keywords-for-agenda
3059 (org-uniquify org-done-keywords-for-agenda))
8d642074 3060 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
20908596
CD
3061 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3062 (awin (get-buffer-window abuf)))
3063 (cond
3064 ((equal (current-buffer) abuf) nil)
3065 (awin (select-window awin))
3066 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3067 ((equal org-agenda-window-setup 'current-window)
c3313451 3068 (switch-to-buffer abuf))
20908596
CD
3069 ((equal org-agenda-window-setup 'other-window)
3070 (org-switch-to-buffer-other-window abuf))
3071 ((equal org-agenda-window-setup 'other-frame)
8d642074 3072 (switch-to-buffer-other-frame abuf))
20908596
CD
3073 ((equal org-agenda-window-setup 'reorganize-frame)
3074 (delete-other-windows)
afe98dfa
CD
3075 (org-switch-to-buffer-other-window abuf)))
3076 ;; additional test in case agenda is invoked from within agenda
3077 ;; buffer via elisp link
3078 (unless (equal (current-buffer) abuf)
c3313451 3079 (switch-to-buffer abuf)))
20908596
CD
3080 (setq buffer-read-only nil)
3081 (let ((inhibit-read-only t)) (erase-buffer))
3082 (org-agenda-mode)
3083 (and name (not org-agenda-name)
3084 (org-set-local 'org-agenda-name name)))
3085 (setq buffer-read-only nil))
3086
3087(defun org-finalize-agenda ()
3088 "Finishing touch for the agenda buffer, called just before displaying it."
3089 (unless org-agenda-multi
3090 (save-excursion
3091 (let ((inhibit-read-only t))
3092 (goto-char (point-min))
3093 (while (org-activate-bracket-links (point-max))
3094 (add-text-properties (match-beginning 0) (match-end 0)
3095 '(face org-link)))
3096 (org-agenda-align-tags)
3097 (unless org-agenda-with-colors
3098 (remove-text-properties (point-min) (point-max) '(face nil))))
33306645
CD
3099 (if (and (boundp 'org-agenda-overriding-columns-format)
3100 org-agenda-overriding-columns-format)
3101 (org-set-local 'org-agenda-overriding-columns-format
3102 org-agenda-overriding-columns-format))
20908596
CD
3103 (if (and (boundp 'org-agenda-view-columns-initially)
3104 org-agenda-view-columns-initially)
3105 (org-agenda-columns))
3106 (when org-agenda-fontify-priorities
c8d0cf5c 3107 (org-agenda-fontify-priorities))
d6685abc
CD
3108 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3109 (org-agenda-dim-blocked-tasks))
54a0dee5
CD
3110 (org-agenda-mark-clocking-task)
3111 (when org-agenda-entry-text-mode
3112 (org-agenda-entry-text-hide)
f924a367 3113 (org-agenda-entry-text-show))
8bfe682a
CD
3114 (if (functionp 'org-habit-insert-consistency-graphs)
3115 (org-habit-insert-consistency-graphs))
20908596 3116 (run-hooks 'org-finalize-agenda-hook)
8d642074 3117 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
86fbb8ca 3118 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
c8d0cf5c 3119 (org-agenda-filter-apply org-agenda-filter))
20908596
CD
3120 )))
3121
54a0dee5
CD
3122(defun org-agenda-mark-clocking-task ()
3123 "Mark the current clock entry in the agenda if it is present."
3124 (mapc (lambda (o)
86fbb8ca
CD
3125 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3126 (delete-overlay o)))
3127 (overlays-in (point-min) (point-max)))
54a0dee5
CD
3128 (when (marker-buffer org-clock-hd-marker)
3129 (save-excursion
3130 (goto-char (point-min))
3131 (let (s ov)
3132 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3133 (goto-char s)
8d642074 3134 (when (equal (org-get-at-bol 'org-hd-marker)
54a0dee5 3135 org-clock-hd-marker)
86fbb8ca
CD
3136 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3137 (overlay-put ov 'type 'org-agenda-clocking)
3138 (overlay-put ov 'face 'org-agenda-clocking)
3139 (overlay-put ov 'help-echo
54a0dee5
CD
3140 "The clock is running in this item")))))))
3141
c8d0cf5c 3142(defun org-agenda-fontify-priorities ()
20908596
CD
3143 "Make highest priority lines bold, and lowest italic."
3144 (interactive)
86fbb8ca
CD
3145 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3146 (delete-overlay o)))
3147 (overlays-in (point-min) (point-max)))
20908596
CD
3148 (save-excursion
3149 (let ((inhibit-read-only t)
3150 b e p ov h l)
3151 (goto-char (point-min))
3152 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3153 (setq h (or (get-char-property (point) 'org-highest-priority)
3154 org-highest-priority)
3155 l (or (get-char-property (point) 'org-lowest-priority)
3156 org-lowest-priority)
3157 p (string-to-char (match-string 1))
c8d0cf5c
CD
3158 b (match-beginning 0)
3159 e (if (eq org-agenda-fontify-priorities 'cookies)
3160 (match-end 0)
3161 (point-at-eol))
86fbb8ca
CD
3162 ov (make-overlay b e))
3163 (overlay-put
20908596 3164 ov 'face
ed21c5c8
CD
3165 (cond ((org-face-from-face-or-color
3166 'priority nil
3167 (cdr (assoc p org-priority-faces))))
c8d0cf5c 3168 ((and (listp org-agenda-fontify-priorities)
ed21c5c8
CD
3169 (org-face-from-face-or-color
3170 'priority nil
3171 (cdr (assoc p org-agenda-fontify-priorities)))))
20908596
CD
3172 ((equal p l) 'italic)
3173 ((equal p h) 'bold)))
86fbb8ca 3174 (overlay-put ov 'org-type 'org-priority)))))
20908596 3175
d6685abc
CD
3176(defun org-agenda-dim-blocked-tasks ()
3177 "Dim currently blocked TODO's in the agenda display."
86fbb8ca
CD
3178 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3179 (delete-overlay o)))
3180 (overlays-in (point-min) (point-max)))
d6685abc
CD
3181 (save-excursion
3182 (let ((inhibit-read-only t)
72d06d81 3183 (org-depend-tag-blocked nil)
d6685abc 3184 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
c8d0cf5c
CD
3185 org-blocked-by-checkboxes
3186 invis1 b e p ov h l)
d6685abc
CD
3187 (goto-char (point-min))
3188 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3189 (and pos (goto-char (1+ pos))))
c8d0cf5c 3190 (setq org-blocked-by-checkboxes nil invis1 invis)
8d642074 3191 (let ((marker (org-get-at-bol 'org-hd-marker)))
d6685abc
CD
3192 (when (and marker
3193 (not (with-current-buffer (marker-buffer marker)
3194 (save-excursion
3195 (goto-char marker)
c8d0cf5c
CD
3196 (if (org-entry-get nil "NOBLOCKING")
3197 t ;; Never block this entry
3198 (run-hook-with-args-until-failure
3199 'org-blocker-hook
3200 (list :type 'todo-state-change
3201 :position marker
3202 :from 'todo
3203 :to 'done)))))))
3204 (if org-blocked-by-checkboxes (setq invis1 nil))
53e31a31
CD
3205 (setq b (if invis1
3206 (max (point-min) (1- (point-at-bol)))
3207 (point-at-bol))
d6685abc 3208 e (point-at-eol)
86fbb8ca 3209 ov (make-overlay b e))
c8d0cf5c 3210 (if invis1
86fbb8ca
CD
3211 (overlay-put ov 'invisible t)
3212 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3213 (overlay-put ov 'org-type 'org-blocked-todo)))))))
20908596
CD
3214
3215(defvar org-agenda-skip-function nil
3216 "Function to be called at each match during agenda construction.
3217If this function returns nil, the current match should not be skipped.
3218Otherwise, the function must return a position from where the search
3219should be continued.
3220This may also be a Lisp form, it will be evaluated.
3221Never set this variable using `setq' or so, because then it will apply
3ab2c837
BG
3222to all future agenda commands. If you do want a global skipping condition,
3223use the option `org-agenda-skip-function-global' instead.
3224The correct usage for `org-agenda-skip-function' is to bind it with
3225`let' to scope it dynamically into the agenda-constructing command.
3226A good way to set it is through options in `org-agenda-custom-commands'.")
20908596
CD
3227
3228(defun org-agenda-skip ()
3229 "Throw to `:skip' in places that should be skipped.
3230Also moves point to the end of the skipped region, so that search can
3231continue from there."
3ab2c837 3232 (let ((p (point-at-bol)) to)
2c3ad40d 3233 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
20908596
CD
3234 (get-text-property p :org-archived)
3235 (org-end-of-subtree t)
3236 (throw :skip t))
b349f79f
CD
3237 (and org-agenda-skip-comment-trees
3238 (get-text-property p :org-comment)
20908596
CD
3239 (org-end-of-subtree t)
3240 (throw :skip t))
3241 (if (equal (char-after p) ?#) (throw :skip t))
3ab2c837
BG
3242 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3243 (org-agenda-skip-eval org-agenda-skip-function)))
20908596
CD
3244 (goto-char to)
3245 (throw :skip t))))
3246
3ab2c837
BG
3247(defun org-agenda-skip-eval (form)
3248 "If FORM is a function or a list, call (or eval) is and return result.
3249`save-excursion' and `save-match-data' are wrapped around the call, so point
3250and match data are returned to the previous state no matter what these
3251functions do."
3252 (let (fp)
3253 (and form
3254 (or (setq fp (functionp form))
3255 (consp form))
3256 (save-excursion
3257 (save-match-data
3258 (if fp
3259 (funcall form)
3260 (eval form)))))))
3261
20908596
CD
3262(defvar org-agenda-markers nil
3263 "List of all currently active markers created by `org-agenda'.")
54a0dee5 3264(defvar org-agenda-last-marker-time (org-float-time)
20908596
CD
3265 "Creation time of the last agenda marker.")
3266
3267(defun org-agenda-new-marker (&optional pos)
3268 "Return a new agenda marker.
3269Org-mode keeps a list of these markers and resets them when they are
3270no longer in use."
3271 (let ((m (copy-marker (or pos (point)))))
54a0dee5 3272 (setq org-agenda-last-marker-time (org-float-time))
20908596
CD
3273 (push m org-agenda-markers)
3274 m))
3275
3276(defun org-agenda-reset-markers ()
3277 "Reset markers created by `org-agenda'."
3278 (while org-agenda-markers
3279 (move-marker (pop org-agenda-markers) nil)))
3280
b349f79f
CD
3281(defun org-agenda-save-markers-for-cut-and-paste (beg end)
3282 "Save relative positions of markers in region."
3283 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3284 org-agenda-markers))
3285
54a0dee5
CD
3286;;; Entry text mode
3287
3288(defun org-agenda-entry-text-show-here ()
8bfe682a 3289 "Add some text from the entry as context to the current line."
54a0dee5 3290 (let (m txt o)
8d642074 3291 (setq m (org-get-at-bol 'org-hd-marker))
54a0dee5
CD
3292 (unless (marker-buffer m)
3293 (error "No marker points to an entry here"))
3294 (setq txt (concat "\n" (org-no-properties
3295 (org-agenda-get-some-entry-text
8d642074 3296 m org-agenda-entry-text-maxlines " > "))))
54a0dee5 3297 (when (string-match "\\S-" txt)
86fbb8ca
CD
3298 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3299 (overlay-put o 'evaporate t)
3300 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3301 (overlay-put o 'after-string txt))))
54a0dee5
CD
3302
3303(defun org-agenda-entry-text-show ()
3304 "Add entry context for all agenda lines."
3305 (interactive)
3306 (save-excursion
3307 (goto-char (point-max))
3308 (beginning-of-line 1)
3309 (while (not (bobp))
8d642074 3310 (when (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
3311 (org-agenda-entry-text-show-here))
3312 (beginning-of-line 0))))
3313
3314(defun org-agenda-entry-text-hide ()
3315 "Remove any shown entry context."
3316 (delq nil
3317 (mapcar (lambda (o)
86fbb8ca 3318 (if (eq (overlay-get o 'org-overlay-type)
54a0dee5 3319 'agenda-entry-content)
86fbb8ca
CD
3320 (progn (delete-overlay o) t)))
3321 (overlays-in (point-min) (point-max)))))
54a0dee5 3322
acedf35c
CD
3323(defun org-agenda-get-day-face (date)
3324 "Return the face DATE should be displayed with."
3325 (or (and (functionp org-agenda-day-face-function)
3326 (funcall org-agenda-day-face-function date))
3327 (cond ((org-agenda-todayp date)
3328 'org-agenda-date-today)
3329 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3330 'org-agenda-date-weekend)
3331 (t 'org-agenda-date))))
3332
20908596
CD
3333;;; Agenda timeline
3334
3335(defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3336
3337(defun org-timeline (&optional include-all)
3338 "Show a time-sorted view of the entries in the current org file.
3339Only entries with a time stamp of today or later will be listed. With
3340\\[universal-argument] prefix, all unfinished TODO items will also be shown,
3341under the current date.
3342If the buffer contains an active region, only check the region for
3343dates."
3344 (interactive "P")
20908596
CD
3345 (org-compile-prefix-format 'timeline)
3346 (org-set-sorting-strategy 'timeline)
3347 (let* ((dopast t)
3348 (dotodo include-all)
3349 (doclosed org-agenda-show-log)
afe98dfa
CD
3350 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3351 (current-buffer))))
20908596
CD
3352 (date (calendar-current-date))
3353 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3354 (end (if (org-region-active-p) (region-end) (point-max)))
3355 (day-numbers (org-get-all-dates beg end 'no-ranges
3356 t doclosed ; always include today
3357 org-timeline-show-empty-dates))
3358 (org-deadline-warning-days 0)
3359 (org-agenda-only-exact-dates t)
acedf35c 3360 (today (org-today))
20908596
CD
3361 (past t)
3362 args
acedf35c 3363 s e rtn d emptyp)
20908596
CD
3364 (setq org-agenda-redo-command
3365 (list 'progn
3366 (list 'org-switch-to-buffer-other-window (current-buffer))
3367 (list 'org-timeline (list 'quote include-all))))
3368 (if (not dopast)
3369 ;; Remove past dates from the list of dates.
3370 (setq day-numbers (delq nil (mapcar (lambda(x)
3371 (if (>= x today) x nil))
3372 day-numbers))))
afe98dfa 3373 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
20908596
CD
3374 (if doclosed (push :closed args))
3375 (push :timestamp args)
3376 (push :deadline args)
3377 (push :scheduled args)
3378 (push :sexp args)
3379 (if dotodo (push :todo args))
8d642074
CD
3380 (insert "Timeline of file " entry "\n")
3381 (add-text-properties (point-min) (point)
3382 (list 'face 'org-agenda-structure))
3383 (org-agenda-mark-header-line (point-min))
20908596
CD
3384 (while (setq d (pop day-numbers))
3385 (if (and (listp d) (eq (car d) :omitted))
3386 (progn
3387 (setq s (point))
3388 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3389 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3390 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3391 (if (and (>= d today)
3392 dopast
3393 past)
3394 (progn
3395 (setq past nil)
3396 (insert (make-string 79 ?-) "\n")))
acedf35c 3397 (setq date (calendar-gregorian-from-absolute d))
20908596
CD
3398 (setq s (point))
3399 (setq rtn (and (not emptyp)
3400 (apply 'org-agenda-get-day-entries entry
3401 date args)))
3402 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3403 (progn
3404 (insert
3405 (if (stringp org-agenda-format-date)
3406 (format-time-string org-agenda-format-date
3407 (org-time-from-absolute date))
3408 (funcall org-agenda-format-date date))
3409 "\n")
3410 (put-text-property s (1- (point)) 'face
acedf35c 3411 (org-agenda-get-day-face date))
20908596 3412 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3413 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596
CD
3414 (if (equal d today)
3415 (put-text-property s (1- (point)) 'org-today t))
3416 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3417 (put-text-property s (1- (point)) 'day d)))))
3418 (goto-char (point-min))
3419 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3420 (point-min)))
3421 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3422 (org-finalize-agenda)
3423 (setq buffer-read-only t)))
3424
3425(defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3426 "Return a list of all relevant day numbers from BEG to END buffer positions.
3427If NO-RANGES is non-nil, include only the start and end dates of a range,
3428not every single day in the range. If FORCE-TODAY is non-nil, make
3429sure that TODAY is included in the list. If INACTIVE is non-nil, also
3430inactive time stamps (those in square brackets) are included.
3431When EMPTY is non-nil, also include days without any entries."
3432 (let ((re (concat
3433 (if pre-re pre-re "")
3434 (if inactive org-ts-regexp-both org-ts-regexp)))
3435 dates dates1 date day day1 day2 ts1 ts2)
3436 (if force-today
acedf35c 3437 (setq dates (list (org-today))))
20908596
CD
3438 (save-excursion
3439 (goto-char beg)
3440 (while (re-search-forward re end t)
3441 (setq day (time-to-days (org-time-string-to-time
3442 (substring (match-string 1) 0 10))))
3443 (or (memq day dates) (push day dates)))
3444 (unless no-ranges
3445 (goto-char beg)
3446 (while (re-search-forward org-tr-regexp end t)
3447 (setq ts1 (substring (match-string 1) 0 10)
3448 ts2 (substring (match-string 2) 0 10)
3449 day1 (time-to-days (org-time-string-to-time ts1))
3450 day2 (time-to-days (org-time-string-to-time ts2)))
3451 (while (< (setq day1 (1+ day1)) day2)
3452 (or (memq day1 dates) (push day1 dates)))))
3453 (setq dates (sort dates '<))
3454 (when empty
3455 (while (setq day (pop dates))
3456 (setq day2 (car dates))
3457 (push day dates1)
3458 (when (and day2 empty)
3459 (if (or (eq empty t)
3460 (and (numberp empty) (<= (- day2 day) empty)))
3461 (while (< (setq day (1+ day)) day2)
3462 (push (list day) dates1))
3463 (push (cons :omitted (- day2 day)) dates1))))
3464 (setq dates (nreverse dates1)))
3465 dates)))
3466
3467;;; Agenda Daily/Weekly
3468
c8d0cf5c 3469(defvar org-agenda-start-day nil ; dynamically scoped parameter
3ab2c837
BG
3470"Start day for the agenda view.
3471Custom commands can set this variable in the options section.")
20908596 3472(defvar org-starting-day nil) ; local variable in the agenda buffer
acedf35c
CD
3473(defvar org-agenda-current-span nil
3474 "The current span used in the agenda view.") ; local variable in the agenda buffer
20908596 3475(defvar org-include-all-loc nil) ; local variable
20908596 3476
ed21c5c8
CD
3477(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3478 "List of types searched for when creating the daily/weekly agenda.
3479This variable is a list of symbols that controls the types of
3480items that appear in the daily/weekly agenda. Allowed symbols in this
3481list are are
3482
3483 :timestamp List items containing a date stamp or date range matching
3484 the selected date. This includes sexp entries in
3485 angular brackets.
3486
3487 :sexp List entries resulting from plain diary-like sexps.
3488
3489 :deadline List deadline due on that date. When the date is today,
3490 also list any deadlines past due, or due within
3491 `org-deadline-warning-days'. `:deadline' must appear before
3492 `:scheduled' if the setting of
3493 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3494 any effect.
3495
3496 :scheduled List all items which are scheduled for the given date.
3497 The diary for *today* also contains items which were
3498 scheduled earlier and are not yet marked DONE.
3499
3500By default, all four types are turned on.
3501
3502Never set this variable globally using `setq', because then it
3503will apply to all future agenda commands. Instead, bind it with
9b053e76 3504`let' to scope it dynamically into the agenda-constructing
ed21c5c8
CD
3505command. A good way to set it is through options in
3506`org-agenda-custom-commands'. For a more flexible (though
3507somewhat less efficient) way of determining what is included in
3508the daily/weekly agenda, see `org-agenda-skip-function'.")
3509
20908596 3510;;;###autoload
acedf35c 3511(defun org-agenda-list (&optional include-all start-day span)
20908596
CD
3512 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3513The view will be for the current day or week, but from the overview buffer
3514you will be able to go to other days/weeks.
3515
20908596 3516With a numeric prefix argument in an interactive call, the agenda will
acedf35c
CD
3517span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3518the number of days. SPAN defaults to `org-agenda-span'.
20908596
CD
3519
3520START-DAY defaults to TODAY, or to the most recent match for the weekday
3521given in `org-agenda-start-on-weekday'."
3522 (interactive "P")
3523 (if (and (integerp include-all) (> include-all 0))
acedf35c
CD
3524 (setq span include-all include-all nil))
3525 (setq start-day (or start-day org-agenda-start-day))
20908596
CD
3526 (if org-agenda-overriding-arguments
3527 (setq include-all (car org-agenda-overriding-arguments)
3528 start-day (nth 1 org-agenda-overriding-arguments)
acedf35c 3529 span (nth 2 org-agenda-overriding-arguments)))
20908596
CD
3530 (if (stringp start-day)
3531 ;; Convert to an absolute day number
3532 (setq start-day (time-to-days (org-read-date nil t start-day))))
acedf35c 3533 (setq org-agenda-last-arguments (list include-all start-day span))
20908596
CD
3534 (org-compile-prefix-format 'agenda)
3535 (org-set-sorting-strategy 'agenda)
3ab2c837
BG
3536 (let* ((span (org-agenda-ndays-to-span
3537 (or span org-agenda-ndays org-agenda-span)))
acedf35c
CD
3538 (today (org-today))
3539 (sd (or start-day today))
3540 (ndays (org-agenda-span-to-ndays span sd))
3541 (org-agenda-start-on-weekday
3542 (if (eq ndays 7)
3543 org-agenda-start-on-weekday))
2c3ad40d 3544 (thefiles (org-agenda-files nil 'ifmode))
20908596 3545 (files thefiles)
20908596 3546 (start (if (or (null org-agenda-start-on-weekday)
acedf35c 3547 (< ndays 7))
20908596
CD
3548 sd
3549 (let* ((nt (calendar-day-of-week
3550 (calendar-gregorian-from-absolute sd)))
3551 (n1 org-agenda-start-on-weekday)
3552 (d (- nt n1)))
3553 (- sd (+ (if (< d 0) 7 0) d)))))
3554 (day-numbers (list start))
3555 (day-cnt 0)
3556 (inhibit-redisplay (not debug-on-error))
acedf35c
CD
3557 s e rtn rtnall file date d start-pos end-pos todayp
3558 clocktable-start clocktable-end filter)
20908596 3559 (setq org-agenda-redo-command
acedf35c
CD
3560 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3561 (dotimes (n (1- ndays))
3562 (push (1+ (car day-numbers)) day-numbers))
20908596
CD
3563 (setq day-numbers (nreverse day-numbers))
3564 (setq clocktable-start (car day-numbers)
3565 clocktable-end (1+ (or (org-last day-numbers) 0)))
3566 (org-prepare-agenda "Day/Week")
3567 (org-set-local 'org-starting-day (car day-numbers))
3568 (org-set-local 'org-include-all-loc include-all)
acedf35c 3569 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
20908596
CD
3570 (unless org-agenda-compact-blocks
3571 (let* ((d1 (car day-numbers))
3572 (d2 (org-last day-numbers))
3573 (w1 (org-days-to-iso-week d1))
3574 (w2 (org-days-to-iso-week d2)))
3575 (setq s (point))
c8d0cf5c
CD
3576 (if org-agenda-overriding-header
3577 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3578 nil 'face 'org-agenda-structure) "\n")
acedf35c 3579 (insert (org-agenda-span-name span)
c8d0cf5c
CD
3580 "-agenda"
3581 (if (< (- d2 d1) 350)
3582 (if (= w1 w2)
3583 (format " (W%02d)" w1)
3584 (format " (W%02d-W%02d)" w1 w2))
3585 "")
3586 ":\n")))
20908596 3587 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
8d642074
CD
3588 'org-date-line t))
3589 (org-agenda-mark-header-line s))
20908596
CD
3590 (while (setq d (pop day-numbers))
3591 (setq date (calendar-gregorian-from-absolute d)
20908596
CD
3592 s (point))
3593 (if (or (setq todayp (= d today))
3594 (and (not start-pos) (= d sd)))
3595 (setq start-pos (point))
3596 (if (and start-pos (not end-pos))
3597 (setq end-pos (point))))
3598 (setq files thefiles
3599 rtnall nil)
3600 (while (setq file (pop files))
3601 (catch 'nextfile
3602 (org-check-agenda-file file)
ed21c5c8
CD
3603 (let ((org-agenda-entry-types org-agenda-entry-types))
3604 (unless org-agenda-include-deadlines
3605 (setq org-agenda-entry-types
3606 (delq :deadline org-agenda-entry-types)))
3607 (cond
3ab2c837 3608 ((memq org-agenda-show-log '(only clockcheck))
ed21c5c8
CD
3609 (setq rtn (org-agenda-get-day-entries
3610 file date :closed)))
3611 (org-agenda-show-log
3612 (setq rtn (apply 'org-agenda-get-day-entries
3613 file date
3614 (append '(:closed) org-agenda-entry-types))))
3615 (t
3616 (setq rtn (apply 'org-agenda-get-day-entries
3617 file date
3618 org-agenda-entry-types)))))
20908596
CD
3619 (setq rtnall (append rtnall rtn))))
3620 (if org-agenda-include-diary
c8d0cf5c 3621 (let ((org-agenda-search-headline-for-time t))
20908596
CD
3622 (require 'diary-lib)
3623 (setq rtn (org-get-entries-from-diary date))
3624 (setq rtnall (append rtnall rtn))))
3625 (if (or rtnall org-agenda-show-all-dates)
3626 (progn
3627 (setq day-cnt (1+ day-cnt))
3628 (insert
3629 (if (stringp org-agenda-format-date)
3630 (format-time-string org-agenda-format-date
3631 (org-time-from-absolute date))
3632 (funcall org-agenda-format-date date))
3633 "\n")
3634 (put-text-property s (1- (point)) 'face
acedf35c 3635 (org-agenda-get-day-face date))
20908596 3636 (put-text-property s (1- (point)) 'org-date-line t)
8d642074 3637 (put-text-property s (1- (point)) 'org-agenda-date-header t)
20908596 3638 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
c8d0cf5c 3639 (when todayp
acedf35c 3640 (put-text-property s (1- (point)) 'org-today t))
20908596
CD
3641 (if rtnall (insert
3642 (org-finalize-agenda-entries
3643 (org-agenda-add-time-grid-maybe
acedf35c 3644 rtnall ndays todayp))
20908596
CD
3645 "\n"))
3646 (put-text-property s (1- (point)) 'day d)
3647 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3648 (when (and org-agenda-clockreport-mode clocktable-start)
2c3ad40d 3649 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
20908596 3650 ;; the above line is to ensure the restricted range!
3ab2c837 3651 (p (copy-sequence org-agenda-clockreport-parameter-plist))
20908596
CD
3652 tbl)
3653 (setq p (org-plist-delete p :block))
3654 (setq p (plist-put p :tstart clocktable-start))
3655 (setq p (plist-put p :tend clocktable-end))
3656 (setq p (plist-put p :scope 'agenda))
acedf35c
CD
3657 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3658 (setq filter (or org-agenda-filter-while-redo
3659 (get 'org-agenda-filter :preset-filter))))
3660 (setq p (plist-put p :tags (mapconcat (lambda (x)
3661 (if (string-match "[<>=]" x)
3662 ""
3663 x))
3664 filter ""))))
20908596
CD
3665 (setq tbl (apply 'org-get-clocktable p))
3666 (insert tbl)))
3667 (goto-char (point-min))
c8d0cf5c 3668 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3669 (unless (and (pos-visible-in-window-p (point-min))
3670 (pos-visible-in-window-p (point-max)))
3671 (goto-char (1- (point-max)))
3672 (recenter -1)
3673 (if (not (pos-visible-in-window-p (or start-pos 1)))
3674 (progn
3675 (goto-char (or start-pos 1))
3676 (recenter 1))))
3677 (goto-char (or start-pos 1))
3678 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3ab2c837
BG
3679 (if (eq org-agenda-show-log 'clockcheck)
3680 (org-agenda-show-clocking-issues))
20908596
CD
3681 (org-finalize-agenda)
3682 (setq buffer-read-only t)
3683 (message "")))
3684
3685(defun org-agenda-ndays-to-span (n)
acedf35c
CD
3686 "Return a span symbol for a span of N days, or N if none matches."
3687 (cond ((symbolp n) n)
3688 ((= n 1) 'day)
3689 ((= n 7) 'week)
3690 (t n)))
3691
3692(defun org-agenda-span-to-ndays (span start-day)
3693 "Return ndays from SPAN starting at START-DAY."
3694 (cond ((numberp span) span)
3695 ((eq span 'day) 1)
3696 ((eq span 'week) 7)
3697 ((eq span 'month)
3698 (let ((date (calendar-gregorian-from-absolute start-day)))
3699 (calendar-last-day-of-month (car date) (caddr date))))
3700 ((eq span 'year)
3701 (let ((date (calendar-gregorian-from-absolute start-day)))
3702 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3703
3704(defun org-agenda-span-name (span)
3705 "Return a SPAN name."
3706 (if (null span)
3707 ""
3708 (if (symbolp span)
3709 (capitalize (symbol-name span))
3710 (format "%d days" span))))
20908596
CD
3711
3712;;; Agenda word search
3713
3714(defvar org-agenda-search-history nil)
3715(defvar org-todo-only nil)
3716
3717(defvar org-search-syntax-table nil
7877f373
JB
3718 "Special syntax table for Org-mode search.
3719In this table, we have single quotes not as word constituents, so
3720that when \"+Ameli\" is searched as a word, it will also match \"Ameli's\"")
20908596
CD
3721
3722(defun org-search-syntax-table ()
3723 (unless org-search-syntax-table
3724 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3725 (modify-syntax-entry ?' "." org-search-syntax-table)
3726 (modify-syntax-entry ?` "." org-search-syntax-table))
3727 org-search-syntax-table)
3728
ed21c5c8
CD
3729(defvar org-agenda-last-search-view-search-was-boolean nil)
3730
20908596
CD
3731;;;###autoload
3732(defun org-search-view (&optional todo-only string edit-at)
ed21c5c8 3733 "Show all entries that contain a phrase or words or regular expressions.
20908596
CD
3734
3735With optional prefix argument TODO-ONLY, only consider entries that are
3736TODO entries. The argument STRING can be used to pass a default search
3737string into this function. If EDIT-AT is non-nil, it means that the
3738user should get a chance to edit this string, with cursor at position
3739EDIT-AT.
3740
ed21c5c8
CD
3741The search string can be viewed either as a phrase that should be found as
3742is, or it can be broken into a number of snippets, each of which must match
3743in a Boolean way to select an entry. The default depends on the variable
3744`org-agenda-search-view-always-boolean'.
3745Even if this is turned off (the default) you can always switch to
86fbb8ca 3746Boolean search dynamically by preceding the first word with \"+\" or \"-\".
ed21c5c8
CD
3747
3748The default is a direct search of the whole phrase, where each space in
3749the search string can expand to an arbitrary amount of whitespace,
3750including newlines.
3751
3752If using a Boolean search, the search string is split on whitespace and
3753each snippet is searched separately, with logical AND to select an entry.
3754Words prefixed with a minus must *not* occur in the entry. Words without
3755a prefix or prefixed with a plus must occur in the entry. Matching is
3756case-insensitive. Words are enclosed by word delimiters (i.e. they must
3757match whole words, not parts of a word) if
3758`org-agenda-search-view-force-full-words' is set (default is nil).
3759
3760Boolean search snippets enclosed by curly braces are interpreted as
86fbb8ca 3761regular expressions that must or (when preceded with \"-\") must not
ed21c5c8 3762match in the entry. Snippets enclosed into double quotes will be taken
86fbb8ca 3763as a whole, to include whitespace.
ed21c5c8
CD
3764
3765- If the search string starts with an asterisk, search only in headlines.
3766- If (possibly after the leading star) the search string starts with an
3767 exclamation mark, this also means to look at TODO entries only, an effect
3768 that can also be achieved with a prefix argument.
3769- If (possibly after star and exclamation mark) the search string starts
3770 with a colon, this will mean that the (non-regexp) snippets of the
3771 Boolean search must match as full words.
20908596
CD
3772
3773This command searches the agenda files, and in addition the files listed
3774in `org-agenda-text-search-extra-files'."
3775 (interactive "P")
3776 (org-compile-prefix-format 'search)
3777 (org-set-sorting-strategy 'search)
3778 (org-prepare-agenda "SEARCH")
3779 (let* ((props (list 'face nil
c8d0cf5c 3780 'done-face 'org-agenda-done
20908596
CD
3781 'org-not-done-regexp org-not-done-regexp
3782 'org-todo-regexp org-todo-regexp
b349f79f 3783 'org-complex-heading-regexp org-complex-heading-regexp
20908596 3784 'mouse-face 'highlight
20908596 3785 'help-echo (format "mouse-2 or RET jump to location")))
ed21c5c8 3786 (full-words org-agenda-search-view-force-full-words)
86fbb8ca 3787 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
20908596 3788 regexp rtn rtnall files file pos
ed21c5c8 3789 marker category tags c neg re boolean
20908596
CD
3790 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3791 (unless (and (not edit-at)
3792 (stringp string)
3793 (string-match "\\S-" string))
ed21c5c8
CD
3794 (setq string (read-string
3795 (if org-agenda-search-view-always-boolean
3796 "[+-]Word/{Regexp} ...: "
3797 "Phrase, or [+-]Word/{Regexp} ...: ")
3798 (cond
3799 ((integerp edit-at) (cons string edit-at))
3800 (edit-at string))
3801 'org-agenda-search-history)))
20908596
CD
3802 (org-set-local 'org-todo-only todo-only)
3803 (setq org-agenda-redo-command
3804 (list 'org-search-view (if todo-only t nil) string
3805 '(if current-prefix-arg 1 nil)))
3806 (setq org-agenda-query-string string)
3807
3808 (if (equal (string-to-char string) ?*)
3809 (setq hdl-only t
3810 words (substring string 1))
3811 (setq words string))
3812 (when (equal (string-to-char words) ?!)
3813 (setq todo-only t
3814 words (substring words 1)))
ed21c5c8
CD
3815 (when (equal (string-to-char words) ?:)
3816 (setq full-words t
3817 words (substring words 1)))
3818 (if (or org-agenda-search-view-always-boolean
3819 (member (string-to-char words) '(?- ?+ ?\{)))
3820 (setq boolean t))
20908596 3821 (setq words (org-split-string words))
afe98dfa
CD
3822 (let (www w)
3823 (while (setq w (pop words))
3824 (while (and (string-match "\\\\\\'" w) words)
3825 (setq w (concat (substring w 0 -1) " " (pop words))))
3826 (push w www))
3827 (setq words (nreverse www) www nil)
3828 (while (setq w (pop words))
3829 (when (and (string-match "\\`[-+]?{" w)
3830 (not (string-match "}\\'" w)))
3831 (while (and words (not (string-match "}\\'" (car words))))
3832 (setq w (concat w " " (pop words))))
3833 (setq w (concat w " " (pop words))))
3834 (push w www))
3835 (setq words (nreverse www)))
ed21c5c8
CD
3836 (setq org-agenda-last-search-view-search-was-boolean boolean)
3837 (when boolean
3838 (let (wds w)
3839 (while (setq w (pop words))
3840 (if (or (equal (substring w 0 1) "\"")
3841 (and (> (length w) 1)
3842 (member (substring w 0 1) '("+" "-"))
3843 (equal (substring w 1 2) "\"")))
3844 (while (and words (not (equal (substring w -1) "\"")))
3845 (setq w (concat w " " (pop words)))))
3846 (and (string-match "\\`\\([-+]?\\)\"" w)
3847 (setq w (replace-match "\\1" nil nil w)))
3848 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3849 (push w wds))
3850 (setq words (nreverse wds))))
3851 (if boolean
8bfe682a
CD
3852 (mapc (lambda (w)
3853 (setq c (string-to-char w))
3854 (if (equal c ?-)
3855 (setq neg t w (substring w 1))
3856 (if (equal c ?+)
3857 (setq neg nil w (substring w 1))
ed21c5c8 3858 (setq neg nil)))
8bfe682a
CD
3859 (if (string-match "\\`{.*}\\'" w)
3860 (setq re (substring w 1 -1))
ed21c5c8
CD
3861 (if full-words
3862 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3863 (setq re (regexp-quote (downcase w)))))
8bfe682a
CD
3864 (if neg (push re regexps-) (push re regexps+)))
3865 words)
3866 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3867 regexps+))
20908596
CD
3868 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3869 (if (not regexps+)
3ab2c837 3870 (setq regexp org-outline-regexp-bol)
20908596
CD
3871 (setq regexp (pop regexps+))
3872 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3873 regexp))))
2c3ad40d 3874 (setq files (org-agenda-files nil 'ifmode))
20908596
CD
3875 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3876 (pop org-agenda-text-search-extra-files)
3877 (setq files (org-add-archive-files files)))
3878 (setq files (append files org-agenda-text-search-extra-files)
3879 rtnall nil)
3880 (while (setq file (pop files))
3881 (setq ee nil)
3882 (catch 'nextfile
3883 (org-check-agenda-file file)
3884 (setq buffer (if (file-exists-p file)
3885 (org-get-agenda-file-buffer file)
3886 (error "No such file %s" file)))
3887 (if (not buffer)
3888 ;; If file does not exist, make sure an error message is sent
3889 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3890 file))))
3891 (with-current-buffer buffer
3892 (with-syntax-table (org-search-syntax-table)
3893 (unless (org-mode-p)
3894 (error "Agenda file %s is not in `org-mode'" file))
3895 (let ((case-fold-search t))
3896 (save-excursion
3897 (save-restriction
3898 (if org-agenda-restrict
3899 (narrow-to-region org-agenda-restrict-begin
3900 org-agenda-restrict-end)
3901 (widen))
3902 (goto-char (point-min))
3903 (unless (or (org-on-heading-p)
3904 (outline-next-heading))
3905 (throw 'nextfile t))
3906 (goto-char (max (point-min) (1- (point))))
3907 (while (re-search-forward regexp nil t)
3908 (org-back-to-heading t)
3909 (skip-chars-forward "* ")
3910 (setq beg (point-at-bol)
3911 beg1 (point)
3912 end (progn (outline-next-heading) (point)))
3913 (catch :skip
3914 (goto-char beg)
3915 (org-agenda-skip)
3916 (setq str (buffer-substring-no-properties
3917 (point-at-bol)
3918 (if hdl-only (point-at-eol) end)))
3919 (mapc (lambda (wr) (when (string-match wr str)
3920 (goto-char (1- end))
3921 (throw :skip t)))
3922 regexps-)
3923 (mapc (lambda (wr) (unless (string-match wr str)
3924 (goto-char (1- end))
3925 (throw :skip t)))
3926 (if todo-only
3927 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3928 regexps+)
3929 regexps+))
3930 (goto-char beg)
3931 (setq marker (org-agenda-new-marker (point))
3932 category (org-get-category)
3933 tags (org-get-tags-at (point))
3934 txt (org-format-agenda-item
3935 ""
3936 (buffer-substring-no-properties
3937 beg1 (point-at-eol))
3938 category tags))
3939 (org-add-props txt props
3940 'org-marker marker 'org-hd-marker marker
3941 'org-todo-regexp org-todo-regexp
b349f79f 3942 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
3943 'priority 1000 'org-category category
3944 'type "search")
3945 (push txt ee)
3946 (goto-char (1- end))))))))))
3947 (setq rtn (nreverse ee))
3948 (setq rtnall (append rtnall rtn)))
3949 (if org-agenda-overriding-header
3950 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3951 nil 'face 'org-agenda-structure) "\n")
3952 (insert "Search words: ")
3953 (add-text-properties (point-min) (1- (point))
3954 (list 'face 'org-agenda-structure))
3955 (setq pos (point))
3956 (insert string "\n")
3957 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3958 (setq pos (point))
3959 (unless org-agenda-multi
3960 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3961 (add-text-properties pos (1- (point))
3962 (list 'face 'org-agenda-structure))))
8d642074 3963 (org-agenda-mark-header-line (point-min))
20908596
CD
3964 (when rtnall
3965 (insert (org-finalize-agenda-entries rtnall) "\n"))
3966 (goto-char (point-min))
c8d0cf5c 3967 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
3968 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3969 (org-finalize-agenda)
3970 (setq buffer-read-only t)))
3971
3972;;; Agenda TODO list
3973
3974(defvar org-select-this-todo-keyword nil)
3975(defvar org-last-arg nil)
3976
3977;;;###autoload
3978(defun org-todo-list (arg)
86fbb8ca 3979 "Show all (not done) TODO entries from all agenda file in a single list.
20908596
CD
3980The prefix arg can be used to select a specific TODO keyword and limit
3981the list to these. When using \\[universal-argument], you will be prompted
3982for a keyword. A numeric prefix directly selects the Nth keyword in
3983`org-todo-keywords-1'."
3984 (interactive "P")
20908596
CD
3985 (org-compile-prefix-format 'todo)
3986 (org-set-sorting-strategy 'todo)
3987 (org-prepare-agenda "TODO")
ed21c5c8 3988 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
acedf35c 3989 (let* ((today (org-today))
20908596
CD
3990 (date (calendar-gregorian-from-absolute today))
3991 (kwds org-todo-keywords-for-agenda)
3992 (completion-ignore-case t)
3993 (org-select-this-todo-keyword
3994 (if (stringp arg) arg
3995 (and arg (integerp arg) (> arg 0)
3996 (nth (1- arg) kwds))))
3997 rtn rtnall files file pos)
3998 (when (equal arg '(4))
3999 (setq org-select-this-todo-keyword
54a0dee5 4000 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
20908596
CD
4001 (mapcar 'list kwds) nil nil)))
4002 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4003 (org-set-local 'org-last-arg arg)
4004 (setq org-agenda-redo-command
4005 '(org-todo-list (or current-prefix-arg org-last-arg)))
2c3ad40d 4006 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
4007 rtnall nil)
4008 (while (setq file (pop files))
4009 (catch 'nextfile
4010 (org-check-agenda-file file)
4011 (setq rtn (org-agenda-get-day-entries file date :todo))
4012 (setq rtnall (append rtnall rtn))))
4013 (if org-agenda-overriding-header
4014 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4015 nil 'face 'org-agenda-structure) "\n")
4016 (insert "Global list of TODO items of type: ")
4017 (add-text-properties (point-min) (1- (point))
8d642074
CD
4018 (list 'face 'org-agenda-structure
4019 'short-heading
4020 (concat "ToDo: "
4021 (or org-select-this-todo-keyword "ALL"))))
4022 (org-agenda-mark-header-line (point-min))
20908596
CD
4023 (setq pos (point))
4024 (insert (or org-select-this-todo-keyword "ALL") "\n")
4025 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4026 (setq pos (point))
4027 (unless org-agenda-multi
4028 (insert "Available with `N r': (0)ALL")
4029 (let ((n 0) s)
4030 (mapc (lambda (x)
4031 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4032 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4033 (insert "\n "))
4034 (insert " " s))
4035 kwds))
4036 (insert "\n"))
4037 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4038 (org-agenda-mark-header-line (point-min))
20908596
CD
4039 (when rtnall
4040 (insert (org-finalize-agenda-entries rtnall) "\n"))
4041 (goto-char (point-min))
c8d0cf5c 4042 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4043 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4044 (org-finalize-agenda)
4045 (setq buffer-read-only t)))
4046
4047;;; Agenda tags match
4048
4049;;;###autoload
4050(defun org-tags-view (&optional todo-only match)
4051 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4052The prefix arg TODO-ONLY limits the search to TODO entries."
4053 (interactive "P")
4054 (org-compile-prefix-format 'tags)
4055 (org-set-sorting-strategy 'tags)
4056 (let* ((org-tags-match-list-sublevels
c8d0cf5c 4057 org-tags-match-list-sublevels)
20908596
CD
4058 (completion-ignore-case t)
4059 rtn rtnall files file pos matcher
4060 buffer)
ed21c5c8
CD
4061 (when (and (stringp match) (not (string-match "\\S-" match)))
4062 (setq match nil))
20908596
CD
4063 (setq matcher (org-make-tags-matcher match)
4064 match (car matcher) matcher (cdr matcher))
4065 (org-prepare-agenda (concat "TAGS " match))
4066 (setq org-agenda-query-string match)
4067 (setq org-agenda-redo-command
4068 (list 'org-tags-view (list 'quote todo-only)
4069 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2c3ad40d 4070 (setq files (org-agenda-files nil 'ifmode)
20908596
CD
4071 rtnall nil)
4072 (while (setq file (pop files))
4073 (catch 'nextfile
4074 (org-check-agenda-file file)
4075 (setq buffer (if (file-exists-p file)
4076 (org-get-agenda-file-buffer file)
4077 (error "No such file %s" file)))
4078 (if (not buffer)
33306645 4079 ;; If file does not exist, error message to agenda
20908596
CD
4080 (setq rtn (list
4081 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4082 rtnall (append rtnall rtn))
4083 (with-current-buffer buffer
4084 (unless (org-mode-p)
4085 (error "Agenda file %s is not in `org-mode'" file))
4086 (save-excursion
4087 (save-restriction
4088 (if org-agenda-restrict
4089 (narrow-to-region org-agenda-restrict-begin
4090 org-agenda-restrict-end)
4091 (widen))
4092 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4093 (setq rtnall (append rtnall rtn))))))))
4094 (if org-agenda-overriding-header
4095 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4096 nil 'face 'org-agenda-structure) "\n")
4097 (insert "Headlines with TAGS match: ")
4098 (add-text-properties (point-min) (1- (point))
8d642074
CD
4099 (list 'face 'org-agenda-structure
4100 'short-heading
4101 (concat "Match: " match)))
20908596
CD
4102 (setq pos (point))
4103 (insert match "\n")
4104 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4105 (setq pos (point))
4106 (unless org-agenda-multi
4107 (insert "Press `C-u r' to search again with new search string\n"))
4108 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
8d642074 4109 (org-agenda-mark-header-line (point-min))
20908596
CD
4110 (when rtnall
4111 (insert (org-finalize-agenda-entries rtnall) "\n"))
4112 (goto-char (point-min))
c8d0cf5c 4113 (or org-agenda-multi (org-fit-agenda-window))
20908596
CD
4114 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4115 (org-finalize-agenda)
4116 (setq buffer-read-only t)))
4117
4118;;; Agenda Finding stuck projects
4119
4120(defvar org-agenda-skip-regexp nil
4121 "Regular expression used in skipping subtrees for the agenda.
4122This is basically a temporary global variable that can be set and then
4123used by user-defined selections using `org-agenda-skip-function'.")
4124
4125(defvar org-agenda-overriding-header nil
3ab2c837 4126 "When set during agenda, todo and tags searches it replaces the header.
c8d0cf5c
CD
4127This variable should not be set directly, but custom commands can bind it
4128in the options section.")
4129
4130(defun org-agenda-skip-entry-when-regexp-matches ()
86fbb8ca 4131 "Check if the current entry contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4132If yes, it returns the end position of this entry, causing agenda commands
4133to skip the entry but continuing the search in the subtree. This is a
4134function that can be put into `org-agenda-skip-function' for the duration
4135of a command."
4136 (let ((end (save-excursion (org-end-of-subtree t)))
4137 skip)
4138 (save-excursion
4139 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4140 (and skip end)))
20908596
CD
4141
4142(defun org-agenda-skip-subtree-when-regexp-matches ()
86fbb8ca 4143 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
20908596
CD
4144If yes, it returns the end position of this tree, causing agenda commands
4145to skip this subtree. This is a function that can be put into
4146`org-agenda-skip-function' for the duration of a command."
4147 (let ((end (save-excursion (org-end-of-subtree t)))
4148 skip)
4149 (save-excursion
4150 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4151 (and skip end)))
4152
c8d0cf5c 4153(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
86fbb8ca 4154 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
c8d0cf5c
CD
4155If yes, it returns the end position of the current entry (NOT the tree),
4156causing agenda commands to skip the entry but continuing the search in
4157the subtree. This is a function that can be put into
4158`org-agenda-skip-function' for the duration of a command. An important
4159use of this function is for the stuck project list."
4160 (let ((end (save-excursion (org-end-of-subtree t)))
4161 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4162 skip)
4163 (save-excursion
4164 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4165 (and skip entry-end)))
4166
20908596
CD
4167(defun org-agenda-skip-entry-if (&rest conditions)
4168 "Skip entry if any of CONDITIONS is true.
4169See `org-agenda-skip-if' for details."
4170 (org-agenda-skip-if nil conditions))
4171
4172(defun org-agenda-skip-subtree-if (&rest conditions)
4173 "Skip entry if any of CONDITIONS is true.
4174See `org-agenda-skip-if' for details."
4175 (org-agenda-skip-if t conditions))
4176
4177(defun org-agenda-skip-if (subtree conditions)
4178 "Checks current entity for CONDITIONS.
4179If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4180the entry, i.e. the text before the next heading is checked.
4181
4182CONDITIONS is a list of symbols, boolean OR is used to combine the results
4183from different tests. Valid conditions are:
4184
4185scheduled Check if there is a scheduled cookie
4186notscheduled Check if there is no scheduled cookie
4187deadline Check if there is a deadline
4188notdeadline Check if there is no deadline
c8d0cf5c
CD
4189timestamp Check if there is a timestamp (also deadline or scheduled)
4190nottimestamp Check if there is no timestamp (also deadline or scheduled)
20908596
CD
4191regexp Check if regexp matches
4192notregexp Check if regexp does not match.
ed21c5c8
CD
4193todo Check if TODO keyword matches
4194nottodo Check if TODO keyword does not match
20908596
CD
4195
4196The regexp is taken from the conditions list, it must come right after
4197the `regexp' or `notregexp' element.
4198
ed21c5c8
CD
4199`todo' and `nottodo' accept as an argument a list of todo
4200keywords, which may include \"*\" to match any todo keyword.
4201
4202 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4203
4204would skip all entries with \"TODO\" or \"WAITING\" keywords.
4205
4206Instead of a list a keyword class may be given
4207
4208 (org-agenda-skip-entry-if 'nottodo 'done)
4209
4210would skip entries that haven't been marked with any of \"DONE\"
4211keywords. Possible classes are: `todo', `done', `any'.
4212
20908596
CD
4213If any of these conditions is met, this function returns the end point of
4214the entity, causing the search to continue from there. This is a function
4215that can be put into `org-agenda-skip-function' for the duration of a command."
4216 (let (beg end m)
4217 (org-back-to-heading t)
4218 (setq beg (point)
4219 end (if subtree
4220 (progn (org-end-of-subtree t) (point))
4221 (progn (outline-next-heading) (1- (point)))))
4222 (goto-char beg)
4223 (and
4224 (or
4225 (and (memq 'scheduled conditions)
4226 (re-search-forward org-scheduled-time-regexp end t))
4227 (and (memq 'notscheduled conditions)
4228 (not (re-search-forward org-scheduled-time-regexp end t)))
4229 (and (memq 'deadline conditions)
4230 (re-search-forward org-deadline-time-regexp end t))
4231 (and (memq 'notdeadline conditions)
4232 (not (re-search-forward org-deadline-time-regexp end t)))
c8d0cf5c
CD
4233 (and (memq 'timestamp conditions)
4234 (re-search-forward org-ts-regexp end t))
4235 (and (memq 'nottimestamp conditions)
4236 (not (re-search-forward org-ts-regexp end t)))
20908596
CD
4237 (and (setq m (memq 'regexp conditions))
4238 (stringp (nth 1 m))
4239 (re-search-forward (nth 1 m) end t))
4240 (and (setq m (memq 'notregexp conditions))
4241 (stringp (nth 1 m))
ed21c5c8
CD
4242 (not (re-search-forward (nth 1 m) end t)))
4243 (and (or
4244 (setq m (memq 'todo conditions))
4245 (setq m (memq 'nottodo conditions)))
4246 (org-agenda-skip-if-todo m end)))
20908596
CD
4247 end)))
4248
ed21c5c8
CD
4249(defun org-agenda-skip-if-todo (args end)
4250 "Helper function for `org-agenda-skip-if', do not use it directly.
4251ARGS is a list with first element either `todo' or `nottodo'.
4252The remainder is either a list of TODO keywords, or a state symbol
4253`todo' or `done' or `any'."
4254 (let ((kw (car args))
4255 (arg (cadr args))
4256 todo-wds todo-re)
4257 (setq todo-wds
4258 (org-uniquify
4259 (cond
4260 ((listp arg) ;; list of keywords
4261 (if (member "*" arg)
4262 (mapcar 'substring-no-properties org-todo-keywords-1)
4263 arg))
4264 ((symbolp arg) ;; keyword class name
4265 (cond
4266 ((eq arg 'todo)
4267 (org-delete-all org-done-keywords
4268 (mapcar 'substring-no-properties
4269 org-todo-keywords-1)))
4270 ((eq arg 'done) org-done-keywords)
4271 ((eq arg 'any)
4272 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4273 (setq todo-re
4274 (concat "^\\*+[ \t]+\\<\\("
4275 (mapconcat 'identity todo-wds "\\|")
4276 "\\)\\>"))
4277 (if (eq kw 'todo)
4278 (re-search-forward todo-re end t)
4279 (not (re-search-forward todo-re end t)))))
4280
20908596
CD
4281;;;###autoload
4282(defun org-agenda-list-stuck-projects (&rest ignore)
4283 "Create agenda view for projects that are stuck.
4284Stuck projects are project that have no next actions. For the definitions
4285of what a project is and how to check if it stuck, customize the variable
afe98dfa 4286`org-stuck-projects'."
20908596 4287 (interactive)
c8d0cf5c
CD
4288 (let* ((org-agenda-skip-function
4289 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
20908596 4290 ;; We could have used org-agenda-skip-if here.
c8d0cf5c
CD
4291 (org-agenda-overriding-header
4292 (or org-agenda-overriding-header "List of stuck projects: "))
20908596
CD
4293 (matcher (nth 0 org-stuck-projects))
4294 (todo (nth 1 org-stuck-projects))
4295 (todo-wds (if (member "*" todo)
4296 (progn
2c3ad40d
CD
4297 (org-prepare-agenda-buffers (org-agenda-files
4298 nil 'ifmode))
20908596
CD
4299 (org-delete-all
4300 org-done-keywords-for-agenda
4301 (copy-sequence org-todo-keywords-for-agenda)))
4302 todo))
4303 (todo-re (concat "^\\*+[ \t]+\\("
4304 (mapconcat 'identity todo-wds "\\|")
4305 "\\)\\>"))
4306 (tags (nth 2 org-stuck-projects))
4307 (tags-re (if (member "*" tags)
3ab2c837
BG
4308 (org-re (concat org-outline-regexp-bol
4309 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
c8d0cf5c 4310 (if tags
3ab2c837
BG
4311 (concat org-outline-regexp-bol
4312 ".*:\\("
c8d0cf5c 4313 (mapconcat 'identity tags "\\|")
afe98dfa 4314 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
20908596
CD
4315 (gen-re (nth 3 org-stuck-projects))
4316 (re-list
4317 (delq nil
4318 (list
4319 (if todo todo-re)
4320 (if tags tags-re)
4321 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4322 gen-re)))))
4323 (setq org-agenda-skip-regexp
4324 (if re-list
4325 (mapconcat 'identity re-list "\\|")
4326 (error "No information how to identify unstuck projects")))
4327 (org-tags-view nil matcher)
4328 (with-current-buffer org-agenda-buffer-name
4329 (setq org-agenda-redo-command
4330 '(org-agenda-list-stuck-projects
4331 (or current-prefix-arg org-last-arg))))))
4332
4333;;; Diary integration
4334
4335(defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4336(defvar list-diary-entries-hook)
3ab2c837 4337(defvar diary-time-regexp)
20908596
CD
4338(defun org-get-entries-from-diary (date)
4339 "Get the (Emacs Calendar) diary entries for DATE."
4340 (require 'diary-lib)
4341 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
20908596 4342 (diary-display-hook '(fancy-diary-display))
ca8ef0dc 4343 (diary-display-function 'fancy-diary-display)
20908596
CD
4344 (pop-up-frames nil)
4345 (list-diary-entries-hook
4346 (cons 'org-diary-default-entry list-diary-entries-hook))
4347 (diary-file-name-prefix-function nil) ; turn this feature off
4348 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4349 entries
4350 (org-disable-agenda-to-diary t))
4351 (save-excursion
4352 (save-window-excursion
4353 (funcall (if (fboundp 'diary-list-entries)
4354 'diary-list-entries 'list-diary-entries)
4355 date 1)))
4356 (if (not (get-buffer diary-fancy-buffer))
4357 (setq entries nil)
4358 (with-current-buffer diary-fancy-buffer
4359 (setq buffer-read-only nil)
4360 (if (zerop (buffer-size))
4361 ;; No entries
4362 (setq entries nil)
4363 ;; Omit the date and other unnecessary stuff
4364 (org-agenda-cleanup-fancy-diary)
4365 ;; Add prefix to each line and extend the text properties
4366 (if (zerop (buffer-size))
4367 (setq entries nil)
3ab2c837
BG
4368 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4369 (setq entries
4370 (with-temp-buffer
4371 (insert entries) (goto-char (point-min))
4372 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4373 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4374 (replace-match (concat "; " (match-string 1)))))
4375 (buffer-string)))))
20908596
CD
4376 (set-buffer-modified-p nil)
4377 (kill-buffer diary-fancy-buffer)))
4378 (when entries
4379 (setq entries (org-split-string entries "\n"))
4380 (setq entries
4381 (mapcar
4382 (lambda (x)
4383 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4384 ;; Extend the text properties to the beginning of the line
4385 (org-add-props x (text-properties-at (1- (length x)) x)
ed21c5c8 4386 'type "diary" 'date date 'face 'org-agenda-diary))
20908596
CD
4387 entries)))))
4388
c8d0cf5c
CD
4389(defvar org-agenda-cleanup-fancy-diary-hook nil
4390 "Hook run when the fancy diary buffer is cleaned up.")
4391
20908596
CD
4392(defun org-agenda-cleanup-fancy-diary ()
4393 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4394This gets rid of the date, the underline under the date, and
4395the dummy entry installed by `org-mode' to ensure non-empty diary for each
4396date. It also removes lines that contain only whitespace."
4397 (goto-char (point-min))
4398 (if (looking-at ".*?:[ \t]*")
4399 (progn
4400 (replace-match "")
4401 (re-search-forward "\n=+$" nil t)
4402 (replace-match "")
4403 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4404 (re-search-forward "\n=+$" nil t)
4405 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4406 (goto-char (point-min))
4407 (while (re-search-forward "^ +\n" nil t)
4408 (replace-match ""))
4409 (goto-char (point-min))
4410 (if (re-search-forward "^Org-mode dummy\n?" nil t)
c8d0cf5c
CD
4411 (replace-match ""))
4412 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
20908596
CD
4413
4414;; Make sure entries from the diary have the right text properties.
4415(eval-after-load "diary-lib"
4416 '(if (boundp 'diary-modify-entry-list-string-function)
4417 ;; We can rely on the hook, nothing to do
4418 nil
33306645 4419 ;; Hook not available, must use advice to make this work
20908596
CD
4420 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4421 "Make the position visible."
4422 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4423 (stringp string)
4424 buffer-file-name)
4425 (setq string (org-modify-diary-entry-string string))))))
4426
4427(defun org-modify-diary-entry-string (string)
7877f373 4428 "Add text properties to string, allowing Org-mode to act on it."
20908596
CD
4429 (org-add-props string nil
4430 'mouse-face 'highlight
20908596
CD
4431 'help-echo (if buffer-file-name
4432 (format "mouse-2 or RET jump to diary file %s"
4433 (abbreviate-file-name buffer-file-name))
4434 "")
4435 'org-agenda-diary-link t
4436 'org-marker (org-agenda-new-marker (point-at-bol))))
4437
4438(defun org-diary-default-entry ()
4439 "Add a dummy entry to the diary.
4440Needed to avoid empty dates which mess up holiday display."
4441 ;; Catch the error if dealing with the new add-to-diary-alist
4442 (when org-disable-agenda-to-diary
4443 (condition-case nil
4444 (org-add-to-diary-list original-date "Org-mode dummy" "")
4445 (error
4446 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4447
4448(defun org-add-to-diary-list (&rest args)
4449 (if (fboundp 'diary-add-to-list)
4450 (apply 'diary-add-to-list args)
4451 (apply 'add-to-diary-list args)))
4452
ed21c5c8
CD
4453(defvar org-diary-last-run-time nil)
4454
20908596
CD
4455;;;###autoload
4456(defun org-diary (&rest args)
4457 "Return diary information from org-files.
4458This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4459It accesses org files and extracts information from those files to be
4460listed in the diary. The function accepts arguments specifying what
ed21c5c8
CD
4461items should be listed. For a list of arguments allowed here, see the
4462variable `org-agenda-entry-types'.
20908596
CD
4463
4464The call in the diary file should look like this:
4465
4466 &%%(org-diary) ~/path/to/some/orgfile.org
4467
4468Use a separate line for each org file to check. Or, if you omit the file name,
4469all files listed in `org-agenda-files' will be checked automatically:
4470
4471 &%%(org-diary)
4472
4473If you don't give any arguments (as in the example above), the default
4474arguments (:deadline :scheduled :timestamp :sexp) are used.
4475So the example above may also be written as
4476
4477 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4478
4479The function expects the lisp variables `entry' and `date' to be provided
4480by the caller, because this is how the calendar works. Don't use this
4481function from a program - use `org-agenda-get-day-entries' instead."
54a0dee5 4482 (when (> (- (org-float-time)
20908596
CD
4483 org-agenda-last-marker-time)
4484 5)
4485 (org-agenda-reset-markers))
4486 (org-compile-prefix-format 'agenda)
4487 (org-set-sorting-strategy 'agenda)
4488 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4489 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4490 (list entry)
4491 (org-agenda-files t)))
ed21c5c8 4492 (time (org-float-time))
20908596 4493 file rtn results)
ed21c5c8
CD
4494 (when (or (not org-diary-last-run-time)
4495 (> (- time
4496 org-diary-last-run-time)
4497 3))
4498 (org-prepare-agenda-buffers files))
4499 (setq org-diary-last-run-time time)
20908596
CD
4500 ;; If this is called during org-agenda, don't return any entries to
4501 ;; the calendar. Org Agenda will list these entries itself.
4502 (if org-disable-agenda-to-diary (setq files nil))
4503 (while (setq file (pop files))
4504 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4505 (setq results (append results rtn)))
4506 (if results
4507 (concat (org-finalize-agenda-entries results) "\n"))))
4508
4509;;; Agenda entry finders
4510
4511(defun org-agenda-get-day-entries (file date &rest args)
4512 "Does the work for `org-diary' and `org-agenda'.
4513FILE is the path to a file to be checked for entries. DATE is date like
4514the one returned by `calendar-current-date'. ARGS are symbols indicating
4515which kind of entries should be extracted. For details about these, see
4516the documentation of `org-diary'."
4517 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4518 (let* ((org-startup-folded nil)
4519 (org-startup-align-all-tables nil)
4520 (buffer (if (file-exists-p file)
4521 (org-get-agenda-file-buffer file)
4522 (error "No such file %s" file)))
54a0dee5 4523 arg results rtn deadline-results)
20908596
CD
4524 (if (not buffer)
4525 ;; If file does not exist, make sure an error message ends up in diary
4526 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4527 (with-current-buffer buffer
4528 (unless (org-mode-p)
4529 (error "Agenda file %s is not in `org-mode'" file))
4530 (let ((case-fold-search nil))
4531 (save-excursion
4532 (save-restriction
4533 (if org-agenda-restrict
4534 (narrow-to-region org-agenda-restrict-begin
4535 org-agenda-restrict-end)
4536 (widen))
4537 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4538 (while (setq arg (pop args))
4539 (cond
4540 ((and (eq arg :todo)
3ab2c837
BG
4541 (equal date (calendar-gregorian-from-absolute
4542 (org-today))))
20908596
CD
4543 (setq rtn (org-agenda-get-todos))
4544 (setq results (append results rtn)))
4545 ((eq arg :timestamp)
4546 (setq rtn (org-agenda-get-blocks))
4547 (setq results (append results rtn))
4548 (setq rtn (org-agenda-get-timestamps))
4549 (setq results (append results rtn)))
4550 ((eq arg :sexp)
4551 (setq rtn (org-agenda-get-sexps))
4552 (setq results (append results rtn)))
4553 ((eq arg :scheduled)
54a0dee5 4554 (setq rtn (org-agenda-get-scheduled deadline-results))
20908596
CD
4555 (setq results (append results rtn)))
4556 ((eq arg :closed)
93b62de8 4557 (setq rtn (org-agenda-get-progress))
20908596
CD
4558 (setq results (append results rtn)))
4559 ((eq arg :deadline)
4560 (setq rtn (org-agenda-get-deadlines))
54a0dee5 4561 (setq deadline-results (copy-sequence rtn))
20908596
CD
4562 (setq results (append results rtn))))))))
4563 results))))
4564
4565(defun org-agenda-get-todos ()
4566 "Return the TODO information for agenda display."
4567 (let* ((props (list 'face nil
c8d0cf5c 4568 'done-face 'org-agenda-done
20908596
CD
4569 'org-not-done-regexp org-not-done-regexp
4570 'org-todo-regexp org-todo-regexp
b349f79f 4571 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4572 'mouse-face 'highlight
20908596
CD
4573 'help-echo
4574 (format "mouse-2 or RET jump to org file %s"
4575 (abbreviate-file-name buffer-file-name))))
4576 (regexp (concat "^\\*+[ \t]+\\("
4577 (if org-select-this-todo-keyword
4578 (if (equal org-select-this-todo-keyword "*")
4579 org-todo-regexp
4580 (concat "\\<\\("
4581 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4582 "\\)\\>"))
4583 org-not-done-regexp)
4584 "[^\n\r]*\\)"))
621f83e4 4585 marker priority category tags todo-state
20908596
CD
4586 ee txt beg end)
4587 (goto-char (point-min))
4588 (while (re-search-forward regexp nil t)
4589 (catch :skip
4590 (save-match-data
4591 (beginning-of-line)
3ab2c837 4592 (org-agenda-skip)
d6685abc 4593 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
0bd48b37 4594 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
20908596
CD
4595 (goto-char (1+ beg))
4596 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4597 (throw :skip nil)))
20908596
CD
4598 (goto-char (match-beginning 1))
4599 (setq marker (org-agenda-new-marker (match-beginning 0))
4600 category (org-get-category)
c8d0cf5c 4601 txt (match-string 1)
20908596 4602 tags (org-get-tags-at (point))
c8d0cf5c 4603 txt (org-format-agenda-item "" txt category tags)
621f83e4
CD
4604 priority (1+ (org-get-priority txt))
4605 todo-state (org-get-todo-state))
20908596
CD
4606 (org-add-props txt props
4607 'org-marker marker 'org-hd-marker marker
4608 'priority priority 'org-category category
621f83e4 4609 'type "todo" 'todo-state todo-state)
20908596
CD
4610 (push txt ee)
4611 (if org-agenda-todo-list-sublevels
4612 (goto-char (match-end 1))
4613 (org-end-of-subtree 'invisible))))
4614 (nreverse ee)))
4615
3ab2c837
BG
4616(defun org-agenda-todo-custom-ignore-p (time n)
4617 "Check whether timestamp is farther away then n number of days.
4618This function is invoked if `org-agenda-todo-ignore-deadlines',
4619`org-agenda-todo-ignore-scheduled' or
4620`org-agenda-todo-ignore-timestamp' is set to an integer."
4621 (let ((days (org-days-to-time time)))
4622 (if (>= n 0)
4623 (>= days n)
4624 (<= days n))))
4625
0bd48b37 4626;;;###autoload
ed21c5c8
CD
4627(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4628 (&optional end)
4629 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
0bd48b37
CD
4630 (when (or org-agenda-todo-ignore-with-date
4631 org-agenda-todo-ignore-scheduled
acedf35c
CD
4632 org-agenda-todo-ignore-deadlines
4633 org-agenda-todo-ignore-timestamp)
0bd48b37
CD
4634 (setq end (or end (save-excursion (outline-next-heading) (point))))
4635 (save-excursion
4636 (or (and org-agenda-todo-ignore-with-date
4637 (re-search-forward org-ts-regexp end t))
4638 (and org-agenda-todo-ignore-scheduled
ed21c5c8
CD
4639 (re-search-forward org-scheduled-time-regexp end t)
4640 (cond
4641 ((eq org-agenda-todo-ignore-scheduled 'future)
4642 (> (org-days-to-time (match-string 1)) 0))
4643 ((eq org-agenda-todo-ignore-scheduled 'past)
4644 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4645 ((numberp org-agenda-todo-ignore-scheduled)
4646 (org-agenda-todo-custom-ignore-p
4647 (match-string 1) org-agenda-todo-ignore-scheduled))
ed21c5c8 4648 (t)))
0bd48b37
CD
4649 (and org-agenda-todo-ignore-deadlines
4650 (re-search-forward org-deadline-time-regexp end t)
ed21c5c8
CD
4651 (cond
4652 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4653 ((eq org-agenda-todo-ignore-deadlines 'far)
4654 (not (org-deadline-close (match-string 1))))
4655 ((eq org-agenda-todo-ignore-deadlines 'future)
4656 (> (org-days-to-time (match-string 1)) 0))
4657 ((eq org-agenda-todo-ignore-deadlines 'past)
4658 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4659 ((numberp org-agenda-todo-ignore-deadlines)
4660 (org-agenda-todo-custom-ignore-p
4661 (match-string 1) org-agenda-todo-ignore-deadlines))
acedf35c
CD
4662 (t (org-deadline-close (match-string 1)))))
4663 (and org-agenda-todo-ignore-timestamp
4664 (let ((buffer (current-buffer))
4665 (regexp
4666 (concat
4667 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4668 (start (point)))
4669 ;; Copy current buffer into a temporary one
4670 (with-temp-buffer
4671 (insert-buffer-substring buffer start end)
4672 (goto-char (point-min))
4673 ;; Delete SCHEDULED and DEADLINE items
4674 (while (re-search-forward regexp end t)
4675 (delete-region (match-beginning 0) (match-end 0)))
4676 (goto-char (point-min))
4677 ;; No search for timestamp left
4678 (when (re-search-forward org-ts-regexp nil t)
4679 (cond
4680 ((eq org-agenda-todo-ignore-timestamp 'future)
4681 (> (org-days-to-time (match-string 1)) 0))
4682 ((eq org-agenda-todo-ignore-timestamp 'past)
4683 (<= (org-days-to-time (match-string 1)) 0))
3ab2c837
BG
4684 ((numberp org-agenda-todo-ignore-timestamp)
4685 (org-agenda-todo-custom-ignore-p
4686 (match-string 1) org-agenda-todo-ignore-timestamp))
acedf35c 4687 (t))))))))))
0bd48b37 4688
20908596
CD
4689(defconst org-agenda-no-heading-message
4690 "No heading for this item in buffer or region.")
4691
4692(defun org-agenda-get-timestamps ()
4693 "Return the date stamp information for agenda display."
4694 (let* ((props (list 'face nil
4695 'org-not-done-regexp org-not-done-regexp
4696 'org-todo-regexp org-todo-regexp
b349f79f 4697 'org-complex-heading-regexp org-complex-heading-regexp
20908596 4698 'mouse-face 'highlight
20908596
CD
4699 'help-echo
4700 (format "mouse-2 or RET jump to org file %s"
4701 (abbreviate-file-name buffer-file-name))))
4702 (d1 (calendar-absolute-from-gregorian date))
4703 (remove-re
4704 (concat
4705 (regexp-quote
4706 (format-time-string
4707 "<%Y-%m-%d"
4708 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4709 ".*?>"))
4710 (regexp
4711 (concat
4712 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4713 (regexp-quote
4714 (substring
4715 (format-time-string
4716 (car org-time-stamp-formats)
4717 (apply 'encode-time ; DATE bound by calendar
4718 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4719 1 11))
4720 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4721 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4722 marker hdmarker deadlinep scheduledp clockp closedp inactivep
621f83e4 4723 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3ab2c837 4724 todo-state end-of-match show-all)
20908596 4725 (goto-char (point-min))
c8d0cf5c 4726 (while (setq end-of-match (re-search-forward regexp nil t))
20908596 4727 (setq b0 (match-beginning 0)
3ab2c837
BG
4728 b3 (match-beginning 3) e3 (match-end 3)
4729 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4730 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4731 (member todo-state
4732 org-agenda-repeating-timestamp-show-all)))
20908596
CD
4733 (catch :skip
4734 (and (org-at-date-range-p) (throw :skip nil))
4735 (org-agenda-skip)
4736 (if (and (match-end 1)
4737 (not (= d1 (org-time-string-to-absolute
3ab2c837 4738 (match-string 1) d1 nil show-all))))
20908596
CD
4739 (throw :skip nil))
4740 (if (and e3
4741 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4742 (throw :skip nil))
c8d0cf5c 4743 (setq tmp (buffer-substring (max (point-min)
20908596
CD
4744 (- b0 org-ds-keyword-length))
4745 b0)
4746 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4747 inactivep (= (char-after b0) ?\[)
4748 deadlinep (string-match org-deadline-regexp tmp)
4749 scheduledp (string-match org-scheduled-regexp tmp)
4750 closedp (and org-agenda-include-inactive-timestamps
4751 (string-match org-closed-string tmp))
4752 clockp (and org-agenda-include-inactive-timestamps
4753 (or (string-match org-clock-string tmp)
4754 (string-match "]-+\\'" tmp)))
621f83e4 4755 donep (member todo-state org-done-keywords))
c8d0cf5c
CD
4756 (if (or scheduledp deadlinep closedp clockp
4757 (and donep org-agenda-skip-timestamp-if-done))
20908596
CD
4758 (throw :skip t))
4759 (if (string-match ">" timestr)
4760 ;; substring should only run to end of time stamp
4761 (setq timestr (substring timestr 0 (match-end 0))))
c8d0cf5c
CD
4762 (setq marker (org-agenda-new-marker b0)
4763 category (org-get-category b0))
20908596 4764 (save-excursion
3ab2c837 4765 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
4766 (setq txt org-agenda-no-heading-message)
4767 (goto-char (match-beginning 0))
4768 (setq hdmarker (org-agenda-new-marker)
4769 tags (org-get-tags-at))
4770 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3ab2c837 4771 (setq head (or (match-string 1) ""))
c8d0cf5c 4772 (setq txt (org-format-agenda-item
ed21c5c8 4773 (if inactivep org-agenda-inactive-leader nil)
3ab2c837 4774 head category tags timestr
c8d0cf5c 4775 remove-re)))
20908596
CD
4776 (setq priority (org-get-priority txt))
4777 (org-add-props txt props
4778 'org-marker marker 'org-hd-marker hdmarker)
4779 (org-add-props txt nil 'priority priority
4780 'org-category category 'date date
621f83e4 4781 'todo-state todo-state
20908596
CD
4782 'type "timestamp")
4783 (push txt ee))
c8d0cf5c
CD
4784 (if org-agenda-skip-additional-timestamps-same-entry
4785 (outline-next-heading)
4786 (goto-char end-of-match))))
20908596
CD
4787 (nreverse ee)))
4788
4789(defun org-agenda-get-sexps ()
4790 "Return the sexp information for agenda display."
4791 (require 'diary-lib)
3ab2c837 4792 (let* ((props (list 'mouse-face 'highlight
20908596
CD
4793 'help-echo
4794 (format "mouse-2 or RET jump to org file %s"
4795 (abbreviate-file-name buffer-file-name))))
4796 (regexp "^&?%%(")
c8d0cf5c
CD
4797 marker category ee txt tags entry result beg b sexp sexp-entry
4798 todo-state)
20908596
CD
4799 (goto-char (point-min))
4800 (while (re-search-forward regexp nil t)
4801 (catch :skip
4802 (org-agenda-skip)
4803 (setq beg (match-beginning 0))
4804 (goto-char (1- (match-end 0)))
4805 (setq b (point))
4806 (forward-sexp 1)
4807 (setq sexp (buffer-substring b (point)))
4808 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4809 (org-trim (match-string 1))
4810 ""))
4811 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4812 (when result
4813 (setq marker (org-agenda-new-marker beg)
c8d0cf5c
CD
4814 category (org-get-category beg)
4815 todo-state (org-get-todo-state))
20908596 4816
afe98dfa
CD
4817 (dolist (r (if (stringp result)
4818 (list result)
4819 result)) ;; we expect a list here
4820 (if (string-match "\\S-" r)
4821 (setq txt r)
4822 (setq txt "SEXP entry returned empty string"))
4823
4824 (setq txt (org-format-agenda-item
4825 "" txt category tags 'time))
4826 (org-add-props txt props 'org-marker marker)
4827 (org-add-props txt nil
4828 'org-category category 'date date 'todo-state todo-state
4829 'type "sexp")
4830 (push txt ee)))))
20908596
CD
4831 (nreverse ee)))
4832
3ab2c837
BG
4833;; Calendar sanity: define some functions that are independent of
4834;; `calendar-date-style'.
4835;; Normally I would like to use ISO format when calling the diary functions,
4836;; but to make sure we still have Emacs 22 compatibility we bind
4837;; also `european-calendar-style' and use european format
4838(defun org-anniversary (year month day &optional mark)
4839 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4840 (org-no-warnings
4841 (let ((calendar-date-style 'european) (european-calendar-style t))
4842 (diary-anniversary day month year mark))))
4843(defun org-cyclic (N year month day &optional mark)
4844 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4845 (org-no-warnings
4846 (let ((calendar-date-style 'european) (european-calendar-style t))
4847 (diary-cyclic N day month year mark))))
4848(defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4849 "Like `diary-block', but with fixed (ISO) order of arguments."
4850 (org-no-warnings
4851 (let ((calendar-date-style 'european) (european-calendar-style t))
4852 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4853(defun org-date (year month day &optional mark)
4854 "Like `diary-date', but with fixed (ISO) order of arguments."
4855 (org-no-warnings
4856 (let ((calendar-date-style 'european) (european-calendar-style t))
4857 (diary-date day month year mark))))
4858(defalias 'org-float 'diary-float)
4859
4860;; Define the` org-class' function
4861(defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
ed21c5c8 4862 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
ed21c5c8
CD
4863DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4864is any number of ISO weeks in the block period for which the item should
4865be skipped."
3ab2c837
BG
4866 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4867 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
ed21c5c8
CD
4868 (d (calendar-absolute-from-gregorian date)))
4869 (and
4870 (<= date1 d)
4871 (<= d date2)
4872 (= (calendar-day-of-week date) dayname)
4873 (or (not skip-weeks)
4874 (progn
4875 (require 'cal-iso)
4876 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4877 entry)))
4878
3ab2c837
BG
4879(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4880 "Like `org-class', but honor `calendar-date-style'.
4881The order of the first 2 times 3 arguments depends on the variable
4882`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4883So for American calendars, give this as MONTH DAY YEAR, for European as
4884DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4885DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4886is any number of ISO weeks in the block period for which the item should
4887be skipped.
4888
4889This function is here only for backward compatibility and it is deprecated,
4890please use `org-class' instead."
4891 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4892 (date2 (org-order-calendar-date-args m2 d2 y2)))
4893 (org-class
4894 (nth 2 date1) (car date1) (nth 1 date1)
4895 (nth 2 date2) (car date2) (nth 1 date2)
4896 dayname skip-weeks)))
4897
d60b1ba1 4898(defalias 'org-get-closed 'org-agenda-get-progress)
93b62de8 4899(defun org-agenda-get-progress ()
20908596
CD
4900 "Return the logged TODO entries for agenda display."
4901 (let* ((props (list 'mouse-face 'highlight
4902 'org-not-done-regexp org-not-done-regexp
4903 'org-todo-regexp org-todo-regexp
b349f79f 4904 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
4905 'help-echo
4906 (format "mouse-2 or RET jump to org file %s"
4907 (abbreviate-file-name buffer-file-name))))
93b62de8
CD
4908 (items (if (consp org-agenda-show-log)
4909 org-agenda-show-log
3ab2c837
BG
4910 (if (eq org-agenda-show-log 'clockcheck)
4911 '(clock)
4912 org-agenda-log-mode-items)))
ff4be292 4913 (parts
93b62de8
CD
4914 (delq nil
4915 (list
4916 (if (memq 'closed items) (concat "\\<" org-closed-string))
4917 (if (memq 'clock items) (concat "\\<" org-clock-string))
c8d0cf5c 4918 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
93b62de8
CD
4919 (parts-re (if parts (mapconcat 'identity parts "\\|")
4920 (error "`org-agenda-log-mode-items' is empty")))
20908596 4921 (regexp (concat
93b62de8
CD
4922 "\\(" parts-re "\\)"
4923 " *\\["
20908596
CD
4924 (regexp-quote
4925 (substring
4926 (format-time-string
4927 (car org-time-stamp-formats)
4928 (apply 'encode-time ; DATE bound by calendar
4929 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4930 1 11))))
c8d0cf5c
CD
4931 (org-agenda-search-headline-for-time nil)
4932 marker hdmarker priority category tags closedp statep clockp state
4933 ee txt extra timestr rest clocked)
20908596
CD
4934 (goto-char (point-min))
4935 (while (re-search-forward regexp nil t)
4936 (catch :skip
4937 (org-agenda-skip)
4938 (setq marker (org-agenda-new-marker (match-beginning 0))
4939 closedp (equal (match-string 1) org-closed-string)
93b62de8 4940 statep (equal (string-to-char (match-string 1)) ?-)
c8d0cf5c 4941 clockp (not (or closedp statep))
93b62de8 4942 state (and statep (match-string 2))
20908596
CD
4943 category (org-get-category (match-beginning 0))
4944 timestr (buffer-substring (match-beginning 0) (point-at-eol))
20908596 4945 )
b349f79f
CD
4946 (when (string-match "\\]" timestr)
4947 ;; substring should only run to end of time stamp
4948 (setq rest (substring timestr (match-end 0))
4949 timestr (substring timestr 0 (match-end 0)))
93b62de8 4950 (if (and (not closedp) (not statep)
c8d0cf5c 4951 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
621f83e4
CD
4952 (progn (setq timestr (concat (substring timestr 0 -1)
4953 "-" (match-string 1 rest) "]"))
4954 (setq clocked (match-string 2 rest)))
4955 (setq clocked "-")))
20908596 4956 (save-excursion
3ab2c837
BG
4957 (setq extra
4958 (cond
4959 ((not org-agenda-log-mode-add-notes) nil)
4960 (statep
4961 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4962 (match-string 1)))
4963 (clockp
4964 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4965 (match-string 1)))))
4966 (if (not (re-search-backward org-outline-regexp-bol nil t))
c8d0cf5c
CD
4967 (setq txt org-agenda-no-heading-message)
4968 (goto-char (match-beginning 0))
4969 (setq hdmarker (org-agenda-new-marker)
4970 tags (org-get-tags-at))
4971 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4972 (setq txt (match-string 1))
4973 (when extra
4974 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4975 (setq txt (concat (substring txt 0 (match-beginning 1))
4976 " - " extra " " (match-string 2 txt)))
4977 (setq txt (concat txt " - " extra))))
4978 (setq txt (org-format-agenda-item
4979 (cond
4980 (closedp "Closed: ")
93b62de8
CD
4981 (statep (concat "State: (" state ")"))
4982 (t (concat "Clocked: (" clocked ")")))
c8d0cf5c 4983 txt category tags timestr)))
20908596
CD
4984 (setq priority 100000)
4985 (org-add-props txt props
c8d0cf5c 4986 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
20908596
CD
4987 'priority priority 'org-category category
4988 'type "closed" 'date date
c8d0cf5c 4989 'undone-face 'org-warning 'done-face 'org-agenda-done)
20908596
CD
4990 (push txt ee))
4991 (goto-char (point-at-eol))))
4992 (nreverse ee)))
4993
3ab2c837
BG
4994(defun org-agenda-show-clocking-issues ()
4995 "Add overlays, showing issues with clocking.
4996See also the user option `org-agenda-clock-consistency-checks'."
4997 (interactive)
4998 (let* ((pl org-agenda-clock-consistency-checks)
4999 (re (concat "^[ \t]*"
5000 org-clock-string
5001 "[ \t]+"
5002 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5003 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5004 (tlstart 0.)
5005 (tlend 0.)
fe3c5669 5006 (maxtime (org-hh:mm-string-to-minutes
3ab2c837 5007 (or (plist-get pl :max-duration) "24:00")))
fe3c5669 5008 (mintime (org-hh:mm-string-to-minutes
3ab2c837
BG
5009 (or (plist-get pl :min-duration) 0)))
5010 (maxgap (org-hh:mm-string-to-minutes
5011 ;; default 30:00 means never complain
5012 (or (plist-get pl :max-gap) "30:00")))
5013 (gapok (mapcar 'org-hh:mm-string-to-minutes
5014 (plist-get pl :gap-ok-around)))
5015 (def-face (or (plist-get pl :default-face)
5016 '((:background "DarkRed") (:foreground "white"))))
5017 issue face m te ts dt ov)
5018 (goto-char (point-min))
5019 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5020 (setq issue nil face def-face)
5021 (catch 'next
5022 (setq m (org-get-at-bol 'org-marker)
5023 te nil ts nil)
5024 (unless (and m (markerp m))
5025 (setq issue "No valid clock line") (throw 'next t))
5026 (org-with-point-at m
5027 (save-excursion
5028 (goto-char (point-at-bol))
5029 (unless (looking-at re)
5030 (error "No valid Clock line")
5031 (throw 'next t))
5032 (unless (match-end 3)
5033 (setq issue "No end time"
5034 face (or (plist-get pl :no-end-time-face) face))
5035 (throw 'next t))
5036 (setq ts (match-string 1)
5037 te (match-string 3)
5038 ts (org-float-time
5039 (apply 'encode-time (org-parse-time-string ts)))
5040 te (org-float-time
5041 (apply 'encode-time (org-parse-time-string te)))
5042 dt (- te ts))))
5043 (cond
5044 ((> dt (* 60 maxtime))
5045 ;; a very long clocking chunk
5046 (setq issue (format "Clocking interval is very long: %s"
5047 (org-minutes-to-hh:mm-string
5048 (floor (/ (float dt) 60.))))
5049 face (or (plist-get pl :long-face) face)))
5050 ((< dt (* 60 mintime))
5051 ;; a very short clocking chunk
5052 (setq issue (format "Clocking interval is very short: %s"
5053 (org-minutes-to-hh:mm-string
5054 (floor (/ (float dt) 60.))))
5055 face (or (plist-get pl :short-face) face)))
5056 ((and (> tlend 0) (< ts tlend))
5057 ;; Two clock entries are overlapping
5058 (setq issue (format "Clocking overlap: %d minutes"
5059 (/ (- tlend ts) 60))
5060 face (or (plist-get pl :overlap-face) face)))
5061 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
c7015153 5062 ;; There is a gap, let's see if we need to report it
3ab2c837
BG
5063 (unless (org-agenda-check-clock-gap tlend ts gapok)
5064 (setq issue (format "Clocking gap: %d minutes"
5065 (/ (- ts tlend) 60))
5066 face (or (plist-get pl :gap-face) face))))
5067 (t nil)))
5068 (setq tlend (or te tlend) tlstart (or ts tlstart))
5069 (when issue
5070 ;; OK, there was some issue, add an overlay to show the issue
5071 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5072 (overlay-put ov 'before-string
5073 (concat
5074 (org-add-props
5075 (format "%-43s" (concat " " issue))
5076 nil
5077 'face face)
5078 "\n"))
5079 (overlay-put ov 'evaporate t)))))
5080
5081(defun org-agenda-check-clock-gap (t1 t2 ok-list)
5082 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5083 (catch 'exit
5084 (unless ok-list
5085 ;; there are no OK times for gaps...
5086 (throw 'exit nil))
5087 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5088 ;; This is more than 24 hours, so it is OK.
5089 ;; because we have at least one OK time, that must be in the
5090 ;; 24 hour interval.
5091 (throw 'exit t))
5092 ;; We have a shorter gap.
5093 ;; Now we have to get the minute of the day when these times are
5094 (let* ((t1dec (decode-time (seconds-to-time t1)))
5095 (t2dec (decode-time (seconds-to-time t2)))
5096 ;; compute the minute on the day
5097 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5098 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5099 (when (< min2 min1)
5100 ;; if min2 is smaller than min1, this means it is on the next day.
5101 ;; Wrap it to after midnight.
5102 (setq min2 (+ min2 1440)))
5103 ;; Now check if any of the OK times is in the gap
5104 (mapc (lambda (x)
5105 ;; Wrap the time to after midnight if necessary
5106 (if (< x min1) (setq x (+ x 1440)))
5107 ;; Check if in interval
5108 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5109 ok-list)
5110 ;; Nope, this gap is not OK
5111 nil)))
5112
20908596
CD
5113(defun org-agenda-get-deadlines ()
5114 "Return the deadline information for agenda display."
5115 (let* ((props (list 'mouse-face 'highlight
5116 'org-not-done-regexp org-not-done-regexp
5117 'org-todo-regexp org-todo-regexp
b349f79f 5118 'org-complex-heading-regexp org-complex-heading-regexp
20908596
CD
5119 'help-echo
5120 (format "mouse-2 or RET jump to org file %s"
5121 (abbreviate-file-name buffer-file-name))))
5122 (regexp org-deadline-time-regexp)
621f83e4 5123 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596
CD
5124 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5125 d2 diff dfrac wdays pos pos1 category tags
ed21c5c8 5126 suppress-prewarning
3ab2c837 5127 ee txt head face s todo-state show-all upcomingp donep timestr)
20908596
CD
5128 (goto-char (point-min))
5129 (while (re-search-forward regexp nil t)
ed21c5c8 5130 (setq suppress-prewarning nil)
20908596
CD
5131 (catch :skip
5132 (org-agenda-skip)
ed21c5c8
CD
5133 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5134 (save-match-data
5135 (string-match org-scheduled-time-regexp
5136 (buffer-substring (point-at-bol)
5137 (point-at-eol)))))
5138 (setq suppress-prewarning
5139 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5140 org-agenda-skip-deadline-prewarning-if-scheduled
5141 0)))
20908596 5142 (setq s (match-string 1)
c8d0cf5c 5143 txt nil
20908596 5144 pos (1- (match-beginning 1))
3ab2c837
BG
5145 todo-state (save-match-data (org-get-todo-state))
5146 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5147 (member todo-state
5148 org-agenda-repeating-timestamp-show-all))
20908596 5149 d2 (org-time-string-to-absolute
3ab2c837 5150 (match-string 1) d1 'past show-all)
20908596 5151 diff (- d2 d1)
ed21c5c8
CD
5152 wdays (if suppress-prewarning
5153 (let ((org-deadline-warning-days suppress-prewarning))
5154 (org-get-wdays s))
5155 (org-get-wdays s))
20908596
CD
5156 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5157 upcomingp (and todayp (> diff 0)))
5158 ;; When to show a deadline in the calendar:
5159 ;; If the expiration is within wdays warning time.
5160 ;; Past-due deadlines are only shown on the current date
8bfe682a
CD
5161 (if (and (or (and (<= diff wdays)
5162 (and todayp (not org-agenda-only-exact-dates)))
5163 (= diff 0)))
20908596 5164 (save-excursion
3ab2c837 5165 ;; (setq todo-state (org-get-todo-state))
c8d0cf5c
CD
5166 (setq donep (member todo-state org-done-keywords))
5167 (if (and donep
5168 (or org-agenda-skip-deadline-if-done
5169 (not (= diff 0))))
5170 (setq txt nil)
5171 (setq category (org-get-category))
5172 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5173 (setq txt org-agenda-no-heading-message)
5174 (goto-char (match-end 0))
5175 (setq pos1 (match-beginning 0))
5176 (setq tags (org-get-tags-at pos1))
5177 (setq head (buffer-substring-no-properties
5178 (point)
5179 (progn (skip-chars-forward "^\r\n")
5180 (point))))
5181 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5182 (setq timestr
5183 (concat (substring s (match-beginning 1)) " "))
5184 (setq timestr 'time))
5185 (setq txt (org-format-agenda-item
5186 (if (= diff 0)
5187 (car org-agenda-deadline-leaders)
5188 (if (functionp
5189 (nth 1 org-agenda-deadline-leaders))
5190 (funcall
5191 (nth 1 org-agenda-deadline-leaders)
5192 diff date)
5193 (format (nth 1 org-agenda-deadline-leaders)
5194 diff)))
5195 head category tags
5196 (if (not (= diff 0)) nil timestr)))))
20908596
CD
5197 (when txt
5198 (setq face (org-agenda-deadline-face dfrac wdays))
5199 (org-add-props txt props
5200 'org-marker (org-agenda-new-marker pos)
5201 'org-hd-marker (org-agenda-new-marker pos1)
5202 'priority (+ (- diff)
5203 (org-get-priority txt))
5204 'org-category category
621f83e4 5205 'todo-state todo-state
20908596
CD
5206 'type (if upcomingp "upcoming-deadline" "deadline")
5207 'date (if upcomingp date d2)
c8d0cf5c
CD
5208 'face (if donep 'org-agenda-done face)
5209 'undone-face face 'done-face 'org-agenda-done)
20908596
CD
5210 (push txt ee))))))
5211 (nreverse ee)))
5212
5213(defun org-agenda-deadline-face (fraction &optional wdays)
5214 "Return the face to displaying a deadline item.
5215FRACTION is what fraction of the head-warning time has passed."
5216 (if (equal wdays 0) (setq fraction 1.))
5217 (let ((faces org-agenda-deadline-faces) f)
5218 (catch 'exit
5219 (while (setq f (pop faces))
5220 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5221
54a0dee5 5222(defun org-agenda-get-scheduled (&optional deadline-results)
20908596
CD
5223 "Return the scheduled information for agenda display."
5224 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5225 'org-todo-regexp org-todo-regexp
b349f79f 5226 'org-complex-heading-regexp org-complex-heading-regexp
c8d0cf5c 5227 'done-face 'org-agenda-done
20908596 5228 'mouse-face 'highlight
20908596
CD
5229 'help-echo
5230 (format "mouse-2 or RET jump to org file %s"
5231 (abbreviate-file-name buffer-file-name))))
5232 (regexp org-scheduled-time-regexp)
621f83e4 5233 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
20908596 5234 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
54a0dee5
CD
5235 mm
5236 (deadline-position-alist
5237 (mapcar (lambda (a) (and (setq mm (get-text-property
8bfe682a
CD
5238 0 'org-hd-marker a))
5239 (cons (marker-position mm) a)))
54a0dee5 5240 deadline-results))
621f83e4 5241 d2 diff pos pos1 category tags donep
3ab2c837 5242 ee txt head pastschedp todo-state face timestr s habitp show-all)
20908596
CD
5243 (goto-char (point-min))
5244 (while (re-search-forward regexp nil t)
5245 (catch :skip
5246 (org-agenda-skip)
5247 (setq s (match-string 1)
c8d0cf5c 5248 txt nil
20908596 5249 pos (1- (match-beginning 1))
3ab2c837
BG
5250 todo-state (save-match-data (org-get-todo-state))
5251 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5252 (member todo-state
5253 org-agenda-repeating-timestamp-show-all))
20908596 5254 d2 (org-time-string-to-absolute
3ab2c837 5255 (match-string 1) d1 'past show-all)
20908596
CD
5256 diff (- d2 d1))
5257 (setq pastschedp (and todayp (< diff 0)))
5258 ;; When to show a scheduled item in the calendar:
5259 ;; If it is on or past the date.
8bfe682a
CD
5260 (when (or (and (< diff 0)
5261 (< (abs diff) org-scheduled-past-days)
5262 (and todayp (not org-agenda-only-exact-dates)))
5263 (= diff 0))
5264 (save-excursion
8bfe682a 5265 (setq donep (member todo-state org-done-keywords))
8bfe682a 5266 (if (and donep
3ab2c837
BG
5267 (or org-agenda-skip-scheduled-if-done
5268 (not (= diff 0))
5269 (and (functionp 'org-is-habit-p)
5270 (org-is-habit-p))))
8bfe682a 5271 (setq txt nil)
3ab2c837
BG
5272 (setq habitp (and (functionp 'org-is-habit-p)
5273 (org-is-habit-p)))
8bfe682a
CD
5274 (setq category (org-get-category))
5275 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5276 (setq txt org-agenda-no-heading-message)
5277 (goto-char (match-end 0))
5278 (setq pos1 (match-beginning 0))
5279 (if habitp
5280 (if (or (not org-habit-show-habits)
5281 (and (not todayp)
5282 org-habit-show-habits-only-for-today))
5283 (throw :skip nil))
54a0dee5
CD
5284 (if (and
5285 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5286 (and org-agenda-skip-scheduled-if-deadline-is-shown
5287 pastschedp))
5288 (setq mm (assoc pos1 deadline-position-alist)))
8bfe682a
CD
5289 (throw :skip nil)))
5290 (setq tags (org-get-tags-at))
5291 (setq head (buffer-substring-no-properties
5292 (point)
5293 (progn (skip-chars-forward "^\r\n") (point))))
5294 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5295 (setq timestr
5296 (concat (substring s (match-beginning 1)) " "))
5297 (setq timestr 'time))
5298 (setq txt (org-format-agenda-item
5299 (if (= diff 0)
5300 (car org-agenda-scheduled-leaders)
5301 (format (nth 1 org-agenda-scheduled-leaders)
5302 (- 1 diff)))
5303 head category tags
5304 (if (not (= diff 0)) nil timestr)
3ab2c837 5305 nil habitp))))
8bfe682a
CD
5306 (when txt
5307 (setq face
5308 (cond
5309 ((and (not habitp) pastschedp)
5310 'org-scheduled-previously)
5311 (todayp 'org-scheduled-today)
5312 (t 'org-scheduled))
5313 habitp (and habitp (org-habit-parse-todo)))
5314 (org-add-props txt props
5315 'undone-face face
5316 'face (if donep 'org-agenda-done face)
5317 'org-marker (org-agenda-new-marker pos)
5318 'org-hd-marker (org-agenda-new-marker pos1)
5319 'type (if pastschedp "past-scheduled" "scheduled")
5320 'date (if pastschedp d2 date)
5321 'priority (if habitp
5322 (org-habit-get-priority habitp)
5323 (+ 94 (- 5 diff) (org-get-priority txt)))
5324 'org-category category
5325 'org-habit-p habitp
5326 'todo-state todo-state)
5327 (push txt ee))))))
20908596
CD
5328 (nreverse ee)))
5329
5330(defun org-agenda-get-blocks ()
5331 "Return the date-range information for agenda display."
5332 (let* ((props (list 'face nil
5333 'org-not-done-regexp org-not-done-regexp
5334 'org-todo-regexp org-todo-regexp
b349f79f 5335 'org-complex-heading-regexp org-complex-heading-regexp
20908596 5336 'mouse-face 'highlight
20908596
CD
5337 'help-echo
5338 (format "mouse-2 or RET jump to org file %s"
5339 (abbreviate-file-name buffer-file-name))))
5340 (regexp org-tr-regexp)
5341 (d0 (calendar-absolute-from-gregorian date))
3ab2c837 5342 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
c8d0cf5c 5343 head donep)
20908596
CD
5344 (goto-char (point-min))
5345 (while (re-search-forward regexp nil t)
5346 (catch :skip
5347 (org-agenda-skip)
5348 (setq pos (point))
3ab2c837
BG
5349 (let ((start-time (match-string 1))
5350 (end-time (match-string 2)))
5351 (setq s1 (match-string 1)
5352 s2 (match-string 2)
5353 d1 (time-to-days (org-time-string-to-time s1))
5354 d2 (time-to-days (org-time-string-to-time s2)))
5355 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5356 ;; Only allow days between the limits, because the normal
5357 ;; date stamps will catch the limits.
5358 (save-excursion
5359 (setq todo-state (org-get-todo-state))
5360 (setq donep (member todo-state org-done-keywords))
5361 (if (and donep org-agenda-skip-timestamp-if-done)
5362 (throw :skip t))
5363 (setq marker (org-agenda-new-marker (point)))
5364 (setq category (org-get-category))
5365 (if (not (re-search-backward org-outline-regexp-bol nil t))
5366 (setq txt org-agenda-no-heading-message)
5367 (goto-char (match-beginning 0))
5368 (setq hdmarker (org-agenda-new-marker (point)))
5369 (setq tags (org-get-tags-at))
5370 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5371 (setq head (match-string 1))
5372 (let ((remove-re
5373 (if org-agenda-remove-timeranges-from-blocks
5374 (concat
5375 "<" (regexp-quote s1) ".*?>"
5376 "--"
5377 "<" (regexp-quote s2) ".*?>")
5378 nil)))
5379 (setq txt (org-format-agenda-item
5380 (format
5381 (nth (if (= d1 d2) 0 1)
5382 org-agenda-timerange-leaders)
5383 (1+ (- d0 d1)) (1+ (- d2 d1)))
5384 head category tags
5385 (cond ((= d1 d0)
5386 (concat "<" start-time ">"))
5387 ((= d2 d0)
5388 (concat "<" end-time ">"))
5389 (t nil))
5390 remove-re))))
5391 (org-add-props txt props
5392 'org-marker marker 'org-hd-marker hdmarker
5393 'type "block" 'date date
5394 'todo-state todo-state
5395 'priority (org-get-priority txt) 'org-category category)
5396 (push txt ee))))
20908596
CD
5397 (goto-char pos)))
5398 ;; Sort the entries by expiration date.
5399 (nreverse ee)))
5400
5401;;; Agenda presentation and sorting
5402
5403(defvar org-prefix-has-time nil
5404 "A flag, set by `org-compile-prefix-format'.
5405The flag is set if the currently compiled format contains a `%t'.")
5406(defvar org-prefix-has-tag 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-effort nil
5410 "A flag, set by `org-compile-prefix-format'.
5411The flag is set if the currently compiled format contains a `%e'.")
8d642074 5412(defvar org-prefix-category-length nil
86fbb8ca 5413 "Used by `org-compile-prefix-format' to remember the category field width.")
8bfe682a 5414(defvar org-prefix-category-max-length nil
86fbb8ca 5415 "Used by `org-compile-prefix-format' to remember the category field width.")
20908596 5416
acedf35c
CD
5417(defun org-agenda-get-category-icon (category)
5418 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5419 (dolist (entry org-agenda-category-icon-alist)
5420 (when (org-string-match-p (car entry) category)
5421 (if (listp (cadr entry))
5422 (return (cadr entry))
5423 (return (apply 'create-image (cdr entry)))))))
5424
20908596 5425(defun org-format-agenda-item (extra txt &optional category tags dotime
3ab2c837 5426 remove-re habitp)
20908596
CD
5427 "Format TXT to be inserted into the agenda buffer.
5428In particular, it adds the prefix and corresponding text properties. EXTRA
5429must be a string and replaces the `%s' specifier in the prefix format.
5430CATEGORY (string, symbol or nil) may be used to overrule the default
5431category taken from local variable or file name. It will replace the `%c'
5432specifier in the format. DOTIME, when non-nil, indicates that a
5433time-of-day should be extracted from TXT for sorting of this entry, and for
5434the `%t' specifier in the format. When DOTIME is a string, this string is
3ab2c837 5435searched for a time before TXT is. TAGS can be the tags of the headline.
20908596
CD
5436Any match of REMOVE-RE will be removed from TXT."
5437 (save-match-data
5438 ;; Diary entries sometimes have extra whitespace at the beginning
5439 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5dec9555
CD
5440
5441 ;; Fix the tags part in txt
5442 (setq txt (org-agenda-fix-displayed-tags
5443 txt tags
5444 org-agenda-show-inherited-tags
5445 org-agenda-hide-tags-regexp))
20908596 5446 (let* ((category (or category
acedf35c
CD
5447 (if (stringp org-category)
5448 org-category
5449 (and org-category (symbol-name org-category)))
20908596
CD
5450 (if buffer-file-name
5451 (file-name-sans-extension
5452 (file-name-nondirectory buffer-file-name))
5453 "")))
acedf35c
CD
5454 (category-icon (org-agenda-get-category-icon category))
5455 (category-icon (if category-icon
5456 (propertize " " 'display category-icon)
5457 ""))
20908596
CD
5458 ;; time, tag, effort are needed for the eval of the prefix format
5459 (tag (if tags (nth (1- (length tags)) tags) ""))
5460 time effort neffort
c8d0cf5c
CD
5461 (ts (if dotime (concat
5462 (if (stringp dotime) dotime "")
5463 (and org-agenda-search-headline-for-time txt))))
20908596 5464 (time-of-day (and dotime (org-get-time-of-day ts)))
3ab2c837 5465 stamp plain s0 s1 s2 rtn srp l
8bfe682a 5466 duration thecategory)
20908596
CD
5467 (and (org-mode-p) buffer-file-name
5468 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5469 (when (and dotime time-of-day)
5470 ;; Extract starting and ending time and move them to prefix
5471 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5472 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5473 (setq s0 (match-string 0 ts)
5474 srp (and stamp (match-end 3))
5475 s1 (match-string (if plain 1 2) ts)
5476 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5477
5478 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5479 ;; them, we might want to remove them there to avoid duplication.
5480 ;; The user can turn this off with a variable.
5481 (if (and org-prefix-has-time
5482 org-agenda-remove-times-when-in-prefix (or stamp plain)
5483 (string-match (concat (regexp-quote s0) " *") txt)
5484 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5485 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5486 (= (match-beginning 0) 0)
5487 t))
5488 (setq txt (replace-match "" nil nil txt))))
5489 ;; Normalize the time(s) to 24 hour
5490 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5491 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
3ab2c837
BG
5492
5493 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5494 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5495 (setq s2
5496 (org-minutes-to-hh:mm-string
5497 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5498
20908596 5499 ;; Compute the duration
3ab2c837
BG
5500 (when s2
5501 (setq duration (- (org-hh:mm-string-to-minutes s2)
5502 (org-hh:mm-string-to-minutes s1)))))
20908596 5503
afe98dfa 5504 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
5505 txt)
5506 ;; Tags are in the string
5507 (if (or (eq org-agenda-remove-tags t)
5508 (and org-agenda-remove-tags
5509 org-prefix-has-tag))
5510 (setq txt (replace-match "" t t txt))
5511 (setq txt (replace-match
5512 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5513 (match-string 2 txt))
5514 t t txt))))
5515 (when (org-mode-p)
5516 (setq effort
5517 (condition-case nil
5518 (org-get-effort
5519 (or (get-text-property 0 'org-hd-marker txt)
5520 (get-text-property 0 'org-marker txt)))
5521 (error nil)))
5522 (when effort
3ab2c837
BG
5523 (setq neffort (org-duration-string-to-minutes effort)
5524 effort (setq effort (concat "[" effort "]")))))
5525 ;; prevent erroring out with %e format when there is no effort
5526 (or effort (setq effort ""))
20908596
CD
5527
5528 (when remove-re
5529 (while (string-match remove-re txt)
5530 (setq txt (replace-match "" t t txt))))
5531
3ab2c837
BG
5532 ;; Set org-heading property on `txt' to mark the start of the
5533 ;; heading.
5534 (add-text-properties 0 (length txt) '(org-heading t) txt)
5535
5536 ;; Prepare the variables needed in the eval of the compiled format
5537 (setq time (cond (s2 (concat
5538 (org-agenda-time-of-day-to-ampm-maybe s1)
5539 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5540 (if org-agenda-timegrid-use-ampm " ")))
5541 (s1 (concat
5542 (org-agenda-time-of-day-to-ampm-maybe s1)
5543 (if org-agenda-timegrid-use-ampm
5544 "........ "
5545 "......")))
5546 (t ""))
5547 extra (or (and (not habitp) extra) "")
5548 category (if (symbolp category) (symbol-name category) category)
5549 thecategory (copy-sequence category))
5550 (if (string-match org-bracket-link-regexp category)
5551 (progn
5552 (setq l (if (match-end 3)
5553 (- (match-end 3) (match-beginning 3))
5554 (- (match-end 1) (match-beginning 1))))
5555 (when (< l (or org-prefix-category-length 0))
5556 (setq category (copy-sequence category))
5557 (org-add-props category nil
5558 'extra-space (make-string
5559 (- org-prefix-category-length l 1) ?\ ))))
5560 (if (and org-prefix-category-max-length
5561 (>= (length category) org-prefix-category-max-length))
5562 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5563 ;; Evaluate the compiled format
5564 (setq rtn (concat (eval org-prefix-format-compiled) txt))
20908596
CD
5565
5566 ;; And finally add the text properties
c8d0cf5c 5567 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
20908596 5568 (org-add-props rtn nil
8bfe682a 5569 'org-category (if thecategory (downcase thecategory) category)
ff4be292 5570 'tags (mapcar 'org-downcase-keep-props tags)
20908596
CD
5571 'org-highest-priority org-highest-priority
5572 'org-lowest-priority org-lowest-priority
20908596
CD
5573 'time-of-day time-of-day
5574 'duration duration
5575 'effort effort
5576 'effort-minutes neffort
5577 'txt txt
5578 'time time
5579 'extra extra
3ab2c837 5580 'format org-prefix-format-compiled
20908596
CD
5581 'dotime dotime))))
5582
5dec9555
CD
5583(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5584 "Remove tags string from TXT, and add a modified list of tags.
5585The modified list may contain inherited tags, and tags matched by
5586`org-agenda-hide-tags-regexp' will be removed."
5587 (when (or add-inherited hide-re)
afe98dfa 5588 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5dec9555 5589 (setq txt (substring txt 0 (match-beginning 0))))
ed21c5c8
CD
5590 (setq tags
5591 (delq nil
5592 (mapcar (lambda (tg)
5593 (if (or (and hide-re (string-match hide-re tg))
5594 (and (not add-inherited)
5595 (get-text-property 0 'inherited tg)))
5596 nil
5597 tg))
5598 tags)))
5dec9555 5599 (when tags
5dec9555
CD
5600 (let ((have-i (get-text-property 0 'inherited (car tags)))
5601 i)
5602 (setq txt (concat txt " :"
5603 (mapconcat
5604 (lambda (x)
5605 (setq i (get-text-property 0 'inherited x))
5606 (if (and have-i (not i))
5607 (progn
5608 (setq have-i nil)
5609 (concat ":" x))
5610 x))
5611 tags ":")
5612 (if have-i "::" ":"))))))
5613 txt)
ff4be292
CD
5614
5615(defun org-downcase-keep-props (s)
5616 (let ((props (text-properties-at 0 s)))
5617 (setq s (downcase s))
5618 (add-text-properties 0 (length s) props s)
5619 s))
5620
20908596
CD
5621(defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5622(defvar org-agenda-sorting-strategy-selected nil)
5623
5624(defun org-agenda-add-time-grid-maybe (list ndays todayp)
5625 (catch 'exit
5626 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5627 ((and todayp (member 'today (car org-agenda-time-grid))))
5628 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5629 ((member 'weekly (car org-agenda-time-grid)))
5630 (t (throw 'exit list)))
5631 (let* ((have (delq nil (mapcar
5632 (lambda (x) (get-text-property 1 'time-of-day x))
5633 list)))
5634 (string (nth 1 org-agenda-time-grid))
5635 (gridtimes (nth 2 org-agenda-time-grid))
5636 (req (car org-agenda-time-grid))
5637 (remove (member 'remove-match req))
5638 new time)
5639 (if (and (member 'require-timed req) (not have))
5640 ;; don't show empty grid
5641 (throw 'exit list))
5642 (while (setq time (pop gridtimes))
5643 (unless (and remove (member time have))
afe98dfa 5644 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
20908596
CD
5645 (push (org-format-agenda-item
5646 nil string "" nil
5647 (concat (substring time 0 -2) ":" (substring time -2)))
5648 new)
5649 (put-text-property
afe98dfa 5650 2 (length (car new)) 'face 'org-time-grid (car new))))
3ab2c837
BG
5651 (when (and todayp org-agenda-show-current-time-in-grid)
5652 (push (org-format-agenda-item
5653 nil
5654 org-agenda-current-time-string
5655 "" nil
5656 (format-time-string "%H:%M "))
5657 new)
5658 (put-text-property
5659 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5660
20908596
CD
5661 (if (member 'time-up org-agenda-sorting-strategy-selected)
5662 (append new list)
5663 (append list new)))))
5664
5665(defun org-compile-prefix-format (key)
5666 "Compile the prefix format into a Lisp form that can be evaluated.
5667The resulting form is returned and stored in the variable
5668`org-prefix-format-compiled'."
5669 (setq org-prefix-has-time nil org-prefix-has-tag nil
8d642074 5670 org-prefix-category-length nil org-prefix-has-effort nil)
20908596
CD
5671 (let ((s (cond
5672 ((stringp org-agenda-prefix-format)
5673 org-agenda-prefix-format)
5674 ((assq key org-agenda-prefix-format)
5675 (cdr (assq key org-agenda-prefix-format)))
5676 (t " %-12:c%?-12t% s")))
5677 (start 0)
5678 varform vars var e c f opt)
3ab2c837 5679 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
20908596 5680 s start)
3ab2c837
BG
5681 (setq var (or (cdr (assoc (match-string 4 s)
5682 '(("c" . category) ("t" . time) ("s" . extra)
5683 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5684 'eval)
20908596
CD
5685 c (or (match-string 3 s) "")
5686 opt (match-beginning 1)
5687 start (1+ (match-beginning 0)))
5688 (if (equal var 'time) (setq org-prefix-has-time t))
5689 (if (equal var 'tag) (setq org-prefix-has-tag t))
5690 (if (equal var 'effort) (setq org-prefix-has-effort t))
5691 (setq f (concat "%" (match-string 2 s) "s"))
8bfe682a
CD
5692 (when (equal var 'category)
5693 (setq org-prefix-category-length
5694 (floor (abs (string-to-number (match-string 2 s)))))
5695 (setq org-prefix-category-max-length
5696 (let ((x (match-string 2 s)))
5697 (save-match-data
5698 (if (string-match "\\.[0-9]+" x)
5699 (string-to-number (substring (match-string 0 x) 1)))))))
3ab2c837
BG
5700 (if (eq var 'eval)
5701 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5702 (if opt
5703 (setq varform
5704 `(if (equal "" ,var)
5705 ""
5706 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5707 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
20908596
CD
5708 (setq s (replace-match "%s" t nil s))
5709 (push varform vars))
5710 (setq vars (nreverse vars))
5711 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5712
5713(defun org-set-sorting-strategy (key)
5714 (if (symbolp (car org-agenda-sorting-strategy))
5715 ;; the old format
5716 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5717 (setq org-agenda-sorting-strategy-selected
5718 (or (cdr (assq key org-agenda-sorting-strategy))
5719 (cdr (assq 'agenda org-agenda-sorting-strategy))
5720 '(time-up category-keep priority-down)))))
5721
5722(defun org-get-time-of-day (s &optional string mod24)
5723 "Check string S for a time of day.
5724If found, return it as a military time number between 0 and 2400.
5725If not found, return nil.
5726The optional STRING argument forces conversion into a 5 character wide string
5727HH:MM."
5728 (save-match-data
5729 (when
5730 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5731 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5732 (let* ((h (string-to-number (match-string 1 s)))
5733 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5734 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5735 (am-p (equal ampm "am"))
5736 (h1 (cond ((not ampm) h)
5737 ((= h 12) (if am-p 0 12))
5738 (t (+ h (if am-p 0 12)))))
5739 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5740 (mod h1 24) h1))
5741 (t0 (+ (* 100 h2) m))
5742 (t1 (concat (if (>= h1 24) "+" " ")
ed21c5c8
CD
5743 (if (and org-agenda-time-leading-zero
5744 (< t0 1000)) "0" "")
20908596
CD
5745 (if (< t0 100) "0" "")
5746 (if (< t0 10) "0" "")
5747 (int-to-string t0))))
5748 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5749
afe98dfa
CD
5750(defvar org-agenda-before-sorting-filter-function nil
5751 "Function to be applied to agenda items prior to sorting.
5752Prior to sorting also means just before they are inserted into the agenda.
5753
5754To aid sorting, you may revisit the original entries and add more text
5755properties which will later be used by the sorting functions.
5756
5757The function should take a string argument, an agenda line.
5758It has access to the text properties in that line, which contain among
5759other things, the property `org-hd-marker' that points to the entry
5760where the line comes from. Note that not all lines going into the agenda
5761have this property, only most.
5762
5763The function should return the modified string. It is probably best
5764to ONLY change text properties.
5765
5766You can also use this function as a filter, by returning nil for lines
5767you don't want to have in the agenda at all. For this application, you
5768could bind the variable in the options section of a custom command.")
5769
20908596
CD
5770(defun org-finalize-agenda-entries (list &optional nosort)
5771 "Sort and concatenate the agenda items."
5772 (setq list (mapcar 'org-agenda-highlight-todo list))
5773 (if nosort
5774 list
afe98dfa
CD
5775 (when org-agenda-before-sorting-filter-function
5776 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
20908596
CD
5777 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5778
5779(defun org-agenda-highlight-todo (x)
621f83e4 5780 (let ((org-done-keywords org-done-keywords-for-agenda)
ed21c5c8 5781 (case-fold-search nil)
3ab2c837 5782 re)
20908596
CD
5783 (if (eq x 'line)
5784 (save-excursion
5785 (beginning-of-line 1)
8d642074 5786 (setq re (org-get-at-bol 'org-todo-regexp))
3ab2c837 5787 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
621f83e4 5788 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
c8d0cf5c 5789 (add-text-properties (match-beginning 0) (match-end 1)
621f83e4 5790 (list 'face (org-get-todo-face 1)))
20908596
CD
5791 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5792 (delete-region (match-beginning 1) (1- (match-end 0)))
5793 (goto-char (match-beginning 1))
5794 (insert (format org-agenda-todo-keyword-format s)))))
3ab2c837
BG
5795 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5796 (setq re (get-text-property 0 'org-todo-regexp x))
5797 (when (and re
5798 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5799 x (or pl 0)) pl))
5800 (add-text-properties
5801 (or (match-end 1) (match-end 0)) (match-end 0)
5802 (list 'face (org-get-todo-face (match-string 2 x)))
20908596 5803 x)
3ab2c837
BG
5804 (when (match-end 1)
5805 (setq x (concat (substring x 0 (match-end 1))
5806 (format org-agenda-todo-keyword-format
5807 (match-string 2 x))
86fbb8ca 5808 (org-add-props " " (text-properties-at 0 x))
3ab2c837 5809 (substring x (match-end 3)))))))
20908596
CD
5810 x)))
5811
5812(defsubst org-cmp-priority (a b)
5813 "Compare the priorities of string A and B."
5814 (let ((pa (or (get-text-property 1 'priority a) 0))
5815 (pb (or (get-text-property 1 'priority b) 0)))
5816 (cond ((> pa pb) +1)
5817 ((< pa pb) -1)
5818 (t nil))))
5819
5820(defsubst org-cmp-effort (a b)
5821 "Compare the priorities of string A and B."
5822 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5823 (ea (or (get-text-property 1 'effort-minutes a) def))
5824 (eb (or (get-text-property 1 'effort-minutes b) def)))
5825 (cond ((> ea eb) +1)
5826 ((< ea eb) -1)
5827 (t nil))))
5828
5829(defsubst org-cmp-category (a b)
5830 "Compare the string values of categories of strings A and B."
5831 (let ((ca (or (get-text-property 1 'org-category a) ""))
5832 (cb (or (get-text-property 1 'org-category b) "")))
5833 (cond ((string-lessp ca cb) -1)
5834 ((string-lessp cb ca) +1)
5835 (t nil))))
5836
621f83e4
CD
5837(defsubst org-cmp-todo-state (a b)
5838 "Compare the todo states of strings A and B."
c8d0cf5c
CD
5839 (let* ((ma (or (get-text-property 1 'org-marker a)
5840 (get-text-property 1 'org-hd-marker a)))
5841 (mb (or (get-text-property 1 'org-marker b)
5842 (get-text-property 1 'org-hd-marker b)))
5843 (fa (and ma (marker-buffer ma)))
5844 (fb (and mb (marker-buffer mb)))
5845 (todo-kwds
5846 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5847 (and fb (with-current-buffer fb org-todo-keywords-1))))
5848 (ta (or (get-text-property 1 'todo-state a) ""))
621f83e4 5849 (tb (or (get-text-property 1 'todo-state b) ""))
c8d0cf5c
CD
5850 (la (- (length (member ta todo-kwds))))
5851 (lb (- (length (member tb todo-kwds))))
ff4be292 5852 (donepa (member ta org-done-keywords-for-agenda))
621f83e4
CD
5853 (donepb (member tb org-done-keywords-for-agenda)))
5854 (cond ((and donepa (not donepb)) -1)
5855 ((and (not donepa) donepb) +1)
5856 ((< la lb) -1)
5857 ((< lb la) +1)
5858 (t nil))))
5859
86fbb8ca
CD
5860(defsubst org-cmp-alpha (a b)
5861 "Compare the headlines, alphabetically."
3ab2c837
BG
5862 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5863 (plb (text-property-any 0 (length b) 'org-heading t b))
86fbb8ca
CD
5864 (ta (and pla (substring a pla)))
5865 (tb (and plb (substring b plb))))
5866 (when pla
5867 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5868 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5869 (setq ta (substring ta (match-end 0))))
5870 (setq ta (downcase ta)))
5871 (when plb
5872 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5873 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5874 (setq tb (substring tb (match-end 0))))
5875 (setq tb (downcase tb)))
5876 (cond ((not ta) +1)
5877 ((not tb) -1)
5878 ((string-lessp ta tb) -1)
5879 ((string-lessp tb ta) +1)
5880 (t nil))))
5881
20908596 5882(defsubst org-cmp-tag (a b)
71d35b24 5883 "Compare the string values of the first tags of A and B."
20908596
CD
5884 (let ((ta (car (last (get-text-property 1 'tags a))))
5885 (tb (car (last (get-text-property 1 'tags b)))))
5886 (cond ((not ta) +1)
5887 ((not tb) -1)
5888 ((string-lessp ta tb) -1)
5889 ((string-lessp tb ta) +1)
5890 (t nil))))
5891
5892(defsubst org-cmp-time (a b)
5893 "Compare the time-of-day values of strings A and B."
5894 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5895 (ta (or (get-text-property 1 'time-of-day a) def))
5896 (tb (or (get-text-property 1 'time-of-day b) def)))
5897 (cond ((< ta tb) -1)
5898 ((< tb ta) +1)
5899 (t nil))))
5900
8bfe682a
CD
5901(defsubst org-cmp-habit-p (a b)
5902 "Compare the todo states of strings A and B."
5903 (let ((ha (get-text-property 1 'org-habit-p a))
5904 (hb (get-text-property 1 'org-habit-p b)))
5905 (cond ((and ha (not hb)) -1)
5906 ((and (not ha) hb) +1)
5907 (t nil))))
5908
86fbb8ca
CD
5909(defsubst org-em (x y list) (or (memq x list) (memq y list)))
5910
20908596
CD
5911(defun org-entries-lessp (a b)
5912 "Predicate for sorting agenda entries."
5913 ;; The following variables will be used when the form is evaluated.
5914 ;; So even though the compiler complains, keep them.
86fbb8ca
CD
5915 (let* ((ss org-agenda-sorting-strategy-selected)
5916 (time-up (and (org-em 'time-up 'time-down ss)
5917 (org-cmp-time a b)))
5918 (time-down (if time-up (- time-up) nil))
5919 (priority-up (and (org-em 'priority-up 'priority-down ss)
5920 (org-cmp-priority a b)))
5921 (priority-down (if priority-up (- priority-up) nil))
5922 (effort-up (and (org-em 'effort-up 'effort-down ss)
5923 (org-cmp-effort a b)))
5924 (effort-down (if effort-up (- effort-up) nil))
5925 (category-up (and (or (org-em 'category-up 'category-down ss)
5926 (memq 'category-keep ss))
5927 (org-cmp-category a b)))
5928 (category-down (if category-up (- category-up) nil))
5929 (category-keep (if category-up +1 nil))
5930 (tag-up (and (org-em 'tag-up 'tag-down ss)
5931 (org-cmp-tag a b)))
5932 (tag-down (if tag-up (- tag-up) nil))
5933 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5934 (org-cmp-todo-state a b)))
c8d0cf5c 5935 (todo-state-down (if todo-state-up (- todo-state-up) nil))
86fbb8ca
CD
5936 (habit-up (and (org-em 'habit-up 'habit-down ss)
5937 (org-cmp-habit-p a b)))
5938 (habit-down (if habit-up (- habit-up) nil))
5939 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5940 (org-cmp-alpha a b)))
5941 (alpha-down (if alpha-up (- alpha-up) nil))
afe98dfa 5942 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
c8d0cf5c 5943 user-defined-up user-defined-down)
afe98dfa 5944 (if (and need-user-cmp org-agenda-cmp-user-defined
c8d0cf5c
CD
5945 (functionp org-agenda-cmp-user-defined))
5946 (setq user-defined-up
5947 (funcall org-agenda-cmp-user-defined a b)
5948 user-defined-down (if user-defined-up (- user-defined-up) nil)))
20908596
CD
5949 (cdr (assoc
5950 (eval (cons 'or org-agenda-sorting-strategy-selected))
5951 '((-1 . t) (1 . nil) (nil . nil))))))
5952
5953;;; Agenda restriction lock
5954
86fbb8ca 5955(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
8bfe682a 5956 "Overlay to mark the headline to which agenda commands are restricted.")
86fbb8ca
CD
5957(overlay-put org-agenda-restriction-lock-overlay
5958 'face 'org-agenda-restriction-lock)
5959(overlay-put org-agenda-restriction-lock-overlay
5960 'help-echo "Agendas are currently limited to this subtree.")
20908596
CD
5961(org-detach-overlay org-agenda-restriction-lock-overlay)
5962
5963(defun org-agenda-set-restriction-lock (&optional type)
5964 "Set restriction lock for agenda, to current subtree or file.
5965Restriction will be the file if TYPE is `file', or if type is the
5966universal prefix '(4), or if the cursor is before the first headline
5967in the file. Otherwise, restriction will be to the current subtree."
5968 (interactive "P")
5969 (and (equal type '(4)) (setq type 'file))
5970 (setq type (cond
5971 (type type)
5972 ((org-at-heading-p) 'subtree)
5973 ((condition-case nil (org-back-to-heading t) (error nil))
5974 'subtree)
5975 (t 'file)))
5976 (if (eq type 'subtree)
5977 (progn
5978 (setq org-agenda-restrict t)
5979 (setq org-agenda-overriding-restriction 'subtree)
5980 (put 'org-agenda-files 'org-restrict
5981 (list (buffer-file-name (buffer-base-buffer))))
5982 (org-back-to-heading t)
86fbb8ca 5983 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
20908596
CD
5984 (move-marker org-agenda-restrict-begin (point))
5985 (move-marker org-agenda-restrict-end
5986 (save-excursion (org-end-of-subtree t)))
5987 (message "Locking agenda restriction to subtree"))
5988 (put 'org-agenda-files 'org-restrict
5989 (list (buffer-file-name (buffer-base-buffer))))
5990 (setq org-agenda-restrict nil)
5991 (setq org-agenda-overriding-restriction 'file)
5992 (move-marker org-agenda-restrict-begin nil)
5993 (move-marker org-agenda-restrict-end nil)
5994 (message "Locking agenda restriction to file"))
5995 (setq current-prefix-arg nil)
5996 (org-agenda-maybe-redo))
5997
5998(defun org-agenda-remove-restriction-lock (&optional noupdate)
5999 "Remove the agenda restriction lock."
6000 (interactive "P")
6001 (org-detach-overlay org-agenda-restriction-lock-overlay)
6002 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6003 (setq org-agenda-overriding-restriction nil)
6004 (setq org-agenda-restrict nil)
6005 (put 'org-agenda-files 'org-restrict nil)
6006 (move-marker org-agenda-restrict-begin nil)
6007 (move-marker org-agenda-restrict-end nil)
6008 (setq current-prefix-arg nil)
6009 (message "Agenda restriction lock removed")
6010 (or noupdate (org-agenda-maybe-redo)))
6011
6012(defun org-agenda-maybe-redo ()
6013 "If there is any window showing the agenda view, update it."
6014 (let ((w (get-buffer-window org-agenda-buffer-name t))
6015 (w0 (selected-window)))
6016 (when w
6017 (select-window w)
6018 (org-agenda-redo)
6019 (select-window w0)
6020 (if org-agenda-overriding-restriction
6021 (message "Agenda view shifted to new %s restriction"
6022 org-agenda-overriding-restriction)
6023 (message "Agenda restriction lock removed")))))
6024
6025;;; Agenda commands
6026
6027(defun org-agenda-check-type (error &rest types)
6028 "Check if agenda buffer is of allowed type.
6029If ERROR is non-nil, throw an error, otherwise just return nil."
6030 (if (memq org-agenda-type types)
6031 t
6032 (if error
6033 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6034 nil)))
6035
6036(defun org-agenda-quit ()
6037 "Exit agenda by removing the window or the buffer."
6038 (interactive)
6039 (if org-agenda-columns-active
6040 (org-columns-quit)
6041 (let ((buf (current-buffer)))
8d642074
CD
6042 (if (eq org-agenda-window-setup 'other-frame)
6043 (progn
6044 (kill-buffer buf)
6045 (org-agenda-reset-markers)
6046 (org-columns-remove-overlays)
6047 (setq org-agenda-archives-mode nil)
6048 (delete-frame))
6049 (and (not (eq org-agenda-window-setup 'current-window))
6050 (not (one-window-p))
6051 (delete-window))
6052 (kill-buffer buf)
6053 (org-agenda-reset-markers)
6054 (org-columns-remove-overlays)
6055 (setq org-agenda-archives-mode nil)))
20908596
CD
6056 ;; Maybe restore the pre-agenda window configuration.
6057 (and org-agenda-restore-windows-after-quit
6058 (not (eq org-agenda-window-setup 'other-frame))
6059 org-pre-agenda-window-conf
6060 (set-window-configuration org-pre-agenda-window-conf))))
6061
6062(defun org-agenda-exit ()
6063 "Exit agenda by removing the window or the buffer.
6064Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6065Org-mode buffers visited directly by the user will not be touched."
6066 (interactive)
6067 (org-release-buffers org-agenda-new-buffers)
6068 (setq org-agenda-new-buffers nil)
6069 (org-agenda-quit))
6070
6071(defun org-agenda-execute (arg)
86fbb8ca
CD
6072 "Execute another agenda command, keeping same window.
6073So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6074in the agenda."
20908596
CD
6075 (interactive "P")
6076 (let ((org-agenda-window-setup 'current-window))
6077 (org-agenda arg)))
6078
20908596
CD
6079(defun org-agenda-redo ()
6080 "Rebuild Agenda.
6081When this is the global TODO list, a prefix argument will be interpreted."
6082 (interactive)
6083 (let* ((org-agenda-keep-modes t)
71d35b24 6084 (filter org-agenda-filter)
c8d0cf5c 6085 (preset (get 'org-agenda-filter :preset-filter))
acedf35c 6086 (org-agenda-filter-while-redo (or filter preset))
20908596
CD
6087 (cols org-agenda-columns-active)
6088 (line (org-current-line))
6089 (window-line (- line (org-current-line (window-start))))
6090 (lprops (get 'org-agenda-redo-command 'org-lprops)))
c8d0cf5c 6091 (put 'org-agenda-filter :preset-filter nil)
20908596
CD
6092 (and cols (org-columns-quit))
6093 (message "Rebuilding agenda buffer...")
6094 (org-let lprops '(eval org-agenda-redo-command))
6095 (setq org-agenda-undo-list nil
6096 org-agenda-pending-undo-list nil)
6097 (message "Rebuilding agenda buffer...done")
c8d0cf5c
CD
6098 (put 'org-agenda-filter :preset-filter preset)
6099 (and (or filter preset) (org-agenda-filter-apply filter))
3ab2c837 6100 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
54a0dee5 6101 (org-goto-line line)
20908596
CD
6102 (recenter window-line)))
6103
71d35b24 6104
621f83e4 6105(defvar org-global-tags-completion-table nil)
71d35b24
CD
6106(defvar org-agenda-filter-form nil)
6107(defun org-agenda-filter-by-tag (strip &optional char narrow)
621f83e4
CD
6108 "Keep only those lines in the agenda buffer that have a specific tag.
6109The tag is selected with its fast selection letter, as configured.
71d35b24
CD
6110With prefix argument STRIP, remove all lines that do have the tag.
6111A lisp caller can specify CHAR. NARROW means that the new tag should be
6112used to narrow the search - the interactive user can also press `-' or `+'
6113to switch to narrowing."
621f83e4 6114 (interactive "P")
71d35b24 6115 (let* ((alist org-tag-alist-for-agenda)
8bfe682a
CD
6116 (tag-chars (mapconcat
6117 (lambda (x) (if (and (not (symbolp (car x)))
6118 (cdr x))
6119 (char-to-string (cdr x))
6120 ""))
6121 alist ""))
6122 (efforts (org-split-string
6123 (or (cdr (assoc (concat org-effort-property "_ALL")
6124 org-global-properties))
3ab2c837
BG
6125 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6126 "")))
8bfe682a
CD
6127 (effort-op org-agenda-filter-effort-default-operator)
6128 (effort-prompt "")
6129 (inhibit-read-only t)
6130 (current org-agenda-filter)
3ab2c837 6131 maybe-refresh a n tag)
71d35b24 6132 (unless char
ff4be292 6133 (message
8bfe682a
CD
6134 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6135 (if narrow "Narrow" "Filter") tag-chars
6136 (if org-agenda-auto-exclude-function "[RET], " ""))
71d35b24
CD
6137 (setq char (read-char)))
6138 (when (member char '(?+ ?-))
6139 ;; Narrowing down
6140 (cond ((equal char ?-) (setq strip t narrow t))
6141 ((equal char ?+) (setq strip nil narrow t)))
ff4be292 6142 (message
71d35b24
CD
6143 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6144 (setq char (read-char)))
c8d0cf5c 6145 (when (member char '(?< ?> ?= ??))
71d35b24
CD
6146 ;; An effort operator
6147 (setq effort-op (char-to-string char))
71d35b24 6148 (setq alist nil) ; to make sure it will be interpreted as effort.
c8d0cf5c
CD
6149 (unless (equal char ??)
6150 (loop for i from 0 to 9 do
6151 (setq effort-prompt
6152 (concat
6153 effort-prompt " ["
6154 (if (= i 9) "0" (int-to-string (1+ i)))
6155 "]" (nth i efforts))))
6156 (message "Effort%s: %s " effort-op effort-prompt)
6157 (setq char (read-char))
6158 (when (or (< char ?0) (> char ?9))
6159 (error "Need 1-9,0 to select effort" ))))
71d35b24
CD
6160 (when (equal char ?\t)
6161 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6162 (org-set-local 'org-global-tags-completion-table
6163 (org-global-tags-completion-table)))
6164 (let ((completion-ignore-case t))
54a0dee5 6165 (setq tag (org-icompleting-read
71d35b24
CD
6166 "Tag: " org-global-tags-completion-table))))
6167 (cond
8bfe682a
CD
6168 ((equal char ?\r)
6169 (org-agenda-filter-by-tag-show-all)
6170 (when org-agenda-auto-exclude-function
6171 (setq org-agenda-filter '())
ed21c5c8
CD
6172 (dolist (tag (org-agenda-get-represented-tags))
6173 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
8bfe682a
CD
6174 (if modifier
6175 (push modifier org-agenda-filter))))
6176 (if (not (null org-agenda-filter))
3ab2c837
BG
6177 (org-agenda-filter-apply org-agenda-filter)))
6178 (setq maybe-refresh t))
c8d0cf5c
CD
6179 ((equal char ?/)
6180 (org-agenda-filter-by-tag-show-all)
6181 (when (get 'org-agenda-filter :preset-filter)
3ab2c837
BG
6182 (org-agenda-filter-apply org-agenda-filter))
6183 (setq maybe-refresh t))
71d35b24
CD
6184 ((or (equal char ?\ )
6185 (setq a (rassoc char alist))
6186 (and (>= char ?0) (<= char ?9)
6187 (setq n (if (= char ?0) 9 (- char ?0 1))
6188 tag (concat effort-op (nth n efforts))
6189 a (cons tag nil)))
c8d0cf5c
CD
6190 (and (= char ??)
6191 (setq tag "?eff")
6192 a (cons tag nil))
71d35b24
CD
6193 (and tag (setq a (cons tag nil))))
6194 (org-agenda-filter-by-tag-show-all)
6195 (setq tag (car a))
6196 (setq org-agenda-filter
6197 (cons (concat (if strip "-" "+") tag)
6198 (if narrow current nil)))
3ab2c837
BG
6199 (org-agenda-filter-apply org-agenda-filter)
6200 (setq maybe-refresh t))
6201 (t (error "Invalid tag selection character %c" char)))
58e9b49a
BG
6202 (when (and maybe-refresh
6203 (eq org-agenda-clockreport-mode 'with-filter))
3ab2c837 6204 (org-agenda-redo))))
71d35b24 6205
ed21c5c8
CD
6206(defun org-agenda-get-represented-tags ()
6207 "Get a list of all tags currently represented in the agenda."
6208 (let (p tags)
6209 (save-excursion
6210 (goto-char (point-min))
6211 (while (setq p (next-single-property-change (point) 'tags))
6212 (goto-char p)
6213 (mapc (lambda (x) (add-to-list 'tags x))
6214 (get-text-property (point) 'tags))))
6215 tags))
6216
71d35b24
CD
6217(defun org-agenda-filter-by-tag-refine (strip &optional char)
6218 "Refine the current filter. See `org-agenda-filter-by-tag."
6219 (interactive "P")
6220 (org-agenda-filter-by-tag strip char 'refine))
6221
6222(defun org-agenda-filter-make-matcher ()
6223 "Create the form that tests a line for the agenda filter."
6224 (let (f f1)
c8d0cf5c
CD
6225 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6226 org-agenda-filter))
71d35b24 6227 (if (member x '("-" "+"))
8bfe682a 6228 (setq f1 (if (equal x "-") 'tags '(not tags)))
c8d0cf5c 6229 (if (string-match "[<=>?]" x)
71d35b24
CD
6230 (setq f1 (org-agenda-filter-effort-form x))
6231 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6232 (if (equal (string-to-char x) ?-)
6233 (setq f1 (list 'not f1))))
6234 (push f1 f))
6235 (cons 'and (nreverse f))))
6236
6237(defun org-agenda-filter-effort-form (e)
6238 "Return the form to compare the effort of the current line with what E says.
86fbb8ca 6239E looks like \"+<2:25\"."
71d35b24
CD
6240 (let (op)
6241 (setq e (substring e 1))
6242 (setq op (string-to-char e) e (substring e 1))
c8d0cf5c
CD
6243 (setq op (cond ((equal op ?<) '<=)
6244 ((equal op ?>) '>=)
6245 ((equal op ??) op)
6246 (t '=)))
71d35b24 6247 (list 'org-agenda-compare-effort (list 'quote op)
3ab2c837 6248 (org-duration-string-to-minutes e))))
71d35b24
CD
6249
6250(defun org-agenda-compare-effort (op value)
6251 "Compare the effort of the current line with VALUE, using OP.
6252If the line does not have an effort defined, return nil."
8d642074 6253 (let ((eff (org-get-at-bol 'effort-minutes)))
c8d0cf5c
CD
6254 (if (equal op ??)
6255 (not eff)
6256 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6257 value))))
71d35b24
CD
6258
6259(defun org-agenda-filter-apply (filter)
6260 "Set FILTER as the new agenda filter and apply it."
6261 (let (tags)
6262 (setq org-agenda-filter filter
6263 org-agenda-filter-form (org-agenda-filter-make-matcher))
6264 (org-agenda-set-mode-name)
6265 (save-excursion
6266 (goto-char (point-min))
6267 (while (not (eobp))
8d642074 6268 (if (org-get-at-bol 'org-marker)
71d35b24 6269 (progn
8d642074 6270 (setq tags (org-get-at-bol 'tags)) ; used in eval
71d35b24
CD
6271 (if (not (eval org-agenda-filter-form))
6272 (org-agenda-filter-by-tag-hide-line))
6273 (beginning-of-line 2))
afe98dfa
CD
6274 (beginning-of-line 2))))
6275 (if (get-char-property (point) 'invisible)
6276 (org-agenda-previous-line))))
621f83e4 6277
621f83e4
CD
6278(defun org-agenda-filter-by-tag-hide-line ()
6279 (let (ov)
86fbb8ca 6280 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
621f83e4 6281 (point-at-eol)))
86fbb8ca
CD
6282 (overlay-put ov 'invisible t)
6283 (overlay-put ov 'type 'tags-filter)
621f83e4
CD
6284 (push ov org-agenda-filter-overlays)))
6285
71d35b24
CD
6286(defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6287 (setq pos (or pos (point)))
6288 (save-excursion
86fbb8ca
CD
6289 (dolist (ov (overlays-at pos))
6290 (when (and (overlay-get ov 'invisible)
6291 (eq (overlay-get ov 'type) 'tags-filter))
71d35b24 6292 (goto-char pos)
86fbb8ca
CD
6293 (if (< (overlay-start ov) (point-at-eol))
6294 (move-overlay ov (point-at-eol)
6295 (overlay-end ov)))))))
71d35b24 6296
621f83e4 6297(defun org-agenda-filter-by-tag-show-all ()
86fbb8ca 6298 (mapc 'delete-overlay org-agenda-filter-overlays)
71d35b24
CD
6299 (setq org-agenda-filter-overlays nil)
6300 (setq org-agenda-filter nil)
6301 (setq org-agenda-filter-form nil)
6302 (org-agenda-set-mode-name))
621f83e4 6303
20908596
CD
6304(defun org-agenda-manipulate-query-add ()
6305 "Manipulate the query by adding a search term with positive selection.
ed21c5c8 6306Positive selection means the term must be matched for selection of an entry."
20908596
CD
6307 (interactive)
6308 (org-agenda-manipulate-query ?\[))
6309(defun org-agenda-manipulate-query-subtract ()
6310 "Manipulate the query by adding a search term with negative selection.
ed21c5c8 6311Negative selection means term must not be matched for selection of an entry."
20908596
CD
6312 (interactive)
6313 (org-agenda-manipulate-query ?\]))
6314(defun org-agenda-manipulate-query-add-re ()
6315 "Manipulate the query by adding a search regexp with positive selection.
ed21c5c8 6316Positive selection means the regexp must match for selection of an entry."
20908596
CD
6317 (interactive)
6318 (org-agenda-manipulate-query ?\{))
6319(defun org-agenda-manipulate-query-subtract-re ()
6320 "Manipulate the query by adding a search regexp with negative selection.
ed21c5c8 6321Negative selection means regexp must not match for selection of an entry."
20908596
CD
6322 (interactive)
6323 (org-agenda-manipulate-query ?\}))
6324(defun org-agenda-manipulate-query (char)
6325 (cond
6326 ((memq org-agenda-type '(timeline agenda))
54a0dee5
CD
6327 (let ((org-agenda-include-inactive-timestamps t))
6328 (org-agenda-redo))
6329 (message "Display now includes inactive timestamps as well"))
20908596
CD
6330 ((eq org-agenda-type 'search)
6331 (org-add-to-string
6332 'org-agenda-query-string
ed21c5c8
CD
6333 (if org-agenda-last-search-view-search-was-boolean
6334 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6335 (?\{ . " +{}") (?\} . " -{}"))))
6336 " "))
20908596
CD
6337 (setq org-agenda-redo-command
6338 (list 'org-search-view
6339 org-todo-only
6340 org-agenda-query-string
6341 (+ (length org-agenda-query-string)
6342 (if (member char '(?\{ ?\})) 0 1))))
6343 (set-register org-agenda-query-register org-agenda-query-string)
6344 (org-agenda-redo))
6345 (t (error "Cannot manipulate query for %s-type agenda buffers"
6346 org-agenda-type))))
6347
6348(defun org-add-to-string (var string)
6349 (set var (concat (symbol-value var) string)))
6350
6351(defun org-agenda-goto-date (date)
6352 "Jump to DATE in agenda."
afe98dfa
CD
6353 (interactive (list (let ((org-read-date-prefer-future
6354 (eval org-agenda-jump-prefer-future)))
6355 (org-read-date))))
20908596
CD
6356 (org-agenda-list nil date))
6357
6358(defun org-agenda-goto-today ()
6359 "Go to today."
6360 (interactive)
6361 (org-agenda-check-type t 'timeline 'agenda)
6362 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6363 (cond
6364 (tdpos (goto-char tdpos))
6365 ((eq org-agenda-type 'agenda)
acedf35c 6366 (let* ((sd (org-agenda-compute-starting-span
3ab2c837 6367 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
20908596 6368 (org-agenda-overriding-arguments org-agenda-last-arguments))
acedf35c 6369 (setf (nth 1 org-agenda-overriding-arguments) sd)
20908596
CD
6370 (org-agenda-redo)
6371 (org-agenda-find-same-or-today-or-agenda)))
6372 (t (error "Cannot find today")))))
6373
6374(defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6375 (goto-char
6376 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6377 (text-property-any (point-min) (point-max) 'org-today t)
6378 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6379 (point-min))))
6380
6381(defun org-agenda-later (arg)
6382 "Go forward in time by thee current span.
6383With prefix ARG, go forward that many times the current span."
6384 (interactive "p")
6385 (org-agenda-check-type t 'agenda)
acedf35c 6386 (let* ((span org-agenda-current-span)
20908596
CD
6387 (sd org-starting-day)
6388 (greg (calendar-gregorian-from-absolute sd))
8d642074 6389 (cnt (org-get-at-bol 'org-day-cnt))
acedf35c 6390 greg2)
20908596
CD
6391 (cond
6392 ((eq span 'day)
acedf35c 6393 (setq sd (+ arg sd)))
20908596 6394 ((eq span 'week)
acedf35c 6395 (setq sd (+ (* 7 arg) sd)))
20908596
CD
6396 ((eq span 'month)
6397 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6398 sd (calendar-absolute-from-gregorian greg2))
acedf35c 6399 (setcar greg2 (1+ (car greg2))))
20908596
CD
6400 ((eq span 'year)
6401 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6402 sd (calendar-absolute-from-gregorian greg2))
acedf35c
CD
6403 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6404 (t
6405 (setq sd (+ (* span arg) sd))))
20908596 6406 (let ((org-agenda-overriding-arguments
acedf35c 6407 (list (car org-agenda-last-arguments) sd span t)))
20908596
CD
6408 (org-agenda-redo)
6409 (org-agenda-find-same-or-today-or-agenda cnt))))
6410
6411(defun org-agenda-earlier (arg)
6412 "Go backward in time by the current span.
6413With prefix ARG, go backward that many times the current span."
6414 (interactive "p")
6415 (org-agenda-later (- arg)))
6416
c8d0cf5c
CD
6417(defun org-agenda-view-mode-dispatch ()
6418 "Call one of the view mode commands."
6419 (interactive)
3ab2c837
BG
6420 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6421 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6422 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6423 [E]ntryText")
c8d0cf5c
CD
6424 (let ((a (read-char-exclusive)))
6425 (case a
3ab2c837 6426 (?\ (call-interactively 'org-agenda-reset-view))
c8d0cf5c
CD
6427 (?d (call-interactively 'org-agenda-day-view))
6428 (?w (call-interactively 'org-agenda-week-view))
6429 (?m (call-interactively 'org-agenda-month-view))
6430 (?y (call-interactively 'org-agenda-year-view))
6431 (?l (call-interactively 'org-agenda-log-mode))
ed21c5c8 6432 (?L (org-agenda-log-mode '(4)))
3ab2c837 6433 (?c (org-agenda-log-mode 'clockcheck))
54a0dee5 6434 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
c8d0cf5c
CD
6435 (?a (call-interactively 'org-agenda-archives-mode))
6436 (?A (org-agenda-archives-mode 'files))
54a0dee5
CD
6437 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6438 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
c8d0cf5c
CD
6439 (?G (call-interactively 'org-agenda-toggle-time-grid))
6440 (?D (call-interactively 'org-agenda-toggle-diary))
ed21c5c8 6441 (?\! (call-interactively 'org-agenda-toggle-deadlines))
54a0dee5
CD
6442 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6443 (org-agenda-check-type t 'timeline 'agenda)
6444 (org-agenda-redo))
6445 (message "Display now includes inactive timestamps as well"))
c8d0cf5c
CD
6446 (?q (message "Abort"))
6447 (otherwise (error "Invalid key" )))))
6448
3ab2c837
BG
6449(defun org-agenda-reset-view ()
6450 "Switch to default view for agenda."
6451 (interactive)
6452 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
20908596
CD
6453(defun org-agenda-day-view (&optional day-of-year)
6454 "Switch to daily view for agenda.
6455With argument DAY-OF-YEAR, switch to that day of the year."
6456 (interactive "P")
20908596
CD
6457 (org-agenda-change-time-span 'day day-of-year))
6458(defun org-agenda-week-view (&optional iso-week)
6459 "Switch to daily view for agenda.
6460With argument ISO-WEEK, switch to the corresponding ISO week.
6461If ISO-WEEK has more then 2 digits, only the last two encode the
6462week. Any digits before this encode a year. So 200712 means
6463week 12 of year 2007. Years in the range 1938-2037 can also be
6464written as 2-digit years."
6465 (interactive "P")
20908596
CD
6466 (org-agenda-change-time-span 'week iso-week))
6467(defun org-agenda-month-view (&optional month)
b349f79f 6468 "Switch to monthly view for agenda.
20908596
CD
6469With argument MONTH, switch to that month."
6470 (interactive "P")
6471 (org-agenda-change-time-span 'month month))
6472(defun org-agenda-year-view (&optional year)
b349f79f 6473 "Switch to yearly view for agenda.
20908596
CD
6474With argument YEAR, switch to that year.
6475If MONTH has more then 2 digits, only the last two encode the
6476month. Any digits before this encode a year. So 200712 means
6477December year 2007. Years in the range 1938-2037 can also be
6478written as 2-digit years."
6479 (interactive "P")
6480 (when year
6481 (setq year (org-small-year-to-year year)))
6482 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6483 (org-agenda-change-time-span 'year year)
6484 (error "Abort")))
6485
6486(defun org-agenda-change-time-span (span &optional n)
6487 "Change the agenda view to SPAN.
6488SPAN may be `day', `week', `month', `year'."
6489 (org-agenda-check-type t 'agenda)
acedf35c 6490 (if (and (not n) (equal org-agenda-current-span span))
20908596 6491 (error "Viewing span is already \"%s\"" span))
8d642074 6492 (let* ((sd (or (org-get-at-bol 'day)
20908596 6493 org-starting-day))
acedf35c 6494 (sd (org-agenda-compute-starting-span sd span n))
20908596 6495 (org-agenda-overriding-arguments
3ab2c837
BG
6496 (or org-agenda-overriding-arguments
6497 (list (car org-agenda-last-arguments) sd span t))))
20908596
CD
6498 (org-agenda-redo)
6499 (org-agenda-find-same-or-today-or-agenda))
6500 (org-agenda-set-mode-name)
6501 (message "Switched to %s view" span))
6502
acedf35c
CD
6503(defun org-agenda-compute-starting-span (sd span &optional n)
6504 "Compute starting date for agenda.
20908596
CD
6505SPAN may be `day', `week', `month', `year'. The return value
6506is a cons cell with the starting date and the number of days,
6507so that the date SD will be in that range."
6508 (let* ((greg (calendar-gregorian-from-absolute sd))
6509 (dg (nth 1 greg))
6510 (mg (car greg))
acedf35c 6511 (yg (nth 2 greg)))
20908596
CD
6512 (cond
6513 ((eq span 'day)
6514 (when n
6515 (setq sd (+ (calendar-absolute-from-gregorian
6516 (list mg 1 yg))
acedf35c 6517 n -1))))
20908596
CD
6518 ((eq span 'week)
6519 (let* ((nt (calendar-day-of-week
6520 (calendar-gregorian-from-absolute sd)))
6521 (d (if org-agenda-start-on-weekday
6522 (- nt org-agenda-start-on-weekday)
acedf35c
CD
6523 0))
6524 y1)
20908596
CD
6525 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6526 (when n
6527 (require 'cal-iso)
20908596
CD
6528 (when (> n 99)
6529 (setq y1 (org-small-year-to-year (/ n 100))
6530 n (mod n 100)))
6531 (setq sd
6532 (calendar-absolute-from-iso
6533 (list n 1
acedf35c 6534 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
20908596 6535 ((eq span 'month)
acedf35c
CD
6536 (let (y1)
6537 (when (and n (> n 99))
6538 (setq y1 (org-small-year-to-year (/ n 100))
6539 n (mod n 100)))
6540 (setq sd (calendar-absolute-from-gregorian
6541 (list (or n mg) 1 (or y1 yg))))))
20908596
CD
6542 ((eq span 'year)
6543 (setq sd (calendar-absolute-from-gregorian
acedf35c
CD
6544 (list 1 1 (or n yg))))))
6545 sd))
20908596
CD
6546
6547(defun org-agenda-next-date-line (&optional arg)
6548 "Jump to the next line indicating a date in agenda buffer."
6549 (interactive "p")
6550 (org-agenda-check-type t 'agenda 'timeline)
6551 (beginning-of-line 1)
6552 ;; This does not work if user makes date format that starts with a blank
6553 (if (looking-at "^\\S-") (forward-char 1))
6554 (if (not (re-search-forward "^\\S-" nil t arg))
6555 (progn
6556 (backward-char 1)
6557 (error "No next date after this line in this buffer")))
6558 (goto-char (match-beginning 0)))
6559
6560(defun org-agenda-previous-date-line (&optional arg)
6561 "Jump to the previous line indicating a date in agenda buffer."
6562 (interactive "p")
6563 (org-agenda-check-type t 'agenda 'timeline)
6564 (beginning-of-line 1)
6565 (if (not (re-search-backward "^\\S-" nil t arg))
6566 (error "No previous date before this line in this buffer")))
6567
6568;; Initialize the highlight
86fbb8ca
CD
6569(defvar org-hl (make-overlay 1 1))
6570(overlay-put org-hl 'face 'highlight)
20908596
CD
6571
6572(defun org-highlight (begin end &optional buffer)
6573 "Highlight a region with overlay."
86fbb8ca 6574 (move-overlay org-hl begin end (or buffer (current-buffer))))
20908596
CD
6575
6576(defun org-unhighlight ()
6577 "Detach overlay INDEX."
86fbb8ca 6578 (org-detach-overlay org-hl))
20908596
CD
6579
6580;; FIXME this is currently not used.
6581(defun org-highlight-until-next-command (beg end &optional buffer)
6582 "Move the highlight overlay to BEG/END, remove it before the next command."
6583 (org-highlight beg end buffer)
6584 (add-hook 'pre-command-hook 'org-unhighlight-once))
6585(defun org-unhighlight-once ()
6586 "Remove the highlight from its position, and this function from the hook."
6587 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6588 (org-unhighlight))
6589
6590(defun org-agenda-follow-mode ()
6591 "Toggle follow mode in an agenda buffer."
6592 (interactive)
6593 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6594 (org-agenda-set-mode-name)
8bfe682a
CD
6595 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6596 (org-agenda-show))
20908596
CD
6597 (message "Follow mode is %s"
6598 (if org-agenda-follow-mode "on" "off")))
6599
54a0dee5
CD
6600(defun org-agenda-entry-text-mode (&optional arg)
6601 "Toggle entry text mode in an agenda buffer."
6602 (interactive "P")
365f8d85
SM
6603 (setq org-agenda-entry-text-mode (or (integerp arg)
6604 (not org-agenda-entry-text-mode)))
54a0dee5
CD
6605 (org-agenda-entry-text-hide)
6606 (and org-agenda-entry-text-mode
6607 (let ((org-agenda-entry-text-maxlines
6608 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6609 (org-agenda-entry-text-show)))
6610 (org-agenda-set-mode-name)
6611 (message "Entry text mode is %s. Maximum number of lines is %d"
6612 (if org-agenda-entry-text-mode "on" "off")
6613 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6614
acedf35c
CD
6615(defun org-agenda-clockreport-mode (&optional with-filter)
6616 "Toggle clocktable mode in an agenda buffer.
6617With prefix arg WITH-FILTER, make the clocktable respect the current
6618agenda filter."
6619 (interactive "P")
20908596 6620 (org-agenda-check-type t 'agenda)
acedf35c
CD
6621 (if with-filter
6622 (setq org-agenda-clockreport-mode 'with-filter)
6623 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
20908596
CD
6624 (org-agenda-set-mode-name)
6625 (org-agenda-redo)
6626 (message "Clocktable mode is %s"
6627 (if org-agenda-clockreport-mode "on" "off")))
6628
93b62de8
CD
6629(defun org-agenda-log-mode (&optional special)
6630 "Toggle log mode in an agenda buffer.
6631With argument SPECIAL, show all possible log items, not only the ones
6632configured in `org-agenda-log-mode-items'.
6633With a double `C-u' prefix arg, show *only* log items, nothing else."
6634 (interactive "P")
20908596 6635 (org-agenda-check-type t 'agenda 'timeline)
93b62de8 6636 (setq org-agenda-show-log
3ab2c837
BG
6637 (cond
6638 ((equal special '(16)) 'only)
6639 ((eq special 'clockcheck)
6640 (if (eq org-agenda-show-log 'clockcheck)
6641 nil 'clockcheck))
6642 (special '(closed clock state))
6643 (t (not org-agenda-show-log))))
20908596
CD
6644 (org-agenda-set-mode-name)
6645 (org-agenda-redo)
6646 (message "Log mode is %s"
6647 (if org-agenda-show-log "on" "off")))
6648
2c3ad40d 6649(defun org-agenda-archives-mode (&optional with-files)
c8d0cf5c
CD
6650 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6651When called with a prefix argument, include all archive files as well."
2c3ad40d
CD
6652 (interactive "P")
6653 (setq org-agenda-archives-mode
6654 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6655 (org-agenda-set-mode-name)
6656 (org-agenda-redo)
6657 (message
6658 "%s"
6659 (cond
6660 ((eq org-agenda-archives-mode nil)
6661 "No archives are included")
6662 ((eq org-agenda-archives-mode 'trees)
6663 (format "Trees with :%s: tag are included" org-archive-tag))
6664 ((eq org-agenda-archives-mode t)
6665 (format "Trees with :%s: tag and all active archive files are included"
6666 org-archive-tag)))))
6667
20908596
CD
6668(defun org-agenda-toggle-diary ()
6669 "Toggle diary inclusion in an agenda buffer."
6670 (interactive)
6671 (org-agenda-check-type t 'agenda)
6672 (setq org-agenda-include-diary (not org-agenda-include-diary))
6673 (org-agenda-redo)
6674 (org-agenda-set-mode-name)
6675 (message "Diary inclusion turned %s"
6676 (if org-agenda-include-diary "on" "off")))
6677
ed21c5c8 6678(defun org-agenda-toggle-deadlines ()
acedf35c 6679 "Toggle inclusion of entries with a deadline in an agenda buffer."
ed21c5c8
CD
6680 (interactive)
6681 (org-agenda-check-type t 'agenda)
6682 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6683 (org-agenda-redo)
6684 (org-agenda-set-mode-name)
6685 (message "Deadlines inclusion turned %s"
6686 (if org-agenda-include-deadlines "on" "off")))
6687
20908596
CD
6688(defun org-agenda-toggle-time-grid ()
6689 "Toggle time grid in an agenda buffer."
6690 (interactive)
6691 (org-agenda-check-type t 'agenda)
6692 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6693 (org-agenda-redo)
6694 (org-agenda-set-mode-name)
6695 (message "Time-grid turned %s"
6696 (if org-agenda-use-time-grid "on" "off")))
6697
6698(defun org-agenda-set-mode-name ()
6699 "Set the mode name to indicate all the small mode settings."
6700 (setq mode-name
acedf35c
CD
6701 (list "Org-Agenda"
6702 (if (get 'org-agenda-files 'org-restrict) " []" "")
6703 " "
6704 '(:eval (org-agenda-span-name org-agenda-current-span))
6705 (if org-agenda-follow-mode " Follow" "")
6706 (if org-agenda-entry-text-mode " ETxt" "")
6707 (if org-agenda-include-diary " Diary" "")
6708 (if org-agenda-include-deadlines " Ddl" "")
6709 (if org-agenda-use-time-grid " Grid" "")
6710 (if (and (boundp 'org-habit-show-habits)
6711 org-habit-show-habits) " Habit" "")
3ab2c837
BG
6712 (cond
6713 ((consp org-agenda-show-log) " LogAll")
6714 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6715 (org-agenda-show-log " Log")
6716 (t ""))
acedf35c
CD
6717 (if (or org-agenda-filter (get 'org-agenda-filter
6718 :preset-filter))
6719 (concat " {" (mapconcat
6720 'identity
6721 (append (get 'org-agenda-filter
6722 :preset-filter)
6723 org-agenda-filter) "") "}")
6724 "")
6725 (if org-agenda-archives-mode
6726 (if (eq org-agenda-archives-mode t)
6727 " Archives"
6728 (format " :%s:" org-archive-tag))
6729 "")
6730 (if org-agenda-clockreport-mode
6731 (if (eq org-agenda-clockreport-mode 'with-filter)
6732 " Clock{}" " Clock")
6733 "")))
20908596
CD
6734 (force-mode-line-update))
6735
6736(defun org-agenda-post-command-hook ()
b349f79f
CD
6737 (setq org-agenda-type
6738 (or (get-text-property (point) 'org-agenda-type)
6739 (get-text-property (max (point-min) (1- (point)))
8bfe682a
CD
6740 'org-agenda-type))))
6741
6742(defun org-agenda-next-line ()
86fbb8ca 6743 "Move cursor to the next line, and show if follow mode is active."
8bfe682a
CD
6744 (interactive)
6745 (call-interactively 'next-line)
1bcdebed
CD
6746 (org-agenda-do-context-action))
6747
8bfe682a
CD
6748(defun org-agenda-previous-line ()
6749 "Move cursor to the previous line, and show if follow-mode is active."
8bfe682a
CD
6750 (interactive)
6751 (call-interactively 'previous-line)
1bcdebed
CD
6752 (org-agenda-do-context-action))
6753
6754(defun org-agenda-do-context-action ()
86fbb8ca 6755 "Show outline path and, maybe, follow mode window."
1bcdebed
CD
6756 (let ((m (org-get-at-bol 'org-marker)))
6757 (if (and org-agenda-follow-mode m)
6758 (org-agenda-show))
6759 (if (and m org-agenda-show-outline-path)
5dec9555
CD
6760 (org-with-point-at m
6761 (org-display-outline-path t)))))
20908596
CD
6762
6763(defun org-agenda-show-priority ()
6764 "Show the priority of the current item.
6765This priority is composed of the main priority given with the [#A] cookies,
6766and by additional input from the age of a schedules or deadline entry."
6767 (interactive)
8d642074 6768 (let* ((pri (org-get-at-bol 'priority)))
20908596
CD
6769 (message "Priority is %d" (if pri pri -1000))))
6770
6771(defun org-agenda-show-tags ()
6772 "Show the tags applicable to the current item."
6773 (interactive)
8d642074 6774 (let* ((tags (org-get-at-bol 'tags)))
20908596
CD
6775 (if tags
6776 (message "Tags are :%s:"
6777 (org-no-properties (mapconcat 'identity tags ":")))
6778 (message "No tags associated with this line"))))
6779
6780(defun org-agenda-goto (&optional highlight)
6781 "Go to the Org-mode file which contains the item at point."
6782 (interactive)
8d642074 6783 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6784 (org-agenda-error)))
6785 (buffer (marker-buffer marker))
6786 (pos (marker-position marker)))
6787 (switch-to-buffer-other-window buffer)
6788 (widen)
86fbb8ca 6789 (push-mark)
20908596
CD
6790 (goto-char pos)
6791 (when (org-mode-p)
6792 (org-show-context 'agenda)
6793 (save-excursion
6794 (and (outline-next-heading)
3ab2c837
BG
6795 (org-flag-heading nil)))) ; show the next heading
6796 (when (outline-invisible-p)
6797 (show-entry)) ; display invisible text
20908596
CD
6798 (recenter (/ (window-height) 2))
6799 (run-hooks 'org-agenda-after-show-hook)
6800 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6801
6802(defvar org-agenda-after-show-hook nil
6803 "Normal hook run after an item has been shown from the agenda.
6804Point is in the buffer where the item originated.")
6805
6806(defun org-agenda-kill ()
6807 "Kill the entry or subtree belonging to the current agenda entry."
6808 (interactive)
6809 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6810 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6811 (org-agenda-error)))
6812 (buffer (marker-buffer marker))
6813 (pos (marker-position marker))
8d642074 6814 (type (org-get-at-bol 'type))
20908596
CD
6815 dbeg dend (n 0) conf)
6816 (org-with-remote-undo buffer
6817 (with-current-buffer buffer
6818 (save-excursion
6819 (goto-char pos)
6820 (if (and (org-mode-p) (not (member type '("sexp"))))
6821 (setq dbeg (progn (org-back-to-heading t) (point))
6822 dend (org-end-of-subtree t t))
6823 (setq dbeg (point-at-bol)
6824 dend (min (point-max) (1+ (point-at-eol)))))
6825 (goto-char dbeg)
6826 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6827 (setq conf (or (eq t org-agenda-confirm-kill)
6828 (and (numberp org-agenda-confirm-kill)
6829 (> n org-agenda-confirm-kill))))
6830 (and conf
6831 (not (y-or-n-p
6832 (format "Delete entry with %d lines in buffer \"%s\"? "
6833 n (buffer-name buffer))))
6834 (error "Abort"))
6835 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6836 (with-current-buffer buffer (delete-region dbeg dend))
6837 (message "Agenda item and source killed"))))
6838
8bfe682a
CD
6839(defvar org-archive-default-command)
6840(defun org-agenda-archive-default ()
6841 "Archive the entry or subtree belonging to the current agenda entry."
6842 (interactive)
6843 (require 'org-archive)
6844 (org-agenda-archive-with org-archive-default-command))
6845
6846(defun org-agenda-archive-default-with-confirmation ()
6847 "Archive the entry or subtree belonging to the current agenda entry."
6848 (interactive)
6849 (require 'org-archive)
6850 (org-agenda-archive-with org-archive-default-command 'confirm))
6851
20908596
CD
6852(defun org-agenda-archive ()
6853 "Archive the entry or subtree belonging to the current agenda entry."
6854 (interactive)
8bfe682a 6855 (org-agenda-archive-with 'org-archive-subtree))
20908596
CD
6856
6857(defun org-agenda-archive-to-archive-sibling ()
8bfe682a
CD
6858 "Move the entry to the archive sibling."
6859 (interactive)
6860 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6861
6862(defun org-agenda-archive-with (cmd &optional confirm)
20908596
CD
6863 "Move the entry to the archive sibling."
6864 (interactive)
6865 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8d642074 6866 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
6867 (org-agenda-error)))
6868 (buffer (marker-buffer marker))
6869 (pos (marker-position marker)))
6870 (org-with-remote-undo buffer
6871 (with-current-buffer buffer
6872 (if (org-mode-p)
8bfe682a
CD
6873 (if (and confirm
6874 (not (y-or-n-p "Archive this subtree or entry? ")))
6875 (error "Abort")
6876 (save-excursion
6877 (goto-char pos)
6878 (org-remove-subtree-entries-from-agenda)
6879 (org-back-to-heading t)
6880 (funcall cmd)))
20908596
CD
6881 (error "Archiving works only in Org-mode files"))))))
6882
6883(defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6884 "Remove all lines in the agenda that correspond to a given subtree.
6885The subtree is the one in buffer BUF, starting at BEG and ending at END.
6886If this information is not given, the function uses the tree at point."
6887 (let ((buf (or buf (current-buffer))) m p)
6888 (save-excursion
6889 (unless (and beg end)
6890 (org-back-to-heading t)
6891 (setq beg (point))
6892 (org-end-of-subtree t)
6893 (setq end (point)))
6894 (set-buffer (get-buffer org-agenda-buffer-name))
6895 (save-excursion
6896 (goto-char (point-max))
6897 (beginning-of-line 1)
6898 (while (not (bobp))
8d642074 6899 (when (and (setq m (org-get-at-bol 'org-marker))
20908596
CD
6900 (equal buf (marker-buffer m))
6901 (setq p (marker-position m))
6902 (>= p beg)
c8d0cf5c 6903 (< p end))
20908596
CD
6904 (let ((inhibit-read-only t))
6905 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6906 (beginning-of-line 0))))))
6907
86fbb8ca 6908(defun org-agenda-refile (&optional goto rfloc no-update)
c8d0cf5c 6909 "Refile the item at point."
54a0dee5
CD
6910 (interactive "P")
6911 (if (equal goto '(16))
6912 (org-refile-goto-last-stored)
8d642074 6913 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
6914 (org-agenda-error)))
6915 (buffer (marker-buffer marker))
6916 (pos (marker-position marker))
6917 (rfloc (or rfloc
6918 (org-refile-get-location
3ab2c837 6919 (if goto "Goto" "Refile to") buffer
54a0dee5
CD
6920 org-refile-allow-creating-parent-nodes))))
6921 (with-current-buffer buffer
6922 (save-excursion
6923 (save-restriction
6924 (widen)
6925 (goto-char marker)
6926 (org-remove-subtree-entries-from-agenda)
86fbb8ca
CD
6927 (org-refile goto buffer rfloc)))))
6928 (unless no-update (org-agenda-redo))))
54a0dee5
CD
6929
6930(defun org-agenda-open-link (&optional arg)
6931 "Follow the link in the current line, if any.
8bfe682a 6932This looks for a link in the displayed line in the agenda. It also looks
54a0dee5 6933at the text of the entry itself."
c8d0cf5c 6934 (interactive "P")
8d642074
CD
6935 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6936 (org-get-at-bol 'org-marker)))
6937 (buffer (and marker (marker-buffer marker)))
6938 (prefix (buffer-substring
3ab2c837 6939 (point-at-bol) (point-at-eol))))
8bfe682a
CD
6940 (cond
6941 (buffer
6942 (with-current-buffer buffer
6943 (save-excursion
6944 (save-restriction
6945 (widen)
6946 (goto-char marker)
6947 (org-offer-links-in-entry arg prefix)))))
6948 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6949 (save-excursion
6950 (beginning-of-line 1)
6951 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6952 (org-open-link-from-string (match-string 1)))
6953 (t (error "No link to open here")))))
20908596
CD
6954
6955(defun org-agenda-copy-local-variable (var)
6956 "Get a variable from a referenced buffer and install it here."
8d642074 6957 (let ((m (org-get-at-bol 'org-marker)))
20908596
CD
6958 (when (and m (buffer-live-p (marker-buffer m)))
6959 (org-set-local var (with-current-buffer (marker-buffer m)
6960 (symbol-value var))))))
6961
6962(defun org-agenda-switch-to (&optional delete-other-windows)
6963 "Go to the Org-mode file which contains the item at point."
6964 (interactive)
8bfe682a
CD
6965 (if (and org-return-follows-link
6966 (not (org-get-at-bol 'org-marker))
6967 (org-in-regexp org-bracket-link-regexp))
6968 (org-open-link-from-string (match-string 0))
6969 (let* ((marker (or (org-get-at-bol 'org-marker)
6970 (org-agenda-error)))
6971 (buffer (marker-buffer marker))
6972 (pos (marker-position marker)))
c3313451 6973 (switch-to-buffer buffer)
8bfe682a
CD
6974 (and delete-other-windows (delete-other-windows))
6975 (widen)
6976 (goto-char pos)
6977 (when (org-mode-p)
6978 (org-show-context 'agenda)
6979 (save-excursion
6980 (and (outline-next-heading)
3ab2c837
BG
6981 (org-flag-heading nil))) ; show the next heading
6982 (when (outline-invisible-p)
6983 (show-entry)))))) ; display invisible text
20908596
CD
6984
6985(defun org-agenda-goto-mouse (ev)
6986 "Go to the Org-mode file which contains the item at the mouse click."
6987 (interactive "e")
6988 (mouse-set-point ev)
6989 (org-agenda-goto))
6990
fdf730ed
CD
6991(defun org-agenda-show (&optional full-entry)
6992 "Display the Org-mode file which contains the item at point.
6993With prefix argument FULL-ENTRY, make the entire entry visible
6994if it was hidden in the outline."
6995 (interactive "P")
20908596 6996 (let ((win (selected-window)))
fdf730ed
CD
6997 (if full-entry
6998 (let ((org-show-entry-below t))
6999 (org-agenda-goto t))
7000 (org-agenda-goto t))
20908596
CD
7001 (select-window win)))
7002
8bfe682a
CD
7003(defvar org-agenda-show-window nil)
7004(defun org-agenda-show-and-scroll-up ()
7005 "Display the Org-mode file which contains the item at point.
7006When called repeatedly, scroll the window that is displaying the buffer."
7007 (interactive)
7008 (let ((win (selected-window)))
7009 (if (and (window-live-p org-agenda-show-window)
7010 (eq this-command last-command))
7011 (progn
7012 (select-window org-agenda-show-window)
7013 (ignore-errors (scroll-up)))
7014 (org-agenda-goto t)
7015 (show-subtree)
7016 (setq org-agenda-show-window (selected-window)))
7017 (select-window win)))
7018
7019(defun org-agenda-show-scroll-down ()
7020 "Scroll down the window showing the agenda."
7021 (interactive)
7022 (let ((win (selected-window)))
7023 (when (window-live-p org-agenda-show-window)
7024 (select-window org-agenda-show-window)
7025 (ignore-errors (scroll-down))
7026 (select-window win))))
7027
c8d0cf5c
CD
7028(defun org-agenda-show-1 (&optional more)
7029 "Display the Org-mode file which contains the item at point.
8bfe682a 7030The prefix arg selects the amount of information to display:
c8d0cf5c
CD
7031
70320 hide the subtree
70331 just show the entry according to defaults.
54a0dee5
CD
70342 show the children view
70353 show the subtree view
c8d0cf5c
CD
70364 show the entire subtree and any LOGBOOK drawers
70375 show the entire subtree and any drawers
7038With prefix argument FULL-ENTRY, make the entire entry visible
7039if it was hidden in the outline."
7040 (interactive "p")
7041 (let ((win (selected-window)))
7042 (org-agenda-goto t)
7043 (org-recenter-heading 1)
7044 (cond
7045 ((= more 0)
7046 (hide-subtree)
54a0dee5
CD
7047 (save-excursion
7048 (org-back-to-heading)
7049 (run-hook-with-args 'org-cycle-hook 'folded))
7050 (message "Remote: FOLDED"))
3ab2c837 7051 ((and (org-called-interactively-p 'any) (= more 1))
c8d0cf5c
CD
7052 (message "Remote: show with default settings"))
7053 ((= more 2)
7054 (show-entry)
54a0dee5 7055 (show-children)
c8d0cf5c
CD
7056 (save-excursion
7057 (org-back-to-heading)
54a0dee5
CD
7058 (run-hook-with-args 'org-cycle-hook 'children))
7059 (message "Remote: CHILDREN"))
c8d0cf5c
CD
7060 ((= more 3)
7061 (show-subtree)
7062 (save-excursion
7063 (org-back-to-heading)
54a0dee5
CD
7064 (run-hook-with-args 'org-cycle-hook 'subtree))
7065 (message "Remote: SUBTREE"))
c8d0cf5c
CD
7066 ((= more 4)
7067 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7068 (org-drawer-regexp
7069 (concat "^[ \t]*:\\("
7070 (mapconcat 'regexp-quote org-drawers "\\|")
7071 "\\):[ \t]*$")))
7072 (show-subtree)
7073 (save-excursion
7074 (org-back-to-heading)
7075 (org-cycle-hide-drawers 'subtree)))
54a0dee5 7076 (message "Remote: SUBTREE AND LOGBOOK"))
c8d0cf5c
CD
7077 ((> more 4)
7078 (show-subtree)
54a0dee5 7079 (message "Remote: SUBTREE AND ALL DRAWERS")))
c8d0cf5c
CD
7080 (select-window win)))
7081
7082(defun org-recenter-heading (n)
7083 (save-excursion
7084 (org-back-to-heading)
7085 (recenter n)))
7086
7087(defvar org-agenda-cycle-counter nil)
54a0dee5 7088(defun org-agenda-cycle-show (&optional n)
c8d0cf5c
CD
7089 "Show the current entry in another window, with default settings.
7090Default settings are taken from `org-show-hierarchy-above' and siblings.
54a0dee5 7091When use repeatedly in immediate succession, the remote entry will cycle
c8d0cf5c
CD
7092through visibility
7093
54a0dee5
CD
7094children -> subtree -> folded
7095
7096When called with a numeric prefix arg, that arg will be passed through to
7097`org-agenda-show-1'. For the interpretation of that argument, see the
7098docstring of `org-agenda-show-1'."
7099 (interactive "P")
7100 (if (integerp n)
7101 (setq org-agenda-cycle-counter n)
7102 (if (not (eq last-command this-command))
7103 (setq org-agenda-cycle-counter 1)
7104 (if (equal org-agenda-cycle-counter 0)
7105 (setq org-agenda-cycle-counter 2)
7106 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7107 (if (> org-agenda-cycle-counter 3)
7108 (setq org-agenda-cycle-counter 0)))))
c8d0cf5c
CD
7109 (org-agenda-show-1 org-agenda-cycle-counter))
7110
20908596
CD
7111(defun org-agenda-recenter (arg)
7112 "Display the Org-mode file which contains the item at point and recenter."
7113 (interactive "P")
7114 (let ((win (selected-window)))
7115 (org-agenda-goto t)
7116 (recenter arg)
7117 (select-window win)))
7118
7119(defun org-agenda-show-mouse (ev)
7120 "Display the Org-mode file which contains the item at the mouse click."
7121 (interactive "e")
7122 (mouse-set-point ev)
7123 (org-agenda-show))
7124
7125(defun org-agenda-check-no-diary ()
7126 "Check if the entry is a diary link and abort if yes."
8d642074 7127 (if (org-get-at-bol 'org-agenda-diary-link)
20908596
CD
7128 (org-agenda-error)))
7129
7130(defun org-agenda-error ()
7131 (error "Command not allowed in this line"))
7132
7133(defun org-agenda-tree-to-indirect-buffer ()
7134 "Show the subtree corresponding to the current entry in an indirect buffer.
7135This calls the command `org-tree-to-indirect-buffer' from the original
7136Org-mode buffer.
7137With numerical prefix arg ARG, go up to this level and then take that tree.
86fbb8ca
CD
7138With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7139use the dedicated frame)."
20908596
CD
7140 (interactive)
7141 (org-agenda-check-no-diary)
8d642074 7142 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7143 (org-agenda-error)))
7144 (buffer (marker-buffer marker))
7145 (pos (marker-position marker)))
7146 (with-current-buffer buffer
7147 (save-excursion
7148 (goto-char pos)
7149 (call-interactively 'org-tree-to-indirect-buffer)))))
7150
7151(defvar org-last-heading-marker (make-marker)
7152 "Marker pointing to the headline that last changed its TODO state
7153by a remote command from the agenda.")
7154
7155(defun org-agenda-todo-nextset ()
7156 "Switch TODO entry to next sequence."
7157 (interactive)
7158 (org-agenda-todo 'nextset))
7159
7160(defun org-agenda-todo-previousset ()
7161 "Switch TODO entry to previous sequence."
7162 (interactive)
7163 (org-agenda-todo 'previousset))
7164
7165(defun org-agenda-todo (&optional arg)
7166 "Cycle TODO state of line at point, also in Org-mode file.
7167This changes the line at point, all other lines in the agenda referring to
7168the same tree node, and the headline of the tree node in the Org-mode file."
7169 (interactive "P")
7170 (org-agenda-check-no-diary)
7171 (let* ((col (current-column))
8d642074 7172 (marker (or (org-get-at-bol 'org-marker)
20908596
CD
7173 (org-agenda-error)))
7174 (buffer (marker-buffer marker))
7175 (pos (marker-position marker))
8d642074 7176 (hdmarker (org-get-at-bol 'org-hd-marker))
acedf35c 7177 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
20908596 7178 (inhibit-read-only t)
93b62de8 7179 org-agenda-headline-snapshot-before-repeat newhead just-one)
20908596
CD
7180 (org-with-remote-undo buffer
7181 (with-current-buffer buffer
7182 (widen)
7183 (goto-char pos)
7184 (org-show-context 'agenda)
7185 (save-excursion
7186 (and (outline-next-heading)
7187 (org-flag-heading nil))) ; show the next heading
a2a2e7fb
CD
7188 (let ((current-prefix-arg arg))
7189 (call-interactively 'org-todo))
20908596
CD
7190 (and (bolp) (forward-char 1))
7191 (setq newhead (org-get-heading))
93b62de8
CD
7192 (when (and (org-bound-and-true-p
7193 org-agenda-headline-snapshot-before-repeat)
7194 (not (equal org-agenda-headline-snapshot-before-repeat
7195 newhead))
7196 todayp)
7197 (setq newhead org-agenda-headline-snapshot-before-repeat
7198 just-one t))
20908596
CD
7199 (save-excursion
7200 (org-back-to-heading)
7201 (move-marker org-last-heading-marker (point))))
7202 (beginning-of-line 1)
7203 (save-excursion
93b62de8 7204 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
20908596
CD
7205 (org-move-to-column col))))
7206
7207(defun org-agenda-add-note (&optional arg)
7208 "Add a time-stamped note to the entry at point."
7209 (interactive "P")
7210 (org-agenda-check-no-diary)
8d642074 7211 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7212 (org-agenda-error)))
7213 (buffer (marker-buffer marker))
7214 (pos (marker-position marker))
8d642074 7215 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
7216 (inhibit-read-only t))
7217 (with-current-buffer buffer
7218 (widen)
7219 (goto-char pos)
7220 (org-show-context 'agenda)
7221 (save-excursion
7222 (and (outline-next-heading)
7223 (org-flag-heading nil))) ; show the next heading
7224 (org-add-note))))
7225
db55f368 7226(defun org-agenda-change-all-lines (newhead hdmarker
4ed008de 7227 &optional fixface just-this)
20908596
CD
7228 "Change all lines in the agenda buffer which match HDMARKER.
7229The new content of the line will be NEWHEAD (as modified by
7230`org-format-agenda-item'). HDMARKER is checked with
7231`equal' against all `org-hd-marker' text properties in the file.
33306645 7232If FIXFACE is non-nil, the face of each item is modified according to
db55f368
CD
7233the new TODO state.
7234If JUST-THIS is non-nil, change just the current line, not all.
33306645 7235If FORCE-TAGS is non nil, the car of it returns the new tags."
20908596 7236 (let* ((inhibit-read-only t)
93b62de8 7237 (line (org-current-line))
fdf730ed 7238 (thetags (with-current-buffer (marker-buffer hdmarker)
4ed008de
CD
7239 (save-excursion (save-restriction (widen)
7240 (goto-char hdmarker)
fdf730ed 7241 (org-get-tags-at)))))
20908596
CD
7242 props m pl undone-face done-face finish new dotime cat tags)
7243 (save-excursion
7244 (goto-char (point-max))
7245 (beginning-of-line 1)
7246 (while (not finish)
7247 (setq finish (bobp))
8d642074 7248 (when (and (setq m (org-get-at-bol 'org-hd-marker))
93b62de8 7249 (or (not just-this) (= (org-current-line) line))
20908596
CD
7250 (equal m hdmarker))
7251 (setq props (text-properties-at (point))
8d642074
CD
7252 dotime (org-get-at-bol 'dotime)
7253 cat (org-get-at-bol 'org-category)
4ed008de 7254 tags thetags
3ab2c837
BG
7255 new
7256 (let ((org-prefix-format-compiled
7257 (or (get-text-property (point) 'format)
7258 org-prefix-format-compiled)))
7259 (with-current-buffer (marker-buffer hdmarker)
7260 (save-excursion
7261 (save-restriction
7262 (widen)
7263 (org-format-agenda-item (org-get-at-bol 'extra)
7264 newhead cat tags dotime)))))
7265 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8d642074
CD
7266 undone-face (org-get-at-bol 'undone-face)
7267 done-face (org-get-at-bol 'done-face))
3ab2c837 7268 (beginning-of-line 1)
20908596
CD
7269 (cond
7270 ((equal new "")
20908596
CD
7271 (and (looking-at ".*\n?") (replace-match "")))
7272 ((looking-at ".*")
7273 (replace-match new t t)
7274 (beginning-of-line 1)
7275 (add-text-properties (point-at-bol) (point-at-eol) props)
7276 (when fixface
7277 (add-text-properties
7278 (point-at-bol) (point-at-eol)
7279 (list 'face
7280 (if org-last-todo-state-is-todo
7281 undone-face done-face))))
7282 (org-agenda-highlight-todo 'line)
7283 (beginning-of-line 1))
7284 (t (error "Line update did not work"))))
7285 (beginning-of-line 0)))
7286 (org-finalize-agenda)))
7287
7288(defun org-agenda-align-tags (&optional line)
7289 "Align all tags in agenda items to `org-agenda-tags-column'."
7290 (let ((inhibit-read-only t) l c)
7291 (save-excursion
7292 (goto-char (if line (point-at-bol) (point-min)))
afe98dfa 7293 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
20908596
CD
7294 (if line (point-at-eol) nil) t)
7295 (add-text-properties
7296 (match-beginning 2) (match-end 2)
30ab4580
GM
7297 (list 'face (delq nil (let ((prop (get-text-property
7298 (match-beginning 2) 'face)))
7299 (or (listp prop) (setq prop (list prop)))
7300 (if (memq 'org-tag prop)
7301 prop
7302 (cons 'org-tag prop))))))
20908596
CD
7303 (setq l (- (match-end 2) (match-beginning 2))
7304 c (if (< org-agenda-tags-column 0)
7305 (- (abs org-agenda-tags-column) l)
7306 org-agenda-tags-column))
7307 (delete-region (match-beginning 1) (match-end 1))
7308 (goto-char (match-beginning 1))
7309 (insert (org-add-props
7310 (make-string (max 1 (- c (current-column))) ?\ )
ed21c5c8
CD
7311 (plist-put (copy-sequence (text-properties-at (point)))
7312 'face nil))))
ff4be292
CD
7313 (goto-char (point-min))
7314 (org-font-lock-add-tag-faces (point-max)))))
20908596
CD
7315
7316(defun org-agenda-priority-up ()
7317 "Increase the priority of line at point, also in Org-mode file."
7318 (interactive)
7319 (org-agenda-priority 'up))
7320
7321(defun org-agenda-priority-down ()
7322 "Decrease the priority of line at point, also in Org-mode file."
7323 (interactive)
7324 (org-agenda-priority 'down))
7325
7326(defun org-agenda-priority (&optional force-direction)
7327 "Set the priority of line at point, also in Org-mode file.
7328This changes the line at point, all other lines in the agenda referring to
7329the same tree node, and the headline of the tree node in the Org-mode file."
7330 (interactive)
c8d0cf5c
CD
7331 (unless org-enable-priority-commands
7332 (error "Priority commands are disabled"))
20908596 7333 (org-agenda-check-no-diary)
8d642074 7334 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7335 (org-agenda-error)))
8d642074 7336 (hdmarker (org-get-at-bol 'org-hd-marker))
20908596
CD
7337 (buffer (marker-buffer hdmarker))
7338 (pos (marker-position hdmarker))
7339 (inhibit-read-only t)
7340 newhead)
7341 (org-with-remote-undo buffer
7342 (with-current-buffer buffer
7343 (widen)
7344 (goto-char pos)
7345 (org-show-context 'agenda)
7346 (save-excursion
7347 (and (outline-next-heading)
7348 (org-flag-heading nil))) ; show the next heading
7349 (funcall 'org-priority force-direction)
7350 (end-of-line 1)
7351 (setq newhead (org-get-heading)))
7352 (org-agenda-change-all-lines newhead hdmarker)
7353 (beginning-of-line 1))))
7354
7355;; FIXME: should fix the tags property of the agenda line.
c8d0cf5c 7356(defun org-agenda-set-tags (&optional tag onoff)
20908596
CD
7357 "Set tags for the current headline."
7358 (interactive)
7359 (org-agenda-check-no-diary)
3ab2c837 7360 (if (and (org-region-active-p) (org-called-interactively-p 'any))
20908596 7361 (call-interactively 'org-change-tag-in-region)
8d642074 7362 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7363 (org-agenda-error)))
7364 (buffer (marker-buffer hdmarker))
7365 (pos (marker-position hdmarker))
7366 (inhibit-read-only t)
4ed008de 7367 newhead)
20908596
CD
7368 (org-with-remote-undo buffer
7369 (with-current-buffer buffer
7370 (widen)
7371 (goto-char pos)
7372 (save-excursion
7373 (org-show-context 'agenda))
7374 (save-excursion
7375 (and (outline-next-heading)
7376 (org-flag-heading nil))) ; show the next heading
7377 (goto-char pos)
c8d0cf5c
CD
7378 (if tag
7379 (org-toggle-tag tag onoff)
7380 (call-interactively 'org-set-tags))
20908596
CD
7381 (end-of-line 1)
7382 (setq newhead (org-get-heading)))
4ed008de 7383 (org-agenda-change-all-lines newhead hdmarker)
20908596
CD
7384 (beginning-of-line 1)))))
7385
54a0dee5
CD
7386(defun org-agenda-set-property ()
7387 "Set a property for the current headline."
7388 (interactive)
7389 (org-agenda-check-no-diary)
8d642074 7390 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7391 (org-agenda-error)))
7392 (buffer (marker-buffer hdmarker))
7393 (pos (marker-position hdmarker))
7394 (inhibit-read-only t)
7395 newhead)
7396 (org-with-remote-undo buffer
7397 (with-current-buffer buffer
7398 (widen)
7399 (goto-char pos)
7400 (save-excursion
7401 (org-show-context 'agenda))
7402 (save-excursion
7403 (and (outline-next-heading)
7404 (org-flag-heading nil))) ; show the next heading
7405 (goto-char pos)
7406 (call-interactively 'org-set-property)))))
7407
7408(defun org-agenda-set-effort ()
7409 "Set the effort property for the current headline."
7410 (interactive)
7411 (org-agenda-check-no-diary)
8d642074 7412 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
54a0dee5
CD
7413 (org-agenda-error)))
7414 (buffer (marker-buffer hdmarker))
7415 (pos (marker-position hdmarker))
7416 (inhibit-read-only t)
7417 newhead)
7418 (org-with-remote-undo buffer
7419 (with-current-buffer buffer
7420 (widen)
7421 (goto-char pos)
7422 (save-excursion
7423 (org-show-context 'agenda))
7424 (save-excursion
7425 (and (outline-next-heading)
3ab2c837 7426 (org-flag-heading nil))) ; show the next heading
54a0dee5
CD
7427 (goto-char pos)
7428 (call-interactively 'org-set-effort)
3ab2c837
BG
7429 (end-of-line 1)
7430 (setq newhead (org-get-heading)))
7431 (org-agenda-change-all-lines newhead hdmarker))))
54a0dee5 7432
20908596
CD
7433(defun org-agenda-toggle-archive-tag ()
7434 "Toggle the archive tag for the current entry."
7435 (interactive)
7436 (org-agenda-check-no-diary)
8d642074 7437 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
20908596
CD
7438 (org-agenda-error)))
7439 (buffer (marker-buffer hdmarker))
7440 (pos (marker-position hdmarker))
7441 (inhibit-read-only t)
7442 newhead)
7443 (org-with-remote-undo buffer
7444 (with-current-buffer buffer
7445 (widen)
7446 (goto-char pos)
7447 (org-show-context 'agenda)
7448 (save-excursion
7449 (and (outline-next-heading)
7450 (org-flag-heading nil))) ; show the next heading
7451 (call-interactively 'org-toggle-archive-tag)
7452 (end-of-line 1)
7453 (setq newhead (org-get-heading)))
7454 (org-agenda-change-all-lines newhead hdmarker)
7455 (beginning-of-line 1))))
7456
c8d0cf5c
CD
7457(defun org-agenda-do-date-later (arg)
7458 (interactive "P")
7459 (cond
7460 ((or (equal arg '(16))
7461 (memq last-command
7462 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7463 (setq this-command 'org-agenda-date-later-minutes)
7464 (org-agenda-date-later-minutes 1))
7465 ((or (equal arg '(4))
7466 (memq last-command
7467 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7468 (setq this-command 'org-agenda-date-later-hours)
7469 (org-agenda-date-later-hours 1))
7470 (t
7471 (org-agenda-date-later (prefix-numeric-value arg)))))
7472
7473(defun org-agenda-do-date-earlier (arg)
7474 (interactive "P")
7475 (cond
7476 ((or (equal arg '(16))
7477 (memq last-command
7478 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7479 (setq this-command 'org-agenda-date-earlier-minutes)
7480 (org-agenda-date-earlier-minutes 1))
7481 ((or (equal arg '(4))
7482 (memq last-command
7483 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7484 (setq this-command 'org-agenda-date-earlier-hours)
7485 (org-agenda-date-earlier-hours 1))
7486 (t
7487 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7488
20908596 7489(defun org-agenda-date-later (arg &optional what)
3ab2c837 7490 "Change the date of this item to ARG day(s) later."
20908596
CD
7491 (interactive "p")
7492 (org-agenda-check-type t 'agenda 'timeline)
7493 (org-agenda-check-no-diary)
8d642074 7494 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7495 (org-agenda-error)))
7496 (buffer (marker-buffer marker))
7497 (pos (marker-position marker)))
7498 (org-with-remote-undo buffer
7499 (with-current-buffer buffer
7500 (widen)
7501 (goto-char pos)
7502 (if (not (org-at-timestamp-p))
7503 (error "Cannot find time stamp"))
7504 (org-timestamp-change arg (or what 'day)))
7505 (org-agenda-show-new-time marker org-last-changed-timestamp))
7506 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7507
7508(defun org-agenda-date-earlier (arg &optional what)
3ab2c837 7509 "Change the date of this item to ARG day(s) earlier."
20908596
CD
7510 (interactive "p")
7511 (org-agenda-date-later (- arg) what))
7512
c8d0cf5c
CD
7513(defun org-agenda-date-later-minutes (arg)
7514 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7515 (interactive "p")
7516 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7517 (org-agenda-date-later arg 'minute))
7518
7519(defun org-agenda-date-earlier-minutes (arg)
7520 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7521 (interactive "p")
7522 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7523 (org-agenda-date-earlier arg 'minute))
7524
7525(defun org-agenda-date-later-hours (arg)
7526 "Change the time of this item, in hour steps."
7527 (interactive "p")
7528 (org-agenda-date-later arg 'hour))
7529
7530(defun org-agenda-date-earlier-hours (arg)
7531 "Change the time of this item, in hour steps."
7532 (interactive "p")
7533 (org-agenda-date-earlier arg 'hour))
7534
20908596
CD
7535(defun org-agenda-show-new-time (marker stamp &optional prefix)
7536 "Show new date stamp via text properties."
7537 ;; We use text properties to make this undoable
71d35b24
CD
7538 (let ((inhibit-read-only t)
7539 (buffer-invisibility-spec))
20908596
CD
7540 (setq stamp (concat " " prefix " => " stamp))
7541 (save-excursion
7542 (goto-char (point-max))
7543 (while (not (bobp))
8d642074 7544 (when (equal marker (org-get-at-bol 'org-marker))
20908596 7545 (org-move-to-column (- (window-width) (length stamp)) t)
71d35b24 7546 (org-agenda-fix-tags-filter-overlays-at (point))
20908596
CD
7547 (if (featurep 'xemacs)
7548 ;; Use `duplicable' property to trigger undo recording
7549 (let ((ex (make-extent nil nil))
7550 (gl (make-glyph stamp)))
7551 (set-glyph-face gl 'secondary-selection)
7552 (set-extent-properties
7553 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7554 (insert-extent ex (1- (point)) (point-at-eol)))
7555 (add-text-properties
7556 (1- (point)) (point-at-eol)
7557 (list 'display (org-add-props stamp nil
7558 'face 'secondary-selection))))
7559 (beginning-of-line 1))
7560 (beginning-of-line 0)))))
7561
7562(defun org-agenda-date-prompt (arg)
7563 "Change the date of this item. Date is prompted for, with default today.
7564The prefix ARG is passed to the `org-time-stamp' command and can therefore
7565be used to request time specification in the time stamp."
7566 (interactive "P")
7567 (org-agenda-check-type t 'agenda 'timeline)
7568 (org-agenda-check-no-diary)
8d642074 7569 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7570 (org-agenda-error)))
7571 (buffer (marker-buffer marker))
7572 (pos (marker-position marker)))
7573 (org-with-remote-undo buffer
7574 (with-current-buffer buffer
7575 (widen)
7576 (goto-char pos)
ed21c5c8 7577 (if (not (org-at-timestamp-p t))
20908596 7578 (error "Cannot find time stamp"))
ed21c5c8 7579 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8d642074
CD
7580 (org-agenda-show-new-time marker org-last-changed-timestamp))
7581 (message "Time stamp changed to %s" org-last-changed-timestamp)))
20908596 7582
3ab2c837 7583(defun org-agenda-schedule (arg &optional time)
ed21c5c8 7584 "Schedule the item at point.
3ab2c837 7585ARG is passed through to `org-schedule'."
20908596
CD
7586 (interactive "P")
7587 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7588 (org-agenda-check-no-diary)
8d642074 7589 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7590 (org-agenda-error)))
7591 (type (marker-insertion-type marker))
7592 (buffer (marker-buffer marker))
7593 (pos (marker-position marker))
7594 (org-insert-labeled-timestamps-at-point nil)
7595 ts)
20908596
CD
7596 (set-marker-insertion-type marker t)
7597 (org-with-remote-undo buffer
7598 (with-current-buffer buffer
7599 (widen)
7600 (goto-char pos)
3ab2c837 7601 (setq ts (org-schedule arg time)))
20908596
CD
7602 (org-agenda-show-new-time marker ts "S"))
7603 (message "Item scheduled for %s" ts)))
7604
3ab2c837 7605(defun org-agenda-deadline (arg &optional time)
ed21c5c8 7606 "Schedule the item at point.
3ab2c837 7607ARG is passed through to `org-deadline'."
20908596
CD
7608 (interactive "P")
7609 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7610 (org-agenda-check-no-diary)
8d642074 7611 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596
CD
7612 (org-agenda-error)))
7613 (buffer (marker-buffer marker))
7614 (pos (marker-position marker))
7615 (org-insert-labeled-timestamps-at-point nil)
7616 ts)
7617 (org-with-remote-undo buffer
7618 (with-current-buffer buffer
7619 (widen)
7620 (goto-char pos)
3ab2c837 7621 (setq ts (org-deadline arg time)))
8d642074 7622 (org-agenda-show-new-time marker ts "D"))
20908596
CD
7623 (message "Deadline for this item set to %s" ts)))
7624
b349f79f
CD
7625(defun org-agenda-action ()
7626 "Select entry for agenda action, or execute an agenda action.
7627This command prompts for another letter. Valid inputs are:
7628
7629m Mark the entry at point for an agenda action
7630s Schedule the marked entry to the date at the cursor
7631d Set the deadline of the marked entry to the date at the cursor
7632r Call `org-remember' with cursor date as the default date
86fbb8ca 7633c Call `org-capture' with cursor date as the default date
b349f79f
CD
7634SPC Show marked entry in other window
7635TAB Visit marked entry in other window
7636
7637The cursor may be at a date in the calendar, or in the Org agenda."
7638 (interactive)
65c439fd 7639 (let (ans)
86fbb8ca 7640 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
b349f79f
CD
7641 (setq ans (read-char-exclusive))
7642 (cond
7643 ((equal ans ?m)
7644 ;; Mark this entry
7645 (if (eq major-mode 'org-agenda-mode)
8d642074
CD
7646 (let ((m (or (org-get-at-bol 'org-hd-marker)
7647 (org-get-at-bol 'org-marker))))
b349f79f
CD
7648 (if m
7649 (progn
7650 (move-marker org-agenda-action-marker
7651 (marker-position m) (marker-buffer m))
7652 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7653 (error "Don't know which entry to mark")))
7654 (error "This command works only in the agenda")))
7655 ((equal ans ?s)
7656 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7657 ((equal ans ?d)
7658 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7659 ((equal ans ?r)
7660 (org-agenda-do-action '(org-remember) t))
86fbb8ca
CD
7661 ((equal ans ?c)
7662 (org-agenda-do-action '(org-capture) t))
b349f79f
CD
7663 ((equal ans ?\ )
7664 (let ((cw (selected-window)))
7665 (org-switch-to-buffer-other-window
7666 (marker-buffer org-agenda-action-marker))
7667 (goto-char org-agenda-action-marker)
7668 (org-show-context 'agenda)
7669 (select-window cw)))
7670 ((equal ans ?\C-i)
7671 (org-switch-to-buffer-other-window
7672 (marker-buffer org-agenda-action-marker))
7673 (goto-char org-agenda-action-marker)
7674 (org-show-context 'agenda))
7675 (t (error "Invalid agenda action %c" ans)))))
7676
7677(defun org-agenda-do-action (form &optional current-buffer)
7678 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7679 (let ((org-overriding-default-time (org-get-cursor-date)))
7680 (if current-buffer
7681 (eval form)
7682 (if (not (marker-buffer org-agenda-action-marker))
8bfe682a 7683 (error "No entry has been selected for agenda action")
b349f79f
CD
7684 (with-current-buffer (marker-buffer org-agenda-action-marker)
7685 (save-excursion
7686 (save-restriction
7687 (widen)
7688 (goto-char org-agenda-action-marker)
7689 (eval form))))))))
ff4be292 7690
20908596
CD
7691(defun org-agenda-clock-in (&optional arg)
7692 "Start the clock on the currently selected item."
7693 (interactive "P")
7694 (org-agenda-check-no-diary)
7695 (if (equal arg '(4))
7696 (org-clock-in arg)
8d642074 7697 (let* ((marker (or (org-get-at-bol 'org-marker)
20908596 7698 (org-agenda-error)))
8d642074 7699 (hdmarker (or (org-get-at-bol 'org-hd-marker)
b349f79f
CD
7700 marker))
7701 (pos (marker-position marker))
7702 newhead)
20908596
CD
7703 (org-with-remote-undo (marker-buffer marker)
7704 (with-current-buffer (marker-buffer marker)
7705 (widen)
7706 (goto-char pos)
b349f79f
CD
7707 (org-show-context 'agenda)
7708 (org-show-entry)
7709 (org-cycle-hide-drawers 'children)
7710 (org-clock-in arg)
7711 (setq newhead (org-get-heading)))
c8d0cf5c 7712 (org-agenda-change-all-lines newhead hdmarker)))))
20908596 7713
afe98dfa 7714(defun org-agenda-clock-out ()
20908596 7715 "Stop the currently running clock."
afe98dfa 7716 (interactive)
20908596
CD
7717 (unless (marker-buffer org-clock-marker)
7718 (error "No running clock"))
c8d0cf5c
CD
7719 (let ((marker (make-marker)) newhead)
7720 (org-with-remote-undo (marker-buffer org-clock-marker)
7721 (with-current-buffer (marker-buffer org-clock-marker)
7722 (save-excursion
7723 (save-restriction
7724 (widen)
7725 (goto-char org-clock-marker)
7726 (org-back-to-heading t)
7727 (move-marker marker (point))
7728 (org-clock-out)
7729 (setq newhead (org-get-heading))))))
7730 (org-agenda-change-all-lines newhead marker)
7731 (move-marker marker nil)))
20908596
CD
7732
7733(defun org-agenda-clock-cancel (&optional arg)
7734 "Cancel the currently running clock."
7735 (interactive "P")
7736 (unless (marker-buffer org-clock-marker)
7737 (error "No running clock"))
7738 (org-with-remote-undo (marker-buffer org-clock-marker)
7739 (org-clock-cancel)))
7740
afe98dfa
CD
7741(defun org-agenda-clock-goto ()
7742 "Jump to the currently clocked in task within the agenda.
7743If the currently clocked in task is not listed in the agenda
7744buffer, display it in another window."
7745 (interactive)
7746 (let (pos)
7747 (mapc (lambda (o)
7748 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7749 (setq pos (overlay-start o))))
7750 (overlays-in (point-min) (point-max)))
7751 (cond (pos (goto-char pos))
7752 ;; If the currently clocked entry is not in the agenda
7753 ;; buffer, we visit it in another window:
7754 (org-clock-current-task
7755 (org-switch-to-buffer-other-window (org-clock-goto)))
7756 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7757
8bfe682a
CD
7758(defun org-agenda-diary-entry-in-org-file ()
7759 "Make a diary entry in the file `org-agenda-diary-file'."
5dec9555 7760 (let (d1 d2 char (text "") dp1 dp2)
8bfe682a
CD
7761 (if (equal (buffer-name) "*Calendar*")
7762 (setq d1 (calendar-cursor-to-date t)
7763 d2 (car calendar-mark-ring))
5dec9555
CD
7764 (setq dp1 (get-text-property (point-at-bol) 'day))
7765 (unless dp1 (error "No date defined in current line"))
7766 (setq d1 (calendar-gregorian-from-absolute dp1)
7767 d2 (and (ignore-errors (mark))
7768 (save-excursion
7769 (goto-char (mark))
7770 (setq dp2 (get-text-property (point-at-bol) 'day)))
7771 (calendar-gregorian-from-absolute dp2))))
8bfe682a
CD
7772 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7773 (setq char (read-char-exclusive))
7774 (cond
7775 ((equal char ?d)
7776 (setq text (read-string "Day entry: "))
5dec9555
CD
7777 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7778 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7779 ((equal char ?a)
7780 (setq d1 (list (car d1) (nth 1 d1)
7781 (read-number (format "Reference year [%d]: " (nth 2 d1))
7782 (nth 2 d1))))
7783 (setq text (read-string "Anniversary (use %d to show years): "))
5dec9555
CD
7784 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7785 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7786 ((equal char ?b)
7787 (setq text (read-string "Block entry: "))
7788 (unless (and d1 d2 (not (equal d1 d2)))
7789 (error "No block of days selected"))
5dec9555
CD
7790 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7791 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8bfe682a
CD
7792 ((equal char ?j)
7793 (org-switch-to-buffer-other-window
7794 (find-file-noselect org-agenda-diary-file))
ed21c5c8 7795 (require 'org-datetree)
8bfe682a
CD
7796 (org-datetree-find-date-create d1)
7797 (org-reveal t))
7798 (t (error "Invalid selection character `%c'" char)))))
7799
5dec9555
CD
7800(defcustom org-agenda-insert-diary-strategy 'date-tree
7801 "Where in `org-agenda-diary-file' should new entries be added?
7802Valid values:
7803
7804date-tree in the date tree, as child of the date
7805top-level as top-level entries at the end of the file."
7806 :group 'org-agenda
7807 :type '(choice
7808 (const :tag "in a date tree" date-tree)
7809 (const :tag "as top level at end of file" top-level)))
7810
ed21c5c8
CD
7811(defcustom org-agenda-insert-diary-extract-time nil
7812 "Non-nil means extract any time specification from the diary entry."
7813 :group 'org-agenda
7814 :type 'boolean)
7815
8bfe682a
CD
7816(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7817 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7818If TEXT is not empty, it will become the headline of the new entry, and
7819the resulting entry will not be shown. When TEXT is empty, switch to
7820`org-agenda-diary-file' and let the user finish the entry there."
7821 (let ((cw (current-window-configuration)))
7822 (org-switch-to-buffer-other-window
7823 (find-file-noselect org-agenda-diary-file))
7824 (widen)
7825 (goto-char (point-min))
7826 (cond
7827 ((eq type 'anniversary)
7828 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7829 (progn
7830 (or (org-on-heading-p t)
7831 (progn
7832 (outline-next-heading)
7833 (insert "* Anniversaries\n\n")
7834 (beginning-of-line -1)))))
7835 (outline-next-heading)
7836 (org-back-over-empty-lines)
7837 (backward-char 1)
7838 (insert "\n")
3ab2c837
BG
7839 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7840 (nth 2 d1) (car d1) (nth 1 d1) text)))
8bfe682a 7841 ((eq type 'day)
ed21c5c8
CD
7842 (let ((org-prefix-has-time t)
7843 (org-agenda-time-leading-zero t)
7844 fmt time time2)
7845 (if org-agenda-insert-diary-extract-time
7846 ;; Use org-format-agenda-item to parse text for a time-range and
7847 ;; remove it. FIXME: This is a hack, we should refactor
7848 ;; that function to make time extraction available separately
7849 (setq fmt (org-format-agenda-item nil text nil nil t)
7850 time (get-text-property 0 'time fmt)
7851 time2 (if (> (length time) 0)
7852 ;; split-string removes trailing ...... if
7853 ;; no end time given. First space
7854 ;; separates time from date.
7855 (concat " " (car (split-string time "\\.")))
7856 nil)
7857 text (get-text-property 0 'txt fmt)))
7858 (if (eq org-agenda-insert-diary-strategy 'top-level)
7859 (org-agenda-insert-diary-as-top-level text)
7860 (require 'org-datetree)
7861 (org-datetree-find-date-create d1)
7862 (org-agenda-insert-diary-make-new-entry text))
7863 (org-insert-time-stamp (org-time-from-absolute
7864 (calendar-absolute-from-gregorian d1))
7865 nil nil nil nil time2))
8bfe682a
CD
7866 (end-of-line 0))
7867 ((eq type 'block)
7868 (if (> (calendar-absolute-from-gregorian d1)
7869 (calendar-absolute-from-gregorian d2))
7870 (setq d1 (prog1 d2 (setq d2 d1))))
5dec9555
CD
7871 (if (eq org-agenda-insert-diary-strategy 'top-level)
7872 (org-agenda-insert-diary-as-top-level text)
7873 (require 'org-datetree)
7874 (org-datetree-find-date-create d1)
7875 (org-agenda-insert-diary-make-new-entry text))
8bfe682a
CD
7876 (org-insert-time-stamp (org-time-from-absolute
7877 (calendar-absolute-from-gregorian d1)))
7878 (insert "--")
7879 (org-insert-time-stamp (org-time-from-absolute
7880 (calendar-absolute-from-gregorian d2)))
7881 (end-of-line 0)))
7882 (if (string-match "\\S-" text)
7883 (progn
7884 (set-window-configuration cw)
7885 (message "%s entry added to %s"
7886 (capitalize (symbol-name type))
7887 (abbreviate-file-name org-agenda-diary-file)))
7888 (org-reveal t)
7889 (message "Please finish entry here"))))
7890
5dec9555
CD
7891(defun org-agenda-insert-diary-as-top-level (text)
7892 "Make new entry as a top-level entry at the end of the file.
7893Add TEXT as headline, and position the cursor in the second line so that
7894a timestamp can be added there."
7895 (widen)
7896 (goto-char (point-max))
7897 (or (bolp) (insert "\n"))
7898 (insert "* " text "\n")
7899 (if org-adapt-indentation (org-indent-to-column 2)))
7900
8bfe682a
CD
7901(defun org-agenda-insert-diary-make-new-entry (text)
7902 "Make new entry as last child of current entry.
7903Add TEXT as headline, and position the cursor in the second line so that
7904a timestamp can be added there."
7905 (let ((org-show-following-heading t)
7906 (org-show-siblings t)
7907 (org-show-hierarchy-above t)
7908 (org-show-entry-below t)
7909 col)
7910 (outline-next-heading)
7911 (org-back-over-empty-lines)
7912 (or (looking-at "[ \t]*$")
7913 (progn (insert "\n") (backward-char 1)))
ed21c5c8 7914 (org-insert-heading nil t)
8bfe682a
CD
7915 (org-do-demote)
7916 (setq col (current-column))
7917 (insert text "\n")
7918 (if org-adapt-indentation (org-indent-to-column col))
7919 (let ((org-show-following-heading t)
7920 (org-show-siblings t)
7921 (org-show-hierarchy-above t)
7922 (org-show-entry-below t))
7923 (org-show-context))))
7924
20908596
CD
7925(defun org-agenda-diary-entry ()
7926 "Make a diary entry, like the `i' command from the calendar.
8bfe682a
CD
7927All the standard commands work: block, weekly etc.
7928When `org-agenda-diary-file' points to a file,
7929`org-agenda-diary-entry-in-org-file' is called instead to create
7930entries in that Org-mode file."
20908596
CD
7931 (interactive)
7932 (org-agenda-check-type t 'agenda 'timeline)
8bfe682a
CD
7933 (if (not (eq org-agenda-diary-file 'diary-file))
7934 (org-agenda-diary-entry-in-org-file)
7935 (require 'diary-lib)
7936 (let* ((char (progn
7937 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7938 (read-char-exclusive)))
7939 (cmd (cdr (assoc char
7940 '((?d . insert-diary-entry)
7941 (?w . insert-weekly-diary-entry)
7942 (?m . insert-monthly-diary-entry)
7943 (?y . insert-yearly-diary-entry)
7944 (?a . insert-anniversary-diary-entry)
7945 (?b . insert-block-diary-entry)
7946 (?c . insert-cyclic-diary-entry)))))
7947 (oldf (symbol-function 'calendar-cursor-to-date))
7948 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7949 (point (point))
7950 (mark (or (mark t) (point))))
7951 (unless cmd
7952 (error "No command associated with <%c>" char))
7953 (unless (and (get-text-property point 'day)
7954 (or (not (equal ?b char))
7955 (get-text-property mark 'day)))
7956 (error "Don't know which date to use for diary entry"))
7957 ;; We implement this by hacking the `calendar-cursor-to-date' function
7958 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7959 (let ((calendar-mark-ring
7960 (list (calendar-gregorian-from-absolute
7961 (or (get-text-property mark 'day)
7962 (get-text-property point 'day))))))
7963 (unwind-protect
7964 (progn
7965 (fset 'calendar-cursor-to-date
7966 (lambda (&optional error dummy)
7967 (calendar-gregorian-from-absolute
7968 (get-text-property point 'day))))
20908596 7969 (call-interactively cmd))
8bfe682a 7970 (fset 'calendar-cursor-to-date oldf))))))
20908596 7971
20908596
CD
7972(defun org-agenda-execute-calendar-command (cmd)
7973 "Execute a calendar command from the agenda, with the date associated to
7974the cursor position."
7975 (org-agenda-check-type t 'agenda 'timeline)
7976 (require 'diary-lib)
7977 (unless (get-text-property (point) 'day)
7978 (error "Don't know which date to use for calendar command"))
7979 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7980 (point (point))
7981 (date (calendar-gregorian-from-absolute
7982 (get-text-property point 'day)))
7983 ;; the following 2 vars are needed in the calendar
7984 (displayed-month (car date))
7985 (displayed-year (nth 2 date)))
7986 (unwind-protect
7987 (progn
7988 (fset 'calendar-cursor-to-date
0627c265 7989 (lambda (&optional error dummy)
20908596
CD
7990 (calendar-gregorian-from-absolute
7991 (get-text-property point 'day))))
7992 (call-interactively cmd))
7993 (fset 'calendar-cursor-to-date oldf))))
7994
7995(defun org-agenda-phases-of-moon ()
7996 "Display the phases of the moon for the 3 months around the cursor date."
7997 (interactive)
7998 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7999
8000(defun org-agenda-holidays ()
8001 "Display the holidays for the 3 months around the cursor date."
8002 (interactive)
8003 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8004
8005(defvar calendar-longitude)
8006(defvar calendar-latitude)
8007(defvar calendar-location-name)
8008
8009(defun org-agenda-sunrise-sunset (arg)
8010 "Display sunrise and sunset for the cursor date.
8011Latitude and longitude can be specified with the variables
8012`calendar-latitude' and `calendar-longitude'. When called with prefix
8013argument, latitude and longitude will be prompted for."
8014 (interactive "P")
8015 (require 'solar)
8016 (let ((calendar-longitude (if arg nil calendar-longitude))
8017 (calendar-latitude (if arg nil calendar-latitude))
8018 (calendar-location-name
8019 (if arg "the given coordinates" calendar-location-name)))
8020 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8021
8022(defun org-agenda-goto-calendar ()
8023 "Open the Emacs calendar with the date at the cursor."
8024 (interactive)
8025 (org-agenda-check-type t 'agenda 'timeline)
8026 (let* ((day (or (get-text-property (point) 'day)
8027 (error "Don't know which date to open in calendar")))
8028 (date (calendar-gregorian-from-absolute day))
8029 (calendar-move-hook nil)
8030 (calendar-view-holidays-initially-flag nil)
3820f429 8031 (calendar-view-diary-initially-flag nil))
20908596
CD
8032 (calendar)
8033 (calendar-goto-date date)))
8034
8035;;;###autoload
8036(defun org-calendar-goto-agenda ()
8037 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8038This is a command that has to be installed in `calendar-mode-map'."
8039 (interactive)
8040 (org-agenda-list nil (calendar-absolute-from-gregorian
8041 (calendar-cursor-to-date))
8042 nil))
8043
8044(defun org-agenda-convert-date ()
8045 (interactive)
8046 (org-agenda-check-type t 'agenda 'timeline)
8047 (let ((day (get-text-property (point) 'day))
8048 date s)
8049 (unless day
8050 (error "Don't know which date to convert"))
8051 (setq date (calendar-gregorian-from-absolute day))
8052 (setq s (concat
8053 "Gregorian: " (calendar-date-string date) "\n"
8054 "ISO: " (calendar-iso-date-string date) "\n"
8055 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8056 "Julian: " (calendar-julian-date-string date) "\n"
8057 "Astron. JD: " (calendar-astro-date-string date)
8058 " (Julian date number at noon UTC)\n"
8059 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8060 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8061 "French: " (calendar-french-date-string date) "\n"
8062 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8063 "Mayan: " (calendar-mayan-date-string date) "\n"
8064 "Coptic: " (calendar-coptic-date-string date) "\n"
8065 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8066 "Persian: " (calendar-persian-date-string date) "\n"
8067 "Chinese: " (calendar-chinese-date-string date) "\n"))
8068 (with-output-to-temp-buffer "*Dates*"
8069 (princ s))
93b62de8 8070 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
20908596 8071
c8d0cf5c
CD
8072;;; Bulk commands
8073
8074(defvar org-agenda-bulk-marked-entries nil
8075 "List of markers that refer to marked entries in the agenda.")
8076
54a0dee5
CD
8077(defun org-agenda-bulk-marked-p ()
8078 (eq (get-char-property (point-at-bol) 'type)
8079 'org-marked-entry-overlay))
8080
acedf35c 8081(defun org-agenda-bulk-mark (&optional arg)
c8d0cf5c 8082 "Mark the entry at point for future bulk action."
acedf35c
CD
8083 (interactive "p")
8084 (dotimes (i (max arg 1))
8085 (unless (org-get-at-bol 'org-agenda-diary-link)
8086 (let* ((m (org-get-at-bol 'org-hd-marker))
8087 ov)
8088 (unless (org-agenda-bulk-marked-p)
8089 (unless m (error "Nothing to mark at point"))
8090 (push m org-agenda-bulk-marked-entries)
8091 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8092 (org-overlay-display ov "> "
8093 (org-get-todo-face "TODO")
8094 'evaporate)
8095 (overlay-put ov 'type 'org-marked-entry-overlay))
8096 (beginning-of-line 2)
8097 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8098 (beginning-of-line 2))
8099 (message "%d entries marked for bulk action"
8100 (length org-agenda-bulk-marked-entries))))))
c8d0cf5c 8101
3ab2c837
BG
8102(defun org-agenda-bulk-mark-regexp (regexp)
8103 "Mark entries match REGEXP."
8104 (interactive "sMark entries matching regexp: ")
8105 (let (entries-marked)
8106 (save-excursion
8107 (goto-char (point-min))
8108 (goto-char (next-single-property-change (point) 'txt))
8109 (while (re-search-forward regexp nil t)
8110 (when (string-match regexp (get-text-property (point) 'txt))
8111 (setq entries-marked (+ entries-marked 1))
8112 (call-interactively 'org-agenda-bulk-mark))))
8113 (if (not entries-marked)
8114 (message "No entry matching this regexp."))))
8115
c8d0cf5c
CD
8116(defun org-agenda-bulk-unmark ()
8117 "Unmark the entry at point for future bulk action."
8118 (interactive)
54a0dee5 8119 (when (org-agenda-bulk-marked-p)
c8d0cf5c
CD
8120 (org-agenda-bulk-remove-overlays
8121 (point-at-bol) (+ 2 (point-at-bol)))
8122 (setq org-agenda-bulk-marked-entries
8d642074 8123 (delete (org-get-at-bol 'org-hd-marker)
c8d0cf5c
CD
8124 org-agenda-bulk-marked-entries)))
8125 (beginning-of-line 2)
ed21c5c8
CD
8126 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8127 (beginning-of-line 2))
c8d0cf5c
CD
8128 (message "%d entries marked for bulk action"
8129 (length org-agenda-bulk-marked-entries)))
8130
54a0dee5
CD
8131(defun org-agenda-bulk-toggle ()
8132 "Toggle marking the entry at point for bulk action."
8133 (interactive)
8134 (if (org-agenda-bulk-marked-p)
8135 (org-agenda-bulk-unmark)
8136 (org-agenda-bulk-mark)))
c8d0cf5c
CD
8137
8138(defun org-agenda-bulk-remove-overlays (&optional beg end)
8139 "Remove the mark overlays between BEG and END in the agenda buffer.
8140BEG and END default to the buffer limits.
8141
8142This only removes the overlays, it does not remove the markers
8143from the list in `org-agenda-bulk-marked-entries'."
8144 (interactive)
8145 (mapc (lambda (ov)
86fbb8ca
CD
8146 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8147 (delete-overlay ov)))
8148 (overlays-in (or beg (point-min)) (or end (point-max)))))
c8d0cf5c
CD
8149
8150(defun org-agenda-bulk-remove-all-marks ()
8151 "Remove all marks in the agenda buffer.
8152This will remove the markers, and the overlays."
8153 (interactive)
8154 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8155 (setq org-agenda-bulk-marked-entries nil)
8156 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8157
ed21c5c8
CD
8158(defun org-agenda-bulk-action (&optional arg)
8159 "Execute an remote-editing action on all marked entries.
8160The prefix arg is passed through to the command if possible."
8161 (interactive "P")
3ab2c837
BG
8162 ;; Make sure we have markers, and only valid ones
8163 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8164 (mapc
8165 (lambda (m)
8166 (unless (and (markerp m)
8167 (marker-buffer m)
8168 (buffer-live-p (marker-buffer m))
8169 (marker-position m))
8170 (error "Marker %s for bulk command is invalid" m)))
8171 org-agenda-bulk-marked-entries)
8172
8173 ;; Prompt for the bulk command
8174 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8175 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8176 (when org-agenda-bulk-custom-functions
8177 (concat " Custom: ["
8178 (mapconcat (lambda(f) (char-to-string (car f)))
8179 org-agenda-bulk-custom-functions "")
8180 "]"))))
c8d0cf5c 8181 (let* ((action (read-char-exclusive))
ed21c5c8 8182 (org-log-refile (if org-log-refile 'time nil))
c8d0cf5c 8183 (entries (reverse org-agenda-bulk-marked-entries))
86fbb8ca 8184 redo-at-end
c8d0cf5c
CD
8185 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8186 (cond
8187 ((equal action ?$)
8188 (setq cmd '(org-agenda-archive)))
8189
8190 ((equal action ?A)
8191 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8192
8193 ((member action '(?r ?w))
8194 (setq rfloc (org-refile-get-location
3ab2c837 8195 "Refile to"
c8d0cf5c
CD
8196 (marker-buffer (car org-agenda-bulk-marked-entries))
8197 org-refile-allow-creating-parent-nodes))
86fbb8ca
CD
8198 (if (nth 3 rfloc)
8199 (setcar (nthcdr 3 rfloc)
8200 (move-marker (make-marker) (nth 3 rfloc)
8201 (or (get-file-buffer (nth 1 rfloc))
8202 (find-buffer-visiting (nth 1 rfloc))
8203 (error "This should not happen")))))
c8d0cf5c 8204
86fbb8ca
CD
8205 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8206 redo-at-end t))
c8d0cf5c
CD
8207
8208 ((equal action ?t)
54a0dee5 8209 (setq state (org-icompleting-read
c8d0cf5c
CD
8210 "Todo state: "
8211 (with-current-buffer (marker-buffer (car entries))
8212 (mapcar 'list org-todo-keywords-1))))
8213 (setq cmd `(let ((org-inhibit-blocking t)
8214 (org-inhibit-logging 'note))
8215 (org-agenda-todo ,state))))
8216
8217 ((memq action '(?- ?+))
54a0dee5 8218 (setq tag (org-icompleting-read
c8d0cf5c
CD
8219 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8220 (with-current-buffer (marker-buffer (car entries))
8221 (delq nil
8222 (mapcar (lambda (x)
8223 (if (stringp (car x)) x)) org-tag-alist)))))
8224 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8225
8226 ((memq action '(?s ?d))
ed21c5c8
CD
8227 (let* ((date (unless arg
8228 (org-read-date
8229 nil nil nil
8230 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8231 (ans (if arg nil org-read-date-final-answer))
c8d0cf5c
CD
8232 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8233 (setq cmd `(let* ((bound (fboundp 'read-string))
8234 (old (and bound (symbol-function 'read-string))))
8235 (unwind-protect
8236 (progn
8237 (fset 'read-string (lambda (&rest ignore) ,ans))
ed21c5c8 8238 (eval '(,c1 arg)))
c8d0cf5c
CD
8239 (if bound
8240 (fset 'read-string old)
8241 (fmakunbound 'read-string)))))))
acedf35c 8242
3ab2c837
BG
8243 ((equal action ?S)
8244 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8245 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8246 (let ((days (read-number
8247 (format "Scatter tasks across how many %sdays: "
8248 (if arg "week" "")) 7)))
8249 (setq cmd
8250 `(let ((distance (1+ (random ,days))))
8251 (if arg
8252 (let ((dist distance)
8253 (day-of-week
8254 (calendar-day-of-week
8255 (calendar-gregorian-from-absolute (org-today)))))
8256 (dotimes (i (1+ dist))
8257 (while (member day-of-week org-agenda-weekend-days)
8258 (incf distance)
8259 (incf day-of-week)
8260 (if (= day-of-week 7)
8261 (setq day-of-week 0)))
acedf35c
CD
8262 (incf day-of-week)
8263 (if (= day-of-week 7)
3ab2c837
BG
8264 (setq day-of-week 0)))))
8265 ;; silently fail when try to replan a sexp entry
8266 (condition-case nil
8267 (let* ((date (calendar-gregorian-from-absolute
8268 (+ (org-today) distance)))
8269 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8270 (nth 2 date))))
8271 (org-agenda-schedule nil time))
8272 (error nil)))))))
8273
8274 ((assoc action org-agenda-bulk-custom-functions)
8275 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8276 redo-at-end t))
8277
8278 ((equal action ?f)
8279 (setq cmd (list (intern
8280 (org-icompleting-read "Function: "
8281 obarray 'fboundp t nil nil)))))
acedf35c 8282
c8d0cf5c
CD
8283 (t (error "Invalid bulk action")))
8284
8285 ;; Sort the markers, to make sure that parents are handled before children
8286 (setq entries (sort entries
8287 (lambda (a b)
8288 (cond
8289 ((equal (marker-buffer a) (marker-buffer b))
8290 (< (marker-position a) (marker-position b)))
8291 (t
8292 (string< (buffer-name (marker-buffer a))
8293 (buffer-name (marker-buffer b))))))))
8294
8295 ;; Now loop over all markers and apply cmd
8296 (while (setq e (pop entries))
8297 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8298 (if (not pos)
8299 (progn (message "Skipping removed entry at %s" e)
8300 (setq cntskip (1+ cntskip)))
8301 (goto-char pos)
8302 (eval cmd)
8303 (setq org-agenda-bulk-marked-entries
8304 (delete e org-agenda-bulk-marked-entries))
8305 (setq cnt (1+ cnt))))
8306 (setq org-agenda-bulk-marked-entries nil)
8307 (org-agenda-bulk-remove-all-marks)
86fbb8ca 8308 (when redo-at-end (org-agenda-redo))
c8d0cf5c
CD
8309 (message "Acted on %d entries%s"
8310 cnt
8311 (if (= cntskip 0)
8312 ""
8313 (format ", skipped %d (disappeared before their turn)"
8314 cntskip)))))
8d642074
CD
8315
8316;;; Flagging notes
8317
8318(defun org-agenda-show-the-flagging-note ()
8319 "Display the flagging note in the other window.
8320When called a second time in direct sequence, offer to remove the FLAGGING
8321tag and (if present) the flagging note."
8322 (interactive)
8323 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8324 (win (selected-window))
8325 note heading newhead)
8326 (unless hdmarker
8327 (error "No linked entry at point"))
8328 (if (and (eq this-command last-command)
8329 (y-or-n-p "Unflag and remove any flagging note? "))
8330 (progn
8331 (org-agenda-remove-flag hdmarker)
8332 (let ((win (get-buffer-window "*Flagging Note*")))
8333 (and win (delete-window win)))
8334 (message "Entry unflaged"))
8335 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8336 (unless note
8337 (error "No flagging note"))
8338 (org-kill-new note)
8339 (org-switch-to-buffer-other-window "*Flagging Note*")
8340 (erase-buffer)
8341 (insert note)
8342 (goto-char (point-min))
8343 (while (re-search-forward "\\\\n" nil t)
8344 (replace-match "\n" t t))
8345 (goto-char (point-min))
8346 (select-window win)
8347 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8348
8349(defun org-agenda-remove-flag (marker)
8bfe682a 8350 "Remove the FLAGGED tag and any flagging note in the entry."
8d642074
CD
8351 (let (newhead)
8352 (org-with-point-at marker
8353 (org-toggle-tag "FLAGGED" 'off)
8354 (org-entry-delete nil "THEFLAGGINGNOTE")
8355 (setq newhead (org-get-heading)))
8356 (org-agenda-change-all-lines newhead marker)
8357 (message "Entry unflaged")))
8358
8359(defun org-agenda-get-any-marker (&optional pos)
8360 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8361 (get-text-property (or pos (point-at-bol)) 'org-marker)))
c8d0cf5c 8362
20908596
CD
8363;;; Appointment reminders
8364
8365(defvar appt-time-msg-list)
8366
8367;;;###autoload
8368(defun org-agenda-to-appt (&optional refresh filter)
8369 "Activate appointments found in `org-agenda-files'.
8370With a \\[universal-argument] prefix, refresh the list of
33306645 8371appointments.
20908596
CD
8372
8373If FILTER is t, interactively prompt the user for a regular
8374expression, and filter out entries that don't match it.
8375
8376If FILTER is a string, use this string as a regular expression
8377for filtering entries out.
8378
8379FILTER can also be an alist with the car of each cell being
8380either 'headline or 'category. For example:
8381
8382 '((headline \"IMPORTANT\")
8383 (category \"Work\"))
8384
8385will only add headlines containing IMPORTANT or headlines
8386belonging to the \"Work\" category."
8387 (interactive "P")
20908596
CD
8388 (if refresh (setq appt-time-msg-list nil))
8389 (if (eq filter t)
8390 (setq filter (read-from-minibuffer "Regexp filter: ")))
8391 (let* ((cnt 0) ; count added events
8392 (org-agenda-new-buffers nil)
8393 (org-deadline-warning-days 0)
acedf35c
CD
8394 ;; Do not use `org-today' here because appt only takes
8395 ;; time and without date as argument, so it may pass wrong
8396 ;; information otherwise
20908596
CD
8397 (today (org-date-to-gregorian
8398 (time-to-days (current-time))))
c8d0cf5c 8399 (org-agenda-restrict nil)
621f83e4 8400 (files (org-agenda-files 'unrestricted)) entries file)
20908596 8401 ;; Get all entries which may contain an appt
db55f368 8402 (org-prepare-agenda-buffers files)
20908596
CD
8403 (while (setq file (pop files))
8404 (setq entries
8405 (append entries
8406 (org-agenda-get-day-entries
8407 file today :timestamp :scheduled :deadline))))
8408 (setq entries (delq nil entries))
8409 ;; Map thru entries and find if we should filter them out
8410 (mapc
8411 (lambda(x)
621f83e4 8412 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
20908596
CD
8413 (cat (get-text-property 1 'org-category x))
8414 (tod (get-text-property 1 'time-of-day x))
8415 (ok (or (null filter)
8416 (and (stringp filter) (string-match filter evt))
8417 (and (listp filter)
8418 (or (string-match
8419 (cadr (assoc 'category filter)) cat)
8420 (string-match
8421 (cadr (assoc 'headline filter)) evt))))))
8422 ;; FIXME: Shall we remove text-properties for the appt text?
8423 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8424 (when (and ok tod)
621f83e4 8425 (setq tod (concat "00" (number-to-string tod))
20908596 8426 tod (when (string-match
621f83e4 8427 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
20908596
CD
8428 (concat (match-string 1 tod) ":"
8429 (match-string 2 tod))))
8430 (appt-add tod evt)
8431 (setq cnt (1+ cnt))))) entries)
8432 (org-release-buffers org-agenda-new-buffers)
8433 (if (eq cnt 0)
8434 (message "No event to add")
8435 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8436
621f83e4
CD
8437(defun org-agenda-todayp (date)
8438 "Does DATE mean today, when considering `org-extend-today-until'?"
acedf35c
CD
8439 (let ((today (org-today))
8440 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8441 date)))
8442 (eq date today)))
621f83e4 8443
20908596
CD
8444(provide 'org-agenda)
8445
5b409b39 8446
b349f79f 8447
20908596 8448;;; org-agenda.el ends here