]> code.delx.au - gnu-emacs/blob - lisp/org/org-agenda.el
Quoting fixes in lisp/org
[gnu-emacs] / lisp / org / org-agenda.el
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
2
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
4
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
10 ;;
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;
25 ;;; Commentary:
26
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
28 ;;
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
35 ;;
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
37 ;;
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
42 ;;
43 ;; This is a hack, but it works even when running Org byte-compiled.
44 ;;
45
46 ;;; Code:
47
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
52
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
71
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
86 (declare-function org-capture "org-capture" (&optional goto keys))
87
88 (defvar calendar-mode-map) ; defined in calendar.el
89 (defvar org-clock-current-task nil) ; defined in org-clock.el
90 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91 (defvar org-habit-show-habits) ; defined in org-habit.el
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
94
95 ;; Defined somewhere in this file, but used before definition.
96 (defvar org-agenda-buffer-name "*Org Agenda*")
97 (defvar org-agenda-overriding-header nil)
98 (defvar org-agenda-title-append nil)
99 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
101 (defvar original-date) ; dynamically scoped, calendar.el does scope this
102
103 (defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105 (defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
107
108 (defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110 When t, a confirmation is always needed. When a number N, confirmation is
111 only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
116 (integer :tag "When more than N lines")))
117
118 (defcustom org-agenda-compact-blocks nil
119 "Non-nil means make the block agenda more compact.
120 This is done globally by leaving out lines like the agenda span
121 name and week number or the separator lines."
122 :group 'org-agenda
123 :type 'boolean)
124
125 (defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127 If this is a string, it will be used as the separator, with a newline added.
128 If it is a character, it will be repeated to fill the window width.
129 If nil the separator is disabled. In `org-agenda-custom-commands' this
130 addresses the separator between the current and the previous block."
131 :group 'org-agenda
132 :type '(choice
133 (const :tag "Disabled" nil)
134 (character)
135 (string)))
136
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
141
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
146
147 (defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
149 This is a good place to set options for ps-print and for htmlize.
150 Note that the way this is implemented, the values will be evaluated
151 before assigned to the variables. So make sure to quote values you do
152 *not* want evaluated, for example
153
154 (setq org-agenda-exporter-settings
155 '((ps-print-color-p 'black-white)))"
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
161
162 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164 A useful function for this hook is `org-agenda-add-entry-text'."
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
168
169 (defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171 This is only relevant when `org-agenda-add-entry-text' is part of
172 `org-agenda-before-write-hook', which is the default.
173 When this is 0, nothing will happen. When it is greater than 0, it
174 specifies the maximum number of lines that will be added for each entry
175 that is listed in the agenda view.
176
177 Note that this variable is not used during display, only when exporting
178 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179 and `org-agenda-entry-text-maxlines'."
180 :group 'org-agenda
181 :type 'integer)
182
183 (defcustom org-agenda-add-entry-text-descriptive-links t
184 "Non-nil means export org-links as descriptive links in agenda added text.
185 This variable applies to the text added to the agenda when
186 `org-agenda-add-entry-text-maxlines' is larger than 0.
187 When this variable nil, the URL will (also) be shown."
188 :group 'org-agenda
189 :type 'boolean)
190
191 (defcustom org-agenda-export-html-style nil
192 "The style specification for exported HTML Agenda files.
193 If this variable contains a string, it will replace the default <style>
194 section as produced by `htmlize'.
195 Since there are different ways of setting style information, this variable
196 needs to contain the full HTML structure to provide a style, including the
197 surrounding HTML tags. The style specifications should include definitions
198 the fonts used by the agenda, here is an example:
199
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
206 }
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
210 }
211 .org-agenda-done {
212 color: #339933;
213 }
214 .org-done {
215 color: #339933;
216 }
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
221
222 or, if you want to keep the style in a file,
223
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
225
226 As the value of this option simply gets inserted into the HTML <head> header,
227 you can \"misuse\" it to also add other text to the header."
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type '(choice
231 (const nil)
232 (string)))
233
234 (defcustom org-agenda-persistent-filter nil
235 "When set, keep filters from one agenda view to the next."
236 :group 'org-agenda
237 :type 'boolean)
238
239 (defgroup org-agenda-custom-commands nil
240 "Options concerning agenda views in Org-mode."
241 :tag "Org Agenda Custom Commands"
242 :group 'org-agenda)
243
244 (defconst org-sorting-choice
245 '(choice
246 (const time-up) (const time-down)
247 (const timestamp-up) (const timestamp-down)
248 (const scheduled-up) (const scheduled-down)
249 (const deadline-up) (const deadline-down)
250 (const ts-up) (const ts-down)
251 (const tsia-up) (const tsia-down)
252 (const category-keep) (const category-up) (const category-down)
253 (const tag-down) (const tag-up)
254 (const priority-up) (const priority-down)
255 (const todo-state-up) (const todo-state-down)
256 (const effort-up) (const effort-down)
257 (const habit-up) (const habit-down)
258 (const alpha-up) (const alpha-down)
259 (const user-defined-up) (const user-defined-down))
260 "Sorting choices.")
261
262 ;; Keep custom values for `org-agenda-filter-preset' compatible with
263 ;; the new variable `org-agenda-tag-filter-preset'.
264 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
266
267 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
268 "List of types searched for when creating the daily/weekly agenda.
269 This variable is a list of symbols that controls the types of
270 items that appear in the daily/weekly agenda. Allowed symbols in this
271 list are are
272
273 :timestamp List items containing a date stamp or date range matching
274 the selected date. This includes sexp entries in angular
275 brackets.
276
277 :sexp List entries resulting from plain diary-like sexps.
278
279 :deadline List deadline due on that date. When the date is today,
280 also list any deadlines past due, or due within
281 `org-deadline-warning-days'. `:deadline' must appear before
282 `:scheduled' if the setting of
283 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
284 any effect.
285
286 :deadline* Same as above, but only include the deadline if it has an
287 hour specification as [h]h:mm.
288
289 :scheduled List all items which are scheduled for the given date.
290 The diary for *today* also contains items which were
291 scheduled earlier and are not yet marked DONE.
292
293 :scheduled* Same as above, but only include the scheduled item if it
294 has an hour specification as [h]h:mm.
295
296 By default, all four non-starred types are turned on.
297
298 When :scheduled* or :deadline* are included, :schedule or :deadline
299 will be ignored.
300
301 Never set this variable globally using `setq', because then it
302 will apply to all future agenda commands. Instead, bind it with
303 `let' to scope it dynamically into the agenda-constructing
304 command. A good way to set it is through options in
305 `org-agenda-custom-commands'. For a more flexible (though
306 somewhat less efficient) way of determining what is included in
307 the daily/weekly agenda, see `org-agenda-skip-function'.")
308
309 (defconst org-agenda-custom-commands-local-options
310 `(repeat :tag "Local settings for this command. Remember to quote values"
311 (choice :tag "Setting"
312 (list :tag "Heading for this block"
313 (const org-agenda-overriding-header)
314 (string :tag "Headline"))
315 (list :tag "Files to be searched"
316 (const org-agenda-files)
317 (list
318 (const :format "" quote)
319 (repeat (file))))
320 (list :tag "Sorting strategy"
321 (const org-agenda-sorting-strategy)
322 (list
323 (const :format "" quote)
324 (repeat
325 ,org-sorting-choice)))
326 (list :tag "Prefix format"
327 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
328 (string))
329 (list :tag "Number of days in agenda"
330 (const org-agenda-span)
331 (choice (const :tag "Day" day)
332 (const :tag "Week" week)
333 (const :tag "Fortnight" fortnight)
334 (const :tag "Month" month)
335 (const :tag "Year" year)
336 (integer :tag "Custom")))
337 (list :tag "Fixed starting date"
338 (const org-agenda-start-day)
339 (string :value "2007-11-01"))
340 (list :tag "Start on day of week"
341 (const org-agenda-start-on-weekday)
342 (choice :value 1
343 (const :tag "Today" nil)
344 (integer :tag "Weekday No.")))
345 (list :tag "Include data from diary"
346 (const org-agenda-include-diary)
347 (boolean))
348 (list :tag "Deadline Warning days"
349 (const org-deadline-warning-days)
350 (integer :value 1))
351 (list :tag "Category filter preset"
352 (const org-agenda-category-filter-preset)
353 (list
354 (const :format "" quote)
355 (repeat
356 (string :tag "+category or -category"))))
357 (list :tag "Tags filter preset"
358 (const org-agenda-tag-filter-preset)
359 (list
360 (const :format "" quote)
361 (repeat
362 (string :tag "+tag or -tag"))))
363 (list :tag "Regexp filter preset"
364 (const org-agenda-regexp-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+regexp or -regexp"))))
369 (list :tag "Set daily/weekly entry types"
370 (const org-agenda-entry-types)
371 (list
372 (const :format "" quote)
373 (set :greedy t :value ,org-agenda-entry-types
374 (const :deadline)
375 (const :scheduled)
376 (const :deadline*)
377 (const :scheduled*)
378 (const :timestamp)
379 (const :sexp))))
380 (list :tag "Standard skipping condition"
381 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
382 (const org-agenda-skip-function)
383 (list
384 (const :format "" quote)
385 (list
386 (choice
387 :tag "Skipping range"
388 (const :tag "Skip entry" org-agenda-skip-entry-if)
389 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
390 (repeat :inline t :tag "Conditions for skipping"
391 (choice
392 :tag "Condition type"
393 (list :tag "Regexp matches" :inline t
394 (const :format "" 'regexp)
395 (regexp))
396 (list :tag "Regexp does not match" :inline t
397 (const :format "" 'notregexp)
398 (regexp))
399 (list :tag "TODO state is" :inline t
400 (const 'todo)
401 (choice
402 (const :tag "Any not-done state" 'todo)
403 (const :tag "Any done state" 'done)
404 (const :tag "Any state" 'any)
405 (list :tag "Keyword list"
406 (const :format "" quote)
407 (repeat (string :tag "Keyword")))))
408 (list :tag "TODO state is not" :inline t
409 (const 'nottodo)
410 (choice
411 (const :tag "Any not-done state" 'todo)
412 (const :tag "Any done state" 'done)
413 (const :tag "Any state" 'any)
414 (list :tag "Keyword list"
415 (const :format "" quote)
416 (repeat (string :tag "Keyword")))))
417 (const :tag "scheduled" 'scheduled)
418 (const :tag "not scheduled" 'notscheduled)
419 (const :tag "deadline" 'deadline)
420 (const :tag "no deadline" 'notdeadline)
421 (const :tag "timestamp" 'timestamp)
422 (const :tag "no timestamp" 'nottimestamp))))))
423 (list :tag "Non-standard skipping condition"
424 :value (org-agenda-skip-function)
425 (const org-agenda-skip-function)
426 (sexp :tag "Function or form (quoted!)"))
427 (list :tag "Any variable"
428 (variable :tag "Variable")
429 (sexp :tag "Value (sexp)"))))
430 "Selection of examples for agenda command settings.
431 This will be spliced into the custom type of
432 `org-agenda-custom-commands'.")
433
434
435 (defcustom org-agenda-custom-commands
436 '(("n" "Agenda and all TODOs" ((agenda "") (alltodo ""))))
437 "Custom commands for the agenda.
438 These commands will be offered on the splash screen displayed by the
439 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
440
441 (key desc type match settings files)
442
443 key The key (one or more characters as a string) to be associated
444 with the command.
445 desc A description of the command, when omitted or nil, a default
446 description is built using MATCH.
447 type The command type, any of the following symbols:
448 agenda The daily/weekly agenda.
449 todo Entries with a specific TODO keyword, in all agenda files.
450 search Entries containing search words entry or headline.
451 tags Tags/Property/TODO match in all agenda files.
452 tags-todo Tags/P/T match in all agenda files, TODO entries only.
453 todo-tree Sparse tree of specific TODO keyword in *current* file.
454 tags-tree Sparse tree with all tags matches in *current* file.
455 occur-tree Occur sparse tree for *current* file.
456 ... A user-defined function.
457 match What to search for:
458 - a single keyword for TODO keyword searches
459 - a tags match expression for tags searches
460 - a word search expression for text searches.
461 - a regular expression for occur searches
462 For all other commands, this should be the empty string.
463 settings A list of option settings, similar to that in a let form, so like
464 this: ((opt1 val1) (opt2 val2) ...). The values will be
465 evaluated at the moment of execution, so quote them when needed.
466 files A list of files file to write the produced agenda buffer to
467 with the command `org-store-agenda-views'.
468 If a file name ends in \".html\", an HTML version of the buffer
469 is written out. If it ends in \".ps\", a postscript version is
470 produced. Otherwise, only the plain text is written to the file.
471
472 You can also define a set of commands, to create a composite agenda buffer.
473 In this case, an entry looks like this:
474
475 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
476
477 where
478
479 desc A description string to be displayed in the dispatcher menu.
480 cmd An agenda command, similar to the above. However, tree commands
481 are not allowed, but instead you can get agenda and global todo list.
482 So valid commands for a set are:
483 (agenda \"\" settings)
484 (alltodo \"\" settings)
485 (stuck \"\" settings)
486 (todo \"match\" settings files)
487 (search \"match\" settings files)
488 (tags \"match\" settings files)
489 (tags-todo \"match\" settings files)
490
491 Each command can carry a list of options, and another set of options can be
492 given for the whole set of commands. Individual command options take
493 precedence over the general options.
494
495 When using several characters as key to a command, the first characters
496 are prefix commands. For the dispatcher to display useful information, you
497 should provide a description for the prefix, like
498
499 (setq org-agenda-custom-commands
500 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
501 (\"hl\" tags \"+HOME+Lisa\")
502 (\"hp\" tags \"+HOME+Peter\")
503 (\"hk\" tags \"+HOME+Kim\")))"
504 :group 'org-agenda-custom-commands
505 :type `(repeat
506 (choice :value ("x" "Describe command here" tags "" nil)
507 (list :tag "Single command"
508 (string :tag "Access Key(s) ")
509 (option (string :tag "Description"))
510 (choice
511 (const :tag "Agenda" agenda)
512 (const :tag "TODO list" alltodo)
513 (const :tag "Search words" search)
514 (const :tag "Stuck projects" stuck)
515 (const :tag "Tags/Property match (all agenda files)" tags)
516 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
517 (const :tag "TODO keyword search (all agenda files)" todo)
518 (const :tag "Tags sparse tree (current buffer)" tags-tree)
519 (const :tag "TODO keyword tree (current buffer)" todo-tree)
520 (const :tag "Occur tree (current buffer)" occur-tree)
521 (sexp :tag "Other, user-defined function"))
522 (string :tag "Match (only for some commands)")
523 ,org-agenda-custom-commands-local-options
524 (option (repeat :tag "Export" (file :tag "Export to"))))
525 (list :tag "Command series, all agenda files"
526 (string :tag "Access Key(s)")
527 (string :tag "Description ")
528 (repeat :tag "Component"
529 (choice
530 (list :tag "Agenda"
531 (const :format "" agenda)
532 (const :tag "" :format "" "")
533 ,org-agenda-custom-commands-local-options)
534 (list :tag "TODO list (all keywords)"
535 (const :format "" alltodo)
536 (const :tag "" :format "" "")
537 ,org-agenda-custom-commands-local-options)
538 (list :tag "Search words"
539 (const :format "" search)
540 (string :tag "Match")
541 ,org-agenda-custom-commands-local-options)
542 (list :tag "Stuck projects"
543 (const :format "" stuck)
544 (const :tag "" :format "" "")
545 ,org-agenda-custom-commands-local-options)
546 (list :tag "Tags search"
547 (const :format "" tags)
548 (string :tag "Match")
549 ,org-agenda-custom-commands-local-options)
550 (list :tag "Tags search, TODO entries only"
551 (const :format "" tags-todo)
552 (string :tag "Match")
553 ,org-agenda-custom-commands-local-options)
554 (list :tag "TODO keyword search"
555 (const :format "" todo)
556 (string :tag "Match")
557 ,org-agenda-custom-commands-local-options)
558 (list :tag "Other, user-defined function"
559 (symbol :tag "function")
560 (string :tag "Match")
561 ,org-agenda-custom-commands-local-options)))
562
563 (repeat :tag "Settings for entire command set"
564 (list (variable :tag "Any variable")
565 (sexp :tag "Value")))
566 (option (repeat :tag "Export" (file :tag "Export to"))))
567 (cons :tag "Prefix key documentation"
568 (string :tag "Access Key(s)")
569 (string :tag "Description ")))))
570
571 (defcustom org-agenda-query-register ?o
572 "The register holding the current query string.
573 The purpose of this is that if you construct a query string interactively,
574 you can then use it to define a custom command."
575 :group 'org-agenda-custom-commands
576 :type 'character)
577
578 (defcustom org-stuck-projects
579 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
580 "How to identify stuck projects.
581 This is a list of four items:
582 1. A tags/todo/property matcher string that is used to identify a project.
583 See the manual for a description of tag and property searches.
584 The entire tree below a headline matched by this is considered one project.
585 2. A list of TODO keywords identifying non-stuck projects.
586 If the project subtree contains any headline with one of these todo
587 keywords, the project is considered to be not stuck. If you specify
588 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
589 3. A list of tags identifying non-stuck projects.
590 If the project subtree contains any headline with one of these tags,
591 the project is considered to be not stuck. If you specify \"*\" as
592 a tag, any tag will mark the project unstuck. Note that this is about
593 the explicit presence of a tag somewhere in the subtree, inherited
594 tags do not count here. If inherited tags make a project not stuck,
595 use \"-TAG\" in the tags part of the matcher under (1.) above.
596 4. An arbitrary regular expression matching non-stuck projects.
597
598 If the project turns out to be not stuck, search continues also in the
599 subtree to see if any of the subtasks have project status.
600
601 See also the variable `org-tags-match-list-sublevels' which applies
602 to projects matched by this search as well.
603
604 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
605 or `C-c a #' to produce the list."
606 :group 'org-agenda-custom-commands
607 :type '(list
608 (string :tag "Tags/TODO match to identify a project")
609 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
610 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
611 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
612
613 (defcustom org-agenda-filter-effort-default-operator "<"
614 "The default operator for effort estimate filtering.
615 If you select an effort estimate limit without first pressing an operator,
616 this one will be used."
617 :group 'org-agenda-custom-commands
618 :type '(choice (const :tag "less or equal" "<")
619 (const :tag "greater or equal"">")
620 (const :tag "equal" "=")))
621
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
626
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
633
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
642
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
659
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
666
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
673
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
679
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
687
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
699
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
704
705 past Don't show entries for today or in the past.
706
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
711
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
715
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
723
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
736
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
741
742 past Don't show entries scheduled today or in the past.
743
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
747
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
751
752 t Same as `all', for backward compatibility.
753
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
756
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
769
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
774
775 This applies when creating the global todo list.
776 Valid values are:
777
778 near Don't show near deadline entries. A deadline is near when it is
779 closer than `org-deadline-warning-days' days. The idea behind this
780 is that such items will appear in the agenda anyway.
781
782 far Don't show TODO entries where a deadline has been defined, but
783 the deadline is not near. This is useful if you don't want to
784 use the todo list to figure out what to do now.
785
786 past Don't show entries with a deadline timestamp for today or in the past.
787
788 future Don't show entries with a deadline timestamp in the future, not even
789 when they become `near' ones. Use it with caution.
790
791 all Ignore all TODO entries that do have a deadline.
792
793 t Same as `near', for backward compatibility.
794
795 This variable can also have an integer as a value. See
796 `org-agenda-todo-ignore-timestamp' for more details.
797
798 See also `org-agenda-todo-ignore-with-date'.
799 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
800 to make his option also apply to the tags-todo list."
801 :group 'org-agenda-skip
802 :group 'org-agenda-todo-list
803 :type '(choice
804 (const :tag "Ignore near deadlines" near)
805 (const :tag "Ignore near deadlines (compatibility)" t)
806 (const :tag "Ignore far deadlines" far)
807 (const :tag "Ignore all TODOs with a deadlines" all)
808 (const :tag "Show all TODOs, even if they have a deadline" nil)
809 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
810
811 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
812 "Time unit to use when possibly ignoring an agenda item.
813
814 See the docstring of various `org-agenda-todo-ignore-*' options.
815 The default is to compare time stamps using days. An item is thus
816 considered to be in the future if it is at least one day after today.
817 Non-nil means to compare time stamps using seconds. An item is then
818 considered future if it has a time value later than current time."
819 :group 'org-agenda-skip
820 :group 'org-agenda-todo-list
821 :version "24.4"
822 :package-version '(Org . "8.0")
823 :type '(choice
824 (const :tag "Compare time with days" nil)
825 (const :tag "Compare time with seconds" t)))
826
827 (defcustom org-agenda-tags-todo-honor-ignore-options nil
828 "Non-nil means honor todo-list ignores options also in tags-todo search.
829 The variables
830 `org-agenda-todo-ignore-with-date',
831 `org-agenda-todo-ignore-timestamp',
832 `org-agenda-todo-ignore-scheduled',
833 `org-agenda-todo-ignore-deadlines'
834 make the global TODO list skip entries that have time stamps of certain
835 kinds. If this option is set, the same options will also apply for the
836 tags-todo search, which is the general tags/property matcher
837 restricted to unfinished TODO entries only."
838 :group 'org-agenda-skip
839 :group 'org-agenda-todo-list
840 :group 'org-agenda-match-view
841 :type 'boolean)
842
843 (defcustom org-agenda-skip-scheduled-if-done nil
844 "Non-nil means don't show scheduled items in agenda when they are done.
845 This is relevant for the daily/weekly agenda, not for the TODO list. And
846 it applies only to the actual date of the scheduling. Warnings about
847 an item with a past scheduling dates are always turned off when the item
848 is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
852
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
855
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
859
860 When this variable is nil, the entry will be shown several times.
861
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
865
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
868
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
878
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
893
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. And it applied only to the
898 actually date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
903
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
924
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
940
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
947
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
953
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
960
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
976
977 (defcustom org-timeline-show-empty-dates 3
978 "Non-nil means `org-timeline' also shows dates without an entry.
979 When nil, only the days which actually have entries are shown.
980 When t, all days between the first and the last date are shown.
981 When an integer, show also empty dates, but if there is a gap of more than
982 N days, just insert a special line indicating the size of the gap."
983 :group 'org-agenda-skip
984 :type '(choice
985 (const :tag "None" nil)
986 (const :tag "All" t)
987 (integer :tag "at most")))
988
989 (defgroup org-agenda-startup nil
990 "Options concerning initial settings in the Agenda in Org Mode."
991 :tag "Org Agenda Startup"
992 :group 'org-agenda)
993
994 (defcustom org-agenda-menu-show-matcher t
995 "Non-nil means show the match string in the agenda dispatcher menu.
996 When nil, the matcher string is not shown, but is put into the help-echo
997 property so than moving the mouse over the command shows it.
998 Setting it to nil is good if matcher strings are very long and/or if
999 you want to use two-columns display (see `org-agenda-menu-two-columns')."
1000 :group 'org-agenda
1001 :version "24.1"
1002 :type 'boolean)
1003
1004 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1005
1006 (defcustom org-agenda-menu-two-columns nil
1007 "Non-nil means, use two columns to show custom commands in the dispatcher.
1008 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1009 to nil."
1010 :group 'org-agenda
1011 :version "24.1"
1012 :type 'boolean)
1013
1014 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1015 (defcustom org-agenda-finalize-hook nil
1016 "Hook run just before displaying an agenda buffer.
1017 The buffer is still writable when the hook is called.
1018
1019 You can modify some of the buffer substrings but you should be
1020 extra careful not to modify the text properties of the agenda
1021 headlines as the agenda display heavily relies on them."
1022 :group 'org-agenda-startup
1023 :type 'hook)
1024
1025 (defcustom org-agenda-mouse-1-follows-link nil
1026 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1027 A longer mouse click will still set point. Does not work on XEmacs.
1028 Needs to be set before org.el is loaded."
1029 :group 'org-agenda-startup
1030 :type 'boolean)
1031
1032 (defcustom org-agenda-start-with-follow-mode nil
1033 "The initial value of follow mode in a newly created agenda window."
1034 :group 'org-agenda-startup
1035 :type 'boolean)
1036
1037 (defcustom org-agenda-follow-indirect nil
1038 "Non-nil means `org-agenda-follow-mode' displays only the
1039 current item's tree, in an indirect buffer."
1040 :group 'org-agenda
1041 :version "24.1"
1042 :type 'boolean)
1043
1044 (defcustom org-agenda-show-outline-path t
1045 "Non-nil means show outline path in echo area after line motion."
1046 :group 'org-agenda-startup
1047 :type 'boolean)
1048
1049 (defcustom org-agenda-start-with-entry-text-mode nil
1050 "The initial value of entry-text-mode in a newly created agenda window."
1051 :group 'org-agenda-startup
1052 :type 'boolean)
1053
1054 (defcustom org-agenda-entry-text-maxlines 5
1055 "Number of text lines to be added when `E' is pressed in the agenda.
1056
1057 Note that this variable only used during agenda display. Add add entry text
1058 when exporting the agenda, configure the variable
1059 `org-agenda-add-entry-ext-maxlines'."
1060 :group 'org-agenda
1061 :type 'integer)
1062
1063 (defcustom org-agenda-entry-text-exclude-regexps nil
1064 "List of regular expressions to clean up entry text.
1065 The complete matches of all regular expressions in this list will be
1066 removed from entry text before it is shown in the agenda."
1067 :group 'org-agenda
1068 :type '(repeat (regexp)))
1069
1070 (defcustom org-agenda-entry-text-leaders " > "
1071 "Text prepended to the entry text in agenda buffers."
1072 :version "24.4"
1073 :package-version '(Org . "8.0")
1074 :group 'org-agenda
1075 :type 'string)
1076
1077 (defvar org-agenda-entry-text-cleanup-hook nil
1078 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1079 This cleanup is done in a temporary buffer, so the function may inspect and
1080 change the entire buffer.
1081 Some default stuff like drawers and scheduling/deadline dates will already
1082 have been removed when this is called, as will any matches for regular
1083 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1084
1085 (defvar org-agenda-include-inactive-timestamps nil
1086 "Non-nil means include inactive time stamps in agenda and timeline.
1087 Dynamically scoped.")
1088
1089 (defgroup org-agenda-windows nil
1090 "Options concerning the windows used by the Agenda in Org Mode."
1091 :tag "Org Agenda Windows"
1092 :group 'org-agenda)
1093
1094 (defcustom org-agenda-window-setup 'reorganize-frame
1095 "How the agenda buffer should be displayed.
1096 Possible values for this option are:
1097
1098 current-window Show agenda in the current window, keeping all other windows.
1099 other-window Use `switch-to-buffer-other-window' to display agenda.
1100 reorganize-frame Show only two windows on the current frame, the current
1101 window and the agenda.
1102 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1103 Also, when exiting the agenda, kill that frame.
1104 See also the variable `org-agenda-restore-windows-after-quit'."
1105 :group 'org-agenda-windows
1106 :type '(choice
1107 (const current-window)
1108 (const other-frame)
1109 (const other-window)
1110 (const reorganize-frame)))
1111
1112 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1113 "The min and max height of the agenda window as a fraction of frame height.
1114 The value of the variable is a cons cell with two numbers between 0 and 1.
1115 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1116 :group 'org-agenda-windows
1117 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1118
1119 (defcustom org-agenda-restore-windows-after-quit nil
1120 "Non-nil means restore window configuration upon exiting agenda.
1121 Before the window configuration is changed for displaying the agenda,
1122 the current status is recorded. When the agenda is exited with
1123 `q' or `x' and this option is set, the old state is restored. If
1124 `org-agenda-window-setup' is `other-frame', the value of this
1125 option will be ignored."
1126 :group 'org-agenda-windows
1127 :type 'boolean)
1128
1129 (defcustom org-agenda-ndays nil
1130 "Number of days to include in overview display.
1131 Should be 1 or 7.
1132 Obsolete, see `org-agenda-span'."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const nil)
1135 (integer)))
1136
1137 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1138
1139 (defcustom org-agenda-span 'week
1140 "Number of days to include in overview display.
1141 Can be day, week, month, year, or any number of days.
1142 Custom commands can set this variable in the options section."
1143 :group 'org-agenda-daily/weekly
1144 :type '(choice (const :tag "Day" day)
1145 (const :tag "Week" week)
1146 (const :tag "Fortnight" fortnight)
1147 (const :tag "Month" month)
1148 (const :tag "Year" year)
1149 (integer :tag "Custom")))
1150
1151 (defcustom org-agenda-start-on-weekday 1
1152 "Non-nil means start the overview always on the specified weekday.
1153 0 denotes Sunday, 1 denotes Monday, etc.
1154 When nil, always start on the current day.
1155 Custom commands can set this variable in the options section."
1156 :group 'org-agenda-daily/weekly
1157 :type '(choice (const :tag "Today" nil)
1158 (integer :tag "Weekday No.")))
1159
1160 (defcustom org-agenda-show-all-dates t
1161 "Non-nil means `org-agenda' shows every day in the selected range.
1162 When nil, only the days which actually have entries are shown."
1163 :group 'org-agenda-daily/weekly
1164 :type 'boolean)
1165
1166 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1167 "Format string for displaying dates in the agenda.
1168 Used by the daily/weekly agenda and by the timeline. This should be
1169 a format string understood by `format-time-string', or a function returning
1170 the formatted date as a string. The function must take a single argument,
1171 a calendar-style date list like (month day year)."
1172 :group 'org-agenda-daily/weekly
1173 :type '(choice
1174 (string :tag "Format string")
1175 (function :tag "Function")))
1176
1177 (defun org-agenda-format-date-aligned (date)
1178 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1179 This function makes sure that dates are aligned for easy reading."
1180 (require 'cal-iso)
1181 (let* ((dayname (calendar-day-name date))
1182 (day (cadr date))
1183 (day-of-week (calendar-day-of-week date))
1184 (month (car date))
1185 (monthname (calendar-month-name month))
1186 (year (nth 2 date))
1187 (iso-week (org-days-to-iso-week
1188 (calendar-absolute-from-gregorian date)))
1189 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1190 (1- year))
1191 ((and (= month 12) (<= iso-week 1))
1192 (1+ year))
1193 (t year)))
1194 (weekstring (if (= day-of-week 1)
1195 (format " W%02d" iso-week)
1196 "")))
1197 (format "%-10s %2d %s %4d%s"
1198 dayname day monthname year weekstring)))
1199
1200 (defcustom org-agenda-time-leading-zero nil
1201 "Non-nil means use leading zero for military times in agenda.
1202 For example, 9:30am would become 09:30 rather than 9:30."
1203 :group 'org-agenda-daily/weekly
1204 :version "24.1"
1205 :type 'boolean)
1206
1207 (defcustom org-agenda-timegrid-use-ampm nil
1208 "When set, show AM/PM style timestamps on the timegrid."
1209 :group 'org-agenda
1210 :version "24.1"
1211 :type 'boolean)
1212
1213 (defun org-agenda-time-of-day-to-ampm (time)
1214 "Convert TIME of a string like ‘13:45’ to an AM/PM style time string."
1215 (let* ((hour-number (string-to-number (substring time 0 -3)))
1216 (minute (substring time -2))
1217 (ampm "am"))
1218 (cond
1219 ((equal hour-number 12)
1220 (setq ampm "pm"))
1221 ((> hour-number 12)
1222 (setq ampm "pm")
1223 (setq hour-number (- hour-number 12))))
1224 (concat
1225 (if org-agenda-time-leading-zero
1226 (format "%02d" hour-number)
1227 (format "%02s" (number-to-string hour-number)))
1228 ":" minute ampm)))
1229
1230 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1231 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1232 (if org-agenda-timegrid-use-ampm
1233 (org-agenda-time-of-day-to-ampm time)
1234 time))
1235
1236 (defcustom org-agenda-weekend-days '(6 0)
1237 "Which days are weekend?
1238 These days get the special face `org-agenda-date-weekend' in the agenda
1239 and timeline buffers."
1240 :group 'org-agenda-daily/weekly
1241 :type '(set :greedy t
1242 (const :tag "Monday" 1)
1243 (const :tag "Tuesday" 2)
1244 (const :tag "Wednesday" 3)
1245 (const :tag "Thursday" 4)
1246 (const :tag "Friday" 5)
1247 (const :tag "Saturday" 6)
1248 (const :tag "Sunday" 0)))
1249
1250 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1251 "Non-nil means jump to today when moving a past date forward in time.
1252 When using S-right in the agenda to move a a date forward, and the date
1253 stamp currently points to the past, the first key press will move it
1254 to today. WHen nil, just move one day forward even if the date stays
1255 in the past."
1256 :group 'org-agenda-daily/weekly
1257 :version "24.1"
1258 :type 'boolean)
1259
1260 (defcustom org-agenda-include-diary nil
1261 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :type 'boolean)
1265
1266 (defcustom org-agenda-include-deadlines t
1267 "If non-nil, include entries within their deadline warning period.
1268 Custom commands can set this variable in the options section."
1269 :group 'org-agenda-daily/weekly
1270 :version "24.1"
1271 :type 'boolean)
1272
1273 (defcustom org-agenda-repeating-timestamp-show-all t
1274 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1275 When set to a list of strings, only show occurrences of repeating
1276 stamps for these TODO keywords. When nil, only one occurrence is
1277 shown, either today or the nearest into the future."
1278 :group 'org-agenda-daily/weekly
1279 :type '(choice
1280 (const :tag "Show repeating stamps" t)
1281 (repeat :tag "Show repeating stamps for these TODO keywords"
1282 (string :tag "TODO Keyword"))
1283 (const :tag "Don't show repeating stamps" nil)))
1284
1285 (defcustom org-scheduled-past-days 10000
1286 "Number of days to continue listing scheduled items not marked DONE.
1287 When an item is scheduled on a date, it shows up in the agenda on this
1288 day and will be listed until it is marked done for the number of days
1289 given here."
1290 :group 'org-agenda-daily/weekly
1291 :type 'integer)
1292
1293 (defcustom org-agenda-log-mode-items '(closed clock)
1294 "List of items that should be shown in agenda log mode.
1295 This list may contain the following symbols:
1296
1297 closed Show entries that have been closed on that day.
1298 clock Show entries that have received clocked time on that day.
1299 state Show all logged state changes.
1300 Note that instead of changing this variable, you can also press `C-u l' in
1301 the agenda to display all available LOG items temporarily."
1302 :group 'org-agenda-daily/weekly
1303 :type '(set :greedy t (const closed) (const clock) (const state)))
1304
1305 (defcustom org-agenda-clock-consistency-checks
1306 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1307 :gap-ok-around ("4:00")
1308 :default-face ((:background "DarkRed") (:foreground "white"))
1309 :overlap-face nil :gap-face nil :no-end-time-face nil
1310 :long-face nil :short-face nil)
1311 "This is a property list, with the following keys:
1312
1313 :max-duration Mark clocking chunks that are longer than this time.
1314 This is a time string like \"HH:MM\", or the number
1315 of minutes as an integer.
1316
1317 :min-duration Mark clocking chunks that are shorter that this.
1318 This is a time string like \"HH:MM\", or the number
1319 of minutes as an integer.
1320
1321 :max-gap Mark gaps between clocking chunks that are longer than
1322 this duration. A number of minutes, or a string
1323 like \"HH:MM\".
1324
1325 :gap-ok-around List of times during the day which are usually not working
1326 times. When a gap is detected, but the gap contains any
1327 of these times, the gap is *not* reported. For example,
1328 if this is (\"4:00\" \"13:00\") then gaps that contain
1329 4:00 in the morning (i.e. the night) and 13:00
1330 (i.e. a typical lunch time) do not cause a warning.
1331 You should have at least one time during the night in this
1332 list, or otherwise the first task each morning will trigger
1333 a warning because it follows a long gap.
1334
1335 Furthermore, the following properties can be used to define faces for
1336 issue display.
1337
1338 :default-face the default face, if the specific face is undefined
1339 :overlap-face face for overlapping clocks
1340 :gap-face face for gaps between clocks
1341 :no-end-time-face face for incomplete clocks
1342 :long-face face for clock intervals that are too long
1343 :short-face face for clock intervals that are too short"
1344 :group 'org-agenda-daily/weekly
1345 :group 'org-clock
1346 :version "24.1"
1347 :type 'plist)
1348
1349 (defcustom org-agenda-log-mode-add-notes t
1350 "Non-nil means add first line of notes to log entries in agenda views.
1351 If a log item like a state change or a clock entry is associated with
1352 notes, the first line of these notes will be added to the entry in the
1353 agenda display."
1354 :group 'org-agenda-daily/weekly
1355 :type 'boolean)
1356
1357 (defcustom org-agenda-start-with-log-mode nil
1358 "The initial value of log-mode in a newly created agenda window.
1359 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1360 explanations on the possible values."
1361 :group 'org-agenda-startup
1362 :group 'org-agenda-daily/weekly
1363 :type '(choice (const :tag "Don't show log items" nil)
1364 (const :tag "Show only log items" only)
1365 (const :tag "Show all possible log items" clockcheck)
1366 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1367 (choice (const :tag "Show closed log items" closed)
1368 (const :tag "Show clocked log items" clock)
1369 (const :tag "Show all logged state changes" state)))))
1370
1371 (defcustom org-agenda-start-with-clockreport-mode nil
1372 "The initial value of clockreport-mode in a newly created agenda window."
1373 :group 'org-agenda-startup
1374 :group 'org-agenda-daily/weekly
1375 :type 'boolean)
1376
1377 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1378 "Property list with parameters for the clocktable in clockreport mode.
1379 This is the display mode that shows a clock table in the daily/weekly
1380 agenda, the properties for this dynamic block can be set here.
1381 The usual clocktable parameters are allowed here, but you cannot set
1382 the properties :name, :tstart, :tend, :block, and :scope - these will
1383 be overwritten to make sure the content accurately reflects the
1384 current display in the agenda."
1385 :group 'org-agenda-daily/weekly
1386 :type 'plist)
1387
1388 (defcustom org-agenda-search-view-always-boolean nil
1389 "Non-nil means the search string is interpreted as individual parts.
1390
1391 The search string for search view can either be interpreted as a phrase,
1392 or as a list of snippets that define a boolean search for a number of
1393 strings.
1394
1395 When this is non-nil, the string will be split on whitespace, and each
1396 snippet will be searched individually, and all must match in order to
1397 select an entry. A snippet is then a single string of non-white
1398 characters, or a string in double quotes, or a regexp in {} braces.
1399 If a snippet is preceded by \"-\", the snippet must *not* match.
1400 \"+\" is syntactic sugar for positive selection. Each snippet may
1401 be found as a full word or a partial word, but see the variable
1402 `org-agenda-search-view-force-full-words'.
1403
1404 When this is nil, search will look for the entire search phrase as one,
1405 with each space character matching any amount of whitespace, including
1406 line breaks.
1407
1408 Even when this is nil, you can still switch to Boolean search dynamically
1409 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1410 is a regexp marked with braces like \"{abc}\", this will also switch to
1411 boolean search."
1412 :group 'org-agenda-search-view
1413 :version "24.1"
1414 :type 'boolean)
1415
1416 (org-defvaralias 'org-agenda-search-view-search-words-only
1417 'org-agenda-search-view-always-boolean)
1418
1419 (defcustom org-agenda-search-view-force-full-words nil
1420 "Non-nil means, search words must be matches as complete words.
1421 When nil, they may also match part of a word."
1422 :group 'org-agenda-search-view
1423 :version "24.1"
1424 :type 'boolean)
1425
1426 (defcustom org-agenda-search-view-max-outline-level 0
1427 "Maximum outline level to display in search view.
1428 E.g. when this is set to 1, the search view will only
1429 show headlines of level 1. When set to 0, the default
1430 value, don't limit agenda view by outline level."
1431 :group 'org-agenda-search-view
1432 :version "24.4"
1433 :package-version '(Org . "8.3")
1434 :type 'integer)
1435
1436 (defgroup org-agenda-time-grid nil
1437 "Options concerning the time grid in the Org-mode Agenda."
1438 :tag "Org Agenda Time Grid"
1439 :group 'org-agenda)
1440
1441 (defcustom org-agenda-search-headline-for-time t
1442 "Non-nil means search headline for a time-of-day.
1443 If the headline contains a time-of-day in one format or another, it will
1444 be used to sort the entry into the time sequence of items for a day.
1445 Some people have time stamps in the headline that refer to the creation
1446 time or so, and then this produces an unwanted side effect. If this is
1447 the case for your, use this variable to turn off searching the headline
1448 for a time."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1451
1452 (defcustom org-agenda-use-time-grid t
1453 "Non-nil means show a time grid in the agenda schedule.
1454 A time grid is a set of lines for specific times (like every two hours between
1455 8:00 and 20:00). The items scheduled for a day at specific times are
1456 sorted in between these lines.
1457 For details about when the grid will be shown, and what it will look like, see
1458 the variable `org-agenda-time-grid'."
1459 :group 'org-agenda-time-grid
1460 :type 'boolean)
1461
1462 (defcustom org-agenda-time-grid
1463 '((daily today require-timed)
1464 "----------------"
1465 (800 1000 1200 1400 1600 1800 2000))
1466
1467 "The settings for time grid for agenda display.
1468 This is a list of three items. The first item is again a list. It contains
1469 symbols specifying conditions when the grid should be displayed:
1470
1471 daily if the agenda shows a single day
1472 weekly if the agenda shows an entire week
1473 today show grid on current date, independent of daily/weekly display
1474 require-timed show grid only if at least one item has a time specification
1475 remove-match skip grid times already present in an entry
1476
1477 The second item is a string which will be placed behind the grid time.
1478
1479 The third item is a list of integers, indicating the times that should have
1480 a grid line."
1481 :group 'org-agenda-time-grid
1482 :type
1483 '(list
1484 (set :greedy t :tag "Grid Display Options"
1485 (const :tag "Show grid in single day agenda display" daily)
1486 (const :tag "Show grid in weekly agenda display" weekly)
1487 (const :tag "Always show grid for today" today)
1488 (const :tag "Show grid only if any timed entries are present"
1489 require-timed)
1490 (const :tag "Skip grid times already present in an entry"
1491 remove-match))
1492 (string :tag "Grid String")
1493 (repeat :tag "Grid Times" (integer :tag "Time"))))
1494
1495 (defcustom org-agenda-show-current-time-in-grid t
1496 "Non-nil means show the current time in the time grid."
1497 :group 'org-agenda-time-grid
1498 :version "24.1"
1499 :type 'boolean)
1500
1501 (defcustom org-agenda-current-time-string
1502 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1503 "The string for the current time marker in the agenda."
1504 :group 'org-agenda-time-grid
1505 :version "24.1"
1506 :type 'string)
1507
1508 (defgroup org-agenda-sorting nil
1509 "Options concerning sorting in the Org-mode Agenda."
1510 :tag "Org Agenda Sorting"
1511 :group 'org-agenda)
1512
1513 (defcustom org-agenda-sorting-strategy
1514 '((agenda habit-down time-up priority-down category-keep)
1515 (todo priority-down category-keep)
1516 (tags priority-down category-keep)
1517 (search category-keep))
1518 "Sorting structure for the agenda items of a single day.
1519 This is a list of symbols which will be used in sequence to determine
1520 if an entry should be listed before another entry. The following
1521 symbols are recognized:
1522
1523 time-up Put entries with time-of-day indications first, early first
1524 time-down Put entries with time-of-day indications first, late first
1525 timestamp-up Sort by any timestamp, early first
1526 timestamp-down Sort by any timestamp, late first
1527 scheduled-up Sort by scheduled timestamp, early first
1528 scheduled-down Sort by scheduled timestamp, late first
1529 deadline-up Sort by deadline timestamp, early first
1530 deadline-down Sort by deadline timestamp, late first
1531 ts-up Sort by active timestamp, early first
1532 ts-down Sort by active timestamp, late first
1533 tsia-up Sort by inactive timestamp, early first
1534 tsia-down Sort by inactive timestamp, late first
1535 category-keep Keep the default order of categories, corresponding to the
1536 sequence in `org-agenda-files'.
1537 category-up Sort alphabetically by category, A-Z.
1538 category-down Sort alphabetically by category, Z-A.
1539 tag-up Sort alphabetically by last tag, A-Z.
1540 tag-down Sort alphabetically by last tag, Z-A.
1541 priority-up Sort numerically by priority, high priority last.
1542 priority-down Sort numerically by priority, high priority first.
1543 todo-state-up Sort by todo state, tasks that are done last.
1544 todo-state-down Sort by todo state, tasks that are done first.
1545 effort-up Sort numerically by estimated effort, high effort last.
1546 effort-down Sort numerically by estimated effort, high effort first.
1547 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1548 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1549 habit-up Put entries that are habits first
1550 habit-down Put entries that are habits last
1551 alpha-up Sort headlines alphabetically
1552 alpha-down Sort headlines alphabetically, reversed
1553
1554 The different possibilities will be tried in sequence, and testing stops
1555 if one comparison returns a \"not-equal\". For example, the default
1556 '(time-up category-keep priority-down)
1557 means: Pull out all entries having a specified time of day and sort them,
1558 in order to make a time schedule for the current day the first thing in the
1559 agenda listing for the day. Of the entries without a time indication, keep
1560 the grouped in categories, don't sort the categories, but keep them in
1561 the sequence given in `org-agenda-files'. Within each category sort by
1562 priority.
1563
1564 Leaving out `category-keep' would mean that items will be sorted across
1565 categories by priority.
1566
1567 Instead of a single list, this can also be a set of list for specific
1568 contents, with a context symbol in the car of the list, any of
1569 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1570
1571 Custom commands can bind this variable in the options section."
1572 :group 'org-agenda-sorting
1573 :type `(choice
1574 (repeat :tag "General" ,org-sorting-choice)
1575 (list :tag "Individually"
1576 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1577 (repeat ,org-sorting-choice))
1578 (cons (const :tag "Strategy for TODO lists" todo)
1579 (repeat ,org-sorting-choice))
1580 (cons (const :tag "Strategy for Tags matches" tags)
1581 (repeat ,org-sorting-choice))
1582 (cons (const :tag "Strategy for search matches" search)
1583 (repeat ,org-sorting-choice)))))
1584
1585 (defcustom org-agenda-cmp-user-defined nil
1586 "A function to define the comparison `user-defined'.
1587 This function must receive two arguments, agenda entry a and b.
1588 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1589 the user comparison, return nil.
1590 When this is defined, you can make `user-defined-up' and `user-defined-down'
1591 part of an agenda sorting strategy."
1592 :group 'org-agenda-sorting
1593 :type 'symbol)
1594
1595 (defcustom org-sort-agenda-notime-is-late t
1596 "Non-nil means items without time are considered late.
1597 This is only relevant for sorting. When t, items which have no explicit
1598 time like 15:30 will be considered as 99:01, i.e. later than any items which
1599 do have a time. When nil, the default time is before 0:00. You can use this
1600 option to decide if the schedule for today should come before or after timeless
1601 agenda entries."
1602 :group 'org-agenda-sorting
1603 :type 'boolean)
1604
1605 (defcustom org-sort-agenda-noeffort-is-high t
1606 "Non-nil means items without effort estimate are sorted as high effort.
1607 This also applies when filtering an agenda view with respect to the
1608 < or > effort operator. Then, tasks with no effort defined will be treated
1609 as tasks with high effort.
1610 When nil, such items are sorted as 0 minutes effort."
1611 :group 'org-agenda-sorting
1612 :type 'boolean)
1613
1614 (defgroup org-agenda-line-format nil
1615 "Options concerning the entry prefix in the Org-mode agenda display."
1616 :tag "Org Agenda Line Format"
1617 :group 'org-agenda)
1618
1619 (defcustom org-agenda-prefix-format
1620 '((agenda . " %i %-12:c%?-12t% s")
1621 (timeline . " % s")
1622 (todo . " %i %-12:c")
1623 (tags . " %i %-12:c")
1624 (search . " %i %-12:c"))
1625 "Format specifications for the prefix of items in the agenda views.
1626 An alist with five entries, each for the different agenda types. The
1627 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1628 The values are format strings.
1629
1630 This format works similar to a printf format, with the following meaning:
1631
1632 %c the category of the item, \"Diary\" for entries from the diary,
1633 or as given by the CATEGORY keyword or derived from the file name
1634 %e the effort required by the item
1635 %l the level of the item (insert X space(s) if item is of level X)
1636 %i the icon category of the item, see `org-agenda-category-icon-alist'
1637 %T the last tag of the item (ignore inherited tags, which come first)
1638 %t the HH:MM time-of-day specification if one applies to the entry
1639 %s Scheduling/Deadline information, a short string
1640 %b show breadcrumbs, i.e., the names of the higher levels
1641 %(expression) Eval EXPRESSION and replace the control string
1642 by the result
1643
1644 All specifiers work basically like the standard `%s' of printf, but may
1645 contain two additional characters: a question mark just after the `%'
1646 and a whitespace/punctuation character just before the final letter.
1647
1648 If the first character after `%' is a question mark, the entire field
1649 will only be included if the corresponding value applies to the current
1650 entry. This is useful for fields which should have fixed width when
1651 present, but zero width when absent. For example, \"%?-12t\" will
1652 result in a 12 character time field if a time of the day is specified,
1653 but will completely disappear in entries which do not contain a time.
1654
1655 If there is punctuation or whitespace character just before the
1656 final format letter, this character will be appended to the field
1657 value if the value is not empty. For example, the format
1658 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1659 the category is empty, no additional colon is inserted.
1660
1661 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1662 which means:
1663
1664 - Indent the line with two space characters
1665 - Give the category a 12 chars wide field, padded with whitespace on
1666 the right (because of `-'). Append a colon if there is a category
1667 (because of `:').
1668 - If there is a time-of-day, put it into a 12 chars wide field. If no
1669 time, don't put in an empty field, just skip it (because of '?').
1670 - Finally, put the scheduling information.
1671
1672 See also the variables `org-agenda-remove-times-when-in-prefix' and
1673 `org-agenda-remove-tags'.
1674
1675 Custom commands can set this variable in the options section."
1676 :type '(choice
1677 (string :tag "General format")
1678 (list :greedy t :tag "View dependent"
1679 (cons (const agenda) (string :tag "Format"))
1680 (cons (const timeline) (string :tag "Format"))
1681 (cons (const todo) (string :tag "Format"))
1682 (cons (const tags) (string :tag "Format"))
1683 (cons (const search) (string :tag "Format"))))
1684 :group 'org-agenda-line-format)
1685
1686 (defvar org-prefix-format-compiled nil
1687 "The compiled prefix format and associated variables.
1688 This is a list where first element is a list of variable bindings, and second
1689 element is the compiled format expression. See the variable
1690 `org-agenda-prefix-format'.")
1691
1692 (defcustom org-agenda-todo-keyword-format "%-1s"
1693 "Format for the TODO keyword in agenda lines.
1694 Set this to something like \"%-12s\" if you want all TODO keywords
1695 to occupy a fixed space in the agenda display."
1696 :group 'org-agenda-line-format
1697 :type 'string)
1698
1699 (defcustom org-agenda-diary-sexp-prefix nil
1700 "A regexp that matches part of a diary sexp entry
1701 which should be treated as scheduling/deadline information in
1702 `org-agenda'.
1703
1704 For example, you can use this to extract the `diary-remind-message' from
1705 `diary-remind' entries."
1706 :group 'org-agenda-line-format
1707 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1708
1709 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1710 "Text preceding timerange entries in the agenda view.
1711 This is a list with two strings. The first applies when the range
1712 is entirely on one day. The second applies if the range spans several days.
1713 The strings may have two \"%d\" format specifiers which will be filled
1714 with the sequence number of the days, and the total number of days in the
1715 range, respectively."
1716 :group 'org-agenda-line-format
1717 :type '(list
1718 (string :tag "Deadline today ")
1719 (choice :tag "Deadline relative"
1720 (string :tag "Format string")
1721 (function))))
1722
1723 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1724 "Text preceding scheduled items in the agenda view.
1725 This is a list with two strings. The first applies when the item is
1726 scheduled on the current day. The second applies when it has been scheduled
1727 previously, it may contain a %d indicating that this is the nth time that
1728 this item is scheduled, due to automatic rescheduling of unfinished items
1729 for the following day. So this number is one larger than the number of days
1730 that passed since this item was scheduled first."
1731 :group 'org-agenda-line-format
1732 :version "24.4"
1733 :package-version '(Org . "8.0")
1734 :type '(list
1735 (string :tag "Scheduled today ")
1736 (string :tag "Scheduled previously")))
1737
1738 (defcustom org-agenda-inactive-leader "["
1739 "Text preceding item pulled into the agenda by inactive time stamps.
1740 These entries are added to the agenda when pressing \"[\"."
1741 :group 'org-agenda-line-format
1742 :version "24.1"
1743 :type 'string)
1744
1745 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1746 "Text preceding deadline items in the agenda view.
1747 This is a list with three strings. The first applies when the item has its
1748 deadline on the current day. The second applies when the deadline is in the
1749 future, the third one when it is in the past. The strings may contain %d
1750 to capture the number of days."
1751 :group 'org-agenda-line-format
1752 :version "24.4"
1753 :package-version '(Org . "8.0")
1754 :type '(list
1755 (string :tag "Deadline today ")
1756 (string :tag "Deadline in the future ")
1757 (string :tag "Deadline in the past ")))
1758
1759 (defcustom org-agenda-remove-times-when-in-prefix t
1760 "Non-nil means remove duplicate time specifications in agenda items.
1761 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1762 time-of-day specification in a headline or diary entry is extracted and
1763 placed into the prefix. If this option is non-nil, the original specification
1764 \(a timestamp or -range, or just a plain time(range) specification like
1765 11:30-4pm) will be removed for agenda display. This makes the agenda less
1766 cluttered.
1767 The option can be t or nil. It may also be the symbol `beg', indicating
1768 that the time should only be removed when it is located at the beginning of
1769 the headline/diary entry."
1770 :group 'org-agenda-line-format
1771 :type '(choice
1772 (const :tag "Always" t)
1773 (const :tag "Never" nil)
1774 (const :tag "When at beginning of entry" beg)))
1775
1776 (defcustom org-agenda-remove-timeranges-from-blocks nil
1777 "Non-nil means remove time ranges specifications in agenda
1778 items that span on several days."
1779 :group 'org-agenda-line-format
1780 :version "24.1"
1781 :type 'boolean)
1782
1783 (defcustom org-agenda-default-appointment-duration nil
1784 "Default duration for appointments that only have a starting time.
1785 When nil, no duration is specified in such cases.
1786 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1787 :group 'org-agenda-line-format
1788 :type '(choice
1789 (integer :tag "Minutes")
1790 (const :tag "No default duration")))
1791
1792 (defcustom org-agenda-show-inherited-tags t
1793 "Non-nil means show inherited tags in each agenda line.
1794
1795 When this option is set to 'always, it take precedences over
1796 `org-agenda-use-tag-inheritance' and inherited tags are shown
1797 in every agenda.
1798
1799 When this option is set to t (the default), inherited tags are
1800 shown when they are available, i.e. when the value of
1801 `org-agenda-use-tag-inheritance' has been taken into account.
1802
1803 This can be set to a list of agenda types in which the agenda
1804 must display the inherited tags. Available types are 'todo,
1805 'agenda, 'search and 'timeline.
1806
1807 When set to nil, never show inherited tags in agenda lines."
1808 :group 'org-agenda-line-format
1809 :group 'org-agenda
1810 :version "24.3"
1811 :type '(choice
1812 (const :tag "Show inherited tags when available" t)
1813 (const :tag "Always show inherited tags" always)
1814 (repeat :tag "Show inherited tags only in selected agenda types"
1815 (symbol :tag "Agenda type"))))
1816
1817 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1818 "List of agenda view types where to use tag inheritance.
1819
1820 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1821 controlled by `org-use-tag-inheritance'. In other agenda types,
1822 `org-use-tag-inheritance' is not used for the selection of the
1823 agenda entries. Still, you may want the agenda to be aware of
1824 the inherited tags anyway, e.g. for later tag filtering.
1825
1826 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1827
1828 This variable has no effect if `org-agenda-show-inherited-tags'
1829 is set to 'always. In that case, the agenda is aware of those
1830 tags.
1831
1832 The default value sets tags in every agenda type. Setting this
1833 option to nil will speed up non-tags agenda view a lot."
1834 :group 'org-agenda
1835 :version "24.3"
1836 :type '(choice
1837 (const :tag "Use tag inheritance in all agenda types" t)
1838 (repeat :tag "Use tag inheritance in selected agenda types"
1839 (symbol :tag "Agenda type"))))
1840
1841 (defcustom org-agenda-hide-tags-regexp nil
1842 "Regular expression used to filter away specific tags in agenda views.
1843 This means that these tags will be present, but not be shown in the agenda
1844 line. Secondary filtering will still work on the hidden tags.
1845 Nil means don't hide any tags."
1846 :group 'org-agenda-line-format
1847 :type '(choice
1848 (const :tag "Hide none" nil)
1849 (string :tag "Regexp ")))
1850
1851 (defcustom org-agenda-remove-tags nil
1852 "Non-nil means remove the tags from the headline copy in the agenda.
1853 When this is the symbol `prefix', only remove tags when
1854 `org-agenda-prefix-format' contains a `%T' specifier."
1855 :group 'org-agenda-line-format
1856 :type '(choice
1857 (const :tag "Always" t)
1858 (const :tag "Never" nil)
1859 (const :tag "When prefix format contains %T" prefix)))
1860
1861 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1862 'org-agenda-remove-tags)
1863
1864 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1865 "Shift tags in agenda items to this column.
1866 If this number is positive, it specifies the column. If it is negative,
1867 it means that the tags should be flushright to that column. For example,
1868 -80 works well for a normal 80 character screen."
1869 :group 'org-agenda-line-format
1870 :type 'integer)
1871
1872 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1873
1874 (defcustom org-agenda-fontify-priorities 'cookies
1875 "Non-nil means highlight low and high priorities in agenda.
1876 When t, the highest priority entries are bold, lowest priority italic.
1877 However, settings in `org-priority-faces' will overrule these faces.
1878 When this variable is the symbol `cookies', only fontify the
1879 cookies, not the entire task.
1880 This may also be an association list of priority faces, whose
1881 keys are the character values of `org-highest-priority',
1882 `org-default-priority', and `org-lowest-priority' (the default values
1883 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1884 color as a string, or a list like `(:background \"Red\")'.
1885 If it is a color, the variable `org-faces-easy-properties'
1886 determines if it is a foreground or a background color."
1887 :group 'org-agenda-line-format
1888 :type '(choice
1889 (const :tag "Never" nil)
1890 (const :tag "Defaults" t)
1891 (const :tag "Cookies only" cookies)
1892 (repeat :tag "Specify"
1893 (list (character :tag "Priority" :value ?A)
1894 (choice :tag "Face "
1895 (string :tag "Color")
1896 (sexp :tag "Face"))))))
1897
1898 (defcustom org-agenda-day-face-function nil
1899 "Function called to determine what face should be used to display a day.
1900 The only argument passed to that function is the day. It should
1901 returns a face, or nil if does not want to specify a face and let
1902 the normal rules apply."
1903 :group 'org-agenda-line-format
1904 :version "24.1"
1905 :type '(choice (const nil) (function)))
1906
1907 (defcustom org-agenda-category-icon-alist nil
1908 "Alist of category icon to be displayed in agenda views.
1909
1910 Each entry should have the following format:
1911
1912 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1913
1914 Where CATEGORY-REGEXP is a regexp matching the categories where
1915 the icon should be displayed.
1916 FILE-OR-DATA either a file path or a string containing image data.
1917
1918 The other fields can be omitted safely if not needed:
1919 TYPE indicates the image type.
1920 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1921 image data.
1922 PROPS are additional image attributes to assign to the image,
1923 like, e.g. `:ascent center'.
1924
1925 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1926
1927 If you want to set the display properties yourself, just put a
1928 list as second element:
1929
1930 (CATEGORY-REGEXP (MY PROPERTY LIST))
1931
1932 For example, to display a 16px horizontal space for Emacs
1933 category, you can use:
1934
1935 (\"Emacs\" '(space . (:width (16))))"
1936 :group 'org-agenda-line-format
1937 :version "24.1"
1938 :type '(alist :key-type (string :tag "Regexp matching category")
1939 :value-type (choice (list :tag "Icon"
1940 (string :tag "File or data")
1941 (symbol :tag "Type")
1942 (boolean :tag "Data?")
1943 (repeat :tag "Extra image properties" :inline t symbol))
1944 (list :tag "Display properties" sexp))))
1945
1946 (defgroup org-agenda-column-view nil
1947 "Options concerning column view in the agenda."
1948 :tag "Org Agenda Column View"
1949 :group 'org-agenda)
1950
1951 (defcustom org-agenda-columns-show-summaries t
1952 "Non-nil means show summaries for columns displayed in the agenda view."
1953 :group 'org-agenda-column-view
1954 :type 'boolean)
1955
1956 (defcustom org-agenda-columns-compute-summary-properties t
1957 "Non-nil means recompute all summary properties before column view.
1958 When column view in the agenda is listing properties that have a summary
1959 operator, it can go to all relevant buffers and recompute the summaries
1960 there. This can mean overhead for the agenda column view, but is necessary
1961 to have thing up to date.
1962 As a special case, a CLOCKSUM property also makes sure that the clock
1963 computations are current."
1964 :group 'org-agenda-column-view
1965 :type 'boolean)
1966
1967 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1968 "Non-nil means the duration of an appointment will add to day effort.
1969 The property to which appointment durations will be added is the one given
1970 in the option `org-effort-property'. If an appointment does not have
1971 an end time, `org-agenda-default-appointment-duration' will be used. If that
1972 is not set, an appointment without end time will not contribute to the time
1973 estimate."
1974 :group 'org-agenda-column-view
1975 :type 'boolean)
1976
1977 (defcustom org-agenda-auto-exclude-function nil
1978 "A function called with a tag to decide if it is filtered on ‘/ RET’.
1979 The sole argument to the function, which is called once for each
1980 possible tag, is a string giving the name of the tag. The
1981 function should return either nil if the tag should be included
1982 as normal, or \"-<TAG>\" to exclude the tag.
1983 Note that for the purpose of tag filtering, only the lower-case version of
1984 all tags will be considered, so that this function will only ever see
1985 the lower-case version of all tags."
1986 :group 'org-agenda
1987 :type '(choice (const nil) (function)))
1988
1989 (defcustom org-agenda-bulk-custom-functions nil
1990 "Alist of characters and custom functions for bulk actions.
1991 For example, this value makes those two functions available:
1992
1993 '((?R set-category)
1994 (?C bulk-cut))
1995
1996 With selected entries in an agenda buffer, `B R' will call
1997 the custom function `set-category' on the selected entries.
1998 Note that functions in this alist don't need to be quoted."
1999 :type 'alist
2000 :version "24.1"
2001 :group 'org-agenda)
2002
2003 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2004 "Execute BODY with point at location given by `org-hd-marker' property.
2005 If STRING is non-nil, the text property will be fetched from position 0
2006 in that string. If STRING is nil, it will be fetched from the beginning
2007 of the current line."
2008 (org-with-gensyms (marker)
2009 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2010 'org-hd-marker ,string)))
2011 (with-current-buffer (marker-buffer ,marker)
2012 (save-excursion
2013 (goto-char ,marker)
2014 ,@body)))))
2015 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2016
2017 (defun org-add-agenda-custom-command (entry)
2018 "Replace or add a command in `org-agenda-custom-commands'.
2019 This is mostly for hacking and trying a new command - once the command
2020 works you probably want to add it to `org-agenda-custom-commands' for good."
2021 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2022 (if ass
2023 (setcdr ass (cdr entry))
2024 (push entry org-agenda-custom-commands))))
2025
2026 ;;; Define the org-agenda-mode
2027
2028 (defvar org-agenda-mode-map (make-sparse-keymap)
2029 "Keymap for `org-agenda-mode'.")
2030 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2031
2032 (defvar org-agenda-menu) ; defined later in this file.
2033 (defvar org-agenda-restrict nil) ; defined later in this file.
2034 (defvar org-agenda-follow-mode nil)
2035 (defvar org-agenda-entry-text-mode nil)
2036 (defvar org-agenda-clockreport-mode nil)
2037 (defvar org-agenda-show-log nil)
2038 (defvar org-agenda-redo-command nil)
2039 (defvar org-agenda-query-string nil)
2040 (defvar org-agenda-mode-hook nil
2041 "Hook run after `org-agenda-mode' is turned on.
2042 The buffer is still writable when this hook is called.")
2043 (defvar org-agenda-type nil)
2044 (defvar org-agenda-force-single-file nil)
2045 (defvar org-agenda-bulk-marked-entries nil
2046 "List of markers that refer to marked entries in the agenda.")
2047
2048 ;;; Multiple agenda buffers support
2049
2050 (defcustom org-agenda-sticky nil
2051 "Non-nil means agenda q key will bury agenda buffers.
2052 Agenda commands will then show existing buffer instead of generating new ones.
2053 When nil, `q' will kill the single agenda buffer."
2054 :group 'org-agenda
2055 :version "24.3"
2056 :type 'boolean)
2057
2058 \f
2059 ;;;###autoload
2060 (defun org-toggle-sticky-agenda (&optional arg)
2061 "Toggle `org-agenda-sticky'."
2062 (interactive "P")
2063 (let ((new-value (if arg
2064 (> (prefix-numeric-value arg) 0)
2065 (not org-agenda-sticky))))
2066 (if (equal new-value org-agenda-sticky)
2067 (and (org-called-interactively-p 'interactive)
2068 (message "Sticky agenda was already %s"
2069 (if org-agenda-sticky "enabled" "disabled")))
2070 (setq org-agenda-sticky new-value)
2071 (org-agenda-kill-all-agenda-buffers)
2072 (and (org-called-interactively-p 'interactive)
2073 (message "Sticky agenda was %s"
2074 (if org-agenda-sticky "enabled" "disabled"))))))
2075
2076 (defvar org-agenda-buffer nil
2077 "Agenda buffer currently being generated.")
2078
2079 (defvar org-agenda-last-prefix-arg nil)
2080 (defvar org-agenda-this-buffer-name nil)
2081 (defvar org-agenda-doing-sticky-redo nil)
2082 (defvar org-agenda-this-buffer-is-sticky nil)
2083
2084 (defconst org-agenda-local-vars
2085 '(org-agenda-this-buffer-name
2086 org-agenda-undo-list
2087 org-agenda-pending-undo-list
2088 org-agenda-follow-mode
2089 org-agenda-entry-text-mode
2090 org-agenda-clockreport-mode
2091 org-agenda-show-log
2092 org-agenda-redo-command
2093 org-agenda-query-string
2094 org-agenda-type
2095 org-agenda-bulk-marked-entries
2096 org-agenda-undo-has-started-in
2097 org-agenda-info
2098 org-agenda-pre-window-conf
2099 org-agenda-columns-active
2100 org-agenda-tag-filter
2101 org-agenda-category-filter
2102 org-agenda-top-headline-filter
2103 org-agenda-regexp-filter
2104 org-agenda-markers
2105 org-agenda-last-search-view-search-was-boolean
2106 org-agenda-filtered-by-category
2107 org-agenda-filter-form
2108 org-agenda-cycle-counter
2109 org-agenda-last-prefix-arg)
2110 "Variables that must be local in agenda buffers to allow multiple buffers.")
2111
2112 (defun org-agenda-mode ()
2113 "Mode for time-sorted view on action items in Org-mode files.
2114
2115 The following commands are available:
2116
2117 \\{org-agenda-mode-map}"
2118 (interactive)
2119 (cond (org-agenda-doing-sticky-redo
2120 ;; Refreshing sticky agenda-buffer
2121 ;;
2122 ;; Preserve the value of `org-agenda-local-vars' variables,
2123 ;; while letting `kill-all-local-variables' kill the rest
2124 (let ((save (buffer-local-variables)))
2125 (kill-all-local-variables)
2126 (mapc 'make-local-variable org-agenda-local-vars)
2127 (dolist (elem save)
2128 (let ((var (car elem))
2129 (val (cdr elem)))
2130 (when (and val
2131 (member var org-agenda-local-vars))
2132 (set var val)))))
2133 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 (org-agenda-sticky
2135 ;; Creating a sticky Agenda buffer for the first time
2136 (kill-all-local-variables)
2137 (mapc 'make-local-variable org-agenda-local-vars)
2138 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2139 (t
2140 ;; Creating a non-sticky agenda buffer
2141 (kill-all-local-variables)
2142 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2143 (setq org-agenda-undo-list nil
2144 org-agenda-pending-undo-list nil
2145 org-agenda-bulk-marked-entries nil)
2146 (setq major-mode 'org-agenda-mode)
2147 ;; Keep global-font-lock-mode from turning on font-lock-mode
2148 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2149 (setq mode-name "Org-Agenda")
2150 (setq indent-tabs-mode nil)
2151 (use-local-map org-agenda-mode-map)
2152 (easy-menu-add org-agenda-menu)
2153 (if org-startup-truncated (setq truncate-lines t))
2154 (org-set-local 'line-move-visual nil)
2155 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2156 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2157 ;; Make sure properties are removed when copying text
2158 (org-add-hook 'filter-buffer-substring-functions
2159 (lambda (fun start end delete)
2160 (substring-no-properties (funcall fun start end delete)))
2161 nil t)
2162 (unless org-agenda-keep-modes
2163 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2164 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2165 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2166 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2167 (add-to-invisibility-spec '(org-filtered))
2168 (add-to-invisibility-spec '(org-link))
2169 (easy-menu-change
2170 '("Agenda") "Agenda Files"
2171 (append
2172 (list
2173 (vector
2174 (if (get 'org-agenda-files 'org-restrict)
2175 "Restricted to single file"
2176 "Edit File List")
2177 '(org-edit-agenda-file-list)
2178 (not (get 'org-agenda-files 'org-restrict)))
2179 "--")
2180 (mapcar 'org-file-menu-entry (org-agenda-files))))
2181 (org-agenda-set-mode-name)
2182 (apply
2183 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2184 (list 'org-agenda-mode-hook)))
2185
2186 (substitute-key-definition 'undo 'org-agenda-undo
2187 org-agenda-mode-map global-map)
2188 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2189 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2190 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2191 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2192 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2193 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2194 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2195 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2196 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2197 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2198 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2199 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2200 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2201 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2202 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2203 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2204 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2205 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2206 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2207 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2208 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2209 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2210 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2211 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2212 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2213 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2214 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2215 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2216 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2217 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2218 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2219 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2220 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2221 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2222 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2223 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2224 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2225 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2226 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2227 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2228 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2229 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2230 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2231 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2232 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2233 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2234 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2235 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2236 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2237 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2238
2239 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2240 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2241 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2242 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2243 (while l (org-defkey org-agenda-mode-map
2244 (int-to-string (pop l)) 'digit-argument)))
2245
2246 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2247 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2248 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2249 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2250 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2251 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2252 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2253 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2254 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2255 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2256 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2257 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2258 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2259 'org-clock-modify-effort-estimate)
2260 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2261 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2262 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2263 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2264 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2265 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2266 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2267 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2268 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2269 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2270 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2271 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2272 (substitute-key-definition 'next-line 'org-agenda-next-line
2273 org-agenda-mode-map global-map)
2274 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2275 org-agenda-mode-map global-map)
2276 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2277 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2278 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2279 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2280 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2281 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2282 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2283 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2284 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2285 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2286 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2287 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2289 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2291 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2293 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2295 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2296 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2297 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2298 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2299 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2300 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2301 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2302 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2303 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2304 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2305 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2306
2307 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2308 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2309 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2310 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2311 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2312 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2313 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2314 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2315 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2316 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2317 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2318 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2319 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2320 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2321 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2322
2323 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2324 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2325 (when org-agenda-mouse-1-follows-link
2326 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2327 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2328 '("Agenda"
2329 ("Agenda Files")
2330 "--"
2331 ("Agenda Dates"
2332 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2333 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2334 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2335 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2336 "--"
2337 ("View"
2338 ["Day View" org-agenda-day-view
2339 :active (org-agenda-check-type nil 'agenda)
2340 :style radio :selected (eq org-agenda-current-span 'day)
2341 :keys "v d (or just d)"]
2342 ["Week View" org-agenda-week-view
2343 :active (org-agenda-check-type nil 'agenda)
2344 :style radio :selected (eq org-agenda-current-span 'week)
2345 :keys "v w"]
2346 ["Fortnight View" org-agenda-fortnight-view
2347 :active (org-agenda-check-type nil 'agenda)
2348 :style radio :selected (eq org-agenda-current-span 'fortnight)
2349 :keys "v f"]
2350 ["Month View" org-agenda-month-view
2351 :active (org-agenda-check-type nil 'agenda)
2352 :style radio :selected (eq org-agenda-current-span 'month)
2353 :keys "v m"]
2354 ["Year View" org-agenda-year-view
2355 :active (org-agenda-check-type nil 'agenda)
2356 :style radio :selected (eq org-agenda-current-span 'year)
2357 :keys "v y"]
2358 "--"
2359 ["Include Diary" org-agenda-toggle-diary
2360 :style toggle :selected org-agenda-include-diary
2361 :active (org-agenda-check-type nil 'agenda)]
2362 ["Include Deadlines" org-agenda-toggle-deadlines
2363 :style toggle :selected org-agenda-include-deadlines
2364 :active (org-agenda-check-type nil 'agenda)]
2365 ["Use Time Grid" org-agenda-toggle-time-grid
2366 :style toggle :selected org-agenda-use-time-grid
2367 :active (org-agenda-check-type nil 'agenda)]
2368 "--"
2369 ["Show clock report" org-agenda-clockreport-mode
2370 :style toggle :selected org-agenda-clockreport-mode
2371 :active (org-agenda-check-type nil 'agenda)]
2372 ["Show some entry text" org-agenda-entry-text-mode
2373 :style toggle :selected org-agenda-entry-text-mode
2374 :active t]
2375 "--"
2376 ["Show Logbook entries" org-agenda-log-mode
2377 :style toggle :selected org-agenda-show-log
2378 :active (org-agenda-check-type nil 'agenda 'timeline)
2379 :keys "v l (or just l)"]
2380 ["Include archived trees" org-agenda-archives-mode
2381 :style toggle :selected org-agenda-archives-mode :active t
2382 :keys "v a"]
2383 ["Include archive files" (org-agenda-archives-mode t)
2384 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2385 :keys "v A"]
2386 "--"
2387 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2388 ["Write view to file" org-agenda-write t]
2389 ["Rebuild buffer" org-agenda-redo t]
2390 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2391 "--"
2392 ["Show original entry" org-agenda-show t]
2393 ["Go To (other window)" org-agenda-goto t]
2394 ["Go To (this window)" org-agenda-switch-to t]
2395 ["Capture with cursor date" org-agenda-capture t]
2396 ["Follow Mode" org-agenda-follow-mode
2397 :style toggle :selected org-agenda-follow-mode :active t]
2398 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2399 "--"
2400 ("TODO"
2401 ["Cycle TODO" org-agenda-todo t]
2402 ["Next TODO set" org-agenda-todo-nextset t]
2403 ["Previous TODO set" org-agenda-todo-previousset t]
2404 ["Add note" org-agenda-add-note t])
2405 ("Archive/Refile/Delete"
2406 ["Archive default" org-agenda-archive-default t]
2407 ["Archive default" org-agenda-archive-default-with-confirmation t]
2408 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2409 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2410 ["Archive subtree" org-agenda-archive t]
2411 "--"
2412 ["Refile" org-agenda-refile t]
2413 "--"
2414 ["Delete subtree" org-agenda-kill t])
2415 ("Bulk action"
2416 ["Mark entry" org-agenda-bulk-mark t]
2417 ["Mark all" org-agenda-bulk-mark-all t]
2418 ["Unmark entry" org-agenda-bulk-unmark t]
2419 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2420 ["Toggle mark" org-agenda-bulk-toggle t]
2421 ["Toggle all" org-agenda-bulk-toggle-all t]
2422 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2423 ["Act on all marked" org-agenda-bulk-action t]
2424 "--"
2425 ("Tags and Properties"
2426 ["Show all Tags" org-agenda-show-tags t]
2427 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2428 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2429 "--"
2430 ["Column View" org-columns t])
2431 ("Deadline/Schedule"
2432 ["Schedule" org-agenda-schedule t]
2433 ["Set Deadline" org-agenda-deadline t]
2434 "--"
2435 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2437 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2438 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2439 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2440 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2441 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2442 ("Clock and Effort"
2443 ["Clock in" org-agenda-clock-in t]
2444 ["Clock out" org-agenda-clock-out t]
2445 ["Clock cancel" org-agenda-clock-cancel t]
2446 ["Goto running clock" org-clock-goto t]
2447 "--"
2448 ["Set Effort" org-agenda-set-effort t]
2449 ["Change clocked effort" org-clock-modify-effort-estimate
2450 (org-clock-is-active)])
2451 ("Priority"
2452 ["Set Priority" org-agenda-priority t]
2453 ["Increase Priority" org-agenda-priority-up t]
2454 ["Decrease Priority" org-agenda-priority-down t]
2455 ["Show Priority" org-show-priority t])
2456 ("Calendar/Diary"
2457 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2462 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2463 "--"
2464 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2465 "--"
2466 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2467 "--"
2468 ("MobileOrg"
2469 ["Push Files and Views" org-mobile-push t]
2470 ["Get Captured and Flagged" org-mobile-pull t]
2471 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2472 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2473 "--"
2474 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2475 "--"
2476 ["Quit" org-agenda-quit t]
2477 ["Exit and Release Buffers" org-agenda-exit t]
2478 ))
2479
2480 ;;; Agenda undo
2481
2482 (defvar org-agenda-allow-remote-undo t
2483 "Non-nil means allow remote undo from the agenda buffer.")
2484 (defvar org-agenda-undo-has-started-in nil
2485 "Buffers that have already seen `undo-start' in the current undo sequence.")
2486
2487 (defun org-agenda-undo ()
2488 "Undo a remote editing step in the agenda.
2489 This undoes changes both in the agenda buffer and in the remote buffer
2490 that have been changed along."
2491 (interactive)
2492 (or org-agenda-allow-remote-undo
2493 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2494 (if (not (eq this-command last-command))
2495 (setq org-agenda-undo-has-started-in nil
2496 org-agenda-pending-undo-list org-agenda-undo-list))
2497 (if (not org-agenda-pending-undo-list)
2498 (user-error "No further undo information"))
2499 (let* ((entry (pop org-agenda-pending-undo-list))
2500 buf line cmd rembuf)
2501 (setq cmd (pop entry) line (pop entry))
2502 (setq rembuf (nth 2 entry))
2503 (org-with-remote-undo rembuf
2504 (while (bufferp (setq buf (pop entry)))
2505 (if (pop entry)
2506 (with-current-buffer buf
2507 (let ((last-undo-buffer buf)
2508 (inhibit-read-only t))
2509 (unless (memq buf org-agenda-undo-has-started-in)
2510 (push buf org-agenda-undo-has-started-in)
2511 (make-local-variable 'pending-undo-list)
2512 (undo-start))
2513 (while (and pending-undo-list
2514 (listp pending-undo-list)
2515 (not (car pending-undo-list)))
2516 (pop pending-undo-list))
2517 (undo-more 1))))))
2518 (org-goto-line line)
2519 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2520
2521 (defun org-verify-change-for-undo (l1 l2)
2522 "Verify that a real change occurred between the undo lists L1 and L2."
2523 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2524 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2525 (not (eq l1 l2)))
2526
2527 ;;; Agenda dispatch
2528
2529 (defvar org-agenda-restrict-begin (make-marker))
2530 (defvar org-agenda-restrict-end (make-marker))
2531 (defvar org-agenda-last-dispatch-buffer nil)
2532 (defvar org-agenda-overriding-restriction nil)
2533
2534 (defcustom org-agenda-custom-commands-contexts nil
2535 "Alist of custom agenda keys and contextual rules.
2536
2537 For example, if you have a custom agenda command \"p\" and you
2538 want this command to be accessible only from plain text files,
2539 use this:
2540
2541 '((\"p\" ((in-file . \"\\.txt\"))))
2542
2543 Here are the available contexts definitions:
2544
2545 in-file: command displayed only in matching files
2546 in-mode: command displayed only in matching modes
2547 not-in-file: command not displayed in matching files
2548 not-in-mode: command not displayed in matching modes
2549 in-buffer: command displayed only in matching buffers
2550 not-in-buffer: command not displayed in matching buffers
2551 [function]: a custom function taking no argument
2552
2553 If you define several checks, the agenda command will be
2554 accessible if there is at least one valid check.
2555
2556 You can also bind a key to another agenda custom command
2557 depending on contextual rules.
2558
2559 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2560
2561 Here it means: in .txt files, use \"p\" as the key for the
2562 agenda command otherwise associated with \"q\". (The command
2563 originally associated with \"q\" is not displayed to avoid
2564 duplicates.)"
2565 :version "24.3"
2566 :group 'org-agenda-custom-commands
2567 :type '(repeat (list :tag "Rule"
2568 (string :tag " Agenda key")
2569 (string :tag "Replace by command")
2570 (repeat :tag "Available when"
2571 (choice
2572 (cons :tag "Condition"
2573 (choice
2574 (const :tag "In file" in-file)
2575 (const :tag "Not in file" not-in-file)
2576 (const :tag "In buffer" in-buffer)
2577 (const :tag "Not in buffer" not-in-buffer)
2578 (const :tag "In mode" in-mode)
2579 (const :tag "Not in mode" not-in-mode))
2580 (regexp))
2581 (function :tag "Custom function"))))))
2582
2583 (defcustom org-agenda-max-entries nil
2584 "Maximum number of entries to display in an agenda.
2585 This can be nil (no limit) or an integer or an alist of agenda
2586 types with an associated number of entries to display in this
2587 type."
2588 :version "24.4"
2589 :package-version '(Org . "8.0")
2590 :group 'org-agenda-custom-commands
2591 :type '(choice (symbol :tag "No limit" nil)
2592 (integer :tag "Max number of entries")
2593 (repeat
2594 (cons (choice :tag "Agenda type"
2595 (const agenda)
2596 (const todo)
2597 (const tags)
2598 (const search)
2599 (const timeline))
2600 (integer :tag "Max number of entries")))))
2601
2602 (defcustom org-agenda-max-todos nil
2603 "Maximum number of TODOs to display in an agenda.
2604 This can be nil (no limit) or an integer or an alist of agenda
2605 types with an associated number of entries to display in this
2606 type."
2607 :version "24.4"
2608 :package-version '(Org . "8.0")
2609 :group 'org-agenda-custom-commands
2610 :type '(choice (symbol :tag "No limit" nil)
2611 (integer :tag "Max number of TODOs")
2612 (repeat
2613 (cons (choice :tag "Agenda type"
2614 (const agenda)
2615 (const todo)
2616 (const tags)
2617 (const search)
2618 (const timeline))
2619 (integer :tag "Max number of TODOs")))))
2620
2621 (defcustom org-agenda-max-tags nil
2622 "Maximum number of tagged entries to display in an agenda.
2623 This can be nil (no limit) or an integer or an alist of agenda
2624 types with an associated number of entries to display in this
2625 type."
2626 :version "24.4"
2627 :package-version '(Org . "8.0")
2628 :group 'org-agenda-custom-commands
2629 :type '(choice (symbol :tag "No limit" nil)
2630 (integer :tag "Max number of tagged entries")
2631 (repeat
2632 (cons (choice :tag "Agenda type"
2633 (const agenda)
2634 (const todo)
2635 (const tags)
2636 (const search)
2637 (const timeline))
2638 (integer :tag "Max number of tagged entries")))))
2639
2640 (defcustom org-agenda-max-effort nil
2641 "Maximum cumulated effort duration for the agenda.
2642 This can be nil (no limit) or a number of minutes (as an integer)
2643 or an alist of agenda types with an associated number of minutes
2644 to limit entries to in this type."
2645 :version "24.4"
2646 :package-version '(Org . "8.0")
2647 :group 'org-agenda-custom-commands
2648 :type '(choice (symbol :tag "No limit" nil)
2649 (integer :tag "Max number of minutes")
2650 (repeat
2651 (cons (choice :tag "Agenda type"
2652 (const agenda)
2653 (const todo)
2654 (const tags)
2655 (const search)
2656 (const timeline))
2657 (integer :tag "Max number of minutes")))))
2658
2659 (defvar org-keys nil)
2660 (defvar org-match nil)
2661 ;;;###autoload
2662 (defun org-agenda (&optional arg org-keys restriction)
2663 "Dispatch agenda commands to collect entries to the agenda buffer.
2664 Prompts for a command to execute. Any prefix arg will be passed
2665 on to the selected command. The default selections are:
2666
2667 a Call `org-agenda-list' to display the agenda for current day or week.
2668 t Call `org-todo-list' to display the global todo list.
2669 T Call `org-todo-list' to display the global todo list, select only
2670 entries with a specific TODO keyword (the user gets a prompt).
2671 m Call `org-tags-view' to display headlines with tags matching
2672 a condition (the user is prompted for the condition).
2673 M Like `m', but select only TODO entries, no ordinary headlines.
2674 L Create a timeline for the current buffer.
2675 e Export views to associated files.
2676 s Search entries for keywords.
2677 S Search entries for keywords, only with TODO keywords.
2678 / Multi occur across all agenda files and also files listed
2679 in `org-agenda-text-search-extra-files'.
2680 < Restrict agenda commands to buffer, subtree, or region.
2681 Press several times to get the desired effect.
2682 > Remove a previous restriction.
2683 # List \"stuck\" projects.
2684 ! Configure what \"stuck\" means.
2685 C Configure custom agenda commands.
2686
2687 More commands can be added by configuring the variable
2688 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2689 searches can be pre-defined in this way.
2690
2691 If the current buffer is in Org-mode and visiting a file, you can also
2692 first press `<' once to indicate that the agenda should be temporarily
2693 \(until the next use of \\[org-agenda]) restricted to the current file.
2694 Pressing `<' twice means to restrict to the current subtree or region
2695 \(if active)."
2696 (interactive "P")
2697 (catch 'exit
2698 (let* ((prefix-descriptions nil)
2699 (org-agenda-buffer-name org-agenda-buffer-name)
2700 (org-agenda-window-setup (if (equal (buffer-name)
2701 org-agenda-buffer-name)
2702 'current-window
2703 org-agenda-window-setup))
2704 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2705 (org-agenda-custom-commands
2706 ;; normalize different versions
2707 (delq nil
2708 (mapcar
2709 (lambda (x)
2710 (cond ((stringp (cdr x))
2711 (push x prefix-descriptions)
2712 nil)
2713 ((stringp (nth 1 x)) x)
2714 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2715 (t (cons (car x) (cons "" (cdr x))))))
2716 org-agenda-custom-commands)))
2717 (org-agenda-custom-commands
2718 (org-contextualize-keys
2719 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2720 (buf (current-buffer))
2721 (bfn (buffer-file-name (buffer-base-buffer)))
2722 entry key type org-match lprops ans)
2723 ;; Turn off restriction unless there is an overriding one,
2724 (unless org-agenda-overriding-restriction
2725 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2726 ;; There is a request to keep the file list in place
2727 (put 'org-agenda-files 'org-restrict nil))
2728 (setq org-agenda-restrict nil)
2729 (move-marker org-agenda-restrict-begin nil)
2730 (move-marker org-agenda-restrict-end nil))
2731 ;; Delete old local properties
2732 (put 'org-agenda-redo-command 'org-lprops nil)
2733 ;; Delete previously set last-arguments
2734 (put 'org-agenda-redo-command 'last-args nil)
2735 ;; Remember where this call originated
2736 (setq org-agenda-last-dispatch-buffer (current-buffer))
2737 (unless org-keys
2738 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2739 org-keys (car ans)
2740 restriction (cdr ans)))
2741 ;; If we have sticky agenda buffers, set a name for the buffer,
2742 ;; depending on the invoking keys. The user may still set this
2743 ;; as a command option, which will overwrite what we do here.
2744 (if org-agenda-sticky
2745 (setq org-agenda-buffer-name
2746 (format "*Org Agenda(%s)*" org-keys)))
2747 ;; Establish the restriction, if any
2748 (when (and (not org-agenda-overriding-restriction) restriction)
2749 (put 'org-agenda-files 'org-restrict (list bfn))
2750 (cond
2751 ((eq restriction 'region)
2752 (setq org-agenda-restrict (current-buffer))
2753 (move-marker org-agenda-restrict-begin (region-beginning))
2754 (move-marker org-agenda-restrict-end (region-end)))
2755 ((eq restriction 'subtree)
2756 (save-excursion
2757 (setq org-agenda-restrict (current-buffer))
2758 (org-back-to-heading t)
2759 (move-marker org-agenda-restrict-begin (point))
2760 (move-marker org-agenda-restrict-end
2761 (progn (org-end-of-subtree t)))))))
2762
2763 ;; For example the todo list should not need it (but does...)
2764 (cond
2765 ((setq entry (assoc org-keys org-agenda-custom-commands))
2766 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2767 (progn
2768 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2769 lprops (nth 4 entry))
2770 (if org-agenda-sticky
2771 (setq org-agenda-buffer-name
2772 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2773 (format "*Org Agenda(%s)*" org-keys))))
2774 (put 'org-agenda-redo-command 'org-lprops lprops)
2775 (cond
2776 ((eq type 'agenda)
2777 (org-let lprops '(org-agenda-list current-prefix-arg)))
2778 ((eq type 'agenda*)
2779 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2780 ((eq type 'alltodo)
2781 (org-let lprops '(org-todo-list current-prefix-arg)))
2782 ((eq type 'search)
2783 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2784 ((eq type 'stuck)
2785 (org-let lprops '(org-agenda-list-stuck-projects
2786 current-prefix-arg)))
2787 ((eq type 'tags)
2788 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2789 ((eq type 'tags-todo)
2790 (org-let lprops '(org-tags-view '(4) org-match)))
2791 ((eq type 'todo)
2792 (org-let lprops '(org-todo-list org-match)))
2793 ((eq type 'tags-tree)
2794 (org-check-for-org-mode)
2795 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2796 ((eq type 'todo-tree)
2797 (org-check-for-org-mode)
2798 (org-let lprops
2799 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2800 (regexp-quote org-match) "\\>"))))
2801 ((eq type 'occur-tree)
2802 (org-check-for-org-mode)
2803 (org-let lprops '(org-occur org-match)))
2804 ((functionp type)
2805 (org-let lprops '(funcall type org-match)))
2806 ((fboundp type)
2807 (org-let lprops '(funcall type org-match)))
2808 (t (user-error "Invalid custom agenda command type %s" type))))
2809 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2810 ((equal org-keys "C")
2811 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2812 (customize-variable 'org-agenda-custom-commands))
2813 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2814 ((equal org-keys "s") (call-interactively 'org-search-view))
2815 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2816 ((equal org-keys "t") (call-interactively 'org-todo-list))
2817 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2818 ((equal org-keys "m") (call-interactively 'org-tags-view))
2819 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2820 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2821 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2822 (org-add-hook
2823 'post-command-hook
2824 (lambda ()
2825 (unless (current-message)
2826 (let* ((m (org-agenda-get-any-marker))
2827 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2828 (when note
2829 (message (concat
2830 "FLAGGING-NOTE ([?] for more info): "
2831 (org-add-props
2832 (replace-regexp-in-string
2833 "\\\\n" "//"
2834 (copy-sequence note))
2835 nil 'face 'org-warning)))))))
2836 t t))
2837 ((equal org-keys "L")
2838 (unless (derived-mode-p 'org-mode)
2839 (user-error "This is not an Org-mode file"))
2840 (unless restriction
2841 (put 'org-agenda-files 'org-restrict (list bfn))
2842 (org-call-with-arg 'org-timeline arg)))
2843 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2844 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2845 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2846 (t (user-error "Invalid agenda key"))))))
2847
2848 (defvar org-agenda-multi)
2849
2850 (defun org-agenda-append-agenda ()
2851 "Append another agenda view to the current one.
2852 This function allows interactive building of block agendas.
2853 Agenda views are separated by `org-agenda-block-separator'."
2854 (interactive)
2855 (unless (derived-mode-p 'org-agenda-mode)
2856 (user-error "Can only append from within agenda buffer"))
2857 (let ((org-agenda-multi t))
2858 (org-agenda)
2859 (widen)
2860 (org-agenda-finalize)
2861 (setq buffer-read-only t)
2862 (org-agenda-fit-window-to-buffer)))
2863
2864 (defun org-agenda-normalize-custom-commands (cmds)
2865 "Normalize custom commands CMDS."
2866 (delq nil
2867 (mapcar
2868 (lambda (x)
2869 (cond ((stringp (cdr x)) nil)
2870 ((stringp (nth 1 x)) x)
2871 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2872 (t (cons (car x) (cons "" (cdr x))))))
2873 cmds)))
2874
2875 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2876 "The user interface for selecting an agenda command."
2877 (catch 'exit
2878 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2879 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2880 (region-p (org-region-active-p))
2881 (custom org-agenda-custom-commands)
2882 (selstring "")
2883 restriction second-time
2884 c entry key type match prefixes rmheader header-end custom1 desc
2885 line lines left right n n1)
2886 (save-window-excursion
2887 (delete-other-windows)
2888 (org-switch-to-buffer-other-window " *Agenda Commands*")
2889 (erase-buffer)
2890 (insert (eval-when-compile
2891 (let ((header
2892 "Press key for an agenda command: < Buffer, subtree/region restriction
2893 -------------------------------- > Remove restriction
2894 a Agenda for current week or day e Export agenda views
2895 t List of all TODO entries T Entries with special TODO kwd
2896 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2897 s Search for keywords S Like s, but only TODO entries
2898 L Timeline for current buffer # List stuck projects (!=configure)
2899 / Multi-occur C Configure custom agenda commands
2900 ? Find :FLAGGED: entries * Toggle sticky agenda views
2901 ")
2902 (start 0))
2903 (while (string-match
2904 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2905 header start)
2906 (setq start (match-end 0))
2907 (add-text-properties (match-beginning 2) (match-end 2)
2908 '(face bold) header))
2909 header)))
2910 (setq header-end (point-marker))
2911 (while t
2912 (setq custom1 custom)
2913 (when (eq rmheader t)
2914 (org-goto-line 1)
2915 (re-search-forward ":" nil t)
2916 (delete-region (match-end 0) (point-at-eol))
2917 (forward-char 1)
2918 (looking-at "-+")
2919 (delete-region (match-end 0) (point-at-eol))
2920 (move-marker header-end (match-end 0)))
2921 (goto-char header-end)
2922 (delete-region (point) (point-max))
2923
2924 ;; Produce all the lines that describe custom commands and prefixes
2925 (setq lines nil)
2926 (while (setq entry (pop custom1))
2927 (setq key (car entry) desc (nth 1 entry)
2928 type (nth 2 entry)
2929 match (nth 3 entry))
2930 (if (> (length key) 1)
2931 (add-to-list 'prefixes (string-to-char key))
2932 (setq line
2933 (format
2934 "%-4s%-14s"
2935 (org-add-props (copy-sequence key)
2936 '(face bold))
2937 (cond
2938 ((string-match "\\S-" desc) desc)
2939 ((eq type 'agenda) "Agenda for current week or day")
2940 ((eq type 'agenda*) "Appointments for current week or day")
2941 ((eq type 'alltodo) "List of all TODO entries")
2942 ((eq type 'search) "Word search")
2943 ((eq type 'stuck) "List of stuck projects")
2944 ((eq type 'todo) "TODO keyword")
2945 ((eq type 'tags) "Tags query")
2946 ((eq type 'tags-todo) "Tags (TODO)")
2947 ((eq type 'tags-tree) "Tags tree")
2948 ((eq type 'todo-tree) "TODO kwd tree")
2949 ((eq type 'occur-tree) "Occur tree")
2950 ((functionp type) (if (symbolp type)
2951 (symbol-name type)
2952 "Lambda expression"))
2953 (t "???"))))
2954 (if org-agenda-menu-show-matcher
2955 (setq line
2956 (concat line ": "
2957 (cond
2958 ((stringp match)
2959 (setq match (copy-sequence match))
2960 (org-add-props match nil 'face 'org-warning))
2961 ((listp type)
2962 (format "set of %d commands" (length type))))))
2963 (if (org-string-nw-p match)
2964 (add-text-properties
2965 0 (length line) (list 'help-echo
2966 (concat "Matcher: " match)) line)))
2967 (push line lines)))
2968 (setq lines (nreverse lines))
2969 (when prefixes
2970 (mapc (lambda (x)
2971 (push
2972 (format "%s %s"
2973 (org-add-props (char-to-string x)
2974 nil 'face 'bold)
2975 (or (cdr (assoc (concat selstring
2976 (char-to-string x))
2977 prefix-descriptions))
2978 "Prefix key"))
2979 lines))
2980 prefixes))
2981
2982 ;; Check if we should display in two columns
2983 (if org-agenda-menu-two-columns
2984 (progn
2985 (setq n (length lines)
2986 n1 (+ (/ n 2) (mod n 2))
2987 right (nthcdr n1 lines)
2988 left (copy-sequence lines))
2989 (setcdr (nthcdr (1- n1) left) nil))
2990 (setq left lines right nil))
2991 (while left
2992 (insert "\n" (pop left))
2993 (when right
2994 (if (< (current-column) 40)
2995 (move-to-column 40 t)
2996 (insert " "))
2997 (insert (pop right))))
2998
2999 ;; Make the window the right size
3000 (goto-char (point-min))
3001 (if second-time
3002 (if (not (pos-visible-in-window-p (point-max)))
3003 (org-fit-window-to-buffer))
3004 (setq second-time t)
3005 (org-fit-window-to-buffer))
3006
3007 ;; Ask for selection
3008 (message "Press key for agenda command%s:"
3009 (if (or restrict-ok org-agenda-overriding-restriction)
3010 (if org-agenda-overriding-restriction
3011 " (restriction lock active)"
3012 (if restriction
3013 (format " (restricted to %s)" restriction)
3014 " (unrestricted)"))
3015 ""))
3016 (setq c (read-char-exclusive))
3017 (message "")
3018 (cond
3019 ((assoc (char-to-string c) custom)
3020 (setq selstring (concat selstring (char-to-string c)))
3021 (throw 'exit (cons selstring restriction)))
3022 ((memq c prefixes)
3023 (setq selstring (concat selstring (char-to-string c))
3024 prefixes nil
3025 rmheader (or rmheader t)
3026 custom (delq nil (mapcar
3027 (lambda (x)
3028 (if (or (= (length (car x)) 1)
3029 (/= (string-to-char (car x)) c))
3030 nil
3031 (cons (substring (car x) 1) (cdr x))))
3032 custom))))
3033 ((eq c ?*)
3034 (call-interactively 'org-toggle-sticky-agenda)
3035 (sit-for 2))
3036 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3037 (message "Restriction is only possible in Org-mode buffers")
3038 (ding) (sit-for 1))
3039 ((eq c ?1)
3040 (org-agenda-remove-restriction-lock 'noupdate)
3041 (setq restriction 'buffer))
3042 ((eq c ?0)
3043 (org-agenda-remove-restriction-lock 'noupdate)
3044 (setq restriction (if region-p 'region 'subtree)))
3045 ((eq c ?<)
3046 (org-agenda-remove-restriction-lock 'noupdate)
3047 (setq restriction
3048 (cond
3049 ((eq restriction 'buffer)
3050 (if region-p 'region 'subtree))
3051 ((memq restriction '(subtree region))
3052 nil)
3053 (t 'buffer))))
3054 ((eq c ?>)
3055 (org-agenda-remove-restriction-lock 'noupdate)
3056 (setq restriction nil))
3057 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3058 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3059 ((and (> (length selstring) 0) (eq c ?\d))
3060 (delete-window)
3061 (org-agenda-get-restriction-and-command prefix-descriptions))
3062
3063 ((equal c ?q) (error "Abort"))
3064 (t (user-error "Invalid key %c" c))))))))
3065
3066 (defun org-agenda-fit-window-to-buffer ()
3067 "Fit the window to the buffer size."
3068 (and (memq org-agenda-window-setup '(reorganize-frame))
3069 (fboundp 'fit-window-to-buffer)
3070 (org-fit-window-to-buffer
3071 nil
3072 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3073 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3074
3075 (defvar org-cmd nil)
3076 (defvar org-agenda-overriding-cmd nil)
3077 (defvar org-agenda-overriding-arguments nil)
3078 (defvar org-agenda-overriding-cmd-arguments nil)
3079 (defun org-agenda-run-series (name series)
3080 "Run agenda NAME as a SERIES of agenda commands."
3081 (org-let (nth 1 series) '(org-agenda-prepare name))
3082 ;; We need to reset agenda markers here, because when constructing a
3083 ;; block agenda, the individual blocks do not do that.
3084 (org-agenda-reset-markers)
3085 (let* ((org-agenda-multi t)
3086 (redo (list 'org-agenda-run-series name (list 'quote series)))
3087 (cmds (car series))
3088 (gprops (nth 1 series))
3089 match ;; The byte compiler incorrectly complains about this. Keep it!
3090 org-cmd type lprops)
3091 (while (setq org-cmd (pop cmds))
3092 (setq type (car org-cmd)
3093 match (eval (nth 1 org-cmd))
3094 lprops (nth 2 org-cmd))
3095 (let ((org-agenda-overriding-arguments
3096 (if (eq org-agenda-overriding-cmd org-cmd)
3097 (or org-agenda-overriding-arguments
3098 org-agenda-overriding-cmd-arguments))))
3099 (cond
3100 ((eq type 'agenda)
3101 (org-let2 gprops lprops
3102 '(call-interactively 'org-agenda-list)))
3103 ((eq type 'agenda*)
3104 (org-let2 gprops lprops
3105 '(funcall 'org-agenda-list nil nil t)))
3106 ((eq type 'alltodo)
3107 (org-let2 gprops lprops
3108 '(call-interactively 'org-todo-list)))
3109 ((eq type 'search)
3110 (org-let2 gprops lprops
3111 '(org-search-view current-prefix-arg match nil)))
3112 ((eq type 'stuck)
3113 (org-let2 gprops lprops
3114 '(call-interactively 'org-agenda-list-stuck-projects)))
3115 ((eq type 'tags)
3116 (org-let2 gprops lprops
3117 '(org-tags-view current-prefix-arg match)))
3118 ((eq type 'tags-todo)
3119 (org-let2 gprops lprops
3120 '(org-tags-view '(4) match)))
3121 ((eq type 'todo)
3122 (org-let2 gprops lprops
3123 '(org-todo-list match)))
3124 ((fboundp type)
3125 (org-let2 gprops lprops
3126 '(funcall type match)))
3127 (t (error "Invalid type in command series")))))
3128 (widen)
3129 (let ((inhibit-read-only t))
3130 (add-text-properties (point-min) (point-max)
3131 `(org-series t org-series-redo-cmd ,redo)))
3132 (setq org-agenda-redo-command redo)
3133 (goto-char (point-min)))
3134 (org-agenda-fit-window-to-buffer)
3135 (org-let (nth 1 series) '(org-agenda-finalize)))
3136
3137 ;;;###autoload
3138 (defmacro org-batch-agenda (cmd-key &rest parameters)
3139 "Run an agenda command in batch mode and send the result to STDOUT.
3140 If CMD-KEY is a string of length 1, it is used as a key in
3141 `org-agenda-custom-commands' and triggers this command. If it is a
3142 longer string it is used as a tags/todo match string.
3143 Parameters are alternating variable names and values that will be bound
3144 before running the agenda command."
3145 (org-eval-in-environment (org-make-parameter-alist parameters)
3146 (let (org-agenda-sticky)
3147 (if (> (length cmd-key) 2)
3148 (org-tags-view nil cmd-key)
3149 (org-agenda nil cmd-key))))
3150 (set-buffer org-agenda-buffer-name)
3151 (princ (buffer-string)))
3152
3153 (defvar org-agenda-info nil)
3154
3155 ;;;###autoload
3156 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3157 "Run an agenda command in batch mode and send the result to STDOUT.
3158 If CMD-KEY is a string of length 1, it is used as a key in
3159 `org-agenda-custom-commands' and triggers this command. If it is a
3160 longer string it is used as a tags/todo match string.
3161 Parameters are alternating variable names and values that will be bound
3162 before running the agenda command.
3163
3164 The output gives a line for each selected agenda item. Each
3165 item is a list of comma-separated values, like this:
3166
3167 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3168
3169 category The category of the item
3170 head The headline, without TODO kwd, TAGS and PRIORITY
3171 type The type of the agenda entry, can be
3172 todo selected in TODO match
3173 tagsmatch selected in tags match
3174 diary imported from diary
3175 deadline a deadline on given date
3176 scheduled scheduled on given date
3177 timestamp entry has timestamp on given date
3178 closed entry was closed on given date
3179 upcoming-deadline warning about deadline
3180 past-scheduled forwarded scheduled item
3181 block entry has date block including g. date
3182 todo The todo keyword, if any
3183 tags All tags including inherited ones, separated by colons
3184 date The relevant date, like 2007-2-14
3185 time The time, like 15:00-16:50
3186 extra Sting with extra planning info
3187 priority-l The priority letter if any was given
3188 priority-n The computed numerical priority
3189 agenda-day The day in the agenda where this is listed"
3190 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3191 (org-make-parameter-alist parameters))
3192 (if (> (length cmd-key) 2)
3193 (org-tags-view nil cmd-key)
3194 (org-agenda nil cmd-key)))
3195 (set-buffer org-agenda-buffer-name)
3196 (let* ((lines (org-split-string (buffer-string) "\n"))
3197 line)
3198 (while (setq line (pop lines))
3199 (catch 'next
3200 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3201 (setq org-agenda-info
3202 (org-fix-agenda-info (text-properties-at 0 line)))
3203 (princ
3204 (mapconcat 'org-agenda-export-csv-mapper
3205 '(org-category txt type todo tags date time extra
3206 priority-letter priority agenda-day)
3207 ","))
3208 (princ "\n")))))
3209
3210 (defun org-fix-agenda-info (props)
3211 "Make sure all properties on an agenda item have a canonical form.
3212 This ensures the export commands can easily use it."
3213 (let (tmp re)
3214 (when (setq tmp (plist-get props 'tags))
3215 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3216 (when (setq tmp (plist-get props 'date))
3217 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3218 (let ((calendar-date-display-form '(year "-" month "-" day)))
3219 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3220
3221 (setq tmp (calendar-date-string tmp)))
3222 (setq props (plist-put props 'date tmp)))
3223 (when (setq tmp (plist-get props 'day))
3224 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3225 (let ((calendar-date-display-form '(year "-" month "-" day)))
3226 (setq tmp (calendar-date-string tmp)))
3227 (setq props (plist-put props 'day tmp))
3228 (setq props (plist-put props 'agenda-day tmp)))
3229 (when (setq tmp (plist-get props 'txt))
3230 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3231 (plist-put props 'priority-letter (match-string 1 tmp))
3232 (setq tmp (replace-match "" t t tmp)))
3233 (when (and (setq re (plist-get props 'org-todo-regexp))
3234 (setq re (concat "\\`\\.*" re " ?"))
3235 (string-match re tmp))
3236 (plist-put props 'todo (match-string 1 tmp))
3237 (setq tmp (replace-match "" t t tmp)))
3238 (plist-put props 'txt tmp)))
3239 props)
3240
3241 (defun org-agenda-export-csv-mapper (prop)
3242 (let ((res (plist-get org-agenda-info prop)))
3243 (setq res
3244 (cond
3245 ((not res) "")
3246 ((stringp res) res)
3247 (t (prin1-to-string res))))
3248 (while (string-match "," res)
3249 (setq res (replace-match ";" t t res)))
3250 (org-trim res)))
3251
3252 ;;;###autoload
3253 (defun org-store-agenda-views (&rest parameters)
3254 "Store agenda views."
3255 (interactive)
3256 (eval (list 'org-batch-store-agenda-views)))
3257
3258 ;;;###autoload
3259 (defmacro org-batch-store-agenda-views (&rest parameters)
3260 "Run all custom agenda commands that have a file argument."
3261 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3262 (pop-up-frames nil)
3263 (dir default-directory)
3264 (pars (org-make-parameter-alist parameters))
3265 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3266 (save-window-excursion
3267 (while cmds
3268 (setq cmd (pop cmds)
3269 thiscmdkey (car cmd)
3270 thiscmdcmd (cdr cmd)
3271 match (nth 2 thiscmdcmd)
3272 bufname (if org-agenda-sticky
3273 (or (and (stringp match)
3274 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3275 (format "*Org Agenda(%s)*" thiscmdkey))
3276 org-agenda-buffer-name)
3277 cmd-or-set (nth 2 cmd)
3278 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3279 files (nth (if (listp cmd-or-set) 4 5) cmd))
3280 (if (stringp files) (setq files (list files)))
3281 (when files
3282 (org-eval-in-environment (append org-agenda-exporter-settings
3283 opts pars)
3284 (org-agenda nil thiscmdkey))
3285 (set-buffer bufname)
3286 (while files
3287 (org-eval-in-environment (append org-agenda-exporter-settings
3288 opts pars)
3289 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3290 (and (get-buffer bufname)
3291 (kill-buffer bufname)))))))
3292
3293 (defvar org-agenda-current-span nil
3294 "The current span used in the agenda view.") ; local variable in the agenda buffer
3295 (defun org-agenda-mark-header-line (pos)
3296 "Mark the line at POS as an agenda structure header."
3297 (save-excursion
3298 (goto-char pos)
3299 (put-text-property (point-at-bol) (point-at-eol)
3300 'org-agenda-structural-header t)
3301 (when org-agenda-title-append
3302 (put-text-property (point-at-bol) (point-at-eol)
3303 'org-agenda-title-append org-agenda-title-append))))
3304
3305 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3306 (defvar org-agenda-write-buffer-name "Agenda View")
3307 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3308 "Write the current buffer (an agenda view) as a file.
3309 Depending on the extension of the file name, plain text (.txt),
3310 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3311 If the extension is .ics, run icalendar export over all files used
3312 to construct the agenda and limit the export to entries listed in the
3313 agenda now.
3314 If the extension is .org, collect all subtrees corresponding to the
3315 agenda entries and add them in an .org file.
3316 With prefix argument OPEN, open the new file immediately.
3317 If NOSETTINGS is given, do not scope the settings of
3318 `org-agenda-exporter-settings' into the export commands. This is used when
3319 the settings have already been scoped and we do not wish to overrule other,
3320 higher priority settings.
3321 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3322 (interactive "FWrite agenda to file: \nP")
3323 (if (or (not (file-writable-p file))
3324 (and (file-exists-p file)
3325 (if (org-called-interactively-p 'any)
3326 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3327 (user-error "Cannot write agenda to file %s" file))
3328 (org-let (if nosettings nil org-agenda-exporter-settings)
3329 '(save-excursion
3330 (save-window-excursion
3331 (let ((bs (copy-sequence (buffer-string))) beg content)
3332 (with-temp-buffer
3333 (rename-buffer org-agenda-write-buffer-name t)
3334 (set-buffer-modified-p nil)
3335 (insert bs)
3336 (org-agenda-remove-marked-text 'org-filtered)
3337 (run-hooks 'org-agenda-before-write-hook)
3338 (cond
3339 ((org-bound-and-true-p org-mobile-creating-agendas)
3340 (org-mobile-write-agenda-for-mobile file))
3341 ((string-match "\\.org\\'" file)
3342 (let (content p m message-log-max)
3343 (goto-char (point-min))
3344 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3345 (goto-char p)
3346 (setq m (get-text-property (point) 'org-hd-marker))
3347 (when m
3348 (push (save-excursion
3349 (set-buffer (marker-buffer m))
3350 (goto-char m)
3351 (org-copy-subtree 1 nil t t)
3352 org-subtree-clip)
3353 content)))
3354 (find-file file)
3355 (erase-buffer)
3356 (dolist (s content) (org-paste-subtree 1 s))
3357 (write-file file)
3358 (kill-buffer (current-buffer))
3359 (message "Org file written to %s" file)))
3360 ((string-match "\\.html?\\'" file)
3361 (require 'htmlize)
3362 (set-buffer (htmlize-buffer (current-buffer)))
3363 (when org-agenda-export-html-style
3364 ;; replace <style> section with org-agenda-export-html-style
3365 (goto-char (point-min))
3366 (kill-region (- (search-forward "<style") 6)
3367 (search-forward "</style>"))
3368 (insert org-agenda-export-html-style))
3369 (write-file file)
3370 (kill-buffer (current-buffer))
3371 (message "HTML written to %s" file))
3372 ((string-match "\\.ps\\'" file)
3373 (require 'ps-print)
3374 (ps-print-buffer-with-faces file)
3375 (message "Postscript written to %s" file))
3376 ((string-match "\\.pdf\\'" file)
3377 (require 'ps-print)
3378 (ps-print-buffer-with-faces
3379 (concat (file-name-sans-extension file) ".ps"))
3380 (call-process "ps2pdf" nil nil nil
3381 (expand-file-name
3382 (concat (file-name-sans-extension file) ".ps"))
3383 (expand-file-name file))
3384 (delete-file (concat (file-name-sans-extension file) ".ps"))
3385 (message "PDF written to %s" file))
3386 ((string-match "\\.ics\\'" file)
3387 (require 'ox-icalendar)
3388 (org-icalendar-export-current-agenda (expand-file-name file)))
3389 (t
3390 (let ((bs (buffer-string)))
3391 (find-file file)
3392 (erase-buffer)
3393 (insert bs)
3394 (save-buffer 0)
3395 (kill-buffer (current-buffer))
3396 (message "Plain text written to %s" file))))))))
3397 (set-buffer (or agenda-bufname
3398 (and (org-called-interactively-p 'any) (buffer-name))
3399 org-agenda-buffer-name)))
3400 (when open (org-open-file file)))
3401
3402 (defun org-agenda-remove-marked-text (property &optional value)
3403 "Delete all text marked with VALUE of PROPERTY.
3404 VALUE defaults to t."
3405 (let (beg)
3406 (setq value (or value t))
3407 (while (setq beg (text-property-any (point-min) (point-max)
3408 property value))
3409 (delete-region
3410 beg (or (next-single-property-change beg property)
3411 (point-max))))))
3412
3413 (defun org-agenda-add-entry-text ()
3414 "Add entry text to agenda lines.
3415 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3416 entry text following headings shown in the agenda.
3417 Drawers will be excluded, also the line with scheduling/deadline info."
3418 (when (and (> org-agenda-add-entry-text-maxlines 0)
3419 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3420 (let (m txt)
3421 (goto-char (point-min))
3422 (while (not (eobp))
3423 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3424 (beginning-of-line 2)
3425 (setq txt (org-agenda-get-some-entry-text
3426 m org-agenda-add-entry-text-maxlines " > "))
3427 (end-of-line 1)
3428 (if (string-match "\\S-" txt)
3429 (insert "\n" txt)
3430 (or (eobp) (forward-char 1))))))))
3431
3432 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3433 &rest keep)
3434 "Extract entry text from MARKER, at most N-LINES lines.
3435 This will ignore drawers etc, just get the text.
3436 If INDENT is given, prefix every line with this string. If KEEP is
3437 given, it is a list of symbols, defining stuff that should not be
3438 removed from the entry content. Currently only `planning' is allowed here."
3439 (let (txt drawer-re kwd-time-re ind)
3440 (save-excursion
3441 (with-current-buffer (marker-buffer marker)
3442 (if (not (derived-mode-p 'org-mode))
3443 (setq txt "")
3444 (save-excursion
3445 (save-restriction
3446 (widen)
3447 (goto-char marker)
3448 (end-of-line 1)
3449 (setq txt (buffer-substring
3450 (min (1+ (point)) (point-max))
3451 (progn (outline-next-heading) (point)))
3452 drawer-re org-drawer-regexp
3453 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3454 ".*\n?"))
3455 (with-temp-buffer
3456 (insert txt)
3457 (when org-agenda-add-entry-text-descriptive-links
3458 (goto-char (point-min))
3459 (while (org-activate-bracket-links (point-max))
3460 (add-text-properties (match-beginning 0) (match-end 0)
3461 '(face org-link))))
3462 (goto-char (point-min))
3463 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3464 (set-text-properties (match-beginning 0) (match-end 0)
3465 nil))
3466 (goto-char (point-min))
3467 (while (re-search-forward drawer-re nil t)
3468 (delete-region
3469 (match-beginning 0)
3470 (progn (re-search-forward
3471 "^[ \t]*:END:.*\n?" nil 'move)
3472 (point))))
3473 (unless (member 'planning keep)
3474 (goto-char (point-min))
3475 (while (re-search-forward kwd-time-re nil t)
3476 (replace-match "")))
3477 (goto-char (point-min))
3478 (when org-agenda-entry-text-exclude-regexps
3479 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3480 (while (setq re (pop re-list))
3481 (goto-char (point-min))
3482 (while (re-search-forward re nil t)
3483 (replace-match "")))))
3484 (goto-char (point-max))
3485 (skip-chars-backward " \t\n")
3486 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3487
3488 ;; find and remove min common indentation
3489 (goto-char (point-min))
3490 (untabify (point-min) (point-max))
3491 (setq ind (org-get-indentation))
3492 (while (not (eobp))
3493 (unless (looking-at "[ \t]*$")
3494 (setq ind (min ind (org-get-indentation))))
3495 (beginning-of-line 2))
3496 (goto-char (point-min))
3497 (while (not (eobp))
3498 (unless (looking-at "[ \t]*$")
3499 (move-to-column ind)
3500 (delete-region (point-at-bol) (point)))
3501 (beginning-of-line 2))
3502
3503 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3504
3505 (goto-char (point-min))
3506 (when indent
3507 (while (and (not (eobp)) (re-search-forward "^" nil t))
3508 (replace-match indent t t)))
3509 (goto-char (point-min))
3510 (while (looking-at "[ \t]*\n") (replace-match ""))
3511 (goto-char (point-max))
3512 (when (> (org-current-line)
3513 n-lines)
3514 (org-goto-line (1+ n-lines))
3515 (backward-char 1))
3516 (setq txt (buffer-substring (point-min) (point)))))))))
3517 txt))
3518
3519 (defun org-check-for-org-mode ()
3520 "Make sure current buffer is in org-mode. Error if not."
3521 (or (derived-mode-p 'org-mode)
3522 (error "Cannot execute org-mode agenda command on buffer in %s"
3523 major-mode)))
3524
3525 ;;; Agenda prepare and finalize
3526
3527 (defvar org-agenda-multi nil) ; dynamically scoped
3528 (defvar org-agenda-pre-window-conf nil)
3529 (defvar org-agenda-columns-active nil)
3530 (defvar org-agenda-name nil)
3531 (defvar org-agenda-tag-filter nil)
3532 (defvar org-agenda-category-filter nil)
3533 (defvar org-agenda-regexp-filter nil)
3534 (defvar org-agenda-top-headline-filter nil)
3535 (defvar org-agenda-tag-filter-preset nil
3536 "A preset of the tags filter used for secondary agenda filtering.
3537 This must be a list of strings, each string must be a single tag preceded
3538 by \"+\" or \"-\".
3539 This variable should not be set directly, but agenda custom commands can
3540 bind it in the options section. The preset filter is a global property of
3541 the entire agenda view. In a block agenda, it will not work reliably to
3542 define a filter for one of the individual blocks. You need to set it in
3543 the global options and expect it to be applied to the entire view.")
3544
3545 (defvar org-agenda-category-filter-preset nil
3546 "A preset of the category filter used for secondary agenda filtering.
3547 This must be a list of strings, each string must be a single category
3548 preceded by \"+\" or \"-\".
3549 This variable should not be set directly, but agenda custom commands can
3550 bind it in the options section. The preset filter is a global property of
3551 the entire agenda view. In a block agenda, it will not work reliably to
3552 define a filter for one of the individual blocks. You need to set it in
3553 the global options and expect it to be applied to the entire view.")
3554
3555 (defvar org-agenda-regexp-filter-preset nil
3556 "A preset of the regexp filter used for secondary agenda filtering.
3557 This must be a list of strings, each string must be a single regexp
3558 preceded by \"+\" or \"-\".
3559 This variable should not be set directly, but agenda custom commands can
3560 bind it in the options section. The preset filter is a global property of
3561 the entire agenda view. In a block agenda, it will not work reliably to
3562 define a filter for one of the individual blocks. You need to set it in
3563 the global options and expect it to be applied to the entire view.")
3564
3565 (defun org-agenda-use-sticky-p ()
3566 "Return non-nil if an agenda buffer named
3567 `org-agenda-buffer-name' exists and should be shown instead of
3568 generating a new one."
3569 (and
3570 ;; turned off by user
3571 org-agenda-sticky
3572 ;; For multi-agenda buffer already exists
3573 (not org-agenda-multi)
3574 ;; buffer found
3575 (get-buffer org-agenda-buffer-name)
3576 ;; C-u parameter is same as last call
3577 (with-current-buffer (get-buffer org-agenda-buffer-name)
3578 (and
3579 (equal current-prefix-arg
3580 org-agenda-last-prefix-arg)
3581 ;; In case user turned stickiness on, while having existing
3582 ;; Agenda buffer active, don't reuse that buffer, because it
3583 ;; does not have org variables local
3584 org-agenda-this-buffer-is-sticky))))
3585
3586 (defun org-agenda-prepare-window (abuf filter-alist)
3587 "Setup agenda buffer in the window.
3588 ABUF is the buffer for the agenda window.
3589 FILTER-ALIST is an alist of filters we need to apply when
3590 `org-agenda-persistent-filter' is non-nil."
3591 (let* ((awin (get-buffer-window abuf)) wconf)
3592 (cond
3593 ((equal (current-buffer) abuf) nil)
3594 (awin (select-window awin))
3595 ((not (setq wconf (current-window-configuration))))
3596 ((equal org-agenda-window-setup 'current-window)
3597 (org-pop-to-buffer-same-window abuf))
3598 ((equal org-agenda-window-setup 'other-window)
3599 (org-switch-to-buffer-other-window abuf))
3600 ((equal org-agenda-window-setup 'other-frame)
3601 (switch-to-buffer-other-frame abuf))
3602 ((equal org-agenda-window-setup 'reorganize-frame)
3603 (delete-other-windows)
3604 (org-switch-to-buffer-other-window abuf)))
3605 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3606 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3607 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3608 ;; Additional test in case agenda is invoked from within agenda
3609 ;; buffer via elisp link.
3610 (unless (equal (current-buffer) abuf)
3611 (org-pop-to-buffer-same-window abuf))
3612 (setq org-agenda-pre-window-conf
3613 (or org-agenda-pre-window-conf wconf))))
3614
3615 (defun org-agenda-prepare (&optional name)
3616 (let ((filter-alist (if org-agenda-persistent-filter
3617 (list `(tag . ,org-agenda-tag-filter)
3618 `(re . ,org-agenda-regexp-filter)
3619 `(car . ,org-agenda-category-filter)))))
3620 (if (org-agenda-use-sticky-p)
3621 (progn
3622 (put 'org-agenda-tag-filter :preset-filter nil)
3623 (put 'org-agenda-category-filter :preset-filter nil)
3624 (put 'org-agenda-regexp-filter :preset-filter nil)
3625 ;; Popup existing buffer
3626 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3627 filter-alist)
3628 (message "Sticky Agenda buffer, use ‘r’ to refresh")
3629 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3630 (throw 'exit "Sticky Agenda buffer, use ‘r’ to refresh"))
3631 (setq org-todo-keywords-for-agenda nil)
3632 (setq org-drawers-for-agenda nil)
3633 (put 'org-agenda-tag-filter :preset-filter
3634 org-agenda-tag-filter-preset)
3635 (put 'org-agenda-category-filter :preset-filter
3636 org-agenda-category-filter-preset)
3637 (put 'org-agenda-regexp-filter :preset-filter
3638 org-agenda-regexp-filter-preset)
3639 (if org-agenda-multi
3640 (progn
3641 (setq buffer-read-only nil)
3642 (goto-char (point-max))
3643 (unless (or (bobp) org-agenda-compact-blocks
3644 (not org-agenda-block-separator))
3645 (insert "\n"
3646 (if (stringp org-agenda-block-separator)
3647 org-agenda-block-separator
3648 (make-string (window-width) org-agenda-block-separator))
3649 "\n"))
3650 (narrow-to-region (point) (point-max)))
3651 (setq org-done-keywords-for-agenda nil)
3652
3653 ;; Setting any org variables that are in org-agenda-local-vars
3654 ;; list need to be done after the prepare call
3655 (org-agenda-prepare-window
3656 (get-buffer-create org-agenda-buffer-name) filter-alist)
3657 (setq buffer-read-only nil)
3658 (org-agenda-reset-markers)
3659 (let ((inhibit-read-only t)) (erase-buffer))
3660 (org-agenda-mode)
3661 (setq org-agenda-buffer (current-buffer))
3662 (setq org-agenda-contributing-files nil)
3663 (setq org-agenda-columns-active nil)
3664 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3665 (setq org-todo-keywords-for-agenda
3666 (org-uniquify org-todo-keywords-for-agenda))
3667 (setq org-done-keywords-for-agenda
3668 (org-uniquify org-done-keywords-for-agenda))
3669 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3670 (setq org-agenda-last-prefix-arg current-prefix-arg)
3671 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3672 (and name (not org-agenda-name)
3673 (org-set-local 'org-agenda-name name)))
3674 (setq buffer-read-only nil))))
3675
3676 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3677 (defun org-agenda-finalize ()
3678 "Finishing touch for the agenda buffer, called just before displaying it."
3679 (unless org-agenda-multi
3680 (save-excursion
3681 (let ((inhibit-read-only t))
3682 (goto-char (point-min))
3683 (save-excursion
3684 (while (org-activate-bracket-links (point-max))
3685 (add-text-properties (match-beginning 0) (match-end 0)
3686 '(face org-link))))
3687 (save-excursion
3688 (while (org-activate-plain-links (point-max))
3689 (add-text-properties (match-beginning 0) (match-end 0)
3690 '(face org-link))))
3691 (unless (eq org-agenda-remove-tags t)
3692 (org-agenda-align-tags))
3693 (unless org-agenda-with-colors
3694 (remove-text-properties (point-min) (point-max) '(face nil)))
3695 (if (and (boundp 'org-agenda-overriding-columns-format)
3696 org-agenda-overriding-columns-format)
3697 (org-set-local 'org-agenda-overriding-columns-format
3698 org-agenda-overriding-columns-format))
3699 (if (and (boundp 'org-agenda-view-columns-initially)
3700 org-agenda-view-columns-initially)
3701 (org-agenda-columns))
3702 (when org-agenda-fontify-priorities
3703 (org-agenda-fontify-priorities))
3704 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3705 (org-agenda-dim-blocked-tasks))
3706 (org-agenda-mark-clocking-task)
3707 (when org-agenda-entry-text-mode
3708 (org-agenda-entry-text-hide)
3709 (org-agenda-entry-text-show))
3710 (if (and (functionp 'org-habit-insert-consistency-graphs)
3711 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3712 (org-habit-insert-consistency-graphs))
3713 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3714 (unless (or (eq org-agenda-show-inherited-tags 'always)
3715 (and (listp org-agenda-show-inherited-tags)
3716 (memq org-agenda-type org-agenda-show-inherited-tags))
3717 (and (eq org-agenda-show-inherited-tags t)
3718 (or (eq org-agenda-use-tag-inheritance t)
3719 (and (listp org-agenda-use-tag-inheritance)
3720 (not (memq org-agenda-type
3721 org-agenda-use-tag-inheritance))))))
3722 (let (mrk)
3723 (save-excursion
3724 (goto-char (point-min))
3725 (while (equal (forward-line) 0)
3726 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3727 (put-text-property (point-at-bol) (point-at-eol)
3728 'tags (org-with-point-at mrk
3729 (delete-dups
3730 (mapcar 'downcase (org-get-tags-at))))))))))
3731 (run-hooks 'org-agenda-finalize-hook)
3732 (when org-agenda-top-headline-filter
3733 (org-agenda-filter-top-headline-apply
3734 org-agenda-top-headline-filter))
3735 (when org-agenda-tag-filter
3736 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3737 (when (get 'org-agenda-tag-filter :preset-filter)
3738 (org-agenda-filter-apply
3739 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3740 (when org-agenda-category-filter
3741 (org-agenda-filter-apply org-agenda-category-filter 'category))
3742 (when (get 'org-agenda-category-filter :preset-filter)
3743 (org-agenda-filter-apply
3744 (get 'org-agenda-category-filter :preset-filter) 'category))
3745 (when org-agenda-regexp-filter
3746 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3747 (when (get 'org-agenda-regexp-filter :preset-filter)
3748 (org-agenda-filter-apply
3749 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3750 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3751
3752 (defun org-agenda-mark-clocking-task ()
3753 "Mark the current clock entry in the agenda if it is present."
3754 ;; We need to widen when `org-agenda-finalize' is called from
3755 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3756 (when org-clock-current-task
3757 (save-restriction
3758 (widen)
3759 (org-agenda-unmark-clocking-task)
3760 (when (marker-buffer org-clock-hd-marker)
3761 (save-excursion
3762 (goto-char (point-min))
3763 (let (s ov)
3764 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3765 (goto-char s)
3766 (when (equal (org-get-at-bol 'org-hd-marker)
3767 org-clock-hd-marker)
3768 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3769 (overlay-put ov 'type 'org-agenda-clocking)
3770 (overlay-put ov 'face 'org-agenda-clocking)
3771 (overlay-put ov 'help-echo
3772 "The clock is running in this item")))))))))
3773
3774 (defun org-agenda-unmark-clocking-task ()
3775 "Unmark the current clocking task."
3776 (mapc (lambda (o)
3777 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3778 (delete-overlay o)))
3779 (overlays-in (point-min) (point-max))))
3780
3781 (defun org-agenda-fontify-priorities ()
3782 "Make highest priority lines bold, and lowest italic."
3783 (interactive)
3784 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3785 (delete-overlay o)))
3786 (overlays-in (point-min) (point-max)))
3787 (save-excursion
3788 (let (b e p ov h l)
3789 (goto-char (point-min))
3790 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3791 (setq h (or (get-char-property (point) 'org-highest-priority)
3792 org-highest-priority)
3793 l (or (get-char-property (point) 'org-lowest-priority)
3794 org-lowest-priority)
3795 p (string-to-char (match-string 1))
3796 b (match-beginning 0)
3797 e (if (eq org-agenda-fontify-priorities 'cookies)
3798 (match-end 0)
3799 (point-at-eol))
3800 ov (make-overlay b e))
3801 (overlay-put
3802 ov 'face
3803 (cons (cond ((org-face-from-face-or-color
3804 'priority nil
3805 (cdr (assoc p org-priority-faces))))
3806 ((and (listp org-agenda-fontify-priorities)
3807 (org-face-from-face-or-color
3808 'priority nil
3809 (cdr (assoc p org-agenda-fontify-priorities)))))
3810 ((equal p l) 'italic)
3811 ((equal p h) 'bold))
3812 'org-priority))
3813 (overlay-put ov 'org-type 'org-priority)))))
3814
3815 (defvar org-depend-tag-blocked)
3816
3817 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3818 "Dim currently blocked TODOs in the agenda display.
3819 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3820 dimming them."
3821 (interactive "P")
3822 (when (org-called-interactively-p 'interactive)
3823 (message "Dim or hide blocked tasks..."))
3824 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3825 (delete-overlay o)))
3826 (overlays-in (point-min) (point-max)))
3827 (save-excursion
3828 (let ((inhibit-read-only t)
3829 (org-depend-tag-blocked nil)
3830 (invis (or (not (null invisible))
3831 (eq org-agenda-dim-blocked-tasks 'invisible)))
3832 org-blocked-by-checkboxes
3833 invis1 b e p ov h l)
3834 (goto-char (point-min))
3835 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3836 (and pos (goto-char (1+ pos))))
3837 (setq org-blocked-by-checkboxes nil invis1 invis)
3838 (let ((marker (org-get-at-bol 'org-hd-marker)))
3839 (when (and marker
3840 (with-current-buffer (marker-buffer marker)
3841 (save-excursion (goto-char marker)
3842 (org-entry-blocked-p))))
3843 (if org-blocked-by-checkboxes (setq invis1 nil))
3844 (setq b (if invis1
3845 (max (point-min) (1- (point-at-bol)))
3846 (point-at-bol))
3847 e (point-at-eol)
3848 ov (make-overlay b e))
3849 (if invis1
3850 (progn (overlay-put ov 'invisible t)
3851 (overlay-put ov 'intangible t))
3852 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3853 (overlay-put ov 'org-type 'org-blocked-todo))))))
3854 (when (org-called-interactively-p 'interactive)
3855 (message "Dim or hide blocked tasks...done")))
3856
3857 (defvar org-agenda-skip-function nil
3858 "Function to be called at each match during agenda construction.
3859 If this function returns nil, the current match should not be skipped.
3860 Otherwise, the function must return a position from where the search
3861 should be continued.
3862 This may also be a Lisp form, it will be evaluated.
3863 Never set this variable using `setq' or so, because then it will apply
3864 to all future agenda commands. If you do want a global skipping condition,
3865 use the option `org-agenda-skip-function-global' instead.
3866 The correct usage for `org-agenda-skip-function' is to bind it with
3867 `let' to scope it dynamically into the agenda-constructing command.
3868 A good way to set it is through options in `org-agenda-custom-commands'.")
3869
3870 (defun org-agenda-skip ()
3871 "Throw to `:skip' in places that should be skipped.
3872 Also moves point to the end of the skipped region, so that search can
3873 continue from there."
3874 (let ((p (point-at-bol)) to)
3875 (when (or
3876 (save-excursion (goto-char p) (looking-at comment-start-skip))
3877 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3878 (get-text-property p :org-archived)
3879 (org-end-of-subtree t))
3880 (and org-agenda-skip-comment-trees
3881 (get-text-property p :org-comment)
3882 (org-end-of-subtree t))
3883 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3884 (org-agenda-skip-eval org-agenda-skip-function)))
3885 (goto-char to))
3886 (org-in-src-block-p t))
3887 (throw :skip t))))
3888
3889 (defun org-agenda-skip-eval (form)
3890 "If FORM is a function or a list, call (or eval) it and return the result.
3891 `save-excursion' and `save-match-data' are wrapped around the call, so point
3892 and match data are returned to the previous state no matter what these
3893 functions do."
3894 (let (fp)
3895 (and form
3896 (or (setq fp (functionp form))
3897 (consp form))
3898 (save-excursion
3899 (save-match-data
3900 (if fp
3901 (funcall form)
3902 (eval form)))))))
3903
3904 (defvar org-agenda-markers nil
3905 "List of all currently active markers created by `org-agenda'.")
3906 (defvar org-agenda-last-marker-time (org-float-time)
3907 "Creation time of the last agenda marker.")
3908
3909 (defun org-agenda-new-marker (&optional pos)
3910 "Return a new agenda marker.
3911 Org-mode keeps a list of these markers and resets them when they are
3912 no longer in use."
3913 (let ((m (copy-marker (or pos (point)))))
3914 (setq org-agenda-last-marker-time (org-float-time))
3915 (if org-agenda-buffer
3916 (with-current-buffer org-agenda-buffer
3917 (push m org-agenda-markers))
3918 (push m org-agenda-markers))
3919 m))
3920
3921 (defun org-agenda-reset-markers ()
3922 "Reset markers created by `org-agenda'."
3923 (while org-agenda-markers
3924 (move-marker (pop org-agenda-markers) nil)))
3925
3926 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3927 "Save relative positions of markers in region.
3928 This check for agenda markers in all agenda buffers currently active."
3929 (dolist (buf (buffer-list))
3930 (with-current-buffer buf
3931 (when (eq major-mode 'org-agenda-mode)
3932 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3933 org-agenda-markers)))))
3934
3935 ;;; Entry text mode
3936
3937 (defun org-agenda-entry-text-show-here ()
3938 "Add some text from the entry as context to the current line."
3939 (let (m txt o)
3940 (setq m (org-get-at-bol 'org-hd-marker))
3941 (unless (marker-buffer m)
3942 (error "No marker points to an entry here"))
3943 (setq txt (concat "\n" (org-no-properties
3944 (org-agenda-get-some-entry-text
3945 m org-agenda-entry-text-maxlines
3946 org-agenda-entry-text-leaders))))
3947 (when (string-match "\\S-" txt)
3948 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3949 (overlay-put o 'evaporate t)
3950 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3951 (overlay-put o 'after-string txt))))
3952
3953 (defun org-agenda-entry-text-show ()
3954 "Add entry context for all agenda lines."
3955 (interactive)
3956 (save-excursion
3957 (goto-char (point-max))
3958 (beginning-of-line 1)
3959 (while (not (bobp))
3960 (when (org-get-at-bol 'org-hd-marker)
3961 (org-agenda-entry-text-show-here))
3962 (beginning-of-line 0))))
3963
3964 (defun org-agenda-entry-text-hide ()
3965 "Remove any shown entry context."
3966 (delq nil
3967 (mapcar (lambda (o)
3968 (if (eq (overlay-get o 'org-overlay-type)
3969 'agenda-entry-content)
3970 (progn (delete-overlay o) t)))
3971 (overlays-in (point-min) (point-max)))))
3972
3973 (defun org-agenda-get-day-face (date)
3974 "Return the face DATE should be displayed with."
3975 (or (and (functionp org-agenda-day-face-function)
3976 (funcall org-agenda-day-face-function date))
3977 (cond ((org-agenda-todayp date)
3978 'org-agenda-date-today)
3979 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3980 'org-agenda-date-weekend)
3981 (t 'org-agenda-date))))
3982
3983 ;;; Agenda timeline
3984
3985 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3986 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
3987
3988 (defun org-timeline (&optional dotodo)
3989 "Show a time-sorted view of the entries in the current org file.
3990 Only entries with a time stamp of today or later will be listed. With
3991 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3992 under the current date.
3993 If the buffer contains an active region, only check the region for
3994 dates."
3995 (interactive "P")
3996 (let* ((dopast t)
3997 (org-agenda-show-log-scoped org-agenda-show-log)
3998 (org-agenda-show-log org-agenda-show-log-scoped)
3999 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4000 (current-buffer))))
4001 (date (calendar-current-date))
4002 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4003 (end (if (org-region-active-p) (region-end) (point-max)))
4004 (day-numbers (org-get-all-dates
4005 beg end 'no-ranges
4006 t org-agenda-show-log-scoped ; always include today
4007 org-timeline-show-empty-dates))
4008 (org-deadline-warning-days 0)
4009 (org-agenda-only-exact-dates t)
4010 (today (org-today))
4011 (past t)
4012 args
4013 s e rtn d emptyp)
4014 (setq org-agenda-redo-command
4015 (list 'let
4016 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4017 (list 'org-switch-to-buffer-other-window (current-buffer))
4018 (list 'org-timeline (list 'quote dotodo))))
4019 (put 'org-agenda-redo-command 'org-lprops nil)
4020 (if (not dopast)
4021 ;; Remove past dates from the list of dates.
4022 (setq day-numbers (delq nil (mapcar (lambda(x)
4023 (if (>= x today) x nil))
4024 day-numbers))))
4025 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4026 (org-compile-prefix-format 'timeline)
4027 (org-set-sorting-strategy 'timeline)
4028 (if org-agenda-show-log-scoped (push :closed args))
4029 (push :timestamp args)
4030 (push :deadline args)
4031 (push :scheduled args)
4032 (push :sexp args)
4033 (if dotodo (push :todo args))
4034 (insert "Timeline of file " entry "\n")
4035 (add-text-properties (point-min) (point)
4036 (list 'face 'org-agenda-structure))
4037 (org-agenda-mark-header-line (point-min))
4038 (while (setq d (pop day-numbers))
4039 (if (and (listp d) (eq (car d) :omitted))
4040 (progn
4041 (setq s (point))
4042 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4043 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4044 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4045 (if (and (>= d today)
4046 dopast
4047 past)
4048 (progn
4049 (setq past nil)
4050 (insert (make-string 79 ?-) "\n")))
4051 (setq date (calendar-gregorian-from-absolute d))
4052 (setq s (point))
4053 (setq rtn (and (not emptyp)
4054 (apply 'org-agenda-get-day-entries entry
4055 date args)))
4056 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4057 (progn
4058 (insert
4059 (if (stringp org-agenda-format-date)
4060 (format-time-string org-agenda-format-date
4061 (org-time-from-absolute date))
4062 (funcall org-agenda-format-date date))
4063 "\n")
4064 (put-text-property s (1- (point)) 'face
4065 (org-agenda-get-day-face date))
4066 (put-text-property s (1- (point)) 'org-date-line t)
4067 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4068 (if (equal d today)
4069 (put-text-property s (1- (point)) 'org-today t))
4070 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4071 (put-text-property s (1- (point)) 'day d)))))
4072 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4073 (point-min)))
4074 (add-text-properties
4075 (point-min) (point-max)
4076 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4077 (org-agenda-finalize)
4078 (setq buffer-read-only t)))
4079
4080 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4081 "Return a list of all relevant day numbers from BEG to END buffer positions.
4082 If NO-RANGES is non-nil, include only the start and end dates of a range,
4083 not every single day in the range. If FORCE-TODAY is non-nil, make
4084 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4085 inactive time stamps (those in square brackets) are included.
4086 When EMPTY is non-nil, also include days without any entries."
4087 (let ((re (concat
4088 (if pre-re pre-re "")
4089 (if inactive org-ts-regexp-both org-ts-regexp)))
4090 dates dates1 date day day1 day2 ts1 ts2 pos)
4091 (if force-today
4092 (setq dates (list (org-today))))
4093 (save-excursion
4094 (goto-char beg)
4095 (while (re-search-forward re end t)
4096 (setq day (time-to-days (org-time-string-to-time
4097 (substring (match-string 1) 0 10)
4098 (current-buffer) (match-beginning 0))))
4099 (or (memq day dates) (push day dates)))
4100 (unless no-ranges
4101 (goto-char beg)
4102 (while (re-search-forward org-tr-regexp end t)
4103 (setq pos (match-beginning 0))
4104 (setq ts1 (substring (match-string 1) 0 10)
4105 ts2 (substring (match-string 2) 0 10)
4106 day1 (time-to-days (org-time-string-to-time
4107 ts1 (current-buffer) pos))
4108 day2 (time-to-days (org-time-string-to-time
4109 ts2 (current-buffer) pos)))
4110 (while (< (setq day1 (1+ day1)) day2)
4111 (or (memq day1 dates) (push day1 dates)))))
4112 (setq dates (sort dates '<))
4113 (when empty
4114 (while (setq day (pop dates))
4115 (setq day2 (car dates))
4116 (push day dates1)
4117 (when (and day2 empty)
4118 (if (or (eq empty t)
4119 (and (numberp empty) (<= (- day2 day) empty)))
4120 (while (< (setq day (1+ day)) day2)
4121 (push (list day) dates1))
4122 (push (cons :omitted (- day2 day)) dates1))))
4123 (setq dates (nreverse dates1)))
4124 dates)))
4125
4126 ;;; Agenda Daily/Weekly
4127
4128 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4129 "Start day for the agenda view.
4130 Custom commands can set this variable in the options section.
4131 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4132 input allowed when reading a date through the Org calendar.
4133 See the docstring of `org-read-date' for details.")
4134 (defvar org-starting-day nil) ; local variable in the agenda buffer
4135 (defvar org-arg-loc nil) ; local variable
4136
4137 (defvar org-agenda-buffer-tmp-name nil)
4138 ;;;###autoload
4139 (defun org-agenda-list (&optional arg start-day span with-hour)
4140 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4141 The view will be for the current day or week, but from the overview buffer
4142 you will be able to go to other days/weeks.
4143
4144 With a numeric prefix argument in an interactive call, the agenda will
4145 span ARG days. Lisp programs should instead specify SPAN to change
4146 the number of days. SPAN defaults to `org-agenda-span'.
4147
4148 START-DAY defaults to TODAY, or to the most recent match for the weekday
4149 given in `org-agenda-start-on-weekday'.
4150
4151 When WITH-HOUR is non-nil, only include scheduled and deadline
4152 items if they have an hour specification like [h]h:mm."
4153 (interactive "P")
4154 (if org-agenda-overriding-arguments
4155 (setq arg (car org-agenda-overriding-arguments)
4156 start-day (nth 1 org-agenda-overriding-arguments)
4157 span (nth 2 org-agenda-overriding-arguments)))
4158 (if (and (integerp arg) (> arg 0))
4159 (setq span arg arg nil))
4160 (catch 'exit
4161 (setq org-agenda-buffer-name
4162 (or org-agenda-buffer-tmp-name
4163 (if org-agenda-sticky
4164 (cond ((and org-keys (stringp org-match))
4165 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4166 (org-keys
4167 (format "*Org Agenda(%s)*" org-keys))
4168 (t "*Org Agenda(a)*")))
4169 org-agenda-buffer-name))
4170 (org-agenda-prepare "Day/Week")
4171 (setq start-day (or start-day org-agenda-start-day))
4172 (if (stringp start-day)
4173 ;; Convert to an absolute day number
4174 (setq start-day (time-to-days (org-read-date nil t start-day))))
4175 (org-compile-prefix-format 'agenda)
4176 (org-set-sorting-strategy 'agenda)
4177 (let* ((span (org-agenda-ndays-to-span
4178 (or span org-agenda-ndays org-agenda-span)))
4179 (today (org-today))
4180 (sd (or start-day today))
4181 (ndays (org-agenda-span-to-ndays span sd))
4182 (org-agenda-start-on-weekday
4183 (if (or (eq ndays 7) (eq ndays 14))
4184 org-agenda-start-on-weekday))
4185 (thefiles (org-agenda-files nil 'ifmode))
4186 (files thefiles)
4187 (start (if (or (null org-agenda-start-on-weekday)
4188 (< ndays 7))
4189 sd
4190 (let* ((nt (calendar-day-of-week
4191 (calendar-gregorian-from-absolute sd)))
4192 (n1 org-agenda-start-on-weekday)
4193 (d (- nt n1)))
4194 (- sd (+ (if (< d 0) 7 0) d)))))
4195 (day-numbers (list start))
4196 (day-cnt 0)
4197 (inhibit-redisplay (not debug-on-error))
4198 (org-agenda-show-log-scoped org-agenda-show-log)
4199 s e rtn rtnall file date d start-pos end-pos todayp
4200 clocktable-start clocktable-end filter)
4201 (setq org-agenda-redo-command
4202 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4203 (dotimes (n (1- ndays))
4204 (push (1+ (car day-numbers)) day-numbers))
4205 (setq day-numbers (nreverse day-numbers))
4206 (setq clocktable-start (car day-numbers)
4207 clocktable-end (1+ (or (org-last day-numbers) 0)))
4208 (org-set-local 'org-starting-day (car day-numbers))
4209 (org-set-local 'org-arg-loc arg)
4210 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4211 (unless org-agenda-compact-blocks
4212 (let* ((d1 (car day-numbers))
4213 (d2 (org-last day-numbers))
4214 (w1 (org-days-to-iso-week d1))
4215 (w2 (org-days-to-iso-week d2)))
4216 (setq s (point))
4217 (if org-agenda-overriding-header
4218 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4219 nil 'face 'org-agenda-structure) "\n")
4220 (insert (org-agenda-span-name span)
4221 "-agenda"
4222 (if (< (- d2 d1) 350)
4223 (if (= w1 w2)
4224 (format " (W%02d)" w1)
4225 (format " (W%02d-W%02d)" w1 w2))
4226 "")
4227 ":\n")))
4228 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4229 'org-date-line t))
4230 (org-agenda-mark-header-line s))
4231 (while (setq d (pop day-numbers))
4232 (setq date (calendar-gregorian-from-absolute d)
4233 s (point))
4234 (if (or (setq todayp (= d today))
4235 (and (not start-pos) (= d sd)))
4236 (setq start-pos (point))
4237 (if (and start-pos (not end-pos))
4238 (setq end-pos (point))))
4239 (setq files thefiles
4240 rtnall nil)
4241 (while (setq file (pop files))
4242 (catch 'nextfile
4243 (org-check-agenda-file file)
4244 (let ((org-agenda-entry-types org-agenda-entry-types))
4245 ;; Starred types override non-starred equivalents
4246 (when (member :deadline* org-agenda-entry-types)
4247 (setq org-agenda-entry-types
4248 (delq :deadline org-agenda-entry-types)))
4249 (when (member :scheduled* org-agenda-entry-types)
4250 (setq org-agenda-entry-types
4251 (delq :scheduled org-agenda-entry-types)))
4252 ;; Honor with-hour
4253 (when with-hour
4254 (when (member :deadline org-agenda-entry-types)
4255 (setq org-agenda-entry-types
4256 (delq :deadline org-agenda-entry-types))
4257 (push :deadline* org-agenda-entry-types))
4258 (when (member :scheduled org-agenda-entry-types)
4259 (setq org-agenda-entry-types
4260 (delq :scheduled org-agenda-entry-types))
4261 (push :scheduled* org-agenda-entry-types)))
4262 (unless org-agenda-include-deadlines
4263 (setq org-agenda-entry-types
4264 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4265 (cond
4266 ((memq org-agenda-show-log-scoped '(only clockcheck))
4267 (setq rtn (org-agenda-get-day-entries
4268 file date :closed)))
4269 (org-agenda-show-log-scoped
4270 (setq rtn (apply 'org-agenda-get-day-entries
4271 file date
4272 (append '(:closed) org-agenda-entry-types))))
4273 (t
4274 (setq rtn (apply 'org-agenda-get-day-entries
4275 file date
4276 org-agenda-entry-types)))))
4277 (setq rtnall (append rtnall rtn)))) ;; all entries
4278 (if org-agenda-include-diary
4279 (let ((org-agenda-search-headline-for-time t))
4280 (require 'diary-lib)
4281 (setq rtn (org-get-entries-from-diary date))
4282 (setq rtnall (append rtnall rtn))))
4283 (if (or rtnall org-agenda-show-all-dates)
4284 (progn
4285 (setq day-cnt (1+ day-cnt))
4286 (insert
4287 (if (stringp org-agenda-format-date)
4288 (format-time-string org-agenda-format-date
4289 (org-time-from-absolute date))
4290 (funcall org-agenda-format-date date))
4291 "\n")
4292 (put-text-property s (1- (point)) 'face
4293 (org-agenda-get-day-face date))
4294 (put-text-property s (1- (point)) 'org-date-line t)
4295 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4296 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4297 (when todayp
4298 (put-text-property s (1- (point)) 'org-today t))
4299 (setq rtnall
4300 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4301 (if rtnall (insert ;; all entries
4302 (org-agenda-finalize-entries rtnall 'agenda)
4303 "\n"))
4304 (put-text-property s (1- (point)) 'day d)
4305 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4306 (when (and org-agenda-clockreport-mode clocktable-start)
4307 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4308 ;; the above line is to ensure the restricted range!
4309 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4310 tbl)
4311 (setq p (org-plist-delete p :block))
4312 (setq p (plist-put p :tstart clocktable-start))
4313 (setq p (plist-put p :tend clocktable-end))
4314 (setq p (plist-put p :scope 'agenda))
4315 (setq tbl (apply 'org-clock-get-clocktable p))
4316 (insert tbl)))
4317 (goto-char (point-min))
4318 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4319 (unless (and (pos-visible-in-window-p (point-min))
4320 (pos-visible-in-window-p (point-max)))
4321 (goto-char (1- (point-max)))
4322 (recenter -1)
4323 (if (not (pos-visible-in-window-p (or start-pos 1)))
4324 (progn
4325 (goto-char (or start-pos 1))
4326 (recenter 1))))
4327 (goto-char (or start-pos 1))
4328 (add-text-properties (point-min) (point-max)
4329 `(org-agenda-type agenda
4330 org-last-args (,arg ,start-day ,span)
4331 org-redo-cmd ,org-agenda-redo-command
4332 org-series-cmd ,org-cmd))
4333 (if (eq org-agenda-show-log-scoped 'clockcheck)
4334 (org-agenda-show-clocking-issues))
4335 (org-agenda-finalize)
4336 (setq buffer-read-only t)
4337 (message ""))))
4338
4339 (defun org-agenda-ndays-to-span (n)
4340 "Return a span symbol for a span of N days, or N if none matches."
4341 (cond ((symbolp n) n)
4342 ((= n 1) 'day)
4343 ((= n 7) 'week)
4344 ((= n 14) 'fortnight)
4345 (t n)))
4346
4347 (defun org-agenda-span-to-ndays (span &optional start-day)
4348 "Return ndays from SPAN, possibly starting at START-DAY.
4349 START-DAY is an absolute time value."
4350 (cond ((numberp span) span)
4351 ((eq span 'day) 1)
4352 ((eq span 'week) 7)
4353 ((eq span 'fortnight) 14)
4354 ((eq span 'month)
4355 (let ((date (calendar-gregorian-from-absolute start-day)))
4356 (calendar-last-day-of-month (car date) (caddr date))))
4357 ((eq span 'year)
4358 (let ((date (calendar-gregorian-from-absolute start-day)))
4359 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4360
4361 (defun org-agenda-span-name (span)
4362 "Return a SPAN name."
4363 (if (null span)
4364 ""
4365 (if (symbolp span)
4366 (capitalize (symbol-name span))
4367 (format "%d days" span))))
4368
4369 ;;; Agenda word search
4370
4371 (defvar org-agenda-search-history nil)
4372
4373 (defvar org-search-syntax-table nil
4374 "Special syntax table for org-mode search.
4375 In this table, we have single quotes not as word constituents, to
4376 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4377
4378 (defvar org-mode-syntax-table) ; From org.el
4379 (defun org-search-syntax-table ()
4380 (unless org-search-syntax-table
4381 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4382 (modify-syntax-entry ?' "." org-search-syntax-table)
4383 (modify-syntax-entry ?` "." org-search-syntax-table))
4384 org-search-syntax-table)
4385
4386 (defvar org-agenda-last-search-view-search-was-boolean nil)
4387
4388 ;;;###autoload
4389 (defun org-search-view (&optional todo-only string edit-at)
4390 "Show all entries that contain a phrase or words or regular expressions.
4391
4392 With optional prefix argument TODO-ONLY, only consider entries that are
4393 TODO entries. The argument STRING can be used to pass a default search
4394 string into this function. If EDIT-AT is non-nil, it means that the
4395 user should get a chance to edit this string, with cursor at position
4396 EDIT-AT.
4397
4398 The search string can be viewed either as a phrase that should be found as
4399 is, or it can be broken into a number of snippets, each of which must match
4400 in a Boolean way to select an entry. The default depends on the variable
4401 `org-agenda-search-view-always-boolean'.
4402 Even if this is turned off (the default) you can always switch to
4403 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4404
4405 The default is a direct search of the whole phrase, where each space in
4406 the search string can expand to an arbitrary amount of whitespace,
4407 including newlines.
4408
4409 If using a Boolean search, the search string is split on whitespace and
4410 each snippet is searched separately, with logical AND to select an entry.
4411 Words prefixed with a minus must *not* occur in the entry. Words without
4412 a prefix or prefixed with a plus must occur in the entry. Matching is
4413 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4414 match whole words, not parts of a word) if
4415 `org-agenda-search-view-force-full-words' is set (default is nil).
4416
4417 Boolean search snippets enclosed by curly braces are interpreted as
4418 regular expressions that must or (when preceded with \"-\") must not
4419 match in the entry. Snippets enclosed into double quotes will be taken
4420 as a whole, to include whitespace.
4421
4422 - If the search string starts with an asterisk, search only in headlines.
4423 - If (possibly after the leading star) the search string starts with an
4424 exclamation mark, this also means to look at TODO entries only, an effect
4425 that can also be achieved with a prefix argument.
4426 - If (possibly after star and exclamation mark) the search string starts
4427 with a colon, this will mean that the (non-regexp) snippets of the
4428 Boolean search must match as full words.
4429
4430 This command searches the agenda files, and in addition the files listed
4431 in `org-agenda-text-search-extra-files'."
4432 (interactive "P")
4433 (if org-agenda-overriding-arguments
4434 (setq todo-only (car org-agenda-overriding-arguments)
4435 string (nth 1 org-agenda-overriding-arguments)
4436 edit-at (nth 2 org-agenda-overriding-arguments)))
4437 (let* ((props (list 'face nil
4438 'done-face 'org-agenda-done
4439 'org-not-done-regexp org-not-done-regexp
4440 'org-todo-regexp org-todo-regexp
4441 'org-complex-heading-regexp org-complex-heading-regexp
4442 'mouse-face 'highlight
4443 'help-echo (format "mouse-2 or RET jump to location")))
4444 (full-words org-agenda-search-view-force-full-words)
4445 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4446 regexp rtn rtnall files file pos inherited-tags
4447 marker category category-pos level tags c neg re boolean
4448 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4449 (unless (and (not edit-at)
4450 (stringp string)
4451 (string-match "\\S-" string))
4452 (setq string (read-string
4453 (if org-agenda-search-view-always-boolean
4454 "[+-]Word/{Regexp} ...: "
4455 "Phrase or [+-]Word/{Regexp} ...: ")
4456 (cond
4457 ((integerp edit-at) (cons string edit-at))
4458 (edit-at string))
4459 'org-agenda-search-history)))
4460 (catch 'exit
4461 (if org-agenda-sticky
4462 (setq org-agenda-buffer-name
4463 (if (stringp string)
4464 (format "*Org Agenda(%s:%s)*"
4465 (or org-keys (or (and todo-only "S") "s")) string)
4466 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4467 (org-agenda-prepare "SEARCH")
4468 (org-compile-prefix-format 'search)
4469 (org-set-sorting-strategy 'search)
4470 (setq org-agenda-redo-command
4471 (list 'org-search-view (if todo-only t nil)
4472 (list 'if 'current-prefix-arg nil string)))
4473 (setq org-agenda-query-string string)
4474 (if (equal (string-to-char string) ?*)
4475 (setq hdl-only t
4476 words (substring string 1))
4477 (setq words string))
4478 (when (equal (string-to-char words) ?!)
4479 (setq todo-only t
4480 words (substring words 1)))
4481 (when (equal (string-to-char words) ?:)
4482 (setq full-words t
4483 words (substring words 1)))
4484 (if (or org-agenda-search-view-always-boolean
4485 (member (string-to-char words) '(?- ?+ ?\{)))
4486 (setq boolean t))
4487 (setq words (org-split-string words))
4488 (let (www w)
4489 (while (setq w (pop words))
4490 (while (and (string-match "\\\\\\'" w) words)
4491 (setq w (concat (substring w 0 -1) " " (pop words))))
4492 (push w www))
4493 (setq words (nreverse www) www nil)
4494 (while (setq w (pop words))
4495 (when (and (string-match "\\`[-+]?{" w)
4496 (not (string-match "}\\'" w)))
4497 (while (and words (not (string-match "}\\'" (car words))))
4498 (setq w (concat w " " (pop words))))
4499 (setq w (concat w " " (pop words))))
4500 (push w www))
4501 (setq words (nreverse www)))
4502 (setq org-agenda-last-search-view-search-was-boolean boolean)
4503 (when boolean
4504 (let (wds w)
4505 (while (setq w (pop words))
4506 (if (or (equal (substring w 0 1) "\"")
4507 (and (> (length w) 1)
4508 (member (substring w 0 1) '("+" "-"))
4509 (equal (substring w 1 2) "\"")))
4510 (while (and words (not (equal (substring w -1) "\"")))
4511 (setq w (concat w " " (pop words)))))
4512 (and (string-match "\\`\\([-+]?\\)\"" w)
4513 (setq w (replace-match "\\1" nil nil w)))
4514 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4515 (push w wds))
4516 (setq words (nreverse wds))))
4517 (if boolean
4518 (mapc (lambda (w)
4519 (setq c (string-to-char w))
4520 (if (equal c ?-)
4521 (setq neg t w (substring w 1))
4522 (if (equal c ?+)
4523 (setq neg nil w (substring w 1))
4524 (setq neg nil)))
4525 (if (string-match "\\`{.*}\\'" w)
4526 (setq re (substring w 1 -1))
4527 (if full-words
4528 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4529 (setq re (regexp-quote (downcase w)))))
4530 (if neg (push re regexps-) (push re regexps+)))
4531 words)
4532 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4533 regexps+))
4534 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4535 (if (not regexps+)
4536 (setq regexp org-outline-regexp-bol)
4537 (setq regexp (pop regexps+))
4538 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4539 regexp))))
4540 (setq files (org-agenda-files nil 'ifmode))
4541 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4542 (pop org-agenda-text-search-extra-files)
4543 (setq files (org-add-archive-files files)))
4544 (setq files (append files org-agenda-text-search-extra-files)
4545 rtnall nil)
4546 (while (setq file (pop files))
4547 (setq ee nil)
4548 (catch 'nextfile
4549 (org-check-agenda-file file)
4550 (setq buffer (if (file-exists-p file)
4551 (org-get-agenda-file-buffer file)
4552 (error "No such file %s" file)))
4553 (if (not buffer)
4554 ;; If file does not exist, make sure an error message is sent
4555 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4556 file))))
4557 (with-current-buffer buffer
4558 (with-syntax-table (org-search-syntax-table)
4559 (unless (derived-mode-p 'org-mode)
4560 (error "Agenda file %s is not in `org-mode'" file))
4561 (let ((case-fold-search t))
4562 (save-excursion
4563 (save-restriction
4564 (if (eq buffer org-agenda-restrict)
4565 (narrow-to-region org-agenda-restrict-begin
4566 org-agenda-restrict-end)
4567 (widen))
4568 (goto-char (point-min))
4569 (unless (or (org-at-heading-p)
4570 (outline-next-heading))
4571 (throw 'nextfile t))
4572 (goto-char (max (point-min) (1- (point))))
4573 (while (re-search-forward regexp nil t)
4574 (org-back-to-heading t)
4575 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4576 (> (org-reduced-level (org-outline-level))
4577 org-agenda-search-view-max-outline-level)
4578 (forward-line -1)
4579 (outline-back-to-heading t)))
4580 (skip-chars-forward "* ")
4581 (setq beg (point-at-bol)
4582 beg1 (point)
4583 end (progn
4584 (outline-next-heading)
4585 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4586 (> (org-reduced-level (org-outline-level))
4587 org-agenda-search-view-max-outline-level)
4588 (forward-line 1)
4589 (outline-next-heading)))
4590 (point)))
4591
4592 (catch :skip
4593 (goto-char beg)
4594 (org-agenda-skip)
4595 (setq str (buffer-substring-no-properties
4596 (point-at-bol)
4597 (if hdl-only (point-at-eol) end)))
4598 (mapc (lambda (wr) (when (string-match wr str)
4599 (goto-char (1- end))
4600 (throw :skip t)))
4601 regexps-)
4602 (mapc (lambda (wr) (unless (string-match wr str)
4603 (goto-char (1- end))
4604 (throw :skip t)))
4605 (if todo-only
4606 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4607 regexps+)
4608 regexps+))
4609 (goto-char beg)
4610 (setq marker (org-agenda-new-marker (point))
4611 category (org-get-category)
4612 level (make-string (org-reduced-level (org-outline-level)) ? )
4613 category-pos (get-text-property (point) 'org-category-position)
4614 inherited-tags
4615 (or (eq org-agenda-show-inherited-tags 'always)
4616 (and (listp org-agenda-show-inherited-tags)
4617 (memq 'todo org-agenda-show-inherited-tags))
4618 (and (eq org-agenda-show-inherited-tags t)
4619 (or (eq org-agenda-use-tag-inheritance t)
4620 (memq 'todo org-agenda-use-tag-inheritance))))
4621 tags (org-get-tags-at nil (not inherited-tags))
4622 txt (org-agenda-format-item
4623 ""
4624 (buffer-substring-no-properties
4625 beg1 (point-at-eol))
4626 level category tags t))
4627 (org-add-props txt props
4628 'org-marker marker 'org-hd-marker marker
4629 'org-todo-regexp org-todo-regexp
4630 'level level
4631 'org-complex-heading-regexp org-complex-heading-regexp
4632 'priority 1000 'org-category category
4633 'org-category-position category-pos
4634 'type "search")
4635 (push txt ee)
4636 (goto-char (1- end))))))))))
4637 (setq rtn (nreverse ee))
4638 (setq rtnall (append rtnall rtn)))
4639 (if org-agenda-overriding-header
4640 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4641 nil 'face 'org-agenda-structure) "\n")
4642 (insert "Search words: ")
4643 (add-text-properties (point-min) (1- (point))
4644 (list 'face 'org-agenda-structure))
4645 (setq pos (point))
4646 (insert string "\n")
4647 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4648 (setq pos (point))
4649 (unless org-agenda-multi
4650 (insert (substitute-command-keys
4651 "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n"))
4652 (add-text-properties pos (1- (point))
4653 (list 'face 'org-agenda-structure))))
4654 (org-agenda-mark-header-line (point-min))
4655 (when rtnall
4656 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4657 (goto-char (point-min))
4658 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4659 (add-text-properties (point-min) (point-max)
4660 `(org-agenda-type search
4661 org-last-args (,todo-only ,string ,edit-at)
4662 org-redo-cmd ,org-agenda-redo-command
4663 org-series-cmd ,org-cmd))
4664 (org-agenda-finalize)
4665 (setq buffer-read-only t))))
4666
4667 ;;; Agenda TODO list
4668
4669 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4670 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4671 (concat
4672 (if (or (equal keywords "ALL") (not keywords))
4673 (propertize "ALL" 'face 'warning)
4674 (mapconcat
4675 (lambda (kw)
4676 (propertize kw 'face (org-get-todo-face kw)))
4677 (org-split-string keywords "|")
4678 "|"))
4679 "\n"))
4680
4681 (defvar org-select-this-todo-keyword nil)
4682 (defvar org-last-arg nil)
4683
4684 ;;;###autoload
4685 (defun org-todo-list (&optional arg)
4686 "Show all (not done) TODO entries from all agenda file in a single list.
4687 The prefix arg can be used to select a specific TODO keyword and limit
4688 the list to these. When using \\[universal-argument], you will be prompted
4689 for a keyword. A numeric prefix directly selects the Nth keyword in
4690 `org-todo-keywords-1'."
4691 (interactive "P")
4692 (if org-agenda-overriding-arguments
4693 (setq arg org-agenda-overriding-arguments))
4694 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4695 (let* ((today (org-today))
4696 (date (calendar-gregorian-from-absolute today))
4697 (kwds org-todo-keywords-for-agenda)
4698 (completion-ignore-case t)
4699 (org-select-this-todo-keyword
4700 (if (stringp arg) arg
4701 (and arg (integerp arg) (> arg 0)
4702 (nth (1- arg) kwds))))
4703 rtn rtnall files file pos)
4704 (when (equal arg '(4))
4705 (setq org-select-this-todo-keyword
4706 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4707 (mapcar 'list kwds) nil nil)))
4708 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4709 (catch 'exit
4710 (if org-agenda-sticky
4711 (setq org-agenda-buffer-name
4712 (if (stringp org-select-this-todo-keyword)
4713 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4714 org-select-this-todo-keyword)
4715 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4716 (org-agenda-prepare "TODO")
4717 (org-compile-prefix-format 'todo)
4718 (org-set-sorting-strategy 'todo)
4719 (setq org-agenda-redo-command
4720 `(org-todo-list (or (and (numberp current-prefix-arg)
4721 current-prefix-arg)
4722 ,org-select-this-todo-keyword
4723 current-prefix-arg ,arg)))
4724 (setq files (org-agenda-files nil 'ifmode)
4725 rtnall nil)
4726 (while (setq file (pop files))
4727 (catch 'nextfile
4728 (org-check-agenda-file file)
4729 (setq rtn (org-agenda-get-day-entries file date :todo))
4730 (setq rtnall (append rtnall rtn))))
4731 (if org-agenda-overriding-header
4732 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4733 nil 'face 'org-agenda-structure) "\n")
4734 (insert "Global list of TODO items of type: ")
4735 (add-text-properties (point-min) (1- (point))
4736 (list 'face 'org-agenda-structure
4737 'short-heading
4738 (concat "ToDo: "
4739 (or org-select-this-todo-keyword "ALL"))))
4740 (org-agenda-mark-header-line (point-min))
4741 (insert (org-agenda-propertize-selected-todo-keywords
4742 org-select-this-todo-keyword))
4743 (setq pos (point))
4744 (unless org-agenda-multi
4745 (insert (substitute-command-keys "Available with `N r': (0)[ALL]"))
4746 (let ((n 0) s)
4747 (mapc (lambda (x)
4748 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4749 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4750 (insert "\n "))
4751 (insert " " s))
4752 kwds))
4753 (insert "\n"))
4754 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4755 (org-agenda-mark-header-line (point-min))
4756 (when rtnall
4757 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4758 (goto-char (point-min))
4759 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4760 (add-text-properties (point-min) (point-max)
4761 `(org-agenda-type todo
4762 org-last-args ,arg
4763 org-redo-cmd ,org-agenda-redo-command
4764 org-series-cmd ,org-cmd))
4765 (org-agenda-finalize)
4766 (setq buffer-read-only t))))
4767
4768 ;;; Agenda tags match
4769
4770 ;;;###autoload
4771 (defun org-tags-view (&optional todo-only match)
4772 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4773 The prefix arg TODO-ONLY limits the search to TODO entries."
4774 (interactive "P")
4775 (if org-agenda-overriding-arguments
4776 (setq todo-only (car org-agenda-overriding-arguments)
4777 match (nth 1 org-agenda-overriding-arguments)))
4778 (let* ((org-tags-match-list-sublevels
4779 org-tags-match-list-sublevels)
4780 (completion-ignore-case t)
4781 rtn rtnall files file pos matcher
4782 buffer)
4783 (when (and (stringp match) (not (string-match "\\S-" match)))
4784 (setq match nil))
4785 (catch 'exit
4786 (if org-agenda-sticky
4787 (setq org-agenda-buffer-name
4788 (if (stringp match)
4789 (format "*Org Agenda(%s:%s)*"
4790 (or org-keys (or (and todo-only "M") "m")) match)
4791 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4792 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4793 ;; expanding tags within `org-make-tags-matcher'
4794 (org-agenda-prepare (concat "TAGS " match))
4795 (setq matcher (org-make-tags-matcher match)
4796 match (car matcher) matcher (cdr matcher))
4797 (org-compile-prefix-format 'tags)
4798 (org-set-sorting-strategy 'tags)
4799 (setq org-agenda-query-string match)
4800 (setq org-agenda-redo-command
4801 (list 'org-tags-view `(quote ,todo-only)
4802 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4803 (setq files (org-agenda-files nil 'ifmode)
4804 rtnall nil)
4805 (while (setq file (pop files))
4806 (catch 'nextfile
4807 (org-check-agenda-file file)
4808 (setq buffer (if (file-exists-p file)
4809 (org-get-agenda-file-buffer file)
4810 (error "No such file %s" file)))
4811 (if (not buffer)
4812 ;; If file does not exist, error message to agenda
4813 (setq rtn (list
4814 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4815 rtnall (append rtnall rtn))
4816 (with-current-buffer buffer
4817 (unless (derived-mode-p 'org-mode)
4818 (error "Agenda file %s is not in `org-mode'" file))
4819 (save-excursion
4820 (save-restriction
4821 (if (eq buffer org-agenda-restrict)
4822 (narrow-to-region org-agenda-restrict-begin
4823 org-agenda-restrict-end)
4824 (widen))
4825 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4826 (setq rtnall (append rtnall rtn))))))))
4827 (if org-agenda-overriding-header
4828 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4829 nil 'face 'org-agenda-structure) "\n")
4830 (insert "Headlines with TAGS match: ")
4831 (add-text-properties (point-min) (1- (point))
4832 (list 'face 'org-agenda-structure
4833 'short-heading
4834 (concat "Match: " match)))
4835 (setq pos (point))
4836 (insert match "\n")
4837 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4838 (setq pos (point))
4839 (unless org-agenda-multi
4840 (insert (substitute-command-keys
4841 "Press `C-u r' to search again with new search string\n")))
4842 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4843 (org-agenda-mark-header-line (point-min))
4844 (when rtnall
4845 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4846 (goto-char (point-min))
4847 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4848 (add-text-properties (point-min) (point-max)
4849 `(org-agenda-type tags
4850 org-last-args (,todo-only ,match)
4851 org-redo-cmd ,org-agenda-redo-command
4852 org-series-cmd ,org-cmd))
4853 (org-agenda-finalize)
4854 (setq buffer-read-only t))))
4855
4856 ;;; Agenda Finding stuck projects
4857
4858 (defvar org-agenda-skip-regexp nil
4859 "Regular expression used in skipping subtrees for the agenda.
4860 This is basically a temporary global variable that can be set and then
4861 used by user-defined selections using `org-agenda-skip-function'.")
4862
4863 (defvar org-agenda-overriding-header nil
4864 "When set during agenda, todo and tags searches it replaces the header.
4865 This variable should not be set directly, but custom commands can bind it
4866 in the options section.")
4867
4868 (defun org-agenda-skip-entry-when-regexp-matches ()
4869 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4870 If yes, it returns the end position of this entry, causing agenda commands
4871 to skip the entry but continuing the search in the subtree. This is a
4872 function that can be put into `org-agenda-skip-function' for the duration
4873 of a command."
4874 (let ((end (save-excursion (org-end-of-subtree t)))
4875 skip)
4876 (save-excursion
4877 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4878 (and skip end)))
4879
4880 (defun org-agenda-skip-subtree-when-regexp-matches ()
4881 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4882 If yes, it returns the end position of this tree, causing agenda commands
4883 to skip this subtree. This is a function that can be put into
4884 `org-agenda-skip-function' for the duration of a command."
4885 (let ((end (save-excursion (org-end-of-subtree t)))
4886 skip)
4887 (save-excursion
4888 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4889 (and skip end)))
4890
4891 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4892 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4893 If yes, it returns the end position of the current entry (NOT the tree),
4894 causing agenda commands to skip the entry but continuing the search in
4895 the subtree. This is a function that can be put into
4896 `org-agenda-skip-function' for the duration of a command. An important
4897 use of this function is for the stuck project list."
4898 (let ((end (save-excursion (org-end-of-subtree t)))
4899 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4900 skip)
4901 (save-excursion
4902 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4903 (and skip entry-end)))
4904
4905 (defun org-agenda-skip-entry-if (&rest conditions)
4906 "Skip entry if any of CONDITIONS is true.
4907 See `org-agenda-skip-if' for details."
4908 (org-agenda-skip-if nil conditions))
4909
4910 (defun org-agenda-skip-subtree-if (&rest conditions)
4911 "Skip subtree if any of CONDITIONS is true.
4912 See `org-agenda-skip-if' for details."
4913 (org-agenda-skip-if t conditions))
4914
4915 (defun org-agenda-skip-if (subtree conditions)
4916 "Checks current entity for CONDITIONS.
4917 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4918 the entry (i.e. the text before the next heading) is checked.
4919
4920 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4921 from different tests. Valid conditions are:
4922
4923 scheduled Check if there is a scheduled cookie
4924 notscheduled Check if there is no scheduled cookie
4925 deadline Check if there is a deadline
4926 notdeadline Check if there is no deadline
4927 timestamp Check if there is a timestamp (also deadline or scheduled)
4928 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4929 regexp Check if regexp matches
4930 notregexp Check if regexp does not match.
4931 todo Check if TODO keyword matches
4932 nottodo Check if TODO keyword does not match
4933
4934 The regexp is taken from the conditions list, it must come right after
4935 the `regexp' or `notregexp' element.
4936
4937 `todo' and `nottodo' accept as an argument a list of todo
4938 keywords, which may include \"*\" to match any todo keyword.
4939
4940 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4941
4942 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4943
4944 Instead of a list, a keyword class may be given. For example:
4945
4946 (org-agenda-skip-entry-if 'nottodo 'done)
4947
4948 would skip entries that haven't been marked with any of \"DONE\"
4949 keywords. Possible classes are: `todo', `done', `any'.
4950
4951 If any of these conditions is met, this function returns the end point of
4952 the entity, causing the search to continue from there. This is a function
4953 that can be put into `org-agenda-skip-function' for the duration of a command."
4954 (let (beg end m)
4955 (org-back-to-heading t)
4956 (setq beg (point)
4957 end (if subtree
4958 (progn (org-end-of-subtree t) (point))
4959 (progn (outline-next-heading) (1- (point)))))
4960 (goto-char beg)
4961 (and
4962 (or
4963 (and (memq 'scheduled conditions)
4964 (re-search-forward org-scheduled-time-regexp end t))
4965 (and (memq 'notscheduled conditions)
4966 (not (re-search-forward org-scheduled-time-regexp end t)))
4967 (and (memq 'deadline conditions)
4968 (re-search-forward org-deadline-time-regexp end t))
4969 (and (memq 'notdeadline conditions)
4970 (not (re-search-forward org-deadline-time-regexp end t)))
4971 (and (memq 'timestamp conditions)
4972 (re-search-forward org-ts-regexp end t))
4973 (and (memq 'nottimestamp conditions)
4974 (not (re-search-forward org-ts-regexp end t)))
4975 (and (setq m (memq 'regexp conditions))
4976 (stringp (nth 1 m))
4977 (re-search-forward (nth 1 m) end t))
4978 (and (setq m (memq 'notregexp conditions))
4979 (stringp (nth 1 m))
4980 (not (re-search-forward (nth 1 m) end t)))
4981 (and (or
4982 (setq m (memq 'nottodo conditions))
4983 (setq m (memq 'todo-unblocked conditions))
4984 (setq m (memq 'nottodo-unblocked conditions))
4985 (setq m (memq 'todo conditions)))
4986 (org-agenda-skip-if-todo m end)))
4987 end)))
4988
4989 (defun org-agenda-skip-if-todo (args end)
4990 "Helper function for `org-agenda-skip-if', do not use it directly.
4991 ARGS is a list with first element either `todo', `nottodo',
4992 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4993 a list of TODO keywords, or a state symbol `todo' or `done' or
4994 `any'."
4995 (let ((kw (car args))
4996 (arg (cadr args))
4997 todo-wds todo-re)
4998 (setq todo-wds
4999 (org-uniquify
5000 (cond
5001 ((listp arg) ;; list of keywords
5002 (if (member "*" arg)
5003 (mapcar 'substring-no-properties org-todo-keywords-1)
5004 arg))
5005 ((symbolp arg) ;; keyword class name
5006 (cond
5007 ((eq arg 'todo)
5008 (org-delete-all org-done-keywords
5009 (mapcar 'substring-no-properties
5010 org-todo-keywords-1)))
5011 ((eq arg 'done) org-done-keywords)
5012 ((eq arg 'any)
5013 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5014 (setq todo-re
5015 (concat "^\\*+[ \t]+\\<\\("
5016 (mapconcat 'identity todo-wds "\\|")
5017 "\\)\\>"))
5018 (cond
5019 ((eq kw 'todo) (re-search-forward todo-re end t))
5020 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5021 ((eq kw 'todo-unblocked)
5022 (catch 'unblocked
5023 (while (re-search-forward todo-re end t)
5024 (or (org-entry-blocked-p) (throw 'unblocked t)))
5025 nil))
5026 ((eq kw 'nottodo-unblocked)
5027 (catch 'unblocked
5028 (while (re-search-forward todo-re end t)
5029 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5030 t))
5031 )))
5032
5033 ;;;###autoload
5034 (defun org-agenda-list-stuck-projects (&rest ignore)
5035 "Create agenda view for projects that are stuck.
5036 Stuck projects are project that have no next actions. For the definitions
5037 of what a project is and how to check if it stuck, customize the variable
5038 `org-stuck-projects'."
5039 (interactive)
5040 (let* ((org-agenda-skip-function
5041 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5042 ;; We could have used org-agenda-skip-if here.
5043 (org-agenda-overriding-header
5044 (or org-agenda-overriding-header "List of stuck projects: "))
5045 (matcher (nth 0 org-stuck-projects))
5046 (todo (nth 1 org-stuck-projects))
5047 (todo-wds (if (member "*" todo)
5048 (progn
5049 (org-agenda-prepare-buffers (org-agenda-files
5050 nil 'ifmode))
5051 (org-delete-all
5052 org-done-keywords-for-agenda
5053 (copy-sequence org-todo-keywords-for-agenda)))
5054 todo))
5055 (todo-re (concat "^\\*+[ \t]+\\("
5056 (mapconcat 'identity todo-wds "\\|")
5057 "\\)\\>"))
5058 (tags (nth 2 org-stuck-projects))
5059 (tags-re (if (member "*" tags)
5060 (concat org-outline-regexp-bol
5061 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5062 (if tags
5063 (concat org-outline-regexp-bol
5064 ".*:\\("
5065 (mapconcat 'identity tags "\\|")
5066 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5067 (gen-re (nth 3 org-stuck-projects))
5068 (re-list
5069 (delq nil
5070 (list
5071 (if todo todo-re)
5072 (if tags tags-re)
5073 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5074 gen-re)))))
5075 (setq org-agenda-skip-regexp
5076 (if re-list
5077 (mapconcat 'identity re-list "\\|")
5078 (error "No information how to identify unstuck projects")))
5079 (org-tags-view nil matcher)
5080 (setq org-agenda-buffer-name (buffer-name))
5081 (with-current-buffer org-agenda-buffer-name
5082 (setq org-agenda-redo-command
5083 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5084
5085 ;;; Diary integration
5086
5087 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5088 (defvar diary-list-entries-hook)
5089 (defvar diary-time-regexp)
5090 (defun org-get-entries-from-diary (date)
5091 "Get the (Emacs Calendar) diary entries for DATE."
5092 (require 'diary-lib)
5093 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5094 (diary-display-function 'diary-fancy-display)
5095 (pop-up-frames nil)
5096 (diary-list-entries-hook
5097 (cons 'org-diary-default-entry diary-list-entries-hook))
5098 (diary-file-name-prefix nil) ; turn this feature off
5099 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5100 entries
5101 (org-disable-agenda-to-diary t))
5102 (save-excursion
5103 (save-window-excursion
5104 (funcall (if (fboundp 'diary-list-entries)
5105 'diary-list-entries 'list-diary-entries)
5106 date 1)))
5107 (if (not (get-buffer diary-fancy-buffer))
5108 (setq entries nil)
5109 (with-current-buffer diary-fancy-buffer
5110 (setq buffer-read-only nil)
5111 (if (zerop (buffer-size))
5112 ;; No entries
5113 (setq entries nil)
5114 ;; Omit the date and other unnecessary stuff
5115 (org-agenda-cleanup-fancy-diary)
5116 ;; Add prefix to each line and extend the text properties
5117 (if (zerop (buffer-size))
5118 (setq entries nil)
5119 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5120 (setq entries
5121 (with-temp-buffer
5122 (insert entries) (goto-char (point-min))
5123 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5124 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5125 (replace-match (concat "; " (match-string 1)))))
5126 (buffer-string)))))
5127 (set-buffer-modified-p nil)
5128 (kill-buffer diary-fancy-buffer)))
5129 (when entries
5130 (setq entries (org-split-string entries "\n"))
5131 (setq entries
5132 (mapcar
5133 (lambda (x)
5134 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5135 ;; Extend the text properties to the beginning of the line
5136 (org-add-props x (text-properties-at (1- (length x)) x)
5137 'type "diary" 'date date 'face 'org-agenda-diary))
5138 entries)))))
5139
5140 (defvar org-agenda-cleanup-fancy-diary-hook nil
5141 "Hook run when the fancy diary buffer is cleaned up.")
5142
5143 (defun org-agenda-cleanup-fancy-diary ()
5144 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5145 This gets rid of the date, the underline under the date, and
5146 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5147 date. It also removes lines that contain only whitespace."
5148 (goto-char (point-min))
5149 (if (looking-at ".*?:[ \t]*")
5150 (progn
5151 (replace-match "")
5152 (re-search-forward "\n=+$" nil t)
5153 (replace-match "")
5154 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5155 (re-search-forward "\n=+$" nil t)
5156 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5157 (goto-char (point-min))
5158 (while (re-search-forward "^ +\n" nil t)
5159 (replace-match ""))
5160 (goto-char (point-min))
5161 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5162 (replace-match ""))
5163 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5164
5165 ;; Make sure entries from the diary have the right text properties.
5166 (eval-after-load "diary-lib"
5167 '(if (boundp 'diary-modify-entry-list-string-function)
5168 ;; We can rely on the hook, nothing to do
5169 nil
5170 ;; Hook not available, must use advice to make this work
5171 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5172 "Make the position visible."
5173 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5174 (stringp string)
5175 buffer-file-name)
5176 (setq string (org-modify-diary-entry-string string))))))
5177
5178 (defun org-modify-diary-entry-string (string)
5179 "Add text properties to string, allowing org-mode to act on it."
5180 (org-add-props string nil
5181 'mouse-face 'highlight
5182 'help-echo (if buffer-file-name
5183 (format "mouse-2 or RET jump to diary file %s"
5184 (abbreviate-file-name buffer-file-name))
5185 "")
5186 'org-agenda-diary-link t
5187 'org-marker (org-agenda-new-marker (point-at-bol))))
5188
5189 (defun org-diary-default-entry ()
5190 "Add a dummy entry to the diary.
5191 Needed to avoid empty dates which mess up holiday display."
5192 ;; Catch the error if dealing with the new add-to-diary-alist
5193 (when org-disable-agenda-to-diary
5194 (condition-case nil
5195 (org-add-to-diary-list original-date "Org-mode dummy" "")
5196 (error
5197 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5198
5199 (defun org-add-to-diary-list (&rest args)
5200 (if (fboundp 'diary-add-to-list)
5201 (apply 'diary-add-to-list args)
5202 (apply 'add-to-diary-list args)))
5203
5204 (defvar org-diary-last-run-time nil)
5205
5206 ;;;###autoload
5207 (defun org-diary (&rest args)
5208 "Return diary information from org files.
5209 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5210 It accesses org files and extracts information from those files to be
5211 listed in the diary. The function accepts arguments specifying what
5212 items should be listed. For a list of arguments allowed here, see the
5213 variable `org-agenda-entry-types'.
5214
5215 The call in the diary file should look like this:
5216
5217 &%%(org-diary) ~/path/to/some/orgfile.org
5218
5219 Use a separate line for each org file to check. Or, if you omit the file name,
5220 all files listed in `org-agenda-files' will be checked automatically:
5221
5222 &%%(org-diary)
5223
5224 If you don't give any arguments (as in the example above), the default value
5225 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5226 So the example above may also be written as
5227
5228 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5229
5230 The function expects the lisp variables `entry' and `date' to be provided
5231 by the caller, because this is how the calendar works. Don't use this
5232 function from a program - use `org-agenda-get-day-entries' instead."
5233 (when (> (- (org-float-time)
5234 org-agenda-last-marker-time)
5235 5)
5236 ;; I am not sure if this works with sticky agendas, because the marker
5237 ;; list is then no longer a global variable.
5238 (org-agenda-reset-markers))
5239 (org-compile-prefix-format 'agenda)
5240 (org-set-sorting-strategy 'agenda)
5241 (setq args (or args org-agenda-entry-types))
5242 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5243 (list entry)
5244 (org-agenda-files t)))
5245 (time (org-float-time))
5246 file rtn results)
5247 (when (or (not org-diary-last-run-time)
5248 (> (- time
5249 org-diary-last-run-time)
5250 3))
5251 (org-agenda-prepare-buffers files))
5252 (setq org-diary-last-run-time time)
5253 ;; If this is called during org-agenda, don't return any entries to
5254 ;; the calendar. Org Agenda will list these entries itself.
5255 (if org-disable-agenda-to-diary (setq files nil))
5256 (while (setq file (pop files))
5257 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5258 (setq results (append results rtn)))
5259 (when results
5260 (setq results
5261 (mapcar (lambda (i) (replace-regexp-in-string
5262 org-bracket-link-regexp "\\3" i)) results))
5263 (concat (org-agenda-finalize-entries results) "\n"))))
5264
5265 ;;; Agenda entry finders
5266
5267 (defun org-agenda-get-day-entries (file date &rest args)
5268 "Does the work for `org-diary' and `org-agenda'.
5269 FILE is the path to a file to be checked for entries. DATE is date like
5270 the one returned by `calendar-current-date'. ARGS are symbols indicating
5271 which kind of entries should be extracted. For details about these, see
5272 the documentation of `org-diary'."
5273 (setq args (or args org-agenda-entry-types))
5274 (let* ((org-startup-folded nil)
5275 (org-startup-align-all-tables nil)
5276 (buffer (if (file-exists-p file)
5277 (org-get-agenda-file-buffer file)
5278 (error "No such file %s" file)))
5279 arg results rtn deadline-results)
5280 (if (not buffer)
5281 ;; If file does not exist, make sure an error message ends up in diary
5282 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5283 (with-current-buffer buffer
5284 (unless (derived-mode-p 'org-mode)
5285 (error "Agenda file %s is not in `org-mode'" file))
5286 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5287 (let ((case-fold-search nil))
5288 (save-excursion
5289 (save-restriction
5290 (if (eq buffer org-agenda-restrict)
5291 (narrow-to-region org-agenda-restrict-begin
5292 org-agenda-restrict-end)
5293 (widen))
5294 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5295 (while (setq arg (pop args))
5296 (cond
5297 ((and (eq arg :todo)
5298 (equal date (calendar-gregorian-from-absolute
5299 (org-today))))
5300 (setq rtn (org-agenda-get-todos))
5301 (setq results (append results rtn)))
5302 ((eq arg :timestamp)
5303 (setq rtn (org-agenda-get-blocks))
5304 (setq results (append results rtn))
5305 (setq rtn (org-agenda-get-timestamps deadline-results))
5306 (setq results (append results rtn)))
5307 ((eq arg :sexp)
5308 (setq rtn (org-agenda-get-sexps))
5309 (setq results (append results rtn)))
5310 ((eq arg :scheduled)
5311 (setq rtn (org-agenda-get-scheduled deadline-results))
5312 (setq results (append results rtn)))
5313 ((eq arg :scheduled*)
5314 (setq rtn (org-agenda-get-scheduled deadline-results t))
5315 (setq results (append results rtn)))
5316 ((eq arg :closed)
5317 (setq rtn (org-agenda-get-progress))
5318 (setq results (append results rtn)))
5319 ((eq arg :deadline)
5320 (setq rtn (org-agenda-get-deadlines))
5321 (setq deadline-results (copy-sequence rtn))
5322 (setq results (append results rtn)))
5323 ((eq arg :deadline*)
5324 (setq rtn (org-agenda-get-deadlines t))
5325 (setq deadline-results (copy-sequence rtn))
5326 (setq results (append results rtn))))))))
5327 results))))
5328
5329 (defsubst org-em (x y list)
5330 "Is X or Y a member of LIST?"
5331 (or (memq x list) (memq y list)))
5332
5333 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5334 (defvar org-agenda-sorting-strategy-selected nil)
5335
5336 (defun org-agenda-get-todos ()
5337 "Return the TODO information for agenda display."
5338 (let* ((props (list 'face nil
5339 'done-face 'org-agenda-done
5340 'org-not-done-regexp org-not-done-regexp
5341 'org-todo-regexp org-todo-regexp
5342 'org-complex-heading-regexp org-complex-heading-regexp
5343 'mouse-face 'highlight
5344 'help-echo
5345 (format "mouse-2 or RET jump to org file %s"
5346 (abbreviate-file-name buffer-file-name))))
5347 (regexp (format org-heading-keyword-regexp-format
5348 (cond
5349 ((and org-select-this-todo-keyword
5350 (equal org-select-this-todo-keyword "*"))
5351 org-todo-regexp)
5352 (org-select-this-todo-keyword
5353 (concat "\\("
5354 (mapconcat 'identity
5355 (org-split-string
5356 org-select-this-todo-keyword
5357 "|")
5358 "\\|") "\\)"))
5359 (t org-not-done-regexp))))
5360 marker priority category category-pos level tags todo-state ts-date ts-date-type
5361 ee txt beg end inherited-tags todo-state-end-pos)
5362 (goto-char (point-min))
5363 (while (re-search-forward regexp nil t)
5364 (catch :skip
5365 (save-match-data
5366 (beginning-of-line)
5367 (org-agenda-skip)
5368 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5369 (unless (and (setq todo-state (org-get-todo-state))
5370 (setq todo-state-end-pos (match-end 2)))
5371 (goto-char end)
5372 (throw :skip nil))
5373 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5374 (goto-char (1+ beg))
5375 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5376 (throw :skip nil)))
5377 (goto-char (match-beginning 2))
5378 (setq marker (org-agenda-new-marker (match-beginning 0))
5379 category (org-get-category)
5380 ts-date (let (ts)
5381 (save-match-data
5382 (cond ((org-em 'scheduled-up 'scheduled-down
5383 org-agenda-sorting-strategy-selected)
5384 (setq ts (org-entry-get (point) "SCHEDULED")
5385 ts-date-type " scheduled"))
5386 ((org-em 'deadline-up 'deadline-down
5387 org-agenda-sorting-strategy-selected)
5388 (setq ts (org-entry-get (point) "DEADLINE")
5389 ts-date-type " deadline"))
5390 ((org-em 'ts-up 'ts-down
5391 org-agenda-sorting-strategy-selected)
5392 (setq ts (org-entry-get (point) "TIMESTAMP")
5393 ts-date-type " timestamp"))
5394 ((org-em 'tsia-up 'tsia-down
5395 org-agenda-sorting-strategy-selected)
5396 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5397 ts-date-type " timestamp_ia"))
5398 ((org-em 'timestamp-up 'timestamp-down
5399 org-agenda-sorting-strategy-selected)
5400 (setq ts (or (org-entry-get (point) "SCHEDULED")
5401 (org-entry-get (point) "DEADLINE")
5402 (org-entry-get (point) "TIMESTAMP")
5403 (org-entry-get (point) "TIMESTAMP_IA"))
5404 ts-date-type ""))
5405 (t (setq ts-date-type "")))
5406 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5407 category-pos (get-text-property (point) 'org-category-position)
5408 txt (org-trim
5409 (buffer-substring (match-beginning 2) (match-end 0)))
5410 inherited-tags
5411 (or (eq org-agenda-show-inherited-tags 'always)
5412 (and (listp org-agenda-show-inherited-tags)
5413 (memq 'todo org-agenda-show-inherited-tags))
5414 (and (eq org-agenda-show-inherited-tags t)
5415 (or (eq org-agenda-use-tag-inheritance t)
5416 (memq 'todo org-agenda-use-tag-inheritance))))
5417 tags (org-get-tags-at nil (not inherited-tags))
5418 level (make-string (org-reduced-level (org-outline-level)) ? )
5419 txt (org-agenda-format-item "" txt level category tags t)
5420 priority (1+ (org-get-priority txt)))
5421 (org-add-props txt props
5422 'org-marker marker 'org-hd-marker marker
5423 'priority priority 'org-category category
5424 'level level
5425 'ts-date ts-date
5426 'org-category-position category-pos
5427 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5428 (push txt ee)
5429 (if org-agenda-todo-list-sublevels
5430 (goto-char todo-state-end-pos)
5431 (org-end-of-subtree 'invisible))))
5432 (nreverse ee)))
5433
5434 (defun org-agenda-todo-custom-ignore-p (time n)
5435 "Check whether timestamp is farther away than n number of days.
5436 This function is invoked if `org-agenda-todo-ignore-deadlines',
5437 `org-agenda-todo-ignore-scheduled' or
5438 `org-agenda-todo-ignore-timestamp' is set to an integer."
5439 (let ((days (org-time-stamp-to-now
5440 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5441 (if (>= n 0)
5442 (>= days n)
5443 (<= days n))))
5444
5445 ;;;###autoload
5446 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5447 (&optional end)
5448 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5449 (when (or org-agenda-todo-ignore-with-date
5450 org-agenda-todo-ignore-scheduled
5451 org-agenda-todo-ignore-deadlines
5452 org-agenda-todo-ignore-timestamp)
5453 (setq end (or end (save-excursion (outline-next-heading) (point))))
5454 (save-excursion
5455 (or (and org-agenda-todo-ignore-with-date
5456 (re-search-forward org-ts-regexp end t))
5457 (and org-agenda-todo-ignore-scheduled
5458 (re-search-forward org-scheduled-time-regexp end t)
5459 (cond
5460 ((eq org-agenda-todo-ignore-scheduled 'future)
5461 (> (org-time-stamp-to-now
5462 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5463 ((eq org-agenda-todo-ignore-scheduled 'past)
5464 (<= (org-time-stamp-to-now
5465 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5466 ((numberp org-agenda-todo-ignore-scheduled)
5467 (org-agenda-todo-custom-ignore-p
5468 (match-string 1) org-agenda-todo-ignore-scheduled))
5469 (t)))
5470 (and org-agenda-todo-ignore-deadlines
5471 (re-search-forward org-deadline-time-regexp end t)
5472 (cond
5473 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5474 ((eq org-agenda-todo-ignore-deadlines 'far)
5475 (not (org-deadline-close (match-string 1))))
5476 ((eq org-agenda-todo-ignore-deadlines 'future)
5477 (> (org-time-stamp-to-now
5478 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5479 ((eq org-agenda-todo-ignore-deadlines 'past)
5480 (<= (org-time-stamp-to-now
5481 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5482 ((numberp org-agenda-todo-ignore-deadlines)
5483 (org-agenda-todo-custom-ignore-p
5484 (match-string 1) org-agenda-todo-ignore-deadlines))
5485 (t (org-deadline-close (match-string 1)))))
5486 (and org-agenda-todo-ignore-timestamp
5487 (let ((buffer (current-buffer))
5488 (regexp
5489 (concat
5490 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5491 (start (point)))
5492 ;; Copy current buffer into a temporary one
5493 (with-temp-buffer
5494 (insert-buffer-substring buffer start end)
5495 (goto-char (point-min))
5496 ;; Delete SCHEDULED and DEADLINE items
5497 (while (re-search-forward regexp end t)
5498 (delete-region (match-beginning 0) (match-end 0)))
5499 (goto-char (point-min))
5500 ;; No search for timestamp left
5501 (when (re-search-forward org-ts-regexp nil t)
5502 (cond
5503 ((eq org-agenda-todo-ignore-timestamp 'future)
5504 (> (org-time-stamp-to-now
5505 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5506 ((eq org-agenda-todo-ignore-timestamp 'past)
5507 (<= (org-time-stamp-to-now
5508 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5509 ((numberp org-agenda-todo-ignore-timestamp)
5510 (org-agenda-todo-custom-ignore-p
5511 (match-string 1) org-agenda-todo-ignore-timestamp))
5512 (t))))))))))
5513
5514 (defun org-agenda-get-timestamps (&optional deadline-results)
5515 "Return the date stamp information for agenda display."
5516 (let* ((props (list 'face 'org-agenda-calendar-event
5517 'org-not-done-regexp org-not-done-regexp
5518 'org-todo-regexp org-todo-regexp
5519 'org-complex-heading-regexp org-complex-heading-regexp
5520 'mouse-face 'highlight
5521 'help-echo
5522 (format "mouse-2 or RET jump to org file %s"
5523 (abbreviate-file-name buffer-file-name))))
5524 (d1 (calendar-absolute-from-gregorian date))
5525 mm
5526 (deadline-position-alist
5527 (mapcar (lambda (a) (and (setq mm (get-text-property
5528 0 'org-hd-marker a))
5529 (cons (marker-position mm) a)))
5530 deadline-results))
5531 (remove-re org-ts-regexp)
5532 (regexp
5533 (concat
5534 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5535 (regexp-quote
5536 (substring
5537 (format-time-string
5538 (car org-time-stamp-formats)
5539 (apply 'encode-time ; DATE bound by calendar
5540 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5541 1 11))
5542 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5543 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5544 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5545 donep tmp priority category category-pos level ee txt timestr tags
5546 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5547 inherited-tags ts-date)
5548 (goto-char (point-min))
5549 (while (setq end-of-match (re-search-forward regexp nil t))
5550 (setq b0 (match-beginning 0)
5551 b3 (match-beginning 3) e3 (match-end 3)
5552 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5553 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5554 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5555 (member todo-state
5556 org-agenda-repeating-timestamp-show-all)))
5557 (catch :skip
5558 (and (org-at-date-range-p) (throw :skip nil))
5559 (org-agenda-skip)
5560 (if (and (match-end 1)
5561 (not (= d1 (org-time-string-to-absolute
5562 (match-string 1) d1 nil show-all
5563 (current-buffer) b0))))
5564 (throw :skip nil))
5565 (if (and e3
5566 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5567 (throw :skip nil))
5568 (setq tmp (buffer-substring (max (point-min)
5569 (- b0 org-ds-keyword-length))
5570 b0)
5571 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5572 inactivep (= (char-after b0) ?\[)
5573 deadlinep (string-match org-deadline-regexp tmp)
5574 scheduledp (string-match org-scheduled-regexp tmp)
5575 closedp (and org-agenda-include-inactive-timestamps
5576 (string-match org-closed-string tmp))
5577 clockp (and org-agenda-include-inactive-timestamps
5578 (or (string-match org-clock-string tmp)
5579 (string-match "]-+\\'" tmp)))
5580 warntime (get-text-property (point) 'org-appt-warntime)
5581 donep (member todo-state org-done-keywords))
5582 (if (or scheduledp deadlinep closedp clockp
5583 (and donep org-agenda-skip-timestamp-if-done))
5584 (throw :skip t))
5585 (if (string-match ">" timestr)
5586 ;; substring should only run to end of time stamp
5587 (setq timestr (substring timestr 0 (match-end 0))))
5588 (setq marker (org-agenda-new-marker b0)
5589 category (org-get-category b0)
5590 category-pos (get-text-property b0 'org-category-position))
5591 (save-excursion
5592 (if (not (re-search-backward org-outline-regexp-bol nil t))
5593 (throw :skip nil)
5594 (goto-char (match-beginning 0))
5595 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5596 (assoc (point) deadline-position-alist))
5597 (throw :skip nil))
5598 (setq hdmarker (org-agenda-new-marker)
5599 inherited-tags
5600 (or (eq org-agenda-show-inherited-tags 'always)
5601 (and (listp org-agenda-show-inherited-tags)
5602 (memq 'agenda org-agenda-show-inherited-tags))
5603 (and (eq org-agenda-show-inherited-tags t)
5604 (or (eq org-agenda-use-tag-inheritance t)
5605 (memq 'agenda org-agenda-use-tag-inheritance))))
5606 tags (org-get-tags-at nil (not inherited-tags))
5607 level (make-string (org-reduced-level (org-outline-level)) ? ))
5608 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5609 (setq head (or (match-string 1) ""))
5610 (setq txt (org-agenda-format-item
5611 (if inactivep org-agenda-inactive-leader nil)
5612 head level category tags timestr
5613 remove-re habitp)))
5614 (setq priority (org-get-priority txt))
5615 (org-add-props txt props 'priority priority
5616 'org-marker marker 'org-hd-marker hdmarker
5617 'org-category category 'date date
5618 'level level
5619 'ts-date
5620 (ignore-errors (org-time-string-to-absolute timestr))
5621 'org-category-position category-pos
5622 'todo-state todo-state
5623 'warntime warntime
5624 'type "timestamp")
5625 (push txt ee))
5626 (if org-agenda-skip-additional-timestamps-same-entry
5627 (outline-next-heading)
5628 (goto-char end-of-match))))
5629 (nreverse ee)))
5630
5631 (defun org-agenda-get-sexps ()
5632 "Return the sexp information for agenda display."
5633 (require 'diary-lib)
5634 (let* ((props (list 'face 'org-agenda-calendar-sexp
5635 'mouse-face 'highlight
5636 'help-echo
5637 (format "mouse-2 or RET jump to org file %s"
5638 (abbreviate-file-name buffer-file-name))))
5639 (regexp "^&?%%(")
5640 marker category extra category-pos level ee txt tags entry
5641 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5642 (goto-char (point-min))
5643 (while (re-search-forward regexp nil t)
5644 (catch :skip
5645 (org-agenda-skip)
5646 (setq beg (match-beginning 0))
5647 (goto-char (1- (match-end 0)))
5648 (setq b (point))
5649 (forward-sexp 1)
5650 (setq sexp (buffer-substring b (point)))
5651 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5652 (org-trim (match-string 1))
5653 ""))
5654 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5655 (when result
5656 (setq marker (org-agenda-new-marker beg)
5657 level (make-string (org-reduced-level (org-outline-level)) ? )
5658 category (org-get-category beg)
5659 category-pos (get-text-property beg 'org-category-position)
5660 inherited-tags
5661 (or (eq org-agenda-show-inherited-tags 'always)
5662 (and (listp org-agenda-show-inherited-tags)
5663 (memq 'agenda org-agenda-show-inherited-tags))
5664 (and (eq org-agenda-show-inherited-tags t)
5665 (or (eq org-agenda-use-tag-inheritance t)
5666 (memq 'agenda org-agenda-use-tag-inheritance))))
5667 tags (org-get-tags-at nil (not inherited-tags))
5668 todo-state (org-get-todo-state)
5669 warntime (get-text-property (point) 'org-appt-warntime)
5670 extra nil)
5671
5672 (dolist (r (if (stringp result)
5673 (list result)
5674 result)) ;; we expect a list here
5675 (when (and org-agenda-diary-sexp-prefix
5676 (string-match org-agenda-diary-sexp-prefix r))
5677 (setq extra (match-string 0 r)
5678 r (replace-match "" nil nil r)))
5679 (if (string-match "\\S-" r)
5680 (setq txt r)
5681 (setq txt "SEXP entry returned empty string"))
5682 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5683 (org-add-props txt props 'org-marker marker
5684 'org-category category 'date date 'todo-state todo-state
5685 'org-category-position category-pos
5686 'level level
5687 'type "sexp" 'warntime warntime)
5688 (push txt ee)))))
5689 (nreverse ee)))
5690
5691 ;; Calendar sanity: define some functions that are independent of
5692 ;; `calendar-date-style'.
5693 ;; Normally I would like to use ISO format when calling the diary functions,
5694 ;; but to make sure we still have Emacs 22 compatibility we bind
5695 ;; also `european-calendar-style' and use european format
5696 (defun org-anniversary (year month day &optional mark)
5697 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5698 (org-no-warnings
5699 (let ((calendar-date-style 'european) (european-calendar-style t))
5700 (diary-anniversary day month year mark))))
5701 (defun org-cyclic (N year month day &optional mark)
5702 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5703 (org-no-warnings
5704 (let ((calendar-date-style 'european) (european-calendar-style t))
5705 (diary-cyclic N day month year mark))))
5706 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5707 "Like `diary-block', but with fixed (ISO) order of arguments."
5708 (org-no-warnings
5709 (let ((calendar-date-style 'european) (european-calendar-style t))
5710 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5711 (defun org-date (year month day &optional mark)
5712 "Like `diary-date', but with fixed (ISO) order of arguments."
5713 (org-no-warnings
5714 (let ((calendar-date-style 'european) (european-calendar-style t))
5715 (diary-date day month year mark))))
5716
5717 ;; Define the `org-class' function
5718 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5719 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5720 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5721 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5722 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5723 `holidays', then any date that is known by the Emacs calendar to be a
5724 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5725 then those holidays will be skipped."
5726 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5727 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5728 (d (calendar-absolute-from-gregorian date))
5729 (h (when skip-weeks (calendar-check-holidays date))))
5730 (and
5731 (<= date1 d)
5732 (<= d date2)
5733 (= (calendar-day-of-week date) dayname)
5734 (or (not skip-weeks)
5735 (progn
5736 (require 'cal-iso)
5737 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5738 (not (or (and h (memq 'holidays skip-weeks))
5739 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5740 entry)))
5741
5742 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5743 "Like `org-class', but honor `calendar-date-style'.
5744 The order of the first 2 times 3 arguments depends on the variable
5745 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5746 So for American calendars, give this as MONTH DAY YEAR, for European as
5747 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5748 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5749 is any number of ISO weeks in the block period for which the item should
5750 be skipped.
5751
5752 This function is here only for backward compatibility and it is deprecated,
5753 please use `org-class' instead."
5754 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5755 (date2 (org-order-calendar-date-args m2 d2 y2)))
5756 (org-class
5757 (nth 2 date1) (car date1) (nth 1 date1)
5758 (nth 2 date2) (car date2) (nth 1 date2)
5759 dayname skip-weeks)))
5760 (make-obsolete 'org-diary-class 'org-class "")
5761
5762 (defalias 'org-get-closed 'org-agenda-get-progress)
5763 (defun org-agenda-get-progress ()
5764 "Return the logged TODO entries for agenda display."
5765 (let* ((props (list 'mouse-face 'highlight
5766 'org-not-done-regexp org-not-done-regexp
5767 'org-todo-regexp org-todo-regexp
5768 'org-complex-heading-regexp org-complex-heading-regexp
5769 'help-echo
5770 (format "mouse-2 or RET jump to org file %s"
5771 (abbreviate-file-name buffer-file-name))))
5772 (items (if (consp org-agenda-show-log-scoped)
5773 org-agenda-show-log-scoped
5774 (if (eq org-agenda-show-log-scoped 'clockcheck)
5775 '(clock)
5776 org-agenda-log-mode-items)))
5777 (parts
5778 (delq nil
5779 (list
5780 (if (memq 'closed items) (concat "\\<" org-closed-string))
5781 (if (memq 'clock items) (concat "\\<" org-clock-string))
5782 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5783 (parts-re (if parts (mapconcat 'identity parts "\\|")
5784 (error "`org-agenda-log-mode-items' is empty")))
5785 (regexp (concat
5786 "\\(" parts-re "\\)"
5787 " *\\["
5788 (regexp-quote
5789 (substring
5790 (format-time-string
5791 (car org-time-stamp-formats)
5792 (apply 'encode-time ; DATE bound by calendar
5793 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5794 1 11))))
5795 (org-agenda-search-headline-for-time nil)
5796 marker hdmarker priority category category-pos level tags closedp
5797 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5798 (goto-char (point-min))
5799 (while (re-search-forward regexp nil t)
5800 (catch :skip
5801 (org-agenda-skip)
5802 (setq marker (org-agenda-new-marker (match-beginning 0))
5803 closedp (equal (match-string 1) org-closed-string)
5804 statep (equal (string-to-char (match-string 1)) ?-)
5805 clockp (not (or closedp statep))
5806 state (and statep (match-string 2))
5807 category (org-get-category (match-beginning 0))
5808 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5809 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5810 (when (string-match "\\]" timestr)
5811 ;; substring should only run to end of time stamp
5812 (setq rest (substring timestr (match-end 0))
5813 timestr (substring timestr 0 (match-end 0)))
5814 (if (and (not closedp) (not statep)
5815 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5816 rest))
5817 (progn (setq timestr (concat (substring timestr 0 -1)
5818 "-" (match-string 1 rest) "]"))
5819 (setq clocked (match-string 2 rest)))
5820 (setq clocked "-")))
5821 (save-excursion
5822 (setq extra
5823 (cond
5824 ((not org-agenda-log-mode-add-notes) nil)
5825 (statep
5826 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5827 (match-string 1)))
5828 (clockp
5829 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5830 (match-string 1)))))
5831 (if (not (re-search-backward org-outline-regexp-bol nil t))
5832 (throw :skip nil)
5833 (goto-char (match-beginning 0))
5834 (setq hdmarker (org-agenda-new-marker)
5835 inherited-tags
5836 (or (eq org-agenda-show-inherited-tags 'always)
5837 (and (listp org-agenda-show-inherited-tags)
5838 (memq 'todo org-agenda-show-inherited-tags))
5839 (and (eq org-agenda-show-inherited-tags t)
5840 (or (eq org-agenda-use-tag-inheritance t)
5841 (memq 'todo org-agenda-use-tag-inheritance))))
5842 tags (org-get-tags-at nil (not inherited-tags))
5843 level (make-string (org-reduced-level (org-outline-level)) ? ))
5844 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5845 (setq txt (match-string 1))
5846 (when extra
5847 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5848 (setq txt (concat (substring txt 0 (match-beginning 1))
5849 " - " extra " " (match-string 2 txt)))
5850 (setq txt (concat txt " - " extra))))
5851 (setq txt (org-agenda-format-item
5852 (cond
5853 (closedp "Closed: ")
5854 (statep (concat "State: (" state ")"))
5855 (t (concat "Clocked: (" clocked ")")))
5856 txt level category tags timestr)))
5857 (setq priority 100000)
5858 (org-add-props txt props
5859 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5860 'priority priority 'org-category category
5861 'org-category-position category-pos
5862 'level level
5863 'type "closed" 'date date
5864 'undone-face 'org-warning 'done-face 'org-agenda-done)
5865 (push txt ee))
5866 (goto-char (point-at-eol))))
5867 (nreverse ee)))
5868
5869 (defun org-agenda-show-clocking-issues ()
5870 "Add overlays, showing issues with clocking.
5871 See also the user option `org-agenda-clock-consistency-checks'."
5872 (interactive)
5873 (let* ((org-time-clocksum-use-effort-durations nil)
5874 (pl org-agenda-clock-consistency-checks)
5875 (re (concat "^[ \t]*"
5876 org-clock-string
5877 "[ \t]+"
5878 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5879 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5880 (tlstart 0.)
5881 (tlend 0.)
5882 (maxtime (org-hh:mm-string-to-minutes
5883 (or (plist-get pl :max-duration) "24:00")))
5884 (mintime (org-hh:mm-string-to-minutes
5885 (or (plist-get pl :min-duration) 0)))
5886 (maxgap (org-hh:mm-string-to-minutes
5887 ;; default 30:00 means never complain
5888 (or (plist-get pl :max-gap) "30:00")))
5889 (gapok (mapcar 'org-hh:mm-string-to-minutes
5890 (plist-get pl :gap-ok-around)))
5891 (def-face (or (plist-get pl :default-face)
5892 '((:background "DarkRed") (:foreground "white"))))
5893 issue face m te ts dt ov)
5894 (goto-char (point-min))
5895 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5896 (setq issue nil face def-face)
5897 (catch 'next
5898 (setq m (org-get-at-bol 'org-marker)
5899 te nil ts nil)
5900 (unless (and m (markerp m))
5901 (setq issue "No valid clock line") (throw 'next t))
5902 (org-with-point-at m
5903 (save-excursion
5904 (goto-char (point-at-bol))
5905 (unless (looking-at re)
5906 (error "No valid Clock line")
5907 (throw 'next t))
5908 (unless (match-end 3)
5909 (setq issue "No end time"
5910 face (or (plist-get pl :no-end-time-face) face))
5911 (throw 'next t))
5912 (setq ts (match-string 1)
5913 te (match-string 3)
5914 ts (org-float-time
5915 (apply 'encode-time (org-parse-time-string ts)))
5916 te (org-float-time
5917 (apply 'encode-time (org-parse-time-string te)))
5918 dt (- te ts))))
5919 (cond
5920 ((> dt (* 60 maxtime))
5921 ;; a very long clocking chunk
5922 (setq issue (format "Clocking interval is very long: %s"
5923 (org-minutes-to-clocksum-string
5924 (floor (/ (float dt) 60.))))
5925 face (or (plist-get pl :long-face) face)))
5926 ((< dt (* 60 mintime))
5927 ;; a very short clocking chunk
5928 (setq issue (format "Clocking interval is very short: %s"
5929 (org-minutes-to-clocksum-string
5930 (floor (/ (float dt) 60.))))
5931 face (or (plist-get pl :short-face) face)))
5932 ((and (> tlend 0) (< ts tlend))
5933 ;; Two clock entries are overlapping
5934 (setq issue (format "Clocking overlap: %d minutes"
5935 (/ (- tlend ts) 60))
5936 face (or (plist-get pl :overlap-face) face)))
5937 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5938 ;; There is a gap, lets see if we need to report it
5939 (unless (org-agenda-check-clock-gap tlend ts gapok)
5940 (setq issue (format "Clocking gap: %d minutes"
5941 (/ (- ts tlend) 60))
5942 face (or (plist-get pl :gap-face) face))))
5943 (t nil)))
5944 (setq tlend (or te tlend) tlstart (or ts tlstart))
5945 (when issue
5946 ;; OK, there was some issue, add an overlay to show the issue
5947 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5948 (overlay-put ov 'before-string
5949 (concat
5950 (org-add-props
5951 (format "%-43s" (concat " " issue))
5952 nil
5953 'face face)
5954 "\n"))
5955 (overlay-put ov 'evaporate t)))))
5956
5957 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5958 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5959 (catch 'exit
5960 (unless ok-list
5961 ;; there are no OK times for gaps...
5962 (throw 'exit nil))
5963 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5964 ;; This is more than 24 hours, so it is OK.
5965 ;; because we have at least one OK time, that must be in the
5966 ;; 24 hour interval.
5967 (throw 'exit t))
5968 ;; We have a shorter gap.
5969 ;; Now we have to get the minute of the day when these times are
5970 (let* ((t1dec (decode-time (seconds-to-time t1)))
5971 (t2dec (decode-time (seconds-to-time t2)))
5972 ;; compute the minute on the day
5973 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5974 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5975 (when (< min2 min1)
5976 ;; if min2 is smaller than min1, this means it is on the next day.
5977 ;; Wrap it to after midnight.
5978 (setq min2 (+ min2 1440)))
5979 ;; Now check if any of the OK times is in the gap
5980 (mapc (lambda (x)
5981 ;; Wrap the time to after midnight if necessary
5982 (if (< x min1) (setq x (+ x 1440)))
5983 ;; Check if in interval
5984 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5985 ok-list)
5986 ;; Nope, this gap is not OK
5987 nil)))
5988
5989 (defun org-agenda-get-deadlines (&optional with-hour)
5990 "Return the deadline information for agenda display.
5991 When WITH-HOUR is non-nil, only return deadlines with an hour
5992 specification like [h]h:mm."
5993 (let* ((props (list 'mouse-face 'highlight
5994 'org-not-done-regexp org-not-done-regexp
5995 'org-todo-regexp org-todo-regexp
5996 'org-complex-heading-regexp org-complex-heading-regexp
5997 'help-echo
5998 (format "mouse-2 or RET jump to org file %s"
5999 (abbreviate-file-name buffer-file-name))))
6000 (regexp (if with-hour
6001 org-deadline-time-hour-regexp
6002 org-deadline-time-regexp))
6003 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6004 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6005 (dl0 (car org-agenda-deadline-leaders))
6006 (dl1 (nth 1 org-agenda-deadline-leaders))
6007 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6008 d2 diff dfrac wdays pos pos1 category category-pos level
6009 tags suppress-prewarning ee txt head face s todo-state
6010 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6011 (goto-char (point-min))
6012 (while (re-search-forward regexp nil t)
6013 (catch :skip
6014 (org-agenda-skip)
6015 (setq s (match-string 1)
6016 txt nil
6017 pos (1- (match-beginning 1))
6018 todo-state (save-match-data (org-get-todo-state))
6019 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6020 (member todo-state
6021 org-agenda-repeating-timestamp-show-all))
6022 d2 (org-time-string-to-absolute
6023 s d1 'past show-all (current-buffer) pos)
6024 diff (- d2 d1))
6025 (setq suppress-prewarning
6026 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6027 (let ((item (buffer-substring (point-at-bol)
6028 (point-at-eol))))
6029 (save-match-data
6030 (and (string-match
6031 org-scheduled-time-regexp item)
6032 (match-string 1 item)))))))
6033 (cond
6034 ((not ds) nil)
6035 ;; The current item has a scheduled date (in ds), so
6036 ;; evaluate its prewarning lead time.
6037 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6038 ;; Use global prewarning-restart lead time.
6039 org-agenda-skip-deadline-prewarning-if-scheduled)
6040 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6041 'pre-scheduled)
6042 ;; Set prewarning to no earlier than scheduled.
6043 (min (- d2 (org-time-string-to-absolute
6044 ds d1 'past show-all (current-buffer) pos))
6045 org-deadline-warning-days))
6046 ;; Set prewarning to deadline.
6047 (t 0))))
6048 (setq wdays (if suppress-prewarning
6049 (let ((org-deadline-warning-days suppress-prewarning))
6050 (org-get-wdays s))
6051 (org-get-wdays s))
6052 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6053 upcomingp (and todayp (> diff 0)))
6054 ;; When to show a deadline in the calendar:
6055 ;; If the expiration is within wdays warning time.
6056 ;; Past-due deadlines are only shown on the current date
6057 (if (and (or (and (<= diff wdays)
6058 (and todayp (not org-agenda-only-exact-dates)))
6059 (= diff 0)))
6060 (save-excursion
6061 ;; (setq todo-state (org-get-todo-state))
6062 (setq donep (member todo-state org-done-keywords))
6063 (if (and donep
6064 (or org-agenda-skip-deadline-if-done
6065 (not (= diff 0))))
6066 (setq txt nil)
6067 (setq category (org-get-category)
6068 warntime (get-text-property (point) 'org-appt-warntime)
6069 category-pos (get-text-property (point) 'org-category-position))
6070 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6071 (throw :skip nil)
6072 (goto-char (match-end 0))
6073 (setq pos1 (match-beginning 0))
6074 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6075 (setq inherited-tags
6076 (or (eq org-agenda-show-inherited-tags 'always)
6077 (and (listp org-agenda-show-inherited-tags)
6078 (memq 'agenda org-agenda-show-inherited-tags))
6079 (and (eq org-agenda-show-inherited-tags t)
6080 (or (eq org-agenda-use-tag-inheritance t)
6081 (memq 'agenda org-agenda-use-tag-inheritance))))
6082 tags (org-get-tags-at pos1 (not inherited-tags)))
6083 (setq head (buffer-substring
6084 (point)
6085 (progn (skip-chars-forward "^\r\n")
6086 (point))))
6087 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6088 (setq timestr
6089 (concat (substring s (match-beginning 1)) " "))
6090 (setq timestr 'time))
6091 (setq txt (org-agenda-format-item
6092 (cond ((= diff 0) dl0)
6093 ((> diff 0)
6094 (if (functionp dl1)
6095 (funcall dl1 diff date)
6096 (format dl1 diff)))
6097 (t
6098 (if (functionp dl2)
6099 (funcall dl2 diff date)
6100 (format dl2 (if (string= dl2 dl1)
6101 diff (abs diff))))))
6102 head level category tags
6103 (if (not (= diff 0)) nil timestr)))))
6104 (when txt
6105 (setq face (org-agenda-deadline-face dfrac))
6106 (org-add-props txt props
6107 'org-marker (org-agenda-new-marker pos)
6108 'warntime warntime
6109 'level level
6110 'ts-date d2
6111 'org-hd-marker (org-agenda-new-marker pos1)
6112 'priority (+ (- diff)
6113 (org-get-priority txt))
6114 'org-category category
6115 'org-category-position category-pos
6116 'todo-state todo-state
6117 'type (if upcomingp "upcoming-deadline" "deadline")
6118 'date (if upcomingp date d2)
6119 'face (if donep 'org-agenda-done face)
6120 'undone-face face 'done-face 'org-agenda-done)
6121 (push txt ee))))))
6122 (nreverse ee)))
6123
6124 (defun org-agenda-deadline-face (fraction)
6125 "Return the face to displaying a deadline item.
6126 FRACTION is what fraction of the head-warning time has passed."
6127 (let ((faces org-agenda-deadline-faces) f)
6128 (catch 'exit
6129 (while (setq f (pop faces))
6130 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6131
6132 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6133 "Return the scheduled information for agenda display.
6134 When WITH-HOUR is non-nil, only return scheduled items with
6135 an hour specification like [h]h:mm."
6136 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6137 'org-todo-regexp org-todo-regexp
6138 'org-complex-heading-regexp org-complex-heading-regexp
6139 'done-face 'org-agenda-done
6140 'mouse-face 'highlight
6141 'help-echo
6142 (format "mouse-2 or RET jump to org file %s"
6143 (abbreviate-file-name buffer-file-name))))
6144 (regexp (if with-hour
6145 org-scheduled-time-hour-regexp
6146 org-scheduled-time-regexp))
6147 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6148 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6149 mm
6150 (deadline-position-alist
6151 (mapcar (lambda (a) (and (setq mm (get-text-property
6152 0 'org-hd-marker a))
6153 (cons (marker-position mm) a)))
6154 deadline-results))
6155 d2 diff pos pos1 category category-pos level tags donep
6156 ee txt head pastschedp todo-state face timestr s habitp show-all
6157 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6158 ddays)
6159 (goto-char (point-min))
6160 (while (re-search-forward regexp nil t)
6161 (catch :skip
6162 (org-agenda-skip)
6163 (setq s (match-string 1)
6164 txt nil
6165 pos (1- (match-beginning 1))
6166 todo-state (save-match-data (org-get-todo-state))
6167 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6168 (member todo-state
6169 org-agenda-repeating-timestamp-show-all))
6170 d2 (org-time-string-to-absolute
6171 s d1 'past show-all (current-buffer) pos)
6172 diff (- d2 d1)
6173 warntime (get-text-property (point) 'org-appt-warntime))
6174 (setq pastschedp (and todayp (< diff 0)))
6175 (setq did-habit-check-p nil)
6176 (setq suppress-delay
6177 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6178 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6179 (save-match-data
6180 (and (string-match
6181 org-deadline-time-regexp item)
6182 (match-string 1 item)))))))
6183 (cond
6184 ((not ds) nil)
6185 ;; The current item has a deadline date (in ds), so
6186 ;; evaluate its delay time.
6187 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6188 ;; Use global delay time.
6189 (- org-agenda-skip-scheduled-delay-if-deadline))
6190 ((eq org-agenda-skip-scheduled-delay-if-deadline
6191 'post-deadline)
6192 ;; Set delay to no later than deadline.
6193 (min (- d2 (org-time-string-to-absolute
6194 ds d1 'past show-all (current-buffer) pos))
6195 org-scheduled-delay-days))
6196 (t 0))))
6197 (setq ddays (if suppress-delay
6198 (let ((org-scheduled-delay-days suppress-delay))
6199 (org-get-wdays s t t))
6200 (org-get-wdays s t)))
6201 ;; Use a delay of 0 when there is a repeater and the delay is
6202 ;; of the form --3d
6203 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6204 (< (org-time-string-to-absolute s)
6205 (org-time-string-to-absolute
6206 s d2 'past nil (current-buffer) pos)))
6207 (setq ddays 0))
6208 ;; When to show a scheduled item in the calendar:
6209 ;; If it is on or past the date.
6210 (when (or (and (> ddays 0) (= diff (- ddays)))
6211 (and (zerop ddays) (= diff 0))
6212 (and (< (+ diff ddays) 0)
6213 (< (abs diff) org-scheduled-past-days)
6214 (and todayp (not org-agenda-only-exact-dates)))
6215 ;; org-is-habit-p uses org-entry-get, which is expansive
6216 ;; so we go extra mile to only call it once
6217 (and todayp
6218 (boundp 'org-habit-show-all-today)
6219 org-habit-show-all-today
6220 (setq did-habit-check-p t)
6221 (setq habitp (and (functionp 'org-is-habit-p)
6222 (org-is-habit-p)))))
6223 (save-excursion
6224 (setq donep (member todo-state org-done-keywords))
6225 (if (and donep
6226 (or org-agenda-skip-scheduled-if-done
6227 (not (= diff 0))
6228 (and (functionp 'org-is-habit-p)
6229 (org-is-habit-p))))
6230 (setq txt nil)
6231 (setq habitp (if did-habit-check-p habitp
6232 (and (functionp 'org-is-habit-p)
6233 (org-is-habit-p))))
6234 (setq category (org-get-category)
6235 category-pos (get-text-property (point) 'org-category-position))
6236 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6237 'repeated-after-deadline)
6238 (org-get-deadline-time (point))
6239 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6240 (throw :skip nil))
6241 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6242 (throw :skip nil)
6243 (goto-char (match-end 0))
6244 (setq pos1 (match-beginning 0))
6245 (if habitp
6246 (if (or (not org-habit-show-habits)
6247 (and (not todayp)
6248 (boundp 'org-habit-show-habits-only-for-today)
6249 org-habit-show-habits-only-for-today))
6250 (throw :skip nil))
6251 (if (and
6252 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6253 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6254 pastschedp))
6255 (setq mm (assoc pos1 deadline-position-alist)))
6256 (throw :skip nil)))
6257 (setq inherited-tags
6258 (or (eq org-agenda-show-inherited-tags 'always)
6259 (and (listp org-agenda-show-inherited-tags)
6260 (memq 'agenda org-agenda-show-inherited-tags))
6261 (and (eq org-agenda-show-inherited-tags t)
6262 (or (eq org-agenda-use-tag-inheritance t)
6263 (memq 'agenda org-agenda-use-tag-inheritance))))
6264
6265 tags (org-get-tags-at nil (not inherited-tags)))
6266 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6267 (setq head (buffer-substring
6268 (point)
6269 (progn (skip-chars-forward "^\r\n") (point))))
6270 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6271 (setq timestr
6272 (concat (substring s (match-beginning 1)) " "))
6273 (setq timestr 'time))
6274 (setq txt (org-agenda-format-item
6275 (if (= diff 0)
6276 (car org-agenda-scheduled-leaders)
6277 (format (nth 1 org-agenda-scheduled-leaders)
6278 (- 1 diff)))
6279 head level category tags
6280 (if (not (= diff 0)) nil timestr)
6281 nil habitp))))
6282 (when txt
6283 (setq face
6284 (cond
6285 ((and (not habitp) pastschedp)
6286 'org-scheduled-previously)
6287 (todayp 'org-scheduled-today)
6288 (t 'org-scheduled))
6289 habitp (and habitp (org-habit-parse-todo)))
6290 (org-add-props txt props
6291 'undone-face face
6292 'face (if donep 'org-agenda-done face)
6293 'org-marker (org-agenda-new-marker pos)
6294 'org-hd-marker (org-agenda-new-marker pos1)
6295 'type (if pastschedp "past-scheduled" "scheduled")
6296 'date (if pastschedp d2 date)
6297 'ts-date d2
6298 'warntime warntime
6299 'level level
6300 'priority (if habitp
6301 (org-habit-get-priority habitp)
6302 (+ 94 (- 5 diff) (org-get-priority txt)))
6303 'org-category category
6304 'category-position category-pos
6305 'org-habit-p habitp
6306 'todo-state todo-state)
6307 (push txt ee))))))
6308 (nreverse ee)))
6309
6310 (defun org-agenda-get-blocks ()
6311 "Return the date-range information for agenda display."
6312 (let* ((props (list 'face nil
6313 'org-not-done-regexp org-not-done-regexp
6314 'org-todo-regexp org-todo-regexp
6315 'org-complex-heading-regexp org-complex-heading-regexp
6316 'mouse-face 'highlight
6317 'help-echo
6318 (format "mouse-2 or RET jump to org file %s"
6319 (abbreviate-file-name buffer-file-name))))
6320 (regexp org-tr-regexp)
6321 (d0 (calendar-absolute-from-gregorian date))
6322 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6323 level todo-state tags pos head donep inherited-tags)
6324 (goto-char (point-min))
6325 (while (re-search-forward regexp nil t)
6326 (catch :skip
6327 (org-agenda-skip)
6328 (setq pos (point))
6329 (let ((start-time (match-string 1))
6330 (end-time (match-string 2)))
6331 (setq s1 (match-string 1)
6332 s2 (match-string 2)
6333 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6334 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6335 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6336 ;; Only allow days between the limits, because the normal
6337 ;; date stamps will catch the limits.
6338 (save-excursion
6339 (setq todo-state (org-get-todo-state))
6340 (setq donep (member todo-state org-done-keywords))
6341 (if (and donep org-agenda-skip-timestamp-if-done)
6342 (throw :skip t))
6343 (setq marker (org-agenda-new-marker (point)))
6344 (setq category (org-get-category)
6345 category-pos (get-text-property (point) 'org-category-position))
6346 (if (not (re-search-backward org-outline-regexp-bol nil t))
6347 (throw :skip nil)
6348 (goto-char (match-beginning 0))
6349 (setq hdmarker (org-agenda-new-marker (point))
6350 inherited-tags
6351 (or (eq org-agenda-show-inherited-tags 'always)
6352 (and (listp org-agenda-show-inherited-tags)
6353 (memq 'agenda org-agenda-show-inherited-tags))
6354 (and (eq org-agenda-show-inherited-tags t)
6355 (or (eq org-agenda-use-tag-inheritance t)
6356 (memq 'agenda org-agenda-use-tag-inheritance))))
6357
6358 tags (org-get-tags-at nil (not inherited-tags)))
6359 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6360 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6361 (setq head (match-string 1))
6362 (let ((remove-re
6363 (if org-agenda-remove-timeranges-from-blocks
6364 (concat
6365 "<" (regexp-quote s1) ".*?>"
6366 "--"
6367 "<" (regexp-quote s2) ".*?>")
6368 nil)))
6369 (setq txt (org-agenda-format-item
6370 (format
6371 (nth (if (= d1 d2) 0 1)
6372 org-agenda-timerange-leaders)
6373 (1+ (- d0 d1)) (1+ (- d2 d1)))
6374 head level category tags
6375 (cond ((and (= d1 d0) (= d2 d0))
6376 (concat "<" start-time ">--<" end-time ">"))
6377 ((= d1 d0)
6378 (concat "<" start-time ">"))
6379 ((= d2 d0)
6380 (concat "<" end-time ">")))
6381 remove-re))))
6382 (org-add-props txt props
6383 'org-marker marker 'org-hd-marker hdmarker
6384 'type "block" 'date date
6385 'level level
6386 'todo-state todo-state
6387 'priority (org-get-priority txt) 'org-category category
6388 'org-category-position category-pos)
6389 (push txt ee))))
6390 (goto-char pos)))
6391 ;; Sort the entries by expiration date.
6392 (nreverse ee)))
6393
6394 ;;; Agenda presentation and sorting
6395
6396 (defvar org-prefix-has-time nil
6397 "A flag, set by `org-compile-prefix-format'.
6398 The flag is set if the currently compiled format contains a `%t'.")
6399 (defvar org-prefix-has-tag nil
6400 "A flag, set by `org-compile-prefix-format'.
6401 The flag is set if the currently compiled format contains a `%T'.")
6402 (defvar org-prefix-has-effort nil
6403 "A flag, set by `org-compile-prefix-format'.
6404 The flag is set if the currently compiled format contains a `%e'.")
6405 (defvar org-prefix-has-breadcrumbs nil
6406 "A flag, set by `org-compile-prefix-format'.
6407 The flag is set if the currently compiled format contains a `%b'.")
6408 (defvar org-prefix-category-length nil
6409 "Used by `org-compile-prefix-format' to remember the category field width.")
6410 (defvar org-prefix-category-max-length nil
6411 "Used by `org-compile-prefix-format' to remember the category field width.")
6412
6413 (defun org-agenda-get-category-icon (category)
6414 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6415 (dolist (entry org-agenda-category-icon-alist)
6416 (when (org-string-match-p (car entry) category)
6417 (if (listp (cadr entry))
6418 (return (cadr entry))
6419 (return (apply 'create-image (cdr entry)))))))
6420
6421 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6422 remove-re habitp)
6423 "Format TXT to be inserted into the agenda buffer.
6424 In particular, add the prefix and corresponding text properties.
6425
6426 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6427 LEVEL may be a string to replace the `%l' specifier.
6428 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6429 category taken from local variable or file name. It will replace the `%c'
6430 specifier in the format.
6431 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6432 TXT for sorting of this entry, and for the `%t' specifier in the format.
6433 When DOTIME is a string, this string is searched for a time before TXT is.
6434 TAGS can be the tags of the headline.
6435 Any match of REMOVE-RE will be removed from TXT."
6436 ;; We keep the org-prefix-* variable values along with a compiled
6437 ;; formatter, so that multiple agendas existing at the same time do
6438 ;; not step on each other toes.
6439 ;;
6440 ;; It was inconvenient to make these variables buffer local in
6441 ;; Agenda buffers, because this function expects to be called with
6442 ;; the buffer where item comes from being current, and not agenda
6443 ;; buffer
6444 (let* ((bindings (car org-prefix-format-compiled))
6445 (formatter (cadr org-prefix-format-compiled)))
6446 (loop for (var value) in bindings
6447 do (set var value))
6448 (save-match-data
6449 ;; Diary entries sometimes have extra whitespace at the beginning
6450 (setq txt (org-trim txt))
6451
6452 ;; Fix the tags part in txt
6453 (setq txt (org-agenda-fix-displayed-tags
6454 txt tags
6455 org-agenda-show-inherited-tags
6456 org-agenda-hide-tags-regexp))
6457
6458 (let* ((category (or category
6459 (if (stringp org-category)
6460 org-category
6461 (and org-category (symbol-name org-category)))
6462 (if buffer-file-name
6463 (file-name-sans-extension
6464 (file-name-nondirectory buffer-file-name))
6465 "")))
6466 (category-icon (org-agenda-get-category-icon category))
6467 (category-icon (if category-icon
6468 (propertize " " 'display category-icon)
6469 ""))
6470 ;; time, tag, effort are needed for the eval of the prefix format
6471 (tag (if tags (nth (1- (length tags)) tags) ""))
6472 time effort neffort
6473 (ts (if dotime (concat
6474 (if (stringp dotime) dotime "")
6475 (and org-agenda-search-headline-for-time txt))))
6476 (time-of-day (and dotime (org-get-time-of-day ts)))
6477 stamp plain s0 s1 s2 rtn srp l
6478 duration thecategory breadcrumbs)
6479 (and (derived-mode-p 'org-mode) buffer-file-name
6480 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6481 (when (and dotime time-of-day)
6482 ;; Extract starting and ending time and move them to prefix
6483 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6484 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6485 (setq s0 (match-string 0 ts)
6486 srp (and stamp (match-end 3))
6487 s1 (match-string (if plain 1 2) ts)
6488 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6489
6490 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6491 ;; them, we might want to remove them there to avoid duplication.
6492 ;; The user can turn this off with a variable.
6493 (if (and org-prefix-has-time
6494 org-agenda-remove-times-when-in-prefix (or stamp plain)
6495 (string-match (concat (regexp-quote s0) " *") txt)
6496 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6497 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6498 (= (match-beginning 0) 0)
6499 t))
6500 (setq txt (replace-match "" nil nil txt))))
6501 ;; Normalize the time(s) to 24 hour
6502 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6503 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6504
6505 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6506 (let (org-time-clocksum-use-effort-durations)
6507 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6508 (setq s2
6509 (org-minutes-to-clocksum-string
6510 (+ (org-hh:mm-string-to-minutes s1)
6511 org-agenda-default-appointment-duration)))))
6512
6513 ;; Compute the duration
6514 (when s2
6515 (setq duration (- (org-hh:mm-string-to-minutes s2)
6516 (org-hh:mm-string-to-minutes s1)))))
6517
6518 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6519 txt)
6520 ;; Tags are in the string
6521 (if (or (eq org-agenda-remove-tags t)
6522 (and org-agenda-remove-tags
6523 org-prefix-has-tag))
6524 (setq txt (replace-match "" t t txt))
6525 (setq txt (replace-match
6526 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6527 (match-string 2 txt))
6528 t t txt))))
6529 (when (derived-mode-p 'org-mode)
6530 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6531
6532 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6533 ;; current buffer, so move this check outside of above
6534 (if effort
6535 (setq neffort (org-duration-string-to-minutes effort)
6536 effort (setq effort (concat "[" effort "]")))
6537 ;; prevent erroring out with %e format when there is no effort
6538 (setq effort ""))
6539
6540 (when remove-re
6541 (while (string-match remove-re txt)
6542 (setq txt (replace-match "" t t txt))))
6543
6544 ;; Set org-heading property on `txt' to mark the start of the
6545 ;; heading.
6546 (add-text-properties 0 (length txt) '(org-heading t) txt)
6547
6548 ;; Prepare the variables needed in the eval of the compiled format
6549 (if org-prefix-has-breadcrumbs
6550 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6551 (let ((s (org-display-outline-path nil nil "->" t)))
6552 (if (eq "" s) "" (concat s "->"))))))
6553 (setq time (cond (s2 (concat
6554 (org-agenda-time-of-day-to-ampm-maybe s1)
6555 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6556 (if org-agenda-timegrid-use-ampm " ")))
6557 (s1 (concat
6558 (org-agenda-time-of-day-to-ampm-maybe s1)
6559 (if org-agenda-timegrid-use-ampm
6560 "........ "
6561 "......")))
6562 (t ""))
6563 extra (or (and (not habitp) extra) "")
6564 category (if (symbolp category) (symbol-name category) category)
6565 thecategory (copy-sequence category)
6566 level (or level ""))
6567 (if (string-match org-bracket-link-regexp category)
6568 (progn
6569 (setq l (if (match-end 3)
6570 (- (match-end 3) (match-beginning 3))
6571 (- (match-end 1) (match-beginning 1))))
6572 (when (< l (or org-prefix-category-length 0))
6573 (setq category (copy-sequence category))
6574 (org-add-props category nil
6575 'extra-space (make-string
6576 (- org-prefix-category-length l 1) ?\ ))))
6577 (if (and org-prefix-category-max-length
6578 (>= (length category) org-prefix-category-max-length))
6579 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6580 ;; Evaluate the compiled format
6581 (setq rtn (concat (eval formatter) txt))
6582
6583 ;; And finally add the text properties
6584 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6585 (org-add-props rtn nil
6586 'org-category (if thecategory (downcase thecategory) category)
6587 'tags (mapcar 'org-downcase-keep-props tags)
6588 'org-highest-priority org-highest-priority
6589 'org-lowest-priority org-lowest-priority
6590 'time-of-day time-of-day
6591 'duration duration
6592 'effort effort
6593 'effort-minutes neffort
6594 'breadcrumbs breadcrumbs
6595 'txt txt
6596 'level level
6597 'time time
6598 'extra extra
6599 'format org-prefix-format-compiled
6600 'dotime dotime)))))
6601
6602 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6603 "Remove tags string from TXT, and add a modified list of tags.
6604 The modified list may contain inherited tags, and tags matched by
6605 `org-agenda-hide-tags-regexp' will be removed."
6606 (when (or add-inherited hide-re)
6607 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6608 (setq txt (substring txt 0 (match-beginning 0))))
6609 (setq tags
6610 (delq nil
6611 (mapcar (lambda (tg)
6612 (if (or (and hide-re (string-match hide-re tg))
6613 (and (not add-inherited)
6614 (get-text-property 0 'inherited tg)))
6615 nil
6616 tg))
6617 tags)))
6618 (when tags
6619 (let ((have-i (get-text-property 0 'inherited (car tags)))
6620 i)
6621 (setq txt (concat txt " :"
6622 (mapconcat
6623 (lambda (x)
6624 (setq i (get-text-property 0 'inherited x))
6625 (if (and have-i (not i))
6626 (progn
6627 (setq have-i nil)
6628 (concat ":" x))
6629 x))
6630 tags ":")
6631 (if have-i "::" ":"))))))
6632 txt)
6633
6634 (defun org-downcase-keep-props (s)
6635 (let ((props (text-properties-at 0 s)))
6636 (setq s (downcase s))
6637 (add-text-properties 0 (length s) props s)
6638 s))
6639
6640 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6641
6642 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6643 "Add a time-grid for agenda items which need it.
6644
6645 LIST is the list of agenda items formatted by `org-agenda-list'.
6646 NDAYS is the span of the current agenda view.
6647 TODAYP is t when the current agenda view is on today."
6648 (catch 'exit
6649 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6650 ((and todayp (member 'today (car org-agenda-time-grid))))
6651 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6652 ((member 'weekly (car org-agenda-time-grid)))
6653 (t (throw 'exit list)))
6654 (let* ((have (delq nil (mapcar
6655 (lambda (x) (get-text-property 1 'time-of-day x))
6656 list)))
6657 (string (nth 1 org-agenda-time-grid))
6658 (gridtimes (nth 2 org-agenda-time-grid))
6659 (req (car org-agenda-time-grid))
6660 (remove (member 'remove-match req))
6661 new time)
6662 (if (and (member 'require-timed req) (not have))
6663 ;; don't show empty grid
6664 (throw 'exit list))
6665 (while (setq time (pop gridtimes))
6666 (unless (and remove (member time have))
6667 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6668 (push (org-agenda-format-item
6669 nil string nil "" nil
6670 (concat (substring time 0 -2) ":" (substring time -2)))
6671 new)
6672 (put-text-property
6673 2 (length (car new)) 'face 'org-time-grid (car new))))
6674 (when (and todayp org-agenda-show-current-time-in-grid)
6675 (push (org-agenda-format-item
6676 nil org-agenda-current-time-string nil "" nil
6677 (format-time-string "%H:%M "))
6678 new)
6679 (put-text-property
6680 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6681
6682 (if (member 'time-up org-agenda-sorting-strategy-selected)
6683 (append new list)
6684 (append list new)))))
6685
6686 (defun org-compile-prefix-format (key)
6687 "Compile the prefix format into a Lisp form that can be evaluated.
6688 The resulting form and associated variable bindings is returned
6689 and stored in the variable `org-prefix-format-compiled'."
6690 (setq org-prefix-has-time nil
6691 org-prefix-has-tag nil
6692 org-prefix-category-length nil
6693 org-prefix-has-effort nil
6694 org-prefix-has-breadcrumbs nil)
6695 (let ((s (cond
6696 ((stringp org-agenda-prefix-format)
6697 org-agenda-prefix-format)
6698 ((assq key org-agenda-prefix-format)
6699 (cdr (assq key org-agenda-prefix-format)))
6700 (t " %-12:c%?-12t% s")))
6701 (start 0)
6702 varform vars var e c f opt)
6703 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6704 s start)
6705 (setq var (or (cdr (assoc (match-string 4 s)
6706 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6707 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6708 'eval)
6709 c (or (match-string 3 s) "")
6710 opt (match-beginning 1)
6711 start (1+ (match-beginning 0)))
6712 (if (equal var 'time) (setq org-prefix-has-time t))
6713 (if (equal var 'tag) (setq org-prefix-has-tag t))
6714 (if (equal var 'effort) (setq org-prefix-has-effort t))
6715 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6716 (setq f (concat "%" (match-string 2 s) "s"))
6717 (when (equal var 'category)
6718 (setq org-prefix-category-length
6719 (floor (abs (string-to-number (match-string 2 s)))))
6720 (setq org-prefix-category-max-length
6721 (let ((x (match-string 2 s)))
6722 (save-match-data
6723 (if (string-match "\\.[0-9]+" x)
6724 (string-to-number (substring (match-string 0 x) 1)))))))
6725 (if (eq var 'eval)
6726 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6727 (if opt
6728 (setq varform
6729 `(if (equal "" ,var)
6730 ""
6731 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6732 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6733 (setq s (replace-match "%s" t nil s))
6734 (push varform vars))
6735 (setq vars (nreverse vars))
6736 (with-current-buffer (or org-agenda-buffer (current-buffer))
6737 (setq org-prefix-format-compiled
6738 (list
6739 `((org-prefix-has-time ,org-prefix-has-time)
6740 (org-prefix-has-tag ,org-prefix-has-tag)
6741 (org-prefix-category-length ,org-prefix-category-length)
6742 (org-prefix-has-effort ,org-prefix-has-effort)
6743 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6744 `(format ,s ,@vars))))))
6745
6746 (defun org-set-sorting-strategy (key)
6747 (if (symbolp (car org-agenda-sorting-strategy))
6748 ;; the old format
6749 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6750 (setq org-agenda-sorting-strategy-selected
6751 (or (cdr (assq key org-agenda-sorting-strategy))
6752 (cdr (assq 'agenda org-agenda-sorting-strategy))
6753 '(time-up category-keep priority-down)))))
6754
6755 (defun org-get-time-of-day (s &optional string mod24)
6756 "Check string S for a time of day.
6757 If found, return it as a military time number between 0 and 2400.
6758 If not found, return nil.
6759 The optional STRING argument forces conversion into a 5 character wide string
6760 HH:MM."
6761 (save-match-data
6762 (when
6763 (and
6764 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6765 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6766 (not (eq (get-text-property 1 'face s) 'org-link)))
6767 (let* ((h (string-to-number (match-string 1 s)))
6768 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6769 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6770 (am-p (equal ampm "am"))
6771 (h1 (cond ((not ampm) h)
6772 ((= h 12) (if am-p 0 12))
6773 (t (+ h (if am-p 0 12)))))
6774 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6775 (mod h1 24) h1))
6776 (t0 (+ (* 100 h2) m))
6777 (t1 (concat (if (>= h1 24) "+" " ")
6778 (if (and org-agenda-time-leading-zero
6779 (< t0 1000)) "0" "")
6780 (if (< t0 100) "0" "")
6781 (if (< t0 10) "0" "")
6782 (int-to-string t0))))
6783 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6784
6785 (defvar org-agenda-before-sorting-filter-function nil
6786 "Function to be applied to agenda items prior to sorting.
6787 Prior to sorting also means just before they are inserted into the agenda.
6788
6789 To aid sorting, you may revisit the original entries and add more text
6790 properties which will later be used by the sorting functions.
6791
6792 The function should take a string argument, an agenda line.
6793 It has access to the text properties in that line, which contain among
6794 other things, the property `org-hd-marker' that points to the entry
6795 where the line comes from. Note that not all lines going into the agenda
6796 have this property, only most.
6797
6798 The function should return the modified string. It is probably best
6799 to ONLY change text properties.
6800
6801 You can also use this function as a filter, by returning nil for lines
6802 you don't want to have in the agenda at all. For this application, you
6803 could bind the variable in the options section of a custom command.")
6804
6805 (defun org-agenda-finalize-entries (list &optional type)
6806 "Sort, limit and concatenate the LIST of agenda items.
6807 The optional argument TYPE tells the agenda type."
6808 (let ((max-effort (cond ((listp org-agenda-max-effort)
6809 (cdr (assoc type org-agenda-max-effort)))
6810 (t org-agenda-max-effort)))
6811 (max-todo (cond ((listp org-agenda-max-todos)
6812 (cdr (assoc type org-agenda-max-todos)))
6813 (t org-agenda-max-todos)))
6814 (max-tags (cond ((listp org-agenda-max-tags)
6815 (cdr (assoc type org-agenda-max-tags)))
6816 (t org-agenda-max-tags)))
6817 (max-entries (cond ((listp org-agenda-max-entries)
6818 (cdr (assoc type org-agenda-max-entries)))
6819 (t org-agenda-max-entries))) l)
6820 (when org-agenda-before-sorting-filter-function
6821 (setq list
6822 (delq nil
6823 (mapcar
6824 org-agenda-before-sorting-filter-function list))))
6825 (setq list (mapcar 'org-agenda-highlight-todo list)
6826 list (mapcar 'identity (sort list 'org-entries-lessp)))
6827 (when max-effort
6828 (setq list (org-agenda-limit-entries
6829 list 'effort-minutes max-effort 'identity)))
6830 (when max-todo
6831 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6832 (when max-tags
6833 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6834 (when max-entries
6835 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6836 (mapconcat 'identity list "\n")))
6837
6838 (defun org-agenda-limit-entries (list prop limit &optional fn)
6839 "Limit the number of agenda entries."
6840 (let ((include (and limit (< limit 0))))
6841 (if limit
6842 (let ((fun (or fn (lambda (p) (if p 1))))
6843 (lim 0))
6844 (delq nil
6845 (mapcar
6846 (lambda (e)
6847 (let ((pval (funcall fun (get-text-property 1 prop e))))
6848 (if pval (setq lim (+ lim pval)))
6849 (cond ((and pval (<= lim (abs limit))) e)
6850 ((and include (not pval)) e))))
6851 list)))
6852 list)))
6853
6854 (defun org-agenda-limit-interactively ()
6855 "In agenda, interactively limit entries to various maximums."
6856 (interactive)
6857 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6858 (num (string-to-number (read-from-minibuffer "How many? "))))
6859 (cond ((equal max ?e)
6860 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6861 ((equal max ?t)
6862 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6863 ((equal max ?T)
6864 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6865 ((equal max ?E)
6866 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6867 (org-agenda-fit-window-to-buffer))
6868
6869 (defun org-agenda-highlight-todo (x)
6870 (let ((org-done-keywords org-done-keywords-for-agenda)
6871 (case-fold-search nil)
6872 re)
6873 (if (eq x 'line)
6874 (save-excursion
6875 (beginning-of-line 1)
6876 (setq re (org-get-at-bol 'org-todo-regexp))
6877 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6878 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6879 (add-text-properties (match-beginning 0) (match-end 1)
6880 (list 'face (org-get-todo-face 1)))
6881 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6882 (delete-region (match-beginning 1) (1- (match-end 0)))
6883 (goto-char (match-beginning 1))
6884 (insert (format org-agenda-todo-keyword-format s)))))
6885 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6886 (setq re (get-text-property 0 'org-todo-regexp x))
6887 (when (and re
6888 ;; Test `pl' because if there's no heading content,
6889 ;; there's no point matching to highlight. Note
6890 ;; that if we didn't test `pl' first, and there
6891 ;; happened to be no keyword from `org-todo-regexp'
6892 ;; on this heading line, then the `equal' comparison
6893 ;; afterwards would spuriously succeed in the case
6894 ;; where `pl' is nil -- causing an args-out-of-range
6895 ;; error when we try to add text properties to text
6896 ;; that isn't there.
6897 pl
6898 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6899 x pl) pl))
6900 (add-text-properties
6901 (or (match-end 1) (match-end 0)) (match-end 0)
6902 (list 'face (org-get-todo-face (match-string 2 x)))
6903 x)
6904 (when (match-end 1)
6905 (setq x (concat (substring x 0 (match-end 1))
6906 (format org-agenda-todo-keyword-format
6907 (match-string 2 x))
6908 (org-add-props " " (text-properties-at 0 x))
6909 (substring x (match-end 3)))))))
6910 x)))
6911
6912 (defsubst org-cmp-priority (a b)
6913 "Compare the priorities of string A and B."
6914 (let ((pa (or (get-text-property 1 'priority a) 0))
6915 (pb (or (get-text-property 1 'priority b) 0)))
6916 (cond ((> pa pb) +1)
6917 ((< pa pb) -1))))
6918
6919 (defsubst org-cmp-effort (a b)
6920 "Compare the effort values of string A and B."
6921 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6922 (ea (or (get-text-property 1 'effort-minutes a) def))
6923 (eb (or (get-text-property 1 'effort-minutes b) def)))
6924 (cond ((> ea eb) +1)
6925 ((< ea eb) -1))))
6926
6927 (defsubst org-cmp-category (a b)
6928 "Compare the string values of categories of strings A and B."
6929 (let ((ca (or (get-text-property 1 'org-category a) ""))
6930 (cb (or (get-text-property 1 'org-category b) "")))
6931 (cond ((string-lessp ca cb) -1)
6932 ((string-lessp cb ca) +1))))
6933
6934 (defsubst org-cmp-todo-state (a b)
6935 "Compare the todo states of strings A and B."
6936 (let* ((ma (or (get-text-property 1 'org-marker a)
6937 (get-text-property 1 'org-hd-marker a)))
6938 (mb (or (get-text-property 1 'org-marker b)
6939 (get-text-property 1 'org-hd-marker b)))
6940 (fa (and ma (marker-buffer ma)))
6941 (fb (and mb (marker-buffer mb)))
6942 (todo-kwds
6943 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6944 (and fb (with-current-buffer fb org-todo-keywords-1))))
6945 (ta (or (get-text-property 1 'todo-state a) ""))
6946 (tb (or (get-text-property 1 'todo-state b) ""))
6947 (la (- (length (member ta todo-kwds))))
6948 (lb (- (length (member tb todo-kwds))))
6949 (donepa (member ta org-done-keywords-for-agenda))
6950 (donepb (member tb org-done-keywords-for-agenda)))
6951 (cond ((and donepa (not donepb)) -1)
6952 ((and (not donepa) donepb) +1)
6953 ((< la lb) -1)
6954 ((< lb la) +1))))
6955
6956 (defsubst org-cmp-alpha (a b)
6957 "Compare the headlines, alphabetically."
6958 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6959 (plb (text-property-any 0 (length b) 'org-heading t b))
6960 (ta (and pla (substring a pla)))
6961 (tb (and plb (substring b plb))))
6962 (when pla
6963 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6964 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6965 (setq ta (substring ta (match-end 0))))
6966 (setq ta (downcase ta)))
6967 (when plb
6968 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6969 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6970 (setq tb (substring tb (match-end 0))))
6971 (setq tb (downcase tb)))
6972 (cond ((not ta) +1)
6973 ((not tb) -1)
6974 ((string-lessp ta tb) -1)
6975 ((string-lessp tb ta) +1))))
6976
6977 (defsubst org-cmp-tag (a b)
6978 "Compare the string values of the first tags of A and B."
6979 (let ((ta (car (last (get-text-property 1 'tags a))))
6980 (tb (car (last (get-text-property 1 'tags b)))))
6981 (cond ((not ta) +1)
6982 ((not tb) -1)
6983 ((string-lessp ta tb) -1)
6984 ((string-lessp tb ta) +1))))
6985
6986 (defsubst org-cmp-time (a b)
6987 "Compare the time-of-day values of strings A and B."
6988 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6989 (ta (or (get-text-property 1 'time-of-day a) def))
6990 (tb (or (get-text-property 1 'time-of-day b) def)))
6991 (cond ((< ta tb) -1)
6992 ((< tb ta) +1))))
6993
6994 (defsubst org-cmp-ts (a b type)
6995 "Compare the timestamps values of entries A and B.
6996 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6997 \"timestamp_ia\", compare within each of these type. When TYPE
6998 is the empty string, compare all timestamps without respect of
6999 their type."
7000 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7001 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7002 (get-text-property 1 'ts-date a))
7003 def))
7004 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7005 (get-text-property 1 'ts-date b))
7006 def)))
7007 (cond ((< ta tb) -1)
7008 ((< tb ta) +1))))
7009
7010 (defsubst org-cmp-habit-p (a b)
7011 "Compare the todo states of strings A and B."
7012 (let ((ha (get-text-property 1 'org-habit-p a))
7013 (hb (get-text-property 1 'org-habit-p b)))
7014 (cond ((and ha (not hb)) -1)
7015 ((and (not ha) hb) +1))))
7016
7017 (defun org-entries-lessp (a b)
7018 "Predicate for sorting agenda entries."
7019 ;; The following variables will be used when the form is evaluated.
7020 ;; So even though the compiler complains, keep them.
7021 (let* ((ss org-agenda-sorting-strategy-selected)
7022 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7023 (org-cmp-ts a b "")))
7024 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7025 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7026 (org-cmp-ts a b "scheduled")))
7027 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7028 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7029 (org-cmp-ts a b "deadline")))
7030 (deadline-down (if deadline-up (- deadline-up) nil))
7031 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7032 (org-cmp-ts a b "timestamp_ia")))
7033 (tsia-down (if tsia-up (- tsia-up) nil))
7034 (ts-up (and (org-em 'ts-up 'ts-down ss)
7035 (org-cmp-ts a b "timestamp")))
7036 (ts-down (if ts-up (- ts-up) nil))
7037 (time-up (and (org-em 'time-up 'time-down ss)
7038 (org-cmp-time a b)))
7039 (time-down (if time-up (- time-up) nil))
7040 (priority-up (and (org-em 'priority-up 'priority-down ss)
7041 (org-cmp-priority a b)))
7042 (priority-down (if priority-up (- priority-up) nil))
7043 (effort-up (and (org-em 'effort-up 'effort-down ss)
7044 (org-cmp-effort a b)))
7045 (effort-down (if effort-up (- effort-up) nil))
7046 (category-up (and (or (org-em 'category-up 'category-down ss)
7047 (memq 'category-keep ss))
7048 (org-cmp-category a b)))
7049 (category-down (if category-up (- category-up) nil))
7050 (category-keep (if category-up +1 nil))
7051 (tag-up (and (org-em 'tag-up 'tag-down ss)
7052 (org-cmp-tag a b)))
7053 (tag-down (if tag-up (- tag-up) nil))
7054 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7055 (org-cmp-todo-state a b)))
7056 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7057 (habit-up (and (org-em 'habit-up 'habit-down ss)
7058 (org-cmp-habit-p a b)))
7059 (habit-down (if habit-up (- habit-up) nil))
7060 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7061 (org-cmp-alpha a b)))
7062 (alpha-down (if alpha-up (- alpha-up) nil))
7063 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7064 user-defined-up user-defined-down)
7065 (if (and need-user-cmp org-agenda-cmp-user-defined
7066 (functionp org-agenda-cmp-user-defined))
7067 (setq user-defined-up
7068 (funcall org-agenda-cmp-user-defined a b)
7069 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7070 (cdr (assoc
7071 (eval (cons 'or org-agenda-sorting-strategy-selected))
7072 '((-1 . t) (1 . nil) (nil . nil))))))
7073
7074 ;;; Agenda restriction lock
7075
7076 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7077 "Overlay to mark the headline to which agenda commands are restricted.")
7078 (overlay-put org-agenda-restriction-lock-overlay
7079 'face 'org-agenda-restriction-lock)
7080 (overlay-put org-agenda-restriction-lock-overlay
7081 'help-echo "Agendas are currently limited to this subtree.")
7082 (org-detach-overlay org-agenda-restriction-lock-overlay)
7083
7084 ;;;###autoload
7085 (defun org-agenda-set-restriction-lock (&optional type)
7086 "Set restriction lock for agenda, to current subtree or file.
7087 Restriction will be the file if TYPE is `file', or if type is the
7088 universal prefix '(4), or if the cursor is before the first headline
7089 in the file. Otherwise, restriction will be to the current subtree."
7090 (interactive "P")
7091 (and (equal type '(4)) (setq type 'file))
7092 (setq type (cond
7093 (type type)
7094 ((org-at-heading-p) 'subtree)
7095 ((condition-case nil (org-back-to-heading t) (error nil))
7096 'subtree)
7097 (t 'file)))
7098 (if (eq type 'subtree)
7099 (progn
7100 (setq org-agenda-restrict (current-buffer))
7101 (setq org-agenda-overriding-restriction 'subtree)
7102 (put 'org-agenda-files 'org-restrict
7103 (list (buffer-file-name (buffer-base-buffer))))
7104 (org-back-to-heading t)
7105 (move-overlay org-agenda-restriction-lock-overlay
7106 (point)
7107 (if org-agenda-restriction-lock-highlight-subtree
7108 (save-excursion (org-end-of-subtree t t) (point))
7109 (point-at-eol)))
7110 (move-marker org-agenda-restrict-begin (point))
7111 (move-marker org-agenda-restrict-end
7112 (save-excursion (org-end-of-subtree t t)))
7113 (message "Locking agenda restriction to subtree"))
7114 (put 'org-agenda-files 'org-restrict
7115 (list (buffer-file-name (buffer-base-buffer))))
7116 (setq org-agenda-restrict nil)
7117 (setq org-agenda-overriding-restriction 'file)
7118 (move-marker org-agenda-restrict-begin nil)
7119 (move-marker org-agenda-restrict-end nil)
7120 (message "Locking agenda restriction to file"))
7121 (setq current-prefix-arg nil)
7122 (org-agenda-maybe-redo))
7123
7124 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7125 "Remove the agenda restriction lock."
7126 (interactive "P")
7127 (org-detach-overlay org-agenda-restriction-lock-overlay)
7128 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7129 (setq org-agenda-overriding-restriction nil)
7130 (setq org-agenda-restrict nil)
7131 (put 'org-agenda-files 'org-restrict nil)
7132 (move-marker org-agenda-restrict-begin nil)
7133 (move-marker org-agenda-restrict-end nil)
7134 (setq current-prefix-arg nil)
7135 (message "Agenda restriction lock removed")
7136 (or noupdate (org-agenda-maybe-redo)))
7137
7138 (defun org-agenda-maybe-redo ()
7139 "If there is any window showing the agenda view, update it."
7140 (let ((w (get-buffer-window org-agenda-buffer-name t))
7141 (w0 (selected-window)))
7142 (when w
7143 (select-window w)
7144 (org-agenda-redo)
7145 (select-window w0)
7146 (if org-agenda-overriding-restriction
7147 (message "Agenda view shifted to new %s restriction"
7148 org-agenda-overriding-restriction)
7149 (message "Agenda restriction lock removed")))))
7150
7151 ;;; Agenda commands
7152
7153 (defun org-agenda-check-type (error &rest types)
7154 "Check if agenda buffer is of allowed type.
7155 If ERROR is non-nil, throw an error, otherwise just return nil.
7156 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7157 (if (not org-agenda-type)
7158 (error "No Org agenda currently displayed")
7159 (if (memq org-agenda-type types)
7160 t
7161 (if error
7162 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7163 nil))))
7164
7165 (defun org-agenda-Quit ()
7166 "Exit the agenda and kill buffers loaded by `org-agenda'.
7167 Also restore the window configuration."
7168 (interactive)
7169 (if org-agenda-columns-active
7170 (org-columns-quit)
7171 (let ((buf (current-buffer)))
7172 (if (eq org-agenda-window-setup 'other-frame)
7173 (progn
7174 (org-agenda-reset-markers)
7175 (kill-buffer buf)
7176 (org-columns-remove-overlays)
7177 (setq org-agenda-archives-mode nil)
7178 (delete-frame))
7179 (and (not (eq org-agenda-window-setup 'current-window))
7180 (not (one-window-p))
7181 (delete-window))
7182 (org-agenda-reset-markers)
7183 (kill-buffer buf)
7184 (org-columns-remove-overlays)
7185 (setq org-agenda-archives-mode nil)))
7186 (setq org-agenda-buffer nil)
7187 ;; Maybe restore the pre-agenda window configuration.
7188 (and org-agenda-restore-windows-after-quit
7189 (not (eq org-agenda-window-setup 'other-frame))
7190 org-agenda-pre-window-conf
7191 (set-window-configuration org-agenda-pre-window-conf)
7192 (setq org-agenda-pre-window-conf nil))))
7193
7194 (defun org-agenda-quit ()
7195 "Exit the agenda and restore the window configuration.
7196 When `org-agenda-sticky' is non-nil, only bury the agenda."
7197 (interactive)
7198 (if (and (eq org-indirect-buffer-display 'other-window)
7199 org-last-indirect-buffer)
7200 (let ((org-last-indirect-window
7201 (get-buffer-window org-last-indirect-buffer)))
7202 (if org-last-indirect-window
7203 (delete-window org-last-indirect-window))))
7204 (if org-agenda-columns-active
7205 (org-columns-quit)
7206 (if org-agenda-sticky
7207 (let ((buf (current-buffer)))
7208 (if (eq org-agenda-window-setup 'other-frame)
7209 (progn
7210 (delete-frame))
7211 (and (not (eq org-agenda-window-setup 'current-window))
7212 (not (one-window-p))
7213 (delete-window)))
7214 (with-current-buffer buf
7215 (bury-buffer)
7216 ;; Maybe restore the pre-agenda window configuration.
7217 (and org-agenda-restore-windows-after-quit
7218 (not (eq org-agenda-window-setup 'other-frame))
7219 org-agenda-pre-window-conf
7220 (set-window-configuration org-agenda-pre-window-conf)
7221 (setq org-agenda-pre-window-conf nil))))
7222 (org-agenda-Quit))))
7223
7224 (defun org-agenda-exit ()
7225 "Exit the agenda and restore the window configuration.
7226 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7227 buffers visited directly by the user will not be touched."
7228 (interactive)
7229 (org-release-buffers org-agenda-new-buffers)
7230 (setq org-agenda-new-buffers nil)
7231 (org-agenda-Quit))
7232
7233 (defun org-agenda-kill-all-agenda-buffers ()
7234 "Kill all buffers in `org-agenda-mode'.
7235 This is used when toggling sticky agendas.
7236 You can also explicitly invoke it with `C-c a C-k'."
7237 (interactive)
7238 (let (blist)
7239 (dolist (buf (buffer-list))
7240 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7241 (push buf blist)))
7242 (mapc 'kill-buffer blist)))
7243
7244 (defun org-agenda-execute (arg)
7245 "Execute another agenda command, keeping same window.
7246 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7247 in the agenda."
7248 (interactive "P")
7249 (let ((org-agenda-window-setup 'current-window))
7250 (org-agenda arg)))
7251
7252 (defun org-agenda-redo (&optional all)
7253 "Rebuild possibly ALL agenda view(s) in the current buffer."
7254 (interactive "P")
7255 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7256 (cpa (unless (eq all t) current-prefix-arg))
7257 (org-agenda-doing-sticky-redo org-agenda-sticky)
7258 (org-agenda-sticky nil)
7259 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7260 org-agenda-buffer-name))
7261 (org-agenda-keep-modes t)
7262 (tag-filter org-agenda-tag-filter)
7263 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7264 (top-hl-filter org-agenda-top-headline-filter)
7265 (cat-filter org-agenda-category-filter)
7266 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7267 (re-filter org-agenda-regexp-filter)
7268 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7269 (cols org-agenda-columns-active)
7270 (line (org-current-line))
7271 (window-line (- line (org-current-line (window-start))))
7272 (lprops (get 'org-agenda-redo-command 'org-lprops))
7273 (redo-cmd (get-text-property p 'org-redo-cmd))
7274 (last-args (get-text-property p 'org-last-args))
7275 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7276 (org-agenda-overriding-cmd-arguments
7277 (unless (eq all t)
7278 (cond ((listp last-args)
7279 (cons (or cpa (car last-args)) (cdr last-args)))
7280 ((stringp last-args)
7281 last-args))))
7282 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7283 (put 'org-agenda-tag-filter :preset-filter nil)
7284 (put 'org-agenda-category-filter :preset-filter nil)
7285 (put 'org-agenda-regexp-filter :preset-filter nil)
7286 (and cols (org-columns-quit))
7287 (message "Rebuilding agenda buffer...")
7288 (if series-redo-cmd
7289 (eval series-redo-cmd)
7290 (org-let lprops redo-cmd))
7291 (setq org-agenda-undo-list nil
7292 org-agenda-pending-undo-list nil
7293 org-agenda-tag-filter tag-filter
7294 org-agenda-category-filter cat-filter
7295 org-agenda-regexp-filter re-filter
7296 org-agenda-top-headline-filter top-hl-filter)
7297 (message "Rebuilding agenda buffer...done")
7298 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7299 (put 'org-agenda-category-filter :preset-filter cat-preset)
7300 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7301 (let ((tag (or tag-filter tag-preset))
7302 (cat (or cat-filter cat-preset))
7303 (re (or re-filter re-preset)))
7304 (when tag (org-agenda-filter-apply tag 'tag))
7305 (when cat (org-agenda-filter-apply cat 'category))
7306 (when re (org-agenda-filter-apply re 'regexp)))
7307 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7308 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7309 (org-goto-line line)
7310 (recenter window-line)))
7311
7312 (defvar org-global-tags-completion-table nil)
7313 (defvar org-agenda-filter-form nil)
7314 (defvar org-agenda-filtered-by-category nil)
7315
7316 (defun org-agenda-filter-by-category (strip)
7317 "Keep only those lines in the agenda buffer that have a specific category.
7318 The category is that of the current line."
7319 (interactive "P")
7320 (if (and org-agenda-filtered-by-category
7321 org-agenda-category-filter)
7322 (org-agenda-filter-show-all-cat)
7323 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7324 (cond
7325 ((and cat strip)
7326 (org-agenda-filter-apply
7327 (push (concat "-" cat) org-agenda-category-filter) 'category))
7328 ((and cat)
7329 (org-agenda-filter-apply
7330 (setq org-agenda-category-filter
7331 (list (concat "+" cat))) 'category))
7332 (t (error "No category at point"))))))
7333
7334 (defun org-find-top-headline (&optional pos)
7335 "Find the topmost parent headline and return it."
7336 (save-excursion
7337 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7338 (if pos (goto-char pos))
7339 ;; Skip up to the topmost parent
7340 (while (ignore-errors (outline-up-heading 1) t))
7341 (ignore-errors
7342 (nth 4 (org-heading-components))))))
7343
7344 (defvar org-agenda-filtered-by-top-headline nil)
7345 (defun org-agenda-filter-by-top-headline (strip)
7346 "Keep only those lines that are descendants from the same top headline.
7347 The top headline is that of the current line."
7348 (interactive "P")
7349 (if org-agenda-filtered-by-top-headline
7350 (progn
7351 (setq org-agenda-filtered-by-top-headline nil
7352 org-agenda-top-headline-filter nil)
7353 (org-agenda-filter-show-all-top-filter))
7354 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7355 (if toph (org-agenda-filter-top-headline-apply toph strip)
7356 (error "No top-level headline at point")))))
7357
7358 (defvar org-agenda-regexp-filter nil)
7359 (defun org-agenda-filter-by-regexp (strip)
7360 "Filter agenda entries by a regular expression.
7361 Regexp filters are cumulative.
7362 With no prefix argument, keep entries matching the regexp.
7363 With one prefix argument, filter out entries matching the regexp.
7364 With two prefix arguments, remove the regexp filters."
7365 (interactive "P")
7366 (if (not (equal strip '(16)))
7367 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7368 (read-from-minibuffer
7369 (if (equal strip '(4))
7370 "Filter out entries matching regexp: "
7371 "Narrow to entries matching regexp: ")))))
7372 (push flt org-agenda-regexp-filter)
7373 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7374 (org-agenda-filter-show-all-re)
7375 (message "Regexp filter removed")))
7376
7377 (defun org-agenda-filter-remove-all ()
7378 "Remove all filters from the current agenda buffer."
7379 (interactive)
7380 (when org-agenda-tag-filter
7381 (org-agenda-filter-show-all-tag))
7382 (when org-agenda-category-filter
7383 (org-agenda-filter-show-all-cat))
7384 (when org-agenda-regexp-filter
7385 (org-agenda-filter-show-all-re))
7386 (when org-agenda-top-headline-filter
7387 (org-agenda-filter-show-all-top-filter))
7388 (org-agenda-finalize))
7389
7390 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7391 "Keep only those lines in the agenda buffer that have a specific tag.
7392 The tag is selected with its fast selection letter, as configured.
7393 With prefix argument STRIP, remove all lines that do have the tag.
7394 A lisp caller can specify CHAR. NARROW means that the new tag should be
7395 used to narrow the search - the interactive user can also press `-' or `+'
7396 to switch to narrowing."
7397 (interactive "P")
7398 (let* ((alist org-tag-alist-for-agenda)
7399 (tag-chars (mapconcat
7400 (lambda (x) (if (and (not (symbolp (car x)))
7401 (cdr x))
7402 (char-to-string (cdr x))
7403 ""))
7404 alist ""))
7405 (efforts (org-split-string
7406 (or (cdr (assoc (concat org-effort-property "_ALL")
7407 org-global-properties))
7408 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7409 "")))
7410 (effort-op org-agenda-filter-effort-default-operator)
7411 (effort-prompt "")
7412 (inhibit-read-only t)
7413 (current org-agenda-tag-filter)
7414 maybe-refresh a n tag)
7415 (unless char
7416 (message
7417 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7418 (if narrow "Narrow" "Filter") tag-chars
7419 (if org-agenda-auto-exclude-function "[RET], " ""))
7420 (setq char (read-char-exclusive)))
7421 (when (member char '(?+ ?-))
7422 ;; Narrowing down
7423 (cond ((equal char ?-) (setq strip t narrow t))
7424 ((equal char ?+) (setq strip nil narrow t)))
7425 (message
7426 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7427 (setq char (read-char-exclusive)))
7428 (when (member char '(?< ?> ?= ??))
7429 ;; An effort operator
7430 (setq effort-op (char-to-string char))
7431 (setq alist nil) ; to make sure it will be interpreted as effort.
7432 (unless (equal char ??)
7433 (loop for i from 0 to 9 do
7434 (setq effort-prompt
7435 (concat
7436 effort-prompt " ["
7437 (if (= i 9) "0" (int-to-string (1+ i)))
7438 "]" (nth i efforts))))
7439 (message "Effort%s: %s " effort-op effort-prompt)
7440 (setq char (read-char-exclusive))
7441 (when (or (< char ?0) (> char ?9))
7442 (error "Need 1-9,0 to select effort"))))
7443 (when (equal char ?\t)
7444 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7445 (org-set-local 'org-global-tags-completion-table
7446 (org-global-tags-completion-table)))
7447 (let ((completion-ignore-case t))
7448 (setq tag (org-icompleting-read
7449 "Tag: " org-global-tags-completion-table))))
7450 (cond
7451 ((equal char ?\r)
7452 (org-agenda-filter-show-all-tag)
7453 (when org-agenda-auto-exclude-function
7454 (setq org-agenda-tag-filter nil)
7455 (dolist (tag (org-agenda-get-represented-tags))
7456 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7457 (if modifier
7458 (push modifier org-agenda-tag-filter))))
7459 (if (not (null org-agenda-tag-filter))
7460 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7461 (setq maybe-refresh t))
7462 ((equal char ?/)
7463 (org-agenda-filter-show-all-tag)
7464 (when (get 'org-agenda-tag-filter :preset-filter)
7465 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7466 (setq maybe-refresh t))
7467 ((equal char ?. )
7468 (setq org-agenda-tag-filter
7469 (mapcar (lambda(tag) (concat "+" tag))
7470 (org-get-at-bol 'tags)))
7471 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7472 (setq maybe-refresh t))
7473 ((or (equal char ?\ )
7474 (setq a (rassoc char alist))
7475 (and (>= char ?0) (<= char ?9)
7476 (setq n (if (= char ?0) 9 (- char ?0 1))
7477 tag (concat effort-op (nth n efforts))
7478 a (cons tag nil)))
7479 (and (= char ??)
7480 (setq tag "?eff")
7481 a (cons tag nil))
7482 (and tag (setq a (cons tag nil))))
7483 (org-agenda-filter-show-all-tag)
7484 (setq tag (car a))
7485 (setq org-agenda-tag-filter
7486 (cons (concat (if strip "-" "+") tag)
7487 (if narrow current nil)))
7488 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7489 (setq maybe-refresh t))
7490 (t (error "Invalid tag selection character %c" char)))
7491 (when maybe-refresh
7492 (org-agenda-redo))))
7493
7494 (defun org-agenda-get-represented-tags ()
7495 "Get a list of all tags currently represented in the agenda."
7496 (let (p tags)
7497 (save-excursion
7498 (goto-char (point-min))
7499 (while (setq p (next-single-property-change (point) 'tags))
7500 (goto-char p)
7501 (mapc (lambda (x) (add-to-list 'tags x))
7502 (get-text-property (point) 'tags))))
7503 tags))
7504
7505 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7506 "Refine the current filter. See `org-agenda-filter-by-tag'."
7507 (interactive "P")
7508 (org-agenda-filter-by-tag strip char 'refine))
7509
7510 (defun org-agenda-filter-make-matcher (filter type)
7511 "Create the form that tests a line for agenda filter."
7512 (let (f f1)
7513 (cond
7514 ;; Tag filter
7515 ((eq type 'tag)
7516 (setq filter
7517 (delete-dups
7518 (append (get 'org-agenda-tag-filter :preset-filter)
7519 filter)))
7520 (dolist (x filter)
7521 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7522 (ffunc
7523 (lambda (nf0 nf01 fltr notgroup op)
7524 (dolist (x fltr)
7525 (if (member x '("-" "+"))
7526 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7527 (if (string-match "[<=>?]" x)
7528 (setq nf01 (org-agenda-filter-effort-form x))
7529 (setq nf01 (list 'member (downcase (substring x 1))
7530 'tags)))
7531 (when (equal (string-to-char x) ?-)
7532 (setq nf01 (list 'not nf01))
7533 (when (not notgroup) (setq op 'and))))
7534 (push nf01 nf0))
7535 (if notgroup
7536 (push (cons 'and nf0) f)
7537 (push (cons (or op 'or) nf0) f)))))
7538 (cond ((equal filter '("+"))
7539 (setq f (list (list 'not 'tags))))
7540 ((equal nfilter filter)
7541 (funcall ffunc f1 f filter t nil))
7542 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7543 ;; Category filter
7544 ((eq type 'category)
7545 (setq filter
7546 (delete-dups
7547 (append (get 'org-agenda-category-filter :preset-filter)
7548 filter)))
7549 (dolist (x filter)
7550 (if (equal "-" (substring x 0 1))
7551 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7552 (setq f1 (list 'equal (substring x 1) 'cat)))
7553 (push f1 f)))
7554 ;; Regexp filter
7555 ((eq type 'regexp)
7556 (setq filter
7557 (delete-dups
7558 (append (get 'org-agenda-regexp-filter :preset-filter)
7559 filter)))
7560 (dolist (x filter)
7561 (if (equal "-" (substring x 0 1))
7562 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7563 (setq f1 (list 'string-match (substring x 1) 'txt)))
7564 (push f1 f))))
7565 (cons 'and (nreverse f))))
7566
7567 (defun org-agenda-filter-effort-form (e)
7568 "Return the form to compare the effort of the current line with what E says.
7569 E looks like \"+<2:25\"."
7570 (let (op)
7571 (setq e (substring e 1))
7572 (setq op (string-to-char e) e (substring e 1))
7573 (setq op (cond ((equal op ?<) '<=)
7574 ((equal op ?>) '>=)
7575 ((equal op ??) op)
7576 (t '=)))
7577 (list 'org-agenda-compare-effort (list 'quote op)
7578 (org-duration-string-to-minutes e))))
7579
7580 (defun org-agenda-compare-effort (op value)
7581 "Compare the effort of the current line with VALUE, using OP.
7582 If the line does not have an effort defined, return nil."
7583 (let ((eff (org-get-at-bol 'effort-minutes)))
7584 (if (equal op ??)
7585 (not eff)
7586 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7587 value))))
7588
7589 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7590 "Expand group tags in FILTER for the agenda.
7591 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7592 (if org-group-tags
7593 (let ((case-fold-search t) rtn)
7594 (mapc
7595 (lambda (f)
7596 (let (f0 dir)
7597 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7598 (setq dir (match-string 1 f) f0 (match-string 2 f))
7599 (setq dir (if no-operator "" "+") f0 f))
7600 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7601 (org-tags-expand f0 t t))
7602 rtn))))
7603 filter)
7604 (reverse rtn))
7605 filter))
7606
7607 (defun org-agenda-filter-apply (filter type)
7608 "Set FILTER as the new agenda filter and apply it."
7609 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7610 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7611 (let (tags cat txt)
7612 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type))
7613 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7614 ;; category is used as the filter:
7615 (setq org-agenda-filtered-by-category
7616 (and (eq type 'category)
7617 (not (equal (substring (car filter) 0 1) "-"))))
7618 (org-agenda-set-mode-name)
7619 (save-excursion
7620 (goto-char (point-min))
7621 (while (not (eobp))
7622 (if (org-get-at-bol 'org-marker)
7623 (progn
7624 (setq tags ; used in eval
7625 (apply 'append
7626 (mapcar (lambda (f)
7627 (org-agenda-filter-expand-tags (list f) t))
7628 (org-get-at-bol 'tags)))
7629 cat (get-text-property (point) 'org-category)
7630 txt (get-text-property (point) 'txt))
7631 (if (not (eval org-agenda-filter-form))
7632 (org-agenda-filter-hide-line type))
7633 (beginning-of-line 2))
7634 (beginning-of-line 2))))
7635 (if (get-char-property (point) 'invisible)
7636 (ignore-errors (org-agenda-previous-line)))))
7637
7638 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7639 "Filter by top headline HL."
7640 (org-agenda-set-mode-name)
7641 (save-excursion
7642 (goto-char (point-min))
7643 (while (not (eobp))
7644 (let* ((pos (org-get-at-bol 'org-hd-marker))
7645 (tophl (and pos (org-find-top-headline pos))))
7646 (if (and tophl (funcall (if negative 'identity 'not)
7647 (string= hl tophl)))
7648 (org-agenda-filter-hide-line 'top-headline)))
7649 (beginning-of-line 2)))
7650 (if (get-char-property (point) 'invisible)
7651 (org-agenda-previous-line))
7652 (setq org-agenda-top-headline-filter hl
7653 org-agenda-filtered-by-top-headline t))
7654
7655 (defun org-agenda-filter-hide-line (type)
7656 "Hide lines with TYPE in the agenda buffer."
7657 (let* ((b (max (point-min) (1- (point-at-bol))))
7658 (e (point-at-eol)))
7659 (let ((inhibit-read-only t))
7660 (add-text-properties
7661 b e `(invisible org-filtered org-filter-type ,type)))))
7662
7663 (defun org-agenda-remove-filter (type)
7664 (interactive)
7665 "Remove filter of type TYPE from the agenda buffer."
7666 (save-excursion
7667 (goto-char (point-min))
7668 (let ((inhibit-read-only t) pos)
7669 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7670 (goto-char pos)
7671 (remove-text-properties
7672 (point) (next-single-property-change (point) 'org-filter-type)
7673 `(invisible org-filtered org-filter-type ,type))))
7674 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7675 (setq org-agenda-filter-form nil)
7676 (org-agenda-set-mode-name)
7677 (org-agenda-finalize)))
7678
7679 (defun org-agenda-filter-show-all-tag nil
7680 (org-agenda-remove-filter 'tag))
7681 (defun org-agenda-filter-show-all-re nil
7682 (org-agenda-remove-filter 'regexp))
7683 (defun org-agenda-filter-show-all-cat nil
7684 (org-agenda-remove-filter 'category))
7685 (defun org-agenda-filter-show-all-top-filter nil
7686 (org-agenda-remove-filter 'top-headline))
7687
7688 (defun org-agenda-manipulate-query-add ()
7689 "Manipulate the query by adding a search term with positive selection.
7690 Positive selection means the term must be matched for selection of an entry."
7691 (interactive)
7692 (org-agenda-manipulate-query ?\[))
7693 (defun org-agenda-manipulate-query-subtract ()
7694 "Manipulate the query by adding a search term with negative selection.
7695 Negative selection means term must not be matched for selection of an entry."
7696 (interactive)
7697 (org-agenda-manipulate-query ?\]))
7698 (defun org-agenda-manipulate-query-add-re ()
7699 "Manipulate the query by adding a search regexp with positive selection.
7700 Positive selection means the regexp must match for selection of an entry."
7701 (interactive)
7702 (org-agenda-manipulate-query ?\{))
7703 (defun org-agenda-manipulate-query-subtract-re ()
7704 "Manipulate the query by adding a search regexp with negative selection.
7705 Negative selection means regexp must not match for selection of an entry."
7706 (interactive)
7707 (org-agenda-manipulate-query ?\}))
7708 (defun org-agenda-manipulate-query (char)
7709 (cond
7710 ((memq org-agenda-type '(timeline agenda))
7711 (let ((org-agenda-include-inactive-timestamps t))
7712 (org-agenda-redo))
7713 (message "Display now includes inactive timestamps as well"))
7714 ((eq org-agenda-type 'search)
7715 (org-add-to-string
7716 'org-agenda-query-string
7717 (if org-agenda-last-search-view-search-was-boolean
7718 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7719 (?\{ . " +{}") (?\} . " -{}"))))
7720 " "))
7721 (setq org-agenda-redo-command
7722 (list 'org-search-view
7723 (car (get-text-property (min (1- (point-max)) (point))
7724 'org-last-args))
7725 org-agenda-query-string
7726 (+ (length org-agenda-query-string)
7727 (if (member char '(?\{ ?\})) 0 1))))
7728 (set-register org-agenda-query-register org-agenda-query-string)
7729 (let ((org-agenda-overriding-arguments
7730 (cdr org-agenda-redo-command)))
7731 (org-agenda-redo)))
7732 (t (error "Cannot manipulate query for %s-type agenda buffers"
7733 org-agenda-type))))
7734
7735 (defun org-add-to-string (var string)
7736 (set var (concat (symbol-value var) string)))
7737
7738 (defun org-agenda-goto-date (span)
7739 "Jump to DATE in agenda."
7740 (interactive "P")
7741 (let* ((org-read-date-prefer-future
7742 (eval org-agenda-jump-prefer-future))
7743 (date (org-read-date))
7744 (day (time-to-days (org-time-string-to-time date)))
7745 (org-agenda-sticky-orig org-agenda-sticky)
7746 (org-agenda-buffer-tmp-name (buffer-name))
7747 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7748 (0-arg (or current-prefix-arg (car args)))
7749 (2-arg (nth 2 args))
7750 (with-hour-p (nth 4 org-agenda-redo-command))
7751 (newcmd (list 'org-agenda-list 0-arg date
7752 (org-agenda-span-to-ndays
7753 2-arg (org-time-string-to-absolute date))
7754 with-hour-p))
7755 (newargs (cdr newcmd))
7756 (inhibit-read-only t)
7757 org-agenda-sticky)
7758 (if (not (org-agenda-check-type t 'agenda))
7759 (error "Not available in non-agenda views")
7760 (add-text-properties (point-min) (point-max)
7761 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7762 (org-agenda-redo)
7763 (goto-char (point-min))
7764 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7765 (save-excursion (move-beginning-of-line 2) (eobp))))
7766 (move-beginning-of-line 2))
7767 (setq org-agenda-sticky org-agenda-sticky-orig
7768 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7769
7770 (defun org-agenda-goto-today ()
7771 "Go to today."
7772 (interactive)
7773 (org-agenda-check-type t 'timeline 'agenda)
7774 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7775 (curspan (nth 2 args))
7776 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7777 (cond
7778 (tdpos (goto-char tdpos))
7779 ((eq org-agenda-type 'agenda)
7780 (let* ((sd (org-agenda-compute-starting-span
7781 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7782 (org-agenda-overriding-arguments args))
7783 (setf (nth 1 org-agenda-overriding-arguments) sd)
7784 (org-agenda-redo)
7785 (org-agenda-find-same-or-today-or-agenda)))
7786 (t (error "Cannot find today")))))
7787
7788 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7789 (goto-char
7790 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7791 (text-property-any (point-min) (point-max) 'org-today t)
7792 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7793 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7794 (org-agenda-goto-block-beginning))
7795 (point-min))))
7796
7797 (defun org-agenda-goto-block-beginning ()
7798 "Go the agenda block beginning."
7799 (interactive)
7800 (if (not (derived-mode-p 'org-agenda-mode))
7801 (error "Cannot execute this command outside of org-agenda-mode buffers")
7802 (let (dest)
7803 (save-excursion
7804 (unless (looking-at "\\'")
7805 (forward-char))
7806 (let* ((prop 'org-agenda-structural-header)
7807 (p (previous-single-property-change (point) prop))
7808 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7809 (1- (point))) prop)))
7810 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7811 (if (not dest)
7812 (error "Cannot find the beginning of the blog")
7813 (goto-char dest)
7814 (move-beginning-of-line 1)))))
7815
7816 (defun org-agenda-later (arg)
7817 "Go forward in time by the current span.
7818 With prefix ARG, go forward that many times the current span."
7819 (interactive "p")
7820 (org-agenda-check-type t 'agenda)
7821 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7822 (span (or (nth 2 args) org-agenda-current-span))
7823 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7824 (greg (calendar-gregorian-from-absolute sd))
7825 (cnt (org-get-at-bol 'org-day-cnt))
7826 greg2)
7827 (cond
7828 ((numberp span)
7829 (setq sd (+ (* span arg) sd)))
7830 ((eq span 'day)
7831 (setq sd (+ arg sd)))
7832 ((eq span 'week)
7833 (setq sd (+ (* 7 arg) sd)))
7834 ((eq span 'fortnight)
7835 (setq sd (+ (* 14 arg) sd)))
7836 ((eq span 'month)
7837 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7838 sd (calendar-absolute-from-gregorian greg2))
7839 (setcar greg2 (1+ (car greg2))))
7840 ((eq span 'year)
7841 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7842 sd (calendar-absolute-from-gregorian greg2))
7843 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7844 (t
7845 (setq sd (+ (* span arg) sd))))
7846 (let ((org-agenda-overriding-cmd
7847 ;; `cmd' may have been set by `org-agenda-run-series' which
7848 ;; uses `org-agenda-overriding-cmd' to decide whether
7849 ;; overriding is allowed for `cmd'
7850 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7851 (org-agenda-overriding-arguments
7852 (list (car args) sd span)))
7853 (org-agenda-redo)
7854 (org-agenda-find-same-or-today-or-agenda cnt))))
7855
7856 (defun org-agenda-earlier (arg)
7857 "Go backward in time by the current span.
7858 With prefix ARG, go backward that many times the current span."
7859 (interactive "p")
7860 (org-agenda-later (- arg)))
7861
7862 (defun org-agenda-view-mode-dispatch ()
7863 "Call one of the view mode commands."
7864 (interactive)
7865 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7866 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7867 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7868 (let ((a (read-char-exclusive)))
7869 (case a
7870 (?\ (call-interactively 'org-agenda-reset-view))
7871 (?d (call-interactively 'org-agenda-day-view))
7872 (?w (call-interactively 'org-agenda-week-view))
7873 (?t (call-interactively 'org-agenda-fortnight-view))
7874 (?m (call-interactively 'org-agenda-month-view))
7875 (?y (call-interactively 'org-agenda-year-view))
7876 (?l (call-interactively 'org-agenda-log-mode))
7877 (?L (org-agenda-log-mode '(4)))
7878 (?c (org-agenda-log-mode 'clockcheck))
7879 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7880 (?a (call-interactively 'org-agenda-archives-mode))
7881 (?A (org-agenda-archives-mode 'files))
7882 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7883 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7884 (?G (call-interactively 'org-agenda-toggle-time-grid))
7885 (?D (call-interactively 'org-agenda-toggle-diary))
7886 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7887 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7888 (org-agenda-check-type t 'timeline 'agenda)
7889 (org-agenda-redo))
7890 (message "Display now includes inactive timestamps as well"))
7891 (?q (message "Abort"))
7892 (otherwise (error "Invalid key" )))))
7893
7894 (defun org-agenda-reset-view ()
7895 "Switch to default view for agenda."
7896 (interactive)
7897 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7898 (defun org-agenda-day-view (&optional day-of-month)
7899 "Switch to daily view for agenda.
7900 With argument DAY-OF-MONTH, switch to that day of the month."
7901 (interactive "P")
7902 (org-agenda-change-time-span 'day day-of-month))
7903 (defun org-agenda-week-view (&optional iso-week)
7904 "Switch to daily view for agenda.
7905 With argument ISO-WEEK, switch to the corresponding ISO week.
7906 If ISO-WEEK has more then 2 digits, only the last two encode the
7907 week. Any digits before this encode a year. So 200712 means
7908 week 12 of year 2007. Years in the range 1938-2037 can also be
7909 written as 2-digit years."
7910 (interactive "P")
7911 (org-agenda-change-time-span 'week iso-week))
7912 (defun org-agenda-fortnight-view (&optional iso-week)
7913 "Switch to daily view for agenda.
7914 With argument ISO-WEEK, switch to the corresponding ISO week.
7915 If ISO-WEEK has more then 2 digits, only the last two encode the
7916 week. Any digits before this encode a year. So 200712 means
7917 week 12 of year 2007. Years in the range 1938-2037 can also be
7918 written as 2-digit years."
7919 (interactive "P")
7920 (org-agenda-change-time-span 'fortnight iso-week))
7921 (defun org-agenda-month-view (&optional month)
7922 "Switch to monthly view for agenda.
7923 With argument MONTH, switch to that month."
7924 (interactive "P")
7925 (org-agenda-change-time-span 'month month))
7926 (defun org-agenda-year-view (&optional year)
7927 "Switch to yearly view for agenda.
7928 With argument YEAR, switch to that year.
7929 If MONTH has more then 2 digits, only the last two encode the
7930 month. Any digits before this encode a year. So 200712 means
7931 December year 2007. Years in the range 1938-2037 can also be
7932 written as 2-digit years."
7933 (interactive "P")
7934 (when year
7935 (setq year (org-small-year-to-year year)))
7936 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7937 (org-agenda-change-time-span 'year year)
7938 (error "Abort")))
7939
7940 (defun org-agenda-change-time-span (span &optional n)
7941 "Change the agenda view to SPAN.
7942 SPAN may be `day', `week', `fortnight', `month', `year'."
7943 (org-agenda-check-type t 'agenda)
7944 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7945 (curspan (nth 2 args)))
7946 (if (and (not n) (equal curspan span))
7947 (error "Viewing span is already \"%s\"" span))
7948 (let* ((sd (or (org-get-at-bol 'day)
7949 (nth 1 args)
7950 org-starting-day))
7951 (sd (org-agenda-compute-starting-span sd span n))
7952 (org-agenda-overriding-cmd
7953 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7954 (org-agenda-overriding-arguments
7955 (list (car args) sd span)))
7956 (org-agenda-redo)
7957 (org-agenda-find-same-or-today-or-agenda))
7958 (org-agenda-set-mode-name)
7959 (message "Switched to %s view" span)))
7960
7961 (defun org-agenda-compute-starting-span (sd span &optional n)
7962 "Compute starting date for agenda.
7963 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7964 is a cons cell with the starting date and the number of days,
7965 so that the date SD will be in that range."
7966 (let* ((greg (calendar-gregorian-from-absolute sd))
7967 (dg (nth 1 greg))
7968 (mg (car greg))
7969 (yg (nth 2 greg)))
7970 (cond
7971 ((eq span 'day)
7972 (when n
7973 (setq sd (+ (calendar-absolute-from-gregorian
7974 (list mg 1 yg))
7975 n -1))))
7976 ((or (eq span 'week) (eq span 'fortnight))
7977 (let* ((nt (calendar-day-of-week
7978 (calendar-gregorian-from-absolute sd)))
7979 (d (if org-agenda-start-on-weekday
7980 (- nt org-agenda-start-on-weekday)
7981 0))
7982 y1)
7983 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7984 (when n
7985 (require 'cal-iso)
7986 (when (> n 99)
7987 (setq y1 (org-small-year-to-year (/ n 100))
7988 n (mod n 100)))
7989 (setq sd
7990 (calendar-absolute-from-iso
7991 (list n 1
7992 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7993 ((eq span 'month)
7994 (let (y1)
7995 (when (and n (> n 99))
7996 (setq y1 (org-small-year-to-year (/ n 100))
7997 n (mod n 100)))
7998 (setq sd (calendar-absolute-from-gregorian
7999 (list (or n mg) 1 (or y1 yg))))))
8000 ((eq span 'year)
8001 (setq sd (calendar-absolute-from-gregorian
8002 (list 1 1 (or n yg))))))
8003 sd))
8004
8005 (defun org-agenda-next-date-line (&optional arg)
8006 "Jump to the next line indicating a date in agenda buffer."
8007 (interactive "p")
8008 (org-agenda-check-type t 'agenda 'timeline)
8009 (beginning-of-line 1)
8010 ;; This does not work if user makes date format that starts with a blank
8011 (if (looking-at "^\\S-") (forward-char 1))
8012 (if (not (re-search-forward "^\\S-" nil t arg))
8013 (progn
8014 (backward-char 1)
8015 (error "No next date after this line in this buffer")))
8016 (goto-char (match-beginning 0)))
8017
8018 (defun org-agenda-previous-date-line (&optional arg)
8019 "Jump to the previous line indicating a date in agenda buffer."
8020 (interactive "p")
8021 (org-agenda-check-type t 'agenda 'timeline)
8022 (beginning-of-line 1)
8023 (if (not (re-search-backward "^\\S-" nil t arg))
8024 (error "No previous date before this line in this buffer")))
8025
8026 ;; Initialize the highlight
8027 (defvar org-hl (make-overlay 1 1))
8028 (overlay-put org-hl 'face 'highlight)
8029
8030 (defun org-highlight (begin end &optional buffer)
8031 "Highlight a region with overlay."
8032 (move-overlay org-hl begin end (or buffer (current-buffer))))
8033
8034 (defun org-unhighlight ()
8035 "Detach overlay INDEX."
8036 (org-detach-overlay org-hl))
8037
8038 (defun org-unhighlight-once ()
8039 "Remove the highlight from its position, and this function from the hook."
8040 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8041 (org-unhighlight))
8042
8043 (defvar org-agenda-pre-follow-window-conf nil)
8044 (defun org-agenda-follow-mode ()
8045 "Toggle follow mode in an agenda buffer."
8046 (interactive)
8047 (unless org-agenda-follow-mode
8048 (setq org-agenda-pre-follow-window-conf
8049 (current-window-configuration)))
8050 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8051 (unless org-agenda-follow-mode
8052 (set-window-configuration org-agenda-pre-follow-window-conf))
8053 (org-agenda-set-mode-name)
8054 (org-agenda-do-context-action)
8055 (message "Follow mode is %s"
8056 (if org-agenda-follow-mode "on" "off")))
8057
8058 (defun org-agenda-entry-text-mode (&optional arg)
8059 "Toggle entry text mode in an agenda buffer."
8060 (interactive "P")
8061 (if (or org-agenda-tag-filter
8062 org-agenda-category-filter
8063 org-agenda-regexp-filter
8064 org-agenda-top-headline-filter)
8065 (user-error "Can't show entry text in filtered views")
8066 (setq org-agenda-entry-text-mode (or (integerp arg)
8067 (not org-agenda-entry-text-mode)))
8068 (org-agenda-entry-text-hide)
8069 (and org-agenda-entry-text-mode
8070 (let ((org-agenda-entry-text-maxlines
8071 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8072 (org-agenda-entry-text-show)))
8073 (org-agenda-set-mode-name)
8074 (message "Entry text mode is %s%s"
8075 (if org-agenda-entry-text-mode "on" "off")
8076 (if (not org-agenda-entry-text-mode) ""
8077 (format " (maximum number of lines is %d)"
8078 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8079
8080 (defun org-agenda-clockreport-mode ()
8081 "Toggle clocktable mode in an agenda buffer."
8082 (interactive)
8083 (org-agenda-check-type t 'agenda)
8084 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8085 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8086 (org-agenda-set-mode-name)
8087 (org-agenda-redo)
8088 (message "Clocktable mode is %s"
8089 (if org-agenda-clockreport-mode "on" "off")))
8090
8091 (defun org-agenda-log-mode (&optional special)
8092 "Toggle log mode in an agenda buffer.
8093 With argument SPECIAL, show all possible log items, not only the ones
8094 configured in `org-agenda-log-mode-items'.
8095 With a double `C-u' prefix arg, show *only* log items, nothing else."
8096 (interactive "P")
8097 (org-agenda-check-type t 'agenda 'timeline)
8098 (setq org-agenda-show-log
8099 (cond
8100 ((equal special '(16)) 'only)
8101 ((eq special 'clockcheck)
8102 (if (eq org-agenda-show-log 'clockcheck)
8103 nil 'clockcheck))
8104 (special '(closed clock state))
8105 (t (not org-agenda-show-log))))
8106 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8107 (org-agenda-set-mode-name)
8108 (org-agenda-redo)
8109 (message "Log mode is %s"
8110 (if org-agenda-show-log "on" "off")))
8111
8112 (defun org-agenda-archives-mode (&optional with-files)
8113 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8114 When called with a prefix argument, include all archive files as well."
8115 (interactive "P")
8116 (setq org-agenda-archives-mode
8117 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8118 (org-agenda-set-mode-name)
8119 (org-agenda-redo)
8120 (message
8121 "%s"
8122 (cond
8123 ((eq org-agenda-archives-mode nil)
8124 "No archives are included")
8125 ((eq org-agenda-archives-mode 'trees)
8126 (format "Trees with :%s: tag are included" org-archive-tag))
8127 ((eq org-agenda-archives-mode t)
8128 (format "Trees with :%s: tag and all active archive files are included"
8129 org-archive-tag)))))
8130
8131 (defun org-agenda-toggle-diary ()
8132 "Toggle diary inclusion in an agenda buffer."
8133 (interactive)
8134 (org-agenda-check-type t 'agenda)
8135 (setq org-agenda-include-diary (not org-agenda-include-diary))
8136 (org-agenda-redo)
8137 (org-agenda-set-mode-name)
8138 (message "Diary inclusion turned %s"
8139 (if org-agenda-include-diary "on" "off")))
8140
8141 (defun org-agenda-toggle-deadlines ()
8142 "Toggle inclusion of entries with a deadline in an agenda buffer."
8143 (interactive)
8144 (org-agenda-check-type t 'agenda)
8145 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8146 (org-agenda-redo)
8147 (org-agenda-set-mode-name)
8148 (message "Deadlines inclusion turned %s"
8149 (if org-agenda-include-deadlines "on" "off")))
8150
8151 (defun org-agenda-toggle-time-grid ()
8152 "Toggle time grid in an agenda buffer."
8153 (interactive)
8154 (org-agenda-check-type t 'agenda)
8155 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8156 (org-agenda-redo)
8157 (org-agenda-set-mode-name)
8158 (message "Time-grid turned %s"
8159 (if org-agenda-use-time-grid "on" "off")))
8160
8161 (defun org-agenda-set-mode-name ()
8162 "Set the mode name to indicate all the small mode settings."
8163 (setq mode-name
8164 (list "Org-Agenda"
8165 (if (get 'org-agenda-files 'org-restrict) " []" "")
8166 " "
8167 '(:eval (org-agenda-span-name org-agenda-current-span))
8168 (if org-agenda-follow-mode " Follow" "")
8169 (if org-agenda-entry-text-mode " ETxt" "")
8170 (if org-agenda-include-diary " Diary" "")
8171 (if org-agenda-include-deadlines " Ddl" "")
8172 (if org-agenda-use-time-grid " Grid" "")
8173 (if (and (boundp 'org-habit-show-habits)
8174 org-habit-show-habits) " Habit" "")
8175 (cond
8176 ((consp org-agenda-show-log) " LogAll")
8177 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8178 (org-agenda-show-log " Log")
8179 (t ""))
8180 (if (or org-agenda-category-filter
8181 (get 'org-agenda-category-filter :preset-filter))
8182 '(:eval (org-propertize
8183 (concat " <"
8184 (mapconcat
8185 'identity
8186 (append
8187 (get 'org-agenda-category-filter :preset-filter)
8188 org-agenda-category-filter)
8189 "")
8190 ">")
8191 'face 'org-agenda-filter-category
8192 'help-echo "Category used in filtering")) "")
8193 (if (or org-agenda-tag-filter
8194 (get 'org-agenda-tag-filter :preset-filter))
8195 '(:eval (org-propertize
8196 (concat " {"
8197 (mapconcat
8198 'identity
8199 (append
8200 (get 'org-agenda-tag-filter :preset-filter)
8201 org-agenda-tag-filter)
8202 "")
8203 "}")
8204 'face 'org-agenda-filter-tags
8205 'help-echo "Tags used in filtering")) "")
8206 (if (or org-agenda-regexp-filter
8207 (get 'org-agenda-regexp-filter :preset-filter))
8208 '(:eval (org-propertize
8209 (concat " ["
8210 (mapconcat
8211 'identity
8212 (append
8213 (get 'org-agenda-regexp-filter :preset-filter)
8214 org-agenda-regexp-filter)
8215 "")
8216 "]")
8217 'face 'org-agenda-filter-regexp
8218 'help-echo "Regexp used in filtering")) "")
8219 (if org-agenda-archives-mode
8220 (if (eq org-agenda-archives-mode t)
8221 " Archives"
8222 (format " :%s:" org-archive-tag))
8223 "")
8224 (if org-agenda-clockreport-mode " Clock" "")))
8225 (force-mode-line-update))
8226
8227 (define-obsolete-function-alias
8228 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8229
8230 (defun org-agenda-update-agenda-type ()
8231 "Update the agenda type after each command."
8232 (setq org-agenda-type
8233 (or (get-text-property (point) 'org-agenda-type)
8234 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8235
8236 (defun org-agenda-next-line ()
8237 "Move cursor to the next line, and show if follow mode is active."
8238 (interactive)
8239 (call-interactively 'next-line)
8240 (org-agenda-do-context-action))
8241
8242 (defun org-agenda-previous-line ()
8243 "Move cursor to the previous line, and show if follow-mode is active."
8244 (interactive)
8245 (call-interactively 'previous-line)
8246 (org-agenda-do-context-action))
8247
8248 (defun org-agenda-next-item (n)
8249 "Move cursor to next agenda item."
8250 (interactive "p")
8251 (let ((col (current-column)))
8252 (dotimes (c n)
8253 (when (next-single-property-change (point-at-eol) 'org-marker)
8254 (move-end-of-line 1)
8255 (goto-char (next-single-property-change (point) 'org-marker))))
8256 (org-move-to-column col))
8257 (org-agenda-do-context-action))
8258
8259 (defun org-agenda-previous-item (n)
8260 "Move cursor to next agenda item."
8261 (interactive "p")
8262 (dotimes (c n)
8263 (let ((col (current-column))
8264 (goto (save-excursion
8265 (move-end-of-line 0)
8266 (previous-single-property-change (point) 'org-marker))))
8267 (if goto (goto-char goto))
8268 (org-move-to-column col)))
8269 (org-agenda-do-context-action))
8270
8271 (defun org-agenda-do-context-action ()
8272 "Show outline path and, maybe, follow mode window."
8273 (let ((m (org-get-at-bol 'org-marker)))
8274 (when (and (markerp m) (marker-buffer m))
8275 (and org-agenda-follow-mode
8276 (if org-agenda-follow-indirect
8277 (org-agenda-tree-to-indirect-buffer nil)
8278 (org-agenda-show)))
8279 (and org-agenda-show-outline-path
8280 (org-with-point-at m (org-display-outline-path t))))))
8281
8282 (defun org-agenda-show-tags ()
8283 "Show the tags applicable to the current item."
8284 (interactive)
8285 (let* ((tags (org-get-at-bol 'tags)))
8286 (if tags
8287 (message "Tags are :%s:"
8288 (org-no-properties (mapconcat 'identity tags ":")))
8289 (message "No tags associated with this line"))))
8290
8291 (defun org-agenda-goto (&optional highlight)
8292 "Go to the Org-mode file which contains the item at point."
8293 (interactive)
8294 (let* ((marker (or (org-get-at-bol 'org-marker)
8295 (org-agenda-error)))
8296 (buffer (marker-buffer marker))
8297 (pos (marker-position marker)))
8298 (switch-to-buffer-other-window buffer)
8299 (widen)
8300 (push-mark)
8301 (goto-char pos)
8302 (when (derived-mode-p 'org-mode)
8303 (org-show-context 'agenda)
8304 (save-excursion
8305 (and (outline-next-heading)
8306 (org-flag-heading nil)))) ; show the next heading
8307 (when (outline-invisible-p)
8308 (show-entry)) ; display invisible text
8309 (recenter (/ (window-height) 2))
8310 (run-hooks 'org-agenda-after-show-hook)
8311 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8312
8313 (defvar org-agenda-after-show-hook nil
8314 "Normal hook run after an item has been shown from the agenda.
8315 Point is in the buffer where the item originated.")
8316
8317 (defun org-agenda-kill ()
8318 "Kill the entry or subtree belonging to the current agenda entry."
8319 (interactive)
8320 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8321 (let* ((bufname-orig (buffer-name))
8322 (marker (or (org-get-at-bol 'org-marker)
8323 (org-agenda-error)))
8324 (buffer (marker-buffer marker))
8325 (pos (marker-position marker))
8326 (type (org-get-at-bol 'type))
8327 dbeg dend (n 0) conf)
8328 (org-with-remote-undo buffer
8329 (with-current-buffer buffer
8330 (save-excursion
8331 (goto-char pos)
8332 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8333 (setq dbeg (progn (org-back-to-heading t) (point))
8334 dend (org-end-of-subtree t t))
8335 (setq dbeg (point-at-bol)
8336 dend (min (point-max) (1+ (point-at-eol)))))
8337 (goto-char dbeg)
8338 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8339 (setq conf (or (eq t org-agenda-confirm-kill)
8340 (and (numberp org-agenda-confirm-kill)
8341 (> n org-agenda-confirm-kill))))
8342 (and conf
8343 (not (y-or-n-p
8344 (format "Delete entry with %d lines in buffer \"%s\"? "
8345 n (buffer-name buffer))))
8346 (error "Abort"))
8347 (let ((org-agenda-buffer-name bufname-orig))
8348 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8349 (with-current-buffer buffer (delete-region dbeg dend))
8350 (message "Agenda item and source killed"))))
8351
8352 (defvar org-archive-default-command) ; defined in org-archive.el
8353 (defun org-agenda-archive-default ()
8354 "Archive the entry or subtree belonging to the current agenda entry."
8355 (interactive)
8356 (require 'org-archive)
8357 (org-agenda-archive-with org-archive-default-command))
8358
8359 (defun org-agenda-archive-default-with-confirmation ()
8360 "Archive the entry or subtree belonging to the current agenda entry."
8361 (interactive)
8362 (require 'org-archive)
8363 (org-agenda-archive-with org-archive-default-command 'confirm))
8364
8365 (defun org-agenda-archive ()
8366 "Archive the entry or subtree belonging to the current agenda entry."
8367 (interactive)
8368 (org-agenda-archive-with 'org-archive-subtree))
8369
8370 (defun org-agenda-archive-to-archive-sibling ()
8371 "Move the entry to the archive sibling."
8372 (interactive)
8373 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8374
8375 (defun org-agenda-archive-with (cmd &optional confirm)
8376 "Move the entry to the archive sibling."
8377 (interactive)
8378 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8379 (let* ((bufname-orig (buffer-name))
8380 (marker (or (org-get-at-bol 'org-marker)
8381 (org-agenda-error)))
8382 (buffer (marker-buffer marker))
8383 (pos (marker-position marker)))
8384 (org-with-remote-undo buffer
8385 (with-current-buffer buffer
8386 (if (derived-mode-p 'org-mode)
8387 (if (and confirm
8388 (not (y-or-n-p "Archive this subtree or entry? ")))
8389 (error "Abort")
8390 (save-window-excursion
8391 (goto-char pos)
8392 (let ((org-agenda-buffer-name bufname-orig))
8393 (org-remove-subtree-entries-from-agenda))
8394 (org-back-to-heading t)
8395 (funcall cmd)))
8396 (error "Archiving works only in Org-mode files"))))))
8397
8398 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8399 "Remove all lines in the agenda that correspond to a given subtree.
8400 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8401 If this information is not given, the function uses the tree at point."
8402 (let ((buf (or buf (current-buffer))) m p)
8403 (save-excursion
8404 (unless (and beg end)
8405 (org-back-to-heading t)
8406 (setq beg (point))
8407 (org-end-of-subtree t)
8408 (setq end (point)))
8409 (set-buffer (get-buffer org-agenda-buffer-name))
8410 (save-excursion
8411 (goto-char (point-max))
8412 (beginning-of-line 1)
8413 (while (not (bobp))
8414 (when (and (setq m (org-get-at-bol 'org-marker))
8415 (equal buf (marker-buffer m))
8416 (setq p (marker-position m))
8417 (>= p beg)
8418 (< p end))
8419 (let ((inhibit-read-only t))
8420 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8421 (beginning-of-line 0))))))
8422
8423 (defun org-agenda-refile (&optional goto rfloc no-update)
8424 "Refile the item at point.
8425
8426 When GOTO is 0 or '(64), clear the refile cache.
8427 When GOTO is '(16), go to the location of the last refiled item.
8428 RFLOC can be a refile location obtained in a different way.
8429 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8430 (interactive "P")
8431 (cond
8432 ((member goto '(0 (64)))
8433 (org-refile-cache-clear))
8434 ((equal goto '(16))
8435 (org-refile-goto-last-stored))
8436 (t
8437 (let* ((buffer-orig (buffer-name))
8438 (marker (or (org-get-at-bol 'org-hd-marker)
8439 (org-agenda-error)))
8440 (buffer (marker-buffer marker))
8441 (pos (marker-position marker))
8442 (rfloc (or rfloc
8443 (org-refile-get-location
8444 (if goto "Goto" "Refile to") buffer
8445 org-refile-allow-creating-parent-nodes))))
8446 (with-current-buffer buffer
8447 (save-excursion
8448 (save-restriction
8449 (widen)
8450 (goto-char marker)
8451 (let ((org-agenda-buffer-name buffer-orig))
8452 (org-remove-subtree-entries-from-agenda))
8453 (org-refile goto buffer rfloc)))))
8454 (unless no-update (org-agenda-redo)))))
8455
8456 (defun org-agenda-open-link (&optional arg)
8457 "Open the link(s) in the current entry, if any.
8458 This looks for a link in the displayed line in the agenda.
8459 It also looks at the text of the entry itself."
8460 (interactive "P")
8461 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8462 (org-get-at-bol 'org-marker)))
8463 (buffer (and marker (marker-buffer marker)))
8464 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8465 (lkall (and buffer (org-offer-links-in-entry
8466 buffer marker arg prefix)))
8467 (lk0 (car lkall))
8468 (lk (if (stringp lk0) (list lk0) lk0))
8469 (lkend (cdr lkall))
8470 trg)
8471 (cond
8472 ((and buffer lk)
8473 (mapcar (lambda(l)
8474 (with-current-buffer buffer
8475 (setq trg (and (string-match org-bracket-link-regexp l)
8476 (match-string 1 l)))
8477 (if (or (not trg) (string-match org-any-link-re trg))
8478 (save-excursion
8479 (save-restriction
8480 (widen)
8481 (goto-char marker)
8482 (when (search-forward l nil lkend)
8483 (goto-char (match-beginning 0))
8484 (org-open-at-point))))
8485 ;; This is an internal link, widen the buffer
8486 (switch-to-buffer-other-window buffer)
8487 (widen)
8488 (goto-char marker)
8489 (when (search-forward l nil lkend)
8490 (goto-char (match-beginning 0))
8491 (org-open-at-point)))))
8492 lk))
8493 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8494 (save-excursion
8495 (beginning-of-line 1)
8496 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8497 (org-open-link-from-string (match-string 1)))
8498 (t (message "No link to open here")))))
8499
8500 (defun org-agenda-copy-local-variable (var)
8501 "Get a variable from a referenced buffer and install it here."
8502 (let ((m (org-get-at-bol 'org-marker)))
8503 (when (and m (buffer-live-p (marker-buffer m)))
8504 (org-set-local var (with-current-buffer (marker-buffer m)
8505 (symbol-value var))))))
8506
8507 (defun org-agenda-switch-to (&optional delete-other-windows)
8508 "Go to the Org-mode file which contains the item at point."
8509 (interactive)
8510 (if (and org-return-follows-link
8511 (not (org-get-at-bol 'org-marker))
8512 (org-in-regexp org-bracket-link-regexp))
8513 (org-open-link-from-string (match-string 0))
8514 (let* ((marker (or (org-get-at-bol 'org-marker)
8515 (org-agenda-error)))
8516 (buffer (marker-buffer marker))
8517 (pos (marker-position marker)))
8518 (org-pop-to-buffer-same-window buffer)
8519 (and delete-other-windows (delete-other-windows))
8520 (widen)
8521 (goto-char pos)
8522 (when (derived-mode-p 'org-mode)
8523 (org-show-context 'agenda)
8524 (save-excursion
8525 (and (outline-next-heading)
8526 (org-flag-heading nil))) ; show the next heading
8527 (when (outline-invisible-p)
8528 (show-entry)) ; display invisible text
8529 (run-hooks 'org-agenda-after-show-hook)))))
8530
8531 (defun org-agenda-goto-mouse (ev)
8532 "Go to the Org-mode file which contains the item at the mouse click."
8533 (interactive "e")
8534 (mouse-set-point ev)
8535 (org-agenda-goto))
8536
8537 (defun org-agenda-show (&optional full-entry)
8538 "Display the Org-mode file which contains the item at point.
8539 With prefix argument FULL-ENTRY, make the entire entry visible
8540 if it was hidden in the outline."
8541 (interactive "P")
8542 (let ((win (selected-window)))
8543 (if full-entry
8544 (let ((org-show-entry-below t))
8545 (org-agenda-goto t))
8546 (org-agenda-goto t))
8547 (select-window win)))
8548
8549 (defvar org-agenda-show-window nil)
8550 (defun org-agenda-show-and-scroll-up (&optional arg)
8551 "Display the Org-mode file which contains the item at point.
8552 When called repeatedly, scroll the window that is displaying the buffer.
8553 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8554 `show-subtree' to display the item, so that drawers and logbooks stay
8555 folded."
8556 (interactive "P")
8557 (let ((win (selected-window)))
8558 (if (and (window-live-p org-agenda-show-window)
8559 (eq this-command last-command))
8560 (progn
8561 (select-window org-agenda-show-window)
8562 (ignore-errors (scroll-up)))
8563 (org-agenda-goto t)
8564 (if arg (org-show-entry) (show-subtree))
8565 (setq org-agenda-show-window (selected-window)))
8566 (select-window win)))
8567
8568 (defun org-agenda-show-scroll-down ()
8569 "Scroll down the window showing the agenda."
8570 (interactive)
8571 (let ((win (selected-window)))
8572 (when (window-live-p org-agenda-show-window)
8573 (select-window org-agenda-show-window)
8574 (ignore-errors (scroll-down))
8575 (select-window win))))
8576
8577 (defun org-agenda-show-1 (&optional more)
8578 "Display the Org-mode file which contains the item at point.
8579 The prefix arg selects the amount of information to display:
8580
8581 0 hide the subtree
8582 1 just show the entry according to defaults.
8583 2 show the children view
8584 3 show the subtree view
8585 4 show the entire subtree and any LOGBOOK drawers
8586 5 show the entire subtree and any drawers
8587 With prefix argument FULL-ENTRY, make the entire entry visible
8588 if it was hidden in the outline."
8589 (interactive "p")
8590 (let ((win (selected-window)))
8591 (org-agenda-goto t)
8592 (org-back-to-heading)
8593 (set-window-start (selected-window) (point-at-bol))
8594 (cond
8595 ((= more 0)
8596 (hide-subtree)
8597 (save-excursion
8598 (org-back-to-heading)
8599 (run-hook-with-args 'org-cycle-hook 'folded))
8600 (message "Remote: FOLDED"))
8601 ((and (org-called-interactively-p 'any) (= more 1))
8602 (message "Remote: show with default settings"))
8603 ((= more 2)
8604 (show-entry)
8605 (show-children)
8606 (save-excursion
8607 (org-back-to-heading)
8608 (run-hook-with-args 'org-cycle-hook 'children))
8609 (message "Remote: CHILDREN"))
8610 ((= more 3)
8611 (show-subtree)
8612 (save-excursion
8613 (org-back-to-heading)
8614 (run-hook-with-args 'org-cycle-hook 'subtree))
8615 (message "Remote: SUBTREE"))
8616 ((= more 4)
8617 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8618 (org-drawer-regexp
8619 (concat "^[ \t]*:\\("
8620 (mapconcat 'regexp-quote org-drawers "\\|")
8621 "\\):[ \t]*$")))
8622 (show-subtree)
8623 (save-excursion
8624 (org-back-to-heading)
8625 (org-cycle-hide-drawers 'subtree)))
8626 (message "Remote: SUBTREE AND LOGBOOK"))
8627 ((> more 4)
8628 (show-subtree)
8629 (message "Remote: SUBTREE AND ALL DRAWERS")))
8630 (select-window win)))
8631
8632 (defvar org-agenda-cycle-counter nil)
8633 (defun org-agenda-cycle-show (&optional n)
8634 "Show the current entry in another window, with default settings.
8635 Default settings are taken from `org-show-hierarchy-above' and siblings.
8636 When use repeatedly in immediate succession, the remote entry will cycle
8637 through visibility
8638
8639 children -> subtree -> folded
8640
8641 When called with a numeric prefix arg, that arg will be passed through to
8642 `org-agenda-show-1'. For the interpretation of that argument, see the
8643 docstring of `org-agenda-show-1'."
8644 (interactive "P")
8645 (if (integerp n)
8646 (setq org-agenda-cycle-counter n)
8647 (if (not (eq last-command this-command))
8648 (setq org-agenda-cycle-counter 1)
8649 (if (equal org-agenda-cycle-counter 0)
8650 (setq org-agenda-cycle-counter 2)
8651 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8652 (if (> org-agenda-cycle-counter 3)
8653 (setq org-agenda-cycle-counter 0)))))
8654 (org-agenda-show-1 org-agenda-cycle-counter))
8655
8656 (defun org-agenda-recenter (arg)
8657 "Display the Org-mode file which contains the item at point and recenter."
8658 (interactive "P")
8659 (let ((win (selected-window)))
8660 (org-agenda-goto t)
8661 (recenter arg)
8662 (select-window win)))
8663
8664 (defun org-agenda-show-mouse (ev)
8665 "Display the Org-mode file which contains the item at the mouse click."
8666 (interactive "e")
8667 (mouse-set-point ev)
8668 (org-agenda-show))
8669
8670 (defun org-agenda-check-no-diary ()
8671 "Check if the entry is a diary link and abort if yes."
8672 (if (org-get-at-bol 'org-agenda-diary-link)
8673 (org-agenda-error)))
8674
8675 (defun org-agenda-error ()
8676 (error "Command not allowed in this line"))
8677
8678 (defun org-agenda-tree-to-indirect-buffer (arg)
8679 "Show the subtree corresponding to the current entry in an indirect buffer.
8680 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8681
8682 With a numerical prefix ARG, go up to this level and then take that tree.
8683 With a negative numeric ARG, go up by this number of levels.
8684 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8685 use the dedicated frame)."
8686 (interactive "P")
8687 (if current-prefix-arg
8688 (org-agenda-do-tree-to-indirect-buffer arg)
8689 (let ((agenda-buffer (buffer-name))
8690 (agenda-window (selected-window))
8691 (indirect-window
8692 (and org-last-indirect-buffer
8693 (get-buffer-window org-last-indirect-buffer))))
8694 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8695 (unless (or (eq org-indirect-buffer-display 'new-frame)
8696 (eq org-indirect-buffer-display 'dedicated-frame))
8697 (unwind-protect
8698 (unless (and indirect-window (window-live-p indirect-window))
8699 (setq indirect-window (split-window agenda-window)))
8700 (and indirect-window (select-window indirect-window))
8701 (switch-to-buffer org-last-indirect-buffer :norecord)
8702 (fit-window-to-buffer indirect-window)))
8703 (select-window (get-buffer-window agenda-buffer)))))
8704
8705 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8706 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8707 (org-agenda-check-no-diary)
8708 (let* ((marker (or (org-get-at-bol 'org-marker)
8709 (org-agenda-error)))
8710 (buffer (marker-buffer marker))
8711 (pos (marker-position marker)))
8712 (with-current-buffer buffer
8713 (save-excursion
8714 (goto-char pos)
8715 (funcall 'org-tree-to-indirect-buffer arg)))))
8716
8717 (defvar org-last-heading-marker (make-marker)
8718 "Marker pointing to the headline that last changed its TODO state
8719 by a remote command from the agenda.")
8720
8721 (defun org-agenda-todo-nextset ()
8722 "Switch TODO entry to next sequence."
8723 (interactive)
8724 (org-agenda-todo 'nextset))
8725
8726 (defun org-agenda-todo-previousset ()
8727 "Switch TODO entry to previous sequence."
8728 (interactive)
8729 (org-agenda-todo 'previousset))
8730
8731 (defun org-agenda-todo (&optional arg)
8732 "Cycle TODO state of line at point, also in Org-mode file.
8733 This changes the line at point, all other lines in the agenda referring to
8734 the same tree node, and the headline of the tree node in the Org-mode file."
8735 (interactive "P")
8736 (org-agenda-check-no-diary)
8737 (let* ((col (current-column))
8738 (marker (or (org-get-at-bol 'org-marker)
8739 (org-agenda-error)))
8740 (buffer (marker-buffer marker))
8741 (pos (marker-position marker))
8742 (hdmarker (org-get-at-bol 'org-hd-marker))
8743 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8744 (inhibit-read-only t)
8745 org-agenda-headline-snapshot-before-repeat newhead just-one)
8746 (org-with-remote-undo buffer
8747 (with-current-buffer buffer
8748 (widen)
8749 (goto-char pos)
8750 (org-show-context 'agenda)
8751 (save-excursion
8752 (and (outline-next-heading)
8753 (org-flag-heading nil))) ; show the next heading
8754 (let ((current-prefix-arg arg))
8755 (call-interactively 'org-todo))
8756 (and (bolp) (forward-char 1))
8757 (setq newhead (org-get-heading))
8758 (when (and (org-bound-and-true-p
8759 org-agenda-headline-snapshot-before-repeat)
8760 (not (equal org-agenda-headline-snapshot-before-repeat
8761 newhead))
8762 todayp)
8763 (setq newhead org-agenda-headline-snapshot-before-repeat
8764 just-one t))
8765 (save-excursion
8766 (org-back-to-heading)
8767 (move-marker org-last-heading-marker (point))))
8768 (beginning-of-line 1)
8769 (save-window-excursion
8770 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8771 (when (org-bound-and-true-p org-clock-out-when-done)
8772 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8773 newhead)
8774 (org-agenda-unmark-clocking-task))
8775 (org-move-to-column col))))
8776
8777 (defun org-agenda-add-note (&optional arg)
8778 "Add a time-stamped note to the entry at point."
8779 (interactive "P")
8780 (org-agenda-check-no-diary)
8781 (let* ((marker (or (org-get-at-bol 'org-marker)
8782 (org-agenda-error)))
8783 (buffer (marker-buffer marker))
8784 (pos (marker-position marker))
8785 (hdmarker (org-get-at-bol 'org-hd-marker))
8786 (inhibit-read-only t))
8787 (with-current-buffer buffer
8788 (widen)
8789 (goto-char pos)
8790 (org-show-context 'agenda)
8791 (save-excursion
8792 (and (outline-next-heading)
8793 (org-flag-heading nil))) ; show the next heading
8794 (org-add-note))))
8795
8796 (defun org-agenda-change-all-lines (newhead hdmarker
8797 &optional fixface just-this)
8798 "Change all lines in the agenda buffer which match HDMARKER.
8799 The new content of the line will be NEWHEAD (as modified by
8800 `org-agenda-format-item'). HDMARKER is checked with
8801 `equal' against all `org-hd-marker' text properties in the file.
8802 If FIXFACE is non-nil, the face of each item is modified according to
8803 the new TODO state.
8804 If JUST-THIS is non-nil, change just the current line, not all.
8805 If FORCE-TAGS is non nil, the car of it returns the new tags."
8806 (let* ((inhibit-read-only t)
8807 (line (org-current-line))
8808 (org-agenda-buffer (current-buffer))
8809 (thetags (with-current-buffer (marker-buffer hdmarker)
8810 (save-excursion (save-restriction (widen)
8811 (goto-char hdmarker)
8812 (org-get-tags-at)))))
8813 props m pl undone-face done-face finish new dotime level cat tags)
8814 (save-excursion
8815 (goto-char (point-max))
8816 (beginning-of-line 1)
8817 (while (not finish)
8818 (setq finish (bobp))
8819 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8820 (or (not just-this) (= (org-current-line) line))
8821 (equal m hdmarker))
8822 (setq props (text-properties-at (point))
8823 dotime (org-get-at-bol 'dotime)
8824 cat (org-get-at-bol 'org-category)
8825 level (org-get-at-bol 'level)
8826 tags thetags
8827 new
8828 (let ((org-prefix-format-compiled
8829 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8830 org-prefix-format-compiled))
8831 (extra (org-get-at-bol 'extra)))
8832 (with-current-buffer (marker-buffer hdmarker)
8833 (save-excursion
8834 (save-restriction
8835 (widen)
8836 (org-agenda-format-item extra newhead level cat tags dotime)))))
8837 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8838 undone-face (org-get-at-bol 'undone-face)
8839 done-face (org-get-at-bol 'done-face))
8840 (beginning-of-line 1)
8841 (cond
8842 ((equal new "")
8843 (and (looking-at ".*\n?") (replace-match "")))
8844 ((looking-at ".*")
8845 (replace-match new t t)
8846 (beginning-of-line 1)
8847 (add-text-properties (point-at-bol) (point-at-eol) props)
8848 (when fixface
8849 (add-text-properties
8850 (point-at-bol) (point-at-eol)
8851 (list 'face
8852 (if org-last-todo-state-is-todo
8853 undone-face done-face))))
8854 (org-agenda-highlight-todo 'line)
8855 (beginning-of-line 1))
8856 (t (error "Line update did not work")))
8857 (save-restriction
8858 (narrow-to-region (point-at-bol) (point-at-eol))
8859 (org-agenda-finalize)))
8860 (beginning-of-line 0)))))
8861
8862 (defun org-agenda-align-tags (&optional line)
8863 "Align all tags in agenda items to `org-agenda-tags-column'."
8864 (let ((inhibit-read-only t) l c)
8865 (save-excursion
8866 (goto-char (if line (point-at-bol) (point-min)))
8867 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8868 (if line (point-at-eol) nil) t)
8869 (add-text-properties
8870 (match-beginning 2) (match-end 2)
8871 (list 'face (delq nil (let ((prop (get-text-property
8872 (match-beginning 2) 'face)))
8873 (or (listp prop) (setq prop (list prop)))
8874 (if (memq 'org-tag prop)
8875 prop
8876 (cons 'org-tag prop))))))
8877 (setq l (- (match-end 2) (match-beginning 2))
8878 c (if (< org-agenda-tags-column 0)
8879 (- (abs org-agenda-tags-column) l)
8880 org-agenda-tags-column))
8881 (delete-region (match-beginning 1) (match-end 1))
8882 (goto-char (match-beginning 1))
8883 (insert (org-add-props
8884 (make-string (max 1 (- c (current-column))) ?\ )
8885 (plist-put (copy-sequence (text-properties-at (point)))
8886 'face nil))))
8887 (goto-char (point-min))
8888 (org-font-lock-add-tag-faces (point-max)))))
8889
8890 (defun org-agenda-priority-up ()
8891 "Increase the priority of line at point, also in Org-mode file."
8892 (interactive)
8893 (org-agenda-priority 'up))
8894
8895 (defun org-agenda-priority-down ()
8896 "Decrease the priority of line at point, also in Org-mode file."
8897 (interactive)
8898 (org-agenda-priority 'down))
8899
8900 (defun org-agenda-priority (&optional force-direction)
8901 "Set the priority of line at point, also in Org-mode file.
8902 This changes the line at point, all other lines in the agenda referring to
8903 the same tree node, and the headline of the tree node in the Org-mode file.
8904 Called with a universal prefix arg, show the priority instead of setting it."
8905 (interactive "P")
8906 (if (equal force-direction '(4))
8907 (org-show-priority)
8908 (unless org-enable-priority-commands
8909 (error "Priority commands are disabled"))
8910 (org-agenda-check-no-diary)
8911 (let* ((col (current-column))
8912 (marker (or (org-get-at-bol 'org-marker)
8913 (org-agenda-error)))
8914 (hdmarker (org-get-at-bol 'org-hd-marker))
8915 (buffer (marker-buffer hdmarker))
8916 (pos (marker-position hdmarker))
8917 (inhibit-read-only t)
8918 newhead)
8919 (org-with-remote-undo buffer
8920 (with-current-buffer buffer
8921 (widen)
8922 (goto-char pos)
8923 (org-show-context 'agenda)
8924 (save-excursion
8925 (and (outline-next-heading)
8926 (org-flag-heading nil))) ; show the next heading
8927 (funcall 'org-priority force-direction)
8928 (end-of-line 1)
8929 (setq newhead (org-get-heading)))
8930 (org-agenda-change-all-lines newhead hdmarker)
8931 (org-move-to-column col)))))
8932
8933 ;; FIXME: should fix the tags property of the agenda line.
8934 (defun org-agenda-set-tags (&optional tag onoff)
8935 "Set tags for the current headline."
8936 (interactive)
8937 (org-agenda-check-no-diary)
8938 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8939 (call-interactively 'org-change-tag-in-region)
8940 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8941 (org-agenda-error)))
8942 (buffer (marker-buffer hdmarker))
8943 (pos (marker-position hdmarker))
8944 (inhibit-read-only t)
8945 newhead)
8946 (org-with-remote-undo buffer
8947 (with-current-buffer buffer
8948 (widen)
8949 (goto-char pos)
8950 (save-excursion
8951 (org-show-context 'agenda))
8952 (save-excursion
8953 (and (outline-next-heading)
8954 (org-flag-heading nil))) ; show the next heading
8955 (goto-char pos)
8956 (if tag
8957 (org-toggle-tag tag onoff)
8958 (call-interactively 'org-set-tags))
8959 (end-of-line 1)
8960 (setq newhead (org-get-heading)))
8961 (org-agenda-change-all-lines newhead hdmarker)
8962 (beginning-of-line 1)))))
8963
8964 (defun org-agenda-set-property ()
8965 "Set a property for the current headline."
8966 (interactive)
8967 (org-agenda-check-no-diary)
8968 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8969 (org-agenda-error)))
8970 (buffer (marker-buffer hdmarker))
8971 (pos (marker-position hdmarker))
8972 (inhibit-read-only t)
8973 newhead)
8974 (org-with-remote-undo buffer
8975 (with-current-buffer buffer
8976 (widen)
8977 (goto-char pos)
8978 (save-excursion
8979 (org-show-context 'agenda))
8980 (save-excursion
8981 (and (outline-next-heading)
8982 (org-flag-heading nil))) ; show the next heading
8983 (goto-char pos)
8984 (call-interactively 'org-set-property)))))
8985
8986 (defun org-agenda-set-effort ()
8987 "Set the effort property for the current headline."
8988 (interactive)
8989 (org-agenda-check-no-diary)
8990 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8991 (org-agenda-error)))
8992 (buffer (marker-buffer hdmarker))
8993 (pos (marker-position hdmarker))
8994 (inhibit-read-only t)
8995 newhead)
8996 (org-with-remote-undo buffer
8997 (with-current-buffer buffer
8998 (widen)
8999 (goto-char pos)
9000 (save-excursion
9001 (org-show-context 'agenda))
9002 (save-excursion
9003 (and (outline-next-heading)
9004 (org-flag-heading nil))) ; show the next heading
9005 (goto-char pos)
9006 (call-interactively 'org-set-effort)
9007 (end-of-line 1)
9008 (setq newhead (org-get-heading)))
9009 (org-agenda-change-all-lines newhead hdmarker))))
9010
9011 (defun org-agenda-toggle-archive-tag ()
9012 "Toggle the archive tag for the current entry."
9013 (interactive)
9014 (org-agenda-check-no-diary)
9015 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9016 (org-agenda-error)))
9017 (buffer (marker-buffer hdmarker))
9018 (pos (marker-position hdmarker))
9019 (inhibit-read-only t)
9020 newhead)
9021 (org-with-remote-undo buffer
9022 (with-current-buffer buffer
9023 (widen)
9024 (goto-char pos)
9025 (org-show-context 'agenda)
9026 (save-excursion
9027 (and (outline-next-heading)
9028 (org-flag-heading nil))) ; show the next heading
9029 (call-interactively 'org-toggle-archive-tag)
9030 (end-of-line 1)
9031 (setq newhead (org-get-heading)))
9032 (org-agenda-change-all-lines newhead hdmarker)
9033 (beginning-of-line 1))))
9034
9035 (defun org-agenda-do-date-later (arg)
9036 (interactive "P")
9037 (cond
9038 ((or (equal arg '(16))
9039 (memq last-command
9040 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9041 (setq this-command 'org-agenda-date-later-minutes)
9042 (org-agenda-date-later-minutes 1))
9043 ((or (equal arg '(4))
9044 (memq last-command
9045 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9046 (setq this-command 'org-agenda-date-later-hours)
9047 (org-agenda-date-later-hours 1))
9048 (t
9049 (org-agenda-date-later (prefix-numeric-value arg)))))
9050
9051 (defun org-agenda-do-date-earlier (arg)
9052 (interactive "P")
9053 (cond
9054 ((or (equal arg '(16))
9055 (memq last-command
9056 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9057 (setq this-command 'org-agenda-date-earlier-minutes)
9058 (org-agenda-date-earlier-minutes 1))
9059 ((or (equal arg '(4))
9060 (memq last-command
9061 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9062 (setq this-command 'org-agenda-date-earlier-hours)
9063 (org-agenda-date-earlier-hours 1))
9064 (t
9065 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9066
9067 (defun org-agenda-date-later (arg &optional what)
9068 "Change the date of this item to ARG day(s) later."
9069 (interactive "p")
9070 (org-agenda-check-type t 'agenda 'timeline)
9071 (org-agenda-check-no-diary)
9072 (let* ((marker (or (org-get-at-bol 'org-marker)
9073 (org-agenda-error)))
9074 (buffer (marker-buffer marker))
9075 (pos (marker-position marker))
9076 cdate today)
9077 (org-with-remote-undo buffer
9078 (with-current-buffer buffer
9079 (widen)
9080 (goto-char pos)
9081 (if (not (org-at-timestamp-p))
9082 (error "Cannot find time stamp"))
9083 (when (and org-agenda-move-date-from-past-immediately-to-today
9084 (equal arg 1)
9085 (or (not what) (eq what 'day))
9086 (not (save-match-data (org-at-date-range-p))))
9087 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9088 cdate (calendar-absolute-from-gregorian
9089 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9090 today (org-today))
9091 (if (> today cdate)
9092 ;; immediately shift to today
9093 (setq arg (- today cdate))))
9094 (org-timestamp-change arg (or what 'day))
9095 (when (and (org-at-date-range-p)
9096 (re-search-backward org-tr-regexp-both (point-at-bol)))
9097 (let ((end org-last-changed-timestamp))
9098 (org-timestamp-change arg (or what 'day))
9099 (setq org-last-changed-timestamp
9100 (concat org-last-changed-timestamp "--" end)))))
9101 (org-agenda-show-new-time marker org-last-changed-timestamp))
9102 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9103
9104 (defun org-agenda-date-earlier (arg &optional what)
9105 "Change the date of this item to ARG day(s) earlier."
9106 (interactive "p")
9107 (org-agenda-date-later (- arg) what))
9108
9109 (defun org-agenda-date-later-minutes (arg)
9110 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9111 (interactive "p")
9112 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9113 (org-agenda-date-later arg 'minute))
9114
9115 (defun org-agenda-date-earlier-minutes (arg)
9116 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9117 (interactive "p")
9118 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9119 (org-agenda-date-earlier arg 'minute))
9120
9121 (defun org-agenda-date-later-hours (arg)
9122 "Change the time of this item, in hour steps."
9123 (interactive "p")
9124 (org-agenda-date-later arg 'hour))
9125
9126 (defun org-agenda-date-earlier-hours (arg)
9127 "Change the time of this item, in hour steps."
9128 (interactive "p")
9129 (org-agenda-date-earlier arg 'hour))
9130
9131 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9132 "Show new date stamp via text properties."
9133 ;; We use text properties to make this undoable
9134 (let ((inhibit-read-only t))
9135 (setq stamp (concat prefix " => " stamp " "))
9136 (save-excursion
9137 (goto-char (point-max))
9138 (while (not (bobp))
9139 (when (equal marker (org-get-at-bol 'org-marker))
9140 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9141 (org-move-to-column (- (window-width) (length stamp)) t)
9142 (if (featurep 'xemacs)
9143 ;; Use `duplicable' property to trigger undo recording
9144 (let ((ex (make-extent nil nil))
9145 (gl (make-glyph stamp)))
9146 (set-glyph-face gl 'secondary-selection)
9147 (set-extent-properties
9148 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9149 (insert-extent ex (1- (point)) (point-at-eol)))
9150 (add-text-properties
9151 (1- (point)) (point-at-eol)
9152 (list 'display (org-add-props stamp nil
9153 'face '(secondary-selection default)))))
9154 (beginning-of-line 1))
9155 (beginning-of-line 0)))))
9156
9157 (defun org-agenda-date-prompt (arg)
9158 "Change the date of this item. Date is prompted for, with default today.
9159 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9160 be used to request time specification in the time stamp."
9161 (interactive "P")
9162 (org-agenda-check-type t 'agenda 'timeline)
9163 (org-agenda-check-no-diary)
9164 (let* ((marker (or (org-get-at-bol 'org-marker)
9165 (org-agenda-error)))
9166 (buffer (marker-buffer marker))
9167 (pos (marker-position marker)))
9168 (org-with-remote-undo buffer
9169 (with-current-buffer buffer
9170 (widen)
9171 (goto-char pos)
9172 (if (not (org-at-timestamp-p t))
9173 (error "Cannot find time stamp"))
9174 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9175 (org-agenda-show-new-time marker org-last-changed-timestamp))
9176 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9177
9178 (defun org-agenda-schedule (arg &optional time)
9179 "Schedule the item at point.
9180 ARG is passed through to `org-schedule'."
9181 (interactive "P")
9182 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9183 (org-agenda-check-no-diary)
9184 (let* ((marker (or (org-get-at-bol 'org-marker)
9185 (org-agenda-error)))
9186 (type (marker-insertion-type marker))
9187 (buffer (marker-buffer marker))
9188 (pos (marker-position marker))
9189 (org-insert-labeled-timestamps-at-point nil)
9190 ts)
9191 (set-marker-insertion-type marker t)
9192 (org-with-remote-undo buffer
9193 (with-current-buffer buffer
9194 (widen)
9195 (goto-char pos)
9196 (setq ts (org-schedule arg time)))
9197 (org-agenda-show-new-time marker ts " S"))
9198 (message "%s" ts)))
9199
9200 (defun org-agenda-deadline (arg &optional time)
9201 "Schedule the item at point.
9202 ARG is passed through to `org-deadline'."
9203 (interactive "P")
9204 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9205 (org-agenda-check-no-diary)
9206 (let* ((marker (or (org-get-at-bol 'org-marker)
9207 (org-agenda-error)))
9208 (buffer (marker-buffer marker))
9209 (pos (marker-position marker))
9210 (org-insert-labeled-timestamps-at-point nil)
9211 ts)
9212 (org-with-remote-undo buffer
9213 (with-current-buffer buffer
9214 (widen)
9215 (goto-char pos)
9216 (setq ts (org-deadline arg time)))
9217 (org-agenda-show-new-time marker ts " D"))
9218 (message "%s" ts)))
9219
9220 (defun org-agenda-clock-in (&optional arg)
9221 "Start the clock on the currently selected item."
9222 (interactive "P")
9223 (org-agenda-check-no-diary)
9224 (if (equal arg '(4))
9225 (org-clock-in arg)
9226 (let* ((marker (or (org-get-at-bol 'org-marker)
9227 (org-agenda-error)))
9228 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9229 (pos (marker-position marker))
9230 (col (current-column))
9231 newhead)
9232 (org-with-remote-undo (marker-buffer marker)
9233 (with-current-buffer (marker-buffer marker)
9234 (widen)
9235 (goto-char pos)
9236 (org-show-context 'agenda)
9237 (org-show-entry)
9238 (org-cycle-hide-drawers 'children)
9239 (org-clock-in arg)
9240 (setq newhead (org-get-heading)))
9241 (org-agenda-change-all-lines newhead hdmarker))
9242 (org-move-to-column col))))
9243
9244 (defun org-agenda-clock-out ()
9245 "Stop the currently running clock."
9246 (interactive)
9247 (unless (marker-buffer org-clock-marker)
9248 (error "No running clock"))
9249 (let ((marker (make-marker)) (col (current-column)) newhead)
9250 (org-with-remote-undo (marker-buffer org-clock-marker)
9251 (with-current-buffer (marker-buffer org-clock-marker)
9252 (save-excursion
9253 (save-restriction
9254 (widen)
9255 (goto-char org-clock-marker)
9256 (org-back-to-heading t)
9257 (move-marker marker (point))
9258 (org-clock-out)
9259 (setq newhead (org-get-heading))))))
9260 (org-agenda-change-all-lines newhead marker)
9261 (move-marker marker nil)
9262 (org-move-to-column col)
9263 (org-agenda-unmark-clocking-task)))
9264
9265 (defun org-agenda-clock-cancel (&optional arg)
9266 "Cancel the currently running clock."
9267 (interactive "P")
9268 (unless (marker-buffer org-clock-marker)
9269 (user-error "No running clock"))
9270 (org-with-remote-undo (marker-buffer org-clock-marker)
9271 (org-clock-cancel)))
9272
9273 (defun org-agenda-clock-goto ()
9274 "Jump to the currently clocked in task within the agenda.
9275 If the currently clocked in task is not listed in the agenda
9276 buffer, display it in another window."
9277 (interactive)
9278 (let (pos)
9279 (mapc (lambda (o)
9280 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9281 (setq pos (overlay-start o))))
9282 (overlays-in (point-min) (point-max)))
9283 (cond (pos (goto-char pos))
9284 ;; If the currently clocked entry is not in the agenda
9285 ;; buffer, we visit it in another window:
9286 (org-clock-current-task
9287 (org-switch-to-buffer-other-window (org-clock-goto)))
9288 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9289
9290 (defun org-agenda-diary-entry-in-org-file ()
9291 "Make a diary entry in the file `org-agenda-diary-file'."
9292 (let (d1 d2 char (text "") dp1 dp2)
9293 (if (equal (buffer-name) "*Calendar*")
9294 (setq d1 (calendar-cursor-to-date t)
9295 d2 (car calendar-mark-ring))
9296 (setq dp1 (get-text-property (point-at-bol) 'day))
9297 (unless dp1 (user-error "No date defined in current line"))
9298 (setq d1 (calendar-gregorian-from-absolute dp1)
9299 d2 (and (ignore-errors (mark))
9300 (save-excursion
9301 (goto-char (mark))
9302 (setq dp2 (get-text-property (point-at-bol) 'day)))
9303 (calendar-gregorian-from-absolute dp2))))
9304 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9305 (setq char (read-char-exclusive))
9306 (cond
9307 ((equal char ?d)
9308 (setq text (read-string "Day entry: "))
9309 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9310 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9311 ((equal char ?a)
9312 (setq d1 (list (car d1) (nth 1 d1)
9313 (read-number (format "Reference year [%d]: " (nth 2 d1))
9314 (nth 2 d1))))
9315 (setq text (read-string "Anniversary (use %d to show years): "))
9316 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9317 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9318 ((equal char ?b)
9319 (setq text (read-string "Block entry: "))
9320 (unless (and d1 d2 (not (equal d1 d2)))
9321 (user-error "No block of days selected"))
9322 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9323 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9324 ((equal char ?j)
9325 (org-switch-to-buffer-other-window
9326 (find-file-noselect org-agenda-diary-file))
9327 (require 'org-datetree)
9328 (org-datetree-find-date-create d1)
9329 (org-reveal t))
9330 (t (user-error "Invalid selection character `%c'" char)))))
9331
9332 (defcustom org-agenda-insert-diary-strategy 'date-tree
9333 "Where in `org-agenda-diary-file' should new entries be added?
9334 Valid values:
9335
9336 date-tree in the date tree, as child of the date
9337 top-level as top-level entries at the end of the file."
9338 :group 'org-agenda
9339 :type '(choice
9340 (const :tag "in a date tree" date-tree)
9341 (const :tag "as top level at end of file" top-level)))
9342
9343 (defcustom org-agenda-insert-diary-extract-time nil
9344 "Non-nil means extract any time specification from the diary entry."
9345 :group 'org-agenda
9346 :version "24.1"
9347 :type 'boolean)
9348
9349 (defcustom org-agenda-bulk-mark-char ">"
9350 "A single-character string to be used as the bulk mark."
9351 :group 'org-agenda
9352 :version "24.1"
9353 :type 'string)
9354
9355 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9356 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9357 If TEXT is not empty, it will become the headline of the new entry, and
9358 the resulting entry will not be shown. When TEXT is empty, switch to
9359 `org-agenda-diary-file' and let the user finish the entry there."
9360 (let ((cw (current-window-configuration)))
9361 (org-switch-to-buffer-other-window
9362 (find-file-noselect org-agenda-diary-file))
9363 (widen)
9364 (goto-char (point-min))
9365 (cond
9366 ((eq type 'anniversary)
9367 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9368 (progn
9369 (or (org-at-heading-p t)
9370 (progn
9371 (outline-next-heading)
9372 (insert "* Anniversaries\n\n")
9373 (beginning-of-line -1)))))
9374 (outline-next-heading)
9375 (org-back-over-empty-lines)
9376 (backward-char 1)
9377 (insert "\n")
9378 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9379 (nth 2 d1) (car d1) (nth 1 d1) text)))
9380 ((eq type 'day)
9381 (let ((org-prefix-has-time t)
9382 (org-agenda-time-leading-zero t)
9383 fmt time time2)
9384 (if org-agenda-insert-diary-extract-time
9385 ;; Use org-agenda-format-item to parse text for a time-range and
9386 ;; remove it. FIXME: This is a hack, we should refactor
9387 ;; that function to make time extraction available separately
9388 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9389 time (get-text-property 0 'time fmt)
9390 time2 (if (> (length time) 0)
9391 ;; split-string removes trailing ...... if
9392 ;; no end time given. First space
9393 ;; separates time from date.
9394 (concat " " (car (split-string time "\\.")))
9395 nil)
9396 text (get-text-property 0 'txt fmt)))
9397 (if (eq org-agenda-insert-diary-strategy 'top-level)
9398 (org-agenda-insert-diary-as-top-level text)
9399 (require 'org-datetree)
9400 (org-datetree-find-date-create d1)
9401 (org-agenda-insert-diary-make-new-entry text))
9402 (org-insert-time-stamp (org-time-from-absolute
9403 (calendar-absolute-from-gregorian d1))
9404 nil nil nil nil time2))
9405 (end-of-line 0))
9406 ((eq type 'block)
9407 (if (> (calendar-absolute-from-gregorian d1)
9408 (calendar-absolute-from-gregorian d2))
9409 (setq d1 (prog1 d2 (setq d2 d1))))
9410 (if (eq org-agenda-insert-diary-strategy 'top-level)
9411 (org-agenda-insert-diary-as-top-level text)
9412 (require 'org-datetree)
9413 (org-datetree-find-date-create d1)
9414 (org-agenda-insert-diary-make-new-entry text))
9415 (org-insert-time-stamp (org-time-from-absolute
9416 (calendar-absolute-from-gregorian d1)))
9417 (insert "--")
9418 (org-insert-time-stamp (org-time-from-absolute
9419 (calendar-absolute-from-gregorian d2)))
9420 (end-of-line 0)))
9421 (if (string-match "\\S-" text)
9422 (progn
9423 (set-window-configuration cw)
9424 (message "%s entry added to %s"
9425 (capitalize (symbol-name type))
9426 (abbreviate-file-name org-agenda-diary-file)))
9427 (org-reveal t)
9428 (message "Please finish entry here"))))
9429
9430 (defun org-agenda-insert-diary-as-top-level (text)
9431 "Make new entry as a top-level entry at the end of the file.
9432 Add TEXT as headline, and position the cursor in the second line so that
9433 a timestamp can be added there."
9434 (widen)
9435 (goto-char (point-max))
9436 (or (bolp) (insert "\n"))
9437 (insert "* " text "\n")
9438 (if org-adapt-indentation (org-indent-to-column 2)))
9439
9440 (defun org-agenda-insert-diary-make-new-entry (text)
9441 "Make a new entry with TEXT as the first child of the current subtree.
9442 Position the point in the line right after the new heading so
9443 that a timestamp can be added there."
9444 (let ((org-show-following-heading t)
9445 (org-show-siblings t)
9446 (org-show-hierarchy-above t)
9447 (org-show-entry-below t)
9448 col)
9449 (outline-next-heading)
9450 (org-back-over-empty-lines)
9451 (or (looking-at "[ \t]*$")
9452 (progn (insert "\n") (backward-char 1)))
9453 (org-insert-heading nil t)
9454 (org-do-demote)
9455 (setq col (current-column))
9456 (insert text "\n")
9457 (if org-adapt-indentation (org-indent-to-column col))
9458 (let ((org-show-following-heading t)
9459 (org-show-siblings t)
9460 (org-show-hierarchy-above t)
9461 (org-show-entry-below t))
9462 (org-show-context))))
9463
9464 (defun org-agenda-diary-entry ()
9465 "Make a diary entry, like the `i' command from the calendar.
9466 All the standard commands work: block, weekly etc.
9467 When `org-agenda-diary-file' points to a file,
9468 `org-agenda-diary-entry-in-org-file' is called instead to create
9469 entries in that Org-mode file."
9470 (interactive)
9471 (if (not (eq org-agenda-diary-file 'diary-file))
9472 (org-agenda-diary-entry-in-org-file)
9473 (require 'diary-lib)
9474 (let* ((char (progn
9475 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9476 (read-char-exclusive)))
9477 (cmd (cdr (assoc char
9478 '((?d . insert-diary-entry)
9479 (?w . insert-weekly-diary-entry)
9480 (?m . insert-monthly-diary-entry)
9481 (?y . insert-yearly-diary-entry)
9482 (?a . insert-anniversary-diary-entry)
9483 (?b . insert-block-diary-entry)
9484 (?c . insert-cyclic-diary-entry)))))
9485 (oldf (symbol-function 'calendar-cursor-to-date))
9486 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9487 (point (point))
9488 (mark (or (mark t) (point))))
9489 (unless cmd
9490 (user-error "No command associated with <%c>" char))
9491 (unless (and (get-text-property point 'day)
9492 (or (not (equal ?b char))
9493 (get-text-property mark 'day)))
9494 (user-error "Don't know which date to use for diary entry"))
9495 ;; We implement this by hacking the `calendar-cursor-to-date' function
9496 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9497 (let ((calendar-mark-ring
9498 (list (calendar-gregorian-from-absolute
9499 (or (get-text-property mark 'day)
9500 (get-text-property point 'day))))))
9501 (unwind-protect
9502 (progn
9503 (fset 'calendar-cursor-to-date
9504 (lambda (&optional error dummy)
9505 (calendar-gregorian-from-absolute
9506 (get-text-property point 'day))))
9507 (call-interactively cmd))
9508 (fset 'calendar-cursor-to-date oldf))))))
9509
9510 (defun org-agenda-execute-calendar-command (cmd)
9511 "Execute a calendar command from the agenda with date from cursor."
9512 (org-agenda-check-type t 'agenda 'timeline)
9513 (require 'diary-lib)
9514 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9515 (user-error "Don't know which date to use for the calendar command"))
9516 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9517 (point (point))
9518 (date (calendar-gregorian-from-absolute
9519 (get-text-property point 'day)))
9520 ;; the following 2 vars are needed in the calendar
9521 (displayed-month (car date))
9522 (displayed-year (nth 2 date)))
9523 (unwind-protect
9524 (progn
9525 (fset 'calendar-cursor-to-date
9526 (lambda (&optional error dummy)
9527 (calendar-gregorian-from-absolute
9528 (get-text-property point 'day))))
9529 (call-interactively cmd))
9530 (fset 'calendar-cursor-to-date oldf))))
9531
9532 (defun org-agenda-phases-of-moon ()
9533 "Display the phases of the moon for the 3 months around the cursor date."
9534 (interactive)
9535 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9536
9537 (defun org-agenda-holidays ()
9538 "Display the holidays for the 3 months around the cursor date."
9539 (interactive)
9540 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9541
9542 (defvar calendar-longitude) ; defined in calendar.el
9543 (defvar calendar-latitude) ; defined in calendar.el
9544 (defvar calendar-location-name) ; defined in calendar.el
9545
9546 (defun org-agenda-sunrise-sunset (arg)
9547 "Display sunrise and sunset for the cursor date.
9548 Latitude and longitude can be specified with the variables
9549 `calendar-latitude' and `calendar-longitude'. When called with prefix
9550 argument, latitude and longitude will be prompted for."
9551 (interactive "P")
9552 (require 'solar)
9553 (let ((calendar-longitude (if arg nil calendar-longitude))
9554 (calendar-latitude (if arg nil calendar-latitude))
9555 (calendar-location-name
9556 (if arg "the given coordinates" calendar-location-name)))
9557 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9558
9559 (defun org-agenda-goto-calendar ()
9560 "Open the Emacs calendar with the date at the cursor."
9561 (interactive)
9562 (org-agenda-check-type t 'agenda 'timeline)
9563 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9564 (user-error "Don't know which date to open in calendar")))
9565 (date (calendar-gregorian-from-absolute day))
9566 (calendar-move-hook nil)
9567 (calendar-view-holidays-initially-flag nil)
9568 (calendar-view-diary-initially-flag nil))
9569 (calendar)
9570 (calendar-goto-date date)))
9571
9572 ;;;###autoload
9573 (defun org-calendar-goto-agenda ()
9574 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9575 This is a command that has to be installed in `calendar-mode-map'."
9576 (interactive)
9577 (org-agenda-list nil (calendar-absolute-from-gregorian
9578 (calendar-cursor-to-date))
9579 nil))
9580
9581 (defun org-agenda-convert-date ()
9582 (interactive)
9583 (org-agenda-check-type t 'agenda 'timeline)
9584 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9585 date s)
9586 (unless day
9587 (user-error "Don't know which date to convert"))
9588 (setq date (calendar-gregorian-from-absolute day))
9589 (setq s (concat
9590 "Gregorian: " (calendar-date-string date) "\n"
9591 "ISO: " (calendar-iso-date-string date) "\n"
9592 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9593 "Julian: " (calendar-julian-date-string date) "\n"
9594 "Astron. JD: " (calendar-astro-date-string date)
9595 " (Julian date number at noon UTC)\n"
9596 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9597 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9598 "French: " (calendar-french-date-string date) "\n"
9599 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9600 "Mayan: " (calendar-mayan-date-string date) "\n"
9601 "Coptic: " (calendar-coptic-date-string date) "\n"
9602 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9603 "Persian: " (calendar-persian-date-string date) "\n"
9604 "Chinese: " (calendar-chinese-date-string date) "\n"))
9605 (with-output-to-temp-buffer "*Dates*"
9606 (princ s))
9607 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9608
9609 ;;; Bulk commands
9610
9611 (defun org-agenda-bulk-marked-p ()
9612 (eq (get-char-property (point-at-bol) 'type)
9613 'org-marked-entry-overlay))
9614
9615 (defun org-agenda-bulk-mark (&optional arg)
9616 "Mark the entry at point for future bulk action."
9617 (interactive "p")
9618 (dotimes (i (or arg 1))
9619 (unless (org-get-at-bol 'org-agenda-diary-link)
9620 (let* ((m (org-get-at-bol 'org-hd-marker))
9621 ov)
9622 (unless (org-agenda-bulk-marked-p)
9623 (unless m (user-error "Nothing to mark at point"))
9624 (push m org-agenda-bulk-marked-entries)
9625 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9626 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9627 (org-get-todo-face "TODO")
9628 'evaporate)
9629 (overlay-put ov 'type 'org-marked-entry-overlay))
9630 (end-of-line 1)
9631 (or (ignore-errors
9632 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9633 (beginning-of-line 2))
9634 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9635 (beginning-of-line 2))
9636 (message "%d entries marked for bulk action"
9637 (length org-agenda-bulk-marked-entries))))))
9638
9639 (defun org-agenda-bulk-mark-all ()
9640 "Mark all entries for future agenda bulk action."
9641 (interactive)
9642 (org-agenda-bulk-mark-regexp "."))
9643
9644 (defun org-agenda-bulk-mark-regexp (regexp)
9645 "Mark entries matching REGEXP for future agenda bulk action."
9646 (interactive "sMark entries matching regexp: ")
9647 (let ((entries-marked 0) txt-at-point)
9648 (save-excursion
9649 (goto-char (point-min))
9650 (goto-char (next-single-property-change (point) 'org-hd-marker))
9651 (while (and (re-search-forward regexp nil t)
9652 (setq txt-at-point (get-text-property (point) 'txt)))
9653 (when (string-match regexp txt-at-point)
9654 (setq entries-marked (1+ entries-marked))
9655 (call-interactively 'org-agenda-bulk-mark))))
9656 (if (not entries-marked)
9657 (message "No entry matching this regexp."))))
9658
9659 (defun org-agenda-bulk-unmark (&optional arg)
9660 "Unmark the entry at point for future bulk action."
9661 (interactive "P")
9662 (if arg
9663 (org-agenda-bulk-unmark-all)
9664 (cond ((org-agenda-bulk-marked-p)
9665 (org-agenda-bulk-remove-overlays
9666 (point-at-bol) (+ 2 (point-at-bol)))
9667 (setq org-agenda-bulk-marked-entries
9668 (delete (org-get-at-bol 'org-hd-marker)
9669 org-agenda-bulk-marked-entries))
9670 (end-of-line 1)
9671 (or (ignore-errors
9672 (goto-char (next-single-property-change (point) 'txt)))
9673 (beginning-of-line 2))
9674 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9675 (beginning-of-line 2))
9676 (message "%d entries left marked for bulk action"
9677 (length org-agenda-bulk-marked-entries)))
9678 (t (message "No entry to unmark here")))))
9679
9680 (defun org-agenda-bulk-toggle-all ()
9681 "Toggle all marks for bulk action."
9682 (interactive)
9683 (save-excursion
9684 (goto-char (point-min))
9685 (while (ignore-errors
9686 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9687 (org-agenda-bulk-toggle))))
9688
9689 (defun org-agenda-bulk-toggle ()
9690 "Toggle the mark at point for bulk action."
9691 (interactive)
9692 (if (org-agenda-bulk-marked-p)
9693 (org-agenda-bulk-unmark)
9694 (org-agenda-bulk-mark)))
9695
9696 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9697 "Remove the mark overlays between BEG and END in the agenda buffer.
9698 BEG and END default to the buffer limits.
9699
9700 This only removes the overlays, it does not remove the markers
9701 from the list in `org-agenda-bulk-marked-entries'."
9702 (interactive)
9703 (mapc (lambda (ov)
9704 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9705 (delete-overlay ov)))
9706 (overlays-in (or beg (point-min)) (or end (point-max)))))
9707
9708 (defun org-agenda-bulk-unmark-all ()
9709 "Remove all marks in the agenda buffer.
9710 This will remove the markers and the overlays."
9711 (interactive)
9712 (if (null org-agenda-bulk-marked-entries)
9713 (message "No entry to unmark")
9714 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9715 (setq org-agenda-bulk-marked-entries nil)
9716 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9717
9718 (defcustom org-agenda-persistent-marks nil
9719 "Non-nil means marked items will stay marked after a bulk action.
9720 You can toggle this interactively by typing `p' when prompted for a
9721 bulk action."
9722 :group 'org-agenda
9723 :version "24.1"
9724 :type 'boolean)
9725
9726 (defun org-agenda-bulk-action (&optional arg)
9727 "Execute an remote-editing action on all marked entries.
9728 The prefix arg is passed through to the command if possible."
9729 (interactive "P")
9730 ;; Make sure we have markers, and only valid ones
9731 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9732 (mapc
9733 (lambda (m)
9734 (unless (and (markerp m)
9735 (marker-buffer m)
9736 (buffer-live-p (marker-buffer m))
9737 (marker-position m))
9738 (user-error "Marker %s for bulk command is invalid" m)))
9739 org-agenda-bulk-marked-entries)
9740
9741 ;; Prompt for the bulk command
9742 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9743 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9744 "[S]catter [f]unction "
9745 (when org-agenda-bulk-custom-functions
9746 (concat " Custom: ["
9747 (mapconcat (lambda(f) (char-to-string (car f)))
9748 org-agenda-bulk-custom-functions "")
9749 "]"))))
9750 (catch 'exit
9751 (let* ((action (read-char-exclusive))
9752 (org-log-refile (if org-log-refile 'time nil))
9753 (entries (reverse org-agenda-bulk-marked-entries))
9754 (org-overriding-default-time
9755 (if (get-text-property (point) 'org-agenda-date-header)
9756 (org-get-cursor-date)))
9757 redo-at-end
9758 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9759 (cond
9760 ((equal action ?p)
9761 (let ((org-agenda-persistent-marks
9762 (not org-agenda-persistent-marks)))
9763 (org-agenda-bulk-action)
9764 (throw 'exit nil)))
9765
9766 ((equal action ?$)
9767 (setq cmd '(org-agenda-archive)))
9768
9769 ((equal action ?A)
9770 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9771
9772 ((member action '(?r ?w))
9773 (setq rfloc (org-refile-get-location
9774 "Refile to"
9775 (marker-buffer (car entries))
9776 org-refile-allow-creating-parent-nodes))
9777 (if (nth 3 rfloc)
9778 (setcar (nthcdr 3 rfloc)
9779 (move-marker (make-marker) (nth 3 rfloc)
9780 (or (get-file-buffer (nth 1 rfloc))
9781 (find-buffer-visiting (nth 1 rfloc))
9782 (error "This should not happen")))))
9783
9784 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9785 redo-at-end t))
9786
9787 ((equal action ?t)
9788 (setq state (org-icompleting-read
9789 "Todo state: "
9790 (with-current-buffer (marker-buffer (car entries))
9791 (mapcar 'list org-todo-keywords-1))))
9792 (setq cmd `(let ((org-inhibit-blocking t)
9793 (org-inhibit-logging 'note))
9794 (org-agenda-todo ,state))))
9795
9796 ((memq action '(?- ?+))
9797 (setq tag (org-icompleting-read
9798 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9799 (with-current-buffer (marker-buffer (car entries))
9800 (delq nil
9801 (mapcar (lambda (x)
9802 (if (stringp (car x)) x)) org-tag-alist)))))
9803 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9804
9805 ((memq action '(?s ?d))
9806 (let* ((time
9807 (unless arg
9808 (org-read-date
9809 nil nil nil
9810 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9811 org-overriding-default-time)))
9812 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9813 (setq cmd `(eval '(,c1 arg ,time)))))
9814
9815 ((equal action ?S)
9816 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9817 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9818 (let ((days (read-number
9819 (format "Scatter tasks across how many %sdays: "
9820 (if arg "week" "")) 7)))
9821 (setq cmd
9822 `(let ((distance (1+ (random ,days))))
9823 (if arg
9824 (let ((dist distance)
9825 (day-of-week
9826 (calendar-day-of-week
9827 (calendar-gregorian-from-absolute (org-today)))))
9828 (dotimes (i (1+ dist))
9829 (while (member day-of-week org-agenda-weekend-days)
9830 (incf distance)
9831 (incf day-of-week)
9832 (if (= day-of-week 7)
9833 (setq day-of-week 0)))
9834 (incf day-of-week)
9835 (if (= day-of-week 7)
9836 (setq day-of-week 0)))))
9837 ;; silently fail when try to replan a sexp entry
9838 (condition-case nil
9839 (let* ((date (calendar-gregorian-from-absolute
9840 (+ (org-today) distance)))
9841 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9842 (nth 2 date))))
9843 (org-agenda-schedule nil time))
9844 (error nil)))))))
9845
9846 ((assoc action org-agenda-bulk-custom-functions)
9847 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9848 redo-at-end t))
9849
9850 ((equal action ?f)
9851 (setq cmd (list (intern
9852 (org-icompleting-read "Function: "
9853 obarray 'fboundp t nil nil)))))
9854
9855 (t (user-error "Invalid bulk action")))
9856
9857 ;; Sort the markers, to make sure that parents are handled before children
9858 (setq entries (sort entries
9859 (lambda (a b)
9860 (cond
9861 ((equal (marker-buffer a) (marker-buffer b))
9862 (< (marker-position a) (marker-position b)))
9863 (t
9864 (string< (buffer-name (marker-buffer a))
9865 (buffer-name (marker-buffer b))))))))
9866
9867 ;; Now loop over all markers and apply cmd
9868 (while (setq e (pop entries))
9869 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9870 (if (not pos)
9871 (progn (message "Skipping removed entry at %s" e)
9872 (setq cntskip (1+ cntskip)))
9873 (goto-char pos)
9874 (let (org-loop-over-headlines-in-active-region)
9875 (eval cmd))
9876 (setq cnt (1+ cnt))))
9877 (when redo-at-end (org-agenda-redo))
9878 (unless org-agenda-persistent-marks
9879 (org-agenda-bulk-unmark-all))
9880 (message "Acted on %d entries%s%s"
9881 cnt
9882 (if (= cntskip 0)
9883 ""
9884 (format ", skipped %d (disappeared before their turn)"
9885 cntskip))
9886 (if (not org-agenda-persistent-marks)
9887 "" " (kept marked)"))))))
9888
9889 (defun org-agenda-capture (&optional with-time)
9890 "Call `org-capture' with the date at point.
9891 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9892 current HH:MM time."
9893 (interactive "P")
9894 (if (not (eq major-mode 'org-agenda-mode))
9895 (user-error "You cannot do this outside of agenda buffers")
9896 (let ((org-overriding-default-time
9897 (org-get-cursor-date (equal with-time 1))))
9898 (call-interactively 'org-capture))))
9899
9900 ;;; Dragging agenda lines forward/backward
9901
9902 (defun org-agenda-reapply-filters ()
9903 "Re-apply all agenda filters."
9904 (mapcar
9905 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9906 `((,org-agenda-tag-filter tag)
9907 (,org-agenda-category-filter category)
9908 (,org-agenda-regexp-filter regexp)
9909 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9910 (,(get 'org-agenda-category-filter :preset-filter) category)
9911 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9912
9913 (defun org-agenda-drag-line-forward (arg &optional backward)
9914 "Drag an agenda line forward by ARG lines.
9915 When the optional argument `backward' is non-nil, move backward."
9916 (interactive "p")
9917 (let ((inhibit-read-only t) lst line)
9918 (if (or (not (get-text-property (point) 'txt))
9919 (save-excursion
9920 (dotimes (n arg)
9921 (move-beginning-of-line (if backward 0 2))
9922 (push (not (get-text-property (point) 'txt)) lst))
9923 (delq nil lst)))
9924 (message "Cannot move line forward")
9925 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9926 (move-beginning-of-line 1)
9927 (setq line (buffer-substring (point) end))
9928 (delete-region (point) end)
9929 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9930 (insert line)
9931 (org-agenda-reapply-filters)
9932 (org-agenda-mark-clocking-task)
9933 (move-beginning-of-line 0)))))
9934
9935 (defun org-agenda-drag-line-backward (arg)
9936 "Drag an agenda line backward by ARG lines."
9937 (interactive "p")
9938 (org-agenda-drag-line-forward arg t))
9939
9940 ;;; Flagging notes
9941
9942 (defun org-agenda-show-the-flagging-note ()
9943 "Display the flagging note in the other window.
9944 When called a second time in direct sequence, offer to remove the FLAGGING
9945 tag and (if present) the flagging note."
9946 (interactive)
9947 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9948 (win (selected-window))
9949 note heading newhead)
9950 (unless hdmarker
9951 (user-error "No linked entry at point"))
9952 (if (and (eq this-command last-command)
9953 (y-or-n-p "Unflag and remove any flagging note? "))
9954 (progn
9955 (org-agenda-remove-flag hdmarker)
9956 (let ((win (get-buffer-window "*Flagging Note*")))
9957 (and win (delete-window win)))
9958 (message "Entry unflagged"))
9959 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9960 (unless note
9961 (user-error "No flagging note"))
9962 (org-kill-new note)
9963 (org-switch-to-buffer-other-window "*Flagging Note*")
9964 (erase-buffer)
9965 (insert note)
9966 (goto-char (point-min))
9967 (while (re-search-forward "\\\\n" nil t)
9968 (replace-match "\n" t t))
9969 (goto-char (point-min))
9970 (select-window win)
9971 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9972
9973 (defun org-agenda-remove-flag (marker)
9974 "Remove the FLAGGED tag and any flagging note in the entry."
9975 (let (newhead)
9976 (org-with-point-at marker
9977 (org-toggle-tag "FLAGGED" 'off)
9978 (org-entry-delete nil "THEFLAGGINGNOTE")
9979 (setq newhead (org-get-heading)))
9980 (org-agenda-change-all-lines newhead marker)
9981 (message "Entry unflagged")))
9982
9983 (defun org-agenda-get-any-marker (&optional pos)
9984 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9985 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9986
9987 ;;; Appointment reminders
9988
9989 (defvar appt-time-msg-list) ; defined in appt.el
9990
9991 ;;;###autoload
9992 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9993 "Activate appointments found in `org-agenda-files'.
9994 With a \\[universal-argument] prefix, refresh the list of
9995 appointments.
9996
9997 If FILTER is t, interactively prompt the user for a regular
9998 expression, and filter out entries that don't match it.
9999
10000 If FILTER is a string, use this string as a regular expression
10001 for filtering entries out.
10002
10003 If FILTER is a function, filter out entries against which
10004 calling the function returns nil. This function takes one
10005 argument: an entry from `org-agenda-get-day-entries'.
10006
10007 FILTER can also be an alist with the car of each cell being
10008 either 'headline or 'category. For example:
10009
10010 '((headline \"IMPORTANT\")
10011 (category \"Work\"))
10012
10013 will only add headlines containing IMPORTANT or headlines
10014 belonging to the \"Work\" category.
10015
10016 ARGS are symbols indicating what kind of entries to consider.
10017 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10018 \(i.e., deadlines and scheduled items with a hh:mm specification)
10019 and :timestamp entries. See the docstring of `org-diary' for
10020 details and examples.
10021
10022 If an entry has a APPT_WARNTIME property, its value will be used
10023 to override `appt-message-warning-time'."
10024 (interactive "P")
10025 (if refresh (setq appt-time-msg-list nil))
10026 (if (eq filter t)
10027 (setq filter (read-from-minibuffer "Regexp filter: ")))
10028 (let* ((cnt 0) ; count added events
10029 (scope (or args '(:deadline* :scheduled* :timestamp)))
10030 (org-agenda-new-buffers nil)
10031 (org-deadline-warning-days 0)
10032 ;; Do not use `org-today' here because appt only takes
10033 ;; time and without date as argument, so it may pass wrong
10034 ;; information otherwise
10035 (today (org-date-to-gregorian
10036 (time-to-days (current-time))))
10037 (org-agenda-restrict nil)
10038 (files (org-agenda-files 'unrestricted)) entries file
10039 (org-agenda-buffer nil))
10040 ;; Get all entries which may contain an appt
10041 (org-agenda-prepare-buffers files)
10042 (while (setq file (pop files))
10043 (setq entries
10044 (delq nil
10045 (append entries
10046 (apply 'org-agenda-get-day-entries
10047 file today scope)))))
10048 ;; Map thru entries and find if we should filter them out
10049 (mapc
10050 (lambda(x)
10051 (let* ((evt (org-trim
10052 (replace-regexp-in-string
10053 org-bracket-link-regexp "\\3"
10054 (or (get-text-property 1 'txt x) ""))))
10055 (cat (get-text-property 1 'org-category x))
10056 (tod (get-text-property 1 'time-of-day x))
10057 (ok (or (null filter)
10058 (and (stringp filter) (string-match filter evt))
10059 (and (functionp filter) (funcall filter x))
10060 (and (listp filter)
10061 (let ((cat-filter (cadr (assoc 'category filter)))
10062 (evt-filter (cadr (assoc 'headline filter))))
10063 (or (and (stringp cat-filter)
10064 (string-match cat-filter cat))
10065 (and (stringp evt-filter)
10066 (string-match evt-filter evt)))))))
10067 (wrn (get-text-property 1 'warntime x)))
10068 ;; FIXME: Shall we remove text-properties for the appt text?
10069 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10070 (when (and ok tod)
10071 (setq tod (concat "00" (number-to-string tod))
10072 tod (when (string-match
10073 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10074 (concat (match-string 1 tod) ":"
10075 (match-string 2 tod))))
10076 (if (version< emacs-version "23.3")
10077 (appt-add tod evt)
10078 (appt-add tod evt wrn))
10079 (setq cnt (1+ cnt))))) entries)
10080 (org-release-buffers org-agenda-new-buffers)
10081 (if (eq cnt 0)
10082 (message "No event to add")
10083 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10084
10085 (defun org-agenda-todayp (date)
10086 "Does DATE mean today, when considering `org-extend-today-until'?"
10087 (let ((today (org-today))
10088 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10089 date)))
10090 (eq date today)))
10091
10092 (defun org-agenda-todo-yesterday (&optional arg)
10093 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10094 (interactive "P")
10095 (let* ((hour (third (decode-time
10096 (org-current-time))))
10097 (org-extend-today-until (1+ hour)))
10098 (org-agenda-todo arg)))
10099
10100 (provide 'org-agenda)
10101
10102 ;;; org-agenda.el ends here