org-agenda.el: Speedup redisplay of an agenda item after it has been modified
[org-mode.git] / lisp / org-agenda.el
blob6f97bbdf4c27f4645c6b154acec30444462aec91
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
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.
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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
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:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
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.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
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))
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))
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)
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
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.")
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")))
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)
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)))
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
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
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"))))
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))
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.
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)
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)
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:
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;
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
211 .org-agenda-done {
212 color: #339933;
214 .org-done {
215 color: #339933;
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
222 or, if you want to keep the style in a file,
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
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 'string)
232 (defcustom org-agenda-persistent-filter nil
233 "When set, keep filters from one agenda view to the next."
234 :group 'org-agenda
235 :type 'boolean)
237 (defgroup org-agenda-custom-commands nil
238 "Options concerning agenda views in Org-mode."
239 :tag "Org Agenda Custom Commands"
240 :group 'org-agenda)
242 (defconst org-sorting-choice
243 '(choice
244 (const time-up) (const time-down)
245 (const category-keep) (const category-up) (const category-down)
246 (const tag-down) (const tag-up)
247 (const priority-up) (const priority-down)
248 (const todo-state-up) (const todo-state-down)
249 (const effort-up) (const effort-down)
250 (const habit-up) (const habit-down)
251 (const alpha-up) (const alpha-down)
252 (const user-defined-up) (const user-defined-down))
253 "Sorting choices.")
255 ;; Keep custom values for `org-agenda-filter-preset' compatible with
256 ;; the new variable `org-agenda-tag-filter-preset'.
257 (if (fboundp 'defvaralias)
258 (defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
259 (defvaralias 'org-agenda-filter 'org-agenda-tag-filter))
261 (defconst org-agenda-custom-commands-local-options
262 `(repeat :tag "Local settings for this command. Remember to quote values"
263 (choice :tag "Setting"
264 (list :tag "Heading for this block"
265 (const org-agenda-overriding-header)
266 (string :tag "Headline"))
267 (list :tag "Files to be searched"
268 (const org-agenda-files)
269 (list
270 (const :format "" quote)
271 (repeat (file))))
272 (list :tag "Sorting strategy"
273 (const org-agenda-sorting-strategy)
274 (list
275 (const :format "" quote)
276 (repeat
277 ,org-sorting-choice)))
278 (list :tag "Prefix format"
279 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
280 (string))
281 (list :tag "Number of days in agenda"
282 (const org-agenda-span)
283 (choice (const :tag "Day" 'day)
284 (const :tag "Week" 'week)
285 (const :tag "Month" 'month)
286 (const :tag "Year" 'year)
287 (integer :tag "Custom")))
288 (list :tag "Fixed starting date"
289 (const org-agenda-start-day)
290 (string :value "2007-11-01"))
291 (list :tag "Start on day of week"
292 (const org-agenda-start-on-weekday)
293 (choice :value 1
294 (const :tag "Today" nil)
295 (integer :tag "Weekday No.")))
296 (list :tag "Include data from diary"
297 (const org-agenda-include-diary)
298 (boolean))
299 (list :tag "Deadline Warning days"
300 (const org-deadline-warning-days)
301 (integer :value 1))
302 (list :tag "Category filter preset"
303 (const org-agenda-category-filter-preset)
304 (list
305 (const :format "" quote)
306 (repeat
307 (string :tag "+category or -category"))))
308 (list :tag "Tags filter preset"
309 (const org-agenda-tag-filter-preset)
310 (list
311 (const :format "" quote)
312 (repeat
313 (string :tag "+tag or -tag"))))
314 (list :tag "Set daily/weekly entry types"
315 (const org-agenda-entry-types)
316 (list
317 (const :format "" quote)
318 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
319 (const :deadline)
320 (const :scheduled)
321 (const :timestamp)
322 (const :sexp))))
323 (list :tag "Standard skipping condition"
324 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
325 (const org-agenda-skip-function)
326 (list
327 (const :format "" quote)
328 (list
329 (choice
330 :tag "Skipping range"
331 (const :tag "Skip entry" org-agenda-skip-entry-if)
332 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
333 (repeat :inline t :tag "Conditions for skipping"
334 (choice
335 :tag "Condition type"
336 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
337 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
338 (list :tag "TODO state is" :inline t
339 (const 'todo)
340 (choice
341 (const :tag "any not-done state" 'todo)
342 (const :tag "any done state" 'done)
343 (const :tag "any state" 'any)
344 (list :tag "Keyword list"
345 (const :format "" quote)
346 (repeat (string :tag "Keyword")))))
347 (list :tag "TODO state is not" :inline t
348 (const 'nottodo)
349 (choice
350 (const :tag "any not-done state" 'todo)
351 (const :tag "any done state" 'done)
352 (const :tag "any state" 'any)
353 (list :tag "Keyword list"
354 (const :format "" quote)
355 (repeat (string :tag "Keyword")))))
356 (const :tag "scheduled" 'scheduled)
357 (const :tag "not scheduled" 'notscheduled)
358 (const :tag "deadline" 'deadline)
359 (const :tag "no deadline" 'notdeadline)
360 (const :tag "timestamp" 'timestamp)
361 (const :tag "no timestamp" 'nottimestamp))))))
362 (list :tag "Non-standard skipping condition"
363 :value (org-agenda-skip-function)
364 (const org-agenda-skip-function)
365 (sexp :tag "Function or form (quoted!)"))
366 (list :tag "Any variable"
367 (variable :tag "Variable")
368 (sexp :tag "Value (sexp)"))))
369 "Selection of examples for agenda command settings.
370 This will be spliced into the custom type of
371 `org-agenda-custom-commands'.")
374 (defcustom org-agenda-custom-commands '(("n" "Agenda and all TODO's"
375 ((agenda "") (alltodo))))
376 "Custom commands for the agenda.
377 These commands will be offered on the splash screen displayed by the
378 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
380 (key desc type match settings files)
382 key The key (one or more characters as a string) to be associated
383 with the command.
384 desc A description of the command, when omitted or nil, a default
385 description is built using MATCH.
386 type The command type, any of the following symbols:
387 agenda The daily/weekly agenda.
388 todo Entries with a specific TODO keyword, in all agenda files.
389 search Entries containing search words entry or headline.
390 tags Tags/Property/TODO match in all agenda files.
391 tags-todo Tags/P/T match in all agenda files, TODO entries only.
392 todo-tree Sparse tree of specific TODO keyword in *current* file.
393 tags-tree Sparse tree with all tags matches in *current* file.
394 occur-tree Occur sparse tree for *current* file.
395 ... A user-defined function.
396 match What to search for:
397 - a single keyword for TODO keyword searches
398 - a tags match expression for tags searches
399 - a word search expression for text searches.
400 - a regular expression for occur searches
401 For all other commands, this should be the empty string.
402 settings A list of option settings, similar to that in a let form, so like
403 this: ((opt1 val1) (opt2 val2) ...). The values will be
404 evaluated at the moment of execution, so quote them when needed.
405 files A list of files file to write the produced agenda buffer to
406 with the command `org-store-agenda-views'.
407 If a file name ends in \".html\", an HTML version of the buffer
408 is written out. If it ends in \".ps\", a postscript version is
409 produced. Otherwise, only the plain text is written to the file.
411 You can also define a set of commands, to create a composite agenda buffer.
412 In this case, an entry looks like this:
414 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
416 where
418 desc A description string to be displayed in the dispatcher menu.
419 cmd An agenda command, similar to the above. However, tree commands
420 are not allowed, but instead you can get agenda and global todo list.
421 So valid commands for a set are:
422 (agenda \"\" settings)
423 (alltodo \"\" settings)
424 (stuck \"\" settings)
425 (todo \"match\" settings files)
426 (search \"match\" settings files)
427 (tags \"match\" settings files)
428 (tags-todo \"match\" settings files)
430 Each command can carry a list of options, and another set of options can be
431 given for the whole set of commands. Individual command options take
432 precedence over the general options.
434 When using several characters as key to a command, the first characters
435 are prefix commands. For the dispatcher to display useful information, you
436 should provide a description for the prefix, like
438 (setq org-agenda-custom-commands
439 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
440 (\"hl\" tags \"+HOME+Lisa\")
441 (\"hp\" tags \"+HOME+Peter\")
442 (\"hk\" tags \"+HOME+Kim\")))"
443 :group 'org-agenda-custom-commands
444 :type `(repeat
445 (choice :value ("x" "Describe command here" tags "" nil)
446 (list :tag "Single command"
447 (string :tag "Access Key(s) ")
448 (option (string :tag "Description"))
449 (choice
450 (const :tag "Agenda" agenda)
451 (const :tag "TODO list" alltodo)
452 (const :tag "Search words" search)
453 (const :tag "Stuck projects" stuck)
454 (const :tag "Tags/Property match (all agenda files)" tags)
455 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
456 (const :tag "TODO keyword search (all agenda files)" todo)
457 (const :tag "Tags sparse tree (current buffer)" tags-tree)
458 (const :tag "TODO keyword tree (current buffer)" todo-tree)
459 (const :tag "Occur tree (current buffer)" occur-tree)
460 (sexp :tag "Other, user-defined function"))
461 (string :tag "Match (only for some commands)")
462 ,org-agenda-custom-commands-local-options
463 (option (repeat :tag "Export" (file :tag "Export to"))))
464 (list :tag "Command series, all agenda files"
465 (string :tag "Access Key(s)")
466 (string :tag "Description ")
467 (repeat :tag "Component"
468 (choice
469 (list :tag "Agenda"
470 (const :format "" agenda)
471 (const :tag "" :format "" "")
472 ,org-agenda-custom-commands-local-options)
473 (list :tag "TODO list (all keywords)"
474 (const :format "" alltodo)
475 (const :tag "" :format "" "")
476 ,org-agenda-custom-commands-local-options)
477 (list :tag "Search words"
478 (const :format "" search)
479 (string :tag "Match")
480 ,org-agenda-custom-commands-local-options)
481 (list :tag "Stuck projects"
482 (const :format "" stuck)
483 (const :tag "" :format "" "")
484 ,org-agenda-custom-commands-local-options)
485 (list :tag "Tags search"
486 (const :format "" tags)
487 (string :tag "Match")
488 ,org-agenda-custom-commands-local-options)
489 (list :tag "Tags search, TODO entries only"
490 (const :format "" tags-todo)
491 (string :tag "Match")
492 ,org-agenda-custom-commands-local-options)
493 (list :tag "TODO keyword search"
494 (const :format "" todo)
495 (string :tag "Match")
496 ,org-agenda-custom-commands-local-options)
497 (list :tag "Other, user-defined function"
498 (symbol :tag "function")
499 (string :tag "Match")
500 ,org-agenda-custom-commands-local-options)))
502 (repeat :tag "Settings for entire command set"
503 (list (variable :tag "Any variable")
504 (sexp :tag "Value")))
505 (option (repeat :tag "Export" (file :tag "Export to"))))
506 (cons :tag "Prefix key documentation"
507 (string :tag "Access Key(s)")
508 (string :tag "Description ")))))
510 (defcustom org-agenda-query-register ?o
511 "The register holding the current query string.
512 The purpose of this is that if you construct a query string interactively,
513 you can then use it to define a custom command."
514 :group 'org-agenda-custom-commands
515 :type 'character)
517 (defcustom org-stuck-projects
518 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
519 "How to identify stuck projects.
520 This is a list of four items:
521 1. A tags/todo/property matcher string that is used to identify a project.
522 See the manual for a description of tag and property searches.
523 The entire tree below a headline matched by this is considered one project.
524 2. A list of TODO keywords identifying non-stuck projects.
525 If the project subtree contains any headline with one of these todo
526 keywords, the project is considered to be not stuck. If you specify
527 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
528 3. A list of tags identifying non-stuck projects.
529 If the project subtree contains any headline with one of these tags,
530 the project is considered to be not stuck. If you specify \"*\" as
531 a tag, any tag will mark the project unstuck. Note that this is about
532 the explicit presence of a tag somewhere in the subtree, inherited
533 tags to not count here. If inherited tags make a project not stuck,
534 use \"-TAG\" in the tags part of the matcher under (1.) above.
535 4. An arbitrary regular expression matching non-stuck projects.
537 If the project turns out to be not stuck, search continues also in the
538 subtree to see if any of the subtasks have project status.
540 See also the variable `org-tags-match-list-sublevels' which applies
541 to projects matched by this search as well.
543 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
544 or `C-c a #' to produce the list."
545 :group 'org-agenda-custom-commands
546 :type '(list
547 (string :tag "Tags/TODO match to identify a project")
548 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
549 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
550 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
552 (defcustom org-agenda-filter-effort-default-operator "<"
553 "The default operator for effort estimate filtering.
554 If you select an effort estimate limit without first pressing an operator,
555 this one will be used."
556 :group 'org-agenda-custom-commands
557 :type '(choice (const :tag "less or equal" "<")
558 (const :tag "greater or equal"">")
559 (const :tag "equal" "=")))
561 (defgroup org-agenda-skip nil
562 "Options concerning skipping parts of agenda files."
563 :tag "Org Agenda Skip"
564 :group 'org-agenda)
566 (defcustom org-agenda-skip-function-global nil
567 "Function to be called at each match during agenda construction.
568 If this function returns nil, the current match should not be skipped.
569 If the function decided to skip an agenda match, is must return the
570 buffer position from which the search should be continued.
571 This may also be a Lisp form, which will be evaluated.
573 This variable will be applied to every agenda match, including
574 tags/property searches and TODO lists. So try to make the test function
575 do its checking as efficiently as possible. To implement a skipping
576 condition just for specific agenda commands, use the variable
577 `org-agenda-skip-function' which can be set in the options section
578 of custom agenda commands."
579 :group 'org-agenda-skip
580 :type 'sexp)
582 (defgroup org-agenda-daily/weekly nil
583 "Options concerning the daily/weekly agenda."
584 :tag "Org Agenda Daily/Weekly"
585 :group 'org-agenda)
586 (defgroup org-agenda-todo-list nil
587 "Options concerning the global todo list agenda view."
588 :tag "Org Agenda Todo List"
589 :group 'org-agenda)
590 (defgroup org-agenda-match-view nil
591 "Options concerning the general tags/property/todo match agenda view."
592 :tag "Org Agenda Match View"
593 :group 'org-agenda)
594 (defgroup org-agenda-search-view nil
595 "Options concerning the general tags/property/todo match agenda view."
596 :tag "Org Agenda Match View"
597 :group 'org-agenda)
599 (defvar org-agenda-archives-mode nil
600 "Non-nil means the agenda will include archived items.
601 If this is the symbol `trees', trees in the selected agenda scope
602 that are marked with the ARCHIVE tag will be included anyway. When this is
603 t, also all archive files associated with the current selection of agenda
604 files will be included.")
606 (defcustom org-agenda-skip-comment-trees t
607 "Non-nil means skip trees that start with the COMMENT keyword.
608 When nil, these trees are also scanned by agenda commands."
609 :group 'org-agenda-skip
610 :type 'boolean)
612 (defcustom org-agenda-todo-list-sublevels t
613 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
614 When nil, the sublevels of a TODO entry are not checked, resulting in
615 potentially much shorter TODO lists."
616 :group 'org-agenda-skip
617 :group 'org-agenda-todo-list
618 :type 'boolean)
620 (defcustom org-agenda-todo-ignore-with-date nil
621 "Non-nil means don't show entries with a date in the global todo list.
622 You can use this if you prefer to mark mere appointments with a TODO keyword,
623 but don't want them to show up in the TODO list.
624 When this is set, it also covers deadlines and scheduled items, the settings
625 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
626 will be ignored.
627 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
628 :group 'org-agenda-skip
629 :group 'org-agenda-todo-list
630 :type 'boolean)
632 (defcustom org-agenda-todo-ignore-timestamp nil
633 "Non-nil means don't show entries with a timestamp.
634 This applies when creating the global todo list.
635 Valid values are:
637 past Don't show entries for today or in the past.
639 future Don't show entries with a timestamp in the future.
640 The idea behind this is that if it has a future
641 timestamp, you don't want to think about it until the
642 date.
644 all Don't show any entries with a timestamp in the global todo list.
645 The idea behind this is that by setting a timestamp, you
646 have already \"taken care\" of this item.
648 This variable can also have an integer as a value. If positive (N),
649 todos with a timestamp N or more days in the future will be ignored. If
650 negative (-N), todos with a timestamp N or more days in the past will be
651 ignored. If 0, todos with a timestamp either today or in the future will
652 be ignored. For example, a value of -1 will exclude todos with a
653 timestamp in the past (yesterday or earlier), while a value of 7 will
654 exclude todos with a timestamp a week or more in the future.
656 See also `org-agenda-todo-ignore-with-date'.
657 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
658 to make his option also apply to the tags-todo list."
659 :group 'org-agenda-skip
660 :group 'org-agenda-todo-list
661 :version "24.1"
662 :type '(choice
663 (const :tag "Ignore future timestamp todos" future)
664 (const :tag "Ignore past or present timestamp todos" past)
665 (const :tag "Ignore all timestamp todos" all)
666 (const :tag "Show timestamp todos" nil)
667 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
669 (defcustom org-agenda-todo-ignore-scheduled nil
670 "Non-nil means, ignore some scheduled TODO items when making TODO list.
671 This applies when creating the global todo list.
672 Valid values are:
674 past Don't show entries scheduled today or in the past.
676 future Don't show entries scheduled in the future.
677 The idea behind this is that by scheduling it, you don't want to
678 think about it until the scheduled date.
680 all Don't show any scheduled entries in the global todo list.
681 The idea behind this is that by scheduling it, you have already
682 \"taken care\" of this item.
684 t Same as `all', for backward compatibility.
686 This variable can also have an integer as a value. See
687 `org-agenda-todo-ignore-timestamp' for more details.
689 See also `org-agenda-todo-ignore-with-date'.
690 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
691 to make his option also apply to the tags-todo list."
692 :group 'org-agenda-skip
693 :group 'org-agenda-todo-list
694 :type '(choice
695 (const :tag "Ignore future-scheduled todos" future)
696 (const :tag "Ignore past- or present-scheduled todos" past)
697 (const :tag "Ignore all scheduled todos" all)
698 (const :tag "Ignore all scheduled todos (compatibility)" t)
699 (const :tag "Show scheduled todos" nil)
700 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
702 (defcustom org-agenda-todo-ignore-deadlines nil
703 "Non-nil means ignore some deadlined TODO items when making TODO list.
704 There are different motivations for using different values, please think
705 carefully when configuring this variable.
707 This applies when creating the global todo list.
708 Valid values are:
710 near Don't show near deadline entries. A deadline is near when it is
711 closer than `org-deadline-warning-days' days. The idea behind this
712 is that such items will appear in the agenda anyway.
714 far Don't show TODO entries where a deadline has been defined, but
715 the deadline is not near. This is useful if you don't want to
716 use the todo list to figure out what to do now.
718 past Don't show entries with a deadline timestamp for today or in the past.
720 future Don't show entries with a deadline timestamp in the future, not even
721 when they become `near' ones. Use it with caution.
723 all Ignore all TODO entries that do have a deadline.
725 t Same as `near', for backward compatibility.
727 This variable can also have an integer as a value. See
728 `org-agenda-todo-ignore-timestamp' for more details.
730 See also `org-agenda-todo-ignore-with-date'.
731 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
732 to make his option also apply to the tags-todo list."
733 :group 'org-agenda-skip
734 :group 'org-agenda-todo-list
735 :type '(choice
736 (const :tag "Ignore near deadlines" near)
737 (const :tag "Ignore near deadlines (compatibility)" t)
738 (const :tag "Ignore far deadlines" far)
739 (const :tag "Ignore all TODOs with a deadlines" all)
740 (const :tag "Show all TODOs, even if they have a deadline" nil)
741 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
743 (defcustom org-agenda-tags-todo-honor-ignore-options nil
744 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
745 The variables
746 `org-agenda-todo-ignore-with-date',
747 `org-agenda-todo-ignore-timestamp',
748 `org-agenda-todo-ignore-scheduled',
749 `org-agenda-todo-ignore-deadlines'
750 make the global TODO list skip entries that have time stamps of certain
751 kinds. If this option is set, the same options will also apply for the
752 tags-todo search, which is the general tags/property matcher
753 restricted to unfinished TODO entries only."
754 :group 'org-agenda-skip
755 :group 'org-agenda-todo-list
756 :group 'org-agenda-match-view
757 :type 'boolean)
759 (defcustom org-agenda-skip-scheduled-if-done nil
760 "Non-nil means don't show scheduled items in agenda when they are done.
761 This is relevant for the daily/weekly agenda, not for the TODO list. And
762 it applies only to the actual date of the scheduling. Warnings about
763 an item with a past scheduling dates are always turned off when the item
764 is DONE."
765 :group 'org-agenda-skip
766 :group 'org-agenda-daily/weekly
767 :type 'boolean)
769 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
770 "Non-nil means skip scheduling line if same entry shows because of deadline.
771 In the agenda of today, an entry can show up multiple times because
772 it is both scheduled and has a nearby deadline, and maybe a plain time
773 stamp as well.
774 When this variable is t, then only the deadline is shown and the fact that
775 the entry is scheduled today or was scheduled previously is not shown.
776 When this variable is nil, the entry will be shown several times. When
777 the variable is the symbol `not-today', then skip scheduled previously,
778 but not scheduled today."
779 :group 'org-agenda-skip
780 :group 'org-agenda-daily/weekly
781 :type '(choice
782 (const :tag "Never" nil)
783 (const :tag "Always" t)
784 (const :tag "Not when scheduled today" not-today)))
786 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
787 "Non-nil means skip timestamp line if same entry shows because of deadline.
788 In the agenda of today, an entry can show up multiple times
789 because it has both a plain timestamp and has a nearby deadline.
790 When this variable is t, then only the deadline is shown and the
791 fact that the entry has a timestamp for or including today is not
792 shown. When this variable is nil, the entry will be shown
793 several times."
794 :group 'org-agenda-skip
795 :group 'org-agenda-daily/weekly
796 :version "24.1"
797 :type '(choice
798 (const :tag "Never" nil)
799 (const :tag "Always" t)))
801 (defcustom org-agenda-skip-deadline-if-done nil
802 "Non-nil means don't show deadlines when the corresponding item is done.
803 When nil, the deadline is still shown and should give you a happy feeling.
804 This is relevant for the daily/weekly agenda. And it applied only to the
805 actually date of the deadline. Warnings about approaching and past-due
806 deadlines are always turned off when the item is DONE."
807 :group 'org-agenda-skip
808 :group 'org-agenda-daily/weekly
809 :type 'boolean)
811 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
812 "Non-nil means skip deadline prewarning when entry is also scheduled.
813 This will apply on all days where a prewarning for the deadline would
814 be shown, but not at the day when the entry is actually due. On that day,
815 the deadline will be shown anyway.
816 This variable may be set to nil, t, or a number which will then give
817 the number of days before the actual deadline when the prewarnings
818 should resume.
819 This can be used in a workflow where the first showing of the deadline will
820 trigger you to schedule it, and then you don't want to be reminded of it
821 because you will take care of it on the day when scheduled."
822 :group 'org-agenda-skip
823 :group 'org-agenda-daily/weekly
824 :version "24.1"
825 :type '(choice
826 (const :tag "Alwas show prewarning" nil)
827 (const :tag "Remove prewarning if entry is scheduled" t)
828 (integer :tag "Restart prewarning N days before deadline")))
830 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
831 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
832 When non-nil, after the search for timestamps has matched once in an
833 entry, the rest of the entry will not be searched."
834 :group 'org-agenda-skip
835 :type 'boolean)
837 (defcustom org-agenda-skip-timestamp-if-done nil
838 "Non-nil means don't select item by timestamp or -range if it is DONE."
839 :group 'org-agenda-skip
840 :group 'org-agenda-daily/weekly
841 :type 'boolean)
843 (defcustom org-agenda-dim-blocked-tasks t
844 "Non-nil means dim blocked tasks in the agenda display.
845 This causes some overhead during agenda construction, but if you
846 have turned on `org-enforce-todo-dependencies',
847 `org-enforce-todo-checkbox-dependencies', or any other blocking
848 mechanism, this will create useful feedback in the agenda.
850 Instead of t, this variable can also have the value `invisible'.
851 Then blocked tasks will be invisible and only become visible when
852 they become unblocked. An exemption to this behavior is when a task is
853 blocked because of unchecked checkboxes below it. Since checkboxes do
854 not show up in the agenda views, making this task invisible you remove any
855 trace from agenda views that there is something to do. Therefore, a task
856 that is blocked because of checkboxes will never be made invisible, it
857 will only be dimmed."
858 :group 'org-agenda-daily/weekly
859 :group 'org-agenda-todo-list
860 :type '(choice
861 (const :tag "Do not dim" nil)
862 (const :tag "Dim to a gray face" t)
863 (const :tag "Make invisible" invisible)))
865 (defcustom org-timeline-show-empty-dates 3
866 "Non-nil means `org-timeline' also shows dates without an entry.
867 When nil, only the days which actually have entries are shown.
868 When t, all days between the first and the last date are shown.
869 When an integer, show also empty dates, but if there is a gap of more than
870 N days, just insert a special line indicating the size of the gap."
871 :group 'org-agenda-skip
872 :type '(choice
873 (const :tag "None" nil)
874 (const :tag "All" t)
875 (integer :tag "at most")))
877 (defgroup org-agenda-startup nil
878 "Options concerning initial settings in the Agenda in Org Mode."
879 :tag "Org Agenda Startup"
880 :group 'org-agenda)
882 (defcustom org-agenda-menu-show-matcher t
883 "Non-nil means show the match string in the agenda dispatcher menu.
884 When nil, the matcher string is not shown, but is put into the help-echo
885 property so than moving the mouse over the command shows it.
886 Setting it to nil is good if matcher strings are very long and/or if
887 you want to use two-columns display (see `org-agenda-menu-two-columns')."
888 :group 'org-agenda
889 :version "24.1"
890 :type 'boolean)
892 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
894 (defcustom org-agenda-menu-two-columns nil
895 "Non-nil means, use two columns to show custom commands in the dispatcher.
896 If you use this, you probably want to set `org-agenda-menu-show-matcher'
897 to nil."
898 :group 'org-agenda
899 :version "24.1"
900 :type 'boolean)
902 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
903 (defcustom org-agenda-finalize-hook nil
904 "Hook run just before displaying an agenda buffer.
905 The buffer is still writable when the hook is called.
907 You can modify some of the buffer substrings but you should be
908 extra careful not to modify the text properties of the agenda
909 headlines as the agenda display heavily relies on them."
910 :group 'org-agenda-startup
911 :type 'hook)
913 (defcustom org-agenda-mouse-1-follows-link nil
914 "Non-nil means mouse-1 on a link will follow the link in the agenda.
915 A longer mouse click will still set point. Does not work on XEmacs.
916 Needs to be set before org.el is loaded."
917 :group 'org-agenda-startup
918 :type 'boolean)
920 (defcustom org-agenda-start-with-follow-mode nil
921 "The initial value of follow mode in a newly created agenda window."
922 :group 'org-agenda-startup
923 :type 'boolean)
925 (defcustom org-agenda-follow-indirect nil
926 "Non-nil means `org-agenda-follow-mode' displays only the
927 current item's tree, in an indirect buffer."
928 :group 'org-agenda
929 :version "24.1"
930 :type 'boolean)
932 (defcustom org-agenda-show-outline-path t
933 "Non-nil means show outline path in echo area after line motion."
934 :group 'org-agenda-startup
935 :type 'boolean)
937 (defcustom org-agenda-start-with-entry-text-mode nil
938 "The initial value of entry-text-mode in a newly created agenda window."
939 :group 'org-agenda-startup
940 :type 'boolean)
942 (defcustom org-agenda-entry-text-maxlines 5
943 "Number of text lines to be added when `E' is pressed in the agenda.
945 Note that this variable only used during agenda display. Add add entry text
946 when exporting the agenda, configure the variable
947 `org-agenda-add-entry-ext-maxlines'."
948 :group 'org-agenda
949 :type 'integer)
951 (defcustom org-agenda-entry-text-exclude-regexps nil
952 "List of regular expressions to clean up entry text.
953 The complete matches of all regular expressions in this list will be
954 removed from entry text before it is shown in the agenda."
955 :group 'org-agenda
956 :type '(repeat (regexp)))
958 (defvar org-agenda-entry-text-cleanup-hook nil
959 "Hook that is run after basic cleanup of entry text to be shown in agenda.
960 This cleanup is done in a temporary buffer, so the function may inspect and
961 change the entire buffer.
962 Some default stuff like drawers and scheduling/deadline dates will already
963 have been removed when this is called, as will any matches for regular
964 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
966 (defvar org-agenda-include-inactive-timestamps nil
967 "Non-nil means include inactive time stamps in agenda and timeline.
968 Dynamically scoped.")
970 (defgroup org-agenda-windows nil
971 "Options concerning the windows used by the Agenda in Org Mode."
972 :tag "Org Agenda Windows"
973 :group 'org-agenda)
975 (defcustom org-agenda-window-setup 'reorganize-frame
976 "How the agenda buffer should be displayed.
977 Possible values for this option are:
979 current-window Show agenda in the current window, keeping all other windows.
980 other-window Use `switch-to-buffer-other-window' to display agenda.
981 reorganize-frame Show only two windows on the current frame, the current
982 window and the agenda.
983 other-frame Use `switch-to-buffer-other-frame' to display agenda.
984 Also, when exiting the agenda, kill that frame.
985 See also the variable `org-agenda-restore-windows-after-quit'."
986 :group 'org-agenda-windows
987 :type '(choice
988 (const current-window)
989 (const other-frame)
990 (const other-window)
991 (const reorganize-frame)))
993 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
994 "The min and max height of the agenda window as a fraction of frame height.
995 The value of the variable is a cons cell with two numbers between 0 and 1.
996 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
997 :group 'org-agenda-windows
998 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1000 (defcustom org-agenda-restore-windows-after-quit nil
1001 "Non-nil means restore window configuration upon exiting agenda.
1002 Before the window configuration is changed for displaying the agenda,
1003 the current status is recorded. When the agenda is exited with
1004 `q' or `x' and this option is set, the old state is restored. If
1005 `org-agenda-window-setup' is `other-frame', the value of this
1006 option will be ignored."
1007 :group 'org-agenda-windows
1008 :type 'boolean)
1010 (defcustom org-agenda-ndays nil
1011 "Number of days to include in overview display.
1012 Should be 1 or 7.
1013 Obsolete, see `org-agenda-span'."
1014 :group 'org-agenda-daily/weekly
1015 :type 'integer)
1017 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1019 (defcustom org-agenda-span 'week
1020 "Number of days to include in overview display.
1021 Can be day, week, month, year, or any number of days.
1022 Custom commands can set this variable in the options section."
1023 :group 'org-agenda-daily/weekly
1024 :type '(choice (const :tag "Day" day)
1025 (const :tag "Week" week)
1026 (const :tag "Month" month)
1027 (const :tag "Year" year)
1028 (integer :tag "Custom")))
1030 (defcustom org-agenda-start-on-weekday 1
1031 "Non-nil means start the overview always on the specified weekday.
1032 0 denotes Sunday, 1 denotes Monday etc.
1033 When nil, always start on the current day.
1034 Custom commands can set this variable in the options section."
1035 :group 'org-agenda-daily/weekly
1036 :type '(choice (const :tag "Today" nil)
1037 (integer :tag "Weekday No.")))
1039 (defcustom org-agenda-show-all-dates t
1040 "Non-nil means `org-agenda' shows every day in the selected range.
1041 When nil, only the days which actually have entries are shown."
1042 :group 'org-agenda-daily/weekly
1043 :type 'boolean)
1045 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1046 "Format string for displaying dates in the agenda.
1047 Used by the daily/weekly agenda and by the timeline. This should be
1048 a format string understood by `format-time-string', or a function returning
1049 the formatted date as a string. The function must take a single argument,
1050 a calendar-style date list like (month day year)."
1051 :group 'org-agenda-daily/weekly
1052 :type '(choice
1053 (string :tag "Format string")
1054 (function :tag "Function")))
1056 (defun org-agenda-format-date-aligned (date)
1057 "Format a date string for display in the daily/weekly agenda, or timeline.
1058 This function makes sure that dates are aligned for easy reading."
1059 (require 'cal-iso)
1060 (let* ((dayname (calendar-day-name date))
1061 (day (cadr date))
1062 (day-of-week (calendar-day-of-week date))
1063 (month (car date))
1064 (monthname (calendar-month-name month))
1065 (year (nth 2 date))
1066 (iso-week (org-days-to-iso-week
1067 (calendar-absolute-from-gregorian date)))
1068 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1069 (1- year))
1070 ((and (= month 12) (<= iso-week 1))
1071 (1+ year))
1072 (t year)))
1073 (weekstring (if (= day-of-week 1)
1074 (format " W%02d" iso-week)
1075 "")))
1076 (format "%-10s %2d %s %4d%s"
1077 dayname day monthname year weekstring)))
1079 (defcustom org-agenda-time-leading-zero nil
1080 "Non-nil means use leading zero for military times in agenda.
1081 For example, 9:30am would become 09:30 rather than 9:30."
1082 :group 'org-agenda-daily/weekly
1083 :version "24.1"
1084 :type 'boolean)
1086 (defcustom org-agenda-timegrid-use-ampm nil
1087 "When set, show AM/PM style timestamps on the timegrid."
1088 :group 'org-agenda
1089 :version "24.1"
1090 :type 'boolean)
1092 (defun org-agenda-time-of-day-to-ampm (time)
1093 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1094 (let* ((hour-number (string-to-number (substring time 0 -3)))
1095 (minute (substring time -2))
1096 (ampm "am"))
1097 (cond
1098 ((equal hour-number 12)
1099 (setq ampm "pm"))
1100 ((> hour-number 12)
1101 (setq ampm "pm")
1102 (setq hour-number (- hour-number 12))))
1103 (concat
1104 (if org-agenda-time-leading-zero
1105 (format "%02d" hour-number)
1106 (format "%02s" (number-to-string hour-number)))
1107 ":" minute ampm)))
1109 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1110 "Conditionally convert TIME to AM/PM format
1111 based on `org-agenda-timegrid-use-ampm'"
1112 (if org-agenda-timegrid-use-ampm
1113 (org-agenda-time-of-day-to-ampm time)
1114 time))
1116 (defcustom org-agenda-weekend-days '(6 0)
1117 "Which days are weekend?
1118 These days get the special face `org-agenda-date-weekend' in the agenda
1119 and timeline buffers."
1120 :group 'org-agenda-daily/weekly
1121 :type '(set :greedy t
1122 (const :tag "Monday" 1)
1123 (const :tag "Tuesday" 2)
1124 (const :tag "Wednesday" 3)
1125 (const :tag "Thursday" 4)
1126 (const :tag "Friday" 5)
1127 (const :tag "Saturday" 6)
1128 (const :tag "Sunday" 0)))
1130 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1131 "Non-nil means jump to today when moving a past date forward in time.
1132 When using S-right in the agenda to move a a date forward, and the date
1133 stamp currently points to the past, the first key press will move it
1134 to today. WHen nil, just move one day forward even if the date stays
1135 in the past."
1136 :group 'org-agenda-daily/weekly
1137 :version "24.1"
1138 :type 'boolean)
1140 (defcustom org-agenda-include-diary nil
1141 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1142 Custom commands can set this variable in the options section."
1143 :group 'org-agenda-daily/weekly
1144 :type 'boolean)
1146 (defcustom org-agenda-include-deadlines t
1147 "If non-nil, include entries within their deadline warning period.
1148 Custom commands can set this variable in the options section."
1149 :group 'org-agenda-daily/weekly
1150 :version "24.1"
1151 :type 'boolean)
1153 (defcustom org-agenda-repeating-timestamp-show-all t
1154 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1155 When set to a list of strings, only show occurrences of repeating
1156 stamps for these TODO keywords. When nil, only one occurrence is
1157 shown, either today or the nearest into the future."
1158 :group 'org-agenda-daily/weekly
1159 :type '(choice
1160 (const :tag "Show repeating stamps" t)
1161 (repeat :tag "Show repeating stamps for these TODO keywords"
1162 (string :tag "TODO Keyword"))
1163 (const :tag "Don't show repeating stamps" nil)))
1165 (defcustom org-scheduled-past-days 10000
1166 "No. of days to continue listing scheduled items that are not marked DONE.
1167 When an item is scheduled on a date, it shows up in the agenda on this
1168 day and will be listed until it is marked done for the number of days
1169 given here."
1170 :group 'org-agenda-daily/weekly
1171 :type 'integer)
1173 (defcustom org-agenda-log-mode-items '(closed clock)
1174 "List of items that should be shown in agenda log mode.
1175 This list may contain the following symbols:
1177 closed Show entries that have been closed on that day.
1178 clock Show entries that have received clocked time on that day.
1179 state Show all logged state changes.
1180 Note that instead of changing this variable, you can also press `C-u l' in
1181 the agenda to display all available LOG items temporarily."
1182 :group 'org-agenda-daily/weekly
1183 :type '(set :greedy t (const closed) (const clock) (const state)))
1185 (defcustom org-agenda-clock-consistency-checks
1186 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1187 :gap-ok-around ("4:00")
1188 :default-face ((:background "DarkRed") (:foreground "white"))
1189 :overlap-face nil :gap-face nil :no-end-time-face nil
1190 :long-face nil :short-face nil)
1191 "This is a property list, with the following keys:
1193 :max-duration Mark clocking chunks that are longer than this time.
1194 This is a time string like \"HH:MM\", or the number
1195 of minutes as an integer.
1197 :min-duration Mark clocking chunks that are shorter that this.
1198 This is a time string like \"HH:MM\", or the number
1199 of minutes as an integer.
1201 :max-gap Mark gaps between clocking chunks that are longer than
1202 this duration. A number of minutes, or a string
1203 like \"HH:MM\".
1205 :gap-ok-around List of times during the day which are usually not working
1206 times. When a gap is detected, but the gap contains any
1207 of these times, the gap is *not* reported. For example,
1208 if this is (\"4:00\" \"13:00\") then gaps that contain
1209 4:00 in the morning (i.e. the night) and 13:00
1210 (i.e. a typical lunch time) do not cause a warning.
1211 You should have at least one time during the night in this
1212 list, or otherwise the first task each morning will trigger
1213 a warning because it follows a long gap.
1215 Furthermore, the following properties can be used to define faces for
1216 issue display.
1218 :default-face the default face, if the specific face is undefined
1219 :overlap-face face for overlapping clocks
1220 :gap-face face for gaps between clocks
1221 :no-end-time-face face for incomplete clocks
1222 :long-face face for clock intervals that are too long
1223 :short-face face for clock intervals that are too short"
1224 :group 'org-agenda-daily/weekly
1225 :group 'org-clock
1226 :version "24.1"
1227 :type 'plist)
1229 (defcustom org-agenda-log-mode-add-notes t
1230 "Non-nil means add first line of notes to log entries in agenda views.
1231 If a log item like a state change or a clock entry is associated with
1232 notes, the first line of these notes will be added to the entry in the
1233 agenda display."
1234 :group 'org-agenda-daily/weekly
1235 :type 'boolean)
1237 (defcustom org-agenda-start-with-log-mode nil
1238 "The initial value of log-mode in a newly created agenda window.
1239 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1240 explanations on the possible values."
1241 :group 'org-agenda-startup
1242 :group 'org-agenda-daily/weekly
1243 :type '(choice (const :tag "Don't show log items" nil)
1244 (const :tag "Show only log items" 'only)
1245 (const :tag "Show all possible log items" 'clockcheck)
1246 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1247 (choice (const :tag "Show closed log items" 'closed)
1248 (const :tag "Show clocked log items" 'clock)
1249 (const :tag "Show all logged state changes" 'state)))))
1251 (defcustom org-agenda-start-with-clockreport-mode nil
1252 "The initial value of clockreport-mode in a newly created agenda window."
1253 :group 'org-agenda-startup
1254 :group 'org-agenda-daily/weekly
1255 :type 'boolean)
1257 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1258 "Property list with parameters for the clocktable in clockreport mode.
1259 This is the display mode that shows a clock table in the daily/weekly
1260 agenda, the properties for this dynamic block can be set here.
1261 The usual clocktable parameters are allowed here, but you cannot set
1262 the properties :name, :tstart, :tend, :block, and :scope - these will
1263 be overwritten to make sure the content accurately reflects the
1264 current display in the agenda."
1265 :group 'org-agenda-daily/weekly
1266 :type 'plist)
1268 (defcustom org-agenda-search-view-always-boolean nil
1269 "Non-nil means the search string is interpreted as individual parts.
1271 The search string for search view can either be interpreted as a phrase,
1272 or as a list of snippets that define a boolean search for a number of
1273 strings.
1275 When this is non-nil, the string will be split on whitespace, and each
1276 snippet will be searched individually, and all must match in order to
1277 select an entry. A snippet is then a single string of non-white
1278 characters, or a string in double quotes, or a regexp in {} braces.
1279 If a snippet is preceded by \"-\", the snippet must *not* match.
1280 \"+\" is syntactic sugar for positive selection. Each snippet may
1281 be found as a full word or a partial word, but see the variable
1282 `org-agenda-search-view-force-full-words'.
1284 When this is nil, search will look for the entire search phrase as one,
1285 with each space character matching any amount of whitespace, including
1286 line breaks.
1288 Even when this is nil, you can still switch to Boolean search dynamically
1289 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1290 is a regexp marked with braces like \"{abc}\", this will also switch to
1291 boolean search."
1292 :group 'org-agenda-search-view
1293 :version "24.1"
1294 :type 'boolean)
1296 (if (fboundp 'defvaralias)
1297 (defvaralias 'org-agenda-search-view-search-words-only
1298 'org-agenda-search-view-always-boolean))
1300 (defcustom org-agenda-search-view-force-full-words nil
1301 "Non-nil means, search words must be matches as complete words.
1302 When nil, they may also match part of a word."
1303 :group 'org-agenda-search-view
1304 :version "24.1"
1305 :type 'boolean)
1307 (defgroup org-agenda-time-grid nil
1308 "Options concerning the time grid in the Org-mode Agenda."
1309 :tag "Org Agenda Time Grid"
1310 :group 'org-agenda)
1312 (defcustom org-agenda-search-headline-for-time t
1313 "Non-nil means search headline for a time-of-day.
1314 If the headline contains a time-of-day in one format or another, it will
1315 be used to sort the entry into the time sequence of items for a day.
1316 Some people have time stamps in the headline that refer to the creation
1317 time or so, and then this produces an unwanted side effect. If this is
1318 the case for your, use this variable to turn off searching the headline
1319 for a time."
1320 :group 'org-agenda-time-grid
1321 :type 'boolean)
1323 (defcustom org-agenda-use-time-grid t
1324 "Non-nil means show a time grid in the agenda schedule.
1325 A time grid is a set of lines for specific times (like every two hours between
1326 8:00 and 20:00). The items scheduled for a day at specific times are
1327 sorted in between these lines.
1328 For details about when the grid will be shown, and what it will look like, see
1329 the variable `org-agenda-time-grid'."
1330 :group 'org-agenda-time-grid
1331 :type 'boolean)
1333 (defcustom org-agenda-time-grid
1334 '((daily today require-timed)
1335 "----------------"
1336 (800 1000 1200 1400 1600 1800 2000))
1338 "The settings for time grid for agenda display.
1339 This is a list of three items. The first item is again a list. It contains
1340 symbols specifying conditions when the grid should be displayed:
1342 daily if the agenda shows a single day
1343 weekly if the agenda shows an entire week
1344 today show grid on current date, independent of daily/weekly display
1345 require-timed show grid only if at least one item has a time specification
1347 The second item is a string which will be placed behind the grid time.
1349 The third item is a list of integers, indicating the times that should have
1350 a grid line."
1351 :group 'org-agenda-time-grid
1352 :type
1353 '(list
1354 (set :greedy t :tag "Grid Display Options"
1355 (const :tag "Show grid in single day agenda display" daily)
1356 (const :tag "Show grid in weekly agenda display" weekly)
1357 (const :tag "Always show grid for today" today)
1358 (const :tag "Show grid only if any timed entries are present"
1359 require-timed)
1360 (const :tag "Skip grid times already present in an entry"
1361 remove-match))
1362 (string :tag "Grid String")
1363 (repeat :tag "Grid Times" (integer :tag "Time"))))
1365 (defcustom org-agenda-show-current-time-in-grid t
1366 "Non-nil means show the current time in the time grid."
1367 :group 'org-agenda-time-grid
1368 :version "24.1"
1369 :type 'boolean)
1371 (defcustom org-agenda-current-time-string
1372 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1373 "The string for the current time marker in the agenda."
1374 :group 'org-agenda-time-grid
1375 :version "24.1"
1376 :type 'string)
1378 (defgroup org-agenda-sorting nil
1379 "Options concerning sorting in the Org-mode Agenda."
1380 :tag "Org Agenda Sorting"
1381 :group 'org-agenda)
1383 (defcustom org-agenda-sorting-strategy
1384 '((agenda habit-down time-up priority-down category-keep)
1385 (todo priority-down category-keep)
1386 (tags priority-down category-keep)
1387 (search category-keep))
1388 "Sorting structure for the agenda items of a single day.
1389 This is a list of symbols which will be used in sequence to determine
1390 if an entry should be listed before another entry. The following
1391 symbols are recognized:
1393 time-up Put entries with time-of-day indications first, early first
1394 time-down Put entries with time-of-day indications first, late first
1395 category-keep Keep the default order of categories, corresponding to the
1396 sequence in `org-agenda-files'.
1397 category-up Sort alphabetically by category, A-Z.
1398 category-down Sort alphabetically by category, Z-A.
1399 tag-up Sort alphabetically by last tag, A-Z.
1400 tag-down Sort alphabetically by last tag, Z-A.
1401 priority-up Sort numerically by priority, high priority last.
1402 priority-down Sort numerically by priority, high priority first.
1403 todo-state-up Sort by todo state, tasks that are done last.
1404 todo-state-down Sort by todo state, tasks that are done first.
1405 effort-up Sort numerically by estimated effort, high effort last.
1406 effort-down Sort numerically by estimated effort, high effort first.
1407 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1408 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1409 habit-up Put entries that are habits first
1410 habit-down Put entries that are habits last
1411 alpha-up Sort headlines alphabetically
1412 alpha-down Sort headlines alphabetically, reversed
1414 The different possibilities will be tried in sequence, and testing stops
1415 if one comparison returns a \"not-equal\". For example, the default
1416 '(time-up category-keep priority-down)
1417 means: Pull out all entries having a specified time of day and sort them,
1418 in order to make a time schedule for the current day the first thing in the
1419 agenda listing for the day. Of the entries without a time indication, keep
1420 the grouped in categories, don't sort the categories, but keep them in
1421 the sequence given in `org-agenda-files'. Within each category sort by
1422 priority.
1424 Leaving out `category-keep' would mean that items will be sorted across
1425 categories by priority.
1427 Instead of a single list, this can also be a set of list for specific
1428 contents, with a context symbol in the car of the list, any of
1429 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1431 Custom commands can bind this variable in the options section."
1432 :group 'org-agenda-sorting
1433 :type `(choice
1434 (repeat :tag "General" ,org-sorting-choice)
1435 (list :tag "Individually"
1436 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1437 (repeat ,org-sorting-choice))
1438 (cons (const :tag "Strategy for TODO lists" todo)
1439 (repeat ,org-sorting-choice))
1440 (cons (const :tag "Strategy for Tags matches" tags)
1441 (repeat ,org-sorting-choice))
1442 (cons (const :tag "Strategy for search matches" search)
1443 (repeat ,org-sorting-choice)))))
1445 (defcustom org-agenda-cmp-user-defined nil
1446 "A function to define the comparison `user-defined'.
1447 This function must receive two arguments, agenda entry a and b.
1448 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1449 the user comparison, return nil.
1450 When this is defined, you can make `user-defined-up' and `user-defined-down'
1451 part of an agenda sorting strategy."
1452 :group 'org-agenda-sorting
1453 :type 'symbol)
1455 (defcustom org-sort-agenda-notime-is-late t
1456 "Non-nil means items without time are considered late.
1457 This is only relevant for sorting. When t, items which have no explicit
1458 time like 15:30 will be considered as 99:01, i.e. later than any items which
1459 do have a time. When nil, the default time is before 0:00. You can use this
1460 option to decide if the schedule for today should come before or after timeless
1461 agenda entries."
1462 :group 'org-agenda-sorting
1463 :type 'boolean)
1465 (defcustom org-sort-agenda-noeffort-is-high t
1466 "Non-nil means items without effort estimate are sorted as high effort.
1467 This also applies when filtering an agenda view with respect to the
1468 < or > effort operator. Then, tasks with no effort defined will be treated
1469 as tasks with high effort.
1470 When nil, such items are sorted as 0 minutes effort."
1471 :group 'org-agenda-sorting
1472 :type 'boolean)
1474 (defgroup org-agenda-line-format nil
1475 "Options concerning the entry prefix in the Org-mode agenda display."
1476 :tag "Org Agenda Line Format"
1477 :group 'org-agenda)
1479 (defcustom org-agenda-prefix-format
1480 '((agenda . " %i %-12:c%?-12t% s")
1481 (timeline . " % s")
1482 (todo . " %i %-12:c")
1483 (tags . " %i %-12:c")
1484 (search . " %i %-12:c"))
1485 "Format specifications for the prefix of items in the agenda views.
1486 An alist with five entries, each for the different agenda types. The
1487 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1488 The values are format strings.
1490 This format works similar to a printf format, with the following meaning:
1492 %c the category of the item, \"Diary\" for entries from the diary,
1493 or as given by the CATEGORY keyword or derived from the file name
1494 %e the effort required by the item
1495 %i the icon category of the item, see `org-agenda-category-icon-alist'
1496 %T the last tag of the item (ignore inherited tags, which come first)
1497 %t the HH:MM time-of-day specification if one applies to the entry
1498 %s Scheduling/Deadline information, a short string
1499 %(expression) Eval EXPRESSION and replace the control string
1500 by the result
1502 All specifiers work basically like the standard `%s' of printf, but may
1503 contain two additional characters: a question mark just after the `%'
1504 and a whitespace/punctuation character just before the final letter.
1506 If the first character after `%' is a question mark, the entire field
1507 will only be included if the corresponding value applies to the current
1508 entry. This is useful for fields which should have fixed width when
1509 present, but zero width when absent. For example, \"%?-12t\" will
1510 result in a 12 character time field if a time of the day is specified,
1511 but will completely disappear in entries which do not contain a time.
1513 If there is punctuation or whitespace character just before the final
1514 format letter, this character will be appended to the field value if
1515 the value is not empty. For example, the format \"%-12:c\" leads to
1516 \"Diary: \" if the category is \"Diary\". If the category were be
1517 empty, no additional colon would be inserted.
1519 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1520 which means:
1522 - Indent the line with two space characters
1523 - Give the category a 12 chars wide field, padded with whitespace on
1524 the right (because of `-'). Append a colon if there is a category
1525 (because of `:').
1526 - If there is a time-of-day, put it into a 12 chars wide field. If no
1527 time, don't put in an empty field, just skip it (because of '?').
1528 - Finally, put the scheduling information.
1530 See also the variables `org-agenda-remove-times-when-in-prefix' and
1531 `org-agenda-remove-tags'.
1533 Custom commands can set this variable in the options section."
1534 :type '(choice
1535 (string :tag "General format")
1536 (list :greedy t :tag "View dependent"
1537 (cons (const agenda) (string :tag "Format"))
1538 (cons (const timeline) (string :tag "Format"))
1539 (cons (const todo) (string :tag "Format"))
1540 (cons (const tags) (string :tag "Format"))
1541 (cons (const search) (string :tag "Format"))))
1542 :group 'org-agenda-line-format)
1544 (defvar org-prefix-format-compiled nil
1545 "The compiled prefix format and associated variables.
1546 This is a list where first element is a list of variable bindings, and second
1547 element is the compiled format expression. See the variable
1548 `org-agenda-prefix-format'.")
1550 (defcustom org-agenda-todo-keyword-format "%-1s"
1551 "Format for the TODO keyword in agenda lines.
1552 Set this to something like \"%-12s\" if you want all TODO keywords
1553 to occupy a fixed space in the agenda display."
1554 :group 'org-agenda-line-format
1555 :type 'string)
1557 (defcustom org-agenda-diary-sexp-prefix nil
1558 "A regexp that matches part of a diary sexp entry
1559 which should be treated as scheduling/deadline information in
1560 `org-agenda'.
1562 For example, you can use this to extract the `diary-remind-message' from
1563 `diary-remind' entries."
1564 :group 'org-agenda-line-format
1565 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1567 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1568 "Text preceding timerange entries in the agenda view.
1569 This is a list with two strings. The first applies when the range
1570 is entirely on one day. The second applies if the range spans several days.
1571 The strings may have two \"%d\" format specifiers which will be filled
1572 with the sequence number of the days, and the total number of days in the
1573 range, respectively."
1574 :group 'org-agenda-line-format
1575 :type '(list
1576 (string :tag "Deadline today ")
1577 (choice :tag "Deadline relative"
1578 (string :tag "Format string")
1579 (function))))
1581 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1582 "Text preceding scheduled items in the agenda view.
1583 This is a list with two strings. The first applies when the item is
1584 scheduled on the current day. The second applies when it has been scheduled
1585 previously, it may contain a %d indicating that this is the nth time that
1586 this item is scheduled, due to automatic rescheduling of unfinished items
1587 for the following day. So this number is one larger than the number of days
1588 that passed since this item was scheduled first."
1589 :group 'org-agenda-line-format
1590 :type '(list
1591 (string :tag "Scheduled today ")
1592 (string :tag "Scheduled previously")))
1594 (defcustom org-agenda-inactive-leader "["
1595 "Text preceding item pulled into the agenda by inactive time stamps.
1596 These entries are added to the agenda when pressing \"[\"."
1597 :group 'org-agenda-line-format
1598 :version "24.1"
1599 :type '(list
1600 (string :tag "Scheduled today ")
1601 (string :tag "Scheduled previously")))
1603 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1604 "Text preceding deadline items in the agenda view.
1605 This is a list with two strings. The first applies when the item has its
1606 deadline on the current day. The second applies when it is in the past or
1607 in the future, it may contain %d to capture how many days away the deadline
1608 is (was)."
1609 :group 'org-agenda-line-format
1610 :type '(list
1611 (string :tag "Deadline today ")
1612 (choice :tag "Deadline relative"
1613 (string :tag "Format string")
1614 (function))))
1616 (defcustom org-agenda-remove-times-when-in-prefix t
1617 "Non-nil means remove duplicate time specifications in agenda items.
1618 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1619 time-of-day specification in a headline or diary entry is extracted and
1620 placed into the prefix. If this option is non-nil, the original specification
1621 \(a timestamp or -range, or just a plain time(range) specification like
1622 11:30-4pm) will be removed for agenda display. This makes the agenda less
1623 cluttered.
1624 The option can be t or nil. It may also be the symbol `beg', indicating
1625 that the time should only be removed when it is located at the beginning of
1626 the headline/diary entry."
1627 :group 'org-agenda-line-format
1628 :type '(choice
1629 (const :tag "Always" t)
1630 (const :tag "Never" nil)
1631 (const :tag "When at beginning of entry" beg)))
1633 (defcustom org-agenda-remove-timeranges-from-blocks nil
1634 "Non-nil means remove time ranges specifications in agenda
1635 items that span on several days."
1636 :group 'org-agenda-line-format
1637 :version "24.1"
1638 :type 'boolean)
1640 (defcustom org-agenda-default-appointment-duration nil
1641 "Default duration for appointments that only have a starting time.
1642 When nil, no duration is specified in such cases.
1643 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1644 :group 'org-agenda-line-format
1645 :type '(choice
1646 (integer :tag "Minutes")
1647 (const :tag "No default duration")))
1649 (defcustom org-agenda-show-inherited-tags t
1650 "Non-nil means show inherited tags in each agenda line."
1651 :group 'org-agenda-line-format
1652 :type 'boolean)
1654 (defcustom org-agenda-hide-tags-regexp nil
1655 "Regular expression used to filter away specific tags in agenda views.
1656 This means that these tags will be present, but not be shown in the agenda
1657 line. Secondary filtering will still work on the hidden tags.
1658 Nil means don't hide any tags."
1659 :group 'org-agenda-line-format
1660 :type '(choice
1661 (const :tag "Hide none" nil)
1662 (string :tag "Regexp ")))
1664 (defcustom org-agenda-remove-tags nil
1665 "Non-nil means remove the tags from the headline copy in the agenda.
1666 When this is the symbol `prefix', only remove tags when
1667 `org-agenda-prefix-format' contains a `%T' specifier."
1668 :group 'org-agenda-line-format
1669 :type '(choice
1670 (const :tag "Always" t)
1671 (const :tag "Never" nil)
1672 (const :tag "When prefix format contains %T" prefix)))
1674 (if (fboundp 'defvaralias)
1675 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1676 'org-agenda-remove-tags))
1678 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1679 "Shift tags in agenda items to this column.
1680 If this number is positive, it specifies the column. If it is negative,
1681 it means that the tags should be flushright to that column. For example,
1682 -80 works well for a normal 80 character screen."
1683 :group 'org-agenda-line-format
1684 :type 'integer)
1686 (if (fboundp 'defvaralias)
1687 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1689 (defcustom org-agenda-fontify-priorities 'cookies
1690 "Non-nil means highlight low and high priorities in agenda.
1691 When t, the highest priority entries are bold, lowest priority italic.
1692 However, settings in `org-priority-faces' will overrule these faces.
1693 When this variable is the symbol `cookies', only fontify the
1694 cookies, not the entire task.
1695 This may also be an association list of priority faces, whose
1696 keys are the character values of `org-highest-priority',
1697 `org-default-priority', and `org-lowest-priority' (the default values
1698 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1699 color as a string, or a list like `(:background \"Red\")'.
1700 If it is a color, the variable `org-faces-easy-properties'
1701 determines if it is a foreground or a background color."
1702 :group 'org-agenda-line-format
1703 :type '(choice
1704 (const :tag "Never" nil)
1705 (const :tag "Defaults" t)
1706 (const :tag "Cookies only" cookies)
1707 (repeat :tag "Specify"
1708 (list (character :tag "Priority" :value ?A)
1709 (choice :tag "Face "
1710 (string :tag "Color")
1711 (sexp :tag "Face"))))))
1713 (defcustom org-agenda-day-face-function nil
1714 "Function called to determine what face should be used to display a day.
1715 The only argument passed to that function is the day. It should
1716 returns a face, or nil if does not want to specify a face and let
1717 the normal rules apply."
1718 :group 'org-agenda-line-format
1719 :version "24.1"
1720 :type 'function)
1722 (defcustom org-agenda-category-icon-alist nil
1723 "Alist of category icon to be displayed in agenda views.
1725 Each entry should have the following format:
1727 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1729 Where CATEGORY-REGEXP is a regexp matching the categories where
1730 the icon should be displayed.
1731 FILE-OR-DATA either a file path or a string containing image data.
1733 The other fields can be omitted safely if not needed:
1734 TYPE indicates the image type.
1735 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1736 image data.
1737 PROPS are additional image attributes to assign to the image,
1738 like, e.g. `:ascent center'.
1740 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1742 If you want to set the display properties yourself, just put a
1743 list as second element:
1745 (CATEGORY-REGEXP (MY PROPERTY LIST))
1747 For example, to display a 16px horizontal space for Emacs
1748 category, you can use:
1750 (\"Emacs\" '(space . (:width (16))))"
1751 :group 'org-agenda-line-format
1752 :version "24.1"
1753 :type '(alist :key-type (string :tag "Regexp matching category")
1754 :value-type (choice (list :tag "Icon"
1755 (string :tag "File or data")
1756 (symbol :tag "Type")
1757 (boolean :tag "Data?")
1758 (repeat :tag "Extra image properties" :inline t symbol))
1759 (list :tag "Display properties" sexp))))
1761 (defgroup org-agenda-column-view nil
1762 "Options concerning column view in the agenda."
1763 :tag "Org Agenda Column View"
1764 :group 'org-agenda)
1766 (defcustom org-agenda-columns-show-summaries t
1767 "Non-nil means show summaries for columns displayed in the agenda view."
1768 :group 'org-agenda-column-view
1769 :type 'boolean)
1771 (defcustom org-agenda-columns-compute-summary-properties t
1772 "Non-nil means recompute all summary properties before column view.
1773 When column view in the agenda is listing properties that have a summary
1774 operator, it can go to all relevant buffers and recompute the summaries
1775 there. This can mean overhead for the agenda column view, but is necessary
1776 to have thing up to date.
1777 As a special case, a CLOCKSUM property also makes sure that the clock
1778 computations are current."
1779 :group 'org-agenda-column-view
1780 :type 'boolean)
1782 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1783 "Non-nil means the duration of an appointment will add to day effort.
1784 The property to which appointment durations will be added is the one given
1785 in the option `org-effort-property'. If an appointment does not have
1786 an end time, `org-agenda-default-appointment-duration' will be used. If that
1787 is not set, an appointment without end time will not contribute to the time
1788 estimate."
1789 :group 'org-agenda-column-view
1790 :type 'boolean)
1792 (defcustom org-agenda-auto-exclude-function nil
1793 "A function called with a tag to decide if it is filtered on '/ RET'.
1794 The sole argument to the function, which is called once for each
1795 possible tag, is a string giving the name of the tag. The
1796 function should return either nil if the tag should be included
1797 as normal, or \"-<TAG>\" to exclude the tag.
1798 Note that for the purpose of tag filtering, only the lower-case version of
1799 all tags will be considered, so that this function will only ever see
1800 the lower-case version of all tags."
1801 :group 'org-agenda
1802 :type 'function)
1804 (defcustom org-agenda-bulk-custom-functions nil
1805 "Alist of characters and custom functions for bulk actions.
1806 For example, this value makes those two functions available:
1808 '((?R set-category)
1809 (?C bulk-cut))
1811 With selected entries in an agenda buffer, `B R' will call
1812 the custom function `set-category' on the selected entries.
1813 Note that functions in this alist don't need to be quoted."
1814 :type 'alist
1815 :version "24.1"
1816 :group 'org-agenda)
1818 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1819 "Execute BODY with point at location given by `org-hd-marker' property.
1820 If STRING is non-nil, the text property will be fetched from position 0
1821 in that string. If STRING is nil, it will be fetched from the beginning
1822 of the current line."
1823 (org-with-gensyms (marker)
1824 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
1825 'org-hd-marker ,string)))
1826 (with-current-buffer (marker-buffer ,marker)
1827 (save-excursion
1828 (goto-char ,marker)
1829 ,@body)))))
1830 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
1832 (defun org-add-agenda-custom-command (entry)
1833 "Replace or add a command in `org-agenda-custom-commands'.
1834 This is mostly for hacking and trying a new command - once the command
1835 works you probably want to add it to `org-agenda-custom-commands' for good."
1836 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1837 (if ass
1838 (setcdr ass (cdr entry))
1839 (push entry org-agenda-custom-commands))))
1841 ;;; Define the org-agenda-mode
1843 (defvar org-agenda-mode-map (make-sparse-keymap)
1844 "Keymap for `org-agenda-mode'.")
1845 (if (fboundp 'defvaralias)
1846 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1848 (defvar org-agenda-menu) ; defined later in this file.
1849 (defvar org-agenda-restrict nil) ; defined later in this file.
1850 (defvar org-agenda-follow-mode nil)
1851 (defvar org-agenda-entry-text-mode nil)
1852 (defvar org-agenda-clockreport-mode nil)
1853 (defvar org-agenda-show-log nil)
1854 (defvar org-agenda-redo-command nil)
1855 (defvar org-agenda-query-string nil)
1856 (defvar org-agenda-mode-hook nil
1857 "Hook run after `org-agenda-mode' is turned on.
1858 The buffer is still writable when this hook is called.")
1859 (defvar org-agenda-type nil)
1860 (defvar org-agenda-force-single-file nil)
1861 (defvar org-agenda-bulk-marked-entries nil
1862 "List of markers that refer to marked entries in the agenda.")
1864 ;;; Multiple agenda buffers support
1866 (defcustom org-agenda-sticky nil
1867 "Non-nil means agenda q key will bury agenda buffers.
1868 Agenda commands will then show existing buffer instead of generating new ones.
1869 When nil, `q' will kill the single agenda buffer."
1870 :group 'org-agenda
1871 ;; :version "24.3"
1872 :type 'boolean)
1874 ;;;###autoload
1875 (defun org-toggle-sticky-agenda (&optional arg)
1876 "Toggle `org-agenda-sticky'."
1877 (interactive "P")
1878 (let ((new-value (if arg
1879 (> (prefix-numeric-value arg) 0)
1880 (not org-agenda-sticky))))
1881 (if (equal new-value org-agenda-sticky)
1882 (and (org-called-interactively-p 'interactive)
1883 (message "Sticky agenda was already %s"
1884 (if org-agenda-sticky "enabled" "disabled")))
1885 (setq org-agenda-sticky new-value)
1886 (org-agenda-kill-all-agenda-buffers)
1887 (and (org-called-interactively-p 'interactive)
1888 (message "Sticky agenda was %s"
1889 (if org-agenda-sticky "enabled" "disabled"))))))
1891 (defvar org-agenda-buffer nil
1892 "Agenda buffer currently being generated.")
1894 (defvar org-agenda-last-prefix-arg nil)
1895 (defvar org-agenda-this-buffer-name nil)
1896 (defvar org-agenda-doing-sticky-redo nil)
1897 (defvar org-agenda-this-buffer-is-sticky nil)
1899 (defconst org-agenda-local-vars
1900 '(org-agenda-this-buffer-name
1901 org-agenda-undo-list
1902 org-agenda-pending-undo-list
1903 org-agenda-follow-mode
1904 org-agenda-entry-text-mode
1905 org-agenda-clockreport-mode
1906 org-agenda-show-log
1907 org-agenda-redo-command
1908 org-agenda-query-string
1909 org-agenda-type
1910 org-agenda-bulk-marked-entries
1911 org-agenda-undo-has-started-in
1912 org-agenda-info
1913 org-agenda-tag-filter-overlays
1914 org-agenda-cat-filter-overlays
1915 org-agenda-pre-window-conf
1916 org-agenda-columns-active
1917 org-agenda-tag-filter
1918 org-agenda-category-filter
1919 org-agenda-markers
1920 org-agenda-last-search-view-search-was-boolean
1921 org-agenda-filtered-by-category
1922 org-agenda-filter-form
1923 org-agenda-show-window
1924 org-agenda-cycle-counter
1925 org-agenda-last-prefix-arg)
1926 "Variables that must be local in agenda buffers to allow multiple buffers.")
1928 (defun org-agenda-mode ()
1929 "Mode for time-sorted view on action items in Org-mode files.
1931 The following commands are available:
1933 \\{org-agenda-mode-map}"
1934 (interactive)
1935 (cond (org-agenda-doing-sticky-redo
1936 ;; Refreshing sticky agenda-buffer
1938 ;; Preserve the value of `org-agenda-local-vars' variables,
1939 ;; while letting `kill-all-local-variables' kill the rest
1940 (let ((save (buffer-local-variables)))
1941 (kill-all-local-variables)
1942 (mapc 'make-local-variable org-agenda-local-vars)
1943 (dolist (elem save)
1944 (let ((var (car elem))
1945 (val (cdr elem)))
1946 (when (and val
1947 (member var org-agenda-local-vars))
1948 (set var val)))))
1949 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1950 (org-agenda-sticky
1951 ;; Creating a sticky Agenda buffer for the first time
1952 (kill-all-local-variables)
1953 (mapc 'make-local-variable org-agenda-local-vars)
1954 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
1956 ;; Creating a non-sticky agenda buffer
1957 (kill-all-local-variables)
1958 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
1959 (setq org-agenda-undo-list nil
1960 org-agenda-pending-undo-list nil
1961 org-agenda-bulk-marked-entries nil)
1962 (setq major-mode 'org-agenda-mode)
1963 ;; Keep global-font-lock-mode from turning on font-lock-mode
1964 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1965 (setq mode-name "Org-Agenda")
1966 (use-local-map org-agenda-mode-map)
1967 (easy-menu-add org-agenda-menu)
1968 (if org-startup-truncated (setq truncate-lines t))
1969 (org-set-local 'line-move-visual nil)
1970 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
1971 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1972 ;; Make sure properties are removed when copying text
1973 (make-local-variable 'filter-buffer-substring-functions)
1974 (add-hook 'filter-buffer-substring-functions
1975 (lambda (fun start end delete)
1976 (substring-no-properties (funcall fun start end delete))))
1977 (unless org-agenda-keep-modes
1978 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1979 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1980 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1981 org-agenda-show-log org-agenda-start-with-log-mode))
1983 (easy-menu-change
1984 '("Agenda") "Agenda Files"
1985 (append
1986 (list
1987 (vector
1988 (if (get 'org-agenda-files 'org-restrict)
1989 "Restricted to single file"
1990 "Edit File List")
1991 '(org-edit-agenda-file-list)
1992 (not (get 'org-agenda-files 'org-restrict)))
1993 "--")
1994 (mapcar 'org-file-menu-entry (org-agenda-files))))
1995 (org-agenda-set-mode-name)
1996 (apply
1997 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1998 (list 'org-agenda-mode-hook)))
2000 (substitute-key-definition 'undo 'org-agenda-undo
2001 org-agenda-mode-map global-map)
2002 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2003 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2004 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2005 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2006 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2007 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2008 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2009 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2010 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2011 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2012 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2013 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2014 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2015 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2016 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2017 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2018 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2019 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2020 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2021 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2022 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2023 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2024 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2025 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2026 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2027 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2028 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2029 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2030 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2031 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2032 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2033 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2034 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2035 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2036 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2037 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2038 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2039 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2040 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2041 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2042 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2043 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2044 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2045 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2046 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2048 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2049 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2050 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2051 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2052 (while l (org-defkey org-agenda-mode-map
2053 (int-to-string (pop l)) 'digit-argument)))
2055 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2056 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2057 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2058 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2059 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2060 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2061 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2062 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2063 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2064 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2065 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2066 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2067 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2068 'org-clock-modify-effort-estimate)
2069 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2070 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2071 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2072 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2073 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2074 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2075 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2076 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2077 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2078 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2079 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2080 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2081 (substitute-key-definition 'next-line 'org-agenda-next-line
2082 org-agenda-mode-map global-map)
2083 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2084 org-agenda-mode-map global-map)
2085 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2086 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2087 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2088 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2089 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2090 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2091 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2092 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2093 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2094 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2095 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2096 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2097 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2098 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2099 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2100 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2101 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2102 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2103 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2104 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2105 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2106 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2107 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2108 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2109 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2110 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2111 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2112 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2113 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2114 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2116 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2117 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2118 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2119 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2120 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2121 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2122 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2123 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-category)
2124 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2125 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2126 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2127 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2129 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2130 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2131 (when org-agenda-mouse-1-follows-link
2132 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2133 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2134 '("Agenda"
2135 ("Agenda Files")
2136 "--"
2137 ("Agenda Dates"
2138 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2139 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2140 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2141 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2142 "--"
2143 ("View"
2144 ["Day View" org-agenda-day-view
2145 :active (org-agenda-check-type nil 'agenda)
2146 :style radio :selected (eq org-agenda-current-span 'day)
2147 :keys "v d (or just d)"]
2148 ["Week View" org-agenda-week-view
2149 :active (org-agenda-check-type nil 'agenda)
2150 :style radio :selected (eq org-agenda-current-span 'week)
2151 :keys "v w (or just w)"]
2152 ["Month View" org-agenda-month-view
2153 :active (org-agenda-check-type nil 'agenda)
2154 :style radio :selected (eq org-agenda-current-span 'month)
2155 :keys "v m"]
2156 ["Year View" org-agenda-year-view
2157 :active (org-agenda-check-type nil 'agenda)
2158 :style radio :selected (eq org-agenda-current-span 'year)
2159 :keys "v y"]
2160 "--"
2161 ["Include Diary" org-agenda-toggle-diary
2162 :style toggle :selected org-agenda-include-diary
2163 :active (org-agenda-check-type nil 'agenda)]
2164 ["Include Deadlines" org-agenda-toggle-deadlines
2165 :style toggle :selected org-agenda-include-deadlines
2166 :active (org-agenda-check-type nil 'agenda)]
2167 ["Use Time Grid" org-agenda-toggle-time-grid
2168 :style toggle :selected org-agenda-use-time-grid
2169 :active (org-agenda-check-type nil 'agenda)]
2170 "--"
2171 ["Show clock report" org-agenda-clockreport-mode
2172 :style toggle :selected org-agenda-clockreport-mode
2173 :active (org-agenda-check-type nil 'agenda)]
2174 ["Show some entry text" org-agenda-entry-text-mode
2175 :style toggle :selected org-agenda-entry-text-mode
2176 :active t]
2177 "--"
2178 ["Show Logbook entries" org-agenda-log-mode
2179 :style toggle :selected org-agenda-show-log
2180 :active (org-agenda-check-type nil 'agenda 'timeline)
2181 :keys "v l (or just l)"]
2182 ["Include archived trees" org-agenda-archives-mode
2183 :style toggle :selected org-agenda-archives-mode :active t
2184 :keys "v a"]
2185 ["Include archive files" (org-agenda-archives-mode t)
2186 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2187 :keys "v A"]
2188 "--"
2189 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2190 ["Write view to file" org-agenda-write t]
2191 ["Rebuild buffer" org-agenda-redo t]
2192 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2193 "--"
2194 ["Show original entry" org-agenda-show t]
2195 ["Go To (other window)" org-agenda-goto t]
2196 ["Go To (this window)" org-agenda-switch-to t]
2197 ["Capture with cursor date" org-agenda-capture t]
2198 ["Follow Mode" org-agenda-follow-mode
2199 :style toggle :selected org-agenda-follow-mode :active t]
2200 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2201 "--"
2202 ("TODO"
2203 ["Cycle TODO" org-agenda-todo t]
2204 ["Next TODO set" org-agenda-todo-nextset t]
2205 ["Previous TODO set" org-agenda-todo-previousset t]
2206 ["Add note" org-agenda-add-note t])
2207 ("Archive/Refile/Delete"
2208 ["Archive default" org-agenda-archive-default t]
2209 ["Archive default" org-agenda-archive-default-with-confirmation t]
2210 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2211 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2212 ["Archive subtree" org-agenda-archive t]
2213 "--"
2214 ["Refile" org-agenda-refile t]
2215 "--"
2216 ["Delete subtree" org-agenda-kill t])
2217 ("Bulk action"
2218 ["Mark entry" org-agenda-bulk-mark t]
2219 ["Mark all" org-agenda-bulk-mark-all t]
2220 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2221 ["Unmark entry" org-agenda-bulk-unmark t]
2222 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "U"])
2223 ["Act on all marked" org-agenda-bulk-action t]
2224 "--"
2225 ("Tags and Properties"
2226 ["Show all Tags" org-agenda-show-tags t]
2227 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2228 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2229 "--"
2230 ["Column View" org-columns t])
2231 ("Deadline/Schedule"
2232 ["Schedule" org-agenda-schedule t]
2233 ["Set Deadline" org-agenda-deadline t]
2234 "--"
2235 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2236 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2237 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2238 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2239 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2240 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2241 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2242 ("Clock and Effort"
2243 ["Clock in" org-agenda-clock-in t]
2244 ["Clock out" org-agenda-clock-out t]
2245 ["Clock cancel" org-agenda-clock-cancel t]
2246 ["Goto running clock" org-clock-goto t]
2247 "--"
2248 ["Set Effort" org-agenda-set-effort t]
2249 ["Change clocked effort" org-clock-modify-effort-estimate
2250 (org-clock-is-active)])
2251 ("Priority"
2252 ["Set Priority" org-agenda-priority t]
2253 ["Increase Priority" org-agenda-priority-up t]
2254 ["Decrease Priority" org-agenda-priority-down t]
2255 ["Show Priority" org-show-priority t])
2256 ("Calendar/Diary"
2257 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2258 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2259 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2260 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2261 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2262 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2263 "--"
2264 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2265 "--"
2266 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2267 "--"
2268 ("MobileOrg"
2269 ["Push Files and Views" org-mobile-push t]
2270 ["Get Captured and Flagged" org-mobile-pull t]
2271 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2272 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2273 "--"
2274 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2275 "--"
2276 ["Quit" org-agenda-quit t]
2277 ["Exit and Release Buffers" org-agenda-exit t]
2280 ;;; Agenda undo
2282 (defvar org-agenda-allow-remote-undo t
2283 "Non-nil means allow remote undo from the agenda buffer.")
2284 (defvar org-agenda-undo-has-started-in nil
2285 "Buffers that have already seen `undo-start' in the current undo sequence.")
2287 (defun org-agenda-undo ()
2288 "Undo a remote editing step in the agenda.
2289 This undoes changes both in the agenda buffer and in the remote buffer
2290 that have been changed along."
2291 (interactive)
2292 (or org-agenda-allow-remote-undo
2293 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2294 (if (not (eq this-command last-command))
2295 (setq org-agenda-undo-has-started-in nil
2296 org-agenda-pending-undo-list org-agenda-undo-list))
2297 (if (not org-agenda-pending-undo-list)
2298 (error "No further undo information"))
2299 (let* ((entry (pop org-agenda-pending-undo-list))
2300 buf line cmd rembuf)
2301 (setq cmd (pop entry) line (pop entry))
2302 (setq rembuf (nth 2 entry))
2303 (org-with-remote-undo rembuf
2304 (while (bufferp (setq buf (pop entry)))
2305 (if (pop entry)
2306 (with-current-buffer buf
2307 (let ((last-undo-buffer buf)
2308 (inhibit-read-only t))
2309 (unless (memq buf org-agenda-undo-has-started-in)
2310 (push buf org-agenda-undo-has-started-in)
2311 (make-local-variable 'pending-undo-list)
2312 (undo-start))
2313 (while (and pending-undo-list
2314 (listp pending-undo-list)
2315 (not (car pending-undo-list)))
2316 (pop pending-undo-list))
2317 (undo-more 1))))))
2318 (org-goto-line line)
2319 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2321 (defun org-verify-change-for-undo (l1 l2)
2322 "Verify that a real change occurred between the undo lists L1 and L2."
2323 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2324 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2325 (not (eq l1 l2)))
2327 ;;; Agenda dispatch
2329 (defvar org-agenda-restrict-begin (make-marker))
2330 (defvar org-agenda-restrict-end (make-marker))
2331 (defvar org-agenda-last-dispatch-buffer nil)
2332 (defvar org-agenda-overriding-restriction nil)
2334 (defcustom org-agenda-custom-commands-contexts nil
2335 "Alist of custom agenda keys and contextual rules.
2337 For example, if you have a custom agenda command \"p\" and you
2338 want this command to be accessible only from plain text files,
2339 use this:
2341 '((\"p\" (in-file . \"\\.txt\")))
2343 Here are the available contexts definitions:
2345 in-file: command displayed only in matching files
2346 in-mode: command displayed only in matching modes
2347 not-in-file: command not displayed in matching files
2348 not-in-mode: command not displayed in matching modes
2349 [function]: a custom function taking no argument
2351 If you define several checks, the agenda command will be
2352 accessible if there is at least one valid check.
2354 You can also bind a key to another agenda custom command
2355 depending on contextual rules.
2357 '((\"p\" \"q\" (in-file . \"\\.txt\")))
2359 Here it means: in .txt files, use \"p\" as the key for the
2360 agenda command otherwise associated with \"q\". (The command
2361 originally associated with \"q\" is not displayed to avoid
2362 duplicates.)"
2363 ;; :version "24.3"
2364 :group 'org-agenda-custom-commands
2365 :type '(repeat (list :tag "Rule"
2366 (string :tag " Agenda key")
2367 (string :tag "Replace by command")
2368 (repeat :tag "Available when"
2369 (choice
2370 (cons :tag "Condition"
2371 (choice
2372 (const :tag "In file" in-file)
2373 (const :tag "Not in file" not-in-file)
2374 (const :tag "In mode" in-mode)
2375 (const :tag "Not in mode" not-in-mode))
2376 (regexp))
2377 (function :tag "Custom function"))))))
2379 (defvar org-keys nil)
2380 (defvar org-match nil)
2381 ;;;###autoload
2382 (defun org-agenda (&optional arg org-keys restriction)
2383 "Dispatch agenda commands to collect entries to the agenda buffer.
2384 Prompts for a command to execute. Any prefix arg will be passed
2385 on to the selected command. The default selections are:
2387 a Call `org-agenda-list' to display the agenda for current day or week.
2388 t Call `org-todo-list' to display the global todo list.
2389 T Call `org-todo-list' to display the global todo list, select only
2390 entries with a specific TODO keyword (the user gets a prompt).
2391 m Call `org-tags-view' to display headlines with tags matching
2392 a condition (the user is prompted for the condition).
2393 M Like `m', but select only TODO entries, no ordinary headlines.
2394 L Create a timeline for the current buffer.
2395 e Export views to associated files.
2396 s Search entries for keywords.
2397 S Search entries for keywords, only with TODO keywords.
2398 / Multi occur across all agenda files and also files listed
2399 in `org-agenda-text-search-extra-files'.
2400 < Restrict agenda commands to buffer, subtree, or region.
2401 Press several times to get the desired effect.
2402 > Remove a previous restriction.
2403 # List \"stuck\" projects.
2404 ! Configure what \"stuck\" means.
2405 C Configure custom agenda commands.
2407 More commands can be added by configuring the variable
2408 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2409 searches can be pre-defined in this way.
2411 If the current buffer is in Org-mode and visiting a file, you can also
2412 first press `<' once to indicate that the agenda should be temporarily
2413 \(until the next use of \\[org-agenda]) restricted to the current file.
2414 Pressing `<' twice means to restrict to the current subtree or region
2415 \(if active)."
2416 (interactive "P")
2417 (catch 'exit
2418 (let* ((prefix-descriptions nil)
2419 (org-agenda-buffer-name org-agenda-buffer-name)
2420 (org-agenda-window-setup (if (equal (buffer-name)
2421 org-agenda-buffer-name)
2422 'current-window
2423 org-agenda-window-setup))
2424 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2425 (org-agenda-custom-commands
2426 ;; normalize different versions
2427 (delq nil
2428 (mapcar
2429 (lambda (x)
2430 (cond ((stringp (cdr x))
2431 (push x prefix-descriptions)
2432 nil)
2433 ((stringp (nth 1 x)) x)
2434 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2435 (t (cons (car x) (cons "" (cdr x))))))
2436 org-agenda-custom-commands)))
2437 (org-agenda-custom-commands
2438 (org-contextualize-keys
2439 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2440 (buf (current-buffer))
2441 (bfn (buffer-file-name (buffer-base-buffer)))
2442 entry key type org-match lprops ans)
2443 ;; Turn off restriction unless there is an overriding one,
2444 (unless org-agenda-overriding-restriction
2445 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2446 ;; There is a request to keep the file list in place
2447 (put 'org-agenda-files 'org-restrict nil))
2448 (setq org-agenda-restrict nil)
2449 (move-marker org-agenda-restrict-begin nil)
2450 (move-marker org-agenda-restrict-end nil))
2451 ;; Delete old local properties
2452 (put 'org-agenda-redo-command 'org-lprops nil)
2453 ;; Delete previously set last-arguments
2454 (put 'org-agenda-redo-command 'last-args nil)
2455 ;; Remember where this call originated
2456 (setq org-agenda-last-dispatch-buffer (current-buffer))
2457 (unless org-keys
2458 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2459 org-keys (car ans)
2460 restriction (cdr ans)))
2461 ;; If we have sticky agenda buffers, set a name for the buffer,
2462 ;; depending on the invoking keys. The user may still set this
2463 ;; as a command option, which will overwrite what we do here.
2464 (if org-agenda-sticky
2465 (setq org-agenda-buffer-name
2466 (format "*Org Agenda(%s)*" org-keys)))
2467 ;; Establish the restriction, if any
2468 (when (and (not org-agenda-overriding-restriction) restriction)
2469 (put 'org-agenda-files 'org-restrict (list bfn))
2470 (cond
2471 ((eq restriction 'region)
2472 (setq org-agenda-restrict t)
2473 (move-marker org-agenda-restrict-begin (region-beginning))
2474 (move-marker org-agenda-restrict-end (region-end)))
2475 ((eq restriction 'subtree)
2476 (save-excursion
2477 (setq org-agenda-restrict t)
2478 (org-back-to-heading t)
2479 (move-marker org-agenda-restrict-begin (point))
2480 (move-marker org-agenda-restrict-end
2481 (progn (org-end-of-subtree t)))))))
2483 ;; For example the todo list should not need it (but does...)
2484 (cond
2485 ((setq entry (assoc org-keys org-agenda-custom-commands))
2486 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2487 (progn
2488 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2489 lprops (nth 4 entry))
2490 (if org-agenda-sticky
2491 (setq org-agenda-buffer-name
2492 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2493 (format "*Org Agenda(%s)*" org-keys))))
2494 (put 'org-agenda-redo-command 'org-lprops lprops)
2495 (cond
2496 ((eq type 'agenda)
2497 (org-let lprops '(org-agenda-list current-prefix-arg)))
2498 ((eq type 'alltodo)
2499 (org-let lprops '(org-todo-list current-prefix-arg)))
2500 ((eq type 'search)
2501 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2502 ((eq type 'stuck)
2503 (org-let lprops '(org-agenda-list-stuck-projects
2504 current-prefix-arg)))
2505 ((eq type 'tags)
2506 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2507 ((eq type 'tags-todo)
2508 (org-let lprops '(org-tags-view '(4) org-match)))
2509 ((eq type 'todo)
2510 (org-let lprops '(org-todo-list org-match)))
2511 ((eq type 'tags-tree)
2512 (org-check-for-org-mode)
2513 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2514 ((eq type 'todo-tree)
2515 (org-check-for-org-mode)
2516 (org-let lprops
2517 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2518 (regexp-quote org-match) "\\>"))))
2519 ((eq type 'occur-tree)
2520 (org-check-for-org-mode)
2521 (org-let lprops '(org-occur org-match)))
2522 ((functionp type)
2523 (org-let lprops '(funcall type org-match)))
2524 ((fboundp type)
2525 (org-let lprops '(funcall type org-match)))
2526 (t (error "Invalid custom agenda command type %s" type))))
2527 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2528 ((equal org-keys "C")
2529 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2530 (customize-variable 'org-agenda-custom-commands))
2531 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2532 ((equal org-keys "s") (call-interactively 'org-search-view))
2533 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2534 ((equal org-keys "t") (call-interactively 'org-todo-list))
2535 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2536 ((equal org-keys "m") (call-interactively 'org-tags-view))
2537 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2538 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2539 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2540 (org-add-hook
2541 'post-command-hook
2542 (lambda ()
2543 (unless (current-message)
2544 (let* ((m (org-agenda-get-any-marker))
2545 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2546 (when note
2547 (message (concat
2548 "FLAGGING-NOTE ([?] for more info): "
2549 (org-add-props
2550 (replace-regexp-in-string
2551 "\\\\n" "//"
2552 (copy-sequence note))
2553 nil 'face 'org-warning)))))))
2554 t t))
2555 ((equal org-keys "L")
2556 (unless (derived-mode-p 'org-mode)
2557 (error "This is not an Org-mode file"))
2558 (unless restriction
2559 (put 'org-agenda-files 'org-restrict (list bfn))
2560 (org-call-with-arg 'org-timeline arg)))
2561 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2562 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2563 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2564 (t (error "Invalid agenda key"))))))
2566 (defun org-agenda-append-agenda ()
2567 "Append another agenda view to the current one.
2568 This function allows interactive building of block agendas.
2569 Agenda views are separated by `org-agenda-block-separator'."
2570 (interactive)
2571 (unless (derived-mode-p 'org-agenda-mode)
2572 (error "Can only append from within agenda buffer"))
2573 (let ((org-agenda-multi t))
2574 (org-agenda)
2575 (widen)
2576 (org-agenda-finalize)
2577 (org-agenda-fit-window-to-buffer)))
2579 (defun org-agenda-normalize-custom-commands (cmds)
2580 (delq nil
2581 (mapcar
2582 (lambda (x)
2583 (cond ((stringp (cdr x)) nil)
2584 ((stringp (nth 1 x)) x)
2585 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2586 (t (cons (car x) (cons "" (cdr x))))))
2587 cmds)))
2589 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2590 "The user interface for selecting an agenda command."
2591 (catch 'exit
2592 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2593 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2594 (region-p (org-region-active-p))
2595 (custom org-agenda-custom-commands)
2596 (selstring "")
2597 restriction second-time
2598 c entry key type match prefixes rmheader header-end custom1 desc
2599 line lines left right n n1)
2600 (save-window-excursion
2601 (delete-other-windows)
2602 (org-switch-to-buffer-other-window " *Agenda Commands*")
2603 (erase-buffer)
2604 (insert (eval-when-compile
2605 (let ((header
2606 "Press key for an agenda command: < Buffer, subtree/region restriction
2607 -------------------------------- > Remove restriction
2608 a Agenda for current week or day e Export agenda views
2609 t List of all TODO entries T Entries with special TODO kwd
2610 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2611 s Search for keywords S Like s, but only TODO entries
2612 L Timeline for current buffer # List stuck projects (!=configure)
2613 / Multi-occur C Configure custom agenda commands
2614 ? Find :FLAGGED: entries * Toggle sticky agenda views
2616 (start 0))
2617 (while (string-match
2618 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2619 header start)
2620 (setq start (match-end 0))
2621 (add-text-properties (match-beginning 2) (match-end 2)
2622 '(face bold) header))
2623 header)))
2624 (setq header-end (move-marker (make-marker) (point)))
2625 (while t
2626 (setq custom1 custom)
2627 (when (eq rmheader t)
2628 (org-goto-line 1)
2629 (re-search-forward ":" nil t)
2630 (delete-region (match-end 0) (point-at-eol))
2631 (forward-char 1)
2632 (looking-at "-+")
2633 (delete-region (match-end 0) (point-at-eol))
2634 (move-marker header-end (match-end 0)))
2635 (goto-char header-end)
2636 (delete-region (point) (point-max))
2638 ;; Produce all the lines that describe custom commands and prefixes
2639 (setq lines nil)
2640 (while (setq entry (pop custom1))
2641 (setq key (car entry) desc (nth 1 entry)
2642 type (nth 2 entry)
2643 match (nth 3 entry))
2644 (if (> (length key) 1)
2645 (add-to-list 'prefixes (string-to-char key))
2646 (setq line
2647 (format
2648 "%-4s%-14s"
2649 (org-add-props (copy-sequence key)
2650 '(face bold))
2651 (cond
2652 ((string-match "\\S-" desc) desc)
2653 ((eq type 'agenda) "Agenda for current week or day")
2654 ((eq type 'alltodo) "List of all TODO entries")
2655 ((eq type 'search) "Word search")
2656 ((eq type 'stuck) "List of stuck projects")
2657 ((eq type 'todo) "TODO keyword")
2658 ((eq type 'tags) "Tags query")
2659 ((eq type 'tags-todo) "Tags (TODO)")
2660 ((eq type 'tags-tree) "Tags tree")
2661 ((eq type 'todo-tree) "TODO kwd tree")
2662 ((eq type 'occur-tree) "Occur tree")
2663 ((functionp type) (if (symbolp type)
2664 (symbol-name type)
2665 "Lambda expression"))
2666 (t "???"))))
2667 (if org-agenda-menu-show-matcher
2668 (setq line
2669 (concat line ": "
2670 (cond
2671 ((stringp match)
2672 (setq match (copy-sequence match))
2673 (org-add-props match nil 'face 'org-warning))
2674 ((listp type)
2675 (format "set of %d commands" (length type))))))
2676 (if (org-string-nw-p match)
2677 (add-text-properties
2678 0 (length line) (list 'help-echo
2679 (concat "Matcher: " match)) line)))
2680 (push line lines)))
2681 (setq lines (nreverse lines))
2682 (when prefixes
2683 (mapc (lambda (x)
2684 (push
2685 (format "%s %s"
2686 (org-add-props (char-to-string x)
2687 nil 'face 'bold)
2688 (or (cdr (assoc (concat selstring
2689 (char-to-string x))
2690 prefix-descriptions))
2691 "Prefix key"))
2692 lines))
2693 prefixes))
2695 ;; Check if we should display in two columns
2696 (if org-agenda-menu-two-columns
2697 (progn
2698 (setq n (length lines)
2699 n1 (+ (/ n 2) (mod n 2))
2700 right (nthcdr n1 lines)
2701 left (copy-sequence lines))
2702 (setcdr (nthcdr (1- n1) left) nil))
2703 (setq left lines right nil))
2704 (while left
2705 (insert "\n" (pop left))
2706 (when right
2707 (if (< (current-column) 40)
2708 (move-to-column 40 t)
2709 (insert " "))
2710 (insert (pop right))))
2712 ;; Make the window the right size
2713 (goto-char (point-min))
2714 (if second-time
2715 (if (not (pos-visible-in-window-p (point-max)))
2716 (org-fit-window-to-buffer))
2717 (setq second-time t)
2718 (org-fit-window-to-buffer))
2720 ;; Ask for selection
2721 (message "Press key for agenda command%s:"
2722 (if (or restrict-ok org-agenda-overriding-restriction)
2723 (if org-agenda-overriding-restriction
2724 " (restriction lock active)"
2725 (if restriction
2726 (format " (restricted to %s)" restriction)
2727 " (unrestricted)"))
2728 ""))
2729 (setq c (read-char-exclusive))
2730 (message "")
2731 (cond
2732 ((assoc (char-to-string c) custom)
2733 (setq selstring (concat selstring (char-to-string c)))
2734 (throw 'exit (cons selstring restriction)))
2735 ((memq c prefixes)
2736 (setq selstring (concat selstring (char-to-string c))
2737 prefixes nil
2738 rmheader (or rmheader t)
2739 custom (delq nil (mapcar
2740 (lambda (x)
2741 (if (or (= (length (car x)) 1)
2742 (/= (string-to-char (car x)) c))
2744 (cons (substring (car x) 1) (cdr x))))
2745 custom))))
2746 ((eq c ?*)
2747 (call-interactively 'org-toggle-sticky-agenda)
2748 (sit-for 2))
2749 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2750 (message "Restriction is only possible in Org-mode buffers")
2751 (ding) (sit-for 1))
2752 ((eq c ?1)
2753 (org-agenda-remove-restriction-lock 'noupdate)
2754 (setq restriction 'buffer))
2755 ((eq c ?0)
2756 (org-agenda-remove-restriction-lock 'noupdate)
2757 (setq restriction (if region-p 'region 'subtree)))
2758 ((eq c ?<)
2759 (org-agenda-remove-restriction-lock 'noupdate)
2760 (setq restriction
2761 (cond
2762 ((eq restriction 'buffer)
2763 (if region-p 'region 'subtree))
2764 ((memq restriction '(subtree region))
2765 nil)
2766 (t 'buffer))))
2767 ((eq c ?>)
2768 (org-agenda-remove-restriction-lock 'noupdate)
2769 (setq restriction nil))
2770 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2771 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2772 ((and (> (length selstring) 0) (eq c ?\d))
2773 (delete-window)
2774 (org-agenda-get-restriction-and-command prefix-descriptions))
2776 ((equal c ?q) (error "Abort"))
2777 (t (error "Invalid key %c" c))))))))
2779 (defun org-agenda-fit-window-to-buffer ()
2780 "Fit the window to the buffer size."
2781 (and (memq org-agenda-window-setup '(reorganize-frame))
2782 (fboundp 'fit-window-to-buffer)
2783 (org-fit-window-to-buffer
2785 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2786 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2788 (defvar org-cmd nil)
2789 (defvar org-agenda-overriding-cmd nil)
2790 (defvar org-agenda-overriding-arguments nil)
2791 (defvar org-agenda-overriding-cmd-arguments nil)
2792 (defun org-agenda-run-series (name series)
2793 (org-let (nth 1 series) '(org-agenda-prepare name))
2794 ;; We need to reset agenda markers here, because when constructing a
2795 ;; block agenda, the individual blocks do not do that.
2796 (org-agenda-reset-markers)
2797 (let* ((org-agenda-multi t)
2798 (redo (list 'org-agenda-run-series name (list 'quote series)))
2799 (cmds (car series))
2800 (gprops (nth 1 series))
2801 match ;; The byte compiler incorrectly complains about this. Keep it!
2802 org-cmd type lprops)
2803 (while (setq org-cmd (pop cmds))
2804 (setq type (car org-cmd)
2805 match (eval (nth 1 org-cmd))
2806 lprops (nth 2 org-cmd))
2807 (let ((org-agenda-overriding-arguments
2808 (if (eq org-agenda-overriding-cmd org-cmd)
2809 (or org-agenda-overriding-arguments
2810 org-agenda-overriding-cmd-arguments))))
2811 (cond
2812 ((eq type 'agenda)
2813 (org-let2 gprops lprops
2814 '(call-interactively 'org-agenda-list)))
2815 ((eq type 'alltodo)
2816 (org-let2 gprops lprops
2817 '(call-interactively 'org-todo-list)))
2818 ((eq type 'search)
2819 (org-let2 gprops lprops
2820 '(org-search-view current-prefix-arg match nil)))
2821 ((eq type 'stuck)
2822 (org-let2 gprops lprops
2823 '(call-interactively 'org-agenda-list-stuck-projects)))
2824 ((eq type 'tags)
2825 (org-let2 gprops lprops
2826 '(org-tags-view current-prefix-arg match)))
2827 ((eq type 'tags-todo)
2828 (org-let2 gprops lprops
2829 '(org-tags-view '(4) match)))
2830 ((eq type 'todo)
2831 (org-let2 gprops lprops
2832 '(org-todo-list match)))
2833 ((fboundp type)
2834 (org-let2 gprops lprops
2835 '(funcall type match)))
2836 (t (error "Invalid type in command series")))))
2837 (widen)
2838 (let ((inhibit-read-only t))
2839 (add-text-properties (point-min) (point-max)
2840 `(org-serie t org-serie-redo-cmd ,redo)))
2841 (setq org-agenda-redo-command redo)
2842 (goto-char (point-min)))
2843 (org-agenda-fit-window-to-buffer)
2844 (org-let (nth 1 series) '(org-agenda-finalize)))
2846 ;;;###autoload
2847 (defmacro org-batch-agenda (cmd-key &rest parameters)
2848 "Run an agenda command in batch mode and send the result to STDOUT.
2849 If CMD-KEY is a string of length 1, it is used as a key in
2850 `org-agenda-custom-commands' and triggers this command. If it is a
2851 longer string it is used as a tags/todo match string.
2852 Parameters are alternating variable names and values that will be bound
2853 before running the agenda command."
2854 (org-eval-in-environment (org-make-parameter-alist parameters)
2855 (if (> (length cmd-key) 2)
2856 (org-tags-view nil cmd-key)
2857 (org-agenda nil cmd-key)))
2858 (set-buffer org-agenda-buffer-name)
2859 (princ (buffer-string)))
2860 (def-edebug-spec org-batch-agenda (form &rest sexp))
2862 (defvar org-agenda-info nil)
2864 ;;;###autoload
2865 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2866 "Run an agenda command in batch mode and send the result to STDOUT.
2867 If CMD-KEY is a string of length 1, it is used as a key in
2868 `org-agenda-custom-commands' and triggers this command. If it is a
2869 longer string it is used as a tags/todo match string.
2870 Parameters are alternating variable names and values that will be bound
2871 before running the agenda command.
2873 The output gives a line for each selected agenda item. Each
2874 item is a list of comma-separated values, like this:
2876 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2878 category The category of the item
2879 head The headline, without TODO kwd, TAGS and PRIORITY
2880 type The type of the agenda entry, can be
2881 todo selected in TODO match
2882 tagsmatch selected in tags match
2883 diary imported from diary
2884 deadline a deadline on given date
2885 scheduled scheduled on given date
2886 timestamp entry has timestamp on given date
2887 closed entry was closed on given date
2888 upcoming-deadline warning about deadline
2889 past-scheduled forwarded scheduled item
2890 block entry has date block including g. date
2891 todo The todo keyword, if any
2892 tags All tags including inherited ones, separated by colons
2893 date The relevant date, like 2007-2-14
2894 time The time, like 15:00-16:50
2895 extra Sting with extra planning info
2896 priority-l The priority letter if any was given
2897 priority-n The computed numerical priority
2898 agenda-day The day in the agenda where this is listed"
2899 (org-eval-in-environment (append '((org-agenda-remove-tags t))
2900 (org-make-parameter-alist parameters))
2901 (if (> (length cmd-key) 2)
2902 (org-tags-view nil cmd-key)
2903 (org-agenda nil cmd-key)))
2904 (set-buffer org-agenda-buffer-name)
2905 (let* ((lines (org-split-string (buffer-string) "\n"))
2906 line)
2907 (while (setq line (pop lines))
2908 (catch 'next
2909 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2910 (setq org-agenda-info
2911 (org-fix-agenda-info (text-properties-at 0 line)))
2912 (princ
2913 (mapconcat 'org-agenda-export-csv-mapper
2914 '(org-category txt type todo tags date time extra
2915 priority-letter priority agenda-day)
2916 ","))
2917 (princ "\n")))))
2918 (def-edebug-spec org-batch-agenda-csv (form &rest sexp))
2920 (defun org-fix-agenda-info (props)
2921 "Make sure all properties on an agenda item have a canonical form.
2922 This ensures the export commands can easily use it."
2923 (let (tmp re)
2924 (when (setq tmp (plist-get props 'tags))
2925 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2926 (when (setq tmp (plist-get props 'date))
2927 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2928 (let ((calendar-date-display-form '(year "-" month "-" day)))
2929 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2931 (setq tmp (calendar-date-string tmp)))
2932 (setq props (plist-put props 'date tmp)))
2933 (when (setq tmp (plist-get props 'day))
2934 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2935 (let ((calendar-date-display-form '(year "-" month "-" day)))
2936 (setq tmp (calendar-date-string tmp)))
2937 (setq props (plist-put props 'day tmp))
2938 (setq props (plist-put props 'agenda-day tmp)))
2939 (when (setq tmp (plist-get props 'txt))
2940 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2941 (plist-put props 'priority-letter (match-string 1 tmp))
2942 (setq tmp (replace-match "" t t tmp)))
2943 (when (and (setq re (plist-get props 'org-todo-regexp))
2944 (setq re (concat "\\`\\.*" re " ?"))
2945 (string-match re tmp))
2946 (plist-put props 'todo (match-string 1 tmp))
2947 (setq tmp (replace-match "" t t tmp)))
2948 (plist-put props 'txt tmp)))
2949 props)
2951 (defun org-agenda-export-csv-mapper (prop)
2952 (let ((res (plist-get org-agenda-info prop)))
2953 (setq res
2954 (cond
2955 ((not res) "")
2956 ((stringp res) res)
2957 (t (prin1-to-string res))))
2958 (while (string-match "," res)
2959 (setq res (replace-match ";" t t res)))
2960 (org-trim res)))
2962 ;;;###autoload
2963 (defun org-store-agenda-views (&rest parameters)
2964 (interactive)
2965 (eval (list 'org-batch-store-agenda-views)))
2967 ;;;###autoload
2968 (defmacro org-batch-store-agenda-views (&rest parameters)
2969 "Run all custom agenda commands that have a file argument."
2970 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2971 (pop-up-frames nil)
2972 (dir default-directory)
2973 (pars (org-make-parameter-alist parameters))
2974 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
2975 (save-window-excursion
2976 (while cmds
2977 (setq cmd (pop cmds)
2978 thiscmdkey (car cmd)
2979 thiscmdcmd (cdr cmd)
2980 match (nth 2 thiscmdcmd)
2981 bufname (if org-agenda-sticky
2982 (or (and (stringp match)
2983 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
2984 (format "*Org Agenda(%s)*" thiscmdkey))
2985 org-agenda-buffer-name)
2986 cmd-or-set (nth 2 cmd)
2987 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2988 files (nth (if (listp cmd-or-set) 4 5) cmd))
2989 (if (stringp files) (setq files (list files)))
2990 (when files
2991 (org-eval-in-environment (append org-agenda-exporter-settings
2992 opts pars)
2993 (org-agenda nil thiscmdkey))
2994 (set-buffer bufname)
2995 (while files
2996 (org-eval-in-environment (append org-agenda-exporter-settings
2997 opts pars)
2998 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
2999 (and (get-buffer bufname)
3000 (kill-buffer bufname)))))))
3001 (def-edebug-spec org-batch-store-agenda-views (&rest sexp))
3003 (defvar org-agenda-current-span nil
3004 "The current span used in the agenda view.") ; local variable in the agenda buffer
3005 (defun org-agenda-mark-header-line (pos)
3006 "Mark the line at POS as an agenda structure header."
3007 (save-excursion
3008 (goto-char pos)
3009 (put-text-property (point-at-bol) (point-at-eol)
3010 'org-agenda-structural-header t)
3011 (when org-agenda-title-append
3012 (put-text-property (point-at-bol) (point-at-eol)
3013 'org-agenda-title-append org-agenda-title-append))))
3015 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3016 (defvar org-agenda-write-buffer-name "Agenda View")
3017 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3018 "Write the current buffer (an agenda view) as a file.
3019 Depending on the extension of the file name, plain text (.txt),
3020 HTML (.html or .htm) or Postscript (.ps) is produced.
3021 If the extension is .ics, run icalendar export over all files used
3022 to construct the agenda and limit the export to entries listed in the
3023 agenda now.
3024 With prefix argument OPEN, open the new file immediately.
3025 If NOSETTINGS is given, do not scope the settings of
3026 `org-agenda-exporter-settings' into the export commands. This is used when
3027 the settings have already been scoped and we do not wish to overrule other,
3028 higher priority settings.
3029 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3030 (interactive "FWrite agenda to file: \nP")
3031 (if (not (file-writable-p file))
3032 (error "Cannot write agenda to file %s" file))
3033 (org-let (if nosettings nil org-agenda-exporter-settings)
3034 '(save-excursion
3035 (save-window-excursion
3036 (org-agenda-mark-filtered-text)
3037 (let ((bs (copy-sequence (buffer-string))) beg)
3038 (org-agenda-unmark-filtered-text)
3039 (with-temp-buffer
3040 (rename-buffer org-agenda-write-buffer-name t)
3041 (set-buffer-modified-p nil)
3042 (insert bs)
3043 (org-agenda-remove-marked-text 'org-filtered)
3044 (while (setq beg (text-property-any (point-min) (point-max)
3045 'org-filtered t))
3046 (delete-region
3047 beg (or (next-single-property-change beg 'org-filtered)
3048 (point-max))))
3049 (run-hooks 'org-agenda-before-write-hook)
3050 (cond
3051 ((org-bound-and-true-p org-mobile-creating-agendas)
3052 (org-mobile-write-agenda-for-mobile file))
3053 ((string-match "\\.html?\\'" file)
3054 (require 'htmlize)
3055 (set-buffer (htmlize-buffer (current-buffer)))
3056 (when org-agenda-export-html-style
3057 ;; replace <style> section with org-agenda-export-html-style
3058 (goto-char (point-min))
3059 (kill-region (- (search-forward "<style") 6)
3060 (search-forward "</style>"))
3061 (insert org-agenda-export-html-style))
3062 (write-file file)
3063 (kill-buffer (current-buffer))
3064 (message "HTML written to %s" file))
3065 ((string-match "\\.ps\\'" file)
3066 (require 'ps-print)
3067 (ps-print-buffer-with-faces file)
3068 (message "Postscript written to %s" file))
3069 ((string-match "\\.pdf\\'" file)
3070 (require 'ps-print)
3071 (ps-print-buffer-with-faces
3072 (concat (file-name-sans-extension file) ".ps"))
3073 (call-process "ps2pdf" nil nil nil
3074 (expand-file-name
3075 (concat (file-name-sans-extension file) ".ps"))
3076 (expand-file-name file))
3077 (delete-file (concat (file-name-sans-extension file) ".ps"))
3078 (message "PDF written to %s" file))
3079 ((string-match "\\.ics\\'" file)
3080 (require 'org-icalendar)
3081 (let ((org-agenda-marker-table
3082 (org-create-marker-find-array
3083 (org-agenda-collect-markers)))
3084 (org-icalendar-verify-function 'org-check-agenda-marker-table)
3085 (org-combined-agenda-icalendar-file file))
3086 (apply 'org-export-icalendar 'combine
3087 (org-agenda-files nil 'ifmode))))
3089 (let ((bs (buffer-string)))
3090 (find-file file)
3091 (erase-buffer)
3092 (insert bs)
3093 (save-buffer 0)
3094 (kill-buffer (current-buffer))
3095 (message "Plain text written to %s" file))))))))
3096 (set-buffer (or agenda-bufname
3097 (and (called-interactively-p 'any) (buffer-name))
3098 org-agenda-buffer-name)))
3099 (when open (org-open-file file)))
3101 (defvar org-agenda-tag-filter-overlays nil)
3102 (defvar org-agenda-cat-filter-overlays nil)
3104 (defun org-agenda-mark-filtered-text ()
3105 "Mark all text hidden by filtering with a text property."
3106 (let ((inhibit-read-only t))
3107 (mapc
3108 (lambda (o)
3109 (when (equal (overlay-buffer o) (current-buffer))
3110 (put-text-property
3111 (overlay-start o) (overlay-end o)
3112 'org-filtered t)))
3113 (append org-agenda-tag-filter-overlays
3114 org-agenda-cat-filter-overlays))))
3116 (defun org-agenda-unmark-filtered-text ()
3117 "Remove the filtering text property."
3118 (let ((inhibit-read-only t))
3119 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3121 (defun org-agenda-remove-marked-text (property &optional value)
3122 "Delete all text marked with VALUE of PROPERTY.
3123 VALUE defaults to t."
3124 (let (beg)
3125 (setq value (or value t))
3126 (while (setq beg (text-property-any (point-min) (point-max)
3127 property value))
3128 (delete-region
3129 beg (or (next-single-property-change beg 'org-filtered)
3130 (point-max))))))
3132 (defun org-agenda-add-entry-text ()
3133 "Add entry text to agenda lines.
3134 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3135 entry text following headings shown in the agenda.
3136 Drawers will be excluded, also the line with scheduling/deadline info."
3137 (when (and (> org-agenda-add-entry-text-maxlines 0)
3138 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3139 (let (m txt)
3140 (goto-char (point-min))
3141 (while (not (eobp))
3142 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3143 (beginning-of-line 2)
3144 (setq txt (org-agenda-get-some-entry-text
3145 m org-agenda-add-entry-text-maxlines " > "))
3146 (end-of-line 1)
3147 (if (string-match "\\S-" txt)
3148 (insert "\n" txt)
3149 (or (eobp) (forward-char 1))))))))
3151 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3152 &rest keep)
3153 "Extract entry text from MARKER, at most N-LINES lines.
3154 This will ignore drawers etc, just get the text.
3155 If INDENT is given, prefix every line with this string. If KEEP is
3156 given, it is a list of symbols, defining stuff that should not be
3157 removed from the entry content. Currently only `planning' is allowed here."
3158 (let (txt drawer-re kwd-time-re ind)
3159 (save-excursion
3160 (with-current-buffer (marker-buffer marker)
3161 (if (not (derived-mode-p 'org-mode))
3162 (setq txt "")
3163 (save-excursion
3164 (save-restriction
3165 (widen)
3166 (goto-char marker)
3167 (end-of-line 1)
3168 (setq txt (buffer-substring
3169 (min (1+ (point)) (point-max))
3170 (progn (outline-next-heading) (point)))
3171 drawer-re org-drawer-regexp
3172 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3173 ".*\n?"))
3174 (with-temp-buffer
3175 (insert txt)
3176 (when org-agenda-add-entry-text-descriptive-links
3177 (goto-char (point-min))
3178 (while (org-activate-bracket-links (point-max))
3179 (add-text-properties (match-beginning 0) (match-end 0)
3180 '(face org-link))))
3181 (goto-char (point-min))
3182 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3183 (set-text-properties (match-beginning 0) (match-end 0)
3184 nil))
3185 (goto-char (point-min))
3186 (while (re-search-forward drawer-re nil t)
3187 (delete-region
3188 (match-beginning 0)
3189 (progn (re-search-forward
3190 "^[ \t]*:END:.*\n?" nil 'move)
3191 (point))))
3192 (unless (member 'planning keep)
3193 (goto-char (point-min))
3194 (while (re-search-forward kwd-time-re nil t)
3195 (replace-match "")))
3196 (goto-char (point-min))
3197 (when org-agenda-entry-text-exclude-regexps
3198 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3199 (while (setq re (pop re-list))
3200 (goto-char (point-min))
3201 (while (re-search-forward re nil t)
3202 (replace-match "")))))
3203 (goto-char (point-max))
3204 (skip-chars-backward " \t\n")
3205 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3207 ;; find and remove min common indentation
3208 (goto-char (point-min))
3209 (untabify (point-min) (point-max))
3210 (setq ind (org-get-indentation))
3211 (while (not (eobp))
3212 (unless (looking-at "[ \t]*$")
3213 (setq ind (min ind (org-get-indentation))))
3214 (beginning-of-line 2))
3215 (goto-char (point-min))
3216 (while (not (eobp))
3217 (unless (looking-at "[ \t]*$")
3218 (move-to-column ind)
3219 (delete-region (point-at-bol) (point)))
3220 (beginning-of-line 2))
3222 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3224 (goto-char (point-min))
3225 (when indent
3226 (while (and (not (eobp)) (re-search-forward "^" nil t))
3227 (replace-match indent t t)))
3228 (goto-char (point-min))
3229 (while (looking-at "[ \t]*\n") (replace-match ""))
3230 (goto-char (point-max))
3231 (when (> (org-current-line)
3232 n-lines)
3233 (org-goto-line (1+ n-lines))
3234 (backward-char 1))
3235 (setq txt (buffer-substring (point-min) (point)))))))))
3236 txt))
3238 (defun org-agenda-collect-markers ()
3239 "Collect the markers pointing to entries in the agenda buffer."
3240 (let (m markers)
3241 (save-excursion
3242 (goto-char (point-min))
3243 (while (not (eobp))
3244 (when (setq m (or (org-get-at-bol 'org-hd-marker)
3245 (org-get-at-bol 'org-marker)))
3246 (push m markers))
3247 (beginning-of-line 2)))
3248 (nreverse markers)))
3250 (defun org-create-marker-find-array (marker-list)
3251 "Create a alist of files names with all marker positions in that file."
3252 (let (f tbl m a p)
3253 (while (setq m (pop marker-list))
3254 (setq p (marker-position m)
3255 f (buffer-file-name (or (buffer-base-buffer
3256 (marker-buffer m))
3257 (marker-buffer m))))
3258 (if (setq a (assoc f tbl))
3259 (push (marker-position m) (cdr a))
3260 (push (list f p) tbl)))
3261 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
3262 tbl)))
3264 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
3265 (defun org-check-agenda-marker-table ()
3266 "Check of the current entry is on the marker list."
3267 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3269 (and (setq a (assoc file org-agenda-marker-table))
3270 (save-match-data
3271 (save-excursion
3272 (org-back-to-heading t)
3273 (member (point) (cdr a)))))))
3275 (defun org-check-for-org-mode ()
3276 "Make sure current buffer is in org-mode. Error if not."
3277 (or (derived-mode-p 'org-mode)
3278 (error "Cannot execute org-mode agenda command on buffer in %s"
3279 major-mode)))
3281 ;;; Agenda prepare and finalize
3283 (defvar org-agenda-multi nil) ; dynamically scoped
3284 (defvar org-agenda-pre-window-conf nil)
3285 (defvar org-agenda-columns-active nil)
3286 (defvar org-agenda-name nil)
3287 (defvar org-agenda-tag-filter nil)
3288 (defvar org-agenda-category-filter nil)
3289 (defvar org-agenda-top-category-filter nil)
3290 (defvar org-agenda-tag-filter-while-redo nil)
3291 (defvar org-agenda-tag-filter-preset nil
3292 "A preset of the tags filter used for secondary agenda filtering.
3293 This must be a list of strings, each string must be a single tag preceded
3294 by \"+\" or \"-\".
3295 This variable should not be set directly, but agenda custom commands can
3296 bind it in the options section. The preset filter is a global property of
3297 the entire agenda view. In a block agenda, it will not work reliably to
3298 define a filter for one of the individual blocks. You need to set it in
3299 the global options and expect it to be applied to the entire view.")
3301 (defvar org-agenda-category-filter-preset nil
3302 "A preset of the category filter used for secondary agenda filtering.
3303 This must be a list of strings, each string must be a single category
3304 preceded by \"+\" or \"-\".
3305 This variable should not be set directly, but agenda custom commands can
3306 bind it in the options section. The preset filter is a global property of
3307 the entire agenda view. In a block agenda, it will not work reliably to
3308 define a filter for one of the individual blocks. You need to set it in
3309 the global options and expect it to be applied to the entire view.")
3312 (defun org-agenda-use-sticky-p ()
3313 "Return non-nil if an agenda buffer named
3314 `org-agenda-buffer-name' exists and should be shown instead of
3315 generating a new one."
3316 (and
3317 ;; turned off by user
3318 org-agenda-sticky
3319 ;; For multi-agenda buffer already exists
3320 (not org-agenda-multi)
3321 ;; buffer found
3322 (get-buffer org-agenda-buffer-name)
3323 ;; C-u parameter is same as last call
3324 (with-current-buffer (get-buffer org-agenda-buffer-name)
3325 (and
3326 (equal current-prefix-arg
3327 org-agenda-last-prefix-arg)
3328 ;; In case user turned stickiness on, while having existing
3329 ;; Agenda buffer active, don't reuse that buffer, because it
3330 ;; does not have org variables local
3331 org-agenda-this-buffer-is-sticky))))
3333 (defun org-agenda-prepare-window (abuf)
3334 "Setup agenda buffer in the window."
3335 (let* ((awin (get-buffer-window abuf))
3336 wconf)
3337 (cond
3338 ((equal (current-buffer) abuf) nil)
3339 (awin (select-window awin))
3340 ((not (setq wconf (current-window-configuration))))
3341 ((equal org-agenda-window-setup 'current-window)
3342 (org-pop-to-buffer-same-window abuf))
3343 ((equal org-agenda-window-setup 'other-window)
3344 (org-switch-to-buffer-other-window abuf))
3345 ((equal org-agenda-window-setup 'other-frame)
3346 (switch-to-buffer-other-frame abuf))
3347 ((equal org-agenda-window-setup 'reorganize-frame)
3348 (delete-other-windows)
3349 (org-switch-to-buffer-other-window abuf)))
3350 ;; additional test in case agenda is invoked from within agenda
3351 ;; buffer via elisp link
3352 (unless (equal (current-buffer) abuf)
3353 (org-pop-to-buffer-same-window abuf))
3354 (setq org-agenda-pre-window-conf
3355 (or org-agenda-pre-window-conf wconf))))
3357 (defun org-agenda-prepare (&optional name)
3358 (if (org-agenda-use-sticky-p)
3359 (progn
3360 ;; Popup existing buffer
3361 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3362 (message "Sticky Agenda buffer, use `r' to refresh")
3363 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3364 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3365 (setq org-todo-keywords-for-agenda nil)
3366 (setq org-drawers-for-agenda nil)
3367 (unless org-agenda-persistent-filter
3368 (setq org-agenda-tag-filter nil
3369 org-agenda-category-filter nil))
3370 (put 'org-agenda-tag-filter :preset-filter
3371 org-agenda-tag-filter-preset)
3372 (put 'org-agenda-category-filter :preset-filter
3373 org-agenda-category-filter-preset)
3374 (if org-agenda-multi
3375 (progn
3376 (setq buffer-read-only nil)
3377 (goto-char (point-max))
3378 (unless (or (bobp) org-agenda-compact-blocks
3379 (not org-agenda-block-separator))
3380 (insert "\n"
3381 (if (stringp org-agenda-block-separator)
3382 org-agenda-block-separator
3383 (make-string (window-width) org-agenda-block-separator))
3384 "\n"))
3385 (narrow-to-region (point) (point-max)))
3386 (setq org-done-keywords-for-agenda nil)
3388 ;; Setting any org variables that are in org-agenda-local-vars
3389 ;; list need to be done after the prepare call
3390 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3391 (setq buffer-read-only nil)
3392 (org-agenda-reset-markers)
3393 (let ((inhibit-read-only t)) (erase-buffer))
3394 (org-agenda-mode)
3395 (setq org-agenda-buffer (current-buffer))
3396 (setq org-agenda-contributing-files nil)
3397 (setq org-agenda-columns-active nil)
3398 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3399 (setq org-todo-keywords-for-agenda
3400 (org-uniquify org-todo-keywords-for-agenda))
3401 (setq org-done-keywords-for-agenda
3402 (org-uniquify org-done-keywords-for-agenda))
3403 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3404 (setq org-agenda-last-prefix-arg current-prefix-arg)
3405 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3406 (and name (not org-agenda-name)
3407 (org-set-local 'org-agenda-name name)))
3408 (setq buffer-read-only nil)))
3410 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3411 (defun org-agenda-finalize ()
3412 "Finishing touch for the agenda buffer, called just before displaying it."
3413 (unless org-agenda-multi
3414 (save-excursion
3415 (let ((inhibit-read-only t))
3416 (goto-char (point-min))
3417 (while (org-activate-bracket-links (point-max))
3418 (add-text-properties (match-beginning 0) (match-end 0)
3419 '(face org-link)))
3420 (org-agenda-align-tags)
3421 (unless org-agenda-with-colors
3422 (remove-text-properties (point-min) (point-max) '(face nil))))
3423 (if (and (boundp 'org-agenda-overriding-columns-format)
3424 org-agenda-overriding-columns-format)
3425 (org-set-local 'org-agenda-overriding-columns-format
3426 org-agenda-overriding-columns-format))
3427 (if (and (boundp 'org-agenda-view-columns-initially)
3428 org-agenda-view-columns-initially)
3429 (org-agenda-columns))
3430 (when org-agenda-fontify-priorities
3431 (org-agenda-fontify-priorities))
3432 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3433 (org-agenda-dim-blocked-tasks))
3434 (org-agenda-mark-clocking-task)
3435 (when org-agenda-entry-text-mode
3436 (org-agenda-entry-text-hide)
3437 (org-agenda-entry-text-show))
3438 (if (functionp 'org-habit-insert-consistency-graphs)
3439 (org-habit-insert-consistency-graphs))
3440 (let ((inhibit-read-only t))
3441 (run-hooks 'org-agenda-finalize-hook))
3442 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3443 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3444 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3445 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3446 (org-agenda-filter-apply org-agenda-category-filter 'category))
3447 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local))))
3449 (defun org-agenda-mark-clocking-task ()
3450 "Mark the current clock entry in the agenda if it is present."
3451 (mapc (lambda (o)
3452 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3453 (delete-overlay o)))
3454 (overlays-in (point-min) (point-max)))
3455 (when (marker-buffer org-clock-hd-marker)
3456 (save-excursion
3457 (goto-char (point-min))
3458 (let (s ov)
3459 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3460 (goto-char s)
3461 (when (equal (org-get-at-bol 'org-hd-marker)
3462 org-clock-hd-marker)
3463 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3464 (overlay-put ov 'type 'org-agenda-clocking)
3465 (overlay-put ov 'face 'org-agenda-clocking)
3466 (overlay-put ov 'help-echo
3467 "The clock is running in this item")))))))
3469 (defun org-agenda-fontify-priorities ()
3470 "Make highest priority lines bold, and lowest italic."
3471 (interactive)
3472 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3473 (delete-overlay o)))
3474 (overlays-in (point-min) (point-max)))
3475 (save-excursion
3476 (let ((inhibit-read-only t)
3477 b e p ov h l)
3478 (goto-char (point-min))
3479 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3480 (setq h (or (get-char-property (point) 'org-highest-priority)
3481 org-highest-priority)
3482 l (or (get-char-property (point) 'org-lowest-priority)
3483 org-lowest-priority)
3484 p (string-to-char (match-string 1))
3485 b (match-beginning 0)
3486 e (if (eq org-agenda-fontify-priorities 'cookies)
3487 (match-end 0)
3488 (point-at-eol))
3489 ov (make-overlay b e))
3490 (overlay-put
3491 ov 'face
3492 (cond ((org-face-from-face-or-color
3493 'priority nil
3494 (cdr (assoc p org-priority-faces))))
3495 ((and (listp org-agenda-fontify-priorities)
3496 (org-face-from-face-or-color
3497 'priority nil
3498 (cdr (assoc p org-agenda-fontify-priorities)))))
3499 ((equal p l) 'italic)
3500 ((equal p h) 'bold)))
3501 (overlay-put ov 'org-type 'org-priority)))))
3503 (defun org-agenda-dim-blocked-tasks ()
3504 "Dim currently blocked TODO's in the agenda display."
3505 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3506 (delete-overlay o)))
3507 (overlays-in (point-min) (point-max)))
3508 (save-excursion
3509 (let ((inhibit-read-only t)
3510 (org-depend-tag-blocked nil)
3511 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3512 org-blocked-by-checkboxes
3513 invis1 b e p ov h l)
3514 (goto-char (point-min))
3515 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3516 (and pos (goto-char (1+ pos))))
3517 (setq org-blocked-by-checkboxes nil invis1 invis)
3518 (let ((marker (org-get-at-bol 'org-hd-marker)))
3519 (when (and marker
3520 (with-current-buffer (marker-buffer marker)
3521 (save-excursion (goto-char marker)
3522 (org-entry-blocked-p))))
3523 (if org-blocked-by-checkboxes (setq invis1 nil))
3524 (setq b (if invis1
3525 (max (point-min) (1- (point-at-bol)))
3526 (point-at-bol))
3527 e (point-at-eol)
3528 ov (make-overlay b e))
3529 (if invis1
3530 (overlay-put ov 'invisible t)
3531 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3532 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3534 (defvar org-agenda-skip-function nil
3535 "Function to be called at each match during agenda construction.
3536 If this function returns nil, the current match should not be skipped.
3537 Otherwise, the function must return a position from where the search
3538 should be continued.
3539 This may also be a Lisp form, it will be evaluated.
3540 Never set this variable using `setq' or so, because then it will apply
3541 to all future agenda commands. If you do want a global skipping condition,
3542 use the option `org-agenda-skip-function-global' instead.
3543 The correct usage for `org-agenda-skip-function' is to bind it with
3544 `let' to scope it dynamically into the agenda-constructing command.
3545 A good way to set it is through options in `org-agenda-custom-commands'.")
3547 (defun org-agenda-skip ()
3548 "Throw to `:skip' in places that should be skipped.
3549 Also moves point to the end of the skipped region, so that search can
3550 continue from there."
3551 (let ((p (point-at-bol)) to)
3552 (when (org-in-src-block-p) (throw :skip t))
3553 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3554 (get-text-property p :org-archived)
3555 (org-end-of-subtree t)
3556 (throw :skip t))
3557 (and org-agenda-skip-comment-trees
3558 (get-text-property p :org-comment)
3559 (org-end-of-subtree t)
3560 (throw :skip t))
3561 (if (equal (char-after p) ?#) (throw :skip t))
3562 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3563 (org-agenda-skip-eval org-agenda-skip-function)))
3564 (goto-char to)
3565 (throw :skip t))))
3567 (defun org-agenda-skip-eval (form)
3568 "If FORM is a function or a list, call (or eval) is and return result.
3569 `save-excursion' and `save-match-data' are wrapped around the call, so point
3570 and match data are returned to the previous state no matter what these
3571 functions do."
3572 (let (fp)
3573 (and form
3574 (or (setq fp (functionp form))
3575 (consp form))
3576 (save-excursion
3577 (save-match-data
3578 (if fp
3579 (funcall form)
3580 (eval form)))))))
3582 (defvar org-agenda-markers nil
3583 "List of all currently active markers created by `org-agenda'.")
3584 (defvar org-agenda-last-marker-time (org-float-time)
3585 "Creation time of the last agenda marker.")
3587 (defun org-agenda-new-marker (&optional pos)
3588 "Return a new agenda marker.
3589 Org-mode keeps a list of these markers and resets them when they are
3590 no longer in use."
3591 (let ((m (copy-marker (or pos (point)))))
3592 (setq org-agenda-last-marker-time (org-float-time))
3593 (if org-agenda-buffer
3594 (with-current-buffer org-agenda-buffer
3595 (push m org-agenda-markers))
3596 (push m org-agenda-markers))
3599 (defun org-agenda-reset-markers ()
3600 "Reset markers created by `org-agenda'."
3601 (while org-agenda-markers
3602 (move-marker (pop org-agenda-markers) nil)))
3604 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3605 "Save relative positions of markers in region.
3606 This check for agenda markers in all agenda buffers currently active."
3607 (dolist (buf (buffer-list))
3608 (with-current-buffer buf
3609 (when (eq major-mode 'org-agenda-mode)
3610 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3611 org-agenda-markers)))))
3613 ;;; Entry text mode
3615 (defun org-agenda-entry-text-show-here ()
3616 "Add some text from the entry as context to the current line."
3617 (let (m txt o)
3618 (setq m (org-get-at-bol 'org-hd-marker))
3619 (unless (marker-buffer m)
3620 (error "No marker points to an entry here"))
3621 (setq txt (concat "\n" (org-no-properties
3622 (org-agenda-get-some-entry-text
3623 m org-agenda-entry-text-maxlines " > "))))
3624 (when (string-match "\\S-" txt)
3625 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3626 (overlay-put o 'evaporate t)
3627 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3628 (overlay-put o 'after-string txt))))
3630 (defun org-agenda-entry-text-show ()
3631 "Add entry context for all agenda lines."
3632 (interactive)
3633 (save-excursion
3634 (goto-char (point-max))
3635 (beginning-of-line 1)
3636 (while (not (bobp))
3637 (when (org-get-at-bol 'org-hd-marker)
3638 (org-agenda-entry-text-show-here))
3639 (beginning-of-line 0))))
3641 (defun org-agenda-entry-text-hide ()
3642 "Remove any shown entry context."
3643 (delq nil
3644 (mapcar (lambda (o)
3645 (if (eq (overlay-get o 'org-overlay-type)
3646 'agenda-entry-content)
3647 (progn (delete-overlay o) t)))
3648 (overlays-in (point-min) (point-max)))))
3650 (defun org-agenda-get-day-face (date)
3651 "Return the face DATE should be displayed with."
3652 (or (and (functionp org-agenda-day-face-function)
3653 (funcall org-agenda-day-face-function date))
3654 (cond ((org-agenda-todayp date)
3655 'org-agenda-date-today)
3656 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3657 'org-agenda-date-weekend)
3658 (t 'org-agenda-date))))
3660 ;;; Agenda timeline
3662 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3664 (defun org-timeline (&optional dotodo)
3665 "Show a time-sorted view of the entries in the current org file.
3666 Only entries with a time stamp of today or later will be listed. With
3667 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3668 under the current date.
3669 If the buffer contains an active region, only check the region for
3670 dates."
3671 (interactive "P")
3672 (let* ((dopast t)
3673 (org-agenda-show-log-scoped org-agenda-show-log)
3674 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3675 (current-buffer))))
3676 (date (calendar-current-date))
3677 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3678 (end (if (org-region-active-p) (region-end) (point-max)))
3679 (day-numbers (org-get-all-dates
3680 beg end 'no-ranges
3681 t org-agenda-show-log-scoped ; always include today
3682 org-timeline-show-empty-dates))
3683 (org-deadline-warning-days 0)
3684 (org-agenda-only-exact-dates t)
3685 (today (org-today))
3686 (past t)
3687 args
3688 s e rtn d emptyp)
3689 (setq org-agenda-redo-command
3690 (list 'progn
3691 (list 'org-switch-to-buffer-other-window (current-buffer))
3692 (list 'org-timeline (list 'quote dotodo))))
3693 (if (not dopast)
3694 ;; Remove past dates from the list of dates.
3695 (setq day-numbers (delq nil (mapcar (lambda(x)
3696 (if (>= x today) x nil))
3697 day-numbers))))
3698 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
3699 (org-compile-prefix-format 'timeline)
3700 (org-set-sorting-strategy 'timeline)
3701 (if org-agenda-show-log-scoped (push :closed args))
3702 (push :timestamp args)
3703 (push :deadline args)
3704 (push :scheduled args)
3705 (push :sexp args)
3706 (if dotodo (push :todo args))
3707 (insert "Timeline of file " entry "\n")
3708 (add-text-properties (point-min) (point)
3709 (list 'face 'org-agenda-structure))
3710 (org-agenda-mark-header-line (point-min))
3711 (while (setq d (pop day-numbers))
3712 (if (and (listp d) (eq (car d) :omitted))
3713 (progn
3714 (setq s (point))
3715 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3716 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3717 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3718 (if (and (>= d today)
3719 dopast
3720 past)
3721 (progn
3722 (setq past nil)
3723 (insert (make-string 79 ?-) "\n")))
3724 (setq date (calendar-gregorian-from-absolute d))
3725 (setq s (point))
3726 (setq rtn (and (not emptyp)
3727 (apply 'org-agenda-get-day-entries entry
3728 date args)))
3729 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3730 (progn
3731 (insert
3732 (if (stringp org-agenda-format-date)
3733 (format-time-string org-agenda-format-date
3734 (org-time-from-absolute date))
3735 (funcall org-agenda-format-date date))
3736 "\n")
3737 (put-text-property s (1- (point)) 'face
3738 (org-agenda-get-day-face date))
3739 (put-text-property s (1- (point)) 'org-date-line t)
3740 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3741 (if (equal d today)
3742 (put-text-property s (1- (point)) 'org-today t))
3743 (and rtn (insert (org-agenda-finalize-entries rtn) "\n"))
3744 (put-text-property s (1- (point)) 'day d)))))
3745 (goto-char (point-min))
3746 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3747 (point-min)))
3748 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3749 (org-agenda-finalize)
3750 (setq buffer-read-only t)))
3752 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3753 "Return a list of all relevant day numbers from BEG to END buffer positions.
3754 If NO-RANGES is non-nil, include only the start and end dates of a range,
3755 not every single day in the range. If FORCE-TODAY is non-nil, make
3756 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3757 inactive time stamps (those in square brackets) are included.
3758 When EMPTY is non-nil, also include days without any entries."
3759 (let ((re (concat
3760 (if pre-re pre-re "")
3761 (if inactive org-ts-regexp-both org-ts-regexp)))
3762 dates dates1 date day day1 day2 ts1 ts2 pos)
3763 (if force-today
3764 (setq dates (list (org-today))))
3765 (save-excursion
3766 (goto-char beg)
3767 (while (re-search-forward re end t)
3768 (setq day (time-to-days (org-time-string-to-time
3769 (substring (match-string 1) 0 10)
3770 (current-buffer) (match-beginning 0))))
3771 (or (memq day dates) (push day dates)))
3772 (unless no-ranges
3773 (goto-char beg)
3774 (while (re-search-forward org-tr-regexp end t)
3775 (setq pos (match-beginning 0))
3776 (setq ts1 (substring (match-string 1) 0 10)
3777 ts2 (substring (match-string 2) 0 10)
3778 day1 (time-to-days (org-time-string-to-time
3779 ts1 (current-buffer) pos))
3780 day2 (time-to-days (org-time-string-to-time
3781 ts2 (current-buffer) pos)))
3782 (while (< (setq day1 (1+ day1)) day2)
3783 (or (memq day1 dates) (push day1 dates)))))
3784 (setq dates (sort dates '<))
3785 (when empty
3786 (while (setq day (pop dates))
3787 (setq day2 (car dates))
3788 (push day dates1)
3789 (when (and day2 empty)
3790 (if (or (eq empty t)
3791 (and (numberp empty) (<= (- day2 day) empty)))
3792 (while (< (setq day (1+ day)) day2)
3793 (push (list day) dates1))
3794 (push (cons :omitted (- day2 day)) dates1))))
3795 (setq dates (nreverse dates1)))
3796 dates)))
3798 ;;; Agenda Daily/Weekly
3800 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3801 "Start day for the agenda view.
3802 Custom commands can set this variable in the options section.")
3803 (defvar org-starting-day nil) ; local variable in the agenda buffer
3804 (defvar org-arg-loc nil) ; local variable
3806 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3807 "List of types searched for when creating the daily/weekly agenda.
3808 This variable is a list of symbols that controls the types of
3809 items that appear in the daily/weekly agenda. Allowed symbols in this
3810 list are are
3812 :timestamp List items containing a date stamp or date range matching
3813 the selected date. This includes sexp entries in
3814 angular brackets.
3816 :sexp List entries resulting from plain diary-like sexps.
3818 :deadline List deadline due on that date. When the date is today,
3819 also list any deadlines past due, or due within
3820 `org-deadline-warning-days'. `:deadline' must appear before
3821 `:scheduled' if the setting of
3822 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3823 any effect.
3825 :scheduled List all items which are scheduled for the given date.
3826 The diary for *today* also contains items which were
3827 scheduled earlier and are not yet marked DONE.
3829 By default, all four types are turned on.
3831 Never set this variable globally using `setq', because then it
3832 will apply to all future agenda commands. Instead, bind it with
3833 `let' to scope it dynamically into the agenda-constructing
3834 command. A good way to set it is through options in
3835 `org-agenda-custom-commands'. For a more flexible (though
3836 somewhat less efficient) way of determining what is included in
3837 the daily/weekly agenda, see `org-agenda-skip-function'.")
3839 (defvar org-agenda-buffer-tmp-name nil)
3840 ;;;###autoload
3841 (defun org-agenda-list (&optional arg start-day span)
3842 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3843 The view will be for the current day or week, but from the overview buffer
3844 you will be able to go to other days/weeks.
3846 With a numeric prefix argument in an interactive call, the agenda will
3847 span ARG days. Lisp programs should instead specify SPAN to change
3848 the number of days. SPAN defaults to `org-agenda-span'.
3850 START-DAY defaults to TODAY, or to the most recent match for the weekday
3851 given in `org-agenda-start-on-weekday'."
3852 (interactive "P")
3853 (if org-agenda-overriding-arguments
3854 (setq arg (car org-agenda-overriding-arguments)
3855 start-day (nth 1 org-agenda-overriding-arguments)
3856 span (nth 2 org-agenda-overriding-arguments)))
3857 (if (and (integerp arg) (> arg 0))
3858 (setq span arg arg nil))
3859 (catch 'exit
3860 (setq org-agenda-buffer-name
3861 (or org-agenda-buffer-tmp-name
3862 (if org-agenda-sticky
3863 (cond ((and org-keys (stringp org-match))
3864 (format "*Org Agenda(%s:%s)*" org-keys org-match))
3865 (org-keys
3866 (format "*Org Agenda(%s)*" org-keys))
3867 (t "*Org Agenda(a)*")))
3868 org-agenda-buffer-name))
3869 (org-agenda-prepare "Day/Week")
3870 (setq start-day (or start-day org-agenda-start-day))
3871 (if (stringp start-day)
3872 ;; Convert to an absolute day number
3873 (setq start-day (time-to-days (org-read-date nil t start-day))))
3874 (org-compile-prefix-format 'agenda)
3875 (org-set-sorting-strategy 'agenda)
3876 (let* ((span (org-agenda-ndays-to-span
3877 (or span org-agenda-ndays org-agenda-span)))
3878 (today (org-today))
3879 (sd (or start-day today))
3880 (ndays (org-agenda-span-to-ndays span sd))
3881 (org-agenda-start-on-weekday
3882 (if (eq ndays 7)
3883 org-agenda-start-on-weekday))
3884 (thefiles (org-agenda-files nil 'ifmode))
3885 (files thefiles)
3886 (start (if (or (null org-agenda-start-on-weekday)
3887 (< ndays 7))
3889 (let* ((nt (calendar-day-of-week
3890 (calendar-gregorian-from-absolute sd)))
3891 (n1 org-agenda-start-on-weekday)
3892 (d (- nt n1)))
3893 (- sd (+ (if (< d 0) 7 0) d)))))
3894 (day-numbers (list start))
3895 (day-cnt 0)
3896 (inhibit-redisplay (not debug-on-error))
3897 (org-agenda-show-log-scoped org-agenda-show-log)
3898 s e rtn rtnall file date d start-pos end-pos todayp
3899 clocktable-start clocktable-end filter)
3900 (setq org-agenda-redo-command
3901 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span)))
3902 (dotimes (n (1- ndays))
3903 (push (1+ (car day-numbers)) day-numbers))
3904 (setq day-numbers (nreverse day-numbers))
3905 (setq clocktable-start (car day-numbers)
3906 clocktable-end (1+ (or (org-last day-numbers) 0)))
3907 (org-set-local 'org-starting-day (car day-numbers))
3908 (org-set-local 'org-arg-loc arg)
3909 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3910 (unless org-agenda-compact-blocks
3911 (let* ((d1 (car day-numbers))
3912 (d2 (org-last day-numbers))
3913 (w1 (org-days-to-iso-week d1))
3914 (w2 (org-days-to-iso-week d2)))
3915 (setq s (point))
3916 (if org-agenda-overriding-header
3917 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3918 nil 'face 'org-agenda-structure) "\n")
3919 (insert (org-agenda-span-name span)
3920 "-agenda"
3921 (if (< (- d2 d1) 350)
3922 (if (= w1 w2)
3923 (format " (W%02d)" w1)
3924 (format " (W%02d-W%02d)" w1 w2))
3926 ":\n")))
3927 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3928 'org-date-line t))
3929 (org-agenda-mark-header-line s))
3930 (while (setq d (pop day-numbers))
3931 (setq date (calendar-gregorian-from-absolute d)
3932 s (point))
3933 (if (or (setq todayp (= d today))
3934 (and (not start-pos) (= d sd)))
3935 (setq start-pos (point))
3936 (if (and start-pos (not end-pos))
3937 (setq end-pos (point))))
3938 (setq files thefiles
3939 rtnall nil)
3940 (while (setq file (pop files))
3941 (catch 'nextfile
3942 (org-check-agenda-file file)
3943 (let ((org-agenda-entry-types org-agenda-entry-types))
3944 (unless org-agenda-include-deadlines
3945 (setq org-agenda-entry-types
3946 (delq :deadline org-agenda-entry-types)))
3947 (cond
3948 ((memq org-agenda-show-log-scoped '(only clockcheck))
3949 (setq rtn (org-agenda-get-day-entries
3950 file date :closed)))
3951 (org-agenda-show-log-scoped
3952 (setq rtn (apply 'org-agenda-get-day-entries
3953 file date
3954 (append '(:closed) org-agenda-entry-types))))
3956 (setq rtn (apply 'org-agenda-get-day-entries
3957 file date
3958 org-agenda-entry-types)))))
3959 (setq rtnall (append rtnall rtn)))) ;; all entries
3960 (if org-agenda-include-diary
3961 (let ((org-agenda-search-headline-for-time t))
3962 (require 'diary-lib)
3963 (setq rtn (org-get-entries-from-diary date))
3964 (setq rtnall (append rtnall rtn))))
3965 (if (or rtnall org-agenda-show-all-dates)
3966 (progn
3967 (setq day-cnt (1+ day-cnt))
3968 (insert
3969 (if (stringp org-agenda-format-date)
3970 (format-time-string org-agenda-format-date
3971 (org-time-from-absolute date))
3972 (funcall org-agenda-format-date date))
3973 "\n")
3974 (put-text-property s (1- (point)) 'face
3975 (org-agenda-get-day-face date))
3976 (put-text-property s (1- (point)) 'org-date-line t)
3977 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3978 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3979 (when todayp
3980 (put-text-property s (1- (point)) 'org-today t))
3981 (setq rtnall
3982 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
3983 (if rtnall (insert ;; all entries
3984 (org-agenda-finalize-entries rtnall)
3985 "\n"))
3986 (put-text-property s (1- (point)) 'day d)
3987 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3988 (when (and org-agenda-clockreport-mode clocktable-start)
3989 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3990 ;; the above line is to ensure the restricted range!
3991 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3992 tbl)
3993 (setq p (org-plist-delete p :block))
3994 (setq p (plist-put p :tstart clocktable-start))
3995 (setq p (plist-put p :tend clocktable-end))
3996 (setq p (plist-put p :scope 'agenda))
3997 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3998 (setq filter (or org-agenda-tag-filter-while-redo
3999 (get 'org-agenda-tag-filter :preset-filter))))
4000 (setq p (plist-put p :tags (mapconcat (lambda (x)
4001 (if (string-match "[<>=]" x)
4004 filter ""))))
4005 (setq tbl (apply 'org-get-clocktable p))
4006 (insert tbl)))
4007 (goto-char (point-min))
4008 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4009 (unless (and (pos-visible-in-window-p (point-min))
4010 (pos-visible-in-window-p (point-max)))
4011 (goto-char (1- (point-max)))
4012 (recenter -1)
4013 (if (not (pos-visible-in-window-p (or start-pos 1)))
4014 (progn
4015 (goto-char (or start-pos 1))
4016 (recenter 1))))
4017 (goto-char (or start-pos 1))
4018 (add-text-properties (point-min) (point-max)
4019 `(org-agenda-type agenda
4020 org-last-args (,arg ,start-day ,span)
4021 org-redo-cmd ,org-agenda-redo-command
4022 org-serie-cmd ,org-cmd))
4023 (if (eq org-agenda-show-log-scoped 'clockcheck)
4024 (org-agenda-show-clocking-issues))
4025 (org-agenda-finalize)
4026 (setq buffer-read-only t)
4027 (message ""))))
4029 (defun org-agenda-ndays-to-span (n)
4030 "Return a span symbol for a span of N days, or N if none matches."
4031 (cond ((symbolp n) n)
4032 ((= n 1) 'day)
4033 ((= n 7) 'week)
4034 (t n)))
4036 (defun org-agenda-span-to-ndays (span &optional start-day)
4037 "Return ndays from SPAN, possibly starting at START-DAY."
4038 (cond ((numberp span) span)
4039 ((eq span 'day) 1)
4040 ((eq span 'week) 7)
4041 ((eq span 'month)
4042 (let ((date (calendar-gregorian-from-absolute start-day)))
4043 (calendar-last-day-of-month (car date) (caddr date))))
4044 ((eq span 'year)
4045 (let ((date (calendar-gregorian-from-absolute start-day)))
4046 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4048 (defun org-agenda-span-name (span)
4049 "Return a SPAN name."
4050 (if (null span)
4052 (if (symbolp span)
4053 (capitalize (symbol-name span))
4054 (format "%d days" span))))
4056 ;;; Agenda word search
4058 (defvar org-agenda-search-history nil)
4060 (defvar org-search-syntax-table nil
4061 "Special syntax table for org-mode search.
4062 In this table, we have single quotes not as word constituents, to
4063 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4065 (defvar org-mode-syntax-table) ; From org.el
4066 (defun org-search-syntax-table ()
4067 (unless org-search-syntax-table
4068 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4069 (modify-syntax-entry ?' "." org-search-syntax-table)
4070 (modify-syntax-entry ?` "." org-search-syntax-table))
4071 org-search-syntax-table)
4073 (defvar org-agenda-last-search-view-search-was-boolean nil)
4075 ;;;###autoload
4076 (defun org-search-view (&optional todo-only string edit-at)
4077 "Show all entries that contain a phrase or words or regular expressions.
4079 With optional prefix argument TODO-ONLY, only consider entries that are
4080 TODO entries. The argument STRING can be used to pass a default search
4081 string into this function. If EDIT-AT is non-nil, it means that the
4082 user should get a chance to edit this string, with cursor at position
4083 EDIT-AT.
4085 The search string can be viewed either as a phrase that should be found as
4086 is, or it can be broken into a number of snippets, each of which must match
4087 in a Boolean way to select an entry. The default depends on the variable
4088 `org-agenda-search-view-always-boolean'.
4089 Even if this is turned off (the default) you can always switch to
4090 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4092 The default is a direct search of the whole phrase, where each space in
4093 the search string can expand to an arbitrary amount of whitespace,
4094 including newlines.
4096 If using a Boolean search, the search string is split on whitespace and
4097 each snippet is searched separately, with logical AND to select an entry.
4098 Words prefixed with a minus must *not* occur in the entry. Words without
4099 a prefix or prefixed with a plus must occur in the entry. Matching is
4100 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4101 match whole words, not parts of a word) if
4102 `org-agenda-search-view-force-full-words' is set (default is nil).
4104 Boolean search snippets enclosed by curly braces are interpreted as
4105 regular expressions that must or (when preceded with \"-\") must not
4106 match in the entry. Snippets enclosed into double quotes will be taken
4107 as a whole, to include whitespace.
4109 - If the search string starts with an asterisk, search only in headlines.
4110 - If (possibly after the leading star) the search string starts with an
4111 exclamation mark, this also means to look at TODO entries only, an effect
4112 that can also be achieved with a prefix argument.
4113 - If (possibly after star and exclamation mark) the search string starts
4114 with a colon, this will mean that the (non-regexp) snippets of the
4115 Boolean search must match as full words.
4117 This command searches the agenda files, and in addition the files listed
4118 in `org-agenda-text-search-extra-files'."
4119 (interactive "P")
4120 (if org-agenda-overriding-arguments
4121 (setq todo-only (car org-agenda-overriding-arguments)
4122 string (nth 1 org-agenda-overriding-arguments)
4123 edit-at (nth 2 org-agenda-overriding-arguments)))
4124 (let* ((props (list 'face nil
4125 'done-face 'org-agenda-done
4126 'org-not-done-regexp org-not-done-regexp
4127 'org-todo-regexp org-todo-regexp
4128 'org-complex-heading-regexp org-complex-heading-regexp
4129 'mouse-face 'highlight
4130 'help-echo (format "mouse-2 or RET jump to location")))
4131 (full-words org-agenda-search-view-force-full-words)
4132 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4133 regexp rtn rtnall files file pos
4134 marker category category-pos tags c neg re boolean
4135 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4136 (unless (and (not edit-at)
4137 (stringp string)
4138 (string-match "\\S-" string))
4139 (setq string (read-string
4140 (if org-agenda-search-view-always-boolean
4141 "[+-]Word/{Regexp} ...: "
4142 "Phrase or [+-]Word/{Regexp} ...: ")
4143 (cond
4144 ((integerp edit-at) (cons string edit-at))
4145 (edit-at string))
4146 'org-agenda-search-history)))
4147 (catch 'exit
4148 (if org-agenda-sticky
4149 (setq org-agenda-buffer-name
4150 (if (stringp string)
4151 (format "*Org Agenda(%s:%s)*"
4152 (or org-keys (or (and todo-only "S") "s")) string)
4153 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4154 (org-agenda-prepare "SEARCH")
4155 (org-compile-prefix-format 'search)
4156 (org-set-sorting-strategy 'search)
4157 (setq org-agenda-redo-command
4158 (list 'org-search-view (if todo-only t nil)
4159 (list 'if 'current-prefix-arg nil string)))
4160 (setq org-agenda-query-string string)
4161 (if (equal (string-to-char string) ?*)
4162 (setq hdl-only t
4163 words (substring string 1))
4164 (setq words string))
4165 (when (equal (string-to-char words) ?!)
4166 (setq todo-only t
4167 words (substring words 1)))
4168 (when (equal (string-to-char words) ?:)
4169 (setq full-words t
4170 words (substring words 1)))
4171 (if (or org-agenda-search-view-always-boolean
4172 (member (string-to-char words) '(?- ?+ ?\{)))
4173 (setq boolean t))
4174 (setq words (org-split-string words))
4175 (let (www w)
4176 (while (setq w (pop words))
4177 (while (and (string-match "\\\\\\'" w) words)
4178 (setq w (concat (substring w 0 -1) " " (pop words))))
4179 (push w www))
4180 (setq words (nreverse www) www nil)
4181 (while (setq w (pop words))
4182 (when (and (string-match "\\`[-+]?{" w)
4183 (not (string-match "}\\'" w)))
4184 (while (and words (not (string-match "}\\'" (car words))))
4185 (setq w (concat w " " (pop words))))
4186 (setq w (concat w " " (pop words))))
4187 (push w www))
4188 (setq words (nreverse www)))
4189 (setq org-agenda-last-search-view-search-was-boolean boolean)
4190 (when boolean
4191 (let (wds w)
4192 (while (setq w (pop words))
4193 (if (or (equal (substring w 0 1) "\"")
4194 (and (> (length w) 1)
4195 (member (substring w 0 1) '("+" "-"))
4196 (equal (substring w 1 2) "\"")))
4197 (while (and words (not (equal (substring w -1) "\"")))
4198 (setq w (concat w " " (pop words)))))
4199 (and (string-match "\\`\\([-+]?\\)\"" w)
4200 (setq w (replace-match "\\1" nil nil w)))
4201 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4202 (push w wds))
4203 (setq words (nreverse wds))))
4204 (if boolean
4205 (mapc (lambda (w)
4206 (setq c (string-to-char w))
4207 (if (equal c ?-)
4208 (setq neg t w (substring w 1))
4209 (if (equal c ?+)
4210 (setq neg nil w (substring w 1))
4211 (setq neg nil)))
4212 (if (string-match "\\`{.*}\\'" w)
4213 (setq re (substring w 1 -1))
4214 (if full-words
4215 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4216 (setq re (regexp-quote (downcase w)))))
4217 (if neg (push re regexps-) (push re regexps+)))
4218 words)
4219 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4220 regexps+))
4221 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4222 (if (not regexps+)
4223 (setq regexp org-outline-regexp-bol)
4224 (setq regexp (pop regexps+))
4225 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4226 regexp))))
4227 (setq files (org-agenda-files nil 'ifmode))
4228 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4229 (pop org-agenda-text-search-extra-files)
4230 (setq files (org-add-archive-files files)))
4231 (setq files (append files org-agenda-text-search-extra-files)
4232 rtnall nil)
4233 (while (setq file (pop files))
4234 (setq ee nil)
4235 (catch 'nextfile
4236 (org-check-agenda-file file)
4237 (setq buffer (if (file-exists-p file)
4238 (org-get-agenda-file-buffer file)
4239 (error "No such file %s" file)))
4240 (if (not buffer)
4241 ;; If file does not exist, make sure an error message is sent
4242 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4243 file))))
4244 (with-current-buffer buffer
4245 (with-syntax-table (org-search-syntax-table)
4246 (unless (derived-mode-p 'org-mode)
4247 (error "Agenda file %s is not in `org-mode'" file))
4248 (let ((case-fold-search t))
4249 (save-excursion
4250 (save-restriction
4251 (if org-agenda-restrict
4252 (narrow-to-region org-agenda-restrict-begin
4253 org-agenda-restrict-end)
4254 (widen))
4255 (goto-char (point-min))
4256 (unless (or (org-at-heading-p)
4257 (outline-next-heading))
4258 (throw 'nextfile t))
4259 (goto-char (max (point-min) (1- (point))))
4260 (while (re-search-forward regexp nil t)
4261 (org-back-to-heading t)
4262 (skip-chars-forward "* ")
4263 (setq beg (point-at-bol)
4264 beg1 (point)
4265 end (progn (outline-next-heading) (point)))
4266 (catch :skip
4267 (goto-char beg)
4268 (org-agenda-skip)
4269 (setq str (buffer-substring-no-properties
4270 (point-at-bol)
4271 (if hdl-only (point-at-eol) end)))
4272 (mapc (lambda (wr) (when (string-match wr str)
4273 (goto-char (1- end))
4274 (throw :skip t)))
4275 regexps-)
4276 (mapc (lambda (wr) (unless (string-match wr str)
4277 (goto-char (1- end))
4278 (throw :skip t)))
4279 (if todo-only
4280 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4281 regexps+)
4282 regexps+))
4283 (goto-char beg)
4284 (setq marker (org-agenda-new-marker (point))
4285 category (org-get-category)
4286 category-pos (get-text-property (point) 'org-category-position)
4287 tags (org-get-tags-at (point))
4288 txt (org-agenda-format-item
4290 (buffer-substring-no-properties
4291 beg1 (point-at-eol))
4292 category tags t))
4293 (org-add-props txt props
4294 'org-marker marker 'org-hd-marker marker
4295 'org-todo-regexp org-todo-regexp
4296 'org-complex-heading-regexp org-complex-heading-regexp
4297 'priority 1000 'org-category category
4298 'org-category-position category-pos
4299 'type "search")
4300 (push txt ee)
4301 (goto-char (1- end))))))))))
4302 (setq rtn (nreverse ee))
4303 (setq rtnall (append rtnall rtn)))
4304 (if org-agenda-overriding-header
4305 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4306 nil 'face 'org-agenda-structure) "\n")
4307 (insert "Search words: ")
4308 (add-text-properties (point-min) (1- (point))
4309 (list 'face 'org-agenda-structure))
4310 (setq pos (point))
4311 (insert string "\n")
4312 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4313 (setq pos (point))
4314 (unless org-agenda-multi
4315 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4316 (add-text-properties pos (1- (point))
4317 (list 'face 'org-agenda-structure))))
4318 (org-agenda-mark-header-line (point-min))
4319 (when rtnall
4320 (insert (org-agenda-finalize-entries rtnall) "\n"))
4321 (goto-char (point-min))
4322 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4323 (add-text-properties (point-min) (point-max)
4324 `(org-agenda-type search
4325 org-last-args (,todo-only ,string ,edit-at)
4326 org-redo-cmd ,org-agenda-redo-command
4327 org-serie-cmd ,org-cmd))
4328 (org-agenda-finalize)
4329 (setq buffer-read-only t))))
4331 ;;; Agenda TODO list
4333 (defvar org-select-this-todo-keyword nil)
4334 (defvar org-last-arg nil)
4336 ;;;###autoload
4337 (defun org-todo-list (&optional arg)
4338 "Show all (not done) TODO entries from all agenda file in a single list.
4339 The prefix arg can be used to select a specific TODO keyword and limit
4340 the list to these. When using \\[universal-argument], you will be prompted
4341 for a keyword. A numeric prefix directly selects the Nth keyword in
4342 `org-todo-keywords-1'."
4343 (interactive "P")
4344 (if org-agenda-overriding-arguments
4345 (setq arg org-agenda-overriding-arguments))
4346 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4347 (let* ((today (org-today))
4348 (date (calendar-gregorian-from-absolute today))
4349 (kwds org-todo-keywords-for-agenda)
4350 (completion-ignore-case t)
4351 (org-select-this-todo-keyword
4352 (if (stringp arg) arg
4353 (and arg (integerp arg) (> arg 0)
4354 (nth (1- arg) kwds))))
4355 rtn rtnall files file pos)
4356 (when (equal arg '(4))
4357 (setq org-select-this-todo-keyword
4358 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4359 (mapcar 'list kwds) nil nil)))
4360 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4361 (catch 'exit
4362 (if org-agenda-sticky
4363 (setq org-agenda-buffer-name
4364 (if (stringp org-select-this-todo-keyword)
4365 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4366 org-select-this-todo-keyword)
4367 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4368 (org-agenda-prepare "TODO")
4369 (org-compile-prefix-format 'todo)
4370 (org-set-sorting-strategy 'todo)
4371 (setq org-agenda-redo-command
4372 `(org-todo-list (or current-prefix-arg (quote ,arg))))
4373 (setq files (org-agenda-files nil 'ifmode)
4374 rtnall nil)
4375 (while (setq file (pop files))
4376 (catch 'nextfile
4377 (org-check-agenda-file file)
4378 (setq rtn (org-agenda-get-day-entries file date :todo))
4379 (setq rtnall (append rtnall rtn))))
4380 (if org-agenda-overriding-header
4381 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4382 nil 'face 'org-agenda-structure) "\n")
4383 (insert "Global list of TODO items of type: ")
4384 (add-text-properties (point-min) (1- (point))
4385 (list 'face 'org-agenda-structure
4386 'short-heading
4387 (concat "ToDo: "
4388 (or org-select-this-todo-keyword "ALL"))))
4389 (org-agenda-mark-header-line (point-min))
4390 (setq pos (point))
4391 (insert (or org-select-this-todo-keyword "ALL") "\n")
4392 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4393 (setq pos (point))
4394 (unless org-agenda-multi
4395 (insert "Available with `N r': (0)[ALL]")
4396 (let ((n 0) s)
4397 (mapc (lambda (x)
4398 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4399 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4400 (insert "\n "))
4401 (insert " " s))
4402 kwds))
4403 (insert "\n"))
4404 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4405 (org-agenda-mark-header-line (point-min))
4406 (when rtnall
4407 (insert (org-agenda-finalize-entries rtnall) "\n"))
4408 (goto-char (point-min))
4409 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4410 (add-text-properties (point-min) (point-max)
4411 `(org-agenda-type todo
4412 org-last-args ,arg
4413 org-redo-cmd ,org-agenda-redo-command
4414 org-serie-cmd ,org-cmd))
4415 (org-agenda-finalize)
4416 (setq buffer-read-only t))))
4418 ;;; Agenda tags match
4420 ;;;###autoload
4421 (defun org-tags-view (&optional todo-only match)
4422 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4423 The prefix arg TODO-ONLY limits the search to TODO entries."
4424 (interactive "P")
4425 (if org-agenda-overriding-arguments
4426 (setq todo-only (car org-agenda-overriding-arguments)
4427 match (nth 1 org-agenda-overriding-arguments)))
4428 (let* ((org-tags-match-list-sublevels
4429 org-tags-match-list-sublevels)
4430 (completion-ignore-case t)
4431 rtn rtnall files file pos matcher
4432 buffer)
4433 (when (and (stringp match) (not (string-match "\\S-" match)))
4434 (setq match nil))
4435 (setq matcher (org-make-tags-matcher match)
4436 match (car matcher) matcher (cdr matcher))
4437 (catch 'exit
4438 (if org-agenda-sticky
4439 (setq org-agenda-buffer-name
4440 (if (stringp match)
4441 (format "*Org Agenda(%s:%s)*"
4442 (or org-keys (or (and todo-only "M") "m")) match)
4443 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4444 (org-agenda-prepare (concat "TAGS " match))
4445 (org-compile-prefix-format 'tags)
4446 (org-set-sorting-strategy 'tags)
4447 (setq org-agenda-query-string match)
4448 (setq org-agenda-redo-command
4449 (list 'org-tags-view `(quote ,todo-only)
4450 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4451 (setq files (org-agenda-files nil 'ifmode)
4452 rtnall nil)
4453 (while (setq file (pop files))
4454 (catch 'nextfile
4455 (org-check-agenda-file file)
4456 (setq buffer (if (file-exists-p file)
4457 (org-get-agenda-file-buffer file)
4458 (error "No such file %s" file)))
4459 (if (not buffer)
4460 ;; If file does not exist, error message to agenda
4461 (setq rtn (list
4462 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4463 rtnall (append rtnall rtn))
4464 (with-current-buffer buffer
4465 (unless (derived-mode-p 'org-mode)
4466 (error "Agenda file %s is not in `org-mode'" file))
4467 (save-excursion
4468 (save-restriction
4469 (if org-agenda-restrict
4470 (narrow-to-region org-agenda-restrict-begin
4471 org-agenda-restrict-end)
4472 (widen))
4473 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4474 (setq rtnall (append rtnall rtn))))))))
4475 (if org-agenda-overriding-header
4476 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4477 nil 'face 'org-agenda-structure) "\n")
4478 (insert "Headlines with TAGS match: ")
4479 (add-text-properties (point-min) (1- (point))
4480 (list 'face 'org-agenda-structure
4481 'short-heading
4482 (concat "Match: " match)))
4483 (setq pos (point))
4484 (insert match "\n")
4485 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4486 (setq pos (point))
4487 (unless org-agenda-multi
4488 (insert "Press `C-u r' to search again with new search string\n"))
4489 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4490 (org-agenda-mark-header-line (point-min))
4491 (when rtnall
4492 (insert (org-agenda-finalize-entries rtnall) "\n"))
4493 (goto-char (point-min))
4494 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4495 (add-text-properties (point-min) (point-max)
4496 `(org-agenda-type tags
4497 org-last-args (,todo-only ,match)
4498 org-redo-cmd ,org-agenda-redo-command
4499 org-serie-cmd ,org-cmd))
4500 (org-agenda-finalize)
4501 (setq buffer-read-only t))))
4503 ;;; Agenda Finding stuck projects
4505 (defvar org-agenda-skip-regexp nil
4506 "Regular expression used in skipping subtrees for the agenda.
4507 This is basically a temporary global variable that can be set and then
4508 used by user-defined selections using `org-agenda-skip-function'.")
4510 (defvar org-agenda-overriding-header nil
4511 "When set during agenda, todo and tags searches it replaces the header.
4512 This variable should not be set directly, but custom commands can bind it
4513 in the options section.")
4515 (defun org-agenda-skip-entry-when-regexp-matches ()
4516 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4517 If yes, it returns the end position of this entry, causing agenda commands
4518 to skip the entry but continuing the search in the subtree. This is a
4519 function that can be put into `org-agenda-skip-function' for the duration
4520 of a command."
4521 (let ((end (save-excursion (org-end-of-subtree t)))
4522 skip)
4523 (save-excursion
4524 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4525 (and skip end)))
4527 (defun org-agenda-skip-subtree-when-regexp-matches ()
4528 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4529 If yes, it returns the end position of this tree, causing agenda commands
4530 to skip this subtree. This is a function that can be put into
4531 `org-agenda-skip-function' for the duration of a command."
4532 (let ((end (save-excursion (org-end-of-subtree t)))
4533 skip)
4534 (save-excursion
4535 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4536 (and skip end)))
4538 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4539 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4540 If yes, it returns the end position of the current entry (NOT the tree),
4541 causing agenda commands to skip the entry but continuing the search in
4542 the subtree. This is a function that can be put into
4543 `org-agenda-skip-function' for the duration of a command. An important
4544 use of this function is for the stuck project list."
4545 (let ((end (save-excursion (org-end-of-subtree t)))
4546 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4547 skip)
4548 (save-excursion
4549 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4550 (and skip entry-end)))
4552 (defun org-agenda-skip-entry-if (&rest conditions)
4553 "Skip entry if any of CONDITIONS is true.
4554 See `org-agenda-skip-if' for details."
4555 (org-agenda-skip-if nil conditions))
4557 (defun org-agenda-skip-subtree-if (&rest conditions)
4558 "Skip entry if any of CONDITIONS is true.
4559 See `org-agenda-skip-if' for details."
4560 (org-agenda-skip-if t conditions))
4562 (defun org-agenda-skip-if (subtree conditions)
4563 "Checks current entity for CONDITIONS.
4564 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4565 the entry (i.e. the text before the next heading) is checked.
4567 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4568 from different tests. Valid conditions are:
4570 scheduled Check if there is a scheduled cookie
4571 notscheduled Check if there is no scheduled cookie
4572 deadline Check if there is a deadline
4573 notdeadline Check if there is no deadline
4574 timestamp Check if there is a timestamp (also deadline or scheduled)
4575 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4576 regexp Check if regexp matches
4577 notregexp Check if regexp does not match.
4578 todo Check if TODO keyword matches
4579 nottodo Check if TODO keyword does not match
4581 The regexp is taken from the conditions list, it must come right after
4582 the `regexp' or `notregexp' element.
4584 `todo' and `nottodo' accept as an argument a list of todo
4585 keywords, which may include \"*\" to match any todo keyword.
4587 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4589 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4591 Instead of a list, a keyword class may be given. For example:
4593 (org-agenda-skip-entry-if 'nottodo 'done)
4595 would skip entries that haven't been marked with any of \"DONE\"
4596 keywords. Possible classes are: `todo', `done', `any'.
4598 If any of these conditions is met, this function returns the end point of
4599 the entity, causing the search to continue from there. This is a function
4600 that can be put into `org-agenda-skip-function' for the duration of a command."
4601 (let (beg end m)
4602 (org-back-to-heading t)
4603 (setq beg (point)
4604 end (if subtree
4605 (progn (org-end-of-subtree t) (point))
4606 (progn (outline-next-heading) (1- (point)))))
4607 (goto-char beg)
4608 (and
4610 (and (memq 'scheduled conditions)
4611 (re-search-forward org-scheduled-time-regexp end t))
4612 (and (memq 'notscheduled conditions)
4613 (not (re-search-forward org-scheduled-time-regexp end t)))
4614 (and (memq 'deadline conditions)
4615 (re-search-forward org-deadline-time-regexp end t))
4616 (and (memq 'notdeadline conditions)
4617 (not (re-search-forward org-deadline-time-regexp end t)))
4618 (and (memq 'timestamp conditions)
4619 (re-search-forward org-ts-regexp end t))
4620 (and (memq 'nottimestamp conditions)
4621 (not (re-search-forward org-ts-regexp end t)))
4622 (and (setq m (memq 'regexp conditions))
4623 (stringp (nth 1 m))
4624 (re-search-forward (nth 1 m) end t))
4625 (and (setq m (memq 'notregexp conditions))
4626 (stringp (nth 1 m))
4627 (not (re-search-forward (nth 1 m) end t)))
4628 (and (or
4629 (setq m (memq 'nottodo conditions))
4630 (setq m (memq 'todo-unblocked conditions))
4631 (setq m (memq 'nottodo-unblocked conditions))
4632 (setq m (memq 'todo conditions)))
4633 (org-agenda-skip-if-todo m end)))
4634 end)))
4636 (defun org-agenda-skip-if-todo (args end)
4637 "Helper function for `org-agenda-skip-if', do not use it directly.
4638 ARGS is a list with first element either `todo', `nottodo',
4639 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4640 a list of TODO keywords, or a state symbol `todo' or `done' or
4641 `any'."
4642 (let ((kw (car args))
4643 (arg (cadr args))
4644 todo-wds todo-re)
4645 (setq todo-wds
4646 (org-uniquify
4647 (cond
4648 ((listp arg) ;; list of keywords
4649 (if (member "*" arg)
4650 (mapcar 'substring-no-properties org-todo-keywords-1)
4651 arg))
4652 ((symbolp arg) ;; keyword class name
4653 (cond
4654 ((eq arg 'todo)
4655 (org-delete-all org-done-keywords
4656 (mapcar 'substring-no-properties
4657 org-todo-keywords-1)))
4658 ((eq arg 'done) org-done-keywords)
4659 ((eq arg 'any)
4660 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4661 (setq todo-re
4662 (concat "^\\*+[ \t]+\\<\\("
4663 (mapconcat 'identity todo-wds "\\|")
4664 "\\)\\>"))
4665 (cond
4666 ((eq kw 'todo) (re-search-forward todo-re end t))
4667 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
4668 ((eq kw 'todo-unblocked)
4669 (catch 'unblocked
4670 (while (re-search-forward todo-re end t)
4671 (or (org-entry-blocked-p) (throw 'unblocked t)))
4672 nil))
4673 ((eq kw 'nottodo-unblocked)
4674 (catch 'unblocked
4675 (while (re-search-forward todo-re end t)
4676 (or (org-entry-blocked-p) (throw 'unblocked nil)))
4680 ;;;###autoload
4681 (defun org-agenda-list-stuck-projects (&rest ignore)
4682 "Create agenda view for projects that are stuck.
4683 Stuck projects are project that have no next actions. For the definitions
4684 of what a project is and how to check if it stuck, customize the variable
4685 `org-stuck-projects'."
4686 (interactive)
4687 (let* ((org-agenda-skip-function
4688 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4689 ;; We could have used org-agenda-skip-if here.
4690 (org-agenda-overriding-header
4691 (or org-agenda-overriding-header "List of stuck projects: "))
4692 (matcher (nth 0 org-stuck-projects))
4693 (todo (nth 1 org-stuck-projects))
4694 (todo-wds (if (member "*" todo)
4695 (progn
4696 (org-agenda-prepare-buffers (org-agenda-files
4697 nil 'ifmode))
4698 (org-delete-all
4699 org-done-keywords-for-agenda
4700 (copy-sequence org-todo-keywords-for-agenda)))
4701 todo))
4702 (todo-re (concat "^\\*+[ \t]+\\("
4703 (mapconcat 'identity todo-wds "\\|")
4704 "\\)\\>"))
4705 (tags (nth 2 org-stuck-projects))
4706 (tags-re (if (member "*" tags)
4707 (concat org-outline-regexp-bol
4708 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4709 (if tags
4710 (concat org-outline-regexp-bol
4711 ".*:\\("
4712 (mapconcat 'identity tags "\\|")
4713 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4714 (gen-re (nth 3 org-stuck-projects))
4715 (re-list
4716 (delq nil
4717 (list
4718 (if todo todo-re)
4719 (if tags tags-re)
4720 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4721 gen-re)))))
4722 (setq org-agenda-skip-regexp
4723 (if re-list
4724 (mapconcat 'identity re-list "\\|")
4725 (error "No information how to identify unstuck projects")))
4726 (org-tags-view nil matcher)
4727 (with-current-buffer org-agenda-buffer-name
4728 (setq org-agenda-redo-command
4729 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
4731 ;;; Diary integration
4733 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4734 (defvar diary-list-entries-hook)
4735 (defvar diary-time-regexp)
4736 (defun org-get-entries-from-diary (date)
4737 "Get the (Emacs Calendar) diary entries for DATE."
4738 (require 'diary-lib)
4739 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4740 (diary-display-hook '(fancy-diary-display))
4741 (diary-display-function 'fancy-diary-display)
4742 (pop-up-frames nil)
4743 (diary-list-entries-hook
4744 (cons 'org-diary-default-entry diary-list-entries-hook))
4745 (diary-file-name-prefix-function nil) ; turn this feature off
4746 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4747 entries
4748 (org-disable-agenda-to-diary t))
4749 (save-excursion
4750 (save-window-excursion
4751 (funcall (if (fboundp 'diary-list-entries)
4752 'diary-list-entries 'list-diary-entries)
4753 date 1)))
4754 (if (not (get-buffer diary-fancy-buffer))
4755 (setq entries nil)
4756 (with-current-buffer diary-fancy-buffer
4757 (setq buffer-read-only nil)
4758 (if (zerop (buffer-size))
4759 ;; No entries
4760 (setq entries nil)
4761 ;; Omit the date and other unnecessary stuff
4762 (org-agenda-cleanup-fancy-diary)
4763 ;; Add prefix to each line and extend the text properties
4764 (if (zerop (buffer-size))
4765 (setq entries nil)
4766 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4767 (setq entries
4768 (with-temp-buffer
4769 (insert entries) (goto-char (point-min))
4770 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4771 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4772 (replace-match (concat "; " (match-string 1)))))
4773 (buffer-string)))))
4774 (set-buffer-modified-p nil)
4775 (kill-buffer diary-fancy-buffer)))
4776 (when entries
4777 (setq entries (org-split-string entries "\n"))
4778 (setq entries
4779 (mapcar
4780 (lambda (x)
4781 (setq x (org-agenda-format-item "" x "Diary" nil 'time))
4782 ;; Extend the text properties to the beginning of the line
4783 (org-add-props x (text-properties-at (1- (length x)) x)
4784 'type "diary" 'date date 'face 'org-agenda-diary))
4785 entries)))))
4787 (defvar org-agenda-cleanup-fancy-diary-hook nil
4788 "Hook run when the fancy diary buffer is cleaned up.")
4790 (defun org-agenda-cleanup-fancy-diary ()
4791 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4792 This gets rid of the date, the underline under the date, and
4793 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4794 date. It also removes lines that contain only whitespace."
4795 (goto-char (point-min))
4796 (if (looking-at ".*?:[ \t]*")
4797 (progn
4798 (replace-match "")
4799 (re-search-forward "\n=+$" nil t)
4800 (replace-match "")
4801 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4802 (re-search-forward "\n=+$" nil t)
4803 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4804 (goto-char (point-min))
4805 (while (re-search-forward "^ +\n" nil t)
4806 (replace-match ""))
4807 (goto-char (point-min))
4808 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4809 (replace-match ""))
4810 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4812 ;; Make sure entries from the diary have the right text properties.
4813 (eval-after-load "diary-lib"
4814 '(if (boundp 'diary-modify-entry-list-string-function)
4815 ;; We can rely on the hook, nothing to do
4817 ;; Hook not available, must use advice to make this work
4818 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4819 "Make the position visible."
4820 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4821 (stringp string)
4822 buffer-file-name)
4823 (setq string (org-modify-diary-entry-string string))))))
4825 (defun org-modify-diary-entry-string (string)
4826 "Add text properties to string, allowing org-mode to act on it."
4827 (org-add-props string nil
4828 'mouse-face 'highlight
4829 'help-echo (if buffer-file-name
4830 (format "mouse-2 or RET jump to diary file %s"
4831 (abbreviate-file-name buffer-file-name))
4833 'org-agenda-diary-link t
4834 'org-marker (org-agenda-new-marker (point-at-bol))))
4836 (defun org-diary-default-entry ()
4837 "Add a dummy entry to the diary.
4838 Needed to avoid empty dates which mess up holiday display."
4839 ;; Catch the error if dealing with the new add-to-diary-alist
4840 (when org-disable-agenda-to-diary
4841 (condition-case nil
4842 (org-add-to-diary-list original-date "Org-mode dummy" "")
4843 (error
4844 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4846 (defun org-add-to-diary-list (&rest args)
4847 (if (fboundp 'diary-add-to-list)
4848 (apply 'diary-add-to-list args)
4849 (apply 'add-to-diary-list args)))
4851 (defvar org-diary-last-run-time nil)
4853 ;;;###autoload
4854 (defun org-diary (&rest args)
4855 "Return diary information from org files.
4856 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4857 It accesses org files and extracts information from those files to be
4858 listed in the diary. The function accepts arguments specifying what
4859 items should be listed. For a list of arguments allowed here, see the
4860 variable `org-agenda-entry-types'.
4862 The call in the diary file should look like this:
4864 &%%(org-diary) ~/path/to/some/orgfile.org
4866 Use a separate line for each org file to check. Or, if you omit the file name,
4867 all files listed in `org-agenda-files' will be checked automatically:
4869 &%%(org-diary)
4871 If you don't give any arguments (as in the example above), the default
4872 arguments (:deadline :scheduled :timestamp :sexp) are used.
4873 So the example above may also be written as
4875 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4877 The function expects the lisp variables `entry' and `date' to be provided
4878 by the caller, because this is how the calendar works. Don't use this
4879 function from a program - use `org-agenda-get-day-entries' instead."
4880 (when (> (- (org-float-time)
4881 org-agenda-last-marker-time)
4883 ;; I am not sure if this works with sticky agendas, because the marker
4884 ;; list is then no longer a global variable.
4885 (org-agenda-reset-markers))
4886 (org-compile-prefix-format 'agenda)
4887 (org-set-sorting-strategy 'agenda)
4888 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4889 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4890 (list entry)
4891 (org-agenda-files t)))
4892 (time (org-float-time))
4893 file rtn results)
4894 (when (or (not org-diary-last-run-time)
4895 (> (- time
4896 org-diary-last-run-time)
4898 (org-agenda-prepare-buffers files))
4899 (setq org-diary-last-run-time time)
4900 ;; If this is called during org-agenda, don't return any entries to
4901 ;; the calendar. Org Agenda will list these entries itself.
4902 (if org-disable-agenda-to-diary (setq files nil))
4903 (while (setq file (pop files))
4904 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4905 (setq results (append results rtn)))
4906 (if results
4907 (concat (org-agenda-finalize-entries results) "\n"))))
4909 ;;; Agenda entry finders
4911 (defun org-agenda-get-day-entries (file date &rest args)
4912 "Does the work for `org-diary' and `org-agenda'.
4913 FILE is the path to a file to be checked for entries. DATE is date like
4914 the one returned by `calendar-current-date'. ARGS are symbols indicating
4915 which kind of entries should be extracted. For details about these, see
4916 the documentation of `org-diary'."
4917 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4918 (let* ((org-startup-folded nil)
4919 (org-startup-align-all-tables nil)
4920 (buffer (if (file-exists-p file)
4921 (org-get-agenda-file-buffer file)
4922 (error "No such file %s" file)))
4923 arg results rtn deadline-results)
4924 (if (not buffer)
4925 ;; If file does not exist, make sure an error message ends up in diary
4926 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4927 (with-current-buffer buffer
4928 (unless (derived-mode-p 'org-mode)
4929 (error "Agenda file %s is not in `org-mode'" file))
4930 (setq org-agenda-buffer (or org-agenda-buffer buffer))
4931 (let ((case-fold-search nil))
4932 (save-excursion
4933 (save-restriction
4934 (if org-agenda-restrict
4935 (narrow-to-region org-agenda-restrict-begin
4936 org-agenda-restrict-end)
4937 (widen))
4938 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4939 (while (setq arg (pop args))
4940 (cond
4941 ((and (eq arg :todo)
4942 (equal date (calendar-gregorian-from-absolute
4943 (org-today))))
4944 (setq rtn (org-agenda-get-todos))
4945 (setq results (append results rtn)))
4946 ((eq arg :timestamp)
4947 (setq rtn (org-agenda-get-blocks))
4948 (setq results (append results rtn))
4949 (setq rtn (org-agenda-get-timestamps deadline-results))
4950 (setq results (append results rtn)))
4951 ((eq arg :sexp)
4952 (setq rtn (org-agenda-get-sexps))
4953 (setq results (append results rtn)))
4954 ((eq arg :scheduled)
4955 (setq rtn (org-agenda-get-scheduled deadline-results))
4956 (setq results (append results rtn)))
4957 ((eq arg :closed)
4958 (setq rtn (org-agenda-get-progress))
4959 (setq results (append results rtn)))
4960 ((eq arg :deadline)
4961 (setq rtn (org-agenda-get-deadlines))
4962 (setq deadline-results (copy-sequence rtn))
4963 (setq results (append results rtn))))))))
4964 results))))
4966 (defvar org-heading-keyword-regexp-format) ; defined in org.el
4967 (defun org-agenda-get-todos ()
4968 "Return the TODO information for agenda display."
4969 (let* ((props (list 'face nil
4970 'done-face 'org-agenda-done
4971 'org-not-done-regexp org-not-done-regexp
4972 'org-todo-regexp org-todo-regexp
4973 'org-complex-heading-regexp org-complex-heading-regexp
4974 'mouse-face 'highlight
4975 'help-echo
4976 (format "mouse-2 or RET jump to org file %s"
4977 (abbreviate-file-name buffer-file-name))))
4978 (regexp (format org-heading-keyword-regexp-format
4979 (cond
4980 ((and org-select-this-todo-keyword
4981 (equal org-select-this-todo-keyword "*"))
4982 org-todo-regexp)
4983 (org-select-this-todo-keyword
4984 (concat "\\("
4985 (mapconcat 'identity
4986 (org-split-string
4987 org-select-this-todo-keyword
4988 "|")
4989 "\\|") "\\)"))
4990 (t org-not-done-regexp))))
4991 marker priority category category-pos tags todo-state
4992 ee txt beg end)
4993 (goto-char (point-min))
4994 (while (re-search-forward regexp nil t)
4995 (catch :skip
4996 (save-match-data
4997 (beginning-of-line)
4998 (org-agenda-skip)
4999 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5000 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5001 (goto-char (1+ beg))
5002 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5003 (throw :skip nil)))
5004 (goto-char (match-beginning 2))
5005 (setq marker (org-agenda-new-marker (match-beginning 0))
5006 category (org-get-category)
5007 category-pos (get-text-property (point) 'org-category-position)
5008 txt (org-trim
5009 (buffer-substring (match-beginning 2) (match-end 0)))
5010 tags (org-get-tags-at (point))
5011 txt (org-agenda-format-item "" txt category tags t)
5012 priority (1+ (org-get-priority txt))
5013 todo-state (org-get-todo-state))
5014 (org-add-props txt props
5015 'org-marker marker 'org-hd-marker marker
5016 'priority priority 'org-category category
5017 'org-category-position category-pos
5018 'type "todo" 'todo-state todo-state)
5019 (push txt ee)
5020 (if org-agenda-todo-list-sublevels
5021 (goto-char (match-end 2))
5022 (org-end-of-subtree 'invisible))))
5023 (nreverse ee)))
5025 (defun org-agenda-todo-custom-ignore-p (time n)
5026 "Check whether timestamp is farther away than n number of days.
5027 This function is invoked if `org-agenda-todo-ignore-deadlines',
5028 `org-agenda-todo-ignore-scheduled' or
5029 `org-agenda-todo-ignore-timestamp' is set to an integer."
5030 (let ((days (org-days-to-time time)))
5031 (if (>= n 0)
5032 (>= days n)
5033 (<= days n))))
5035 ;;;###autoload
5036 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5037 (&optional end)
5038 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5039 (when (or org-agenda-todo-ignore-with-date
5040 org-agenda-todo-ignore-scheduled
5041 org-agenda-todo-ignore-deadlines
5042 org-agenda-todo-ignore-timestamp)
5043 (setq end (or end (save-excursion (outline-next-heading) (point))))
5044 (save-excursion
5045 (or (and org-agenda-todo-ignore-with-date
5046 (re-search-forward org-ts-regexp end t))
5047 (and org-agenda-todo-ignore-scheduled
5048 (re-search-forward org-scheduled-time-regexp end t)
5049 (cond
5050 ((eq org-agenda-todo-ignore-scheduled 'future)
5051 (> (org-days-to-time (match-string 1)) 0))
5052 ((eq org-agenda-todo-ignore-scheduled 'past)
5053 (<= (org-days-to-time (match-string 1)) 0))
5054 ((numberp org-agenda-todo-ignore-scheduled)
5055 (org-agenda-todo-custom-ignore-p
5056 (match-string 1) org-agenda-todo-ignore-scheduled))
5057 (t)))
5058 (and org-agenda-todo-ignore-deadlines
5059 (re-search-forward org-deadline-time-regexp end t)
5060 (cond
5061 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5062 ((eq org-agenda-todo-ignore-deadlines 'far)
5063 (not (org-deadline-close (match-string 1))))
5064 ((eq org-agenda-todo-ignore-deadlines 'future)
5065 (> (org-days-to-time (match-string 1)) 0))
5066 ((eq org-agenda-todo-ignore-deadlines 'past)
5067 (<= (org-days-to-time (match-string 1)) 0))
5068 ((numberp org-agenda-todo-ignore-deadlines)
5069 (org-agenda-todo-custom-ignore-p
5070 (match-string 1) org-agenda-todo-ignore-deadlines))
5071 (t (org-deadline-close (match-string 1)))))
5072 (and org-agenda-todo-ignore-timestamp
5073 (let ((buffer (current-buffer))
5074 (regexp
5075 (concat
5076 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5077 (start (point)))
5078 ;; Copy current buffer into a temporary one
5079 (with-temp-buffer
5080 (insert-buffer-substring buffer start end)
5081 (goto-char (point-min))
5082 ;; Delete SCHEDULED and DEADLINE items
5083 (while (re-search-forward regexp end t)
5084 (delete-region (match-beginning 0) (match-end 0)))
5085 (goto-char (point-min))
5086 ;; No search for timestamp left
5087 (when (re-search-forward org-ts-regexp nil t)
5088 (cond
5089 ((eq org-agenda-todo-ignore-timestamp 'future)
5090 (> (org-days-to-time (match-string 1)) 0))
5091 ((eq org-agenda-todo-ignore-timestamp 'past)
5092 (<= (org-days-to-time (match-string 1)) 0))
5093 ((numberp org-agenda-todo-ignore-timestamp)
5094 (org-agenda-todo-custom-ignore-p
5095 (match-string 1) org-agenda-todo-ignore-timestamp))
5096 (t))))))))))
5098 (defconst org-agenda-no-heading-message
5099 "No heading for this item in buffer or region.")
5101 (defun org-agenda-get-timestamps (&optional deadline-results)
5102 "Return the date stamp information for agenda display."
5103 (let* ((props (list 'face 'org-agenda-calendar-event
5104 'org-not-done-regexp org-not-done-regexp
5105 'org-todo-regexp org-todo-regexp
5106 'org-complex-heading-regexp org-complex-heading-regexp
5107 'mouse-face 'highlight
5108 'help-echo
5109 (format "mouse-2 or RET jump to org file %s"
5110 (abbreviate-file-name buffer-file-name))))
5111 (d1 (calendar-absolute-from-gregorian date))
5113 (deadline-position-alist
5114 (mapcar (lambda (a) (and (setq mm (get-text-property
5115 0 'org-hd-marker a))
5116 (cons (marker-position mm) a)))
5117 deadline-results))
5118 (remove-re org-ts-regexp)
5119 (regexp
5120 (concat
5121 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5122 (regexp-quote
5123 (substring
5124 (format-time-string
5125 (car org-time-stamp-formats)
5126 (apply 'encode-time ; DATE bound by calendar
5127 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5128 1 11))
5129 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5130 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5131 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5132 donep tmp priority category category-pos ee txt timestr tags
5133 b0 b3 e3 head todo-state end-of-match show-all warntime)
5134 (goto-char (point-min))
5135 (while (setq end-of-match (re-search-forward regexp nil t))
5136 (setq b0 (match-beginning 0)
5137 b3 (match-beginning 3) e3 (match-end 3)
5138 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5139 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5140 (member todo-state
5141 org-agenda-repeating-timestamp-show-all)))
5142 (catch :skip
5143 (and (org-at-date-range-p) (throw :skip nil))
5144 (org-agenda-skip)
5145 (if (and (match-end 1)
5146 (not (= d1 (org-time-string-to-absolute
5147 (match-string 1) d1 nil show-all
5148 (current-buffer) b0))))
5149 (throw :skip nil))
5150 (if (and e3
5151 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5152 (throw :skip nil))
5153 (setq tmp (buffer-substring (max (point-min)
5154 (- b0 org-ds-keyword-length))
5156 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5157 inactivep (= (char-after b0) ?\[)
5158 deadlinep (string-match org-deadline-regexp tmp)
5159 scheduledp (string-match org-scheduled-regexp tmp)
5160 closedp (and org-agenda-include-inactive-timestamps
5161 (string-match org-closed-string tmp))
5162 clockp (and org-agenda-include-inactive-timestamps
5163 (or (string-match org-clock-string tmp)
5164 (string-match "]-+\\'" tmp)))
5165 warntime (org-entry-get (point) "APPT_WARNTIME")
5166 donep (member todo-state org-done-keywords))
5167 (if (or scheduledp deadlinep closedp clockp
5168 (and donep org-agenda-skip-timestamp-if-done))
5169 (throw :skip t))
5170 (if (string-match ">" timestr)
5171 ;; substring should only run to end of time stamp
5172 (setq timestr (substring timestr 0 (match-end 0))))
5173 (setq marker (org-agenda-new-marker b0)
5174 category (org-get-category b0)
5175 category-pos (get-text-property b0 'org-category-position))
5176 (save-excursion
5177 (if (not (re-search-backward org-outline-regexp-bol nil t))
5178 (setq txt org-agenda-no-heading-message)
5179 (goto-char (match-beginning 0))
5180 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5181 (assoc (point) deadline-position-alist))
5182 (throw :skip nil))
5183 (setq hdmarker (org-agenda-new-marker)
5184 tags (org-get-tags-at))
5185 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5186 (setq head (or (match-string 1) ""))
5187 (setq txt (org-agenda-format-item
5188 (if inactivep org-agenda-inactive-leader nil)
5189 head category tags timestr
5190 remove-re t)))
5191 (setq priority (org-get-priority txt))
5192 (org-add-props txt props
5193 'org-marker marker 'org-hd-marker hdmarker)
5194 (org-add-props txt nil 'priority priority
5195 'org-category category 'date date
5196 'org-category-position category-pos
5197 'todo-state todo-state
5198 'warntime warntime
5199 'type "timestamp")
5200 (push txt ee))
5201 (if org-agenda-skip-additional-timestamps-same-entry
5202 (outline-next-heading)
5203 (goto-char end-of-match))))
5204 (nreverse ee)))
5206 (defun org-agenda-get-sexps ()
5207 "Return the sexp information for agenda display."
5208 (require 'diary-lib)
5209 (let* ((props (list 'face 'org-agenda-calendar-sexp
5210 'mouse-face 'highlight
5211 'help-echo
5212 (format "mouse-2 or RET jump to org file %s"
5213 (abbreviate-file-name buffer-file-name))))
5214 (regexp "^&?%%(")
5215 marker category extra category-pos ee txt tags entry
5216 result beg b sexp sexp-entry todo-state warntime)
5217 (goto-char (point-min))
5218 (while (re-search-forward regexp nil t)
5219 (catch :skip
5220 (org-agenda-skip)
5221 (setq beg (match-beginning 0))
5222 (goto-char (1- (match-end 0)))
5223 (setq b (point))
5224 (forward-sexp 1)
5225 (setq sexp (buffer-substring b (point)))
5226 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5227 (org-trim (match-string 1))
5228 ""))
5229 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5230 (when result
5231 (setq marker (org-agenda-new-marker beg)
5232 category (org-get-category beg)
5233 category-pos (get-text-property beg 'org-category-position)
5234 tags (save-excursion (org-backward-heading-same-level 0)
5235 (org-get-tags-at))
5236 todo-state (org-get-todo-state)
5237 warntime (org-entry-get (point) "APPT_WARNTIME"))
5239 (dolist (r (if (stringp result)
5240 (list result)
5241 result)) ;; we expect a list here
5242 (when (and org-agenda-diary-sexp-prefix
5243 (string-match org-agenda-diary-sexp-prefix r))
5244 (setq extra (match-string 0 r)
5245 r (replace-match "" nil nil r)))
5246 (if (string-match "\\S-" r)
5247 (setq txt r)
5248 (setq txt "SEXP entry returned empty string"))
5250 (setq txt (org-agenda-format-item
5251 extra txt category tags 'time))
5252 (org-add-props txt props 'org-marker marker)
5253 (org-add-props txt nil
5254 'org-category category 'date date 'todo-state todo-state
5255 'org-category-position category-pos 'tags tags
5256 'type "sexp" 'warntime warntime)
5257 (push txt ee)))))
5258 (nreverse ee)))
5260 ;; Calendar sanity: define some functions that are independent of
5261 ;; `calendar-date-style'.
5262 ;; Normally I would like to use ISO format when calling the diary functions,
5263 ;; but to make sure we still have Emacs 22 compatibility we bind
5264 ;; also `european-calendar-style' and use european format
5265 (defun org-anniversary (year month day &optional mark)
5266 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5267 (org-no-warnings
5268 (let ((calendar-date-style 'european) (european-calendar-style t))
5269 (diary-anniversary day month year mark))))
5270 (defun org-cyclic (N year month day &optional mark)
5271 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5272 (org-no-warnings
5273 (let ((calendar-date-style 'european) (european-calendar-style t))
5274 (diary-cyclic N day month year mark))))
5275 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5276 "Like `diary-block', but with fixed (ISO) order of arguments."
5277 (org-no-warnings
5278 (let ((calendar-date-style 'european) (european-calendar-style t))
5279 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5280 (defun org-date (year month day &optional mark)
5281 "Like `diary-date', but with fixed (ISO) order of arguments."
5282 (org-no-warnings
5283 (let ((calendar-date-style 'european) (european-calendar-style t))
5284 (diary-date day month year mark))))
5285 (defalias 'org-float 'diary-float)
5287 ;; Define the` org-class' function
5288 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5289 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5290 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5291 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5292 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5293 `holidays', then any date that is known by the Emacs calendar to be a
5294 holiday will also be skipped."
5295 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5296 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5297 (d (calendar-absolute-from-gregorian date)))
5298 (and
5299 (<= date1 d)
5300 (<= d date2)
5301 (= (calendar-day-of-week date) dayname)
5302 (or (not skip-weeks)
5303 (progn
5304 (require 'cal-iso)
5305 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5306 (not (and (memq 'holidays skip-weeks)
5307 (calendar-check-holidays date)))
5308 entry)))
5310 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5311 "Like `org-class', but honor `calendar-date-style'.
5312 The order of the first 2 times 3 arguments depends on the variable
5313 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5314 So for American calendars, give this as MONTH DAY YEAR, for European as
5315 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5316 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5317 is any number of ISO weeks in the block period for which the item should
5318 be skipped.
5320 This function is here only for backward compatibility and it is deprecated,
5321 please use `org-class' instead."
5322 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5323 (date2 (org-order-calendar-date-args m2 d2 y2)))
5324 (org-class
5325 (nth 2 date1) (car date1) (nth 1 date1)
5326 (nth 2 date2) (car date2) (nth 1 date2)
5327 dayname skip-weeks)))
5328 (make-obsolete 'org-diary-class 'org-class "")
5330 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5331 (defalias 'org-get-closed 'org-agenda-get-progress)
5332 (defun org-agenda-get-progress ()
5333 "Return the logged TODO entries for agenda display."
5334 (let* ((props (list 'mouse-face 'highlight
5335 'org-not-done-regexp org-not-done-regexp
5336 'org-todo-regexp org-todo-regexp
5337 'org-complex-heading-regexp org-complex-heading-regexp
5338 'help-echo
5339 (format "mouse-2 or RET jump to org file %s"
5340 (abbreviate-file-name buffer-file-name))))
5341 (items (if (consp org-agenda-show-log-scoped)
5342 org-agenda-show-log-scoped
5343 (if (eq org-agenda-show-log-scoped 'clockcheck)
5344 '(clock)
5345 org-agenda-log-mode-items)))
5346 (parts
5347 (delq nil
5348 (list
5349 (if (memq 'closed items) (concat "\\<" org-closed-string))
5350 (if (memq 'clock items) (concat "\\<" org-clock-string))
5351 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5352 (parts-re (if parts (mapconcat 'identity parts "\\|")
5353 (error "`org-agenda-log-mode-items' is empty")))
5354 (regexp (concat
5355 "\\(" parts-re "\\)"
5356 " *\\["
5357 (regexp-quote
5358 (substring
5359 (format-time-string
5360 (car org-time-stamp-formats)
5361 (apply 'encode-time ; DATE bound by calendar
5362 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5363 1 11))))
5364 (org-agenda-search-headline-for-time nil)
5365 marker hdmarker priority category category-pos tags closedp
5366 statep clockp state ee txt extra timestr rest clocked)
5367 (goto-char (point-min))
5368 (while (re-search-forward regexp nil t)
5369 (catch :skip
5370 (org-agenda-skip)
5371 (setq marker (org-agenda-new-marker (match-beginning 0))
5372 closedp (equal (match-string 1) org-closed-string)
5373 statep (equal (string-to-char (match-string 1)) ?-)
5374 clockp (not (or closedp statep))
5375 state (and statep (match-string 2))
5376 category (org-get-category (match-beginning 0))
5377 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5378 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5379 (when (string-match "\\]" timestr)
5380 ;; substring should only run to end of time stamp
5381 (setq rest (substring timestr (match-end 0))
5382 timestr (substring timestr 0 (match-end 0)))
5383 (if (and (not closedp) (not statep)
5384 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5385 rest))
5386 (progn (setq timestr (concat (substring timestr 0 -1)
5387 "-" (match-string 1 rest) "]"))
5388 (setq clocked (match-string 2 rest)))
5389 (setq clocked "-")))
5390 (save-excursion
5391 (setq extra
5392 (cond
5393 ((not org-agenda-log-mode-add-notes) nil)
5394 (statep
5395 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5396 (match-string 1)))
5397 (clockp
5398 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5399 (match-string 1)))))
5400 (if (not (re-search-backward org-outline-regexp-bol nil t))
5401 (setq txt org-agenda-no-heading-message)
5402 (goto-char (match-beginning 0))
5403 (setq hdmarker (org-agenda-new-marker)
5404 tags (org-get-tags-at))
5405 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5406 (setq txt (match-string 1))
5407 (when extra
5408 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5409 (setq txt (concat (substring txt 0 (match-beginning 1))
5410 " - " extra " " (match-string 2 txt)))
5411 (setq txt (concat txt " - " extra))))
5412 (setq txt (org-agenda-format-item
5413 (cond
5414 (closedp "Closed: ")
5415 (statep (concat "State: (" state ")"))
5416 (t (concat "Clocked: (" clocked ")")))
5417 txt category tags timestr)))
5418 (setq priority 100000)
5419 (org-add-props txt props
5420 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5421 'priority priority 'org-category category
5422 'org-category-position category-pos
5423 'type "closed" 'date date
5424 'undone-face 'org-warning 'done-face 'org-agenda-done)
5425 (push txt ee))
5426 (goto-char (point-at-eol))))
5427 (nreverse ee)))
5429 (defun org-agenda-show-clocking-issues ()
5430 "Add overlays, showing issues with clocking.
5431 See also the user option `org-agenda-clock-consistency-checks'."
5432 (interactive)
5433 (let* ((pl org-agenda-clock-consistency-checks)
5434 (re (concat "^[ \t]*"
5435 org-clock-string
5436 "[ \t]+"
5437 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5438 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5439 (tlstart 0.)
5440 (tlend 0.)
5441 (maxtime (org-hh:mm-string-to-minutes
5442 (or (plist-get pl :max-duration) "24:00")))
5443 (mintime (org-hh:mm-string-to-minutes
5444 (or (plist-get pl :min-duration) 0)))
5445 (maxgap (org-hh:mm-string-to-minutes
5446 ;; default 30:00 means never complain
5447 (or (plist-get pl :max-gap) "30:00")))
5448 (gapok (mapcar 'org-hh:mm-string-to-minutes
5449 (plist-get pl :gap-ok-around)))
5450 (def-face (or (plist-get pl :default-face)
5451 '((:background "DarkRed") (:foreground "white"))))
5452 issue face m te ts dt ov)
5453 (goto-char (point-min))
5454 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5455 (setq issue nil face def-face)
5456 (catch 'next
5457 (setq m (org-get-at-bol 'org-marker)
5458 te nil ts nil)
5459 (unless (and m (markerp m))
5460 (setq issue "No valid clock line") (throw 'next t))
5461 (org-with-point-at m
5462 (save-excursion
5463 (goto-char (point-at-bol))
5464 (unless (looking-at re)
5465 (error "No valid Clock line")
5466 (throw 'next t))
5467 (unless (match-end 3)
5468 (setq issue "No end time"
5469 face (or (plist-get pl :no-end-time-face) face))
5470 (throw 'next t))
5471 (setq ts (match-string 1)
5472 te (match-string 3)
5473 ts (org-float-time
5474 (apply 'encode-time (org-parse-time-string ts)))
5475 te (org-float-time
5476 (apply 'encode-time (org-parse-time-string te)))
5477 dt (- te ts))))
5478 (cond
5479 ((> dt (* 60 maxtime))
5480 ;; a very long clocking chunk
5481 (setq issue (format "Clocking interval is very long: %s"
5482 (org-minutes-to-hh:mm-string
5483 (floor (/ (float dt) 60.))))
5484 face (or (plist-get pl :long-face) face)))
5485 ((< dt (* 60 mintime))
5486 ;; a very short clocking chunk
5487 (setq issue (format "Clocking interval is very short: %s"
5488 (org-minutes-to-hh:mm-string
5489 (floor (/ (float dt) 60.))))
5490 face (or (plist-get pl :short-face) face)))
5491 ((and (> tlend 0) (< ts tlend))
5492 ;; Two clock entries are overlapping
5493 (setq issue (format "Clocking overlap: %d minutes"
5494 (/ (- tlend ts) 60))
5495 face (or (plist-get pl :overlap-face) face)))
5496 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5497 ;; There is a gap, lets see if we need to report it
5498 (unless (org-agenda-check-clock-gap tlend ts gapok)
5499 (setq issue (format "Clocking gap: %d minutes"
5500 (/ (- ts tlend) 60))
5501 face (or (plist-get pl :gap-face) face))))
5502 (t nil)))
5503 (setq tlend (or te tlend) tlstart (or ts tlstart))
5504 (when issue
5505 ;; OK, there was some issue, add an overlay to show the issue
5506 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5507 (overlay-put ov 'before-string
5508 (concat
5509 (org-add-props
5510 (format "%-43s" (concat " " issue))
5512 'face face)
5513 "\n"))
5514 (overlay-put ov 'evaporate t)))))
5516 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5517 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5518 (catch 'exit
5519 (unless ok-list
5520 ;; there are no OK times for gaps...
5521 (throw 'exit nil))
5522 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5523 ;; This is more than 24 hours, so it is OK.
5524 ;; because we have at least one OK time, that must be in the
5525 ;; 24 hour interval.
5526 (throw 'exit t))
5527 ;; We have a shorter gap.
5528 ;; Now we have to get the minute of the day when these times are
5529 (let* ((t1dec (decode-time (seconds-to-time t1)))
5530 (t2dec (decode-time (seconds-to-time t2)))
5531 ;; compute the minute on the day
5532 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5533 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5534 (when (< min2 min1)
5535 ;; if min2 is smaller than min1, this means it is on the next day.
5536 ;; Wrap it to after midnight.
5537 (setq min2 (+ min2 1440)))
5538 ;; Now check if any of the OK times is in the gap
5539 (mapc (lambda (x)
5540 ;; Wrap the time to after midnight if necessary
5541 (if (< x min1) (setq x (+ x 1440)))
5542 ;; Check if in interval
5543 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5544 ok-list)
5545 ;; Nope, this gap is not OK
5546 nil)))
5548 (defun org-agenda-get-deadlines ()
5549 "Return the deadline information for agenda display."
5550 (let* ((props (list 'mouse-face 'highlight
5551 'org-not-done-regexp org-not-done-regexp
5552 'org-todo-regexp org-todo-regexp
5553 'org-complex-heading-regexp org-complex-heading-regexp
5554 'help-echo
5555 (format "mouse-2 or RET jump to org file %s"
5556 (abbreviate-file-name buffer-file-name))))
5557 (regexp org-deadline-time-regexp)
5558 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5559 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5560 d2 diff dfrac wdays pos pos1 category category-pos
5561 tags suppress-prewarning ee txt head face s todo-state
5562 show-all upcomingp donep timestr warntime)
5563 (goto-char (point-min))
5564 (while (re-search-forward regexp nil t)
5565 (setq suppress-prewarning nil)
5566 (catch :skip
5567 (org-agenda-skip)
5568 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5569 (save-match-data
5570 (string-match org-scheduled-time-regexp
5571 (buffer-substring (point-at-bol)
5572 (point-at-eol)))))
5573 (setq suppress-prewarning
5574 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5575 org-agenda-skip-deadline-prewarning-if-scheduled
5576 0)))
5577 (setq s (match-string 1)
5578 txt nil
5579 pos (1- (match-beginning 1))
5580 todo-state (save-match-data (org-get-todo-state))
5581 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5582 (member todo-state
5583 org-agenda-repeating-timestamp-show-all))
5584 d2 (org-time-string-to-absolute
5585 (match-string 1) d1 'past show-all
5586 (current-buffer) pos)
5587 diff (- d2 d1)
5588 wdays (if suppress-prewarning
5589 (let ((org-deadline-warning-days suppress-prewarning))
5590 (org-get-wdays s))
5591 (org-get-wdays s))
5592 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
5593 upcomingp (and todayp (> diff 0)))
5594 ;; When to show a deadline in the calendar:
5595 ;; If the expiration is within wdays warning time.
5596 ;; Past-due deadlines are only shown on the current date
5597 (if (and (or (and (<= diff wdays)
5598 (and todayp (not org-agenda-only-exact-dates)))
5599 (= diff 0)))
5600 (save-excursion
5601 ;; (setq todo-state (org-get-todo-state))
5602 (setq donep (member todo-state org-done-keywords))
5603 (if (and donep
5604 (or org-agenda-skip-deadline-if-done
5605 (not (= diff 0))))
5606 (setq txt nil)
5607 (setq category (org-get-category)
5608 warntime (org-entry-get (point) "APPT_WARNTIME")
5609 category-pos (get-text-property (point) 'org-category-position))
5610 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5611 (setq txt org-agenda-no-heading-message)
5612 (goto-char (match-end 0))
5613 (setq pos1 (match-beginning 0))
5614 (setq tags (org-get-tags-at pos1))
5615 (setq head (buffer-substring-no-properties
5616 (point)
5617 (progn (skip-chars-forward "^\r\n")
5618 (point))))
5619 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5620 (setq timestr
5621 (concat (substring s (match-beginning 1)) " "))
5622 (setq timestr 'time))
5623 (setq txt (org-agenda-format-item
5624 (if (= diff 0)
5625 (car org-agenda-deadline-leaders)
5626 (if (functionp
5627 (nth 1 org-agenda-deadline-leaders))
5628 (funcall
5629 (nth 1 org-agenda-deadline-leaders)
5630 diff date)
5631 (format (nth 1 org-agenda-deadline-leaders)
5632 diff)))
5633 head category tags
5634 (if (not (= diff 0)) nil timestr)))))
5635 (when txt
5636 (setq face (org-agenda-deadline-face dfrac))
5637 (org-add-props txt props
5638 'org-marker (org-agenda-new-marker pos)
5639 'warntime warntime
5640 'org-hd-marker (org-agenda-new-marker pos1)
5641 'priority (+ (- diff)
5642 (org-get-priority txt))
5643 'org-category category
5644 'org-category-position category-pos
5645 'todo-state todo-state
5646 'type (if upcomingp "upcoming-deadline" "deadline")
5647 'date (if upcomingp date d2)
5648 'face (if donep 'org-agenda-done face)
5649 'undone-face face 'done-face 'org-agenda-done)
5650 (push txt ee))))))
5651 (nreverse ee)))
5653 (defun org-agenda-deadline-face (fraction)
5654 "Return the face to displaying a deadline item.
5655 FRACTION is what fraction of the head-warning time has passed."
5656 (let ((faces org-agenda-deadline-faces) f)
5657 (catch 'exit
5658 (while (setq f (pop faces))
5659 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5661 (defun org-agenda-get-scheduled (&optional deadline-results)
5662 "Return the scheduled information for agenda display."
5663 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5664 'org-todo-regexp org-todo-regexp
5665 'org-complex-heading-regexp org-complex-heading-regexp
5666 'done-face 'org-agenda-done
5667 'mouse-face 'highlight
5668 'help-echo
5669 (format "mouse-2 or RET jump to org file %s"
5670 (abbreviate-file-name buffer-file-name))))
5671 (regexp org-scheduled-time-regexp)
5672 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5673 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5675 (deadline-position-alist
5676 (mapcar (lambda (a) (and (setq mm (get-text-property
5677 0 'org-hd-marker a))
5678 (cons (marker-position mm) a)))
5679 deadline-results))
5680 d2 diff pos pos1 category category-pos tags donep
5681 ee txt head pastschedp todo-state face timestr s habitp show-all
5682 did-habit-check-p warntime)
5683 (goto-char (point-min))
5684 (while (re-search-forward regexp nil t)
5685 (catch :skip
5686 (org-agenda-skip)
5687 (setq s (match-string 1)
5688 txt nil
5689 pos (1- (match-beginning 1))
5690 todo-state (save-match-data (org-get-todo-state))
5691 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5692 (member todo-state
5693 org-agenda-repeating-timestamp-show-all))
5694 d2 (org-time-string-to-absolute
5695 (match-string 1) d1 'past show-all
5696 (current-buffer) pos)
5697 diff (- d2 d1)
5698 warntime (org-entry-get (point) "APPT_WARNTIME"))
5699 (setq pastschedp (and todayp (< diff 0)))
5700 (setq did-habit-check-p nil)
5701 ;; When to show a scheduled item in the calendar:
5702 ;; If it is on or past the date.
5703 (when (or (and (< diff 0)
5704 (< (abs diff) org-scheduled-past-days)
5705 (and todayp (not org-agenda-only-exact-dates)))
5706 (= diff 0)
5707 ;; org-is-habit-p uses org-entry-get, which is expansive
5708 ;; so we go extra mile to only call it once
5709 (and todayp
5710 (boundp 'org-habit-show-all-today)
5711 org-habit-show-all-today
5712 (setq did-habit-check-p t)
5713 (setq habitp (and (functionp 'org-is-habit-p)
5714 (org-is-habit-p)))))
5715 (save-excursion
5716 (setq donep (member todo-state org-done-keywords))
5717 (if (and donep
5718 (or org-agenda-skip-scheduled-if-done
5719 (not (= diff 0))
5720 (and (functionp 'org-is-habit-p)
5721 (org-is-habit-p))))
5722 (setq txt nil)
5723 (setq habitp (if did-habit-check-p habitp
5724 (and (functionp 'org-is-habit-p)
5725 (org-is-habit-p))))
5726 (setq category (org-get-category)
5727 category-pos (get-text-property (point) 'org-category-position))
5728 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5729 (setq txt org-agenda-no-heading-message)
5730 (goto-char (match-end 0))
5731 (setq pos1 (match-beginning 0))
5732 (if habitp
5733 (if (or (not org-habit-show-habits)
5734 (and (not todayp)
5735 (boundp 'org-habit-show-habits-only-for-today)
5736 org-habit-show-habits-only-for-today))
5737 (throw :skip nil))
5738 (if (and
5739 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5740 (and org-agenda-skip-scheduled-if-deadline-is-shown
5741 pastschedp))
5742 (setq mm (assoc pos1 deadline-position-alist)))
5743 (throw :skip nil)))
5744 (setq tags (org-get-tags-at))
5745 (setq head (buffer-substring-no-properties
5746 (point)
5747 (progn (skip-chars-forward "^\r\n") (point))))
5748 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5749 (setq timestr
5750 (concat (substring s (match-beginning 1)) " "))
5751 (setq timestr 'time))
5752 (setq txt (org-agenda-format-item
5753 (if (= diff 0)
5754 (car org-agenda-scheduled-leaders)
5755 (format (nth 1 org-agenda-scheduled-leaders)
5756 (- 1 diff)))
5757 head category tags
5758 (if (not (= diff 0)) nil timestr)
5759 nil habitp))))
5760 (when txt
5761 (setq face
5762 (cond
5763 ((and (not habitp) pastschedp)
5764 'org-scheduled-previously)
5765 (todayp 'org-scheduled-today)
5766 (t 'org-scheduled))
5767 habitp (and habitp (org-habit-parse-todo)))
5768 (org-add-props txt props
5769 'undone-face face
5770 'face (if donep 'org-agenda-done face)
5771 'org-marker (org-agenda-new-marker pos)
5772 'org-hd-marker (org-agenda-new-marker pos1)
5773 'type (if pastschedp "past-scheduled" "scheduled")
5774 'date (if pastschedp d2 date)
5775 'warntime warntime
5776 'priority (if habitp
5777 (org-habit-get-priority habitp)
5778 (+ 94 (- 5 diff) (org-get-priority txt)))
5779 'org-category category
5780 'category-position category-pos
5781 'org-habit-p habitp
5782 'todo-state todo-state)
5783 (push txt ee))))))
5784 (nreverse ee)))
5786 (defun org-agenda-get-blocks ()
5787 "Return the date-range information for agenda display."
5788 (let* ((props (list 'face nil
5789 'org-not-done-regexp org-not-done-regexp
5790 'org-todo-regexp org-todo-regexp
5791 'org-complex-heading-regexp org-complex-heading-regexp
5792 'mouse-face 'highlight
5793 'help-echo
5794 (format "mouse-2 or RET jump to org file %s"
5795 (abbreviate-file-name buffer-file-name))))
5796 (regexp org-tr-regexp)
5797 (d0 (calendar-absolute-from-gregorian date))
5798 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
5799 todo-state tags pos head donep)
5800 (goto-char (point-min))
5801 (while (re-search-forward regexp nil t)
5802 (catch :skip
5803 (org-agenda-skip)
5804 (setq pos (point))
5805 (let ((start-time (match-string 1))
5806 (end-time (match-string 2)))
5807 (setq s1 (match-string 1)
5808 s2 (match-string 2)
5809 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
5810 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
5811 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5812 ;; Only allow days between the limits, because the normal
5813 ;; date stamps will catch the limits.
5814 (save-excursion
5815 (setq todo-state (org-get-todo-state))
5816 (setq donep (member todo-state org-done-keywords))
5817 (if (and donep org-agenda-skip-timestamp-if-done)
5818 (throw :skip t))
5819 (setq marker (org-agenda-new-marker (point)))
5820 (setq category (org-get-category)
5821 category-pos (get-text-property (point) 'org-category-position))
5822 (if (not (re-search-backward org-outline-regexp-bol nil t))
5823 (setq txt org-agenda-no-heading-message)
5824 (goto-char (match-beginning 0))
5825 (setq hdmarker (org-agenda-new-marker (point)))
5826 (setq tags (org-get-tags-at))
5827 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5828 (setq head (match-string 1))
5829 (let ((remove-re
5830 (if org-agenda-remove-timeranges-from-blocks
5831 (concat
5832 "<" (regexp-quote s1) ".*?>"
5833 "--"
5834 "<" (regexp-quote s2) ".*?>")
5835 nil)))
5836 (setq txt (org-agenda-format-item
5837 (format
5838 (nth (if (= d1 d2) 0 1)
5839 org-agenda-timerange-leaders)
5840 (1+ (- d0 d1)) (1+ (- d2 d1)))
5841 head category tags
5842 (cond ((and (= d1 d0) (= d2 d0))
5843 (concat "<" start-time ">--<" end-time ">"))
5844 ((= d1 d0)
5845 (concat "<" start-time ">"))
5846 ((= d2 d0)
5847 (concat "<" end-time ">")))
5848 remove-re t))))
5849 (org-add-props txt props
5850 'org-marker marker 'org-hd-marker hdmarker
5851 'type "block" 'date date
5852 'todo-state todo-state
5853 'priority (org-get-priority txt) 'org-category category
5854 'org-category-position category-pos)
5855 (push txt ee))))
5856 (goto-char pos)))
5857 ;; Sort the entries by expiration date.
5858 (nreverse ee)))
5860 ;;; Agenda presentation and sorting
5862 (defvar org-prefix-has-time nil
5863 "A flag, set by `org-compile-prefix-format'.
5864 The flag is set if the currently compiled format contains a `%t'.")
5865 (defvar org-prefix-has-tag nil
5866 "A flag, set by `org-compile-prefix-format'.
5867 The flag is set if the currently compiled format contains a `%T'.")
5868 (defvar org-prefix-has-effort nil
5869 "A flag, set by `org-compile-prefix-format'.
5870 The flag is set if the currently compiled format contains a `%e'.")
5871 (defvar org-prefix-category-length nil
5872 "Used by `org-compile-prefix-format' to remember the category field width.")
5873 (defvar org-prefix-category-max-length nil
5874 "Used by `org-compile-prefix-format' to remember the category field width.")
5876 (defun org-agenda-get-category-icon (category)
5877 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5878 (dolist (entry org-agenda-category-icon-alist)
5879 (when (org-string-match-p (car entry) category)
5880 (if (listp (cadr entry))
5881 (return (cadr entry))
5882 (return (apply 'create-image (cdr entry)))))))
5884 (defun org-agenda-format-item (extra txt &optional category tags dotime
5885 remove-re habitp)
5886 "Format TXT to be inserted into the agenda buffer.
5887 In particular, it adds the prefix and corresponding text properties. EXTRA
5888 must be a string and replaces the `%s' specifier in the prefix format.
5889 CATEGORY (string, symbol or nil) may be used to overrule the default
5890 category taken from local variable or file name. It will replace the `%c'
5891 specifier in the format. DOTIME, when non-nil, indicates that a
5892 time-of-day should be extracted from TXT for sorting of this entry, and for
5893 the `%t' specifier in the format. When DOTIME is a string, this string is
5894 searched for a time before TXT is. TAGS can be the tags of the headline.
5895 Any match of REMOVE-RE will be removed from TXT."
5896 ;; We keep the org-prefix-* variable values along with a compiled
5897 ;; formatter, so that multiple agendas existing at the same time, do
5898 ;; not step on each other toes.
5900 ;; It was inconvenient to make these variables buffer local in
5901 ;; Agenda buffers, because this function expects to be called with
5902 ;; the buffer where item comes from being current, and not agenda
5903 ;; buffer
5904 (let* ((bindings (car org-prefix-format-compiled))
5905 (formatter (cadr org-prefix-format-compiled)))
5906 (loop for (var value) in bindings
5907 do (set var value))
5908 (save-match-data
5909 ;; Diary entries sometimes have extra whitespace at the beginning
5910 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5912 ;; Fix the tags part in txt
5913 (setq txt (org-agenda-fix-displayed-tags
5914 txt tags
5915 org-agenda-show-inherited-tags
5916 org-agenda-hide-tags-regexp))
5917 (let* ((category (or category
5918 (if (stringp org-category)
5919 org-category
5920 (and org-category (symbol-name org-category)))
5921 (if buffer-file-name
5922 (file-name-sans-extension
5923 (file-name-nondirectory buffer-file-name))
5924 "")))
5925 (category-icon (org-agenda-get-category-icon category))
5926 (category-icon (if category-icon
5927 (propertize " " 'display category-icon)
5928 ""))
5929 ;; time, tag, effort are needed for the eval of the prefix format
5930 (tag (if tags (nth (1- (length tags)) tags) ""))
5931 time effort neffort
5932 (ts (if dotime (concat
5933 (if (stringp dotime) dotime "")
5934 (and org-agenda-search-headline-for-time txt))))
5935 (time-of-day (and dotime (org-get-time-of-day ts)))
5936 stamp plain s0 s1 s2 rtn srp l
5937 duration thecategory)
5938 (and (derived-mode-p 'org-mode) buffer-file-name
5939 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5940 (when (and dotime time-of-day)
5941 ;; Extract starting and ending time and move them to prefix
5942 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5943 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5944 (setq s0 (match-string 0 ts)
5945 srp (and stamp (match-end 3))
5946 s1 (match-string (if plain 1 2) ts)
5947 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5949 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5950 ;; them, we might want to remove them there to avoid duplication.
5951 ;; The user can turn this off with a variable.
5952 (if (and org-prefix-has-time
5953 org-agenda-remove-times-when-in-prefix (or stamp plain)
5954 (string-match (concat (regexp-quote s0) " *") txt)
5955 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5956 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5957 (= (match-beginning 0) 0)
5959 (setq txt (replace-match "" nil nil txt))))
5960 ;; Normalize the time(s) to 24 hour
5961 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5962 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5964 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5965 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5966 (setq s2
5967 (org-minutes-to-hh:mm-string
5968 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5970 ;; Compute the duration
5971 (when s2
5972 (setq duration (- (org-hh:mm-string-to-minutes s2)
5973 (org-hh:mm-string-to-minutes s1)))))
5975 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5976 txt)
5977 ;; Tags are in the string
5978 (if (or (eq org-agenda-remove-tags t)
5979 (and org-agenda-remove-tags
5980 org-prefix-has-tag))
5981 (setq txt (replace-match "" t t txt))
5982 (setq txt (replace-match
5983 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5984 (match-string 2 txt))
5985 t t txt))))
5986 (when (derived-mode-p 'org-mode)
5987 (setq effort
5988 (condition-case nil
5989 (org-get-effort
5990 (or (get-text-property 0 'org-hd-marker txt)
5991 (get-text-property 0 'org-marker txt)))
5992 (error nil)))
5993 (when effort
5994 (setq neffort (org-duration-string-to-minutes effort)
5995 effort (setq effort (concat "[" effort "]")))))
5996 ;; prevent erroring out with %e format when there is no effort
5997 (or effort (setq effort ""))
5999 (when remove-re
6000 (while (string-match remove-re txt)
6001 (setq txt (replace-match "" t t txt))))
6003 ;; Set org-heading property on `txt' to mark the start of the
6004 ;; heading.
6005 (add-text-properties 0 (length txt) '(org-heading t) txt)
6007 ;; Prepare the variables needed in the eval of the compiled format
6008 (setq time (cond (s2 (concat
6009 (org-agenda-time-of-day-to-ampm-maybe s1)
6010 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6011 (if org-agenda-timegrid-use-ampm " ")))
6012 (s1 (concat
6013 (org-agenda-time-of-day-to-ampm-maybe s1)
6014 (if org-agenda-timegrid-use-ampm
6015 "........ "
6016 "......")))
6017 (t ""))
6018 extra (or (and (not habitp) extra) "")
6019 category (if (symbolp category) (symbol-name category) category)
6020 thecategory (copy-sequence category))
6021 (if (string-match org-bracket-link-regexp category)
6022 (progn
6023 (setq l (if (match-end 3)
6024 (- (match-end 3) (match-beginning 3))
6025 (- (match-end 1) (match-beginning 1))))
6026 (when (< l (or org-prefix-category-length 0))
6027 (setq category (copy-sequence category))
6028 (org-add-props category nil
6029 'extra-space (make-string
6030 (- org-prefix-category-length l 1) ?\ ))))
6031 (if (and org-prefix-category-max-length
6032 (>= (length category) org-prefix-category-max-length))
6033 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6034 ;; Evaluate the compiled format
6035 (setq rtn (concat (eval formatter) txt))
6037 ;; And finally add the text properties
6038 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6039 (org-add-props rtn nil
6040 'org-category (if thecategory (downcase thecategory) category)
6041 'tags (mapcar 'org-downcase-keep-props tags)
6042 'org-highest-priority org-highest-priority
6043 'org-lowest-priority org-lowest-priority
6044 'time-of-day time-of-day
6045 'duration duration
6046 'effort effort
6047 'effort-minutes neffort
6048 'txt txt
6049 'time time
6050 'extra extra
6051 'format org-prefix-format-compiled
6052 'dotime dotime)))))
6054 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6055 "Remove tags string from TXT, and add a modified list of tags.
6056 The modified list may contain inherited tags, and tags matched by
6057 `org-agenda-hide-tags-regexp' will be removed."
6058 (when (or add-inherited hide-re)
6059 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6060 (setq txt (substring txt 0 (match-beginning 0))))
6061 (setq tags
6062 (delq nil
6063 (mapcar (lambda (tg)
6064 (if (or (and hide-re (string-match hide-re tg))
6065 (and (not add-inherited)
6066 (get-text-property 0 'inherited tg)))
6068 tg))
6069 tags)))
6070 (when tags
6071 (let ((have-i (get-text-property 0 'inherited (car tags)))
6073 (setq txt (concat txt " :"
6074 (mapconcat
6075 (lambda (x)
6076 (setq i (get-text-property 0 'inherited x))
6077 (if (and have-i (not i))
6078 (progn
6079 (setq have-i nil)
6080 (concat ":" x))
6082 tags ":")
6083 (if have-i "::" ":"))))))
6084 txt)
6086 (defun org-downcase-keep-props (s)
6087 (let ((props (text-properties-at 0 s)))
6088 (setq s (downcase s))
6089 (add-text-properties 0 (length s) props s)
6092 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6093 (defvar org-agenda-sorting-strategy-selected nil)
6095 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6096 (catch 'exit
6097 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6098 ((and todayp (member 'today (car org-agenda-time-grid))))
6099 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6100 ((member 'weekly (car org-agenda-time-grid)))
6101 (t (throw 'exit list)))
6102 (let* ((have (delq nil (mapcar
6103 (lambda (x) (get-text-property 1 'time-of-day x))
6104 list)))
6105 (string (nth 1 org-agenda-time-grid))
6106 (gridtimes (nth 2 org-agenda-time-grid))
6107 (req (car org-agenda-time-grid))
6108 (remove (member 'remove-match req))
6109 new time)
6110 (if (and (member 'require-timed req) (not have))
6111 ;; don't show empty grid
6112 (throw 'exit list))
6113 (while (setq time (pop gridtimes))
6114 (unless (and remove (member time have))
6115 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6116 (push (org-agenda-format-item
6117 nil string "" nil
6118 (concat (substring time 0 -2) ":" (substring time -2)))
6119 new)
6120 (put-text-property
6121 2 (length (car new)) 'face 'org-time-grid (car new))))
6122 (when (and todayp org-agenda-show-current-time-in-grid)
6123 (push (org-agenda-format-item
6125 org-agenda-current-time-string
6126 "" nil
6127 (format-time-string "%H:%M "))
6128 new)
6129 (put-text-property
6130 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6132 (if (member 'time-up org-agenda-sorting-strategy-selected)
6133 (append new list)
6134 (append list new)))))
6136 (defun org-compile-prefix-format (key)
6137 "Compile the prefix format into a Lisp form that can be evaluated.
6138 The resulting form and associated variable bindings is returned
6139 and stored in the variable `org-prefix-format-compiled'."
6140 (setq org-prefix-has-time nil org-prefix-has-tag nil
6141 org-prefix-category-length nil
6142 org-prefix-has-effort nil)
6143 (let ((s (cond
6144 ((stringp org-agenda-prefix-format)
6145 org-agenda-prefix-format)
6146 ((assq key org-agenda-prefix-format)
6147 (cdr (assq key org-agenda-prefix-format)))
6148 (t " %-12:c%?-12t% s")))
6149 (start 0)
6150 varform vars var e c f opt)
6151 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
6152 s start)
6153 (setq var (or (cdr (assoc (match-string 4 s)
6154 '(("c" . category) ("t" . time) ("s" . extra)
6155 ("i" . category-icon) ("T" . tag) ("e" . effort))))
6156 'eval)
6157 c (or (match-string 3 s) "")
6158 opt (match-beginning 1)
6159 start (1+ (match-beginning 0)))
6160 (if (equal var 'time) (setq org-prefix-has-time t))
6161 (if (equal var 'tag) (setq org-prefix-has-tag t))
6162 (if (equal var 'effort) (setq org-prefix-has-effort t))
6163 (setq f (concat "%" (match-string 2 s) "s"))
6164 (when (equal var 'category)
6165 (setq org-prefix-category-length
6166 (floor (abs (string-to-number (match-string 2 s)))))
6167 (setq org-prefix-category-max-length
6168 (let ((x (match-string 2 s)))
6169 (save-match-data
6170 (if (string-match "\\.[0-9]+" x)
6171 (string-to-number (substring (match-string 0 x) 1)))))))
6172 (if (eq var 'eval)
6173 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6174 (if opt
6175 (setq varform
6176 `(if (equal "" ,var)
6178 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6179 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6180 (setq s (replace-match "%s" t nil s))
6181 (push varform vars))
6182 (setq vars (nreverse vars))
6183 (with-current-buffer (or org-agenda-buffer (current-buffer))
6184 (setq org-prefix-format-compiled
6185 (list
6186 `((org-prefix-has-time ,org-prefix-has-time)
6187 (org-prefix-has-tag ,org-prefix-has-tag)
6188 (org-prefix-category-length ,org-prefix-category-length)
6189 (org-prefix-has-effort ,org-prefix-has-effort))
6190 `(format ,s ,@vars))))))
6192 (defun org-set-sorting-strategy (key)
6193 (if (symbolp (car org-agenda-sorting-strategy))
6194 ;; the old format
6195 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6196 (setq org-agenda-sorting-strategy-selected
6197 (or (cdr (assq key org-agenda-sorting-strategy))
6198 (cdr (assq 'agenda org-agenda-sorting-strategy))
6199 '(time-up category-keep priority-down)))))
6201 (defun org-get-time-of-day (s &optional string mod24)
6202 "Check string S for a time of day.
6203 If found, return it as a military time number between 0 and 2400.
6204 If not found, return nil.
6205 The optional STRING argument forces conversion into a 5 character wide string
6206 HH:MM."
6207 (save-match-data
6208 (when
6209 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6210 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6211 (let* ((h (string-to-number (match-string 1 s)))
6212 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6213 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6214 (am-p (equal ampm "am"))
6215 (h1 (cond ((not ampm) h)
6216 ((= h 12) (if am-p 0 12))
6217 (t (+ h (if am-p 0 12)))))
6218 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6219 (mod h1 24) h1))
6220 (t0 (+ (* 100 h2) m))
6221 (t1 (concat (if (>= h1 24) "+" " ")
6222 (if (and org-agenda-time-leading-zero
6223 (< t0 1000)) "0" "")
6224 (if (< t0 100) "0" "")
6225 (if (< t0 10) "0" "")
6226 (int-to-string t0))))
6227 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6229 (defvar org-agenda-before-sorting-filter-function nil
6230 "Function to be applied to agenda items prior to sorting.
6231 Prior to sorting also means just before they are inserted into the agenda.
6233 To aid sorting, you may revisit the original entries and add more text
6234 properties which will later be used by the sorting functions.
6236 The function should take a string argument, an agenda line.
6237 It has access to the text properties in that line, which contain among
6238 other things, the property `org-hd-marker' that points to the entry
6239 where the line comes from. Note that not all lines going into the agenda
6240 have this property, only most.
6242 The function should return the modified string. It is probably best
6243 to ONLY change text properties.
6245 You can also use this function as a filter, by returning nil for lines
6246 you don't want to have in the agenda at all. For this application, you
6247 could bind the variable in the options section of a custom command.")
6249 (defun org-agenda-finalize-entries (list &optional nosort)
6250 "Sort and concatenate the agenda items."
6251 (setq list (mapcar 'org-agenda-highlight-todo list))
6252 (if nosort
6253 list
6254 (when org-agenda-before-sorting-filter-function
6255 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
6256 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
6258 (defun org-agenda-highlight-todo (x)
6259 (let ((org-done-keywords org-done-keywords-for-agenda)
6260 (case-fold-search nil)
6262 (if (eq x 'line)
6263 (save-excursion
6264 (beginning-of-line 1)
6265 (setq re (org-get-at-bol 'org-todo-regexp))
6266 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6267 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6268 (add-text-properties (match-beginning 0) (match-end 1)
6269 (list 'face (org-get-todo-face 1)))
6270 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6271 (delete-region (match-beginning 1) (1- (match-end 0)))
6272 (goto-char (match-beginning 1))
6273 (insert (format org-agenda-todo-keyword-format s)))))
6274 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6275 (setq re (get-text-property 0 'org-todo-regexp x))
6276 (when (and re
6277 ;; Test `pl' because if there's no heading content,
6278 ;; there's no point matching to highlight. Note
6279 ;; that if we didn't test `pl' first, and there
6280 ;; happened to be no keyword from `org-todo-regexp'
6281 ;; on this heading line, then the `equal' comparison
6282 ;; afterwards would spuriously succeed in the case
6283 ;; where `pl' is nil -- causing an args-out-of-range
6284 ;; error when we try to add text properties to text
6285 ;; that isn't there.
6287 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6288 x pl) pl))
6289 (add-text-properties
6290 (or (match-end 1) (match-end 0)) (match-end 0)
6291 (list 'face (org-get-todo-face (match-string 2 x)))
6293 (when (match-end 1)
6294 (setq x (concat (substring x 0 (match-end 1))
6295 (format org-agenda-todo-keyword-format
6296 (match-string 2 x))
6297 (org-add-props " " (text-properties-at 0 x))
6298 (substring x (match-end 3)))))))
6299 x)))
6301 (defsubst org-cmp-priority (a b)
6302 "Compare the priorities of string A and B."
6303 (let ((pa (or (get-text-property 1 'priority a) 0))
6304 (pb (or (get-text-property 1 'priority b) 0)))
6305 (cond ((> pa pb) +1)
6306 ((< pa pb) -1))))
6308 (defsubst org-cmp-effort (a b)
6309 "Compare the effort values of string A and B."
6310 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6311 (ea (or (get-text-property 1 'effort-minutes a) def))
6312 (eb (or (get-text-property 1 'effort-minutes b) def)))
6313 (cond ((> ea eb) +1)
6314 ((< ea eb) -1))))
6316 (defsubst org-cmp-category (a b)
6317 "Compare the string values of categories of strings A and B."
6318 (let ((ca (or (get-text-property 1 'org-category a) ""))
6319 (cb (or (get-text-property 1 'org-category b) "")))
6320 (cond ((string-lessp ca cb) -1)
6321 ((string-lessp cb ca) +1))))
6323 (defsubst org-cmp-todo-state (a b)
6324 "Compare the todo states of strings A and B."
6325 (let* ((ma (or (get-text-property 1 'org-marker a)
6326 (get-text-property 1 'org-hd-marker a)))
6327 (mb (or (get-text-property 1 'org-marker b)
6328 (get-text-property 1 'org-hd-marker b)))
6329 (fa (and ma (marker-buffer ma)))
6330 (fb (and mb (marker-buffer mb)))
6331 (todo-kwds
6332 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6333 (and fb (with-current-buffer fb org-todo-keywords-1))))
6334 (ta (or (get-text-property 1 'todo-state a) ""))
6335 (tb (or (get-text-property 1 'todo-state b) ""))
6336 (la (- (length (member ta todo-kwds))))
6337 (lb (- (length (member tb todo-kwds))))
6338 (donepa (member ta org-done-keywords-for-agenda))
6339 (donepb (member tb org-done-keywords-for-agenda)))
6340 (cond ((and donepa (not donepb)) -1)
6341 ((and (not donepa) donepb) +1)
6342 ((< la lb) -1)
6343 ((< lb la) +1))))
6345 (defsubst org-cmp-alpha (a b)
6346 "Compare the headlines, alphabetically."
6347 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6348 (plb (text-property-any 0 (length b) 'org-heading t b))
6349 (ta (and pla (substring a pla)))
6350 (tb (and plb (substring b plb))))
6351 (when pla
6352 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6353 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6354 (setq ta (substring ta (match-end 0))))
6355 (setq ta (downcase ta)))
6356 (when plb
6357 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6358 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6359 (setq tb (substring tb (match-end 0))))
6360 (setq tb (downcase tb)))
6361 (cond ((not ta) +1)
6362 ((not tb) -1)
6363 ((string-lessp ta tb) -1)
6364 ((string-lessp tb ta) +1))))
6366 (defsubst org-cmp-tag (a b)
6367 "Compare the string values of the first tags of A and B."
6368 (let ((ta (car (last (get-text-property 1 'tags a))))
6369 (tb (car (last (get-text-property 1 'tags b)))))
6370 (cond ((not ta) +1)
6371 ((not tb) -1)
6372 ((string-lessp ta tb) -1)
6373 ((string-lessp tb ta) +1))))
6375 (defsubst org-cmp-time (a b)
6376 "Compare the time-of-day values of strings A and B."
6377 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6378 (ta (or (get-text-property 1 'time-of-day a) def))
6379 (tb (or (get-text-property 1 'time-of-day b) def)))
6380 (cond ((< ta tb) -1)
6381 ((< tb ta) +1))))
6383 (defsubst org-cmp-habit-p (a b)
6384 "Compare the todo states of strings A and B."
6385 (let ((ha (get-text-property 1 'org-habit-p a))
6386 (hb (get-text-property 1 'org-habit-p b)))
6387 (cond ((and ha (not hb)) -1)
6388 ((and (not ha) hb) +1))))
6390 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
6392 (defun org-entries-lessp (a b)
6393 "Predicate for sorting agenda entries."
6394 ;; The following variables will be used when the form is evaluated.
6395 ;; So even though the compiler complains, keep them.
6396 (let* ((ss org-agenda-sorting-strategy-selected)
6397 (time-up (and (org-em 'time-up 'time-down ss)
6398 (org-cmp-time a b)))
6399 (time-down (if time-up (- time-up) nil))
6400 (priority-up (and (org-em 'priority-up 'priority-down ss)
6401 (org-cmp-priority a b)))
6402 (priority-down (if priority-up (- priority-up) nil))
6403 (effort-up (and (org-em 'effort-up 'effort-down ss)
6404 (org-cmp-effort a b)))
6405 (effort-down (if effort-up (- effort-up) nil))
6406 (category-up (and (or (org-em 'category-up 'category-down ss)
6407 (memq 'category-keep ss))
6408 (org-cmp-category a b)))
6409 (category-down (if category-up (- category-up) nil))
6410 (category-keep (if category-up +1 nil))
6411 (tag-up (and (org-em 'tag-up 'tag-down ss)
6412 (org-cmp-tag a b)))
6413 (tag-down (if tag-up (- tag-up) nil))
6414 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
6415 (org-cmp-todo-state a b)))
6416 (todo-state-down (if todo-state-up (- todo-state-up) nil))
6417 (habit-up (and (org-em 'habit-up 'habit-down ss)
6418 (org-cmp-habit-p a b)))
6419 (habit-down (if habit-up (- habit-up) nil))
6420 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
6421 (org-cmp-alpha a b)))
6422 (alpha-down (if alpha-up (- alpha-up) nil))
6423 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
6424 user-defined-up user-defined-down)
6425 (if (and need-user-cmp org-agenda-cmp-user-defined
6426 (functionp org-agenda-cmp-user-defined))
6427 (setq user-defined-up
6428 (funcall org-agenda-cmp-user-defined a b)
6429 user-defined-down (if user-defined-up (- user-defined-up) nil)))
6430 (cdr (assoc
6431 (eval (cons 'or org-agenda-sorting-strategy-selected))
6432 '((-1 . t) (1 . nil) (nil . nil))))))
6434 ;;; Agenda restriction lock
6436 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
6437 "Overlay to mark the headline to which agenda commands are restricted.")
6438 (overlay-put org-agenda-restriction-lock-overlay
6439 'face 'org-agenda-restriction-lock)
6440 (overlay-put org-agenda-restriction-lock-overlay
6441 'help-echo "Agendas are currently limited to this subtree.")
6442 (org-detach-overlay org-agenda-restriction-lock-overlay)
6444 (defun org-agenda-set-restriction-lock (&optional type)
6445 "Set restriction lock for agenda, to current subtree or file.
6446 Restriction will be the file if TYPE is `file', or if type is the
6447 universal prefix '(4), or if the cursor is before the first headline
6448 in the file. Otherwise, restriction will be to the current subtree."
6449 (interactive "P")
6450 (and (equal type '(4)) (setq type 'file))
6451 (setq type (cond
6452 (type type)
6453 ((org-at-heading-p) 'subtree)
6454 ((condition-case nil (org-back-to-heading t) (error nil))
6455 'subtree)
6456 (t 'file)))
6457 (if (eq type 'subtree)
6458 (progn
6459 (setq org-agenda-restrict t)
6460 (setq org-agenda-overriding-restriction 'subtree)
6461 (put 'org-agenda-files 'org-restrict
6462 (list (buffer-file-name (buffer-base-buffer))))
6463 (org-back-to-heading t)
6464 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
6465 (move-marker org-agenda-restrict-begin (point))
6466 (move-marker org-agenda-restrict-end
6467 (save-excursion (org-end-of-subtree t)))
6468 (message "Locking agenda restriction to subtree"))
6469 (put 'org-agenda-files 'org-restrict
6470 (list (buffer-file-name (buffer-base-buffer))))
6471 (setq org-agenda-restrict nil)
6472 (setq org-agenda-overriding-restriction 'file)
6473 (move-marker org-agenda-restrict-begin nil)
6474 (move-marker org-agenda-restrict-end nil)
6475 (message "Locking agenda restriction to file"))
6476 (setq current-prefix-arg nil)
6477 (org-agenda-maybe-redo))
6479 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6480 "Remove the agenda restriction lock."
6481 (interactive "P")
6482 (org-detach-overlay org-agenda-restriction-lock-overlay)
6483 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6484 (setq org-agenda-overriding-restriction nil)
6485 (setq org-agenda-restrict nil)
6486 (put 'org-agenda-files 'org-restrict nil)
6487 (move-marker org-agenda-restrict-begin nil)
6488 (move-marker org-agenda-restrict-end nil)
6489 (setq current-prefix-arg nil)
6490 (message "Agenda restriction lock removed")
6491 (or noupdate (org-agenda-maybe-redo)))
6493 (defun org-agenda-maybe-redo ()
6494 "If there is any window showing the agenda view, update it."
6495 (let ((w (get-buffer-window org-agenda-buffer-name t))
6496 (w0 (selected-window)))
6497 (when w
6498 (select-window w)
6499 (org-agenda-redo)
6500 (select-window w0)
6501 (if org-agenda-overriding-restriction
6502 (message "Agenda view shifted to new %s restriction"
6503 org-agenda-overriding-restriction)
6504 (message "Agenda restriction lock removed")))))
6506 ;;; Agenda commands
6508 (defun org-agenda-check-type (error &rest types)
6509 "Check if agenda buffer is of allowed type.
6510 If ERROR is non-nil, throw an error, otherwise just return nil."
6511 (if (not org-agenda-type)
6512 (error "No Org agenda currently displayed")
6513 (if (memq org-agenda-type types)
6515 (if error
6516 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6517 nil))))
6519 (defun org-agenda-Quit (&optional arg)
6520 "Exit agenda by removing the window or the buffer."
6521 (interactive)
6522 (if org-agenda-columns-active
6523 (org-columns-quit)
6524 (let ((buf (current-buffer)))
6525 (if (eq org-agenda-window-setup 'other-frame)
6526 (progn
6527 (org-agenda-reset-markers)
6528 (kill-buffer buf)
6529 (org-columns-remove-overlays)
6530 (setq org-agenda-archives-mode nil)
6531 (delete-frame))
6532 (and (not (eq org-agenda-window-setup 'current-window))
6533 (not (one-window-p))
6534 (delete-window))
6535 (org-agenda-reset-markers)
6536 (kill-buffer buf)
6537 (org-columns-remove-overlays)
6538 (setq org-agenda-archives-mode nil)))
6539 ;; Maybe restore the pre-agenda window configuration.
6540 (and org-agenda-restore-windows-after-quit
6541 (not (eq org-agenda-window-setup 'other-frame))
6542 org-agenda-pre-window-conf
6543 (set-window-configuration org-agenda-pre-window-conf)
6544 (setq org-agenda-pre-window-conf nil))))
6546 (defun org-agenda-quit ()
6547 "Exit agenda by killing agenda buffer or burying it when
6548 `org-agenda-sticky' is non-NIL"
6549 (interactive)
6550 (if org-agenda-columns-active
6551 (org-columns-quit)
6552 (if org-agenda-sticky
6553 (let ((buf (current-buffer)))
6554 (if (eq org-agenda-window-setup 'other-frame)
6555 (progn
6556 (delete-frame))
6557 (and (not (eq org-agenda-window-setup 'current-window))
6558 (not (one-window-p))
6559 (delete-window)))
6560 (with-current-buffer buf
6561 (bury-buffer)
6562 ;; Maybe restore the pre-agenda window configuration.
6563 (and org-agenda-restore-windows-after-quit
6564 (not (eq org-agenda-window-setup 'other-frame))
6565 org-agenda-pre-window-conf
6566 (set-window-configuration org-agenda-pre-window-conf)
6567 (setq org-agenda-pre-window-conf nil))))
6568 (org-agenda-Quit))))
6570 (defun org-agenda-exit ()
6571 "Exit agenda by removing the window or the buffer.
6572 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6573 Org-mode buffers visited directly by the user will not be touched."
6574 (interactive)
6575 (org-release-buffers org-agenda-new-buffers)
6576 (setq org-agenda-new-buffers nil)
6577 (org-agenda-Quit))
6579 (defun org-agenda-kill-all-agenda-buffers ()
6580 "Kill all buffers in `org-agena-mode'.
6581 This is used when toggling sticky agendas. You can also explicitly invoke it
6582 with `C-c a C-k'."
6583 (interactive)
6584 (let (blist)
6585 (dolist (buf (buffer-list))
6586 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
6587 (push buf blist)))
6588 (mapc 'kill-buffer blist)))
6590 (defun org-agenda-execute (arg)
6591 "Execute another agenda command, keeping same window.
6592 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6593 in the agenda."
6594 (interactive "P")
6595 (let ((org-agenda-window-setup 'current-window))
6596 (org-agenda arg)))
6598 (defun org-agenda-redo (&optional all)
6599 "Rebuild possibly ALL agenda view(s) in the current buffer."
6600 (interactive "P")
6601 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
6602 (cpa (unless (eq all t) current-prefix-arg))
6603 (org-agenda-doing-sticky-redo org-agenda-sticky)
6604 (org-agenda-sticky nil)
6605 (org-agenda-buffer-name (or org-agenda-this-buffer-name
6606 org-agenda-buffer-name))
6607 (org-agenda-keep-modes t)
6608 (tag-filter org-agenda-tag-filter)
6609 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
6610 (top-cat-filter org-agenda-top-category-filter)
6611 (cat-filter org-agenda-category-filter)
6612 (cat-preset (get 'org-agenda-category-filter :preset-filter))
6613 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
6614 (cols org-agenda-columns-active)
6615 (line (org-current-line))
6616 (window-line (- line (org-current-line (window-start))))
6617 (lprops (get 'org-agenda-redo-command 'org-lprops))
6618 (redo-cmd (get-text-property p 'org-redo-cmd))
6619 (last-args (get-text-property p 'org-last-args))
6620 (org-agenda-overriding-cmd (get-text-property p 'org-serie-cmd))
6621 (org-agenda-overriding-cmd-arguments
6622 (unless (eq all t)
6623 (cond ((listp last-args)
6624 (cons (or cpa (car last-args)) (cdr last-args)))
6625 ((stringp last-args)
6626 last-args))))
6627 (serie-redo-cmd (get-text-property p 'org-serie-redo-cmd)))
6628 (put 'org-agenda-tag-filter :preset-filter nil)
6629 (put 'org-agenda-category-filter :preset-filter nil)
6630 (and cols (org-columns-quit))
6631 (message "Rebuilding agenda buffer...")
6632 (if serie-redo-cmd
6633 (eval serie-redo-cmd)
6634 (org-let lprops '(eval redo-cmd)))
6635 (setq org-agenda-undo-list nil
6636 org-agenda-pending-undo-list nil)
6637 (message "Rebuilding agenda buffer...done")
6638 (put 'org-agenda-tag-filter :preset-filter tag-preset)
6639 (put 'org-agenda-category-filter :preset-filter cat-preset)
6640 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
6641 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
6642 (and top-cat-filter (org-agenda-filter-top-category-apply top-cat-filter))
6643 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6644 (org-goto-line line)
6645 (recenter window-line)))
6647 (defvar org-global-tags-completion-table nil)
6648 (defvar org-agenda-filter-form nil)
6649 (defvar org-agenda-filtered-by-category nil)
6651 (defun org-agenda-filter-by-category (strip)
6652 "Keep only those lines in the agenda buffer that have a specific category.
6653 The category is that of the current line."
6654 (interactive "P")
6655 (if (and org-agenda-filtered-by-category
6656 org-agenda-category-filter)
6657 (org-agenda-filter-show-all-cat)
6658 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
6659 (if cat (org-agenda-filter-apply
6660 (list (concat (if strip "-" "+") cat)) 'category)
6661 (error "No category at point")))))
6663 (defun org-find-top-category (&optional pos)
6664 (save-excursion
6665 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
6666 (if pos (goto-char pos))
6667 ;; Skip up to the topmost parent
6668 (while (ignore-errors (outline-up-heading 1) t))
6669 (ignore-errors
6670 (nth 4 (org-heading-components))))))
6672 (defvar org-agenda-filtered-by-top-category nil)
6674 (defun org-agenda-filter-by-top-category (strip)
6675 "Keep only those lines in the agenda buffer that have a specific category.
6676 The category is that of the current line."
6677 (interactive "P")
6678 (if org-agenda-filtered-by-top-category
6679 (progn
6680 (setq org-agenda-filtered-by-top-category nil
6681 org-agenda-top-category-filter nil)
6682 (org-agenda-filter-show-all-cat))
6683 (let ((cat (org-find-top-category (org-get-at-bol 'org-hd-marker))))
6684 (if cat (org-agenda-filter-top-category-apply cat strip)
6685 (error "No top-level category at point")))))
6687 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6688 "Keep only those lines in the agenda buffer that have a specific tag.
6689 The tag is selected with its fast selection letter, as configured.
6690 With prefix argument STRIP, remove all lines that do have the tag.
6691 A lisp caller can specify CHAR. NARROW means that the new tag should be
6692 used to narrow the search - the interactive user can also press `-' or `+'
6693 to switch to narrowing."
6694 (interactive "P")
6695 (let* ((alist org-tag-alist-for-agenda)
6696 (tag-chars (mapconcat
6697 (lambda (x) (if (and (not (symbolp (car x)))
6698 (cdr x))
6699 (char-to-string (cdr x))
6700 ""))
6701 alist ""))
6702 (efforts (org-split-string
6703 (or (cdr (assoc (concat org-effort-property "_ALL")
6704 org-global-properties))
6705 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6706 "")))
6707 (effort-op org-agenda-filter-effort-default-operator)
6708 (effort-prompt "")
6709 (inhibit-read-only t)
6710 (current org-agenda-tag-filter)
6711 maybe-refresh a n tag)
6712 (unless char
6713 (message
6714 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6715 (if narrow "Narrow" "Filter") tag-chars
6716 (if org-agenda-auto-exclude-function "[RET], " ""))
6717 (setq char (read-char-exclusive)))
6718 (when (member char '(?+ ?-))
6719 ;; Narrowing down
6720 (cond ((equal char ?-) (setq strip t narrow t))
6721 ((equal char ?+) (setq strip nil narrow t)))
6722 (message
6723 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6724 (setq char (read-char-exclusive)))
6725 (when (member char '(?< ?> ?= ??))
6726 ;; An effort operator
6727 (setq effort-op (char-to-string char))
6728 (setq alist nil) ; to make sure it will be interpreted as effort.
6729 (unless (equal char ??)
6730 (loop for i from 0 to 9 do
6731 (setq effort-prompt
6732 (concat
6733 effort-prompt " ["
6734 (if (= i 9) "0" (int-to-string (1+ i)))
6735 "]" (nth i efforts))))
6736 (message "Effort%s: %s " effort-op effort-prompt)
6737 (setq char (read-char-exclusive))
6738 (when (or (< char ?0) (> char ?9))
6739 (error "Need 1-9,0 to select effort"))))
6740 (when (equal char ?\t)
6741 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6742 (org-set-local 'org-global-tags-completion-table
6743 (org-global-tags-completion-table)))
6744 (let ((completion-ignore-case t))
6745 (setq tag (org-icompleting-read
6746 "Tag: " org-global-tags-completion-table))))
6747 (cond
6748 ((equal char ?\r)
6749 (org-agenda-filter-show-all-tag)
6750 (when org-agenda-auto-exclude-function
6751 (setq org-agenda-tag-filter '())
6752 (dolist (tag (org-agenda-get-represented-tags))
6753 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6754 (if modifier
6755 (push modifier org-agenda-tag-filter))))
6756 (if (not (null org-agenda-tag-filter))
6757 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
6758 (setq maybe-refresh t))
6759 ((equal char ?/)
6760 (org-agenda-filter-show-all-tag)
6761 (when (get 'org-agenda-tag-filter :preset-filter)
6762 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
6763 (setq maybe-refresh t))
6764 ((equal char ?. )
6765 (setq org-agenda-tag-filter
6766 (mapcar (lambda(tag) (concat "+" tag))
6767 (org-get-at-bol 'tags)))
6768 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6769 (setq maybe-refresh t))
6770 ((or (equal char ?\ )
6771 (setq a (rassoc char alist))
6772 (and (>= char ?0) (<= char ?9)
6773 (setq n (if (= char ?0) 9 (- char ?0 1))
6774 tag (concat effort-op (nth n efforts))
6775 a (cons tag nil)))
6776 (and (= char ??)
6777 (setq tag "?eff")
6778 a (cons tag nil))
6779 (and tag (setq a (cons tag nil))))
6780 (org-agenda-filter-show-all-tag)
6781 (setq tag (car a))
6782 (setq org-agenda-tag-filter
6783 (cons (concat (if strip "-" "+") tag)
6784 (if narrow current nil)))
6785 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
6786 (setq maybe-refresh t))
6787 (t (error "Invalid tag selection character %c" char)))
6788 (when (and maybe-refresh
6789 (eq org-agenda-clockreport-mode 'with-filter))
6790 (org-agenda-redo))))
6792 (defun org-agenda-get-represented-tags ()
6793 "Get a list of all tags currently represented in the agenda."
6794 (let (p tags)
6795 (save-excursion
6796 (goto-char (point-min))
6797 (while (setq p (next-single-property-change (point) 'tags))
6798 (goto-char p)
6799 (mapc (lambda (x) (add-to-list 'tags x))
6800 (get-text-property (point) 'tags))))
6801 tags))
6803 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6804 "Refine the current filter. See `org-agenda-filter-by-tag'."
6805 (interactive "P")
6806 (org-agenda-filter-by-tag strip char 'refine))
6808 (defun org-agenda-filter-make-matcher ()
6809 "Create the form that tests a line for agenda filter."
6810 (let (f f1)
6811 ;; first compute the tag-filter matcher
6812 (dolist (x (delete-dups
6813 (append (get 'org-agenda-tag-filter
6814 :preset-filter) org-agenda-tag-filter)))
6815 (if (member x '("-" "+"))
6816 (setq f1 (if (equal x "-") 'tags '(not tags)))
6817 (if (string-match "[<=>?]" x)
6818 (setq f1 (org-agenda-filter-effort-form x))
6819 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6820 (if (equal (string-to-char x) ?-)
6821 (setq f1 (list 'not f1))))
6822 (push f1 f))
6823 ;; then compute the category-filter matcher
6824 (dolist (x (delete-dups
6825 (append (get 'org-agenda-category-filter
6826 :preset-filter) org-agenda-category-filter)))
6827 (if (equal "-" (substring x 0 1))
6828 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
6829 (setq f1 (list 'equal (substring x 1) 'cat)))
6830 (push f1 f))
6831 (cons 'and (nreverse f))))
6833 (defun org-agenda-filter-effort-form (e)
6834 "Return the form to compare the effort of the current line with what E says.
6835 E looks like \"+<2:25\"."
6836 (let (op)
6837 (setq e (substring e 1))
6838 (setq op (string-to-char e) e (substring e 1))
6839 (setq op (cond ((equal op ?<) '<=)
6840 ((equal op ?>) '>=)
6841 ((equal op ??) op)
6842 (t '=)))
6843 (list 'org-agenda-compare-effort (list 'quote op)
6844 (org-duration-string-to-minutes e))))
6846 (defun org-agenda-compare-effort (op value)
6847 "Compare the effort of the current line with VALUE, using OP.
6848 If the line does not have an effort defined, return nil."
6849 (let ((eff (org-get-at-bol 'effort-minutes)))
6850 (if (equal op ??)
6851 (not eff)
6852 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6853 value))))
6855 (defun org-agenda-filter-apply (filter type)
6856 "Set FILTER as the new agenda filter and apply it."
6857 (let (tags cat)
6858 (if (eq type 'tag)
6859 (setq org-agenda-tag-filter filter)
6860 (setq org-agenda-category-filter filter))
6861 (setq org-agenda-filter-form (org-agenda-filter-make-matcher))
6862 (if (and (eq type 'category)
6863 (not (equal (substring (car filter) 0 1) "-")))
6864 ;; Only set `org-agenda-filtered-by-category' to t
6865 ;; when a unique category is used as the filter
6866 (setq org-agenda-filtered-by-category t))
6867 (org-agenda-set-mode-name)
6868 (save-excursion
6869 (goto-char (point-min))
6870 (while (not (eobp))
6871 (if (org-get-at-bol 'org-marker)
6872 (progn
6873 (setq tags (org-get-at-bol 'tags) ; used in eval
6874 cat (get-text-property (point) 'org-category))
6875 (if (not (eval org-agenda-filter-form))
6876 (org-agenda-filter-hide-line type))
6877 (beginning-of-line 2))
6878 (beginning-of-line 2))))
6879 (if (get-char-property (point) 'invisible)
6880 (ignore-errors (org-agenda-previous-line)))))
6882 (defun org-agenda-filter-top-category-apply (category &optional negative)
6883 "Set FILTER as the new agenda filter and apply it."
6884 (org-agenda-set-mode-name)
6885 (save-excursion
6886 (goto-char (point-min))
6887 (while (not (eobp))
6888 (let* ((pos (org-get-at-bol 'org-hd-marker))
6889 (topcat (and pos (org-find-top-category pos))))
6890 (if (and topcat (funcall (if negative 'identity 'not)
6891 (string= category topcat)))
6892 (org-agenda-filter-hide-line 'category)))
6893 (beginning-of-line 2)))
6894 (if (get-char-property (point) 'invisible)
6895 (org-agenda-previous-line))
6896 (setq org-agenda-top-category-filter category
6897 org-agenda-filtered-by-top-category t))
6899 (defun org-agenda-filter-hide-line (type)
6900 (let (ov)
6901 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6902 (point-at-eol)))
6903 (overlay-put ov 'invisible t)
6904 (overlay-put ov 'type type)
6905 (if (eq type 'tag)
6906 (push ov org-agenda-tag-filter-overlays)
6907 (push ov org-agenda-cat-filter-overlays))))
6909 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6910 (setq pos (or pos (point)))
6911 (save-excursion
6912 (dolist (ov (overlays-at pos))
6913 (when (and (overlay-get ov 'invisible)
6914 (eq (overlay-get ov 'type) 'tag))
6915 (goto-char pos)
6916 (if (< (overlay-start ov) (point-at-eol))
6917 (move-overlay ov (point-at-eol)
6918 (overlay-end ov)))))))
6920 (defun org-agenda-filter-show-all-tag nil
6921 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
6922 (setq org-agenda-tag-filter-overlays nil
6923 org-agenda-tag-filter nil
6924 org-agenda-filter-form nil)
6925 (org-agenda-set-mode-name))
6927 (defun org-agenda-filter-show-all-cat nil
6928 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
6929 (setq org-agenda-cat-filter-overlays nil
6930 org-agenda-filtered-by-category nil
6931 org-agenda-category-filter nil
6932 org-agenda-filter-form nil)
6933 (org-agenda-set-mode-name))
6935 (defun org-agenda-manipulate-query-add ()
6936 "Manipulate the query by adding a search term with positive selection.
6937 Positive selection means the term must be matched for selection of an entry."
6938 (interactive)
6939 (org-agenda-manipulate-query ?\[))
6940 (defun org-agenda-manipulate-query-subtract ()
6941 "Manipulate the query by adding a search term with negative selection.
6942 Negative selection means term must not be matched for selection of an entry."
6943 (interactive)
6944 (org-agenda-manipulate-query ?\]))
6945 (defun org-agenda-manipulate-query-add-re ()
6946 "Manipulate the query by adding a search regexp with positive selection.
6947 Positive selection means the regexp must match for selection of an entry."
6948 (interactive)
6949 (org-agenda-manipulate-query ?\{))
6950 (defun org-agenda-manipulate-query-subtract-re ()
6951 "Manipulate the query by adding a search regexp with negative selection.
6952 Negative selection means regexp must not match for selection of an entry."
6953 (interactive)
6954 (org-agenda-manipulate-query ?\}))
6955 (defun org-agenda-manipulate-query (char)
6956 (cond
6957 ((memq org-agenda-type '(timeline agenda))
6958 (let ((org-agenda-include-inactive-timestamps t))
6959 (org-agenda-redo))
6960 (message "Display now includes inactive timestamps as well"))
6961 ((eq org-agenda-type 'search)
6962 (org-add-to-string
6963 'org-agenda-query-string
6964 (if org-agenda-last-search-view-search-was-boolean
6965 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6966 (?\{ . " +{}") (?\} . " -{}"))))
6967 " "))
6968 (setq org-agenda-redo-command
6969 (list 'org-search-view
6970 (car (get-text-property (min (1- (point-max)) (point))
6971 'org-last-args))
6972 org-agenda-query-string
6973 (+ (length org-agenda-query-string)
6974 (if (member char '(?\{ ?\})) 0 1))))
6975 (set-register org-agenda-query-register org-agenda-query-string)
6976 (let ((org-agenda-overriding-arguments
6977 (cdr org-agenda-redo-command)))
6978 (org-agenda-redo)))
6979 (t (error "Cannot manipulate query for %s-type agenda buffers"
6980 org-agenda-type))))
6982 (defun org-add-to-string (var string)
6983 (set var (concat (symbol-value var) string)))
6985 (defun org-agenda-goto-date (span)
6986 "Jump to DATE in agenda."
6987 (interactive "P")
6988 (let* ((org-read-date-prefer-future
6989 (eval org-agenda-jump-prefer-future))
6990 (date (org-read-date))
6991 (org-agenda-sticky-orig org-agenda-sticky)
6992 (org-agenda-buffer-tmp-name (buffer-name))
6993 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
6994 (0-arg (or current-prefix-arg (car args)))
6995 (2-arg (nth 2 args))
6996 (newcmd (list 'org-agenda-list 0-arg date
6997 (org-agenda-span-to-ndays 2-arg)))
6998 (newargs (cdr newcmd))
6999 (inhibit-read-only t)
7000 org-agenda-sticky)
7001 (if (not (org-agenda-check-type t 'agenda))
7002 (error "Not available in non-agenda blocks")
7003 (add-text-properties (point-min) (point-max)
7004 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7005 (org-agenda-redo)
7006 (setq org-agenda-sticky org-agenda-sticky-orig
7007 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7009 (defun org-agenda-goto-today ()
7010 "Go to today."
7011 (interactive)
7012 (org-agenda-check-type t 'timeline 'agenda)
7013 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7014 (curspan (nth 2 args))
7015 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7016 (cond
7017 (tdpos (goto-char tdpos))
7018 ((eq org-agenda-type 'agenda)
7019 (let* ((sd (org-agenda-compute-starting-span
7020 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7021 (org-agenda-overriding-arguments args))
7022 (setf (nth 1 org-agenda-overriding-arguments) sd)
7023 (org-agenda-redo)
7024 (org-agenda-find-same-or-today-or-agenda)))
7025 (t (error "Cannot find today")))))
7027 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7028 (goto-char
7029 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7030 (text-property-any (point-min) (point-max) 'org-today t)
7031 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7032 (and (get-text-property (min (1- (point-max)) (point)) 'org-serie)
7033 (org-agenda-goto-block-beginning))
7034 (point-min))))
7036 (defun org-agenda-goto-block-beginning ()
7037 "Go the agenda block beginning."
7038 (interactive)
7039 (if (not (derived-mode-p 'org-agenda-mode))
7040 (error "Cannot execute this command outside of org-agenda-mode buffers")
7041 (let (dest)
7042 (save-excursion
7043 (unless (looking-at "\\'")
7044 (forward-char))
7045 (let* ((prop 'org-agenda-structural-header)
7046 (p (previous-single-property-change (point) prop))
7047 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7048 (1- (point))) prop)))
7049 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7050 (if (not dest)
7051 (error "Cannot find the beginning of the blog")
7052 (goto-char dest)
7053 (move-beginning-of-line 1)))))
7055 (defun org-agenda-later (arg)
7056 "Go forward in time by thee current span.
7057 With prefix ARG, go forward that many times the current span."
7058 (interactive "p")
7059 (org-agenda-check-type t 'agenda)
7060 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7061 (span (or (nth 2 args) org-agenda-current-span))
7062 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7063 (greg (calendar-gregorian-from-absolute sd))
7064 (cnt (org-get-at-bol 'org-day-cnt))
7065 greg2)
7066 (cond
7067 ((numberp span)
7068 (setq sd (+ span sd)))
7069 ((eq span 'day)
7070 (setq sd (+ arg sd)))
7071 ((eq span 'week)
7072 (setq sd (+ (* 7 arg) sd)))
7073 ((eq span 'month)
7074 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7075 sd (calendar-absolute-from-gregorian greg2))
7076 (setcar greg2 (1+ (car greg2))))
7077 ((eq span 'year)
7078 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7079 sd (calendar-absolute-from-gregorian greg2))
7080 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7082 (setq sd (+ (* span arg) sd))))
7083 (let ((org-agenda-overriding-cmd
7084 ;; `cmd' may have been set by `org-agenda-run-series' which
7085 ;; uses `org-agenda-overriding-cmd' to decide whether
7086 ;; overriding is allowed for `cmd'
7087 (get-text-property (min (1- (point-max)) (point)) 'org-serie-cmd))
7088 (org-agenda-overriding-arguments
7089 (list (car args) sd span)))
7090 (org-agenda-redo)
7091 (org-agenda-find-same-or-today-or-agenda cnt))))
7093 (defun org-agenda-earlier (arg)
7094 "Go backward in time by the current span.
7095 With prefix ARG, go backward that many times the current span."
7096 (interactive "p")
7097 (org-agenda-later (- arg)))
7099 (defun org-agenda-view-mode-dispatch ()
7100 "Call one of the view mode commands."
7101 (interactive)
7102 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7103 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7104 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7105 (let ((a (read-char-exclusive)))
7106 (case a
7107 (?\ (call-interactively 'org-agenda-reset-view))
7108 (?d (call-interactively 'org-agenda-day-view))
7109 (?w (call-interactively 'org-agenda-week-view))
7110 (?m (call-interactively 'org-agenda-month-view))
7111 (?y (call-interactively 'org-agenda-year-view))
7112 (?l (call-interactively 'org-agenda-log-mode))
7113 (?L (org-agenda-log-mode '(4)))
7114 (?c (org-agenda-log-mode 'clockcheck))
7115 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7116 (?a (call-interactively 'org-agenda-archives-mode))
7117 (?A (org-agenda-archives-mode 'files))
7118 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7119 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7120 (?G (call-interactively 'org-agenda-toggle-time-grid))
7121 (?D (call-interactively 'org-agenda-toggle-diary))
7122 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7123 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7124 (org-agenda-check-type t 'timeline 'agenda)
7125 (org-agenda-redo))
7126 (message "Display now includes inactive timestamps as well"))
7127 (?q (message "Abort"))
7128 (otherwise (error "Invalid key" )))))
7130 (defun org-agenda-reset-view ()
7131 "Switch to default view for agenda."
7132 (interactive)
7133 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7134 (defun org-agenda-day-view (&optional day-of-year)
7135 "Switch to daily view for agenda.
7136 With argument DAY-OF-YEAR, switch to that day of the year."
7137 (interactive "P")
7138 (org-agenda-change-time-span 'day day-of-year))
7139 (defun org-agenda-week-view (&optional iso-week)
7140 "Switch to daily view for agenda.
7141 With argument ISO-WEEK, switch to the corresponding ISO week.
7142 If ISO-WEEK has more then 2 digits, only the last two encode the
7143 week. Any digits before this encode a year. So 200712 means
7144 week 12 of year 2007. Years in the range 1938-2037 can also be
7145 written as 2-digit years."
7146 (interactive "P")
7147 (org-agenda-change-time-span 'week iso-week))
7148 (defun org-agenda-month-view (&optional month)
7149 "Switch to monthly view for agenda.
7150 With argument MONTH, switch to that month."
7151 (interactive "P")
7152 (org-agenda-change-time-span 'month month))
7153 (defun org-agenda-year-view (&optional year)
7154 "Switch to yearly view for agenda.
7155 With argument YEAR, switch to that year.
7156 If MONTH has more then 2 digits, only the last two encode the
7157 month. Any digits before this encode a year. So 200712 means
7158 December year 2007. Years in the range 1938-2037 can also be
7159 written as 2-digit years."
7160 (interactive "P")
7161 (when year
7162 (setq year (org-small-year-to-year year)))
7163 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7164 (org-agenda-change-time-span 'year year)
7165 (error "Abort")))
7167 (defun org-agenda-change-time-span (span &optional n)
7168 "Change the agenda view to SPAN.
7169 SPAN may be `day', `week', `month', `year'."
7170 (org-agenda-check-type t 'agenda)
7171 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7172 (curspan (nth 2 args)))
7173 (if (and (not n) (equal curspan span))
7174 (error "Viewing span is already \"%s\"" span))
7175 (let* ((sd (or (org-get-at-bol 'day)
7176 (nth 1 args)
7177 org-starting-day))
7178 (sd (org-agenda-compute-starting-span sd span n))
7179 (org-agenda-overriding-cmd
7180 (get-text-property (min (1- (point-max)) (point)) 'org-serie-cmd))
7181 (org-agenda-overriding-arguments
7182 (list (car args) sd span)))
7183 (org-agenda-redo)
7184 (org-agenda-find-same-or-today-or-agenda))
7185 (org-agenda-set-mode-name)
7186 (message "Switched to %s view" span)))
7188 (defun org-agenda-compute-starting-span (sd span &optional n)
7189 "Compute starting date for agenda.
7190 SPAN may be `day', `week', `month', `year'. The return value
7191 is a cons cell with the starting date and the number of days,
7192 so that the date SD will be in that range."
7193 (let* ((greg (calendar-gregorian-from-absolute sd))
7194 (dg (nth 1 greg))
7195 (mg (car greg))
7196 (yg (nth 2 greg)))
7197 (cond
7198 ((eq span 'day)
7199 (when n
7200 (setq sd (+ (calendar-absolute-from-gregorian
7201 (list mg 1 yg))
7202 n -1))))
7203 ((eq span 'week)
7204 (let* ((nt (calendar-day-of-week
7205 (calendar-gregorian-from-absolute sd)))
7206 (d (if org-agenda-start-on-weekday
7207 (- nt org-agenda-start-on-weekday)
7210 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7211 (when n
7212 (require 'cal-iso)
7213 (when (> n 99)
7214 (setq y1 (org-small-year-to-year (/ n 100))
7215 n (mod n 100)))
7216 (setq sd
7217 (calendar-absolute-from-iso
7218 (list n 1
7219 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7220 ((eq span 'month)
7221 (let (y1)
7222 (when (and n (> n 99))
7223 (setq y1 (org-small-year-to-year (/ n 100))
7224 n (mod n 100)))
7225 (setq sd (calendar-absolute-from-gregorian
7226 (list (or n mg) 1 (or y1 yg))))))
7227 ((eq span 'year)
7228 (setq sd (calendar-absolute-from-gregorian
7229 (list 1 1 (or n yg))))))
7230 sd))
7232 (defun org-agenda-next-date-line (&optional arg)
7233 "Jump to the next line indicating a date in agenda buffer."
7234 (interactive "p")
7235 (org-agenda-check-type t 'agenda 'timeline)
7236 (beginning-of-line 1)
7237 ;; This does not work if user makes date format that starts with a blank
7238 (if (looking-at "^\\S-") (forward-char 1))
7239 (if (not (re-search-forward "^\\S-" nil t arg))
7240 (progn
7241 (backward-char 1)
7242 (error "No next date after this line in this buffer")))
7243 (goto-char (match-beginning 0)))
7245 (defun org-agenda-previous-date-line (&optional arg)
7246 "Jump to the previous line indicating a date in agenda buffer."
7247 (interactive "p")
7248 (org-agenda-check-type t 'agenda 'timeline)
7249 (beginning-of-line 1)
7250 (if (not (re-search-backward "^\\S-" nil t arg))
7251 (error "No previous date before this line in this buffer")))
7253 ;; Initialize the highlight
7254 (defvar org-hl (make-overlay 1 1))
7255 (overlay-put org-hl 'face 'highlight)
7257 (defun org-highlight (begin end &optional buffer)
7258 "Highlight a region with overlay."
7259 (move-overlay org-hl begin end (or buffer (current-buffer))))
7261 (defun org-unhighlight ()
7262 "Detach overlay INDEX."
7263 (org-detach-overlay org-hl))
7265 (defun org-unhighlight-once ()
7266 "Remove the highlight from its position, and this function from the hook."
7267 (remove-hook 'pre-command-hook 'org-unhighlight-once)
7268 (org-unhighlight))
7270 (defun org-agenda-follow-mode ()
7271 "Toggle follow mode in an agenda buffer."
7272 (interactive)
7273 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
7274 (org-agenda-set-mode-name)
7275 (org-agenda-do-context-action)
7276 (message "Follow mode is %s"
7277 (if org-agenda-follow-mode "on" "off")))
7279 (defun org-agenda-entry-text-mode (&optional arg)
7280 "Toggle entry text mode in an agenda buffer."
7281 (interactive "P")
7282 (setq org-agenda-entry-text-mode (or (integerp arg)
7283 (not org-agenda-entry-text-mode)))
7284 (org-agenda-entry-text-hide)
7285 (and org-agenda-entry-text-mode
7286 (let ((org-agenda-entry-text-maxlines
7287 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7288 (org-agenda-entry-text-show)))
7289 (org-agenda-set-mode-name)
7290 (message "Entry text mode is %s. Maximum number of lines is %d"
7291 (if org-agenda-entry-text-mode "on" "off")
7292 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
7294 (defun org-agenda-clockreport-mode (&optional with-filter)
7295 "Toggle clocktable mode in an agenda buffer.
7296 With prefix arg WITH-FILTER, make the clocktable respect the current
7297 agenda filter."
7298 (interactive "P")
7299 (org-agenda-check-type t 'agenda)
7300 (if with-filter
7301 (setq org-agenda-clockreport-mode 'with-filter)
7302 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
7303 (org-agenda-set-mode-name)
7304 (org-agenda-redo)
7305 (message "Clocktable mode is %s"
7306 (if org-agenda-clockreport-mode "on" "off")))
7308 (defun org-agenda-log-mode (&optional special)
7309 "Toggle log mode in an agenda buffer.
7310 With argument SPECIAL, show all possible log items, not only the ones
7311 configured in `org-agenda-log-mode-items'.
7312 With a double `C-u' prefix arg, show *only* log items, nothing else."
7313 (interactive "P")
7314 (org-agenda-check-type t 'agenda 'timeline)
7315 (setq org-agenda-show-log
7316 (cond
7317 ((equal special '(16)) 'only)
7318 ((eq special 'clockcheck)
7319 (if (eq org-agenda-show-log 'clockcheck)
7320 nil 'clockcheck))
7321 (special '(closed clock state))
7322 (t (not org-agenda-show-log))))
7323 (org-agenda-set-mode-name)
7324 (org-agenda-redo)
7325 (message "Log mode is %s"
7326 (if org-agenda-show-log "on" "off")))
7328 (defun org-agenda-archives-mode (&optional with-files)
7329 "Toggle inclusion of items in trees marked with :ARCHIVE:.
7330 When called with a prefix argument, include all archive files as well."
7331 (interactive "P")
7332 (setq org-agenda-archives-mode
7333 (if with-files t (if org-agenda-archives-mode nil 'trees)))
7334 (org-agenda-set-mode-name)
7335 (org-agenda-redo)
7336 (message
7337 "%s"
7338 (cond
7339 ((eq org-agenda-archives-mode nil)
7340 "No archives are included")
7341 ((eq org-agenda-archives-mode 'trees)
7342 (format "Trees with :%s: tag are included" org-archive-tag))
7343 ((eq org-agenda-archives-mode t)
7344 (format "Trees with :%s: tag and all active archive files are included"
7345 org-archive-tag)))))
7347 (defun org-agenda-toggle-diary ()
7348 "Toggle diary inclusion in an agenda buffer."
7349 (interactive)
7350 (org-agenda-check-type t 'agenda)
7351 (setq org-agenda-include-diary (not org-agenda-include-diary))
7352 (org-agenda-redo)
7353 (org-agenda-set-mode-name)
7354 (message "Diary inclusion turned %s"
7355 (if org-agenda-include-diary "on" "off")))
7357 (defun org-agenda-toggle-deadlines ()
7358 "Toggle inclusion of entries with a deadline in an agenda buffer."
7359 (interactive)
7360 (org-agenda-check-type t 'agenda)
7361 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
7362 (org-agenda-redo)
7363 (org-agenda-set-mode-name)
7364 (message "Deadlines inclusion turned %s"
7365 (if org-agenda-include-deadlines "on" "off")))
7367 (defun org-agenda-toggle-time-grid ()
7368 "Toggle time grid in an agenda buffer."
7369 (interactive)
7370 (org-agenda-check-type t 'agenda)
7371 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
7372 (org-agenda-redo)
7373 (org-agenda-set-mode-name)
7374 (message "Time-grid turned %s"
7375 (if org-agenda-use-time-grid "on" "off")))
7377 (defun org-agenda-set-mode-name ()
7378 "Set the mode name to indicate all the small mode settings."
7379 (setq mode-name
7380 (list "Org-Agenda"
7381 (if (get 'org-agenda-files 'org-restrict) " []" "")
7383 '(:eval (org-agenda-span-name org-agenda-current-span))
7384 (if org-agenda-follow-mode " Follow" "")
7385 (if org-agenda-entry-text-mode " ETxt" "")
7386 (if org-agenda-include-diary " Diary" "")
7387 (if org-agenda-include-deadlines " Ddl" "")
7388 (if org-agenda-use-time-grid " Grid" "")
7389 (if (and (boundp 'org-habit-show-habits)
7390 org-habit-show-habits) " Habit" "")
7391 (cond
7392 ((consp org-agenda-show-log) " LogAll")
7393 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
7394 (org-agenda-show-log " Log")
7395 (t ""))
7396 (if (or org-agenda-category-filter (get 'org-agenda-category-filter
7397 :preset-filter))
7398 '(:eval (org-propertize
7399 (concat " <"
7400 (mapconcat
7401 'identity
7402 (append
7403 (get 'org-agenda-category-filter :preset-filter)
7404 org-agenda-category-filter)
7406 ">")
7407 'face 'org-agenda-filter-category
7408 'help-echo "Category used in filtering"))
7410 (if (or org-agenda-tag-filter (get 'org-agenda-tag-filter
7411 :preset-filter))
7412 '(:eval (org-propertize
7413 (concat " {"
7414 (mapconcat
7415 'identity
7416 (append
7417 (get 'org-agenda-tag-filter :preset-filter)
7418 org-agenda-tag-filter)
7420 "}")
7421 'face 'org-agenda-filter-tags
7422 'help-echo "Tags used in filtering"))
7424 (if org-agenda-archives-mode
7425 (if (eq org-agenda-archives-mode t)
7426 " Archives"
7427 (format " :%s:" org-archive-tag))
7429 (if org-agenda-clockreport-mode
7430 (if (eq org-agenda-clockreport-mode 'with-filter)
7431 " Clock{}" " Clock")
7432 "")))
7433 (force-mode-line-update))
7435 (define-obsolete-function-alias
7436 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
7438 (defun org-agenda-update-agenda-type ()
7439 "Update the agenda type after each command."
7440 (setq org-agenda-type
7441 (or (get-text-property (point) 'org-agenda-type)
7442 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
7444 (defun org-agenda-next-line ()
7445 "Move cursor to the next line, and show if follow mode is active."
7446 (interactive)
7447 (call-interactively 'next-line)
7448 (org-agenda-do-context-action))
7450 (defun org-agenda-previous-line ()
7451 "Move cursor to the previous line, and show if follow-mode is active."
7452 (interactive)
7453 (call-interactively 'previous-line)
7454 (org-agenda-do-context-action))
7456 (defun org-agenda-next-item (n)
7457 "Move cursor to next agenda item."
7458 (interactive "p")
7459 (let ((col (current-column)))
7460 (dotimes (c n)
7461 (when (next-single-property-change (point-at-eol) 'org-marker)
7462 (move-end-of-line 1)
7463 (goto-char (next-single-property-change (point) 'org-marker))))
7464 (org-move-to-column col))
7465 (org-agenda-do-context-action))
7467 (defun org-agenda-previous-item (n)
7468 "Move cursor to next agenda item."
7469 (interactive "p")
7470 (dotimes (c n)
7471 (let ((col (current-column))
7472 (goto (save-excursion
7473 (move-end-of-line 0)
7474 (previous-single-property-change (point) 'org-marker))))
7475 (if goto (goto-char goto))
7476 (org-move-to-column col)))
7477 (org-agenda-do-context-action))
7479 (defun org-agenda-do-context-action ()
7480 "Show outline path and, maybe, follow mode window."
7481 (let ((m (org-get-at-bol 'org-marker)))
7482 (when (and (markerp m) (marker-buffer m))
7483 (and org-agenda-follow-mode
7484 (if org-agenda-follow-indirect
7485 (org-agenda-tree-to-indirect-buffer nil)
7486 (org-agenda-show)))
7487 (and org-agenda-show-outline-path
7488 (org-with-point-at m (org-display-outline-path t))))))
7490 (defun org-agenda-show-tags ()
7491 "Show the tags applicable to the current item."
7492 (interactive)
7493 (let* ((tags (org-get-at-bol 'tags)))
7494 (if tags
7495 (message "Tags are :%s:"
7496 (org-no-properties (mapconcat 'identity tags ":")))
7497 (message "No tags associated with this line"))))
7499 (defun org-agenda-goto (&optional highlight)
7500 "Go to the Org-mode file which contains the item at point."
7501 (interactive)
7502 (let* ((marker (or (org-get-at-bol 'org-marker)
7503 (org-agenda-error)))
7504 (buffer (marker-buffer marker))
7505 (pos (marker-position marker)))
7506 (switch-to-buffer-other-window buffer)
7507 (widen)
7508 (push-mark)
7509 (goto-char pos)
7510 (when (derived-mode-p 'org-mode)
7511 (org-show-context 'agenda)
7512 (save-excursion
7513 (and (outline-next-heading)
7514 (org-flag-heading nil)))) ; show the next heading
7515 (when (outline-invisible-p)
7516 (show-entry)) ; display invisible text
7517 (recenter (/ (window-height) 2))
7518 (run-hooks 'org-agenda-after-show-hook)
7519 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
7521 (defvar org-agenda-after-show-hook nil
7522 "Normal hook run after an item has been shown from the agenda.
7523 Point is in the buffer where the item originated.")
7525 (defun org-agenda-kill ()
7526 "Kill the entry or subtree belonging to the current agenda entry."
7527 (interactive)
7528 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7529 (let* ((bufname-orig (buffer-name))
7530 (marker (or (org-get-at-bol 'org-marker)
7531 (org-agenda-error)))
7532 (buffer (marker-buffer marker))
7533 (pos (marker-position marker))
7534 (type (org-get-at-bol 'type))
7535 dbeg dend (n 0) conf)
7536 (org-with-remote-undo buffer
7537 (with-current-buffer buffer
7538 (save-excursion
7539 (goto-char pos)
7540 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
7541 (setq dbeg (progn (org-back-to-heading t) (point))
7542 dend (org-end-of-subtree t t))
7543 (setq dbeg (point-at-bol)
7544 dend (min (point-max) (1+ (point-at-eol)))))
7545 (goto-char dbeg)
7546 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
7547 (setq conf (or (eq t org-agenda-confirm-kill)
7548 (and (numberp org-agenda-confirm-kill)
7549 (> n org-agenda-confirm-kill))))
7550 (and conf
7551 (not (y-or-n-p
7552 (format "Delete entry with %d lines in buffer \"%s\"? "
7553 n (buffer-name buffer))))
7554 (error "Abort"))
7555 (let ((org-agenda-buffer-name bufname-orig))
7556 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
7557 (with-current-buffer buffer (delete-region dbeg dend))
7558 (message "Agenda item and source killed"))))
7560 (defvar org-archive-default-command) ; defined in org-archive.el
7561 (defun org-agenda-archive-default ()
7562 "Archive the entry or subtree belonging to the current agenda entry."
7563 (interactive)
7564 (require 'org-archive)
7565 (org-agenda-archive-with org-archive-default-command))
7567 (defun org-agenda-archive-default-with-confirmation ()
7568 "Archive the entry or subtree belonging to the current agenda entry."
7569 (interactive)
7570 (require 'org-archive)
7571 (org-agenda-archive-with org-archive-default-command 'confirm))
7573 (defun org-agenda-archive ()
7574 "Archive the entry or subtree belonging to the current agenda entry."
7575 (interactive)
7576 (org-agenda-archive-with 'org-archive-subtree))
7578 (defun org-agenda-archive-to-archive-sibling ()
7579 "Move the entry to the archive sibling."
7580 (interactive)
7581 (org-agenda-archive-with 'org-archive-to-archive-sibling))
7583 (defun org-agenda-archive-with (cmd &optional confirm)
7584 "Move the entry to the archive sibling."
7585 (interactive)
7586 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
7587 (let* ((bufname-orig (buffer-name))
7588 (marker (or (org-get-at-bol 'org-marker)
7589 (org-agenda-error)))
7590 (buffer (marker-buffer marker))
7591 (pos (marker-position marker)))
7592 (org-with-remote-undo buffer
7593 (with-current-buffer buffer
7594 (if (derived-mode-p 'org-mode)
7595 (if (and confirm
7596 (not (y-or-n-p "Archive this subtree or entry? ")))
7597 (error "Abort")
7598 (save-excursion
7599 (goto-char pos)
7600 (let ((org-agenda-buffer-name bufname-orig))
7601 (org-remove-subtree-entries-from-agenda))
7602 (org-back-to-heading t)
7603 (funcall cmd)))
7604 (error "Archiving works only in Org-mode files"))))))
7606 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
7607 "Remove all lines in the agenda that correspond to a given subtree.
7608 The subtree is the one in buffer BUF, starting at BEG and ending at END.
7609 If this information is not given, the function uses the tree at point."
7610 (let ((buf (or buf (current-buffer))) m p)
7611 (save-excursion
7612 (unless (and beg end)
7613 (org-back-to-heading t)
7614 (setq beg (point))
7615 (org-end-of-subtree t)
7616 (setq end (point)))
7617 (set-buffer (get-buffer org-agenda-buffer-name))
7618 (save-excursion
7619 (goto-char (point-max))
7620 (beginning-of-line 1)
7621 (while (not (bobp))
7622 (when (and (setq m (org-get-at-bol 'org-marker))
7623 (equal buf (marker-buffer m))
7624 (setq p (marker-position m))
7625 (>= p beg)
7626 (< p end))
7627 (let ((inhibit-read-only t))
7628 (delete-region (point-at-bol) (1+ (point-at-eol)))))
7629 (beginning-of-line 0))))))
7631 (defun org-agenda-refile (&optional goto rfloc no-update)
7632 "Refile the item at point."
7633 (interactive "P")
7634 (if (equal goto '(16))
7635 (org-refile-goto-last-stored)
7636 (let* ((buffer-orig (buffer-name))
7637 (marker (or (org-get-at-bol 'org-hd-marker)
7638 (org-agenda-error)))
7639 (buffer (marker-buffer marker))
7640 (pos (marker-position marker))
7641 (rfloc (or rfloc
7642 (org-refile-get-location
7643 (if goto "Goto" "Refile to") buffer
7644 org-refile-allow-creating-parent-nodes))))
7645 (with-current-buffer buffer
7646 (save-excursion
7647 (save-restriction
7648 (widen)
7649 (goto-char marker)
7650 (let ((org-agenda-buffer-name buffer-orig))
7651 (org-remove-subtree-entries-from-agenda))
7652 (org-refile goto buffer rfloc)))))
7653 (unless no-update (org-agenda-redo))))
7655 (defun org-agenda-open-link (&optional arg)
7656 "Follow the link in the current line, if any.
7657 This looks for a link in the displayed line in the agenda. It also looks
7658 at the text of the entry itself."
7659 (interactive "P")
7660 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
7661 (org-get-at-bol 'org-marker)))
7662 (buffer (and marker (marker-buffer marker)))
7663 (prefix (buffer-substring
7664 (point-at-bol) (point-at-eol))))
7665 (cond
7666 (buffer
7667 (with-current-buffer buffer
7668 (save-excursion
7669 (save-restriction
7670 (widen)
7671 (goto-char marker)
7672 (org-offer-links-in-entry arg prefix)))))
7673 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
7674 (save-excursion
7675 (beginning-of-line 1)
7676 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
7677 (org-open-link-from-string (match-string 1)))
7678 (t (error "No link to open here")))))
7680 (defun org-agenda-copy-local-variable (var)
7681 "Get a variable from a referenced buffer and install it here."
7682 (let ((m (org-get-at-bol 'org-marker)))
7683 (when (and m (buffer-live-p (marker-buffer m)))
7684 (org-set-local var (with-current-buffer (marker-buffer m)
7685 (symbol-value var))))))
7687 (defun org-agenda-switch-to (&optional delete-other-windows)
7688 "Go to the Org-mode file which contains the item at point."
7689 (interactive)
7690 (if (and org-return-follows-link
7691 (not (org-get-at-bol 'org-marker))
7692 (org-in-regexp org-bracket-link-regexp))
7693 (org-open-link-from-string (match-string 0))
7694 (let* ((marker (or (org-get-at-bol 'org-marker)
7695 (org-agenda-error)))
7696 (buffer (marker-buffer marker))
7697 (pos (marker-position marker)))
7698 (org-pop-to-buffer-same-window buffer)
7699 (and delete-other-windows (delete-other-windows))
7700 (widen)
7701 (goto-char pos)
7702 (when (derived-mode-p 'org-mode)
7703 (org-show-context 'agenda)
7704 (save-excursion
7705 (and (outline-next-heading)
7706 (org-flag-heading nil))) ; show the next heading
7707 (when (outline-invisible-p)
7708 (show-entry)) ; display invisible text
7709 (run-hooks 'org-agenda-after-show-hook)))))
7711 (defun org-agenda-goto-mouse (ev)
7712 "Go to the Org-mode file which contains the item at the mouse click."
7713 (interactive "e")
7714 (mouse-set-point ev)
7715 (org-agenda-goto))
7717 (defun org-agenda-show (&optional full-entry)
7718 "Display the Org-mode file which contains the item at point.
7719 With prefix argument FULL-ENTRY, make the entire entry visible
7720 if it was hidden in the outline."
7721 (interactive "P")
7722 (let ((win (selected-window)))
7723 (if full-entry
7724 (let ((org-show-entry-below t))
7725 (org-agenda-goto t))
7726 (org-agenda-goto t))
7727 (select-window win)))
7729 (defvar org-agenda-show-window nil)
7730 (defun org-agenda-show-and-scroll-up (&optional arg)
7731 "Display the Org-mode file which contains the item at point.
7732 When called repeatedly, scroll the window that is displaying the buffer.
7733 With a \\[universal-argument] prefix, use `org-show-entry' instead of
7734 `show-subtree' to display the item, so that drawers and logbooks stay
7735 folded."
7736 (interactive "P")
7737 (let ((win (selected-window)))
7738 (if (and (window-live-p org-agenda-show-window)
7739 (eq this-command last-command))
7740 (progn
7741 (select-window org-agenda-show-window)
7742 (ignore-errors (scroll-up)))
7743 (org-agenda-goto t)
7744 (if arg (org-show-entry) (show-subtree))
7745 (setq org-agenda-show-window (selected-window)))
7746 (select-window win)))
7748 (defun org-agenda-show-scroll-down ()
7749 "Scroll down the window showing the agenda."
7750 (interactive)
7751 (let ((win (selected-window)))
7752 (when (window-live-p org-agenda-show-window)
7753 (select-window org-agenda-show-window)
7754 (ignore-errors (scroll-down))
7755 (select-window win))))
7757 (defun org-agenda-show-1 (&optional more)
7758 "Display the Org-mode file which contains the item at point.
7759 The prefix arg selects the amount of information to display:
7761 0 hide the subtree
7762 1 just show the entry according to defaults.
7763 2 show the children view
7764 3 show the subtree view
7765 4 show the entire subtree and any LOGBOOK drawers
7766 5 show the entire subtree and any drawers
7767 With prefix argument FULL-ENTRY, make the entire entry visible
7768 if it was hidden in the outline."
7769 (interactive "p")
7770 (let ((win (selected-window)))
7771 (org-agenda-goto t)
7772 (org-recenter-heading 1)
7773 (cond
7774 ((= more 0)
7775 (hide-subtree)
7776 (save-excursion
7777 (org-back-to-heading)
7778 (run-hook-with-args 'org-cycle-hook 'folded))
7779 (message "Remote: FOLDED"))
7780 ((and (org-called-interactively-p 'any) (= more 1))
7781 (message "Remote: show with default settings"))
7782 ((= more 2)
7783 (show-entry)
7784 (show-children)
7785 (save-excursion
7786 (org-back-to-heading)
7787 (run-hook-with-args 'org-cycle-hook 'children))
7788 (message "Remote: CHILDREN"))
7789 ((= more 3)
7790 (show-subtree)
7791 (save-excursion
7792 (org-back-to-heading)
7793 (run-hook-with-args 'org-cycle-hook 'subtree))
7794 (message "Remote: SUBTREE"))
7795 ((= more 4)
7796 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7797 (org-drawer-regexp
7798 (concat "^[ \t]*:\\("
7799 (mapconcat 'regexp-quote org-drawers "\\|")
7800 "\\):[ \t]*$")))
7801 (show-subtree)
7802 (save-excursion
7803 (org-back-to-heading)
7804 (org-cycle-hide-drawers 'subtree)))
7805 (message "Remote: SUBTREE AND LOGBOOK"))
7806 ((> more 4)
7807 (show-subtree)
7808 (message "Remote: SUBTREE AND ALL DRAWERS")))
7809 (select-window win)))
7811 (defun org-recenter-heading (n)
7812 (save-excursion
7813 (org-back-to-heading)
7814 (recenter n)))
7816 (defvar org-agenda-cycle-counter nil)
7817 (defun org-agenda-cycle-show (&optional n)
7818 "Show the current entry in another window, with default settings.
7819 Default settings are taken from `org-show-hierarchy-above' and siblings.
7820 When use repeatedly in immediate succession, the remote entry will cycle
7821 through visibility
7823 children -> subtree -> folded
7825 When called with a numeric prefix arg, that arg will be passed through to
7826 `org-agenda-show-1'. For the interpretation of that argument, see the
7827 docstring of `org-agenda-show-1'."
7828 (interactive "P")
7829 (if (integerp n)
7830 (setq org-agenda-cycle-counter n)
7831 (if (not (eq last-command this-command))
7832 (setq org-agenda-cycle-counter 1)
7833 (if (equal org-agenda-cycle-counter 0)
7834 (setq org-agenda-cycle-counter 2)
7835 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7836 (if (> org-agenda-cycle-counter 3)
7837 (setq org-agenda-cycle-counter 0)))))
7838 (org-agenda-show-1 org-agenda-cycle-counter))
7840 (defun org-agenda-recenter (arg)
7841 "Display the Org-mode file which contains the item at point and recenter."
7842 (interactive "P")
7843 (let ((win (selected-window)))
7844 (org-agenda-goto t)
7845 (recenter arg)
7846 (select-window win)))
7848 (defun org-agenda-show-mouse (ev)
7849 "Display the Org-mode file which contains the item at the mouse click."
7850 (interactive "e")
7851 (mouse-set-point ev)
7852 (org-agenda-show))
7854 (defun org-agenda-check-no-diary ()
7855 "Check if the entry is a diary link and abort if yes."
7856 (if (org-get-at-bol 'org-agenda-diary-link)
7857 (org-agenda-error)))
7859 (defun org-agenda-error ()
7860 (error "Command not allowed in this line"))
7862 (defun org-agenda-tree-to-indirect-buffer (arg)
7863 "Show the subtree corresponding to the current entry in an indirect buffer.
7864 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
7866 With a numerical prefix ARG, go up to this level and then take that tree.
7867 With a negative numeric ARG, go up by this number of levels.
7868 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7869 use the dedicated frame)."
7870 (interactive "P")
7871 (if current-prefix-arg
7872 (org-agenda-do-tree-to-indirect-buffer arg)
7873 (let ((agenda-buffer (buffer-name))
7874 (agenda-window (selected-window))
7875 (indirect-window
7876 (and org-last-indirect-buffer
7877 (get-buffer-window org-last-indirect-buffer))))
7878 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
7879 (unless (or (eq org-indirect-buffer-display 'new-frame)
7880 (eq org-indirect-buffer-display 'dedicated-frame))
7881 (unwind-protect
7882 (progn
7883 (unless (and indirect-window (window-live-p indirect-window)))
7884 (setq indirect-window (split-window agenda-window)))
7885 (and indirect-window (select-window indirect-window))
7886 (switch-to-buffer org-last-indirect-buffer :norecord)
7887 (fit-window-to-buffer indirect-window)))
7888 (select-window (get-buffer-window agenda-buffer)))))
7890 (defun org-agenda-do-tree-to-indirect-buffer (arg)
7891 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
7892 (org-agenda-check-no-diary)
7893 (let* ((marker (or (org-get-at-bol 'org-marker)
7894 (org-agenda-error)))
7895 (buffer (marker-buffer marker))
7896 (pos (marker-position marker)))
7897 (with-current-buffer buffer
7898 (save-excursion
7899 (goto-char pos)
7900 (funcall 'org-tree-to-indirect-buffer arg)))))
7902 (defvar org-last-heading-marker (make-marker)
7903 "Marker pointing to the headline that last changed its TODO state
7904 by a remote command from the agenda.")
7906 (defun org-agenda-todo-nextset ()
7907 "Switch TODO entry to next sequence."
7908 (interactive)
7909 (org-agenda-todo 'nextset))
7911 (defun org-agenda-todo-previousset ()
7912 "Switch TODO entry to previous sequence."
7913 (interactive)
7914 (org-agenda-todo 'previousset))
7916 (defun org-agenda-todo (&optional arg)
7917 "Cycle TODO state of line at point, also in Org-mode file.
7918 This changes the line at point, all other lines in the agenda referring to
7919 the same tree node, and the headline of the tree node in the Org-mode file."
7920 (interactive "P")
7921 (org-agenda-check-no-diary)
7922 (let* ((col (current-column))
7923 (marker (or (org-get-at-bol 'org-marker)
7924 (org-agenda-error)))
7925 (buffer (marker-buffer marker))
7926 (pos (marker-position marker))
7927 (hdmarker (org-get-at-bol 'org-hd-marker))
7928 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7929 (inhibit-read-only t)
7930 org-agenda-headline-snapshot-before-repeat newhead just-one)
7931 (org-with-remote-undo buffer
7932 (with-current-buffer buffer
7933 (widen)
7934 (goto-char pos)
7935 (org-show-context 'agenda)
7936 (save-excursion
7937 (and (outline-next-heading)
7938 (org-flag-heading nil))) ; show the next heading
7939 (let ((current-prefix-arg arg))
7940 (call-interactively 'org-todo))
7941 (and (bolp) (forward-char 1))
7942 (setq newhead (org-get-heading))
7943 (when (and (org-bound-and-true-p
7944 org-agenda-headline-snapshot-before-repeat)
7945 (not (equal org-agenda-headline-snapshot-before-repeat
7946 newhead))
7947 todayp)
7948 (setq newhead org-agenda-headline-snapshot-before-repeat
7949 just-one t))
7950 (save-excursion
7951 (org-back-to-heading)
7952 (move-marker org-last-heading-marker (point))))
7953 (beginning-of-line 1)
7954 (save-excursion
7955 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7956 (org-move-to-column col))))
7958 (defun org-agenda-add-note (&optional arg)
7959 "Add a time-stamped note to the entry at point."
7960 (interactive "P")
7961 (org-agenda-check-no-diary)
7962 (let* ((marker (or (org-get-at-bol 'org-marker)
7963 (org-agenda-error)))
7964 (buffer (marker-buffer marker))
7965 (pos (marker-position marker))
7966 (hdmarker (org-get-at-bol 'org-hd-marker))
7967 (inhibit-read-only t))
7968 (with-current-buffer buffer
7969 (widen)
7970 (goto-char pos)
7971 (org-show-context 'agenda)
7972 (save-excursion
7973 (and (outline-next-heading)
7974 (org-flag-heading nil))) ; show the next heading
7975 (org-add-note))))
7977 (defun org-agenda-change-all-lines (newhead hdmarker
7978 &optional fixface just-this)
7979 "Change all lines in the agenda buffer which match HDMARKER.
7980 The new content of the line will be NEWHEAD (as modified by
7981 `org-agenda-format-item'). HDMARKER is checked with
7982 `equal' against all `org-hd-marker' text properties in the file.
7983 If FIXFACE is non-nil, the face of each item is modified according to
7984 the new TODO state.
7985 If JUST-THIS is non-nil, change just the current line, not all.
7986 If FORCE-TAGS is non nil, the car of it returns the new tags."
7987 (let* ((inhibit-read-only t)
7988 (line (org-current-line))
7989 (org-agenda-buffer (current-buffer))
7990 (thetags (with-current-buffer (marker-buffer hdmarker)
7991 (save-excursion (save-restriction (widen)
7992 (goto-char hdmarker)
7993 (org-get-tags-at)))))
7994 props m pl undone-face done-face finish new dotime cat tags)
7995 (save-excursion
7996 (goto-char (point-max))
7997 (beginning-of-line 1)
7998 (while (not finish)
7999 (setq finish (bobp))
8000 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8001 (or (not just-this) (= (org-current-line) line))
8002 (equal m hdmarker))
8003 (setq props (text-properties-at (point))
8004 dotime (org-get-at-bol 'dotime)
8005 cat (org-get-at-bol 'org-category)
8006 tags thetags
8008 (let ((org-prefix-format-compiled
8009 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8010 org-prefix-format-compiled))
8011 (extra (org-get-at-bol 'extra)))
8012 (with-current-buffer (marker-buffer hdmarker)
8013 (save-excursion
8014 (save-restriction
8015 (widen)
8016 (org-agenda-format-item extra newhead cat tags dotime)))))
8017 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8018 undone-face (org-get-at-bol 'undone-face)
8019 done-face (org-get-at-bol 'done-face))
8020 (beginning-of-line 1)
8021 (cond
8022 ((equal new "")
8023 (and (looking-at ".*\n?") (replace-match "")))
8024 ((looking-at ".*")
8025 (replace-match new t t)
8026 (beginning-of-line 1)
8027 (add-text-properties (point-at-bol) (point-at-eol) props)
8028 (when fixface
8029 (add-text-properties
8030 (point-at-bol) (point-at-eol)
8031 (list 'face
8032 (if org-last-todo-state-is-todo
8033 undone-face done-face))))
8034 (org-agenda-highlight-todo 'line)
8035 (beginning-of-line 1))
8036 (t (error "Line update did not work")))
8037 (save-restriction
8038 (narrow-to-region (point-at-bol) (point-at-eol))
8039 (org-agenda-finalize)))
8040 (beginning-of-line 0)))))
8042 (defun org-agenda-align-tags (&optional line)
8043 "Align all tags in agenda items to `org-agenda-tags-column'."
8044 (let ((inhibit-read-only t) l c)
8045 (save-excursion
8046 (goto-char (if line (point-at-bol) (point-min)))
8047 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8048 (if line (point-at-eol) nil) t)
8049 (add-text-properties
8050 (match-beginning 2) (match-end 2)
8051 (list 'face (delq nil (let ((prop (get-text-property
8052 (match-beginning 2) 'face)))
8053 (or (listp prop) (setq prop (list prop)))
8054 (if (memq 'org-tag prop)
8055 prop
8056 (cons 'org-tag prop))))))
8057 (setq l (- (match-end 2) (match-beginning 2))
8058 c (if (< org-agenda-tags-column 0)
8059 (- (abs org-agenda-tags-column) l)
8060 org-agenda-tags-column))
8061 (delete-region (match-beginning 1) (match-end 1))
8062 (goto-char (match-beginning 1))
8063 (insert (org-add-props
8064 (make-string (max 1 (- c (current-column))) ?\ )
8065 (plist-put (copy-sequence (text-properties-at (point)))
8066 'face nil))))
8067 (goto-char (point-min))
8068 (org-font-lock-add-tag-faces (point-max)))))
8070 (defun org-agenda-priority-up ()
8071 "Increase the priority of line at point, also in Org-mode file."
8072 (interactive)
8073 (org-agenda-priority 'up))
8075 (defun org-agenda-priority-down ()
8076 "Decrease the priority of line at point, also in Org-mode file."
8077 (interactive)
8078 (org-agenda-priority 'down))
8080 (defun org-agenda-priority (&optional force-direction show)
8081 "Set the priority of line at point, also in Org-mode file.
8082 This changes the line at point, all other lines in the agenda referring to
8083 the same tree node, and the headline of the tree node in the Org-mode file."
8084 (interactive "P")
8085 (if (equal force-direction '(4)) (setq show t))
8086 (unless org-enable-priority-commands
8087 (error "Priority commands are disabled"))
8088 (org-agenda-check-no-diary)
8089 (let* ((marker (or (org-get-at-bol 'org-marker)
8090 (org-agenda-error)))
8091 (hdmarker (org-get-at-bol 'org-hd-marker))
8092 (buffer (marker-buffer hdmarker))
8093 (pos (marker-position hdmarker))
8094 (inhibit-read-only t)
8095 newhead)
8096 (org-with-remote-undo buffer
8097 (with-current-buffer buffer
8098 (widen)
8099 (goto-char pos)
8100 (org-show-context 'agenda)
8101 (save-excursion
8102 (and (outline-next-heading)
8103 (org-flag-heading nil))) ; show the next heading
8104 (funcall 'org-priority force-direction show)
8105 (end-of-line 1)
8106 (setq newhead (org-get-heading)))
8107 (org-agenda-change-all-lines newhead hdmarker)
8108 (beginning-of-line 1))))
8110 ;; FIXME: should fix the tags property of the agenda line.
8111 (defun org-agenda-set-tags (&optional tag onoff)
8112 "Set tags for the current headline."
8113 (interactive)
8114 (org-agenda-check-no-diary)
8115 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8116 (call-interactively 'org-change-tag-in-region)
8117 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8118 (org-agenda-error)))
8119 (buffer (marker-buffer hdmarker))
8120 (pos (marker-position hdmarker))
8121 (inhibit-read-only t)
8122 newhead)
8123 (org-with-remote-undo buffer
8124 (with-current-buffer buffer
8125 (widen)
8126 (goto-char pos)
8127 (save-excursion
8128 (org-show-context 'agenda))
8129 (save-excursion
8130 (and (outline-next-heading)
8131 (org-flag-heading nil))) ; show the next heading
8132 (goto-char pos)
8133 (if tag
8134 (org-toggle-tag tag onoff)
8135 (call-interactively 'org-set-tags))
8136 (end-of-line 1)
8137 (setq newhead (org-get-heading)))
8138 (org-agenda-change-all-lines newhead hdmarker)
8139 (beginning-of-line 1)))))
8141 (defun org-agenda-set-property ()
8142 "Set a property for the current headline."
8143 (interactive)
8144 (org-agenda-check-no-diary)
8145 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8146 (org-agenda-error)))
8147 (buffer (marker-buffer hdmarker))
8148 (pos (marker-position hdmarker))
8149 (inhibit-read-only t)
8150 newhead)
8151 (org-with-remote-undo buffer
8152 (with-current-buffer buffer
8153 (widen)
8154 (goto-char pos)
8155 (save-excursion
8156 (org-show-context 'agenda))
8157 (save-excursion
8158 (and (outline-next-heading)
8159 (org-flag-heading nil))) ; show the next heading
8160 (goto-char pos)
8161 (call-interactively 'org-set-property)))))
8163 (defun org-agenda-set-effort ()
8164 "Set the effort property for the current headline."
8165 (interactive)
8166 (org-agenda-check-no-diary)
8167 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8168 (org-agenda-error)))
8169 (buffer (marker-buffer hdmarker))
8170 (pos (marker-position hdmarker))
8171 (inhibit-read-only t)
8172 newhead)
8173 (org-with-remote-undo buffer
8174 (with-current-buffer buffer
8175 (widen)
8176 (goto-char pos)
8177 (save-excursion
8178 (org-show-context 'agenda))
8179 (save-excursion
8180 (and (outline-next-heading)
8181 (org-flag-heading nil))) ; show the next heading
8182 (goto-char pos)
8183 (call-interactively 'org-set-effort)
8184 (end-of-line 1)
8185 (setq newhead (org-get-heading)))
8186 (org-agenda-change-all-lines newhead hdmarker))))
8188 (defun org-agenda-toggle-archive-tag ()
8189 "Toggle the archive tag for the current entry."
8190 (interactive)
8191 (org-agenda-check-no-diary)
8192 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8193 (org-agenda-error)))
8194 (buffer (marker-buffer hdmarker))
8195 (pos (marker-position hdmarker))
8196 (inhibit-read-only t)
8197 newhead)
8198 (org-with-remote-undo buffer
8199 (with-current-buffer buffer
8200 (widen)
8201 (goto-char pos)
8202 (org-show-context 'agenda)
8203 (save-excursion
8204 (and (outline-next-heading)
8205 (org-flag-heading nil))) ; show the next heading
8206 (call-interactively 'org-toggle-archive-tag)
8207 (end-of-line 1)
8208 (setq newhead (org-get-heading)))
8209 (org-agenda-change-all-lines newhead hdmarker)
8210 (beginning-of-line 1))))
8212 (defun org-agenda-do-date-later (arg)
8213 (interactive "P")
8214 (cond
8215 ((or (equal arg '(16))
8216 (memq last-command
8217 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8218 (setq this-command 'org-agenda-date-later-minutes)
8219 (org-agenda-date-later-minutes 1))
8220 ((or (equal arg '(4))
8221 (memq last-command
8222 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8223 (setq this-command 'org-agenda-date-later-hours)
8224 (org-agenda-date-later-hours 1))
8226 (org-agenda-date-later (prefix-numeric-value arg)))))
8228 (defun org-agenda-do-date-earlier (arg)
8229 (interactive "P")
8230 (cond
8231 ((or (equal arg '(16))
8232 (memq last-command
8233 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
8234 (setq this-command 'org-agenda-date-earlier-minutes)
8235 (org-agenda-date-earlier-minutes 1))
8236 ((or (equal arg '(4))
8237 (memq last-command
8238 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
8239 (setq this-command 'org-agenda-date-earlier-hours)
8240 (org-agenda-date-earlier-hours 1))
8242 (org-agenda-date-earlier (prefix-numeric-value arg)))))
8244 (defun org-agenda-date-later (arg &optional what)
8245 "Change the date of this item to ARG day(s) later."
8246 (interactive "p")
8247 (org-agenda-check-type t 'agenda 'timeline)
8248 (org-agenda-check-no-diary)
8249 (let* ((marker (or (org-get-at-bol 'org-marker)
8250 (org-agenda-error)))
8251 (buffer (marker-buffer marker))
8252 (pos (marker-position marker))
8253 cdate today)
8254 (org-with-remote-undo buffer
8255 (with-current-buffer buffer
8256 (widen)
8257 (goto-char pos)
8258 (if (not (org-at-timestamp-p))
8259 (error "Cannot find time stamp"))
8260 (when (and org-agenda-move-date-from-past-immediately-to-today
8261 (equal arg 1)
8262 (or (not what) (eq what 'day))
8263 (not (save-match-data (org-at-date-range-p))))
8264 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
8265 cdate (calendar-absolute-from-gregorian
8266 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
8267 today (org-today))
8268 (if (> today cdate)
8269 ;; immediately shift to today
8270 (setq arg (- today cdate))))
8271 (org-timestamp-change arg (or what 'day))
8272 (when (and (org-at-date-range-p)
8273 (re-search-backward org-tr-regexp-both (point-at-bol)))
8274 (let ((end org-last-changed-timestamp))
8275 (org-timestamp-change arg (or what 'day))
8276 (setq org-last-changed-timestamp
8277 (concat org-last-changed-timestamp "--" end)))))
8278 (org-agenda-show-new-time marker org-last-changed-timestamp))
8279 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8281 (defun org-agenda-date-earlier (arg &optional what)
8282 "Change the date of this item to ARG day(s) earlier."
8283 (interactive "p")
8284 (org-agenda-date-later (- arg) what))
8286 (defun org-agenda-date-later-minutes (arg)
8287 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8288 (interactive "p")
8289 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8290 (org-agenda-date-later arg 'minute))
8292 (defun org-agenda-date-earlier-minutes (arg)
8293 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
8294 (interactive "p")
8295 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
8296 (org-agenda-date-earlier arg 'minute))
8298 (defun org-agenda-date-later-hours (arg)
8299 "Change the time of this item, in hour steps."
8300 (interactive "p")
8301 (org-agenda-date-later arg 'hour))
8303 (defun org-agenda-date-earlier-hours (arg)
8304 "Change the time of this item, in hour steps."
8305 (interactive "p")
8306 (org-agenda-date-earlier arg 'hour))
8308 (defun org-agenda-show-new-time (marker stamp &optional prefix)
8309 "Show new date stamp via text properties."
8310 ;; We use text properties to make this undoable
8311 (let ((inhibit-read-only t)
8312 (buffer-invisibility-spec))
8313 (setq stamp (concat " " prefix " => " stamp))
8314 (save-excursion
8315 (goto-char (point-max))
8316 (while (not (bobp))
8317 (when (equal marker (org-get-at-bol 'org-marker))
8318 (org-move-to-column (- (window-width) (length stamp)) t)
8319 (org-agenda-fix-tags-filter-overlays-at (point))
8320 (if (featurep 'xemacs)
8321 ;; Use `duplicable' property to trigger undo recording
8322 (let ((ex (make-extent nil nil))
8323 (gl (make-glyph stamp)))
8324 (set-glyph-face gl 'secondary-selection)
8325 (set-extent-properties
8326 ex (list 'invisible t 'end-glyph gl 'duplicable t))
8327 (insert-extent ex (1- (point)) (point-at-eol)))
8328 (add-text-properties
8329 (1- (point)) (point-at-eol)
8330 (list 'display (org-add-props stamp nil
8331 'face 'secondary-selection))))
8332 (beginning-of-line 1))
8333 (beginning-of-line 0)))))
8335 (defun org-agenda-date-prompt (arg)
8336 "Change the date of this item. Date is prompted for, with default today.
8337 The prefix ARG is passed to the `org-time-stamp' command and can therefore
8338 be used to request time specification in the time stamp."
8339 (interactive "P")
8340 (org-agenda-check-type t 'agenda 'timeline)
8341 (org-agenda-check-no-diary)
8342 (let* ((marker (or (org-get-at-bol 'org-marker)
8343 (org-agenda-error)))
8344 (buffer (marker-buffer marker))
8345 (pos (marker-position marker)))
8346 (org-with-remote-undo buffer
8347 (with-current-buffer buffer
8348 (widen)
8349 (goto-char pos)
8350 (if (not (org-at-timestamp-p t))
8351 (error "Cannot find time stamp"))
8352 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
8353 (org-agenda-show-new-time marker org-last-changed-timestamp))
8354 (message "Time stamp changed to %s" org-last-changed-timestamp)))
8356 (defun org-agenda-schedule (arg &optional time)
8357 "Schedule the item at point.
8358 ARG is passed through to `org-schedule'."
8359 (interactive "P")
8360 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8361 (org-agenda-check-no-diary)
8362 (let* ((marker (or (org-get-at-bol 'org-marker)
8363 (org-agenda-error)))
8364 (type (marker-insertion-type marker))
8365 (buffer (marker-buffer marker))
8366 (pos (marker-position marker))
8367 (org-insert-labeled-timestamps-at-point nil)
8369 (set-marker-insertion-type marker t)
8370 (org-with-remote-undo buffer
8371 (with-current-buffer buffer
8372 (widen)
8373 (goto-char pos)
8374 (setq ts (org-schedule arg time)))
8375 (org-agenda-show-new-time marker ts "S"))
8376 (message "Item scheduled for %s" ts)))
8378 (defun org-agenda-deadline (arg &optional time)
8379 "Schedule the item at point.
8380 ARG is passed through to `org-deadline'."
8381 (interactive "P")
8382 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
8383 (org-agenda-check-no-diary)
8384 (let* ((marker (or (org-get-at-bol 'org-marker)
8385 (org-agenda-error)))
8386 (buffer (marker-buffer marker))
8387 (pos (marker-position marker))
8388 (org-insert-labeled-timestamps-at-point nil)
8390 (org-with-remote-undo buffer
8391 (with-current-buffer buffer
8392 (widen)
8393 (goto-char pos)
8394 (setq ts (org-deadline arg time)))
8395 (org-agenda-show-new-time marker ts "D"))
8396 (message "Deadline for this item set to %s" ts)))
8398 (defun org-agenda-clock-in (&optional arg)
8399 "Start the clock on the currently selected item."
8400 (interactive "P")
8401 (org-agenda-check-no-diary)
8402 (if (equal arg '(4))
8403 (org-clock-in arg)
8404 (let* ((marker (or (org-get-at-bol 'org-marker)
8405 (org-agenda-error)))
8406 (hdmarker (or (org-get-at-bol 'org-hd-marker)
8407 marker))
8408 (pos (marker-position marker))
8409 newhead)
8410 (org-with-remote-undo (marker-buffer marker)
8411 (with-current-buffer (marker-buffer marker)
8412 (widen)
8413 (goto-char pos)
8414 (org-show-context 'agenda)
8415 (org-show-entry)
8416 (org-cycle-hide-drawers 'children)
8417 (org-clock-in arg)
8418 (setq newhead (org-get-heading)))
8419 (org-agenda-change-all-lines newhead hdmarker)))))
8421 (defun org-agenda-clock-out ()
8422 "Stop the currently running clock."
8423 (interactive)
8424 (unless (marker-buffer org-clock-marker)
8425 (error "No running clock"))
8426 (let ((marker (make-marker)) newhead)
8427 (org-with-remote-undo (marker-buffer org-clock-marker)
8428 (with-current-buffer (marker-buffer org-clock-marker)
8429 (save-excursion
8430 (save-restriction
8431 (widen)
8432 (goto-char org-clock-marker)
8433 (org-back-to-heading t)
8434 (move-marker marker (point))
8435 (org-clock-out)
8436 (setq newhead (org-get-heading))))))
8437 (org-agenda-change-all-lines newhead marker)
8438 (move-marker marker nil)))
8440 (defun org-agenda-clock-cancel (&optional arg)
8441 "Cancel the currently running clock."
8442 (interactive "P")
8443 (unless (marker-buffer org-clock-marker)
8444 (error "No running clock"))
8445 (org-with-remote-undo (marker-buffer org-clock-marker)
8446 (org-clock-cancel)))
8448 (defun org-agenda-clock-goto ()
8449 "Jump to the currently clocked in task within the agenda.
8450 If the currently clocked in task is not listed in the agenda
8451 buffer, display it in another window."
8452 (interactive)
8453 (let (pos)
8454 (mapc (lambda (o)
8455 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
8456 (setq pos (overlay-start o))))
8457 (overlays-in (point-min) (point-max)))
8458 (cond (pos (goto-char pos))
8459 ;; If the currently clocked entry is not in the agenda
8460 ;; buffer, we visit it in another window:
8461 (org-clock-current-task
8462 (org-switch-to-buffer-other-window (org-clock-goto)))
8463 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
8465 (defun org-agenda-diary-entry-in-org-file ()
8466 "Make a diary entry in the file `org-agenda-diary-file'."
8467 (let (d1 d2 char (text "") dp1 dp2)
8468 (if (equal (buffer-name) "*Calendar*")
8469 (setq d1 (calendar-cursor-to-date t)
8470 d2 (car calendar-mark-ring))
8471 (setq dp1 (get-text-property (point-at-bol) 'day))
8472 (unless dp1 (error "No date defined in current line"))
8473 (setq d1 (calendar-gregorian-from-absolute dp1)
8474 d2 (and (ignore-errors (mark))
8475 (save-excursion
8476 (goto-char (mark))
8477 (setq dp2 (get-text-property (point-at-bol) 'day)))
8478 (calendar-gregorian-from-absolute dp2))))
8479 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
8480 (setq char (read-char-exclusive))
8481 (cond
8482 ((equal char ?d)
8483 (setq text (read-string "Day entry: "))
8484 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
8485 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8486 ((equal char ?a)
8487 (setq d1 (list (car d1) (nth 1 d1)
8488 (read-number (format "Reference year [%d]: " (nth 2 d1))
8489 (nth 2 d1))))
8490 (setq text (read-string "Anniversary (use %d to show years): "))
8491 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
8492 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8493 ((equal char ?b)
8494 (setq text (read-string "Block entry: "))
8495 (unless (and d1 d2 (not (equal d1 d2)))
8496 (error "No block of days selected"))
8497 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
8498 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
8499 ((equal char ?j)
8500 (org-switch-to-buffer-other-window
8501 (find-file-noselect org-agenda-diary-file))
8502 (require 'org-datetree)
8503 (org-datetree-find-date-create d1)
8504 (org-reveal t))
8505 (t (error "Invalid selection character `%c'" char)))))
8507 (defcustom org-agenda-insert-diary-strategy 'date-tree
8508 "Where in `org-agenda-diary-file' should new entries be added?
8509 Valid values:
8511 date-tree in the date tree, as child of the date
8512 top-level as top-level entries at the end of the file."
8513 :group 'org-agenda
8514 :type '(choice
8515 (const :tag "in a date tree" date-tree)
8516 (const :tag "as top level at end of file" top-level)))
8518 (defcustom org-agenda-insert-diary-extract-time nil
8519 "Non-nil means extract any time specification from the diary entry."
8520 :group 'org-agenda
8521 :version "24.1"
8522 :type 'boolean)
8524 (defcustom org-agenda-bulk-mark-char ">"
8525 "A single-character string to be used as the bulk mark."
8526 :group 'org-agenda
8527 :version "24.1"
8528 :type 'string)
8530 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
8531 "Add a diary entry with TYPE to `org-agenda-diary-file'.
8532 If TEXT is not empty, it will become the headline of the new entry, and
8533 the resulting entry will not be shown. When TEXT is empty, switch to
8534 `org-agenda-diary-file' and let the user finish the entry there."
8535 (let ((cw (current-window-configuration)))
8536 (org-switch-to-buffer-other-window
8537 (find-file-noselect org-agenda-diary-file))
8538 (widen)
8539 (goto-char (point-min))
8540 (cond
8541 ((eq type 'anniversary)
8542 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
8543 (progn
8544 (or (org-at-heading-p t)
8545 (progn
8546 (outline-next-heading)
8547 (insert "* Anniversaries\n\n")
8548 (beginning-of-line -1)))))
8549 (outline-next-heading)
8550 (org-back-over-empty-lines)
8551 (backward-char 1)
8552 (insert "\n")
8553 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
8554 (nth 2 d1) (car d1) (nth 1 d1) text)))
8555 ((eq type 'day)
8556 (let ((org-prefix-has-time t)
8557 (org-agenda-time-leading-zero t)
8558 fmt time time2)
8559 (if org-agenda-insert-diary-extract-time
8560 ;; Use org-agenda-format-item to parse text for a time-range and
8561 ;; remove it. FIXME: This is a hack, we should refactor
8562 ;; that function to make time extraction available separately
8563 (setq fmt (org-agenda-format-item nil text nil nil t)
8564 time (get-text-property 0 'time fmt)
8565 time2 (if (> (length time) 0)
8566 ;; split-string removes trailing ...... if
8567 ;; no end time given. First space
8568 ;; separates time from date.
8569 (concat " " (car (split-string time "\\.")))
8570 nil)
8571 text (get-text-property 0 'txt fmt)))
8572 (if (eq org-agenda-insert-diary-strategy 'top-level)
8573 (org-agenda-insert-diary-as-top-level text)
8574 (require 'org-datetree)
8575 (org-datetree-find-date-create d1)
8576 (org-agenda-insert-diary-make-new-entry text))
8577 (org-insert-time-stamp (org-time-from-absolute
8578 (calendar-absolute-from-gregorian d1))
8579 nil nil nil nil time2))
8580 (end-of-line 0))
8581 ((eq type 'block)
8582 (if (> (calendar-absolute-from-gregorian d1)
8583 (calendar-absolute-from-gregorian d2))
8584 (setq d1 (prog1 d2 (setq d2 d1))))
8585 (if (eq org-agenda-insert-diary-strategy 'top-level)
8586 (org-agenda-insert-diary-as-top-level text)
8587 (require 'org-datetree)
8588 (org-datetree-find-date-create d1)
8589 (org-agenda-insert-diary-make-new-entry text))
8590 (org-insert-time-stamp (org-time-from-absolute
8591 (calendar-absolute-from-gregorian d1)))
8592 (insert "--")
8593 (org-insert-time-stamp (org-time-from-absolute
8594 (calendar-absolute-from-gregorian d2)))
8595 (end-of-line 0)))
8596 (if (string-match "\\S-" text)
8597 (progn
8598 (set-window-configuration cw)
8599 (message "%s entry added to %s"
8600 (capitalize (symbol-name type))
8601 (abbreviate-file-name org-agenda-diary-file)))
8602 (org-reveal t)
8603 (message "Please finish entry here"))))
8605 (defun org-agenda-insert-diary-as-top-level (text)
8606 "Make new entry as a top-level entry at the end of the file.
8607 Add TEXT as headline, and position the cursor in the second line so that
8608 a timestamp can be added there."
8609 (widen)
8610 (goto-char (point-max))
8611 (or (bolp) (insert "\n"))
8612 (insert "* " text "\n")
8613 (if org-adapt-indentation (org-indent-to-column 2)))
8615 (defun org-agenda-insert-diary-make-new-entry (text)
8616 "Make new entry as last child of current entry.
8617 Add TEXT as headline, and position the cursor in the second line so that
8618 a timestamp can be added there."
8619 (let ((org-show-following-heading t)
8620 (org-show-siblings t)
8621 (org-show-hierarchy-above t)
8622 (org-show-entry-below t)
8623 col)
8624 (outline-next-heading)
8625 (org-back-over-empty-lines)
8626 (or (looking-at "[ \t]*$")
8627 (progn (insert "\n") (backward-char 1)))
8628 (org-insert-heading nil t)
8629 (org-do-demote)
8630 (setq col (current-column))
8631 (insert text "\n")
8632 (if org-adapt-indentation (org-indent-to-column col))
8633 (let ((org-show-following-heading t)
8634 (org-show-siblings t)
8635 (org-show-hierarchy-above t)
8636 (org-show-entry-below t))
8637 (org-show-context))))
8639 (defun org-agenda-diary-entry ()
8640 "Make a diary entry, like the `i' command from the calendar.
8641 All the standard commands work: block, weekly etc.
8642 When `org-agenda-diary-file' points to a file,
8643 `org-agenda-diary-entry-in-org-file' is called instead to create
8644 entries in that Org-mode file."
8645 (interactive)
8646 (org-agenda-check-type t 'agenda 'timeline)
8647 (if (not (eq org-agenda-diary-file 'diary-file))
8648 (org-agenda-diary-entry-in-org-file)
8649 (require 'diary-lib)
8650 (let* ((char (progn
8651 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
8652 (read-char-exclusive)))
8653 (cmd (cdr (assoc char
8654 '((?d . insert-diary-entry)
8655 (?w . insert-weekly-diary-entry)
8656 (?m . insert-monthly-diary-entry)
8657 (?y . insert-yearly-diary-entry)
8658 (?a . insert-anniversary-diary-entry)
8659 (?b . insert-block-diary-entry)
8660 (?c . insert-cyclic-diary-entry)))))
8661 (oldf (symbol-function 'calendar-cursor-to-date))
8662 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
8663 (point (point))
8664 (mark (or (mark t) (point))))
8665 (unless cmd
8666 (error "No command associated with <%c>" char))
8667 (unless (and (get-text-property point 'day)
8668 (or (not (equal ?b char))
8669 (get-text-property mark 'day)))
8670 (error "Don't know which date to use for diary entry"))
8671 ;; We implement this by hacking the `calendar-cursor-to-date' function
8672 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
8673 (let ((calendar-mark-ring
8674 (list (calendar-gregorian-from-absolute
8675 (or (get-text-property mark 'day)
8676 (get-text-property point 'day))))))
8677 (unwind-protect
8678 (progn
8679 (fset 'calendar-cursor-to-date
8680 (lambda (&optional error dummy)
8681 (calendar-gregorian-from-absolute
8682 (get-text-property point 'day))))
8683 (call-interactively cmd))
8684 (fset 'calendar-cursor-to-date oldf))))))
8686 (defun org-agenda-execute-calendar-command (cmd)
8687 "Execute a calendar command from the agenda with date from cursor."
8688 (org-agenda-check-type t 'agenda 'timeline)
8689 (require 'diary-lib)
8690 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
8691 (error "Don't know which date to use for the calendar command"))
8692 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
8693 (point (point))
8694 (date (calendar-gregorian-from-absolute
8695 (get-text-property point 'day)))
8696 ;; the following 2 vars are needed in the calendar
8697 (displayed-month (car date))
8698 (displayed-year (nth 2 date)))
8699 (unwind-protect
8700 (progn
8701 (fset 'calendar-cursor-to-date
8702 (lambda (&optional error dummy)
8703 (calendar-gregorian-from-absolute
8704 (get-text-property point 'day))))
8705 (call-interactively cmd))
8706 (fset 'calendar-cursor-to-date oldf))))
8708 (defun org-agenda-phases-of-moon ()
8709 "Display the phases of the moon for the 3 months around the cursor date."
8710 (interactive)
8711 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
8713 (defun org-agenda-holidays ()
8714 "Display the holidays for the 3 months around the cursor date."
8715 (interactive)
8716 (org-agenda-execute-calendar-command 'list-calendar-holidays))
8718 (defvar calendar-longitude) ; defined in calendar.el
8719 (defvar calendar-latitude) ; defined in calendar.el
8720 (defvar calendar-location-name) ; defined in calendar.el
8722 (defun org-agenda-sunrise-sunset (arg)
8723 "Display sunrise and sunset for the cursor date.
8724 Latitude and longitude can be specified with the variables
8725 `calendar-latitude' and `calendar-longitude'. When called with prefix
8726 argument, latitude and longitude will be prompted for."
8727 (interactive "P")
8728 (require 'solar)
8729 (let ((calendar-longitude (if arg nil calendar-longitude))
8730 (calendar-latitude (if arg nil calendar-latitude))
8731 (calendar-location-name
8732 (if arg "the given coordinates" calendar-location-name)))
8733 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8735 (defun org-agenda-goto-calendar ()
8736 "Open the Emacs calendar with the date at the cursor."
8737 (interactive)
8738 (org-agenda-check-type t 'agenda 'timeline)
8739 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
8740 (error "Don't know which date to open in calendar")))
8741 (date (calendar-gregorian-from-absolute day))
8742 (calendar-move-hook nil)
8743 (calendar-view-holidays-initially-flag nil)
8744 (calendar-view-diary-initially-flag nil))
8745 (calendar)
8746 (calendar-goto-date date)))
8748 ;;;###autoload
8749 (defun org-calendar-goto-agenda ()
8750 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8751 This is a command that has to be installed in `calendar-mode-map'."
8752 (interactive)
8753 (org-agenda-list nil (calendar-absolute-from-gregorian
8754 (calendar-cursor-to-date))
8755 nil))
8757 (defun org-agenda-convert-date ()
8758 (interactive)
8759 (org-agenda-check-type t 'agenda 'timeline)
8760 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
8761 date s)
8762 (unless day
8763 (error "Don't know which date to convert"))
8764 (setq date (calendar-gregorian-from-absolute day))
8765 (setq s (concat
8766 "Gregorian: " (calendar-date-string date) "\n"
8767 "ISO: " (calendar-iso-date-string date) "\n"
8768 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8769 "Julian: " (calendar-julian-date-string date) "\n"
8770 "Astron. JD: " (calendar-astro-date-string date)
8771 " (Julian date number at noon UTC)\n"
8772 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8773 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8774 "French: " (calendar-french-date-string date) "\n"
8775 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8776 "Mayan: " (calendar-mayan-date-string date) "\n"
8777 "Coptic: " (calendar-coptic-date-string date) "\n"
8778 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8779 "Persian: " (calendar-persian-date-string date) "\n"
8780 "Chinese: " (calendar-chinese-date-string date) "\n"))
8781 (with-output-to-temp-buffer "*Dates*"
8782 (princ s))
8783 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8785 ;;; Bulk commands
8787 (defun org-agenda-bulk-marked-p ()
8788 (eq (get-char-property (point-at-bol) 'type)
8789 'org-marked-entry-overlay))
8791 (defun org-agenda-bulk-mark (&optional arg)
8792 "Mark the entry at point for future bulk action."
8793 (interactive "p")
8794 (dotimes (i (or arg 1))
8795 (unless (org-get-at-bol 'org-agenda-diary-link)
8796 (let* ((m (org-get-at-bol 'org-hd-marker))
8798 (unless (org-agenda-bulk-marked-p)
8799 (unless m (error "Nothing to mark at point"))
8800 (push m org-agenda-bulk-marked-entries)
8801 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8802 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
8803 (org-get-todo-face "TODO")
8804 'evaporate)
8805 (overlay-put ov 'type 'org-marked-entry-overlay))
8806 (beginning-of-line 2)
8807 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8808 (beginning-of-line 2))
8809 (message "%d entries marked for bulk action"
8810 (length org-agenda-bulk-marked-entries))))))
8812 (defun org-agenda-bulk-mark-all ()
8813 "Mark all entries for future agenda bulk action."
8814 (interactive)
8815 (org-agenda-bulk-mark-regexp "."))
8817 (defun org-agenda-bulk-mark-regexp (regexp)
8818 "Mark entries matching REGEXP for future agenda bulk action."
8819 (interactive "sMark entries matching regexp: ")
8820 (let ((entries-marked 0))
8821 (save-excursion
8822 (goto-char (point-min))
8823 (goto-char (next-single-property-change (point) 'txt))
8824 (while (re-search-forward regexp nil t)
8825 (when (string-match regexp (get-text-property (point) 'txt))
8826 (setq entries-marked (1+ entries-marked))
8827 (call-interactively 'org-agenda-bulk-mark))))
8828 (if (not entries-marked)
8829 (message "No entry matching this regexp."))))
8831 (defun org-agenda-bulk-unmark (&optional arg)
8832 "Unmark the entry at point for future bulk action."
8833 (interactive "P")
8834 (if arg
8835 (org-agenda-bulk-unmark-all)
8836 (cond ((org-agenda-bulk-marked-p)
8837 (org-agenda-bulk-remove-overlays
8838 (point-at-bol) (+ 2 (point-at-bol)))
8839 (setq org-agenda-bulk-marked-entries
8840 (delete (org-get-at-bol 'org-hd-marker)
8841 org-agenda-bulk-marked-entries))
8842 (beginning-of-line 2)
8843 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8844 (beginning-of-line 2))
8845 (message "%d entries left marked for bulk action"
8846 (length org-agenda-bulk-marked-entries)))
8847 (t (message "No entry to unmark here")))))
8849 (defun org-agenda-bulk-toggle ()
8850 "Toggle marking the entry at point for bulk action."
8851 (interactive)
8852 (if (org-agenda-bulk-marked-p)
8853 (org-agenda-bulk-unmark)
8854 (org-agenda-bulk-mark)))
8856 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8857 "Remove the mark overlays between BEG and END in the agenda buffer.
8858 BEG and END default to the buffer limits.
8860 This only removes the overlays, it does not remove the markers
8861 from the list in `org-agenda-bulk-marked-entries'."
8862 (interactive)
8863 (mapc (lambda (ov)
8864 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8865 (delete-overlay ov)))
8866 (overlays-in (or beg (point-min)) (or end (point-max)))))
8868 (defun org-agenda-bulk-unmark-all ()
8869 "Remove all marks in the agenda buffer.
8870 This will remove the markers and the overlays."
8871 (interactive)
8872 (if (null org-agenda-bulk-marked-entries)
8873 (message "No entry to unmark")
8874 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8875 (setq org-agenda-bulk-marked-entries nil)
8876 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
8878 (defcustom org-agenda-persistent-marks nil
8879 "Non-nil means marked items will stay marked after a bulk action.
8880 You can toggle this interactively by typing `p' when prompted for a
8881 bulk action."
8882 :group 'org-agenda
8883 :version "24.1"
8884 :type 'boolean)
8886 (defun org-agenda-bulk-action (&optional arg)
8887 "Execute an remote-editing action on all marked entries.
8888 The prefix arg is passed through to the command if possible."
8889 (interactive "P")
8890 ;; Make sure we have markers, and only valid ones
8891 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8892 (mapc
8893 (lambda (m)
8894 (unless (and (markerp m)
8895 (marker-buffer m)
8896 (buffer-live-p (marker-buffer m))
8897 (marker-position m))
8898 (error "Marker %s for bulk command is invalid" m)))
8899 org-agenda-bulk-marked-entries)
8901 ;; Prompt for the bulk command
8902 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
8903 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
8904 "[S]catter [f]unction "
8905 (when org-agenda-bulk-custom-functions
8906 (concat " Custom: ["
8907 (mapconcat (lambda(f) (char-to-string (car f)))
8908 org-agenda-bulk-custom-functions "")
8909 "]"))))
8910 (catch 'exit
8911 (let* ((action (read-char-exclusive))
8912 (org-log-refile (if org-log-refile 'time nil))
8913 (entries (reverse org-agenda-bulk-marked-entries))
8914 (org-overriding-default-time
8915 (if (get-text-property (point) 'org-agenda-date-header)
8916 (org-get-cursor-date)))
8917 redo-at-end
8918 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8919 (cond
8920 ((equal action ?p)
8921 (let ((org-agenda-persistent-marks
8922 (not org-agenda-persistent-marks)))
8923 (org-agenda-bulk-action)
8924 (throw 'exit nil)))
8926 ((equal action ?$)
8927 (setq cmd '(org-agenda-archive)))
8929 ((equal action ?A)
8930 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8932 ((member action '(?r ?w))
8933 (setq rfloc (org-refile-get-location
8934 "Refile to"
8935 (marker-buffer (car entries))
8936 org-refile-allow-creating-parent-nodes))
8937 (if (nth 3 rfloc)
8938 (setcar (nthcdr 3 rfloc)
8939 (move-marker (make-marker) (nth 3 rfloc)
8940 (or (get-file-buffer (nth 1 rfloc))
8941 (find-buffer-visiting (nth 1 rfloc))
8942 (error "This should not happen")))))
8944 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8945 redo-at-end t))
8947 ((equal action ?t)
8948 (setq state (org-icompleting-read
8949 "Todo state: "
8950 (with-current-buffer (marker-buffer (car entries))
8951 (mapcar 'list org-todo-keywords-1))))
8952 (setq cmd `(let ((org-inhibit-blocking t)
8953 (org-inhibit-logging 'note))
8954 (org-agenda-todo ,state))))
8956 ((memq action '(?- ?+))
8957 (setq tag (org-icompleting-read
8958 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8959 (with-current-buffer (marker-buffer (car entries))
8960 (delq nil
8961 (mapcar (lambda (x)
8962 (if (stringp (car x)) x)) org-tag-alist)))))
8963 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8965 ((memq action '(?s ?d))
8966 (let* ((time
8967 (unless arg
8968 (org-read-date
8969 nil nil nil
8970 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
8971 org-overriding-default-time)))
8972 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8973 (setq cmd `(eval '(,c1 arg ,time)))))
8975 ((equal action ?S)
8976 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8977 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8978 (let ((days (read-number
8979 (format "Scatter tasks across how many %sdays: "
8980 (if arg "week" "")) 7)))
8981 (setq cmd
8982 `(let ((distance (1+ (random ,days))))
8983 (if arg
8984 (let ((dist distance)
8985 (day-of-week
8986 (calendar-day-of-week
8987 (calendar-gregorian-from-absolute (org-today)))))
8988 (dotimes (i (1+ dist))
8989 (while (member day-of-week org-agenda-weekend-days)
8990 (incf distance)
8991 (incf day-of-week)
8992 (if (= day-of-week 7)
8993 (setq day-of-week 0)))
8994 (incf day-of-week)
8995 (if (= day-of-week 7)
8996 (setq day-of-week 0)))))
8997 ;; silently fail when try to replan a sexp entry
8998 (condition-case nil
8999 (let* ((date (calendar-gregorian-from-absolute
9000 (+ (org-today) distance)))
9001 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9002 (nth 2 date))))
9003 (org-agenda-schedule nil time))
9004 (error nil)))))))
9006 ((assoc action org-agenda-bulk-custom-functions)
9007 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9008 redo-at-end t))
9010 ((equal action ?f)
9011 (setq cmd (list (intern
9012 (org-icompleting-read "Function: "
9013 obarray 'fboundp t nil nil)))))
9015 (t (error "Invalid bulk action")))
9017 ;; Sort the markers, to make sure that parents are handled before children
9018 (setq entries (sort entries
9019 (lambda (a b)
9020 (cond
9021 ((equal (marker-buffer a) (marker-buffer b))
9022 (< (marker-position a) (marker-position b)))
9024 (string< (buffer-name (marker-buffer a))
9025 (buffer-name (marker-buffer b))))))))
9027 ;; Now loop over all markers and apply cmd
9028 (while (setq e (pop entries))
9029 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9030 (if (not pos)
9031 (progn (message "Skipping removed entry at %s" e)
9032 (setq cntskip (1+ cntskip)))
9033 (goto-char pos)
9034 (let (org-loop-over-headlines-in-active-region)
9035 (eval cmd))
9036 (setq cnt (1+ cnt))))
9037 (when redo-at-end (org-agenda-redo))
9038 (unless org-agenda-persistent-marks
9039 (org-agenda-bulk-unmark-all))
9040 (message "Acted on %d entries%s%s"
9042 (if (= cntskip 0)
9044 (format ", skipped %d (disappeared before their turn)"
9045 cntskip))
9046 (if (not org-agenda-persistent-marks)
9047 "" " (kept marked)"))))))
9049 (defun org-agenda-capture ()
9050 "Call `org-capture' with the date at point."
9051 (interactive)
9052 (if (not (eq major-mode 'org-agenda-mode))
9053 (error "You cannot do this outside of agenda buffers")
9054 (let ((org-overriding-default-time
9055 (org-get-cursor-date)))
9056 (call-interactively 'org-capture))))
9058 ;;; Flagging notes
9060 (defun org-agenda-show-the-flagging-note ()
9061 "Display the flagging note in the other window.
9062 When called a second time in direct sequence, offer to remove the FLAGGING
9063 tag and (if present) the flagging note."
9064 (interactive)
9065 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9066 (win (selected-window))
9067 note heading newhead)
9068 (unless hdmarker
9069 (error "No linked entry at point"))
9070 (if (and (eq this-command last-command)
9071 (y-or-n-p "Unflag and remove any flagging note? "))
9072 (progn
9073 (org-agenda-remove-flag hdmarker)
9074 (let ((win (get-buffer-window "*Flagging Note*")))
9075 (and win (delete-window win)))
9076 (message "Entry unflagged"))
9077 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9078 (unless note
9079 (error "No flagging note"))
9080 (org-kill-new note)
9081 (org-switch-to-buffer-other-window "*Flagging Note*")
9082 (erase-buffer)
9083 (insert note)
9084 (goto-char (point-min))
9085 (while (re-search-forward "\\\\n" nil t)
9086 (replace-match "\n" t t))
9087 (goto-char (point-min))
9088 (select-window win)
9089 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9091 (defun org-agenda-remove-flag (marker)
9092 "Remove the FLAGGED tag and any flagging note in the entry."
9093 (let (newhead)
9094 (org-with-point-at marker
9095 (org-toggle-tag "FLAGGED" 'off)
9096 (org-entry-delete nil "THEFLAGGINGNOTE")
9097 (setq newhead (org-get-heading)))
9098 (org-agenda-change-all-lines newhead marker)
9099 (message "Entry unflagged")))
9101 (defun org-agenda-get-any-marker (&optional pos)
9102 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9103 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9105 ;;; Appointment reminders
9107 (defvar appt-time-msg-list) ; defined in appt.el
9109 ;;;###autoload
9110 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9111 "Activate appointments found in `org-agenda-files'.
9112 With a \\[universal-argument] prefix, refresh the list of
9113 appointments.
9115 If FILTER is t, interactively prompt the user for a regular
9116 expression, and filter out entries that don't match it.
9118 If FILTER is a string, use this string as a regular expression
9119 for filtering entries out.
9121 If FILTER is a function, filter out entries against which
9122 calling the function returns nil. This function takes one
9123 argument: an entry from `org-agenda-get-day-entries'.
9125 FILTER can also be an alist with the car of each cell being
9126 either 'headline or 'category. For example:
9128 '((headline \"IMPORTANT\")
9129 (category \"Work\"))
9131 will only add headlines containing IMPORTANT or headlines
9132 belonging to the \"Work\" category.
9134 ARGS are symbols indicating what kind of entries to consider.
9135 By default `org-agenda-to-appt' will use :deadline, :scheduled
9136 and :timestamp entries. See the docstring of `org-diary' for
9137 details and examples.
9139 If an entry as a APPT_WARNTIME property, its value will be used
9140 to override `appt-message-warning-time'."
9141 (interactive "P")
9142 (if refresh (setq appt-time-msg-list nil))
9143 (if (eq filter t)
9144 (setq filter (read-from-minibuffer "Regexp filter: ")))
9145 (let* ((cnt 0) ; count added events
9146 (scope (or args '(:deadline :scheduled :timestamp)))
9147 (org-agenda-new-buffers nil)
9148 (org-deadline-warning-days 0)
9149 ;; Do not use `org-today' here because appt only takes
9150 ;; time and without date as argument, so it may pass wrong
9151 ;; information otherwise
9152 (today (org-date-to-gregorian
9153 (time-to-days (current-time))))
9154 (org-agenda-restrict nil)
9155 (files (org-agenda-files 'unrestricted)) entries file
9156 (org-agenda-buffer nil))
9157 ;; Get all entries which may contain an appt
9158 (org-agenda-prepare-buffers files)
9159 (while (setq file (pop files))
9160 (setq entries
9161 (delq nil
9162 (append entries
9163 (apply 'org-agenda-get-day-entries
9164 file today scope)))))
9165 ;; Map thru entries and find if we should filter them out
9166 (mapc
9167 (lambda(x)
9168 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
9169 (cat (get-text-property 1 'org-category x))
9170 (tod (get-text-property 1 'time-of-day x))
9171 (ok (or (null filter)
9172 (and (stringp filter) (string-match filter evt))
9173 (and (functionp filter) (funcall filter x))
9174 (and (listp filter)
9175 (let ((cat-filter (cadr (assoc 'category filter)))
9176 (evt-filter (cadr (assoc 'headline filter))))
9177 (or (and (stringp cat-filter)
9178 (string-match cat-filter cat))
9179 (and (stringp evt-filter)
9180 (string-match evt-filter evt)))))))
9181 (wrn (get-text-property 1 'warntime x)))
9182 ;; FIXME: Shall we remove text-properties for the appt text?
9183 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9184 (when (and ok tod)
9185 (setq tod (concat "00" (number-to-string tod))
9186 tod (when (string-match
9187 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9188 (concat (match-string 1 tod) ":"
9189 (match-string 2 tod))))
9190 (if (version< emacs-version "23.3")
9191 (appt-add tod evt)
9192 (appt-add tod evt wrn))
9193 (setq cnt (1+ cnt))))) entries)
9194 (org-release-buffers org-agenda-new-buffers)
9195 (if (eq cnt 0)
9196 (message "No event to add")
9197 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9199 (defun org-agenda-todayp (date)
9200 "Does DATE mean today, when considering `org-extend-today-until'?"
9201 (let ((today (org-today))
9202 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
9203 date)))
9204 (eq date today)))
9206 (defun org-agenda-todo-yesterday (&optional arg)
9207 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
9208 (interactive "P")
9209 (let* ((hour (third (decode-time
9210 (org-current-time))))
9211 (org-extend-today-until (1+ hour)))
9212 (org-agenda-todo arg)))
9214 (provide 'org-agenda)
9216 ;;; org-agenda.el ends here