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