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