Merge branch 'maint'
[org-mode.git] / lisp / org-agenda.el
blob754247afdbd4e4cf1c844c6a83469a2b3e1efbb8
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2013 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 timestamp-up) (const timestamp-down)
246 (const scheduled-up) (const scheduled-down)
247 (const deadline-up) (const deadline-down)
248 (const ts-up) (const ts-down)
249 (const tsia-up) (const tsia-down)
250 (const category-keep) (const category-up) (const category-down)
251 (const tag-down) (const tag-up)
252 (const priority-up) (const priority-down)
253 (const todo-state-up) (const todo-state-down)
254 (const effort-up) (const effort-down)
255 (const habit-up) (const habit-down)
256 (const alpha-up) (const alpha-down)
257 (const user-defined-up) (const user-defined-down))
258 "Sorting choices.")
260 ;; Keep custom values for `org-agenda-filter-preset' compatible with
261 ;; the new variable `org-agenda-tag-filter-preset'.
262 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
263 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
265 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
266 "List of types searched for when creating the daily/weekly agenda.
267 This variable is a list of symbols that controls the types of
268 items that appear in the daily/weekly agenda. Allowed symbols in this
269 list are are
271 :timestamp List items containing a date stamp or date range matching
272 the selected date. This includes sexp entries in angular
273 brackets.
275 :sexp List entries resulting from plain diary-like sexps.
277 :deadline List deadline due on that date. When the date is today,
278 also list any deadlines past due, or due within
279 `org-deadline-warning-days'. `:deadline' must appear before
280 `:scheduled' if the setting of
281 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
282 any effect.
284 :deadline* Same as above, but only include the deadline if it has an
285 hour specification as [h]h:mm.
287 :scheduled List all items which are scheduled for the given date.
288 The diary for *today* also contains items which were
289 scheduled earlier and are not yet marked DONE.
291 :scheduled* Same as above, but only include the scheduled item if it
292 has an hour specification as [h]h:mm.
294 By default, all four non-starred types are turned on.
296 When :scheduled* or :deadline* are included, :schedule or :deadline
297 will be ignored.
299 Never set this variable globally using `setq', because then it
300 will apply to all future agenda commands. Instead, bind it with
301 `let' to scope it dynamically into the agenda-constructing
302 command. A good way to set it is through options in
303 `org-agenda-custom-commands'. For a more flexible (though
304 somewhat less efficient) way of determining what is included in
305 the daily/weekly agenda, see `org-agenda-skip-function'.")
307 (defconst org-agenda-custom-commands-local-options
308 `(repeat :tag "Local settings for this command. Remember to quote values"
309 (choice :tag "Setting"
310 (list :tag "Heading for this block"
311 (const org-agenda-overriding-header)
312 (string :tag "Headline"))
313 (list :tag "Files to be searched"
314 (const org-agenda-files)
315 (list
316 (const :format "" quote)
317 (repeat (file))))
318 (list :tag "Sorting strategy"
319 (const org-agenda-sorting-strategy)
320 (list
321 (const :format "" quote)
322 (repeat
323 ,org-sorting-choice)))
324 (list :tag "Prefix format"
325 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
326 (string))
327 (list :tag "Number of days in agenda"
328 (const org-agenda-span)
329 (choice (const :tag "Day" 'day)
330 (const :tag "Week" 'week)
331 (const :tag "Month" 'month)
332 (const :tag "Year" 'year)
333 (integer :tag "Custom")))
334 (list :tag "Fixed starting date"
335 (const org-agenda-start-day)
336 (string :value "2007-11-01"))
337 (list :tag "Start on day of week"
338 (const org-agenda-start-on-weekday)
339 (choice :value 1
340 (const :tag "Today" nil)
341 (integer :tag "Weekday No.")))
342 (list :tag "Include data from diary"
343 (const org-agenda-include-diary)
344 (boolean))
345 (list :tag "Deadline Warning days"
346 (const org-deadline-warning-days)
347 (integer :value 1))
348 (list :tag "Category filter preset"
349 (const org-agenda-category-filter-preset)
350 (list
351 (const :format "" quote)
352 (repeat
353 (string :tag "+category or -category"))))
354 (list :tag "Tags filter preset"
355 (const org-agenda-tag-filter-preset)
356 (list
357 (const :format "" quote)
358 (repeat
359 (string :tag "+tag or -tag"))))
360 (list :tag "Regexp filter preset"
361 (const org-agenda-regexp-filter-preset)
362 (list
363 (const :format "" quote)
364 (repeat
365 (string :tag "+regexp or -regexp"))))
366 (list :tag "Set daily/weekly entry types"
367 (const org-agenda-entry-types)
368 (list
369 (const :format "" quote)
370 (set :greedy t :value ,org-agenda-entry-types
371 (const :deadline)
372 (const :scheduled)
373 (const :deadline*)
374 (const :scheduled*)
375 (const :timestamp)
376 (const :sexp))))
377 (list :tag "Standard skipping condition"
378 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
379 (const org-agenda-skip-function)
380 (list
381 (const :format "" quote)
382 (list
383 (choice
384 :tag "Skipping range"
385 (const :tag "Skip entry" org-agenda-skip-entry-if)
386 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
387 (repeat :inline t :tag "Conditions for skipping"
388 (choice
389 :tag "Condition type"
390 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
391 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
392 (list :tag "TODO state is" :inline t
393 (const 'todo)
394 (choice
395 (const :tag "any not-done state" 'todo)
396 (const :tag "any done state" 'done)
397 (const :tag "any state" 'any)
398 (list :tag "Keyword list"
399 (const :format "" quote)
400 (repeat (string :tag "Keyword")))))
401 (list :tag "TODO state is not" :inline t
402 (const 'nottodo)
403 (choice
404 (const :tag "any not-done state" 'todo)
405 (const :tag "any done state" 'done)
406 (const :tag "any state" 'any)
407 (list :tag "Keyword list"
408 (const :format "" quote)
409 (repeat (string :tag "Keyword")))))
410 (const :tag "scheduled" 'scheduled)
411 (const :tag "not scheduled" 'notscheduled)
412 (const :tag "deadline" 'deadline)
413 (const :tag "no deadline" 'notdeadline)
414 (const :tag "timestamp" 'timestamp)
415 (const :tag "no timestamp" 'nottimestamp))))))
416 (list :tag "Non-standard skipping condition"
417 :value (org-agenda-skip-function)
418 (const org-agenda-skip-function)
419 (sexp :tag "Function or form (quoted!)"))
420 (list :tag "Any variable"
421 (variable :tag "Variable")
422 (sexp :tag "Value (sexp)"))))
423 "Selection of examples for agenda command settings.
424 This will be spliced into the custom type of
425 `org-agenda-custom-commands'.")
428 (defcustom org-agenda-custom-commands
429 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
430 "Custom commands for the agenda.
431 These commands will be offered on the splash screen displayed by the
432 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
434 (key desc type match settings files)
436 key The key (one or more characters as a string) to be associated
437 with the command.
438 desc A description of the command, when omitted or nil, a default
439 description is built using MATCH.
440 type The command type, any of the following symbols:
441 agenda The daily/weekly agenda.
442 todo Entries with a specific TODO keyword, in all agenda files.
443 search Entries containing search words entry or headline.
444 tags Tags/Property/TODO match in all agenda files.
445 tags-todo Tags/P/T match in all agenda files, TODO entries only.
446 todo-tree Sparse tree of specific TODO keyword in *current* file.
447 tags-tree Sparse tree with all tags matches in *current* file.
448 occur-tree Occur sparse tree for *current* file.
449 ... A user-defined function.
450 match What to search for:
451 - a single keyword for TODO keyword searches
452 - a tags match expression for tags searches
453 - a word search expression for text searches.
454 - a regular expression for occur searches
455 For all other commands, this should be the empty string.
456 settings A list of option settings, similar to that in a let form, so like
457 this: ((opt1 val1) (opt2 val2) ...). The values will be
458 evaluated at the moment of execution, so quote them when needed.
459 files A list of files file to write the produced agenda buffer to
460 with the command `org-store-agenda-views'.
461 If a file name ends in \".html\", an HTML version of the buffer
462 is written out. If it ends in \".ps\", a postscript version is
463 produced. Otherwise, only the plain text is written to the file.
465 You can also define a set of commands, to create a composite agenda buffer.
466 In this case, an entry looks like this:
468 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
470 where
472 desc A description string to be displayed in the dispatcher menu.
473 cmd An agenda command, similar to the above. However, tree commands
474 are not allowed, but instead you can get agenda and global todo list.
475 So valid commands for a set are:
476 (agenda \"\" settings)
477 (alltodo \"\" settings)
478 (stuck \"\" settings)
479 (todo \"match\" settings files)
480 (search \"match\" settings files)
481 (tags \"match\" settings files)
482 (tags-todo \"match\" settings files)
484 Each command can carry a list of options, and another set of options can be
485 given for the whole set of commands. Individual command options take
486 precedence over the general options.
488 When using several characters as key to a command, the first characters
489 are prefix commands. For the dispatcher to display useful information, you
490 should provide a description for the prefix, like
492 (setq org-agenda-custom-commands
493 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
494 (\"hl\" tags \"+HOME+Lisa\")
495 (\"hp\" tags \"+HOME+Peter\")
496 (\"hk\" tags \"+HOME+Kim\")))"
497 :group 'org-agenda-custom-commands
498 :type `(repeat
499 (choice :value ("x" "Describe command here" tags "" nil)
500 (list :tag "Single command"
501 (string :tag "Access Key(s) ")
502 (option (string :tag "Description"))
503 (choice
504 (const :tag "Agenda" agenda)
505 (const :tag "TODO list" alltodo)
506 (const :tag "Search words" search)
507 (const :tag "Stuck projects" stuck)
508 (const :tag "Tags/Property match (all agenda files)" tags)
509 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
510 (const :tag "TODO keyword search (all agenda files)" todo)
511 (const :tag "Tags sparse tree (current buffer)" tags-tree)
512 (const :tag "TODO keyword tree (current buffer)" todo-tree)
513 (const :tag "Occur tree (current buffer)" occur-tree)
514 (sexp :tag "Other, user-defined function"))
515 (string :tag "Match (only for some commands)")
516 ,org-agenda-custom-commands-local-options
517 (option (repeat :tag "Export" (file :tag "Export to"))))
518 (list :tag "Command series, all agenda files"
519 (string :tag "Access Key(s)")
520 (string :tag "Description ")
521 (repeat :tag "Component"
522 (choice
523 (list :tag "Agenda"
524 (const :format "" agenda)
525 (const :tag "" :format "" "")
526 ,org-agenda-custom-commands-local-options)
527 (list :tag "TODO list (all keywords)"
528 (const :format "" alltodo)
529 (const :tag "" :format "" "")
530 ,org-agenda-custom-commands-local-options)
531 (list :tag "Search words"
532 (const :format "" search)
533 (string :tag "Match")
534 ,org-agenda-custom-commands-local-options)
535 (list :tag "Stuck projects"
536 (const :format "" stuck)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "Tags search"
540 (const :format "" tags)
541 (string :tag "Match")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Tags search, TODO entries only"
544 (const :format "" tags-todo)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "TODO keyword search"
548 (const :format "" todo)
549 (string :tag "Match")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Other, user-defined function"
552 (symbol :tag "function")
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)))
556 (repeat :tag "Settings for entire command set"
557 (list (variable :tag "Any variable")
558 (sexp :tag "Value")))
559 (option (repeat :tag "Export" (file :tag "Export to"))))
560 (cons :tag "Prefix key documentation"
561 (string :tag "Access Key(s)")
562 (string :tag "Description ")))))
564 (defcustom org-agenda-query-register ?o
565 "The register holding the current query string.
566 The purpose of this is that if you construct a query string interactively,
567 you can then use it to define a custom command."
568 :group 'org-agenda-custom-commands
569 :type 'character)
571 (defcustom org-stuck-projects
572 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
573 "How to identify stuck projects.
574 This is a list of four items:
575 1. A tags/todo/property matcher string that is used to identify a project.
576 See the manual for a description of tag and property searches.
577 The entire tree below a headline matched by this is considered one project.
578 2. A list of TODO keywords identifying non-stuck projects.
579 If the project subtree contains any headline with one of these todo
580 keywords, the project is considered to be not stuck. If you specify
581 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
582 3. A list of tags identifying non-stuck projects.
583 If the project subtree contains any headline with one of these tags,
584 the project is considered to be not stuck. If you specify \"*\" as
585 a tag, any tag will mark the project unstuck. Note that this is about
586 the explicit presence of a tag somewhere in the subtree, inherited
587 tags do not count here. If inherited tags make a project not stuck,
588 use \"-TAG\" in the tags part of the matcher under (1.) above.
589 4. An arbitrary regular expression matching non-stuck projects.
591 If the project turns out to be not stuck, search continues also in the
592 subtree to see if any of the subtasks have project status.
594 See also the variable `org-tags-match-list-sublevels' which applies
595 to projects matched by this search as well.
597 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
598 or `C-c a #' to produce the list."
599 :group 'org-agenda-custom-commands
600 :type '(list
601 (string :tag "Tags/TODO match to identify a project")
602 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
603 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
604 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
606 (defcustom org-agenda-filter-effort-default-operator "<"
607 "The default operator for effort estimate filtering.
608 If you select an effort estimate limit without first pressing an operator,
609 this one will be used."
610 :group 'org-agenda-custom-commands
611 :type '(choice (const :tag "less or equal" "<")
612 (const :tag "greater or equal"">")
613 (const :tag "equal" "=")))
615 (defgroup org-agenda-skip nil
616 "Options concerning skipping parts of agenda files."
617 :tag "Org Agenda Skip"
618 :group 'org-agenda)
620 (defcustom org-agenda-skip-function-global nil
621 "Function to be called at each match during agenda construction.
622 If this function returns nil, the current match should not be skipped.
623 If the function decided to skip an agenda match, is must return the
624 buffer position from which the search should be continued.
625 This may also be a Lisp form, which will be evaluated.
627 This variable will be applied to every agenda match, including
628 tags/property searches and TODO lists. So try to make the test function
629 do its checking as efficiently as possible. To implement a skipping
630 condition just for specific agenda commands, use the variable
631 `org-agenda-skip-function' which can be set in the options section
632 of custom agenda commands."
633 :group 'org-agenda-skip
634 :type 'sexp)
636 (defgroup org-agenda-daily/weekly nil
637 "Options concerning the daily/weekly agenda."
638 :tag "Org Agenda Daily/Weekly"
639 :group 'org-agenda)
640 (defgroup org-agenda-todo-list nil
641 "Options concerning the global todo list agenda view."
642 :tag "Org Agenda Todo List"
643 :group 'org-agenda)
644 (defgroup org-agenda-match-view nil
645 "Options concerning the general tags/property/todo match agenda view."
646 :tag "Org Agenda Match View"
647 :group 'org-agenda)
648 (defgroup org-agenda-search-view nil
649 "Options concerning the general tags/property/todo match agenda view."
650 :tag "Org Agenda Match View"
651 :group 'org-agenda)
653 (defvar org-agenda-archives-mode nil
654 "Non-nil means the agenda will include archived items.
655 If this is the symbol `trees', trees in the selected agenda scope
656 that are marked with the ARCHIVE tag will be included anyway. When this is
657 t, also all archive files associated with the current selection of agenda
658 files will be included.")
660 (defcustom org-agenda-restriction-lock-highlight-subtree t
661 "Non-nil means highlight the whole subtree when restriction is active.
662 Otherwise only highlight the headline. Highlighting the whole subtree is
663 useful to ensure no edits happen beyond the restricted region."
664 :group 'org-agenda
665 :type 'boolean)
667 (defcustom org-agenda-skip-comment-trees t
668 "Non-nil means skip trees that start with the COMMENT keyword.
669 When nil, these trees are also scanned by agenda commands."
670 :group 'org-agenda-skip
671 :type 'boolean)
673 (defcustom org-agenda-todo-list-sublevels t
674 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
675 When nil, the sublevels of a TODO entry are not checked, resulting in
676 potentially much shorter TODO lists."
677 :group 'org-agenda-skip
678 :group 'org-agenda-todo-list
679 :type 'boolean)
681 (defcustom org-agenda-todo-ignore-with-date nil
682 "Non-nil means don't show entries with a date in the global todo list.
683 You can use this if you prefer to mark mere appointments with a TODO keyword,
684 but don't want them to show up in the TODO list.
685 When this is set, it also covers deadlines and scheduled items, the settings
686 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
687 will be ignored.
688 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
689 :group 'org-agenda-skip
690 :group 'org-agenda-todo-list
691 :type 'boolean)
693 (defcustom org-agenda-todo-ignore-timestamp nil
694 "Non-nil means don't show entries with a timestamp.
695 This applies when creating the global todo list.
696 Valid values are:
698 past Don't show entries for today or in the past.
700 future Don't show entries with a timestamp in the future.
701 The idea behind this is that if it has a future
702 timestamp, you don't want to think about it until the
703 date.
705 all Don't show any entries with a timestamp in the global todo list.
706 The idea behind this is that by setting a timestamp, you
707 have already \"taken care\" of this item.
709 This variable can also have an integer as a value. If positive (N),
710 todos with a timestamp N or more days in the future will be ignored. If
711 negative (-N), todos with a timestamp N or more days in the past will be
712 ignored. If 0, todos with a timestamp either today or in the future will
713 be ignored. For example, a value of -1 will exclude todos with a
714 timestamp in the past (yesterday or earlier), while a value of 7 will
715 exclude todos with a timestamp a week or more in the future.
717 See also `org-agenda-todo-ignore-with-date'.
718 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
719 to make his option also apply to the tags-todo list."
720 :group 'org-agenda-skip
721 :group 'org-agenda-todo-list
722 :version "24.1"
723 :type '(choice
724 (const :tag "Ignore future timestamp todos" future)
725 (const :tag "Ignore past or present timestamp todos" past)
726 (const :tag "Ignore all timestamp todos" all)
727 (const :tag "Show timestamp todos" nil)
728 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
730 (defcustom org-agenda-todo-ignore-scheduled nil
731 "Non-nil means, ignore some scheduled TODO items when making TODO list.
732 This applies when creating the global todo list.
733 Valid values are:
735 past Don't show entries scheduled today or in the past.
737 future Don't show entries scheduled in the future.
738 The idea behind this is that by scheduling it, you don't want to
739 think about it until the scheduled date.
741 all Don't show any scheduled entries in the global todo list.
742 The idea behind this is that by scheduling it, you have already
743 \"taken care\" of this item.
745 t Same as `all', for backward compatibility.
747 This variable can also have an integer as a value. See
748 `org-agenda-todo-ignore-timestamp' for more details.
750 See also `org-agenda-todo-ignore-with-date'.
751 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
752 to make his option also apply to the tags-todo list."
753 :group 'org-agenda-skip
754 :group 'org-agenda-todo-list
755 :type '(choice
756 (const :tag "Ignore future-scheduled todos" future)
757 (const :tag "Ignore past- or present-scheduled todos" past)
758 (const :tag "Ignore all scheduled todos" all)
759 (const :tag "Ignore all scheduled todos (compatibility)" t)
760 (const :tag "Show scheduled todos" nil)
761 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
763 (defcustom org-agenda-todo-ignore-deadlines nil
764 "Non-nil means ignore some deadlined TODO items when making TODO list.
765 There are different motivations for using different values, please think
766 carefully when configuring this variable.
768 This applies when creating the global todo list.
769 Valid values are:
771 near Don't show near deadline entries. A deadline is near when it is
772 closer than `org-deadline-warning-days' days. The idea behind this
773 is that such items will appear in the agenda anyway.
775 far Don't show TODO entries where a deadline has been defined, but
776 the deadline is not near. This is useful if you don't want to
777 use the todo list to figure out what to do now.
779 past Don't show entries with a deadline timestamp for today or in the past.
781 future Don't show entries with a deadline timestamp in the future, not even
782 when they become `near' ones. Use it with caution.
784 all Ignore all TODO entries that do have a deadline.
786 t Same as `near', for backward compatibility.
788 This variable can also have an integer as a value. See
789 `org-agenda-todo-ignore-timestamp' for more details.
791 See also `org-agenda-todo-ignore-with-date'.
792 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
793 to make his option also apply to the tags-todo list."
794 :group 'org-agenda-skip
795 :group 'org-agenda-todo-list
796 :type '(choice
797 (const :tag "Ignore near deadlines" near)
798 (const :tag "Ignore near deadlines (compatibility)" t)
799 (const :tag "Ignore far deadlines" far)
800 (const :tag "Ignore all TODOs with a deadlines" all)
801 (const :tag "Show all TODOs, even if they have a deadline" nil)
802 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
804 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
805 "Time unit to use when possibly ignoring an agenda item.
807 See the docstring of various `org-agenda-todo-ignore-*' options.
808 The default is to compare time stamps using days. An item is thus
809 considered to be in the future if it is at least one day after today.
810 Non-nil means to compare time stamps using seconds. An item is then
811 considered future if it has a time value later than current time."
812 :group 'org-agenda-skip
813 :group 'org-agenda-todo-list
814 :version "24.4"
815 :package-version '(Org . "8.0")
816 :type '(choice
817 (const :tag "Compare time with days" nil)
818 (const :tag "Compare time with seconds" t)))
820 (defcustom org-agenda-tags-todo-honor-ignore-options nil
821 "Non-nil means honor todo-list ignores options also in tags-todo search.
822 The variables
823 `org-agenda-todo-ignore-with-date',
824 `org-agenda-todo-ignore-timestamp',
825 `org-agenda-todo-ignore-scheduled',
826 `org-agenda-todo-ignore-deadlines'
827 make the global TODO list skip entries that have time stamps of certain
828 kinds. If this option is set, the same options will also apply for the
829 tags-todo search, which is the general tags/property matcher
830 restricted to unfinished TODO entries only."
831 :group 'org-agenda-skip
832 :group 'org-agenda-todo-list
833 :group 'org-agenda-match-view
834 :type 'boolean)
836 (defcustom org-agenda-skip-scheduled-if-done nil
837 "Non-nil means don't show scheduled items in agenda when they are done.
838 This is relevant for the daily/weekly agenda, not for the TODO list. And
839 it applies only to the actual date of the scheduling. Warnings about
840 an item with a past scheduling dates are always turned off when the item
841 is DONE."
842 :group 'org-agenda-skip
843 :group 'org-agenda-daily/weekly
844 :type 'boolean)
846 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
847 "Non-nil means skip scheduling line if same entry shows because of deadline.
849 In the agenda of today, an entry can show up multiple times
850 because it is both scheduled and has a nearby deadline, and maybe
851 a plain time stamp as well.
853 When this variable is nil, the entry will be shown several times.
855 When set to t, then only the deadline is shown and the fact that
856 the entry is scheduled today or was scheduled previously is not
857 shown.
859 When set to the symbol `not-today', skip scheduled previously,
860 but not scheduled today.
862 When set to the symbol `repeated-after-deadline', skip scheduled
863 items if they are repeated beyond the current dealine."
864 :group 'org-agenda-skip
865 :group 'org-agenda-daily/weekly
866 :type '(choice
867 (const :tag "Never" nil)
868 (const :tag "Always" t)
869 (const :tag "Not when scheduled today" not-today)
870 (const :tag "When repeated past deadline" repeated-after-deadline)))
872 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
873 "Non-nil means skip timestamp line if same entry shows because of deadline.
874 In the agenda of today, an entry can show up multiple times
875 because it has both a plain timestamp and has a nearby deadline.
876 When this variable is t, then only the deadline is shown and the
877 fact that the entry has a timestamp for or including today is not
878 shown. When this variable is nil, the entry will be shown
879 several times."
880 :group 'org-agenda-skip
881 :group 'org-agenda-daily/weekly
882 :version "24.1"
883 :type '(choice
884 (const :tag "Never" nil)
885 (const :tag "Always" t)))
887 (defcustom org-agenda-skip-deadline-if-done nil
888 "Non-nil means don't show deadlines when the corresponding item is done.
889 When nil, the deadline is still shown and should give you a happy feeling.
890 This is relevant for the daily/weekly agenda. And it applied only to the
891 actually date of the deadline. Warnings about approaching and past-due
892 deadlines are always turned off when the item is DONE."
893 :group 'org-agenda-skip
894 :group 'org-agenda-daily/weekly
895 :type 'boolean)
897 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
898 "Non-nil means skip deadline prewarning when entry is also scheduled.
899 This will apply on all days where a prewarning for the deadline would
900 be shown, but not at the day when the entry is actually due. On that day,
901 the deadline will be shown anyway.
902 This variable may be set to nil, t, the symbol `pre-scheduled',
903 or a number which will then give the number of days before the actual
904 deadline when the prewarnings should resume. The symbol `pre-scheduled'
905 eliminates the deadline prewarning only prior to the scheduled date.
906 This can be used in a workflow where the first showing of the deadline will
907 trigger you to schedule it, and then you don't want to be reminded of it
908 because you will take care of it on the day when scheduled."
909 :group 'org-agenda-skip
910 :group 'org-agenda-daily/weekly
911 :version "24.1"
912 :type '(choice
913 (const :tag "Always show prewarning" nil)
914 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
915 (const :tag "Remove prewarning if entry is scheduled" t)
916 (integer :tag "Restart prewarning N days before deadline")))
918 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
919 "Non-nil means skip scheduled delay when entry also has a deadline.
920 This variable may be set to nil, t, the symbol `post-deadline',
921 or a number which will then give the number of days after the actual
922 scheduled date when the delay should expire. The symbol `post-deadline'
923 eliminates the schedule delay when the date is posterior to the deadline."
924 :group 'org-agenda-skip
925 :group 'org-agenda-daily/weekly
926 :version "24.4"
927 :package-version '(Org . "8.0")
928 :type '(choice
929 (const :tag "Always honor delay" nil)
930 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
931 (const :tag "Ignore delay if entry has a deadline" t)
932 (integer :tag "Honor delay up until N days after the scheduled date")))
934 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
935 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
936 When non-nil, after the search for timestamps has matched once in an
937 entry, the rest of the entry will not be searched."
938 :group 'org-agenda-skip
939 :type 'boolean)
941 (defcustom org-agenda-skip-timestamp-if-done nil
942 "Non-nil means don't select item by timestamp or -range if it is DONE."
943 :group 'org-agenda-skip
944 :group 'org-agenda-daily/weekly
945 :type 'boolean)
947 (defcustom org-agenda-dim-blocked-tasks t
948 "Non-nil means dim blocked tasks in the agenda display.
949 This causes some overhead during agenda construction, but if you
950 have turned on `org-enforce-todo-dependencies',
951 `org-enforce-todo-checkbox-dependencies', or any other blocking
952 mechanism, this will create useful feedback in the agenda.
954 Instead of t, this variable can also have the value `invisible'.
955 Then blocked tasks will be invisible and only become visible when
956 they become unblocked. An exemption to this behavior is when a task is
957 blocked because of unchecked checkboxes below it. Since checkboxes do
958 not show up in the agenda views, making this task invisible you remove any
959 trace from agenda views that there is something to do. Therefore, a task
960 that is blocked because of checkboxes will never be made invisible, it
961 will only be dimmed."
962 :group 'org-agenda-daily/weekly
963 :group 'org-agenda-todo-list
964 :version "24.3"
965 :type '(choice
966 (const :tag "Do not dim" nil)
967 (const :tag "Dim to a gray face" t)
968 (const :tag "Make invisible" invisible)))
970 (defcustom org-timeline-show-empty-dates 3
971 "Non-nil means `org-timeline' also shows dates without an entry.
972 When nil, only the days which actually have entries are shown.
973 When t, all days between the first and the last date are shown.
974 When an integer, show also empty dates, but if there is a gap of more than
975 N days, just insert a special line indicating the size of the gap."
976 :group 'org-agenda-skip
977 :type '(choice
978 (const :tag "None" nil)
979 (const :tag "All" t)
980 (integer :tag "at most")))
982 (defgroup org-agenda-startup nil
983 "Options concerning initial settings in the Agenda in Org Mode."
984 :tag "Org Agenda Startup"
985 :group 'org-agenda)
987 (defcustom org-agenda-menu-show-matcher t
988 "Non-nil means show the match string in the agenda dispatcher menu.
989 When nil, the matcher string is not shown, but is put into the help-echo
990 property so than moving the mouse over the command shows it.
991 Setting it to nil is good if matcher strings are very long and/or if
992 you want to use two-columns display (see `org-agenda-menu-two-columns')."
993 :group 'org-agenda
994 :version "24.1"
995 :type 'boolean)
997 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
999 (defcustom org-agenda-menu-two-columns nil
1000 "Non-nil means, use two columns to show custom commands in the dispatcher.
1001 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1002 to nil."
1003 :group 'org-agenda
1004 :version "24.1"
1005 :type 'boolean)
1007 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1008 (defcustom org-agenda-finalize-hook nil
1009 "Hook run just before displaying an agenda buffer.
1010 The buffer is still writable when the hook is called.
1012 You can modify some of the buffer substrings but you should be
1013 extra careful not to modify the text properties of the agenda
1014 headlines as the agenda display heavily relies on them."
1015 :group 'org-agenda-startup
1016 :type 'hook)
1018 (defcustom org-agenda-mouse-1-follows-link nil
1019 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1020 A longer mouse click will still set point. Does not work on XEmacs.
1021 Needs to be set before org.el is loaded."
1022 :group 'org-agenda-startup
1023 :type 'boolean)
1025 (defcustom org-agenda-start-with-follow-mode nil
1026 "The initial value of follow mode in a newly created agenda window."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-follow-indirect nil
1031 "Non-nil means `org-agenda-follow-mode' displays only the
1032 current item's tree, in an indirect buffer."
1033 :group 'org-agenda
1034 :version "24.1"
1035 :type 'boolean)
1037 (defcustom org-agenda-show-outline-path t
1038 "Non-nil means show outline path in echo area after line motion."
1039 :group 'org-agenda-startup
1040 :type 'boolean)
1042 (defcustom org-agenda-start-with-entry-text-mode nil
1043 "The initial value of entry-text-mode in a newly created agenda window."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-entry-text-maxlines 5
1048 "Number of text lines to be added when `E' is pressed in the agenda.
1050 Note that this variable only used during agenda display. Add add entry text
1051 when exporting the agenda, configure the variable
1052 `org-agenda-add-entry-ext-maxlines'."
1053 :group 'org-agenda
1054 :type 'integer)
1056 (defcustom org-agenda-entry-text-exclude-regexps nil
1057 "List of regular expressions to clean up entry text.
1058 The complete matches of all regular expressions in this list will be
1059 removed from entry text before it is shown in the agenda."
1060 :group 'org-agenda
1061 :type '(repeat (regexp)))
1063 (defcustom org-agenda-entry-text-leaders " > "
1064 "Text prepended to the entry text in agenda buffers."
1065 :version "24.4"
1066 :package-version '(Org . "8.0")
1067 :group 'org-agenda
1068 :type 'string)
1070 (defvar org-agenda-entry-text-cleanup-hook nil
1071 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1072 This cleanup is done in a temporary buffer, so the function may inspect and
1073 change the entire buffer.
1074 Some default stuff like drawers and scheduling/deadline dates will already
1075 have been removed when this is called, as will any matches for regular
1076 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1078 (defvar org-agenda-include-inactive-timestamps nil
1079 "Non-nil means include inactive time stamps in agenda and timeline.
1080 Dynamically scoped.")
1082 (defgroup org-agenda-windows nil
1083 "Options concerning the windows used by the Agenda in Org Mode."
1084 :tag "Org Agenda Windows"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-window-setup 'reorganize-frame
1088 "How the agenda buffer should be displayed.
1089 Possible values for this option are:
1091 current-window Show agenda in the current window, keeping all other windows.
1092 other-window Use `switch-to-buffer-other-window' to display agenda.
1093 reorganize-frame Show only two windows on the current frame, the current
1094 window and the agenda.
1095 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1096 Also, when exiting the agenda, kill that frame.
1097 See also the variable `org-agenda-restore-windows-after-quit'."
1098 :group 'org-agenda-windows
1099 :type '(choice
1100 (const current-window)
1101 (const other-frame)
1102 (const other-window)
1103 (const reorganize-frame)))
1105 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1106 "The min and max height of the agenda window as a fraction of frame height.
1107 The value of the variable is a cons cell with two numbers between 0 and 1.
1108 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1109 :group 'org-agenda-windows
1110 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1112 (defcustom org-agenda-restore-windows-after-quit nil
1113 "Non-nil means restore window configuration upon exiting agenda.
1114 Before the window configuration is changed for displaying the agenda,
1115 the current status is recorded. When the agenda is exited with
1116 `q' or `x' and this option is set, the old state is restored. If
1117 `org-agenda-window-setup' is `other-frame', the value of this
1118 option will be ignored."
1119 :group 'org-agenda-windows
1120 :type 'boolean)
1122 (defcustom org-agenda-ndays nil
1123 "Number of days to include in overview display.
1124 Should be 1 or 7.
1125 Obsolete, see `org-agenda-span'."
1126 :group 'org-agenda-daily/weekly
1127 :type 'integer)
1129 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1131 (defcustom org-agenda-span 'week
1132 "Number of days to include in overview display.
1133 Can be day, week, month, year, or any number of days.
1134 Custom commands can set this variable in the options section."
1135 :group 'org-agenda-daily/weekly
1136 :type '(choice (const :tag "Day" day)
1137 (const :tag "Week" week)
1138 (const :tag "Month" month)
1139 (const :tag "Year" year)
1140 (integer :tag "Custom")))
1142 (defcustom org-agenda-start-on-weekday 1
1143 "Non-nil means start the overview always on the specified weekday.
1144 0 denotes Sunday, 1 denotes Monday, etc.
1145 When nil, always start on the current day.
1146 Custom commands can set this variable in the options section."
1147 :group 'org-agenda-daily/weekly
1148 :type '(choice (const :tag "Today" nil)
1149 (integer :tag "Weekday No.")))
1151 (defcustom org-agenda-show-all-dates t
1152 "Non-nil means `org-agenda' shows every day in the selected range.
1153 When nil, only the days which actually have entries are shown."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1157 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1158 "Format string for displaying dates in the agenda.
1159 Used by the daily/weekly agenda and by the timeline. This should be
1160 a format string understood by `format-time-string', or a function returning
1161 the formatted date as a string. The function must take a single argument,
1162 a calendar-style date list like (month day year)."
1163 :group 'org-agenda-daily/weekly
1164 :type '(choice
1165 (string :tag "Format string")
1166 (function :tag "Function")))
1168 (defun org-agenda-format-date-aligned (date)
1169 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1170 This function makes sure that dates are aligned for easy reading."
1171 (require 'cal-iso)
1172 (let* ((dayname (calendar-day-name date))
1173 (day (cadr date))
1174 (day-of-week (calendar-day-of-week date))
1175 (month (car date))
1176 (monthname (calendar-month-name month))
1177 (year (nth 2 date))
1178 (iso-week (org-days-to-iso-week
1179 (calendar-absolute-from-gregorian date)))
1180 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1181 (1- year))
1182 ((and (= month 12) (<= iso-week 1))
1183 (1+ year))
1184 (t year)))
1185 (weekstring (if (= day-of-week 1)
1186 (format " W%02d" iso-week)
1187 "")))
1188 (format "%-10s %2d %s %4d%s"
1189 dayname day monthname year weekstring)))
1191 (defcustom org-agenda-time-leading-zero nil
1192 "Non-nil means use leading zero for military times in agenda.
1193 For example, 9:30am would become 09:30 rather than 9:30."
1194 :group 'org-agenda-daily/weekly
1195 :version "24.1"
1196 :type 'boolean)
1198 (defcustom org-agenda-timegrid-use-ampm nil
1199 "When set, show AM/PM style timestamps on the timegrid."
1200 :group 'org-agenda
1201 :version "24.1"
1202 :type 'boolean)
1204 (defun org-agenda-time-of-day-to-ampm (time)
1205 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1206 (let* ((hour-number (string-to-number (substring time 0 -3)))
1207 (minute (substring time -2))
1208 (ampm "am"))
1209 (cond
1210 ((equal hour-number 12)
1211 (setq ampm "pm"))
1212 ((> hour-number 12)
1213 (setq ampm "pm")
1214 (setq hour-number (- hour-number 12))))
1215 (concat
1216 (if org-agenda-time-leading-zero
1217 (format "%02d" hour-number)
1218 (format "%02s" (number-to-string hour-number)))
1219 ":" minute ampm)))
1221 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1222 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1223 (if org-agenda-timegrid-use-ampm
1224 (org-agenda-time-of-day-to-ampm time)
1225 time))
1227 (defcustom org-agenda-weekend-days '(6 0)
1228 "Which days are weekend?
1229 These days get the special face `org-agenda-date-weekend' in the agenda
1230 and timeline buffers."
1231 :group 'org-agenda-daily/weekly
1232 :type '(set :greedy t
1233 (const :tag "Monday" 1)
1234 (const :tag "Tuesday" 2)
1235 (const :tag "Wednesday" 3)
1236 (const :tag "Thursday" 4)
1237 (const :tag "Friday" 5)
1238 (const :tag "Saturday" 6)
1239 (const :tag "Sunday" 0)))
1241 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1242 "Non-nil means jump to today when moving a past date forward in time.
1243 When using S-right in the agenda to move a a date forward, and the date
1244 stamp currently points to the past, the first key press will move it
1245 to today. WHen nil, just move one day forward even if the date stays
1246 in the past."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-include-diary nil
1252 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :type 'boolean)
1257 (defcustom org-agenda-include-deadlines t
1258 "If non-nil, include entries within their deadline warning period.
1259 Custom commands can set this variable in the options section."
1260 :group 'org-agenda-daily/weekly
1261 :version "24.1"
1262 :type 'boolean)
1264 (defcustom org-agenda-repeating-timestamp-show-all t
1265 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1266 When set to a list of strings, only show occurrences of repeating
1267 stamps for these TODO keywords. When nil, only one occurrence is
1268 shown, either today or the nearest into the future."
1269 :group 'org-agenda-daily/weekly
1270 :type '(choice
1271 (const :tag "Show repeating stamps" t)
1272 (repeat :tag "Show repeating stamps for these TODO keywords"
1273 (string :tag "TODO Keyword"))
1274 (const :tag "Don't show repeating stamps" nil)))
1276 (defcustom org-scheduled-past-days 10000
1277 "Number of days to continue listing scheduled items not marked DONE.
1278 When an item is scheduled on a date, it shows up in the agenda on this
1279 day and will be listed until it is marked done for the number of days
1280 given here."
1281 :group 'org-agenda-daily/weekly
1282 :type 'integer)
1284 (defcustom org-agenda-log-mode-items '(closed clock)
1285 "List of items that should be shown in agenda log mode.
1286 This list may contain the following symbols:
1288 closed Show entries that have been closed on that day.
1289 clock Show entries that have received clocked time on that day.
1290 state Show all logged state changes.
1291 Note that instead of changing this variable, you can also press `C-u l' in
1292 the agenda to display all available LOG items temporarily."
1293 :group 'org-agenda-daily/weekly
1294 :type '(set :greedy t (const closed) (const clock) (const state)))
1296 (defcustom org-agenda-clock-consistency-checks
1297 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1298 :gap-ok-around ("4:00")
1299 :default-face ((:background "DarkRed") (:foreground "white"))
1300 :overlap-face nil :gap-face nil :no-end-time-face nil
1301 :long-face nil :short-face nil)
1302 "This is a property list, with the following keys:
1304 :max-duration Mark clocking chunks that are longer than this time.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :min-duration Mark clocking chunks that are shorter that this.
1309 This is a time string like \"HH:MM\", or the number
1310 of minutes as an integer.
1312 :max-gap Mark gaps between clocking chunks that are longer than
1313 this duration. A number of minutes, or a string
1314 like \"HH:MM\".
1316 :gap-ok-around List of times during the day which are usually not working
1317 times. When a gap is detected, but the gap contains any
1318 of these times, the gap is *not* reported. For example,
1319 if this is (\"4:00\" \"13:00\") then gaps that contain
1320 4:00 in the morning (i.e. the night) and 13:00
1321 (i.e. a typical lunch time) do not cause a warning.
1322 You should have at least one time during the night in this
1323 list, or otherwise the first task each morning will trigger
1324 a warning because it follows a long gap.
1326 Furthermore, the following properties can be used to define faces for
1327 issue display.
1329 :default-face the default face, if the specific face is undefined
1330 :overlap-face face for overlapping clocks
1331 :gap-face face for gaps between clocks
1332 :no-end-time-face face for incomplete clocks
1333 :long-face face for clock intervals that are too long
1334 :short-face face for clock intervals that are too short"
1335 :group 'org-agenda-daily/weekly
1336 :group 'org-clock
1337 :version "24.1"
1338 :type 'plist)
1340 (defcustom org-agenda-log-mode-add-notes t
1341 "Non-nil means add first line of notes to log entries in agenda views.
1342 If a log item like a state change or a clock entry is associated with
1343 notes, the first line of these notes will be added to the entry in the
1344 agenda display."
1345 :group 'org-agenda-daily/weekly
1346 :type 'boolean)
1348 (defcustom org-agenda-start-with-log-mode nil
1349 "The initial value of log-mode in a newly created agenda window.
1350 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1351 explanations on the possible values."
1352 :group 'org-agenda-startup
1353 :group 'org-agenda-daily/weekly
1354 :type '(choice (const :tag "Don't show log items" nil)
1355 (const :tag "Show only log items" 'only)
1356 (const :tag "Show all possible log items" 'clockcheck)
1357 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1358 (choice (const :tag "Show closed log items" 'closed)
1359 (const :tag "Show clocked log items" 'clock)
1360 (const :tag "Show all logged state changes" 'state)))))
1362 (defcustom org-agenda-start-with-clockreport-mode nil
1363 "The initial value of clockreport-mode in a newly created agenda window."
1364 :group 'org-agenda-startup
1365 :group 'org-agenda-daily/weekly
1366 :type 'boolean)
1368 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1369 "Property list with parameters for the clocktable in clockreport mode.
1370 This is the display mode that shows a clock table in the daily/weekly
1371 agenda, the properties for this dynamic block can be set here.
1372 The usual clocktable parameters are allowed here, but you cannot set
1373 the properties :name, :tstart, :tend, :block, and :scope - these will
1374 be overwritten to make sure the content accurately reflects the
1375 current display in the agenda."
1376 :group 'org-agenda-daily/weekly
1377 :type 'plist)
1379 (defcustom org-agenda-search-view-always-boolean nil
1380 "Non-nil means the search string is interpreted as individual parts.
1382 The search string for search view can either be interpreted as a phrase,
1383 or as a list of snippets that define a boolean search for a number of
1384 strings.
1386 When this is non-nil, the string will be split on whitespace, and each
1387 snippet will be searched individually, and all must match in order to
1388 select an entry. A snippet is then a single string of non-white
1389 characters, or a string in double quotes, or a regexp in {} braces.
1390 If a snippet is preceded by \"-\", the snippet must *not* match.
1391 \"+\" is syntactic sugar for positive selection. Each snippet may
1392 be found as a full word or a partial word, but see the variable
1393 `org-agenda-search-view-force-full-words'.
1395 When this is nil, search will look for the entire search phrase as one,
1396 with each space character matching any amount of whitespace, including
1397 line breaks.
1399 Even when this is nil, you can still switch to Boolean search dynamically
1400 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1401 is a regexp marked with braces like \"{abc}\", this will also switch to
1402 boolean search."
1403 :group 'org-agenda-search-view
1404 :version "24.1"
1405 :type 'boolean)
1407 (org-defvaralias 'org-agenda-search-view-search-words-only
1408 'org-agenda-search-view-always-boolean)
1410 (defcustom org-agenda-search-view-force-full-words nil
1411 "Non-nil means, search words must be matches as complete words.
1412 When nil, they may also match part of a word."
1413 :group 'org-agenda-search-view
1414 :version "24.1"
1415 :type 'boolean)
1417 (defcustom org-agenda-search-view-max-outline-level nil
1418 "Maximum outline level to display in search view.
1419 E.g. when this is set to 1, the search view will only
1420 show headlines of level 1."
1421 :group 'org-agenda-search-view
1422 :version "24.4"
1423 :package-version '(Org . "8.0")
1424 :type 'integer)
1426 (defgroup org-agenda-time-grid nil
1427 "Options concerning the time grid in the Org-mode Agenda."
1428 :tag "Org Agenda Time Grid"
1429 :group 'org-agenda)
1431 (defcustom org-agenda-search-headline-for-time t
1432 "Non-nil means search headline for a time-of-day.
1433 If the headline contains a time-of-day in one format or another, it will
1434 be used to sort the entry into the time sequence of items for a day.
1435 Some people have time stamps in the headline that refer to the creation
1436 time or so, and then this produces an unwanted side effect. If this is
1437 the case for your, use this variable to turn off searching the headline
1438 for a time."
1439 :group 'org-agenda-time-grid
1440 :type 'boolean)
1442 (defcustom org-agenda-use-time-grid t
1443 "Non-nil means show a time grid in the agenda schedule.
1444 A time grid is a set of lines for specific times (like every two hours between
1445 8:00 and 20:00). The items scheduled for a day at specific times are
1446 sorted in between these lines.
1447 For details about when the grid will be shown, and what it will look like, see
1448 the variable `org-agenda-time-grid'."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1452 (defcustom org-agenda-time-grid
1453 '((daily today require-timed)
1454 "----------------"
1455 (800 1000 1200 1400 1600 1800 2000))
1457 "The settings for time grid for agenda display.
1458 This is a list of three items. The first item is again a list. It contains
1459 symbols specifying conditions when the grid should be displayed:
1461 daily if the agenda shows a single day
1462 weekly if the agenda shows an entire week
1463 today show grid on current date, independent of daily/weekly display
1464 require-timed show grid only if at least one item has a time specification
1466 The second item is a string which will be placed behind the grid time.
1468 The third item is a list of integers, indicating the times that should have
1469 a grid line."
1470 :group 'org-agenda-time-grid
1471 :type
1472 '(list
1473 (set :greedy t :tag "Grid Display Options"
1474 (const :tag "Show grid in single day agenda display" daily)
1475 (const :tag "Show grid in weekly agenda display" weekly)
1476 (const :tag "Always show grid for today" today)
1477 (const :tag "Show grid only if any timed entries are present"
1478 require-timed)
1479 (const :tag "Skip grid times already present in an entry"
1480 remove-match))
1481 (string :tag "Grid String")
1482 (repeat :tag "Grid Times" (integer :tag "Time"))))
1484 (defcustom org-agenda-show-current-time-in-grid t
1485 "Non-nil means show the current time in the time grid."
1486 :group 'org-agenda-time-grid
1487 :version "24.1"
1488 :type 'boolean)
1490 (defcustom org-agenda-current-time-string
1491 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1492 "The string for the current time marker in the agenda."
1493 :group 'org-agenda-time-grid
1494 :version "24.1"
1495 :type 'string)
1497 (defgroup org-agenda-sorting nil
1498 "Options concerning sorting in the Org-mode Agenda."
1499 :tag "Org Agenda Sorting"
1500 :group 'org-agenda)
1502 (defcustom org-agenda-sorting-strategy
1503 '((agenda habit-down time-up priority-down category-keep)
1504 (todo priority-down category-keep)
1505 (tags priority-down category-keep)
1506 (search category-keep))
1507 "Sorting structure for the agenda items of a single day.
1508 This is a list of symbols which will be used in sequence to determine
1509 if an entry should be listed before another entry. The following
1510 symbols are recognized:
1512 time-up Put entries with time-of-day indications first, early first
1513 time-down Put entries with time-of-day indications first, late first
1514 timestamp-up Sort by any timestamp, early first
1515 timestamp-down Sort by any timestamp, late first
1516 scheduled-up Sort by scheduled timestamp, early first
1517 scheduled-down Sort by scheduled timestamp, late first
1518 deadline-up Sort by deadline timestamp, early first
1519 deadline-down Sort by deadline timestamp, late first
1520 ts-up Sort by active timestamp, early first
1521 ts-down Sort by active timestamp, late first
1522 tsia-up Sort by inactive timestamp, early first
1523 tsia-down Sort by inactive timestamp, late first
1524 category-keep Keep the default order of categories, corresponding to the
1525 sequence in `org-agenda-files'.
1526 category-up Sort alphabetically by category, A-Z.
1527 category-down Sort alphabetically by category, Z-A.
1528 tag-up Sort alphabetically by last tag, A-Z.
1529 tag-down Sort alphabetically by last tag, Z-A.
1530 priority-up Sort numerically by priority, high priority last.
1531 priority-down Sort numerically by priority, high priority first.
1532 todo-state-up Sort by todo state, tasks that are done last.
1533 todo-state-down Sort by todo state, tasks that are done first.
1534 effort-up Sort numerically by estimated effort, high effort last.
1535 effort-down Sort numerically by estimated effort, high effort first.
1536 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1537 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1538 habit-up Put entries that are habits first
1539 habit-down Put entries that are habits last
1540 alpha-up Sort headlines alphabetically
1541 alpha-down Sort headlines alphabetically, reversed
1543 The different possibilities will be tried in sequence, and testing stops
1544 if one comparison returns a \"not-equal\". For example, the default
1545 '(time-up category-keep priority-down)
1546 means: Pull out all entries having a specified time of day and sort them,
1547 in order to make a time schedule for the current day the first thing in the
1548 agenda listing for the day. Of the entries without a time indication, keep
1549 the grouped in categories, don't sort the categories, but keep them in
1550 the sequence given in `org-agenda-files'. Within each category sort by
1551 priority.
1553 Leaving out `category-keep' would mean that items will be sorted across
1554 categories by priority.
1556 Instead of a single list, this can also be a set of list for specific
1557 contents, with a context symbol in the car of the list, any of
1558 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1560 Custom commands can bind this variable in the options section."
1561 :group 'org-agenda-sorting
1562 :type `(choice
1563 (repeat :tag "General" ,org-sorting-choice)
1564 (list :tag "Individually"
1565 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1566 (repeat ,org-sorting-choice))
1567 (cons (const :tag "Strategy for TODO lists" todo)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for Tags matches" tags)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for search matches" search)
1572 (repeat ,org-sorting-choice)))))
1574 (defcustom org-agenda-cmp-user-defined nil
1575 "A function to define the comparison `user-defined'.
1576 This function must receive two arguments, agenda entry a and b.
1577 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1578 the user comparison, return nil.
1579 When this is defined, you can make `user-defined-up' and `user-defined-down'
1580 part of an agenda sorting strategy."
1581 :group 'org-agenda-sorting
1582 :type 'symbol)
1584 (defcustom org-sort-agenda-notime-is-late t
1585 "Non-nil means items without time are considered late.
1586 This is only relevant for sorting. When t, items which have no explicit
1587 time like 15:30 will be considered as 99:01, i.e. later than any items which
1588 do have a time. When nil, the default time is before 0:00. You can use this
1589 option to decide if the schedule for today should come before or after timeless
1590 agenda entries."
1591 :group 'org-agenda-sorting
1592 :type 'boolean)
1594 (defcustom org-sort-agenda-noeffort-is-high t
1595 "Non-nil means items without effort estimate are sorted as high effort.
1596 This also applies when filtering an agenda view with respect to the
1597 < or > effort operator. Then, tasks with no effort defined will be treated
1598 as tasks with high effort.
1599 When nil, such items are sorted as 0 minutes effort."
1600 :group 'org-agenda-sorting
1601 :type 'boolean)
1603 (defgroup org-agenda-line-format nil
1604 "Options concerning the entry prefix in the Org-mode agenda display."
1605 :tag "Org Agenda Line Format"
1606 :group 'org-agenda)
1608 (defcustom org-agenda-prefix-format
1609 '((agenda . " %i %-12:c%?-12t% s")
1610 (timeline . " % s")
1611 (todo . " %i %-12:c")
1612 (tags . " %i %-12:c")
1613 (search . " %i %-12:c"))
1614 "Format specifications for the prefix of items in the agenda views.
1615 An alist with five entries, each for the different agenda types. The
1616 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1617 The values are format strings.
1619 This format works similar to a printf format, with the following meaning:
1621 %c the category of the item, \"Diary\" for entries from the diary,
1622 or as given by the CATEGORY keyword or derived from the file name
1623 %e the effort required by the item
1624 %l the level of the item (insert X space(s) if item is of level X)
1625 %i the icon category of the item, see `org-agenda-category-icon-alist'
1626 %T the last tag of the item (ignore inherited tags, which come first)
1627 %t the HH:MM time-of-day specification if one applies to the entry
1628 %s Scheduling/Deadline information, a short string
1629 %b show breadcrumbs, i.e., the names of the higher levels
1630 %(expression) Eval EXPRESSION and replace the control string
1631 by the result
1633 All specifiers work basically like the standard `%s' of printf, but may
1634 contain two additional characters: a question mark just after the `%'
1635 and a whitespace/punctuation character just before the final letter.
1637 If the first character after `%' is a question mark, the entire field
1638 will only be included if the corresponding value applies to the current
1639 entry. This is useful for fields which should have fixed width when
1640 present, but zero width when absent. For example, \"%?-12t\" will
1641 result in a 12 character time field if a time of the day is specified,
1642 but will completely disappear in entries which do not contain a time.
1644 If there is punctuation or whitespace character just before the
1645 final format letter, this character will be appended to the field
1646 value if the value is not empty. For example, the format
1647 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1648 the category is empty, no additional colon is inserted.
1650 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1651 which means:
1653 - Indent the line with two space characters
1654 - Give the category a 12 chars wide field, padded with whitespace on
1655 the right (because of `-'). Append a colon if there is a category
1656 (because of `:').
1657 - If there is a time-of-day, put it into a 12 chars wide field. If no
1658 time, don't put in an empty field, just skip it (because of '?').
1659 - Finally, put the scheduling information.
1661 See also the variables `org-agenda-remove-times-when-in-prefix' and
1662 `org-agenda-remove-tags'.
1664 Custom commands can set this variable in the options section."
1665 :type '(choice
1666 (string :tag "General format")
1667 (list :greedy t :tag "View dependent"
1668 (cons (const agenda) (string :tag "Format"))
1669 (cons (const timeline) (string :tag "Format"))
1670 (cons (const todo) (string :tag "Format"))
1671 (cons (const tags) (string :tag "Format"))
1672 (cons (const search) (string :tag "Format"))))
1673 :group 'org-agenda-line-format)
1675 (defvar org-prefix-format-compiled nil
1676 "The compiled prefix format and associated variables.
1677 This is a list where first element is a list of variable bindings, and second
1678 element is the compiled format expression. See the variable
1679 `org-agenda-prefix-format'.")
1681 (defcustom org-agenda-todo-keyword-format "%-1s"
1682 "Format for the TODO keyword in agenda lines.
1683 Set this to something like \"%-12s\" if you want all TODO keywords
1684 to occupy a fixed space in the agenda display."
1685 :group 'org-agenda-line-format
1686 :type 'string)
1688 (defcustom org-agenda-diary-sexp-prefix nil
1689 "A regexp that matches part of a diary sexp entry
1690 which should be treated as scheduling/deadline information in
1691 `org-agenda'.
1693 For example, you can use this to extract the `diary-remind-message' from
1694 `diary-remind' entries."
1695 :group 'org-agenda-line-format
1696 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1698 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1699 "Text preceding timerange entries in the agenda view.
1700 This is a list with two strings. The first applies when the range
1701 is entirely on one day. The second applies if the range spans several days.
1702 The strings may have two \"%d\" format specifiers which will be filled
1703 with the sequence number of the days, and the total number of days in the
1704 range, respectively."
1705 :group 'org-agenda-line-format
1706 :type '(list
1707 (string :tag "Deadline today ")
1708 (choice :tag "Deadline relative"
1709 (string :tag "Format string")
1710 (function))))
1712 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1713 "Text preceding scheduled items in the agenda view.
1714 This is a list with two strings. The first applies when the item is
1715 scheduled on the current day. The second applies when it has been scheduled
1716 previously, it may contain a %d indicating that this is the nth time that
1717 this item is scheduled, due to automatic rescheduling of unfinished items
1718 for the following day. So this number is one larger than the number of days
1719 that passed since this item was scheduled first."
1720 :group 'org-agenda-line-format
1721 :version "24.4"
1722 :package-version '(Org . "8.0")
1723 :type '(list
1724 (string :tag "Scheduled today ")
1725 (string :tag "Scheduled previously")))
1727 (defcustom org-agenda-inactive-leader "["
1728 "Text preceding item pulled into the agenda by inactive time stamps.
1729 These entries are added to the agenda when pressing \"[\"."
1730 :group 'org-agenda-line-format
1731 :version "24.1"
1732 :type '(list
1733 (string :tag "Scheduled today ")
1734 (string :tag "Scheduled previously")))
1736 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1737 "Text preceding deadline items in the agenda view.
1738 This is a list with three strings. The first applies when the item has its
1739 deadline on the current day. The second applies when the deadline is in the
1740 future, the third one when it is in the past. The strings may contain %d
1741 to capture the number of days."
1742 :group 'org-agenda-line-format
1743 :version "24.4"
1744 :package-version '(Org . "8.0")
1745 :type '(list
1746 (string :tag "Deadline today ")
1747 (choice :tag "Deadline relative"
1748 (string :tag "Format string")
1749 (function))))
1751 (defcustom org-agenda-remove-times-when-in-prefix t
1752 "Non-nil means remove duplicate time specifications in agenda items.
1753 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1754 time-of-day specification in a headline or diary entry is extracted and
1755 placed into the prefix. If this option is non-nil, the original specification
1756 \(a timestamp or -range, or just a plain time(range) specification like
1757 11:30-4pm) will be removed for agenda display. This makes the agenda less
1758 cluttered.
1759 The option can be t or nil. It may also be the symbol `beg', indicating
1760 that the time should only be removed when it is located at the beginning of
1761 the headline/diary entry."
1762 :group 'org-agenda-line-format
1763 :type '(choice
1764 (const :tag "Always" t)
1765 (const :tag "Never" nil)
1766 (const :tag "When at beginning of entry" beg)))
1768 (defcustom org-agenda-remove-timeranges-from-blocks nil
1769 "Non-nil means remove time ranges specifications in agenda
1770 items that span on several days."
1771 :group 'org-agenda-line-format
1772 :version "24.1"
1773 :type 'boolean)
1775 (defcustom org-agenda-default-appointment-duration nil
1776 "Default duration for appointments that only have a starting time.
1777 When nil, no duration is specified in such cases.
1778 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1779 :group 'org-agenda-line-format
1780 :type '(choice
1781 (integer :tag "Minutes")
1782 (const :tag "No default duration")))
1784 (defcustom org-agenda-show-inherited-tags t
1785 "Non-nil means show inherited tags in each agenda line.
1787 When this option is set to 'always, it take precedences over
1788 `org-agenda-use-tag-inheritance' and inherited tags are shown
1789 in every agenda.
1791 When this option is set to t (the default), inherited tags are
1792 shown when they are available, i.e. when the value of
1793 `org-agenda-use-tag-inheritance' has been taken into account.
1795 This can be set to a list of agenda types in which the agenda
1796 must display the inherited tags. Available types are 'todo,
1797 'agenda, 'search and 'timeline.
1799 When set to nil, never show inherited tags in agenda lines."
1800 :group 'org-agenda-line-format
1801 :group 'org-agenda
1802 :version "24.3"
1803 :type '(choice
1804 (const :tag "Show inherited tags when available" t)
1805 (const :tag "Always show inherited tags" 'always)
1806 (repeat :tag "Show inherited tags only in selected agenda types"
1807 (symbol :tag "Agenda type"))))
1809 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1810 "List of agenda view types where to use tag inheritance.
1812 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1813 controlled by `org-use-tag-inheritance'. In other agenda types,
1814 `org-use-tag-inheritance' is not used for the selection of the
1815 agenda entries. Still, you may want the agenda to be aware of
1816 the inherited tags anyway, e.g. for later tag filtering.
1818 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1820 This variable has no effect if `org-agenda-show-inherited-tags'
1821 is set to 'always. In that case, the agenda is aware of those
1822 tags.
1824 The default value sets tags in every agenda type. Setting this
1825 option to nil will speed up non-tags agenda view a lot."
1826 :group 'org-agenda
1827 :version "24.3"
1828 :type '(choice
1829 (const :tag "Use tag inheritance in all agenda types" t)
1830 (repeat :tag "Use tag inheritance in selected agenda types"
1831 (symbol :tag "Agenda type"))))
1833 (defcustom org-agenda-hide-tags-regexp nil
1834 "Regular expression used to filter away specific tags in agenda views.
1835 This means that these tags will be present, but not be shown in the agenda
1836 line. Secondary filtering will still work on the hidden tags.
1837 Nil means don't hide any tags."
1838 :group 'org-agenda-line-format
1839 :type '(choice
1840 (const :tag "Hide none" nil)
1841 (string :tag "Regexp ")))
1843 (defcustom org-agenda-remove-tags nil
1844 "Non-nil means remove the tags from the headline copy in the agenda.
1845 When this is the symbol `prefix', only remove tags when
1846 `org-agenda-prefix-format' contains a `%T' specifier."
1847 :group 'org-agenda-line-format
1848 :type '(choice
1849 (const :tag "Always" t)
1850 (const :tag "Never" nil)
1851 (const :tag "When prefix format contains %T" prefix)))
1853 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1854 'org-agenda-remove-tags)
1856 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1857 "Shift tags in agenda items to this column.
1858 If this number is positive, it specifies the column. If it is negative,
1859 it means that the tags should be flushright to that column. For example,
1860 -80 works well for a normal 80 character screen."
1861 :group 'org-agenda-line-format
1862 :type 'integer)
1864 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1866 (defcustom org-agenda-fontify-priorities 'cookies
1867 "Non-nil means highlight low and high priorities in agenda.
1868 When t, the highest priority entries are bold, lowest priority italic.
1869 However, settings in `org-priority-faces' will overrule these faces.
1870 When this variable is the symbol `cookies', only fontify the
1871 cookies, not the entire task.
1872 This may also be an association list of priority faces, whose
1873 keys are the character values of `org-highest-priority',
1874 `org-default-priority', and `org-lowest-priority' (the default values
1875 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1876 color as a string, or a list like `(:background \"Red\")'.
1877 If it is a color, the variable `org-faces-easy-properties'
1878 determines if it is a foreground or a background color."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Never" nil)
1882 (const :tag "Defaults" t)
1883 (const :tag "Cookies only" cookies)
1884 (repeat :tag "Specify"
1885 (list (character :tag "Priority" :value ?A)
1886 (choice :tag "Face "
1887 (string :tag "Color")
1888 (sexp :tag "Face"))))))
1890 (defcustom org-agenda-day-face-function nil
1891 "Function called to determine what face should be used to display a day.
1892 The only argument passed to that function is the day. It should
1893 returns a face, or nil if does not want to specify a face and let
1894 the normal rules apply."
1895 :group 'org-agenda-line-format
1896 :version "24.1"
1897 :type 'function)
1899 (defcustom org-agenda-category-icon-alist nil
1900 "Alist of category icon to be displayed in agenda views.
1902 Each entry should have the following format:
1904 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1906 Where CATEGORY-REGEXP is a regexp matching the categories where
1907 the icon should be displayed.
1908 FILE-OR-DATA either a file path or a string containing image data.
1910 The other fields can be omitted safely if not needed:
1911 TYPE indicates the image type.
1912 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1913 image data.
1914 PROPS are additional image attributes to assign to the image,
1915 like, e.g. `:ascent center'.
1917 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1919 If you want to set the display properties yourself, just put a
1920 list as second element:
1922 (CATEGORY-REGEXP (MY PROPERTY LIST))
1924 For example, to display a 16px horizontal space for Emacs
1925 category, you can use:
1927 (\"Emacs\" '(space . (:width (16))))"
1928 :group 'org-agenda-line-format
1929 :version "24.1"
1930 :type '(alist :key-type (string :tag "Regexp matching category")
1931 :value-type (choice (list :tag "Icon"
1932 (string :tag "File or data")
1933 (symbol :tag "Type")
1934 (boolean :tag "Data?")
1935 (repeat :tag "Extra image properties" :inline t symbol))
1936 (list :tag "Display properties" sexp))))
1938 (defgroup org-agenda-column-view nil
1939 "Options concerning column view in the agenda."
1940 :tag "Org Agenda Column View"
1941 :group 'org-agenda)
1943 (defcustom org-agenda-columns-show-summaries t
1944 "Non-nil means show summaries for columns displayed in the agenda view."
1945 :group 'org-agenda-column-view
1946 :type 'boolean)
1948 (defcustom org-agenda-columns-compute-summary-properties t
1949 "Non-nil means recompute all summary properties before column view.
1950 When column view in the agenda is listing properties that have a summary
1951 operator, it can go to all relevant buffers and recompute the summaries
1952 there. This can mean overhead for the agenda column view, but is necessary
1953 to have thing up to date.
1954 As a special case, a CLOCKSUM property also makes sure that the clock
1955 computations are current."
1956 :group 'org-agenda-column-view
1957 :type 'boolean)
1959 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1960 "Non-nil means the duration of an appointment will add to day effort.
1961 The property to which appointment durations will be added is the one given
1962 in the option `org-effort-property'. If an appointment does not have
1963 an end time, `org-agenda-default-appointment-duration' will be used. If that
1964 is not set, an appointment without end time will not contribute to the time
1965 estimate."
1966 :group 'org-agenda-column-view
1967 :type 'boolean)
1969 (defcustom org-agenda-auto-exclude-function nil
1970 "A function called with a tag to decide if it is filtered on '/ RET'.
1971 The sole argument to the function, which is called once for each
1972 possible tag, is a string giving the name of the tag. The
1973 function should return either nil if the tag should be included
1974 as normal, or \"-<TAG>\" to exclude the tag.
1975 Note that for the purpose of tag filtering, only the lower-case version of
1976 all tags will be considered, so that this function will only ever see
1977 the lower-case version of all tags."
1978 :group 'org-agenda
1979 :type 'function)
1981 (defcustom org-agenda-bulk-custom-functions nil
1982 "Alist of characters and custom functions for bulk actions.
1983 For example, this value makes those two functions available:
1985 '((?R set-category)
1986 (?C bulk-cut))
1988 With selected entries in an agenda buffer, `B R' will call
1989 the custom function `set-category' on the selected entries.
1990 Note that functions in this alist don't need to be quoted."
1991 :type 'alist
1992 :version "24.1"
1993 :group 'org-agenda)
1995 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1996 "Execute BODY with point at location given by `org-hd-marker' property.
1997 If STRING is non-nil, the text property will be fetched from position 0
1998 in that string. If STRING is nil, it will be fetched from the beginning
1999 of the current line."
2000 (org-with-gensyms (marker)
2001 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2002 'org-hd-marker ,string)))
2003 (with-current-buffer (marker-buffer ,marker)
2004 (save-excursion
2005 (goto-char ,marker)
2006 ,@body)))))
2007 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2009 (defun org-add-agenda-custom-command (entry)
2010 "Replace or add a command in `org-agenda-custom-commands'.
2011 This is mostly for hacking and trying a new command - once the command
2012 works you probably want to add it to `org-agenda-custom-commands' for good."
2013 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2014 (if ass
2015 (setcdr ass (cdr entry))
2016 (push entry org-agenda-custom-commands))))
2018 ;;; Define the org-agenda-mode
2020 (defvar org-agenda-mode-map (make-sparse-keymap)
2021 "Keymap for `org-agenda-mode'.")
2022 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2024 (defvar org-agenda-menu) ; defined later in this file.
2025 (defvar org-agenda-restrict nil) ; defined later in this file.
2026 (defvar org-agenda-follow-mode nil)
2027 (defvar org-agenda-entry-text-mode nil)
2028 (defvar org-agenda-clockreport-mode nil)
2029 (defvar org-agenda-show-log nil)
2030 (defvar org-agenda-redo-command nil)
2031 (defvar org-agenda-query-string nil)
2032 (defvar org-agenda-mode-hook nil
2033 "Hook run after `org-agenda-mode' is turned on.
2034 The buffer is still writable when this hook is called.")
2035 (defvar org-agenda-type nil)
2036 (defvar org-agenda-force-single-file nil)
2037 (defvar org-agenda-bulk-marked-entries nil
2038 "List of markers that refer to marked entries in the agenda.")
2040 ;;; Multiple agenda buffers support
2042 (defcustom org-agenda-sticky nil
2043 "Non-nil means agenda q key will bury agenda buffers.
2044 Agenda commands will then show existing buffer instead of generating new ones.
2045 When nil, `q' will kill the single agenda buffer."
2046 :group 'org-agenda
2047 :version "24.3"
2048 :type 'boolean)
2051 ;;;###autoload
2052 (defun org-toggle-sticky-agenda (&optional arg)
2053 "Toggle `org-agenda-sticky'."
2054 (interactive "P")
2055 (let ((new-value (if arg
2056 (> (prefix-numeric-value arg) 0)
2057 (not org-agenda-sticky))))
2058 (if (equal new-value org-agenda-sticky)
2059 (and (org-called-interactively-p 'interactive)
2060 (message "Sticky agenda was already %s"
2061 (if org-agenda-sticky "enabled" "disabled")))
2062 (setq org-agenda-sticky new-value)
2063 (org-agenda-kill-all-agenda-buffers)
2064 (and (org-called-interactively-p 'interactive)
2065 (message "Sticky agenda was %s"
2066 (if org-agenda-sticky "enabled" "disabled"))))))
2068 (defvar org-agenda-buffer nil
2069 "Agenda buffer currently being generated.")
2071 (defvar org-agenda-last-prefix-arg nil)
2072 (defvar org-agenda-this-buffer-name nil)
2073 (defvar org-agenda-doing-sticky-redo nil)
2074 (defvar org-agenda-this-buffer-is-sticky nil)
2076 (defconst org-agenda-local-vars
2077 '(org-agenda-this-buffer-name
2078 org-agenda-undo-list
2079 org-agenda-pending-undo-list
2080 org-agenda-follow-mode
2081 org-agenda-entry-text-mode
2082 org-agenda-clockreport-mode
2083 org-agenda-show-log
2084 org-agenda-redo-command
2085 org-agenda-query-string
2086 org-agenda-type
2087 org-agenda-bulk-marked-entries
2088 org-agenda-undo-has-started-in
2089 org-agenda-info
2090 org-agenda-pre-window-conf
2091 org-agenda-columns-active
2092 org-agenda-tag-filter-overlays
2093 org-agenda-tag-filter
2094 org-agenda-cat-filter-overlays
2095 org-agenda-category-filter
2096 org-agenda-re-filter-overlays
2097 org-agenda-regexp-filter
2098 org-agenda-markers
2099 org-agenda-last-search-view-search-was-boolean
2100 org-agenda-filtered-by-category
2101 org-agenda-filter-form
2102 org-agenda-cycle-counter
2103 org-agenda-last-prefix-arg)
2104 "Variables that must be local in agenda buffers to allow multiple buffers.")
2106 (defun org-agenda-mode ()
2107 "Mode for time-sorted view on action items in Org-mode files.
2109 The following commands are available:
2111 \\{org-agenda-mode-map}"
2112 (interactive)
2113 (cond (org-agenda-doing-sticky-redo
2114 ;; Refreshing sticky agenda-buffer
2116 ;; Preserve the value of `org-agenda-local-vars' variables,
2117 ;; while letting `kill-all-local-variables' kill the rest
2118 (let ((save (buffer-local-variables)))
2119 (kill-all-local-variables)
2120 (mapc 'make-local-variable org-agenda-local-vars)
2121 (dolist (elem save)
2122 (let ((var (car elem))
2123 (val (cdr elem)))
2124 (when (and val
2125 (member var org-agenda-local-vars))
2126 (set var val)))))
2127 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2128 (org-agenda-sticky
2129 ;; Creating a sticky Agenda buffer for the first time
2130 (kill-all-local-variables)
2131 (mapc 'make-local-variable org-agenda-local-vars)
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 ;; Creating a non-sticky agenda buffer
2135 (kill-all-local-variables)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2137 (setq org-agenda-undo-list nil
2138 org-agenda-pending-undo-list nil
2139 org-agenda-bulk-marked-entries nil)
2140 (setq major-mode 'org-agenda-mode)
2141 ;; Keep global-font-lock-mode from turning on font-lock-mode
2142 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2143 (setq mode-name "Org-Agenda")
2144 (use-local-map org-agenda-mode-map)
2145 (easy-menu-add org-agenda-menu)
2146 (if org-startup-truncated (setq truncate-lines t))
2147 (org-set-local 'line-move-visual nil)
2148 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2149 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2150 ;; Make sure properties are removed when copying text
2151 (org-add-hook 'filter-buffer-substring-functions
2152 (lambda (fun start end delete)
2153 (substring-no-properties (funcall fun start end delete)))
2154 nil t)
2155 (unless org-agenda-keep-modes
2156 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2157 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2158 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2159 org-agenda-show-log org-agenda-start-with-log-mode))
2161 (easy-menu-change
2162 '("Agenda") "Agenda Files"
2163 (append
2164 (list
2165 (vector
2166 (if (get 'org-agenda-files 'org-restrict)
2167 "Restricted to single file"
2168 "Edit File List")
2169 '(org-edit-agenda-file-list)
2170 (not (get 'org-agenda-files 'org-restrict)))
2171 "--")
2172 (mapcar 'org-file-menu-entry (org-agenda-files))))
2173 (org-agenda-set-mode-name)
2174 (apply
2175 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2176 (list 'org-agenda-mode-hook)))
2178 (substitute-key-definition 'undo 'org-agenda-undo
2179 org-agenda-mode-map global-map)
2180 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2181 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2182 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2183 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2184 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2185 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2186 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2187 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2188 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2189 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2190 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2191 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2192 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2193 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2194 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2195 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2196 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2197 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2198 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2199 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2200 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2201 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2202 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2203 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2204 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2205 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2206 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2207 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2208 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2209 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2210 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2211 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2212 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2213 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2214 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2215 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2216 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2217 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2218 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2219 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2220 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2221 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2222 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2223 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2224 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2225 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2226 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2227 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2228 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2229 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2231 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2232 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2233 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2234 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2235 (while l (org-defkey org-agenda-mode-map
2236 (int-to-string (pop l)) 'digit-argument)))
2238 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2239 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2240 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2241 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2242 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2243 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2244 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2245 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2246 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2247 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2248 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2249 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2250 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2251 'org-clock-modify-effort-estimate)
2252 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2253 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2254 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2255 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2256 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2257 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2258 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2259 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2260 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2261 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2262 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2263 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2264 (substitute-key-definition 'next-line 'org-agenda-next-line
2265 org-agenda-mode-map global-map)
2266 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2267 org-agenda-mode-map global-map)
2268 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2269 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2270 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2271 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2272 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2273 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2274 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2275 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2276 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2277 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2278 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2279 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2280 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2281 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2282 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2283 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2285 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2287 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2288 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2289 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2290 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2291 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2292 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2293 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2294 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2295 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2296 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2299 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2300 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2301 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2302 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2303 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2304 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2305 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2306 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2307 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2308 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2309 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2310 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2311 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2312 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2313 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2315 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2316 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2317 (when org-agenda-mouse-1-follows-link
2318 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2319 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2320 '("Agenda"
2321 ("Agenda Files")
2322 "--"
2323 ("Agenda Dates"
2324 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2325 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2326 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2327 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2328 "--"
2329 ("View"
2330 ["Day View" org-agenda-day-view
2331 :active (org-agenda-check-type nil 'agenda)
2332 :style radio :selected (eq org-agenda-current-span 'day)
2333 :keys "v d (or just d)"]
2334 ["Week View" org-agenda-week-view
2335 :active (org-agenda-check-type nil 'agenda)
2336 :style radio :selected (eq org-agenda-current-span 'week)
2337 :keys "v w (or just w)"]
2338 ["Month View" org-agenda-month-view
2339 :active (org-agenda-check-type nil 'agenda)
2340 :style radio :selected (eq org-agenda-current-span 'month)
2341 :keys "v m"]
2342 ["Year View" org-agenda-year-view
2343 :active (org-agenda-check-type nil 'agenda)
2344 :style radio :selected (eq org-agenda-current-span 'year)
2345 :keys "v y"]
2346 "--"
2347 ["Include Diary" org-agenda-toggle-diary
2348 :style toggle :selected org-agenda-include-diary
2349 :active (org-agenda-check-type nil 'agenda)]
2350 ["Include Deadlines" org-agenda-toggle-deadlines
2351 :style toggle :selected org-agenda-include-deadlines
2352 :active (org-agenda-check-type nil 'agenda)]
2353 ["Use Time Grid" org-agenda-toggle-time-grid
2354 :style toggle :selected org-agenda-use-time-grid
2355 :active (org-agenda-check-type nil 'agenda)]
2356 "--"
2357 ["Show clock report" org-agenda-clockreport-mode
2358 :style toggle :selected org-agenda-clockreport-mode
2359 :active (org-agenda-check-type nil 'agenda)]
2360 ["Show some entry text" org-agenda-entry-text-mode
2361 :style toggle :selected org-agenda-entry-text-mode
2362 :active t]
2363 "--"
2364 ["Show Logbook entries" org-agenda-log-mode
2365 :style toggle :selected org-agenda-show-log
2366 :active (org-agenda-check-type nil 'agenda 'timeline)
2367 :keys "v l (or just l)"]
2368 ["Include archived trees" org-agenda-archives-mode
2369 :style toggle :selected org-agenda-archives-mode :active t
2370 :keys "v a"]
2371 ["Include archive files" (org-agenda-archives-mode t)
2372 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2373 :keys "v A"]
2374 "--"
2375 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2376 ["Write view to file" org-agenda-write t]
2377 ["Rebuild buffer" org-agenda-redo t]
2378 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2379 "--"
2380 ["Show original entry" org-agenda-show t]
2381 ["Go To (other window)" org-agenda-goto t]
2382 ["Go To (this window)" org-agenda-switch-to t]
2383 ["Capture with cursor date" org-agenda-capture t]
2384 ["Follow Mode" org-agenda-follow-mode
2385 :style toggle :selected org-agenda-follow-mode :active t]
2386 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2387 "--"
2388 ("TODO"
2389 ["Cycle TODO" org-agenda-todo t]
2390 ["Next TODO set" org-agenda-todo-nextset t]
2391 ["Previous TODO set" org-agenda-todo-previousset t]
2392 ["Add note" org-agenda-add-note t])
2393 ("Archive/Refile/Delete"
2394 ["Archive default" org-agenda-archive-default t]
2395 ["Archive default" org-agenda-archive-default-with-confirmation t]
2396 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2397 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2398 ["Archive subtree" org-agenda-archive t]
2399 "--"
2400 ["Refile" org-agenda-refile t]
2401 "--"
2402 ["Delete subtree" org-agenda-kill t])
2403 ("Bulk action"
2404 ["Mark entry" org-agenda-bulk-mark t]
2405 ["Mark all" org-agenda-bulk-mark-all t]
2406 ["Unmark entry" org-agenda-bulk-unmark t]
2407 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2408 ["Toggle mark" org-agenda-bulk-toggle t]
2409 ["Toggle all" org-agenda-bulk-toggle-all t]
2410 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2411 ["Act on all marked" org-agenda-bulk-action t]
2412 "--"
2413 ("Tags and Properties"
2414 ["Show all Tags" org-agenda-show-tags t]
2415 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2416 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2417 "--"
2418 ["Column View" org-columns t])
2419 ("Deadline/Schedule"
2420 ["Schedule" org-agenda-schedule t]
2421 ["Set Deadline" org-agenda-deadline t]
2422 "--"
2423 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2424 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2425 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2426 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2427 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2428 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2429 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2430 ("Clock and Effort"
2431 ["Clock in" org-agenda-clock-in t]
2432 ["Clock out" org-agenda-clock-out t]
2433 ["Clock cancel" org-agenda-clock-cancel t]
2434 ["Goto running clock" org-clock-goto t]
2435 "--"
2436 ["Set Effort" org-agenda-set-effort t]
2437 ["Change clocked effort" org-clock-modify-effort-estimate
2438 (org-clock-is-active)])
2439 ("Priority"
2440 ["Set Priority" org-agenda-priority t]
2441 ["Increase Priority" org-agenda-priority-up t]
2442 ["Decrease Priority" org-agenda-priority-down t]
2443 ["Show Priority" org-show-priority t])
2444 ("Calendar/Diary"
2445 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2446 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2447 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2448 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2449 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2450 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2451 "--"
2452 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2453 "--"
2454 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2455 "--"
2456 ("MobileOrg"
2457 ["Push Files and Views" org-mobile-push t]
2458 ["Get Captured and Flagged" org-mobile-pull t]
2459 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2460 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2461 "--"
2462 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2463 "--"
2464 ["Quit" org-agenda-quit t]
2465 ["Exit and Release Buffers" org-agenda-exit t]
2468 ;;; Agenda undo
2470 (defvar org-agenda-allow-remote-undo t
2471 "Non-nil means allow remote undo from the agenda buffer.")
2472 (defvar org-agenda-undo-has-started-in nil
2473 "Buffers that have already seen `undo-start' in the current undo sequence.")
2475 (defun org-agenda-undo ()
2476 "Undo a remote editing step in the agenda.
2477 This undoes changes both in the agenda buffer and in the remote buffer
2478 that have been changed along."
2479 (interactive)
2480 (or org-agenda-allow-remote-undo
2481 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2482 (if (not (eq this-command last-command))
2483 (setq org-agenda-undo-has-started-in nil
2484 org-agenda-pending-undo-list org-agenda-undo-list))
2485 (if (not org-agenda-pending-undo-list)
2486 (user-error "No further undo information"))
2487 (let* ((entry (pop org-agenda-pending-undo-list))
2488 buf line cmd rembuf)
2489 (setq cmd (pop entry) line (pop entry))
2490 (setq rembuf (nth 2 entry))
2491 (org-with-remote-undo rembuf
2492 (while (bufferp (setq buf (pop entry)))
2493 (if (pop entry)
2494 (with-current-buffer buf
2495 (let ((last-undo-buffer buf)
2496 (inhibit-read-only t))
2497 (unless (memq buf org-agenda-undo-has-started-in)
2498 (push buf org-agenda-undo-has-started-in)
2499 (make-local-variable 'pending-undo-list)
2500 (undo-start))
2501 (while (and pending-undo-list
2502 (listp pending-undo-list)
2503 (not (car pending-undo-list)))
2504 (pop pending-undo-list))
2505 (undo-more 1))))))
2506 (org-goto-line line)
2507 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2509 (defun org-verify-change-for-undo (l1 l2)
2510 "Verify that a real change occurred between the undo lists L1 and L2."
2511 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2512 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2513 (not (eq l1 l2)))
2515 ;;; Agenda dispatch
2517 (defvar org-agenda-restrict-begin (make-marker))
2518 (defvar org-agenda-restrict-end (make-marker))
2519 (defvar org-agenda-last-dispatch-buffer nil)
2520 (defvar org-agenda-overriding-restriction nil)
2522 (defcustom org-agenda-custom-commands-contexts nil
2523 "Alist of custom agenda keys and contextual rules.
2525 For example, if you have a custom agenda command \"p\" and you
2526 want this command to be accessible only from plain text files,
2527 use this:
2529 '((\"p\" ((in-file . \"\\.txt\"))))
2531 Here are the available contexts definitions:
2533 in-file: command displayed only in matching files
2534 in-mode: command displayed only in matching modes
2535 not-in-file: command not displayed in matching files
2536 not-in-mode: command not displayed in matching modes
2537 in-buffer: command displayed only in matching buffers
2538 not-in-buffer: command not displayed in matching buffers
2539 [function]: a custom function taking no argument
2541 If you define several checks, the agenda command will be
2542 accessible if there is at least one valid check.
2544 You can also bind a key to another agenda custom command
2545 depending on contextual rules.
2547 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2549 Here it means: in .txt files, use \"p\" as the key for the
2550 agenda command otherwise associated with \"q\". (The command
2551 originally associated with \"q\" is not displayed to avoid
2552 duplicates.)"
2553 :version "24.3"
2554 :group 'org-agenda-custom-commands
2555 :type '(repeat (list :tag "Rule"
2556 (string :tag " Agenda key")
2557 (string :tag "Replace by command")
2558 (repeat :tag "Available when"
2559 (choice
2560 (cons :tag "Condition"
2561 (choice
2562 (const :tag "In file" in-file)
2563 (const :tag "Not in file" not-in-file)
2564 (const :tag "In buffer" in-buffer)
2565 (const :tag "Not in buffer" not-in-buffer)
2566 (const :tag "In mode" in-mode)
2567 (const :tag "Not in mode" not-in-mode))
2568 (regexp))
2569 (function :tag "Custom function"))))))
2571 (defcustom org-agenda-max-entries nil
2572 "Maximum number of entries to display in an agenda.
2573 This can be nil (no limit) or an integer or an alist of agenda
2574 types with an associated number of entries to display in this
2575 type."
2576 :version "24.4"
2577 :package-version '(Org . "8.0")
2578 :group 'org-agenda-custom-commands
2579 :type '(choice (symbol :tag "No limit" nil)
2580 (integer :tag "Max number of entries")
2581 (repeat
2582 (cons (choice :tag "Agenda type"
2583 (const agenda)
2584 (const todo)
2585 (const tags)
2586 (const search)
2587 (const timeline))
2588 (integer :tag "Max number of entries")))))
2590 (defcustom org-agenda-max-todos nil
2591 "Maximum number of TODOs to display in an agenda.
2592 This can be nil (no limit) or an integer or an alist of agenda
2593 types with an associated number of entries to display in this
2594 type."
2595 :version "24.4"
2596 :package-version '(Org . "8.0")
2597 :group 'org-agenda-custom-commands
2598 :type '(choice (symbol :tag "No limit" nil)
2599 (integer :tag "Max number of entries")
2600 (repeat
2601 (cons (choice :tag "Agenda type"
2602 (const agenda)
2603 (const todo)
2604 (const tags)
2605 (const search)
2606 (const timeline))
2607 (integer :tag "Max number of entries")))))
2609 (defcustom org-agenda-max-tags nil
2610 "Maximum number of tagged entries to display in an agenda.
2611 This can be nil (no limit) or an integer or an alist of agenda
2612 types with an associated number of entries to display in this
2613 type."
2614 :version "24.4"
2615 :package-version '(Org . "8.0")
2616 :group 'org-agenda-custom-commands
2617 :type '(choice (symbol :tag "No limit" nil)
2618 (integer :tag "Max number of entries")
2619 (repeat
2620 (cons (choice :tag "Agenda type"
2621 (const agenda)
2622 (const todo)
2623 (const tags)
2624 (const search)
2625 (const timeline))
2626 (integer :tag "Max number of entries")))))
2628 (defcustom org-agenda-max-effort nil
2629 "Maximum cumulated effort duration for the agenda.
2630 This can be nil (no limit) or a number of minutes (as an integer)
2631 or an alist of agenda types with an associated number of minutes
2632 to limit entries to in this type."
2633 :version "24.4"
2634 :package-version '(Org . "8.0")
2635 :group 'org-agenda-custom-commands
2636 :type '(choice (symbol :tag "No limit" nil)
2637 (integer :tag "Max number of entries")
2638 (repeat
2639 (cons (choice :tag "Agenda type"
2640 (const agenda)
2641 (const todo)
2642 (const tags)
2643 (const search)
2644 (const timeline))
2645 (integer :tag "Max number of entries")))))
2647 (defvar org-keys nil)
2648 (defvar org-match nil)
2649 ;;;###autoload
2650 (defun org-agenda (&optional arg org-keys restriction)
2651 "Dispatch agenda commands to collect entries to the agenda buffer.
2652 Prompts for a command to execute. Any prefix arg will be passed
2653 on to the selected command. The default selections are:
2655 a Call `org-agenda-list' to display the agenda for current day or week.
2656 t Call `org-todo-list' to display the global todo list.
2657 T Call `org-todo-list' to display the global todo list, select only
2658 entries with a specific TODO keyword (the user gets a prompt).
2659 m Call `org-tags-view' to display headlines with tags matching
2660 a condition (the user is prompted for the condition).
2661 M Like `m', but select only TODO entries, no ordinary headlines.
2662 L Create a timeline for the current buffer.
2663 e Export views to associated files.
2664 s Search entries for keywords.
2665 S Search entries for keywords, only with TODO keywords.
2666 / Multi occur across all agenda files and also files listed
2667 in `org-agenda-text-search-extra-files'.
2668 < Restrict agenda commands to buffer, subtree, or region.
2669 Press several times to get the desired effect.
2670 > Remove a previous restriction.
2671 # List \"stuck\" projects.
2672 ! Configure what \"stuck\" means.
2673 C Configure custom agenda commands.
2675 More commands can be added by configuring the variable
2676 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2677 searches can be pre-defined in this way.
2679 If the current buffer is in Org-mode and visiting a file, you can also
2680 first press `<' once to indicate that the agenda should be temporarily
2681 \(until the next use of \\[org-agenda]) restricted to the current file.
2682 Pressing `<' twice means to restrict to the current subtree or region
2683 \(if active)."
2684 (interactive "P")
2685 (catch 'exit
2686 (let* ((prefix-descriptions nil)
2687 (org-agenda-buffer-name org-agenda-buffer-name)
2688 (org-agenda-window-setup (if (equal (buffer-name)
2689 org-agenda-buffer-name)
2690 'current-window
2691 org-agenda-window-setup))
2692 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2693 (org-agenda-custom-commands
2694 ;; normalize different versions
2695 (delq nil
2696 (mapcar
2697 (lambda (x)
2698 (cond ((stringp (cdr x))
2699 (push x prefix-descriptions)
2700 nil)
2701 ((stringp (nth 1 x)) x)
2702 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2703 (t (cons (car x) (cons "" (cdr x))))))
2704 org-agenda-custom-commands)))
2705 (org-agenda-custom-commands
2706 (org-contextualize-keys
2707 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2708 (buf (current-buffer))
2709 (bfn (buffer-file-name (buffer-base-buffer)))
2710 entry key type org-match lprops ans)
2711 ;; Turn off restriction unless there is an overriding one,
2712 (unless org-agenda-overriding-restriction
2713 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2714 ;; There is a request to keep the file list in place
2715 (put 'org-agenda-files 'org-restrict nil))
2716 (setq org-agenda-restrict nil)
2717 (move-marker org-agenda-restrict-begin nil)
2718 (move-marker org-agenda-restrict-end nil))
2719 ;; Delete old local properties
2720 (put 'org-agenda-redo-command 'org-lprops nil)
2721 ;; Delete previously set last-arguments
2722 (put 'org-agenda-redo-command 'last-args nil)
2723 ;; Remember where this call originated
2724 (setq org-agenda-last-dispatch-buffer (current-buffer))
2725 (unless org-keys
2726 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2727 org-keys (car ans)
2728 restriction (cdr ans)))
2729 ;; If we have sticky agenda buffers, set a name for the buffer,
2730 ;; depending on the invoking keys. The user may still set this
2731 ;; as a command option, which will overwrite what we do here.
2732 (if org-agenda-sticky
2733 (setq org-agenda-buffer-name
2734 (format "*Org Agenda(%s)*" org-keys)))
2735 ;; Establish the restriction, if any
2736 (when (and (not org-agenda-overriding-restriction) restriction)
2737 (put 'org-agenda-files 'org-restrict (list bfn))
2738 (cond
2739 ((eq restriction 'region)
2740 (setq org-agenda-restrict t)
2741 (move-marker org-agenda-restrict-begin (region-beginning))
2742 (move-marker org-agenda-restrict-end (region-end)))
2743 ((eq restriction 'subtree)
2744 (save-excursion
2745 (setq org-agenda-restrict t)
2746 (org-back-to-heading t)
2747 (move-marker org-agenda-restrict-begin (point))
2748 (move-marker org-agenda-restrict-end
2749 (progn (org-end-of-subtree t)))))))
2751 ;; For example the todo list should not need it (but does...)
2752 (cond
2753 ((setq entry (assoc org-keys org-agenda-custom-commands))
2754 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2755 (progn
2756 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2757 lprops (nth 4 entry))
2758 (if org-agenda-sticky
2759 (setq org-agenda-buffer-name
2760 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2761 (format "*Org Agenda(%s)*" org-keys))))
2762 (put 'org-agenda-redo-command 'org-lprops lprops)
2763 (cond
2764 ((eq type 'agenda)
2765 (org-let lprops '(org-agenda-list current-prefix-arg)))
2766 ((eq type 'agenda*)
2767 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2768 ((eq type 'alltodo)
2769 (org-let lprops '(org-todo-list current-prefix-arg)))
2770 ((eq type 'search)
2771 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2772 ((eq type 'stuck)
2773 (org-let lprops '(org-agenda-list-stuck-projects
2774 current-prefix-arg)))
2775 ((eq type 'tags)
2776 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2777 ((eq type 'tags-todo)
2778 (org-let lprops '(org-tags-view '(4) org-match)))
2779 ((eq type 'todo)
2780 (org-let lprops '(org-todo-list org-match)))
2781 ((eq type 'tags-tree)
2782 (org-check-for-org-mode)
2783 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2784 ((eq type 'todo-tree)
2785 (org-check-for-org-mode)
2786 (org-let lprops
2787 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2788 (regexp-quote org-match) "\\>"))))
2789 ((eq type 'occur-tree)
2790 (org-check-for-org-mode)
2791 (org-let lprops '(org-occur org-match)))
2792 ((functionp type)
2793 (org-let lprops '(funcall type org-match)))
2794 ((fboundp type)
2795 (org-let lprops '(funcall type org-match)))
2796 (t (user-error "Invalid custom agenda command type %s" type))))
2797 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2798 ((equal org-keys "C")
2799 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2800 (customize-variable 'org-agenda-custom-commands))
2801 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2802 ((equal org-keys "s") (call-interactively 'org-search-view))
2803 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2804 ((equal org-keys "t") (call-interactively 'org-todo-list))
2805 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2806 ((equal org-keys "m") (call-interactively 'org-tags-view))
2807 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2808 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2809 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2810 (org-add-hook
2811 'post-command-hook
2812 (lambda ()
2813 (unless (current-message)
2814 (let* ((m (org-agenda-get-any-marker))
2815 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2816 (when note
2817 (message (concat
2818 "FLAGGING-NOTE ([?] for more info): "
2819 (org-add-props
2820 (replace-regexp-in-string
2821 "\\\\n" "//"
2822 (copy-sequence note))
2823 nil 'face 'org-warning)))))))
2824 t t))
2825 ((equal org-keys "L")
2826 (unless (derived-mode-p 'org-mode)
2827 (user-error "This is not an Org-mode file"))
2828 (unless restriction
2829 (put 'org-agenda-files 'org-restrict (list bfn))
2830 (org-call-with-arg 'org-timeline arg)))
2831 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2832 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2833 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2834 (t (user-error "Invalid agenda key"))))))
2836 (defun org-agenda-append-agenda ()
2837 "Append another agenda view to the current one.
2838 This function allows interactive building of block agendas.
2839 Agenda views are separated by `org-agenda-block-separator'."
2840 (interactive)
2841 (unless (derived-mode-p 'org-agenda-mode)
2842 (user-error "Can only append from within agenda buffer"))
2843 (let ((org-agenda-multi t))
2844 (org-agenda)
2845 (widen)
2846 (org-agenda-finalize)
2847 (setq buffer-read-only t)
2848 (org-agenda-fit-window-to-buffer)))
2850 (defun org-agenda-normalize-custom-commands (cmds)
2851 "Normalize custom commands CMDS."
2852 (delq nil
2853 (mapcar
2854 (lambda (x)
2855 (cond ((stringp (cdr x)) nil)
2856 ((stringp (nth 1 x)) x)
2857 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2858 (t (cons (car x) (cons "" (cdr x))))))
2859 cmds)))
2861 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2862 "The user interface for selecting an agenda command."
2863 (catch 'exit
2864 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2865 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2866 (region-p (org-region-active-p))
2867 (custom org-agenda-custom-commands)
2868 (selstring "")
2869 restriction second-time
2870 c entry key type match prefixes rmheader header-end custom1 desc
2871 line lines left right n n1)
2872 (save-window-excursion
2873 (delete-other-windows)
2874 (org-switch-to-buffer-other-window " *Agenda Commands*")
2875 (erase-buffer)
2876 (insert (eval-when-compile
2877 (let ((header
2878 "Press key for an agenda command: < Buffer, subtree/region restriction
2879 -------------------------------- > Remove restriction
2880 a Agenda for current week or day e Export agenda views
2881 t List of all TODO entries T Entries with special TODO kwd
2882 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2883 s Search for keywords S Like s, but only TODO entries
2884 L Timeline for current buffer # List stuck projects (!=configure)
2885 / Multi-occur C Configure custom agenda commands
2886 ? Find :FLAGGED: entries * Toggle sticky agenda views
2888 (start 0))
2889 (while (string-match
2890 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2891 header start)
2892 (setq start (match-end 0))
2893 (add-text-properties (match-beginning 2) (match-end 2)
2894 '(face bold) header))
2895 header)))
2896 (setq header-end (point-marker))
2897 (while t
2898 (setq custom1 custom)
2899 (when (eq rmheader t)
2900 (org-goto-line 1)
2901 (re-search-forward ":" nil t)
2902 (delete-region (match-end 0) (point-at-eol))
2903 (forward-char 1)
2904 (looking-at "-+")
2905 (delete-region (match-end 0) (point-at-eol))
2906 (move-marker header-end (match-end 0)))
2907 (goto-char header-end)
2908 (delete-region (point) (point-max))
2910 ;; Produce all the lines that describe custom commands and prefixes
2911 (setq lines nil)
2912 (while (setq entry (pop custom1))
2913 (setq key (car entry) desc (nth 1 entry)
2914 type (nth 2 entry)
2915 match (nth 3 entry))
2916 (if (> (length key) 1)
2917 (add-to-list 'prefixes (string-to-char key))
2918 (setq line
2919 (format
2920 "%-4s%-14s"
2921 (org-add-props (copy-sequence key)
2922 '(face bold))
2923 (cond
2924 ((string-match "\\S-" desc) desc)
2925 ((eq type 'agenda) "Agenda for current week or day")
2926 ((eq type 'agenda*) "Appointments for current week or day")
2927 ((eq type 'alltodo) "List of all TODO entries")
2928 ((eq type 'search) "Word search")
2929 ((eq type 'stuck) "List of stuck projects")
2930 ((eq type 'todo) "TODO keyword")
2931 ((eq type 'tags) "Tags query")
2932 ((eq type 'tags-todo) "Tags (TODO)")
2933 ((eq type 'tags-tree) "Tags tree")
2934 ((eq type 'todo-tree) "TODO kwd tree")
2935 ((eq type 'occur-tree) "Occur tree")
2936 ((functionp type) (if (symbolp type)
2937 (symbol-name type)
2938 "Lambda expression"))
2939 (t "???"))))
2940 (if org-agenda-menu-show-matcher
2941 (setq line
2942 (concat line ": "
2943 (cond
2944 ((stringp match)
2945 (setq match (copy-sequence match))
2946 (org-add-props match nil 'face 'org-warning))
2947 ((listp type)
2948 (format "set of %d commands" (length type))))))
2949 (if (org-string-nw-p match)
2950 (add-text-properties
2951 0 (length line) (list 'help-echo
2952 (concat "Matcher: " match)) line)))
2953 (push line lines)))
2954 (setq lines (nreverse lines))
2955 (when prefixes
2956 (mapc (lambda (x)
2957 (push
2958 (format "%s %s"
2959 (org-add-props (char-to-string x)
2960 nil 'face 'bold)
2961 (or (cdr (assoc (concat selstring
2962 (char-to-string x))
2963 prefix-descriptions))
2964 "Prefix key"))
2965 lines))
2966 prefixes))
2968 ;; Check if we should display in two columns
2969 (if org-agenda-menu-two-columns
2970 (progn
2971 (setq n (length lines)
2972 n1 (+ (/ n 2) (mod n 2))
2973 right (nthcdr n1 lines)
2974 left (copy-sequence lines))
2975 (setcdr (nthcdr (1- n1) left) nil))
2976 (setq left lines right nil))
2977 (while left
2978 (insert "\n" (pop left))
2979 (when right
2980 (if (< (current-column) 40)
2981 (move-to-column 40 t)
2982 (insert " "))
2983 (insert (pop right))))
2985 ;; Make the window the right size
2986 (goto-char (point-min))
2987 (if second-time
2988 (if (not (pos-visible-in-window-p (point-max)))
2989 (org-fit-window-to-buffer))
2990 (setq second-time t)
2991 (org-fit-window-to-buffer))
2993 ;; Ask for selection
2994 (message "Press key for agenda command%s:"
2995 (if (or restrict-ok org-agenda-overriding-restriction)
2996 (if org-agenda-overriding-restriction
2997 " (restriction lock active)"
2998 (if restriction
2999 (format " (restricted to %s)" restriction)
3000 " (unrestricted)"))
3001 ""))
3002 (setq c (read-char-exclusive))
3003 (message "")
3004 (cond
3005 ((assoc (char-to-string c) custom)
3006 (setq selstring (concat selstring (char-to-string c)))
3007 (throw 'exit (cons selstring restriction)))
3008 ((memq c prefixes)
3009 (setq selstring (concat selstring (char-to-string c))
3010 prefixes nil
3011 rmheader (or rmheader t)
3012 custom (delq nil (mapcar
3013 (lambda (x)
3014 (if (or (= (length (car x)) 1)
3015 (/= (string-to-char (car x)) c))
3017 (cons (substring (car x) 1) (cdr x))))
3018 custom))))
3019 ((eq c ?*)
3020 (call-interactively 'org-toggle-sticky-agenda)
3021 (sit-for 2))
3022 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3023 (message "Restriction is only possible in Org-mode buffers")
3024 (ding) (sit-for 1))
3025 ((eq c ?1)
3026 (org-agenda-remove-restriction-lock 'noupdate)
3027 (setq restriction 'buffer))
3028 ((eq c ?0)
3029 (org-agenda-remove-restriction-lock 'noupdate)
3030 (setq restriction (if region-p 'region 'subtree)))
3031 ((eq c ?<)
3032 (org-agenda-remove-restriction-lock 'noupdate)
3033 (setq restriction
3034 (cond
3035 ((eq restriction 'buffer)
3036 (if region-p 'region 'subtree))
3037 ((memq restriction '(subtree region))
3038 nil)
3039 (t 'buffer))))
3040 ((eq c ?>)
3041 (org-agenda-remove-restriction-lock 'noupdate)
3042 (setq restriction nil))
3043 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3044 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3045 ((and (> (length selstring) 0) (eq c ?\d))
3046 (delete-window)
3047 (org-agenda-get-restriction-and-command prefix-descriptions))
3049 ((equal c ?q) (error "Abort"))
3050 (t (user-error "Invalid key %c" c))))))))
3052 (defun org-agenda-fit-window-to-buffer ()
3053 "Fit the window to the buffer size."
3054 (and (memq org-agenda-window-setup '(reorganize-frame))
3055 (fboundp 'fit-window-to-buffer)
3056 (org-fit-window-to-buffer
3058 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3059 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3061 (defvar org-cmd nil)
3062 (defvar org-agenda-overriding-cmd nil)
3063 (defvar org-agenda-overriding-arguments nil)
3064 (defvar org-agenda-overriding-cmd-arguments nil)
3065 (defun org-agenda-run-series (name series)
3066 "Run agenda NAME as a SERIES of agenda commands."
3067 (org-let (nth 1 series) '(org-agenda-prepare name))
3068 ;; We need to reset agenda markers here, because when constructing a
3069 ;; block agenda, the individual blocks do not do that.
3070 (org-agenda-reset-markers)
3071 (let* ((org-agenda-multi t)
3072 (redo (list 'org-agenda-run-series name (list 'quote series)))
3073 (cmds (car series))
3074 (gprops (nth 1 series))
3075 match ;; The byte compiler incorrectly complains about this. Keep it!
3076 org-cmd type lprops)
3077 (while (setq org-cmd (pop cmds))
3078 (setq type (car org-cmd)
3079 match (eval (nth 1 org-cmd))
3080 lprops (nth 2 org-cmd))
3081 (let ((org-agenda-overriding-arguments
3082 (if (eq org-agenda-overriding-cmd org-cmd)
3083 (or org-agenda-overriding-arguments
3084 org-agenda-overriding-cmd-arguments))))
3085 (cond
3086 ((eq type 'agenda)
3087 (org-let2 gprops lprops
3088 '(call-interactively 'org-agenda-list)))
3089 ((eq type 'agenda*)
3090 (org-let2 gprops lprops
3091 '(funcall 'org-agenda-list nil nil t)))
3092 ((eq type 'alltodo)
3093 (org-let2 gprops lprops
3094 '(call-interactively 'org-todo-list)))
3095 ((eq type 'search)
3096 (org-let2 gprops lprops
3097 '(org-search-view current-prefix-arg match nil)))
3098 ((eq type 'stuck)
3099 (org-let2 gprops lprops
3100 '(call-interactively 'org-agenda-list-stuck-projects)))
3101 ((eq type 'tags)
3102 (org-let2 gprops lprops
3103 '(org-tags-view current-prefix-arg match)))
3104 ((eq type 'tags-todo)
3105 (org-let2 gprops lprops
3106 '(org-tags-view '(4) match)))
3107 ((eq type 'todo)
3108 (org-let2 gprops lprops
3109 '(org-todo-list match)))
3110 ((fboundp type)
3111 (org-let2 gprops lprops
3112 '(funcall type match)))
3113 (t (error "Invalid type in command series")))))
3114 (widen)
3115 (let ((inhibit-read-only t))
3116 (add-text-properties (point-min) (point-max)
3117 `(org-series t org-series-redo-cmd ,redo)))
3118 (setq org-agenda-redo-command redo)
3119 (goto-char (point-min)))
3120 (org-agenda-fit-window-to-buffer)
3121 (org-let (nth 1 series) '(org-agenda-finalize)))
3123 ;;;###autoload
3124 (defmacro org-batch-agenda (cmd-key &rest parameters)
3125 "Run an agenda command in batch mode and send the result to STDOUT.
3126 If CMD-KEY is a string of length 1, it is used as a key in
3127 `org-agenda-custom-commands' and triggers this command. If it is a
3128 longer string it is used as a tags/todo match string.
3129 Parameters are alternating variable names and values that will be bound
3130 before running the agenda command."
3131 (org-eval-in-environment (org-make-parameter-alist parameters)
3132 (if (> (length cmd-key) 2)
3133 (org-tags-view nil cmd-key)
3134 (org-agenda nil cmd-key)))
3135 (set-buffer org-agenda-buffer-name)
3136 (princ (buffer-string)))
3138 (defvar org-agenda-info nil)
3140 ;;;###autoload
3141 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3142 "Run an agenda command in batch mode and send the result to STDOUT.
3143 If CMD-KEY is a string of length 1, it is used as a key in
3144 `org-agenda-custom-commands' and triggers this command. If it is a
3145 longer string it is used as a tags/todo match string.
3146 Parameters are alternating variable names and values that will be bound
3147 before running the agenda command.
3149 The output gives a line for each selected agenda item. Each
3150 item is a list of comma-separated values, like this:
3152 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3154 category The category of the item
3155 head The headline, without TODO kwd, TAGS and PRIORITY
3156 type The type of the agenda entry, can be
3157 todo selected in TODO match
3158 tagsmatch selected in tags match
3159 diary imported from diary
3160 deadline a deadline on given date
3161 scheduled scheduled on given date
3162 timestamp entry has timestamp on given date
3163 closed entry was closed on given date
3164 upcoming-deadline warning about deadline
3165 past-scheduled forwarded scheduled item
3166 block entry has date block including g. date
3167 todo The todo keyword, if any
3168 tags All tags including inherited ones, separated by colons
3169 date The relevant date, like 2007-2-14
3170 time The time, like 15:00-16:50
3171 extra Sting with extra planning info
3172 priority-l The priority letter if any was given
3173 priority-n The computed numerical priority
3174 agenda-day The day in the agenda where this is listed"
3175 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3176 (org-make-parameter-alist parameters))
3177 (if (> (length cmd-key) 2)
3178 (org-tags-view nil cmd-key)
3179 (org-agenda nil cmd-key)))
3180 (set-buffer org-agenda-buffer-name)
3181 (let* ((lines (org-split-string (buffer-string) "\n"))
3182 line)
3183 (while (setq line (pop lines))
3184 (catch 'next
3185 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3186 (setq org-agenda-info
3187 (org-fix-agenda-info (text-properties-at 0 line)))
3188 (princ
3189 (mapconcat 'org-agenda-export-csv-mapper
3190 '(org-category txt type todo tags date time extra
3191 priority-letter priority agenda-day)
3192 ","))
3193 (princ "\n")))))
3195 (defun org-fix-agenda-info (props)
3196 "Make sure all properties on an agenda item have a canonical form.
3197 This ensures the export commands can easily use it."
3198 (let (tmp re)
3199 (when (setq tmp (plist-get props 'tags))
3200 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3201 (when (setq tmp (plist-get props 'date))
3202 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3203 (let ((calendar-date-display-form '(year "-" month "-" day)))
3204 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3206 (setq tmp (calendar-date-string tmp)))
3207 (setq props (plist-put props 'date tmp)))
3208 (when (setq tmp (plist-get props 'day))
3209 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3210 (let ((calendar-date-display-form '(year "-" month "-" day)))
3211 (setq tmp (calendar-date-string tmp)))
3212 (setq props (plist-put props 'day tmp))
3213 (setq props (plist-put props 'agenda-day tmp)))
3214 (when (setq tmp (plist-get props 'txt))
3215 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3216 (plist-put props 'priority-letter (match-string 1 tmp))
3217 (setq tmp (replace-match "" t t tmp)))
3218 (when (and (setq re (plist-get props 'org-todo-regexp))
3219 (setq re (concat "\\`\\.*" re " ?"))
3220 (string-match re tmp))
3221 (plist-put props 'todo (match-string 1 tmp))
3222 (setq tmp (replace-match "" t t tmp)))
3223 (plist-put props 'txt tmp)))
3224 props)
3226 (defun org-agenda-export-csv-mapper (prop)
3227 (let ((res (plist-get org-agenda-info prop)))
3228 (setq res
3229 (cond
3230 ((not res) "")
3231 ((stringp res) res)
3232 (t (prin1-to-string res))))
3233 (while (string-match "," res)
3234 (setq res (replace-match ";" t t res)))
3235 (org-trim res)))
3237 ;;;###autoload
3238 (defun org-store-agenda-views (&rest parameters)
3239 "Store agenda views."
3240 (interactive)
3241 (eval (list 'org-batch-store-agenda-views)))
3243 ;;;###autoload
3244 (defmacro org-batch-store-agenda-views (&rest parameters)
3245 "Run all custom agenda commands that have a file argument."
3246 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3247 (pop-up-frames nil)
3248 (dir default-directory)
3249 (pars (org-make-parameter-alist parameters))
3250 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3251 (save-window-excursion
3252 (while cmds
3253 (setq cmd (pop cmds)
3254 thiscmdkey (car cmd)
3255 thiscmdcmd (cdr cmd)
3256 match (nth 2 thiscmdcmd)
3257 bufname (if org-agenda-sticky
3258 (or (and (stringp match)
3259 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3260 (format "*Org Agenda(%s)*" thiscmdkey))
3261 org-agenda-buffer-name)
3262 cmd-or-set (nth 2 cmd)
3263 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3264 files (nth (if (listp cmd-or-set) 4 5) cmd))
3265 (if (stringp files) (setq files (list files)))
3266 (when files
3267 (org-eval-in-environment (append org-agenda-exporter-settings
3268 opts pars)
3269 (org-agenda nil thiscmdkey))
3270 (set-buffer bufname)
3271 (while files
3272 (org-eval-in-environment (append org-agenda-exporter-settings
3273 opts pars)
3274 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3275 (and (get-buffer bufname)
3276 (kill-buffer bufname)))))))
3278 (defvar org-agenda-current-span nil
3279 "The current span used in the agenda view.") ; local variable in the agenda buffer
3280 (defun org-agenda-mark-header-line (pos)
3281 "Mark the line at POS as an agenda structure header."
3282 (save-excursion
3283 (goto-char pos)
3284 (put-text-property (point-at-bol) (point-at-eol)
3285 'org-agenda-structural-header t)
3286 (when org-agenda-title-append
3287 (put-text-property (point-at-bol) (point-at-eol)
3288 'org-agenda-title-append org-agenda-title-append))))
3290 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3291 (defvar org-agenda-write-buffer-name "Agenda View")
3292 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3293 "Write the current buffer (an agenda view) as a file.
3294 Depending on the extension of the file name, plain text (.txt),
3295 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3296 If the extension is .ics, run icalendar export over all files used
3297 to construct the agenda and limit the export to entries listed in the
3298 agenda now.
3299 If the extension is .org, collect all subtrees corresponding to the
3300 agenda entries and add them in an .org file.
3301 With prefix argument OPEN, open the new file immediately.
3302 If NOSETTINGS is given, do not scope the settings of
3303 `org-agenda-exporter-settings' into the export commands. This is used when
3304 the settings have already been scoped and we do not wish to overrule other,
3305 higher priority settings.
3306 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3307 (interactive "FWrite agenda to file: \nP")
3308 (if (or (not (file-writable-p file))
3309 (and (file-exists-p file)
3310 (if (called-interactively-p 'any)
3311 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3312 (user-error "Cannot write agenda to file %s" file))
3313 (org-let (if nosettings nil org-agenda-exporter-settings)
3314 '(save-excursion
3315 (save-window-excursion
3316 (org-agenda-mark-filtered-text)
3317 (let ((bs (copy-sequence (buffer-string))) beg content)
3318 (org-agenda-unmark-filtered-text)
3319 (with-temp-buffer
3320 (rename-buffer org-agenda-write-buffer-name t)
3321 (set-buffer-modified-p nil)
3322 (insert bs)
3323 (org-agenda-remove-marked-text 'org-filtered)
3324 (while (setq beg (text-property-any (point-min) (point-max)
3325 'org-filtered t))
3326 (delete-region
3327 beg (or (next-single-property-change beg 'org-filtered)
3328 (point-max))))
3329 (run-hooks 'org-agenda-before-write-hook)
3330 (cond
3331 ((org-bound-and-true-p org-mobile-creating-agendas)
3332 (org-mobile-write-agenda-for-mobile file))
3333 ((string-match "\\.org\\'" file)
3334 (let (content p m message-log-max)
3335 (goto-char (point-min))
3336 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3337 (goto-char p)
3338 (setq m (get-text-property (point) 'org-hd-marker))
3339 (when m
3340 (push (save-excursion
3341 (set-buffer (marker-buffer m))
3342 (goto-char m)
3343 (org-copy-subtree 1 nil t t)
3344 org-subtree-clip)
3345 content)))
3346 (find-file file)
3347 (erase-buffer)
3348 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3349 (write-file file)
3350 (kill-buffer (current-buffer))
3351 (message "Org file written to %s" file)))
3352 ((string-match "\\.html?\\'" file)
3353 (require 'htmlize)
3354 (set-buffer (htmlize-buffer (current-buffer)))
3355 (when org-agenda-export-html-style
3356 ;; replace <style> section with org-agenda-export-html-style
3357 (goto-char (point-min))
3358 (kill-region (- (search-forward "<style") 6)
3359 (search-forward "</style>"))
3360 (insert org-agenda-export-html-style))
3361 (write-file file)
3362 (kill-buffer (current-buffer))
3363 (message "HTML written to %s" file))
3364 ((string-match "\\.ps\\'" file)
3365 (require 'ps-print)
3366 (ps-print-buffer-with-faces file)
3367 (message "Postscript written to %s" file))
3368 ((string-match "\\.pdf\\'" file)
3369 (require 'ps-print)
3370 (ps-print-buffer-with-faces
3371 (concat (file-name-sans-extension file) ".ps"))
3372 (call-process "ps2pdf" nil nil nil
3373 (expand-file-name
3374 (concat (file-name-sans-extension file) ".ps"))
3375 (expand-file-name file))
3376 (delete-file (concat (file-name-sans-extension file) ".ps"))
3377 (message "PDF written to %s" file))
3378 ((string-match "\\.ics\\'" file)
3379 (require 'ox-icalendar)
3380 (org-icalendar-export-current-agenda (expand-file-name file)))
3382 (let ((bs (buffer-string)))
3383 (find-file file)
3384 (erase-buffer)
3385 (insert bs)
3386 (save-buffer 0)
3387 (kill-buffer (current-buffer))
3388 (message "Plain text written to %s" file))))))))
3389 (set-buffer (or agenda-bufname
3390 (and (org-called-interactively-p 'any) (buffer-name))
3391 org-agenda-buffer-name)))
3392 (when open (org-open-file file)))
3394 (defvar org-agenda-tag-filter-overlays nil)
3395 (defvar org-agenda-cat-filter-overlays nil)
3396 (defvar org-agenda-re-filter-overlays nil)
3398 (defun org-agenda-mark-filtered-text ()
3399 "Mark all text hidden by filtering with a text property."
3400 (let ((inhibit-read-only t))
3401 (mapc
3402 (lambda (o)
3403 (when (equal (overlay-buffer o) (current-buffer))
3404 (put-text-property
3405 (overlay-start o) (overlay-end o)
3406 'org-filtered t)))
3407 (append org-agenda-tag-filter-overlays
3408 org-agenda-cat-filter-overlays
3409 org-agenda-re-filter-overlays))))
3411 (defun org-agenda-unmark-filtered-text ()
3412 "Remove the filtering text property."
3413 (let ((inhibit-read-only t))
3414 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3416 (defun org-agenda-remove-marked-text (property &optional value)
3417 "Delete all text marked with VALUE of PROPERTY.
3418 VALUE defaults to t."
3419 (let (beg)
3420 (setq value (or value t))
3421 (while (setq beg (text-property-any (point-min) (point-max)
3422 property value))
3423 (delete-region
3424 beg (or (next-single-property-change beg 'org-filtered)
3425 (point-max))))))
3427 (defun org-agenda-add-entry-text ()
3428 "Add entry text to agenda lines.
3429 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3430 entry text following headings shown in the agenda.
3431 Drawers will be excluded, also the line with scheduling/deadline info."
3432 (when (and (> org-agenda-add-entry-text-maxlines 0)
3433 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3434 (let (m txt)
3435 (goto-char (point-min))
3436 (while (not (eobp))
3437 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3438 (beginning-of-line 2)
3439 (setq txt (org-agenda-get-some-entry-text
3440 m org-agenda-add-entry-text-maxlines " > "))
3441 (end-of-line 1)
3442 (if (string-match "\\S-" txt)
3443 (insert "\n" txt)
3444 (or (eobp) (forward-char 1))))))))
3446 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3447 &rest keep)
3448 "Extract entry text from MARKER, at most N-LINES lines.
3449 This will ignore drawers etc, just get the text.
3450 If INDENT is given, prefix every line with this string. If KEEP is
3451 given, it is a list of symbols, defining stuff that should not be
3452 removed from the entry content. Currently only `planning' is allowed here."
3453 (let (txt drawer-re kwd-time-re ind)
3454 (save-excursion
3455 (with-current-buffer (marker-buffer marker)
3456 (if (not (derived-mode-p 'org-mode))
3457 (setq txt "")
3458 (save-excursion
3459 (save-restriction
3460 (widen)
3461 (goto-char marker)
3462 (end-of-line 1)
3463 (setq txt (buffer-substring
3464 (min (1+ (point)) (point-max))
3465 (progn (outline-next-heading) (point)))
3466 drawer-re org-drawer-regexp
3467 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3468 ".*\n?"))
3469 (with-temp-buffer
3470 (insert txt)
3471 (when org-agenda-add-entry-text-descriptive-links
3472 (goto-char (point-min))
3473 (while (org-activate-bracket-links (point-max))
3474 (add-text-properties (match-beginning 0) (match-end 0)
3475 '(face org-link))))
3476 (goto-char (point-min))
3477 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3478 (set-text-properties (match-beginning 0) (match-end 0)
3479 nil))
3480 (goto-char (point-min))
3481 (while (re-search-forward drawer-re nil t)
3482 (delete-region
3483 (match-beginning 0)
3484 (progn (re-search-forward
3485 "^[ \t]*:END:.*\n?" nil 'move)
3486 (point))))
3487 (unless (member 'planning keep)
3488 (goto-char (point-min))
3489 (while (re-search-forward kwd-time-re nil t)
3490 (replace-match "")))
3491 (goto-char (point-min))
3492 (when org-agenda-entry-text-exclude-regexps
3493 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3494 (while (setq re (pop re-list))
3495 (goto-char (point-min))
3496 (while (re-search-forward re nil t)
3497 (replace-match "")))))
3498 (goto-char (point-max))
3499 (skip-chars-backward " \t\n")
3500 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3502 ;; find and remove min common indentation
3503 (goto-char (point-min))
3504 (untabify (point-min) (point-max))
3505 (setq ind (org-get-indentation))
3506 (while (not (eobp))
3507 (unless (looking-at "[ \t]*$")
3508 (setq ind (min ind (org-get-indentation))))
3509 (beginning-of-line 2))
3510 (goto-char (point-min))
3511 (while (not (eobp))
3512 (unless (looking-at "[ \t]*$")
3513 (move-to-column ind)
3514 (delete-region (point-at-bol) (point)))
3515 (beginning-of-line 2))
3517 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3519 (goto-char (point-min))
3520 (when indent
3521 (while (and (not (eobp)) (re-search-forward "^" nil t))
3522 (replace-match indent t t)))
3523 (goto-char (point-min))
3524 (while (looking-at "[ \t]*\n") (replace-match ""))
3525 (goto-char (point-max))
3526 (when (> (org-current-line)
3527 n-lines)
3528 (org-goto-line (1+ n-lines))
3529 (backward-char 1))
3530 (setq txt (buffer-substring (point-min) (point)))))))))
3531 txt))
3533 (defun org-check-for-org-mode ()
3534 "Make sure current buffer is in org-mode. Error if not."
3535 (or (derived-mode-p 'org-mode)
3536 (error "Cannot execute org-mode agenda command on buffer in %s"
3537 major-mode)))
3539 ;;; Agenda prepare and finalize
3541 (defvar org-agenda-multi nil) ; dynamically scoped
3542 (defvar org-agenda-pre-window-conf nil)
3543 (defvar org-agenda-columns-active nil)
3544 (defvar org-agenda-name nil)
3545 (defvar org-agenda-tag-filter nil)
3546 (defvar org-agenda-category-filter nil)
3547 (defvar org-agenda-regexp-filter nil)
3548 (defvar org-agenda-top-headline-filter nil)
3549 (defvar org-agenda-tag-filter-while-redo nil)
3550 (defvar org-agenda-tag-filter-preset nil
3551 "A preset of the tags filter used for secondary agenda filtering.
3552 This must be a list of strings, each string must be a single tag preceded
3553 by \"+\" or \"-\".
3554 This variable should not be set directly, but agenda custom commands can
3555 bind it in the options section. The preset filter is a global property of
3556 the entire agenda view. In a block agenda, it will not work reliably to
3557 define a filter for one of the individual blocks. You need to set it in
3558 the global options and expect it to be applied to the entire view.")
3560 (defvar org-agenda-category-filter-preset nil
3561 "A preset of the category filter used for secondary agenda filtering.
3562 This must be a list of strings, each string must be a single category
3563 preceded by \"+\" or \"-\".
3564 This variable should not be set directly, but agenda custom commands can
3565 bind it in the options section. The preset filter is a global property of
3566 the entire agenda view. In a block agenda, it will not work reliably to
3567 define a filter for one of the individual blocks. You need to set it in
3568 the global options and expect it to be applied to the entire view.")
3570 (defvar org-agenda-regexp-filter-preset nil
3571 "A preset of the regexp filter used for secondary agenda filtering.
3572 This must be a list of strings, each string must be a single category
3573 preceded by \"+\" or \"-\".
3574 This variable should not be set directly, but agenda custom commands can
3575 bind it in the options section. The preset filter is a global property of
3576 the entire agenda view. In a block agenda, it will not work reliably to
3577 define a filter for one of the individual blocks. You need to set it in
3578 the global options and expect it to be applied to the entire view.")
3580 (defun org-agenda-use-sticky-p ()
3581 "Return non-nil if an agenda buffer named
3582 `org-agenda-buffer-name' exists and should be shown instead of
3583 generating a new one."
3584 (and
3585 ;; turned off by user
3586 org-agenda-sticky
3587 ;; For multi-agenda buffer already exists
3588 (not org-agenda-multi)
3589 ;; buffer found
3590 (get-buffer org-agenda-buffer-name)
3591 ;; C-u parameter is same as last call
3592 (with-current-buffer (get-buffer org-agenda-buffer-name)
3593 (and
3594 (equal current-prefix-arg
3595 org-agenda-last-prefix-arg)
3596 ;; In case user turned stickiness on, while having existing
3597 ;; Agenda buffer active, don't reuse that buffer, because it
3598 ;; does not have org variables local
3599 org-agenda-this-buffer-is-sticky))))
3601 (defun org-agenda-prepare-window (abuf)
3602 "Setup agenda buffer in the window."
3603 (let* ((awin (get-buffer-window abuf))
3604 wconf)
3605 (cond
3606 ((equal (current-buffer) abuf) nil)
3607 (awin (select-window awin))
3608 ((not (setq wconf (current-window-configuration))))
3609 ((equal org-agenda-window-setup 'current-window)
3610 (org-pop-to-buffer-same-window abuf))
3611 ((equal org-agenda-window-setup 'other-window)
3612 (org-switch-to-buffer-other-window abuf))
3613 ((equal org-agenda-window-setup 'other-frame)
3614 (switch-to-buffer-other-frame abuf))
3615 ((equal org-agenda-window-setup 'reorganize-frame)
3616 (delete-other-windows)
3617 (org-switch-to-buffer-other-window abuf)))
3618 ;; additional test in case agenda is invoked from within agenda
3619 ;; buffer via elisp link
3620 (unless (equal (current-buffer) abuf)
3621 (org-pop-to-buffer-same-window abuf))
3622 (setq org-agenda-pre-window-conf
3623 (or org-agenda-pre-window-conf wconf))))
3625 (defun org-agenda-prepare (&optional name)
3626 (if (org-agenda-use-sticky-p)
3627 (progn
3628 ;; Popup existing buffer
3629 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3630 (message "Sticky Agenda buffer, use `r' to refresh")
3631 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3632 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3633 (setq org-todo-keywords-for-agenda nil)
3634 (setq org-drawers-for-agenda nil)
3635 (unless org-agenda-persistent-filter
3636 (setq org-agenda-tag-filter nil
3637 org-agenda-category-filter nil
3638 org-agenda-regexp-filter nil))
3639 (put 'org-agenda-tag-filter :preset-filter
3640 org-agenda-tag-filter-preset)
3641 (put 'org-agenda-category-filter :preset-filter
3642 org-agenda-category-filter-preset)
3643 (put 'org-agenda-regexp-filter :preset-filter
3644 org-agenda-regexp-filter-preset)
3645 (if org-agenda-multi
3646 (progn
3647 (setq buffer-read-only nil)
3648 (goto-char (point-max))
3649 (unless (or (bobp) org-agenda-compact-blocks
3650 (not org-agenda-block-separator))
3651 (insert "\n"
3652 (if (stringp org-agenda-block-separator)
3653 org-agenda-block-separator
3654 (make-string (window-width) org-agenda-block-separator))
3655 "\n"))
3656 (narrow-to-region (point) (point-max)))
3657 (setq org-done-keywords-for-agenda nil)
3659 ;; Setting any org variables that are in org-agenda-local-vars
3660 ;; list need to be done after the prepare call
3661 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3662 (setq buffer-read-only nil)
3663 (org-agenda-reset-markers)
3664 (let ((inhibit-read-only t)) (erase-buffer))
3665 (org-agenda-mode)
3666 (setq org-agenda-buffer (current-buffer))
3667 (setq org-agenda-contributing-files nil)
3668 (setq org-agenda-columns-active nil)
3669 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3670 (setq org-todo-keywords-for-agenda
3671 (org-uniquify org-todo-keywords-for-agenda))
3672 (setq org-done-keywords-for-agenda
3673 (org-uniquify org-done-keywords-for-agenda))
3674 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3675 (setq org-agenda-last-prefix-arg current-prefix-arg)
3676 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3677 (and name (not org-agenda-name)
3678 (org-set-local 'org-agenda-name name)))
3679 (setq buffer-read-only nil)))
3681 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3682 (defun org-agenda-finalize ()
3683 "Finishing touch for the agenda buffer, called just before displaying it."
3684 (unless org-agenda-multi
3685 (save-excursion
3686 (let ((inhibit-read-only t))
3687 (goto-char (point-min))
3688 (save-excursion
3689 (while (org-activate-bracket-links (point-max))
3690 (add-text-properties (match-beginning 0) (match-end 0)
3691 '(face org-link))))
3692 (save-excursion
3693 (while (org-activate-plain-links (point-max))
3694 (add-text-properties (match-beginning 0) (match-end 0)
3695 '(face org-link))))
3696 (unless (eq org-agenda-remove-tags t)
3697 (org-agenda-align-tags))
3698 (unless org-agenda-with-colors
3699 (remove-text-properties (point-min) (point-max) '(face nil)))
3700 (if (and (boundp 'org-agenda-overriding-columns-format)
3701 org-agenda-overriding-columns-format)
3702 (org-set-local 'org-agenda-overriding-columns-format
3703 org-agenda-overriding-columns-format))
3704 (if (and (boundp 'org-agenda-view-columns-initially)
3705 org-agenda-view-columns-initially)
3706 (org-agenda-columns))
3707 (when org-agenda-fontify-priorities
3708 (org-agenda-fontify-priorities))
3709 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3710 (org-agenda-dim-blocked-tasks))
3711 ;; We need to widen when `org-agenda-finalize' is called from
3712 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3713 (when org-clock-current-task
3714 (save-restriction
3715 (widen)
3716 (org-agenda-mark-clocking-task)))
3717 (when org-agenda-entry-text-mode
3718 (org-agenda-entry-text-hide)
3719 (org-agenda-entry-text-show))
3720 (if (and (functionp 'org-habit-insert-consistency-graphs)
3721 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3722 (org-habit-insert-consistency-graphs))
3723 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3724 (unless (or (eq org-agenda-show-inherited-tags 'always)
3725 (and (listp org-agenda-show-inherited-tags)
3726 (memq org-agenda-type org-agenda-show-inherited-tags))
3727 (and (eq org-agenda-show-inherited-tags t)
3728 (or (eq org-agenda-use-tag-inheritance t)
3729 (and (listp org-agenda-use-tag-inheritance)
3730 (not (memq org-agenda-type
3731 org-agenda-use-tag-inheritance))))))
3732 (let (mrk)
3733 (save-excursion
3734 (goto-char (point-min))
3735 (while (equal (forward-line) 0)
3736 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3737 (get-text-property (point) 'org-hd-marker)))
3738 (put-text-property (point-at-bol) (point-at-eol)
3739 'tags (org-with-point-at mrk
3740 (delete-dups
3741 (mapcar 'downcase (org-get-tags-at))))))))))
3742 (run-hooks 'org-agenda-finalize-hook)
3743 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3744 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3745 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3746 (org-agenda-filter-apply org-agenda-category-filter 'category))
3747 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3748 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3749 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3751 (defun org-agenda-mark-clocking-task ()
3752 "Mark the current clock entry in the agenda if it is present."
3753 (org-agenda-unmark-clocking-task)
3754 (when (marker-buffer org-clock-hd-marker)
3755 (save-excursion
3756 (goto-char (point-min))
3757 (let (s ov)
3758 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3759 (goto-char s)
3760 (when (equal (org-get-at-bol 'org-hd-marker)
3761 org-clock-hd-marker)
3762 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3763 (overlay-put ov 'type 'org-agenda-clocking)
3764 (overlay-put ov 'face 'org-agenda-clocking)
3765 (overlay-put ov 'help-echo
3766 "The clock is running in this item")))))))
3768 (defun org-agenda-unmark-clocking-task ()
3769 "Unmark the current clocking task."
3770 (mapc (lambda (o)
3771 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3772 (delete-overlay o)))
3773 (overlays-in (point-min) (point-max))))
3775 (defun org-agenda-fontify-priorities ()
3776 "Make highest priority lines bold, and lowest italic."
3777 (interactive)
3778 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3779 (delete-overlay o)))
3780 (overlays-in (point-min) (point-max)))
3781 (save-excursion
3782 (let (b e p ov h l)
3783 (goto-char (point-min))
3784 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3785 (setq h (or (get-char-property (point) 'org-highest-priority)
3786 org-highest-priority)
3787 l (or (get-char-property (point) 'org-lowest-priority)
3788 org-lowest-priority)
3789 p (string-to-char (match-string 1))
3790 b (match-beginning 0)
3791 e (if (eq org-agenda-fontify-priorities 'cookies)
3792 (match-end 0)
3793 (point-at-eol))
3794 ov (make-overlay b e))
3795 (overlay-put
3796 ov 'face
3797 (cons (cond ((org-face-from-face-or-color
3798 'priority nil
3799 (cdr (assoc p org-priority-faces))))
3800 ((and (listp org-agenda-fontify-priorities)
3801 (org-face-from-face-or-color
3802 'priority nil
3803 (cdr (assoc p org-agenda-fontify-priorities)))))
3804 ((equal p l) 'italic)
3805 ((equal p h) 'bold))
3806 'org-priority))
3807 (overlay-put ov 'org-type 'org-priority)))))
3809 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3810 "Dim currently blocked TODO's in the agenda display.
3811 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3812 dimming them."
3813 (interactive "P")
3814 (when (org-called-interactively-p 'interactive)
3815 (message "Dim or hide blocked tasks..."))
3816 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3817 (delete-overlay o)))
3818 (overlays-in (point-min) (point-max)))
3819 (save-excursion
3820 (let ((inhibit-read-only t)
3821 (org-depend-tag-blocked nil)
3822 (invis (or (not (null invisible))
3823 (eq org-agenda-dim-blocked-tasks 'invisible)))
3824 org-blocked-by-checkboxes
3825 invis1 b e p ov h l)
3826 (goto-char (point-min))
3827 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3828 (and pos (goto-char (1+ pos))))
3829 (setq org-blocked-by-checkboxes nil invis1 invis)
3830 (let ((marker (org-get-at-bol 'org-hd-marker)))
3831 (when (and marker
3832 (with-current-buffer (marker-buffer marker)
3833 (save-excursion (goto-char marker)
3834 (org-entry-blocked-p))))
3835 (if org-blocked-by-checkboxes (setq invis1 nil))
3836 (setq b (if invis1
3837 (max (point-min) (1- (point-at-bol)))
3838 (point-at-bol))
3839 e (point-at-eol)
3840 ov (make-overlay b e))
3841 (if invis1
3842 (overlay-put ov 'invisible t)
3843 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3844 (overlay-put ov 'org-type 'org-blocked-todo))))))
3845 (when (org-called-interactively-p 'interactive)
3846 (message "Dim or hide blocked tasks...done")))
3848 (defvar org-agenda-skip-function nil
3849 "Function to be called at each match during agenda construction.
3850 If this function returns nil, the current match should not be skipped.
3851 Otherwise, the function must return a position from where the search
3852 should be continued.
3853 This may also be a Lisp form, it will be evaluated.
3854 Never set this variable using `setq' or so, because then it will apply
3855 to all future agenda commands. If you do want a global skipping condition,
3856 use the option `org-agenda-skip-function-global' instead.
3857 The correct usage for `org-agenda-skip-function' is to bind it with
3858 `let' to scope it dynamically into the agenda-constructing command.
3859 A good way to set it is through options in `org-agenda-custom-commands'.")
3861 (defun org-agenda-skip ()
3862 "Throw to `:skip' in places that should be skipped.
3863 Also moves point to the end of the skipped region, so that search can
3864 continue from there."
3865 (let ((p (point-at-bol)) to)
3866 (when (or
3867 (save-excursion (goto-char p) (looking-at comment-start-skip))
3868 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3869 (get-text-property p :org-archived)
3870 (org-end-of-subtree t))
3871 (and org-agenda-skip-comment-trees
3872 (get-text-property p :org-comment)
3873 (org-end-of-subtree t))
3874 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3875 (org-agenda-skip-eval org-agenda-skip-function)))
3876 (goto-char to))
3877 (org-in-src-block-p t))
3878 (throw :skip t))))
3880 (defun org-agenda-skip-eval (form)
3881 "If FORM is a function or a list, call (or eval) is and return result.
3882 `save-excursion' and `save-match-data' are wrapped around the call, so point
3883 and match data are returned to the previous state no matter what these
3884 functions do."
3885 (let (fp)
3886 (and form
3887 (or (setq fp (functionp form))
3888 (consp form))
3889 (save-excursion
3890 (save-match-data
3891 (if fp
3892 (funcall form)
3893 (eval form)))))))
3895 (defvar org-agenda-markers nil
3896 "List of all currently active markers created by `org-agenda'.")
3897 (defvar org-agenda-last-marker-time (org-float-time)
3898 "Creation time of the last agenda marker.")
3900 (defun org-agenda-new-marker (&optional pos)
3901 "Return a new agenda marker.
3902 Org-mode keeps a list of these markers and resets them when they are
3903 no longer in use."
3904 (let ((m (copy-marker (or pos (point)))))
3905 (setq org-agenda-last-marker-time (org-float-time))
3906 (if org-agenda-buffer
3907 (with-current-buffer org-agenda-buffer
3908 (push m org-agenda-markers))
3909 (push m org-agenda-markers))
3912 (defun org-agenda-reset-markers ()
3913 "Reset markers created by `org-agenda'."
3914 (while org-agenda-markers
3915 (move-marker (pop org-agenda-markers) nil)))
3917 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3918 "Save relative positions of markers in region.
3919 This check for agenda markers in all agenda buffers currently active."
3920 (dolist (buf (buffer-list))
3921 (with-current-buffer buf
3922 (when (eq major-mode 'org-agenda-mode)
3923 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3924 org-agenda-markers)))))
3926 ;;; Entry text mode
3928 (defun org-agenda-entry-text-show-here ()
3929 "Add some text from the entry as context to the current line."
3930 (let (m txt o)
3931 (setq m (org-get-at-bol 'org-hd-marker))
3932 (unless (marker-buffer m)
3933 (error "No marker points to an entry here"))
3934 (setq txt (concat "\n" (org-no-properties
3935 (org-agenda-get-some-entry-text
3936 m org-agenda-entry-text-maxlines
3937 org-agenda-entry-text-leaders))))
3938 (when (string-match "\\S-" txt)
3939 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3940 (overlay-put o 'evaporate t)
3941 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3942 (overlay-put o 'after-string txt))))
3944 (defun org-agenda-entry-text-show ()
3945 "Add entry context for all agenda lines."
3946 (interactive)
3947 (save-excursion
3948 (goto-char (point-max))
3949 (beginning-of-line 1)
3950 (while (not (bobp))
3951 (when (org-get-at-bol 'org-hd-marker)
3952 (org-agenda-entry-text-show-here))
3953 (beginning-of-line 0))))
3955 (defun org-agenda-entry-text-hide ()
3956 "Remove any shown entry context."
3957 (delq nil
3958 (mapcar (lambda (o)
3959 (if (eq (overlay-get o 'org-overlay-type)
3960 'agenda-entry-content)
3961 (progn (delete-overlay o) t)))
3962 (overlays-in (point-min) (point-max)))))
3964 (defun org-agenda-get-day-face (date)
3965 "Return the face DATE should be displayed with."
3966 (or (and (functionp org-agenda-day-face-function)
3967 (funcall org-agenda-day-face-function date))
3968 (cond ((org-agenda-todayp date)
3969 'org-agenda-date-today)
3970 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3971 'org-agenda-date-weekend)
3972 (t 'org-agenda-date))))
3974 ;;; Agenda timeline
3976 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3978 (defun org-timeline (&optional dotodo)
3979 "Show a time-sorted view of the entries in the current org file.
3980 Only entries with a time stamp of today or later will be listed. With
3981 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3982 under the current date.
3983 If the buffer contains an active region, only check the region for
3984 dates."
3985 (interactive "P")
3986 (let* ((dopast t)
3987 (org-agenda-show-log-scoped org-agenda-show-log)
3988 (org-agenda-show-log org-agenda-show-log-scoped)
3989 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3990 (current-buffer))))
3991 (date (calendar-current-date))
3992 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3993 (end (if (org-region-active-p) (region-end) (point-max)))
3994 (day-numbers (org-get-all-dates
3995 beg end 'no-ranges
3996 t org-agenda-show-log-scoped ; always include today
3997 org-timeline-show-empty-dates))
3998 (org-deadline-warning-days 0)
3999 (org-agenda-only-exact-dates t)
4000 (today (org-today))
4001 (past t)
4002 args
4003 s e rtn d emptyp)
4004 (setq org-agenda-redo-command
4005 (list 'let
4006 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4007 (list 'org-switch-to-buffer-other-window (current-buffer))
4008 (list 'org-timeline (list 'quote dotodo))))
4009 (put 'org-agenda-redo-command 'org-lprops nil)
4010 (if (not dopast)
4011 ;; Remove past dates from the list of dates.
4012 (setq day-numbers (delq nil (mapcar (lambda(x)
4013 (if (>= x today) x nil))
4014 day-numbers))))
4015 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4016 (org-compile-prefix-format 'timeline)
4017 (org-set-sorting-strategy 'timeline)
4018 (if org-agenda-show-log-scoped (push :closed args))
4019 (push :timestamp args)
4020 (push :deadline args)
4021 (push :scheduled args)
4022 (push :sexp args)
4023 (if dotodo (push :todo args))
4024 (insert "Timeline of file " entry "\n")
4025 (add-text-properties (point-min) (point)
4026 (list 'face 'org-agenda-structure))
4027 (org-agenda-mark-header-line (point-min))
4028 (while (setq d (pop day-numbers))
4029 (if (and (listp d) (eq (car d) :omitted))
4030 (progn
4031 (setq s (point))
4032 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4033 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4034 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4035 (if (and (>= d today)
4036 dopast
4037 past)
4038 (progn
4039 (setq past nil)
4040 (insert (make-string 79 ?-) "\n")))
4041 (setq date (calendar-gregorian-from-absolute d))
4042 (setq s (point))
4043 (setq rtn (and (not emptyp)
4044 (apply 'org-agenda-get-day-entries entry
4045 date args)))
4046 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4047 (progn
4048 (insert
4049 (if (stringp org-agenda-format-date)
4050 (format-time-string org-agenda-format-date
4051 (org-time-from-absolute date))
4052 (funcall org-agenda-format-date date))
4053 "\n")
4054 (put-text-property s (1- (point)) 'face
4055 (org-agenda-get-day-face date))
4056 (put-text-property s (1- (point)) 'org-date-line t)
4057 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4058 (if (equal d today)
4059 (put-text-property s (1- (point)) 'org-today t))
4060 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4061 (put-text-property s (1- (point)) 'day d)))))
4062 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4063 (point-min)))
4064 (add-text-properties
4065 (point-min) (point-max)
4066 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4067 (org-agenda-finalize)
4068 (setq buffer-read-only t)))
4070 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4071 "Return a list of all relevant day numbers from BEG to END buffer positions.
4072 If NO-RANGES is non-nil, include only the start and end dates of a range,
4073 not every single day in the range. If FORCE-TODAY is non-nil, make
4074 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4075 inactive time stamps (those in square brackets) are included.
4076 When EMPTY is non-nil, also include days without any entries."
4077 (let ((re (concat
4078 (if pre-re pre-re "")
4079 (if inactive org-ts-regexp-both org-ts-regexp)))
4080 dates dates1 date day day1 day2 ts1 ts2 pos)
4081 (if force-today
4082 (setq dates (list (org-today))))
4083 (save-excursion
4084 (goto-char beg)
4085 (while (re-search-forward re end t)
4086 (setq day (time-to-days (org-time-string-to-time
4087 (substring (match-string 1) 0 10)
4088 (current-buffer) (match-beginning 0))))
4089 (or (memq day dates) (push day dates)))
4090 (unless no-ranges
4091 (goto-char beg)
4092 (while (re-search-forward org-tr-regexp end t)
4093 (setq pos (match-beginning 0))
4094 (setq ts1 (substring (match-string 1) 0 10)
4095 ts2 (substring (match-string 2) 0 10)
4096 day1 (time-to-days (org-time-string-to-time
4097 ts1 (current-buffer) pos))
4098 day2 (time-to-days (org-time-string-to-time
4099 ts2 (current-buffer) pos)))
4100 (while (< (setq day1 (1+ day1)) day2)
4101 (or (memq day1 dates) (push day1 dates)))))
4102 (setq dates (sort dates '<))
4103 (when empty
4104 (while (setq day (pop dates))
4105 (setq day2 (car dates))
4106 (push day dates1)
4107 (when (and day2 empty)
4108 (if (or (eq empty t)
4109 (and (numberp empty) (<= (- day2 day) empty)))
4110 (while (< (setq day (1+ day)) day2)
4111 (push (list day) dates1))
4112 (push (cons :omitted (- day2 day)) dates1))))
4113 (setq dates (nreverse dates1)))
4114 dates)))
4116 ;;; Agenda Daily/Weekly
4118 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4119 "Start day for the agenda view.
4120 Custom commands can set this variable in the options section.
4121 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4122 input allowed when reading a date through the Org calendar.
4123 See the docstring of `org-read-date' for details.")
4124 (defvar org-starting-day nil) ; local variable in the agenda buffer
4125 (defvar org-arg-loc nil) ; local variable
4127 (defvar org-agenda-buffer-tmp-name nil)
4128 ;;;###autoload
4129 (defun org-agenda-list (&optional arg start-day span with-hour)
4130 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4131 The view will be for the current day or week, but from the overview buffer
4132 you will be able to go to other days/weeks.
4134 With a numeric prefix argument in an interactive call, the agenda will
4135 span ARG days. Lisp programs should instead specify SPAN to change
4136 the number of days. SPAN defaults to `org-agenda-span'.
4138 START-DAY defaults to TODAY, or to the most recent match for the weekday
4139 given in `org-agenda-start-on-weekday'.
4141 When WITH-HOUR is non-nil, only include scheduled and deadline
4142 items if they have an hour specification like [h]h:mm."
4143 (interactive "P")
4144 (if org-agenda-overriding-arguments
4145 (setq arg (car org-agenda-overriding-arguments)
4146 start-day (nth 1 org-agenda-overriding-arguments)
4147 span (nth 2 org-agenda-overriding-arguments)))
4148 (if (and (integerp arg) (> arg 0))
4149 (setq span arg arg nil))
4150 (catch 'exit
4151 (setq org-agenda-buffer-name
4152 (or org-agenda-buffer-tmp-name
4153 (if org-agenda-sticky
4154 (cond ((and org-keys (stringp org-match))
4155 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4156 (org-keys
4157 (format "*Org Agenda(%s)*" org-keys))
4158 (t "*Org Agenda(a)*")))
4159 org-agenda-buffer-name))
4160 (org-agenda-prepare "Day/Week")
4161 (setq start-day (or start-day org-agenda-start-day))
4162 (if (stringp start-day)
4163 ;; Convert to an absolute day number
4164 (setq start-day (time-to-days (org-read-date nil t start-day))))
4165 (org-compile-prefix-format 'agenda)
4166 (org-set-sorting-strategy 'agenda)
4167 (let* ((span (org-agenda-ndays-to-span
4168 (or span org-agenda-ndays org-agenda-span)))
4169 (today (org-today))
4170 (sd (or start-day today))
4171 (ndays (org-agenda-span-to-ndays span sd))
4172 (org-agenda-start-on-weekday
4173 (if (eq ndays 7)
4174 org-agenda-start-on-weekday))
4175 (thefiles (org-agenda-files nil 'ifmode))
4176 (files thefiles)
4177 (start (if (or (null org-agenda-start-on-weekday)
4178 (< ndays 7))
4180 (let* ((nt (calendar-day-of-week
4181 (calendar-gregorian-from-absolute sd)))
4182 (n1 org-agenda-start-on-weekday)
4183 (d (- nt n1)))
4184 (- sd (+ (if (< d 0) 7 0) d)))))
4185 (day-numbers (list start))
4186 (day-cnt 0)
4187 (inhibit-redisplay (not debug-on-error))
4188 (org-agenda-show-log-scoped org-agenda-show-log)
4189 s e rtn rtnall file date d start-pos end-pos todayp
4190 clocktable-start clocktable-end filter)
4191 (setq org-agenda-redo-command
4192 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4193 (dotimes (n (1- ndays))
4194 (push (1+ (car day-numbers)) day-numbers))
4195 (setq day-numbers (nreverse day-numbers))
4196 (setq clocktable-start (car day-numbers)
4197 clocktable-end (1+ (or (org-last day-numbers) 0)))
4198 (org-set-local 'org-starting-day (car day-numbers))
4199 (org-set-local 'org-arg-loc arg)
4200 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4201 (unless org-agenda-compact-blocks
4202 (let* ((d1 (car day-numbers))
4203 (d2 (org-last day-numbers))
4204 (w1 (org-days-to-iso-week d1))
4205 (w2 (org-days-to-iso-week d2)))
4206 (setq s (point))
4207 (if org-agenda-overriding-header
4208 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4209 nil 'face 'org-agenda-structure) "\n")
4210 (insert (org-agenda-span-name span)
4211 "-agenda"
4212 (if (< (- d2 d1) 350)
4213 (if (= w1 w2)
4214 (format " (W%02d)" w1)
4215 (format " (W%02d-W%02d)" w1 w2))
4217 ":\n")))
4218 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4219 'org-date-line t))
4220 (org-agenda-mark-header-line s))
4221 (while (setq d (pop day-numbers))
4222 (setq date (calendar-gregorian-from-absolute d)
4223 s (point))
4224 (if (or (setq todayp (= d today))
4225 (and (not start-pos) (= d sd)))
4226 (setq start-pos (point))
4227 (if (and start-pos (not end-pos))
4228 (setq end-pos (point))))
4229 (setq files thefiles
4230 rtnall nil)
4231 (while (setq file (pop files))
4232 (catch 'nextfile
4233 (org-check-agenda-file file)
4234 (let ((org-agenda-entry-types org-agenda-entry-types))
4235 ;; Starred types override non-starred equivalents
4236 (when (member :deadline* org-agenda-entry-types)
4237 (setq org-agenda-entry-types
4238 (delq :deadline org-agenda-entry-types)))
4239 (when (member :scheduled* org-agenda-entry-types)
4240 (setq org-agenda-entry-types
4241 (delq :scheduled org-agenda-entry-types)))
4242 ;; Honor with-hour
4243 (when with-hour
4244 (when (member :deadline org-agenda-entry-types)
4245 (setq org-agenda-entry-types
4246 (delq :deadline org-agenda-entry-types))
4247 (push :deadline* org-agenda-entry-types))
4248 (when (member :scheduled org-agenda-entry-types)
4249 (setq org-agenda-entry-types
4250 (delq :scheduled org-agenda-entry-types))
4251 (push :scheduled* org-agenda-entry-types)))
4252 (unless org-agenda-include-deadlines
4253 (setq org-agenda-entry-types
4254 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4255 (cond
4256 ((memq org-agenda-show-log-scoped '(only clockcheck))
4257 (setq rtn (org-agenda-get-day-entries
4258 file date :closed)))
4259 (org-agenda-show-log-scoped
4260 (setq rtn (apply 'org-agenda-get-day-entries
4261 file date
4262 (append '(:closed) org-agenda-entry-types))))
4264 (setq rtn (apply 'org-agenda-get-day-entries
4265 file date
4266 org-agenda-entry-types)))))
4267 (setq rtnall (append rtnall rtn)))) ;; all entries
4268 (if org-agenda-include-diary
4269 (let ((org-agenda-search-headline-for-time t))
4270 (require 'diary-lib)
4271 (setq rtn (org-get-entries-from-diary date))
4272 (setq rtnall (append rtnall rtn))))
4273 (if (or rtnall org-agenda-show-all-dates)
4274 (progn
4275 (setq day-cnt (1+ day-cnt))
4276 (insert
4277 (if (stringp org-agenda-format-date)
4278 (format-time-string org-agenda-format-date
4279 (org-time-from-absolute date))
4280 (funcall org-agenda-format-date date))
4281 "\n")
4282 (put-text-property s (1- (point)) 'face
4283 (org-agenda-get-day-face date))
4284 (put-text-property s (1- (point)) 'org-date-line t)
4285 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4286 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4287 (when todayp
4288 (put-text-property s (1- (point)) 'org-today t))
4289 (setq rtnall
4290 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4291 (if rtnall (insert ;; all entries
4292 (org-agenda-finalize-entries rtnall 'agenda)
4293 "\n"))
4294 (put-text-property s (1- (point)) 'day d)
4295 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4296 (when (and org-agenda-clockreport-mode clocktable-start)
4297 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4298 ;; the above line is to ensure the restricted range!
4299 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4300 tbl)
4301 (setq p (org-plist-delete p :block))
4302 (setq p (plist-put p :tstart clocktable-start))
4303 (setq p (plist-put p :tend clocktable-end))
4304 (setq p (plist-put p :scope 'agenda))
4305 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4306 (setq filter (or org-agenda-tag-filter-while-redo
4307 (get 'org-agenda-tag-filter :preset-filter))))
4308 (setq p (plist-put p :tags (mapconcat (lambda (x)
4309 (if (string-match "[<>=]" x)
4312 filter ""))))
4313 (setq tbl (apply 'org-clock-get-clocktable p))
4314 (insert tbl)))
4315 (goto-char (point-min))
4316 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4317 (unless (and (pos-visible-in-window-p (point-min))
4318 (pos-visible-in-window-p (point-max)))
4319 (goto-char (1- (point-max)))
4320 (recenter -1)
4321 (if (not (pos-visible-in-window-p (or start-pos 1)))
4322 (progn
4323 (goto-char (or start-pos 1))
4324 (recenter 1))))
4325 (goto-char (or start-pos 1))
4326 (add-text-properties (point-min) (point-max)
4327 `(org-agenda-type agenda
4328 org-last-args (,arg ,start-day ,span)
4329 org-redo-cmd ,org-agenda-redo-command
4330 org-series-cmd ,org-cmd))
4331 (if (eq org-agenda-show-log-scoped 'clockcheck)
4332 (org-agenda-show-clocking-issues))
4333 (org-agenda-finalize)
4334 (setq buffer-read-only t)
4335 (message ""))))
4337 (defun org-agenda-ndays-to-span (n)
4338 "Return a span symbol for a span of N days, or N if none matches."
4339 (cond ((symbolp n) n)
4340 ((= n 1) 'day)
4341 ((= n 7) 'week)
4342 (t n)))
4344 (defun org-agenda-span-to-ndays (span &optional start-day)
4345 "Return ndays from SPAN, possibly starting at START-DAY.
4346 START-DAY is an absolute time value."
4347 (cond ((numberp span) span)
4348 ((eq span 'day) 1)
4349 ((eq span 'week) 7)
4350 ((eq span 'month)
4351 (let ((date (calendar-gregorian-from-absolute start-day)))
4352 (calendar-last-day-of-month (car date) (caddr date))))
4353 ((eq span 'year)
4354 (let ((date (calendar-gregorian-from-absolute start-day)))
4355 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4357 (defun org-agenda-span-name (span)
4358 "Return a SPAN name."
4359 (if (null span)
4361 (if (symbolp span)
4362 (capitalize (symbol-name span))
4363 (format "%d days" span))))
4365 ;;; Agenda word search
4367 (defvar org-agenda-search-history nil)
4369 (defvar org-search-syntax-table nil
4370 "Special syntax table for org-mode search.
4371 In this table, we have single quotes not as word constituents, to
4372 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4374 (defvar org-mode-syntax-table) ; From org.el
4375 (defun org-search-syntax-table ()
4376 (unless org-search-syntax-table
4377 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4378 (modify-syntax-entry ?' "." org-search-syntax-table)
4379 (modify-syntax-entry ?` "." org-search-syntax-table))
4380 org-search-syntax-table)
4382 (defvar org-agenda-last-search-view-search-was-boolean nil)
4384 ;;;###autoload
4385 (defun org-search-view (&optional todo-only string edit-at)
4386 "Show all entries that contain a phrase or words or regular expressions.
4388 With optional prefix argument TODO-ONLY, only consider entries that are
4389 TODO entries. The argument STRING can be used to pass a default search
4390 string into this function. If EDIT-AT is non-nil, it means that the
4391 user should get a chance to edit this string, with cursor at position
4392 EDIT-AT.
4394 The search string can be viewed either as a phrase that should be found as
4395 is, or it can be broken into a number of snippets, each of which must match
4396 in a Boolean way to select an entry. The default depends on the variable
4397 `org-agenda-search-view-always-boolean'.
4398 Even if this is turned off (the default) you can always switch to
4399 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4401 The default is a direct search of the whole phrase, where each space in
4402 the search string can expand to an arbitrary amount of whitespace,
4403 including newlines.
4405 If using a Boolean search, the search string is split on whitespace and
4406 each snippet is searched separately, with logical AND to select an entry.
4407 Words prefixed with a minus must *not* occur in the entry. Words without
4408 a prefix or prefixed with a plus must occur in the entry. Matching is
4409 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4410 match whole words, not parts of a word) if
4411 `org-agenda-search-view-force-full-words' is set (default is nil).
4413 Boolean search snippets enclosed by curly braces are interpreted as
4414 regular expressions that must or (when preceded with \"-\") must not
4415 match in the entry. Snippets enclosed into double quotes will be taken
4416 as a whole, to include whitespace.
4418 - If the search string starts with an asterisk, search only in headlines.
4419 - If (possibly after the leading star) the search string starts with an
4420 exclamation mark, this also means to look at TODO entries only, an effect
4421 that can also be achieved with a prefix argument.
4422 - If (possibly after star and exclamation mark) the search string starts
4423 with a colon, this will mean that the (non-regexp) snippets of the
4424 Boolean search must match as full words.
4426 This command searches the agenda files, and in addition the files listed
4427 in `org-agenda-text-search-extra-files'."
4428 (interactive "P")
4429 (if org-agenda-overriding-arguments
4430 (setq todo-only (car org-agenda-overriding-arguments)
4431 string (nth 1 org-agenda-overriding-arguments)
4432 edit-at (nth 2 org-agenda-overriding-arguments)))
4433 (let* ((props (list 'face nil
4434 'done-face 'org-agenda-done
4435 'org-not-done-regexp org-not-done-regexp
4436 'org-todo-regexp org-todo-regexp
4437 'org-complex-heading-regexp org-complex-heading-regexp
4438 'mouse-face 'highlight
4439 'help-echo (format "mouse-2 or RET jump to location")))
4440 (full-words org-agenda-search-view-force-full-words)
4441 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4442 regexp rtn rtnall files file pos inherited-tags
4443 marker category category-pos level tags c neg re boolean
4444 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4445 (unless (and (not edit-at)
4446 (stringp string)
4447 (string-match "\\S-" string))
4448 (setq string (read-string
4449 (if org-agenda-search-view-always-boolean
4450 "[+-]Word/{Regexp} ...: "
4451 "Phrase or [+-]Word/{Regexp} ...: ")
4452 (cond
4453 ((integerp edit-at) (cons string edit-at))
4454 (edit-at string))
4455 'org-agenda-search-history)))
4456 (catch 'exit
4457 (if org-agenda-sticky
4458 (setq org-agenda-buffer-name
4459 (if (stringp string)
4460 (format "*Org Agenda(%s:%s)*"
4461 (or org-keys (or (and todo-only "S") "s")) string)
4462 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4463 (org-agenda-prepare "SEARCH")
4464 (org-compile-prefix-format 'search)
4465 (org-set-sorting-strategy 'search)
4466 (setq org-agenda-redo-command
4467 (list 'org-search-view (if todo-only t nil)
4468 (list 'if 'current-prefix-arg nil string)))
4469 (setq org-agenda-query-string string)
4470 (if (equal (string-to-char string) ?*)
4471 (setq hdl-only t
4472 words (substring string 1))
4473 (setq words string))
4474 (when (equal (string-to-char words) ?!)
4475 (setq todo-only t
4476 words (substring words 1)))
4477 (when (equal (string-to-char words) ?:)
4478 (setq full-words t
4479 words (substring words 1)))
4480 (if (or org-agenda-search-view-always-boolean
4481 (member (string-to-char words) '(?- ?+ ?\{)))
4482 (setq boolean t))
4483 (setq words (org-split-string words))
4484 (let (www w)
4485 (while (setq w (pop words))
4486 (while (and (string-match "\\\\\\'" w) words)
4487 (setq w (concat (substring w 0 -1) " " (pop words))))
4488 (push w www))
4489 (setq words (nreverse www) www nil)
4490 (while (setq w (pop words))
4491 (when (and (string-match "\\`[-+]?{" w)
4492 (not (string-match "}\\'" w)))
4493 (while (and words (not (string-match "}\\'" (car words))))
4494 (setq w (concat w " " (pop words))))
4495 (setq w (concat w " " (pop words))))
4496 (push w www))
4497 (setq words (nreverse www)))
4498 (setq org-agenda-last-search-view-search-was-boolean boolean)
4499 (when boolean
4500 (let (wds w)
4501 (while (setq w (pop words))
4502 (if (or (equal (substring w 0 1) "\"")
4503 (and (> (length w) 1)
4504 (member (substring w 0 1) '("+" "-"))
4505 (equal (substring w 1 2) "\"")))
4506 (while (and words (not (equal (substring w -1) "\"")))
4507 (setq w (concat w " " (pop words)))))
4508 (and (string-match "\\`\\([-+]?\\)\"" w)
4509 (setq w (replace-match "\\1" nil nil w)))
4510 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4511 (push w wds))
4512 (setq words (nreverse wds))))
4513 (if boolean
4514 (mapc (lambda (w)
4515 (setq c (string-to-char w))
4516 (if (equal c ?-)
4517 (setq neg t w (substring w 1))
4518 (if (equal c ?+)
4519 (setq neg nil w (substring w 1))
4520 (setq neg nil)))
4521 (if (string-match "\\`{.*}\\'" w)
4522 (setq re (substring w 1 -1))
4523 (if full-words
4524 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4525 (setq re (regexp-quote (downcase w)))))
4526 (if neg (push re regexps-) (push re regexps+)))
4527 words)
4528 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4529 regexps+))
4530 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4531 (if (not regexps+)
4532 (setq regexp org-outline-regexp-bol)
4533 (setq regexp (pop regexps+))
4534 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4535 regexp))))
4536 (setq files (org-agenda-files nil 'ifmode))
4537 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4538 (pop org-agenda-text-search-extra-files)
4539 (setq files (org-add-archive-files files)))
4540 (setq files (append files org-agenda-text-search-extra-files)
4541 rtnall nil)
4542 (while (setq file (pop files))
4543 (setq ee nil)
4544 (catch 'nextfile
4545 (org-check-agenda-file file)
4546 (setq buffer (if (file-exists-p file)
4547 (org-get-agenda-file-buffer file)
4548 (error "No such file %s" file)))
4549 (if (not buffer)
4550 ;; If file does not exist, make sure an error message is sent
4551 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4552 file))))
4553 (with-current-buffer buffer
4554 (with-syntax-table (org-search-syntax-table)
4555 (unless (derived-mode-p 'org-mode)
4556 (error "Agenda file %s is not in `org-mode'" file))
4557 (let ((case-fold-search t))
4558 (save-excursion
4559 (save-restriction
4560 (if org-agenda-restrict
4561 (narrow-to-region org-agenda-restrict-begin
4562 org-agenda-restrict-end)
4563 (widen))
4564 (goto-char (point-min))
4565 (unless (or (org-at-heading-p)
4566 (outline-next-heading))
4567 (throw 'nextfile t))
4568 (goto-char (max (point-min) (1- (point))))
4569 (while (re-search-forward regexp nil t)
4570 (org-back-to-heading t)
4571 (while (and org-agenda-search-view-max-outline-level
4572 (> (org-reduced-level (org-outline-level))
4573 org-agenda-search-view-max-outline-level)
4574 (forward-line -1)
4575 (outline-back-to-heading t)))
4576 (skip-chars-forward "* ")
4577 (setq beg (point-at-bol)
4578 beg1 (point)
4579 end (progn
4580 (outline-next-heading)
4581 (while (and org-agenda-search-view-max-outline-level
4582 (> (org-reduced-level (org-outline-level))
4583 org-agenda-search-view-max-outline-level)
4584 (forward-line 1)
4585 (outline-next-heading)))
4586 (point)))
4588 (catch :skip
4589 (goto-char beg)
4590 (org-agenda-skip)
4591 (setq str (buffer-substring-no-properties
4592 (point-at-bol)
4593 (if hdl-only (point-at-eol) end)))
4594 (mapc (lambda (wr) (when (string-match wr str)
4595 (goto-char (1- end))
4596 (throw :skip t)))
4597 regexps-)
4598 (mapc (lambda (wr) (unless (string-match wr str)
4599 (goto-char (1- end))
4600 (throw :skip t)))
4601 (if todo-only
4602 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4603 regexps+)
4604 regexps+))
4605 (goto-char beg)
4606 (setq marker (org-agenda-new-marker (point))
4607 category (org-get-category)
4608 level (make-string (org-reduced-level (org-outline-level)) ? )
4609 category-pos (get-text-property (point) 'org-category-position)
4610 inherited-tags
4611 (or (eq org-agenda-show-inherited-tags 'always)
4612 (and (listp org-agenda-show-inherited-tags)
4613 (memq 'todo org-agenda-show-inherited-tags))
4614 (and (eq org-agenda-show-inherited-tags t)
4615 (or (eq org-agenda-use-tag-inheritance t)
4616 (memq 'todo org-agenda-use-tag-inheritance))))
4617 tags (org-get-tags-at nil (not inherited-tags))
4618 txt (org-agenda-format-item
4620 (buffer-substring-no-properties
4621 beg1 (point-at-eol))
4622 level category tags t))
4623 (org-add-props txt props
4624 'org-marker marker 'org-hd-marker marker
4625 'org-todo-regexp org-todo-regexp
4626 'level level
4627 'org-complex-heading-regexp org-complex-heading-regexp
4628 'priority 1000 'org-category category
4629 'org-category-position category-pos
4630 'type "search")
4631 (push txt ee)
4632 (goto-char (1- end))))))))))
4633 (setq rtn (nreverse ee))
4634 (setq rtnall (append rtnall rtn)))
4635 (if org-agenda-overriding-header
4636 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4637 nil 'face 'org-agenda-structure) "\n")
4638 (insert "Search words: ")
4639 (add-text-properties (point-min) (1- (point))
4640 (list 'face 'org-agenda-structure))
4641 (setq pos (point))
4642 (insert string "\n")
4643 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4644 (setq pos (point))
4645 (unless org-agenda-multi
4646 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4647 (add-text-properties pos (1- (point))
4648 (list 'face 'org-agenda-structure))))
4649 (org-agenda-mark-header-line (point-min))
4650 (when rtnall
4651 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4652 (goto-char (point-min))
4653 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4654 (add-text-properties (point-min) (point-max)
4655 `(org-agenda-type search
4656 org-last-args (,todo-only ,string ,edit-at)
4657 org-redo-cmd ,org-agenda-redo-command
4658 org-series-cmd ,org-cmd))
4659 (org-agenda-finalize)
4660 (setq buffer-read-only t))))
4662 ;;; Agenda TODO list
4664 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4665 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4666 (concat
4667 (if (or (equal keywords "ALL") (not keywords))
4668 (propertize "ALL" 'face 'warning)
4669 (mapconcat
4670 (lambda (kw)
4671 (propertize kw 'face (org-get-todo-face kw)))
4672 (org-split-string keywords "|")
4673 "|"))
4674 "\n"))
4676 (defvar org-select-this-todo-keyword nil)
4677 (defvar org-last-arg nil)
4679 ;;;###autoload
4680 (defun org-todo-list (&optional arg)
4681 "Show all (not done) TODO entries from all agenda file in a single list.
4682 The prefix arg can be used to select a specific TODO keyword and limit
4683 the list to these. When using \\[universal-argument], you will be prompted
4684 for a keyword. A numeric prefix directly selects the Nth keyword in
4685 `org-todo-keywords-1'."
4686 (interactive "P")
4687 (if org-agenda-overriding-arguments
4688 (setq arg org-agenda-overriding-arguments))
4689 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4690 (let* ((today (org-today))
4691 (date (calendar-gregorian-from-absolute today))
4692 (kwds org-todo-keywords-for-agenda)
4693 (completion-ignore-case t)
4694 (org-select-this-todo-keyword
4695 (if (stringp arg) arg
4696 (and arg (integerp arg) (> arg 0)
4697 (nth (1- arg) kwds))))
4698 rtn rtnall files file pos)
4699 (when (equal arg '(4))
4700 (setq org-select-this-todo-keyword
4701 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4702 (mapcar 'list kwds) nil nil)))
4703 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4704 (catch 'exit
4705 (if org-agenda-sticky
4706 (setq org-agenda-buffer-name
4707 (if (stringp org-select-this-todo-keyword)
4708 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4709 org-select-this-todo-keyword)
4710 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4711 (org-agenda-prepare "TODO")
4712 (org-compile-prefix-format 'todo)
4713 (org-set-sorting-strategy 'todo)
4714 (setq org-agenda-redo-command
4715 `(org-todo-list (or (and (numberp current-prefix-arg)
4716 current-prefix-arg)
4717 ,org-select-this-todo-keyword
4718 current-prefix-arg ,arg)))
4719 (setq files (org-agenda-files nil 'ifmode)
4720 rtnall nil)
4721 (while (setq file (pop files))
4722 (catch 'nextfile
4723 (org-check-agenda-file file)
4724 (setq rtn (org-agenda-get-day-entries file date :todo))
4725 (setq rtnall (append rtnall rtn))))
4726 (if org-agenda-overriding-header
4727 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4728 nil 'face 'org-agenda-structure) "\n")
4729 (insert "Global list of TODO items of type: ")
4730 (add-text-properties (point-min) (1- (point))
4731 (list 'face 'org-agenda-structure
4732 'short-heading
4733 (concat "ToDo: "
4734 (or org-select-this-todo-keyword "ALL"))))
4735 (org-agenda-mark-header-line (point-min))
4736 (insert (org-agenda-propertize-selected-todo-keywords
4737 org-select-this-todo-keyword))
4738 (setq pos (point))
4739 (unless org-agenda-multi
4740 (insert "Available with `N r': (0)[ALL]")
4741 (let ((n 0) s)
4742 (mapc (lambda (x)
4743 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4744 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4745 (insert "\n "))
4746 (insert " " s))
4747 kwds))
4748 (insert "\n"))
4749 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4750 (org-agenda-mark-header-line (point-min))
4751 (when rtnall
4752 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4753 (goto-char (point-min))
4754 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4755 (add-text-properties (point-min) (point-max)
4756 `(org-agenda-type todo
4757 org-last-args ,arg
4758 org-redo-cmd ,org-agenda-redo-command
4759 org-series-cmd ,org-cmd))
4760 (org-agenda-finalize)
4761 (setq buffer-read-only t))))
4763 ;;; Agenda tags match
4765 ;;;###autoload
4766 (defun org-tags-view (&optional todo-only match)
4767 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4768 The prefix arg TODO-ONLY limits the search to TODO entries."
4769 (interactive "P")
4770 (if org-agenda-overriding-arguments
4771 (setq todo-only (car org-agenda-overriding-arguments)
4772 match (nth 1 org-agenda-overriding-arguments)))
4773 (let* ((org-tags-match-list-sublevels
4774 org-tags-match-list-sublevels)
4775 (completion-ignore-case t)
4776 rtn rtnall files file pos matcher
4777 buffer)
4778 (when (and (stringp match) (not (string-match "\\S-" match)))
4779 (setq match nil))
4780 (catch 'exit
4781 (if org-agenda-sticky
4782 (setq org-agenda-buffer-name
4783 (if (stringp match)
4784 (format "*Org Agenda(%s:%s)*"
4785 (or org-keys (or (and todo-only "M") "m")) match)
4786 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4787 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4788 ;; expanding tags within `org-make-tags-matcher'
4789 (org-agenda-prepare (concat "TAGS " match))
4790 (setq matcher (org-make-tags-matcher match)
4791 match (car matcher) matcher (cdr matcher))
4792 (org-compile-prefix-format 'tags)
4793 (org-set-sorting-strategy 'tags)
4794 (setq org-agenda-query-string match)
4795 (setq org-agenda-redo-command
4796 (list 'org-tags-view `(quote ,todo-only)
4797 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4798 (setq files (org-agenda-files nil 'ifmode)
4799 rtnall nil)
4800 (while (setq file (pop files))
4801 (catch 'nextfile
4802 (org-check-agenda-file file)
4803 (setq buffer (if (file-exists-p file)
4804 (org-get-agenda-file-buffer file)
4805 (error "No such file %s" file)))
4806 (if (not buffer)
4807 ;; If file does not exist, error message to agenda
4808 (setq rtn (list
4809 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4810 rtnall (append rtnall rtn))
4811 (with-current-buffer buffer
4812 (unless (derived-mode-p 'org-mode)
4813 (error "Agenda file %s is not in `org-mode'" file))
4814 (save-excursion
4815 (save-restriction
4816 (if org-agenda-restrict
4817 (narrow-to-region org-agenda-restrict-begin
4818 org-agenda-restrict-end)
4819 (widen))
4820 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4821 (setq rtnall (append rtnall rtn))))))))
4822 (if org-agenda-overriding-header
4823 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4824 nil 'face 'org-agenda-structure) "\n")
4825 (insert "Headlines with TAGS match: ")
4826 (add-text-properties (point-min) (1- (point))
4827 (list 'face 'org-agenda-structure
4828 'short-heading
4829 (concat "Match: " match)))
4830 (setq pos (point))
4831 (insert match "\n")
4832 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4833 (setq pos (point))
4834 (unless org-agenda-multi
4835 (insert "Press `C-u r' to search again with new search string\n"))
4836 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4837 (org-agenda-mark-header-line (point-min))
4838 (when rtnall
4839 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4840 (goto-char (point-min))
4841 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4842 (add-text-properties (point-min) (point-max)
4843 `(org-agenda-type tags
4844 org-last-args (,todo-only ,match)
4845 org-redo-cmd ,org-agenda-redo-command
4846 org-series-cmd ,org-cmd))
4847 (org-agenda-finalize)
4848 (setq buffer-read-only t))))
4850 ;;; Agenda Finding stuck projects
4852 (defvar org-agenda-skip-regexp nil
4853 "Regular expression used in skipping subtrees for the agenda.
4854 This is basically a temporary global variable that can be set and then
4855 used by user-defined selections using `org-agenda-skip-function'.")
4857 (defvar org-agenda-overriding-header nil
4858 "When set during agenda, todo and tags searches it replaces the header.
4859 This variable should not be set directly, but custom commands can bind it
4860 in the options section.")
4862 (defun org-agenda-skip-entry-when-regexp-matches ()
4863 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4864 If yes, it returns the end position of this entry, causing agenda commands
4865 to skip the entry but continuing the search in the subtree. This is a
4866 function that can be put into `org-agenda-skip-function' for the duration
4867 of a command."
4868 (let ((end (save-excursion (org-end-of-subtree t)))
4869 skip)
4870 (save-excursion
4871 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4872 (and skip end)))
4874 (defun org-agenda-skip-subtree-when-regexp-matches ()
4875 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4876 If yes, it returns the end position of this tree, causing agenda commands
4877 to skip this subtree. This is a function that can be put into
4878 `org-agenda-skip-function' for the duration of a command."
4879 (let ((end (save-excursion (org-end-of-subtree t)))
4880 skip)
4881 (save-excursion
4882 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4883 (and skip end)))
4885 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4886 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4887 If yes, it returns the end position of the current entry (NOT the tree),
4888 causing agenda commands to skip the entry but continuing the search in
4889 the subtree. This is a function that can be put into
4890 `org-agenda-skip-function' for the duration of a command. An important
4891 use of this function is for the stuck project list."
4892 (let ((end (save-excursion (org-end-of-subtree t)))
4893 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4894 skip)
4895 (save-excursion
4896 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4897 (and skip entry-end)))
4899 (defun org-agenda-skip-entry-if (&rest conditions)
4900 "Skip entry if any of CONDITIONS is true.
4901 See `org-agenda-skip-if' for details."
4902 (org-agenda-skip-if nil conditions))
4904 (defun org-agenda-skip-subtree-if (&rest conditions)
4905 "Skip entry if any of CONDITIONS is true.
4906 See `org-agenda-skip-if' for details."
4907 (org-agenda-skip-if t conditions))
4909 (defun org-agenda-skip-if (subtree conditions)
4910 "Checks current entity for CONDITIONS.
4911 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4912 the entry (i.e. the text before the next heading) is checked.
4914 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4915 from different tests. Valid conditions are:
4917 scheduled Check if there is a scheduled cookie
4918 notscheduled Check if there is no scheduled cookie
4919 deadline Check if there is a deadline
4920 notdeadline Check if there is no deadline
4921 timestamp Check if there is a timestamp (also deadline or scheduled)
4922 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4923 regexp Check if regexp matches
4924 notregexp Check if regexp does not match.
4925 todo Check if TODO keyword matches
4926 nottodo Check if TODO keyword does not match
4928 The regexp is taken from the conditions list, it must come right after
4929 the `regexp' or `notregexp' element.
4931 `todo' and `nottodo' accept as an argument a list of todo
4932 keywords, which may include \"*\" to match any todo keyword.
4934 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4936 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4938 Instead of a list, a keyword class may be given. For example:
4940 (org-agenda-skip-entry-if 'nottodo 'done)
4942 would skip entries that haven't been marked with any of \"DONE\"
4943 keywords. Possible classes are: `todo', `done', `any'.
4945 If any of these conditions is met, this function returns the end point of
4946 the entity, causing the search to continue from there. This is a function
4947 that can be put into `org-agenda-skip-function' for the duration of a command."
4948 (let (beg end m)
4949 (org-back-to-heading t)
4950 (setq beg (point)
4951 end (if subtree
4952 (progn (org-end-of-subtree t) (point))
4953 (progn (outline-next-heading) (1- (point)))))
4954 (goto-char beg)
4955 (and
4957 (and (memq 'scheduled conditions)
4958 (re-search-forward org-scheduled-time-regexp end t))
4959 (and (memq 'notscheduled conditions)
4960 (not (re-search-forward org-scheduled-time-regexp end t)))
4961 (and (memq 'deadline conditions)
4962 (re-search-forward org-deadline-time-regexp end t))
4963 (and (memq 'notdeadline conditions)
4964 (not (re-search-forward org-deadline-time-regexp end t)))
4965 (and (memq 'timestamp conditions)
4966 (re-search-forward org-ts-regexp end t))
4967 (and (memq 'nottimestamp conditions)
4968 (not (re-search-forward org-ts-regexp end t)))
4969 (and (setq m (memq 'regexp conditions))
4970 (stringp (nth 1 m))
4971 (re-search-forward (nth 1 m) end t))
4972 (and (setq m (memq 'notregexp conditions))
4973 (stringp (nth 1 m))
4974 (not (re-search-forward (nth 1 m) end t)))
4975 (and (or
4976 (setq m (memq 'nottodo conditions))
4977 (setq m (memq 'todo-unblocked conditions))
4978 (setq m (memq 'nottodo-unblocked conditions))
4979 (setq m (memq 'todo conditions)))
4980 (org-agenda-skip-if-todo m end)))
4981 end)))
4983 (defun org-agenda-skip-if-todo (args end)
4984 "Helper function for `org-agenda-skip-if', do not use it directly.
4985 ARGS is a list with first element either `todo', `nottodo',
4986 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4987 a list of TODO keywords, or a state symbol `todo' or `done' or
4988 `any'."
4989 (let ((kw (car args))
4990 (arg (cadr args))
4991 todo-wds todo-re)
4992 (setq todo-wds
4993 (org-uniquify
4994 (cond
4995 ((listp arg) ;; list of keywords
4996 (if (member "*" arg)
4997 (mapcar 'substring-no-properties org-todo-keywords-1)
4998 arg))
4999 ((symbolp arg) ;; keyword class name
5000 (cond
5001 ((eq arg 'todo)
5002 (org-delete-all org-done-keywords
5003 (mapcar 'substring-no-properties
5004 org-todo-keywords-1)))
5005 ((eq arg 'done) org-done-keywords)
5006 ((eq arg 'any)
5007 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5008 (setq todo-re
5009 (concat "^\\*+[ \t]+\\<\\("
5010 (mapconcat 'identity todo-wds "\\|")
5011 "\\)\\>"))
5012 (cond
5013 ((eq kw 'todo) (re-search-forward todo-re end t))
5014 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5015 ((eq kw 'todo-unblocked)
5016 (catch 'unblocked
5017 (while (re-search-forward todo-re end t)
5018 (or (org-entry-blocked-p) (throw 'unblocked t)))
5019 nil))
5020 ((eq kw 'nottodo-unblocked)
5021 (catch 'unblocked
5022 (while (re-search-forward todo-re end t)
5023 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5027 ;;;###autoload
5028 (defun org-agenda-list-stuck-projects (&rest ignore)
5029 "Create agenda view for projects that are stuck.
5030 Stuck projects are project that have no next actions. For the definitions
5031 of what a project is and how to check if it stuck, customize the variable
5032 `org-stuck-projects'."
5033 (interactive)
5034 (let* ((org-agenda-skip-function
5035 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5036 ;; We could have used org-agenda-skip-if here.
5037 (org-agenda-overriding-header
5038 (or org-agenda-overriding-header "List of stuck projects: "))
5039 (matcher (nth 0 org-stuck-projects))
5040 (todo (nth 1 org-stuck-projects))
5041 (todo-wds (if (member "*" todo)
5042 (progn
5043 (org-agenda-prepare-buffers (org-agenda-files
5044 nil 'ifmode))
5045 (org-delete-all
5046 org-done-keywords-for-agenda
5047 (copy-sequence org-todo-keywords-for-agenda)))
5048 todo))
5049 (todo-re (concat "^\\*+[ \t]+\\("
5050 (mapconcat 'identity todo-wds "\\|")
5051 "\\)\\>"))
5052 (tags (nth 2 org-stuck-projects))
5053 (tags-re (if (member "*" tags)
5054 (concat org-outline-regexp-bol
5055 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5056 (if tags
5057 (concat org-outline-regexp-bol
5058 ".*:\\("
5059 (mapconcat 'identity tags "\\|")
5060 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5061 (gen-re (nth 3 org-stuck-projects))
5062 (re-list
5063 (delq nil
5064 (list
5065 (if todo todo-re)
5066 (if tags tags-re)
5067 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5068 gen-re)))))
5069 (setq org-agenda-skip-regexp
5070 (if re-list
5071 (mapconcat 'identity re-list "\\|")
5072 (error "No information how to identify unstuck projects")))
5073 (org-tags-view nil matcher)
5074 (with-current-buffer org-agenda-buffer-name
5075 (setq org-agenda-redo-command
5076 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5078 ;;; Diary integration
5080 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5081 (defvar diary-list-entries-hook)
5082 (defvar diary-time-regexp)
5083 (defun org-get-entries-from-diary (date)
5084 "Get the (Emacs Calendar) diary entries for DATE."
5085 (require 'diary-lib)
5086 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5087 (diary-display-hook '(fancy-diary-display))
5088 (diary-display-function 'fancy-diary-display)
5089 (pop-up-frames nil)
5090 (diary-list-entries-hook
5091 (cons 'org-diary-default-entry diary-list-entries-hook))
5092 (diary-file-name-prefix nil) ; turn this feature off
5093 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5094 entries
5095 (org-disable-agenda-to-diary t))
5096 (save-excursion
5097 (save-window-excursion
5098 (funcall (if (fboundp 'diary-list-entries)
5099 'diary-list-entries 'list-diary-entries)
5100 date 1)))
5101 (if (not (get-buffer diary-fancy-buffer))
5102 (setq entries nil)
5103 (with-current-buffer diary-fancy-buffer
5104 (setq buffer-read-only nil)
5105 (if (zerop (buffer-size))
5106 ;; No entries
5107 (setq entries nil)
5108 ;; Omit the date and other unnecessary stuff
5109 (org-agenda-cleanup-fancy-diary)
5110 ;; Add prefix to each line and extend the text properties
5111 (if (zerop (buffer-size))
5112 (setq entries nil)
5113 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5114 (setq entries
5115 (with-temp-buffer
5116 (insert entries) (goto-char (point-min))
5117 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5118 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5119 (replace-match (concat "; " (match-string 1)))))
5120 (buffer-string)))))
5121 (set-buffer-modified-p nil)
5122 (kill-buffer diary-fancy-buffer)))
5123 (when entries
5124 (setq entries (org-split-string entries "\n"))
5125 (setq entries
5126 (mapcar
5127 (lambda (x)
5128 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5129 ;; Extend the text properties to the beginning of the line
5130 (org-add-props x (text-properties-at (1- (length x)) x)
5131 'type "diary" 'date date 'face 'org-agenda-diary))
5132 entries)))))
5134 (defvar org-agenda-cleanup-fancy-diary-hook nil
5135 "Hook run when the fancy diary buffer is cleaned up.")
5137 (defun org-agenda-cleanup-fancy-diary ()
5138 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5139 This gets rid of the date, the underline under the date, and
5140 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5141 date. It also removes lines that contain only whitespace."
5142 (goto-char (point-min))
5143 (if (looking-at ".*?:[ \t]*")
5144 (progn
5145 (replace-match "")
5146 (re-search-forward "\n=+$" nil t)
5147 (replace-match "")
5148 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5149 (re-search-forward "\n=+$" nil t)
5150 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5151 (goto-char (point-min))
5152 (while (re-search-forward "^ +\n" nil t)
5153 (replace-match ""))
5154 (goto-char (point-min))
5155 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5156 (replace-match ""))
5157 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5159 ;; Make sure entries from the diary have the right text properties.
5160 (eval-after-load "diary-lib"
5161 '(if (boundp 'diary-modify-entry-list-string-function)
5162 ;; We can rely on the hook, nothing to do
5164 ;; Hook not available, must use advice to make this work
5165 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5166 "Make the position visible."
5167 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5168 (stringp string)
5169 buffer-file-name)
5170 (setq string (org-modify-diary-entry-string string))))))
5172 (defun org-modify-diary-entry-string (string)
5173 "Add text properties to string, allowing org-mode to act on it."
5174 (org-add-props string nil
5175 'mouse-face 'highlight
5176 'help-echo (if buffer-file-name
5177 (format "mouse-2 or RET jump to diary file %s"
5178 (abbreviate-file-name buffer-file-name))
5180 'org-agenda-diary-link t
5181 'org-marker (org-agenda-new-marker (point-at-bol))))
5183 (defun org-diary-default-entry ()
5184 "Add a dummy entry to the diary.
5185 Needed to avoid empty dates which mess up holiday display."
5186 ;; Catch the error if dealing with the new add-to-diary-alist
5187 (when org-disable-agenda-to-diary
5188 (condition-case nil
5189 (org-add-to-diary-list original-date "Org-mode dummy" "")
5190 (error
5191 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5193 (defun org-add-to-diary-list (&rest args)
5194 (if (fboundp 'diary-add-to-list)
5195 (apply 'diary-add-to-list args)
5196 (apply 'add-to-diary-list args)))
5198 (defvar org-diary-last-run-time nil)
5200 ;;;###autoload
5201 (defun org-diary (&rest args)
5202 "Return diary information from org files.
5203 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5204 It accesses org files and extracts information from those files to be
5205 listed in the diary. The function accepts arguments specifying what
5206 items should be listed. For a list of arguments allowed here, see the
5207 variable `org-agenda-entry-types'.
5209 The call in the diary file should look like this:
5211 &%%(org-diary) ~/path/to/some/orgfile.org
5213 Use a separate line for each org file to check. Or, if you omit the file name,
5214 all files listed in `org-agenda-files' will be checked automatically:
5216 &%%(org-diary)
5218 If you don't give any arguments (as in the example above), the default value
5219 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5220 So the example above may also be written as
5222 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5224 The function expects the lisp variables `entry' and `date' to be provided
5225 by the caller, because this is how the calendar works. Don't use this
5226 function from a program - use `org-agenda-get-day-entries' instead."
5227 (when (> (- (org-float-time)
5228 org-agenda-last-marker-time)
5230 ;; I am not sure if this works with sticky agendas, because the marker
5231 ;; list is then no longer a global variable.
5232 (org-agenda-reset-markers))
5233 (org-compile-prefix-format 'agenda)
5234 (org-set-sorting-strategy 'agenda)
5235 (setq args (or args org-agenda-entry-types))
5236 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5237 (list entry)
5238 (org-agenda-files t)))
5239 (time (org-float-time))
5240 file rtn results)
5241 (when (or (not org-diary-last-run-time)
5242 (> (- time
5243 org-diary-last-run-time)
5245 (org-agenda-prepare-buffers files))
5246 (setq org-diary-last-run-time time)
5247 ;; If this is called during org-agenda, don't return any entries to
5248 ;; the calendar. Org Agenda will list these entries itself.
5249 (if org-disable-agenda-to-diary (setq files nil))
5250 (while (setq file (pop files))
5251 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5252 (setq results (append results rtn)))
5253 (when results
5254 (setq results
5255 (mapcar (lambda (i) (replace-regexp-in-string
5256 org-bracket-link-regexp "\\3" i)) results))
5257 (concat (org-agenda-finalize-entries results) "\n"))))
5259 ;;; Agenda entry finders
5261 (defun org-agenda-get-day-entries (file date &rest args)
5262 "Does the work for `org-diary' and `org-agenda'.
5263 FILE is the path to a file to be checked for entries. DATE is date like
5264 the one returned by `calendar-current-date'. ARGS are symbols indicating
5265 which kind of entries should be extracted. For details about these, see
5266 the documentation of `org-diary'."
5267 (setq args (or args org-agenda-entry-types))
5268 (let* ((org-startup-folded nil)
5269 (org-startup-align-all-tables nil)
5270 (buffer (if (file-exists-p file)
5271 (org-get-agenda-file-buffer file)
5272 (error "No such file %s" file)))
5273 arg results rtn deadline-results)
5274 (if (not buffer)
5275 ;; If file does not exist, make sure an error message ends up in diary
5276 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5277 (with-current-buffer buffer
5278 (unless (derived-mode-p 'org-mode)
5279 (error "Agenda file %s is not in `org-mode'" file))
5280 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5281 (let ((case-fold-search nil))
5282 (save-excursion
5283 (save-restriction
5284 (if org-agenda-restrict
5285 (narrow-to-region org-agenda-restrict-begin
5286 org-agenda-restrict-end)
5287 (widen))
5288 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5289 (while (setq arg (pop args))
5290 (cond
5291 ((and (eq arg :todo)
5292 (equal date (calendar-gregorian-from-absolute
5293 (org-today))))
5294 (setq rtn (org-agenda-get-todos))
5295 (setq results (append results rtn)))
5296 ((eq arg :timestamp)
5297 (setq rtn (org-agenda-get-blocks))
5298 (setq results (append results rtn))
5299 (setq rtn (org-agenda-get-timestamps deadline-results))
5300 (setq results (append results rtn)))
5301 ((eq arg :sexp)
5302 (setq rtn (org-agenda-get-sexps))
5303 (setq results (append results rtn)))
5304 ((eq arg :scheduled)
5305 (setq rtn (org-agenda-get-scheduled deadline-results))
5306 (setq results (append results rtn)))
5307 ((eq arg :scheduled*)
5308 (setq rtn (org-agenda-get-scheduled deadline-results t))
5309 (setq results (append results rtn)))
5310 ((eq arg :closed)
5311 (setq rtn (org-agenda-get-progress))
5312 (setq results (append results rtn)))
5313 ((eq arg :deadline)
5314 (setq rtn (org-agenda-get-deadlines))
5315 (setq deadline-results (copy-sequence rtn))
5316 (setq results (append results rtn)))
5317 ((eq arg :deadline*)
5318 (setq rtn (org-agenda-get-deadlines t))
5319 (setq deadline-results (copy-sequence rtn))
5320 (setq results (append results rtn))))))))
5321 results))))
5323 (defsubst org-em (x y list)
5324 "Is X or Y a member of LIST?"
5325 (or (memq x list) (memq y list)))
5327 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5328 (defvar org-agenda-sorting-strategy-selected nil)
5330 (defun org-agenda-get-todos ()
5331 "Return the TODO information for agenda display."
5332 (let* ((props (list 'face nil
5333 'done-face 'org-agenda-done
5334 'org-not-done-regexp org-not-done-regexp
5335 'org-todo-regexp org-todo-regexp
5336 'org-complex-heading-regexp org-complex-heading-regexp
5337 'mouse-face 'highlight
5338 'help-echo
5339 (format "mouse-2 or RET jump to org file %s"
5340 (abbreviate-file-name buffer-file-name))))
5341 (regexp (format org-heading-keyword-regexp-format
5342 (cond
5343 ((and org-select-this-todo-keyword
5344 (equal org-select-this-todo-keyword "*"))
5345 org-todo-regexp)
5346 (org-select-this-todo-keyword
5347 (concat "\\("
5348 (mapconcat 'identity
5349 (org-split-string
5350 org-select-this-todo-keyword
5351 "|")
5352 "\\|") "\\)"))
5353 (t org-not-done-regexp))))
5354 marker priority category category-pos level tags todo-state ts-date ts-date-type
5355 ee txt beg end inherited-tags todo-state-end-pos)
5356 (goto-char (point-min))
5357 (while (re-search-forward regexp nil t)
5358 (catch :skip
5359 (save-match-data
5360 (beginning-of-line)
5361 (org-agenda-skip)
5362 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5363 (unless (and (setq todo-state (org-get-todo-state))
5364 (setq todo-state-end-pos (match-end 2)))
5365 (goto-char end)
5366 (throw :skip nil))
5367 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5368 (goto-char (1+ beg))
5369 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5370 (throw :skip nil)))
5371 (goto-char (match-beginning 2))
5372 (setq marker (org-agenda-new-marker (match-beginning 0))
5373 category (org-get-category)
5374 ts-date (let (ts)
5375 (save-match-data
5376 (cond ((org-em 'scheduled-up 'scheduled-down
5377 org-agenda-sorting-strategy-selected)
5378 (setq ts (org-entry-get (point) "SCHEDULED")
5379 ts-date-type " scheduled"))
5380 ((org-em 'deadline-up 'deadline-down
5381 org-agenda-sorting-strategy-selected)
5382 (setq ts (org-entry-get (point) "DEADLINE")
5383 ts-date-type " deadline"))
5384 ((org-em 'ts-up 'ts-down
5385 org-agenda-sorting-strategy-selected)
5386 (setq ts (org-entry-get (point) "TIMESTAMP")
5387 ts-date-type " timestamp"))
5388 ((org-em 'tsia-up 'tsia-down
5389 org-agenda-sorting-strategy-selected)
5390 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5391 ts-date-type " timestamp_ia"))
5392 ((org-em 'timestamp-up 'timestamp-down
5393 org-agenda-sorting-strategy-selected)
5394 (setq ts (or (org-entry-get (point) "SCHEDULED")
5395 (org-entry-get (point) "DEADLINE")
5396 (org-entry-get (point) "TIMESTAMP")
5397 (org-entry-get (point) "TIMESTAMP_IA"))
5398 ts-date-type ""))
5399 (t (setq ts-date-type "")))
5400 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5401 category-pos (get-text-property (point) 'org-category-position)
5402 txt (org-trim
5403 (buffer-substring (match-beginning 2) (match-end 0)))
5404 inherited-tags
5405 (or (eq org-agenda-show-inherited-tags 'always)
5406 (and (listp org-agenda-show-inherited-tags)
5407 (memq 'todo org-agenda-show-inherited-tags))
5408 (and (eq org-agenda-show-inherited-tags t)
5409 (or (eq org-agenda-use-tag-inheritance t)
5410 (memq 'todo org-agenda-use-tag-inheritance))))
5411 tags (org-get-tags-at nil (not inherited-tags))
5412 level (make-string (org-reduced-level (org-outline-level)) ? )
5413 txt (org-agenda-format-item "" txt level category tags t)
5414 priority (1+ (org-get-priority txt)))
5415 (org-add-props txt props
5416 'org-marker marker 'org-hd-marker marker
5417 'priority priority 'org-category category
5418 'level level
5419 'ts-date ts-date
5420 'org-category-position category-pos
5421 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5422 (push txt ee)
5423 (if org-agenda-todo-list-sublevels
5424 (goto-char todo-state-end-pos)
5425 (org-end-of-subtree 'invisible))))
5426 (nreverse ee)))
5428 (defun org-agenda-todo-custom-ignore-p (time n)
5429 "Check whether timestamp is farther away than n number of days.
5430 This function is invoked if `org-agenda-todo-ignore-deadlines',
5431 `org-agenda-todo-ignore-scheduled' or
5432 `org-agenda-todo-ignore-timestamp' is set to an integer."
5433 (let ((days (org-time-stamp-to-now
5434 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5435 (if (>= n 0)
5436 (>= days n)
5437 (<= days n))))
5439 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5440 (&optional end)
5441 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5442 (when (or org-agenda-todo-ignore-with-date
5443 org-agenda-todo-ignore-scheduled
5444 org-agenda-todo-ignore-deadlines
5445 org-agenda-todo-ignore-timestamp)
5446 (setq end (or end (save-excursion (outline-next-heading) (point))))
5447 (save-excursion
5448 (or (and org-agenda-todo-ignore-with-date
5449 (re-search-forward org-ts-regexp end t))
5450 (and org-agenda-todo-ignore-scheduled
5451 (re-search-forward org-scheduled-time-regexp end t)
5452 (cond
5453 ((eq org-agenda-todo-ignore-scheduled 'future)
5454 (> (org-time-stamp-to-now
5455 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5456 ((eq org-agenda-todo-ignore-scheduled 'past)
5457 (<= (org-time-stamp-to-now
5458 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5459 ((numberp org-agenda-todo-ignore-scheduled)
5460 (org-agenda-todo-custom-ignore-p
5461 (match-string 1) org-agenda-todo-ignore-scheduled))
5462 (t)))
5463 (and org-agenda-todo-ignore-deadlines
5464 (re-search-forward org-deadline-time-regexp end t)
5465 (cond
5466 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5467 ((eq org-agenda-todo-ignore-deadlines 'far)
5468 (not (org-deadline-close (match-string 1))))
5469 ((eq org-agenda-todo-ignore-deadlines 'future)
5470 (> (org-time-stamp-to-now
5471 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5472 ((eq org-agenda-todo-ignore-deadlines 'past)
5473 (<= (org-time-stamp-to-now
5474 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5475 ((numberp org-agenda-todo-ignore-deadlines)
5476 (org-agenda-todo-custom-ignore-p
5477 (match-string 1) org-agenda-todo-ignore-deadlines))
5478 (t (org-deadline-close (match-string 1)))))
5479 (and org-agenda-todo-ignore-timestamp
5480 (let ((buffer (current-buffer))
5481 (regexp
5482 (concat
5483 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5484 (start (point)))
5485 ;; Copy current buffer into a temporary one
5486 (with-temp-buffer
5487 (insert-buffer-substring buffer start end)
5488 (goto-char (point-min))
5489 ;; Delete SCHEDULED and DEADLINE items
5490 (while (re-search-forward regexp end t)
5491 (delete-region (match-beginning 0) (match-end 0)))
5492 (goto-char (point-min))
5493 ;; No search for timestamp left
5494 (when (re-search-forward org-ts-regexp nil t)
5495 (cond
5496 ((eq org-agenda-todo-ignore-timestamp 'future)
5497 (> (org-time-stamp-to-now
5498 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5499 ((eq org-agenda-todo-ignore-timestamp 'past)
5500 (<= (org-time-stamp-to-now
5501 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5502 ((numberp org-agenda-todo-ignore-timestamp)
5503 (org-agenda-todo-custom-ignore-p
5504 (match-string 1) org-agenda-todo-ignore-timestamp))
5505 (t))))))))))
5507 (defun org-agenda-get-timestamps (&optional deadline-results)
5508 "Return the date stamp information for agenda display."
5509 (let* ((props (list 'face 'org-agenda-calendar-event
5510 'org-not-done-regexp org-not-done-regexp
5511 'org-todo-regexp org-todo-regexp
5512 'org-complex-heading-regexp org-complex-heading-regexp
5513 'mouse-face 'highlight
5514 'help-echo
5515 (format "mouse-2 or RET jump to org file %s"
5516 (abbreviate-file-name buffer-file-name))))
5517 (d1 (calendar-absolute-from-gregorian date))
5519 (deadline-position-alist
5520 (mapcar (lambda (a) (and (setq mm (get-text-property
5521 0 'org-hd-marker a))
5522 (cons (marker-position mm) a)))
5523 deadline-results))
5524 (remove-re org-ts-regexp)
5525 (regexp
5526 (concat
5527 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5528 (regexp-quote
5529 (substring
5530 (format-time-string
5531 (car org-time-stamp-formats)
5532 (apply 'encode-time ; DATE bound by calendar
5533 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5534 1 11))
5535 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5536 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5537 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5538 donep tmp priority category category-pos level ee txt timestr tags
5539 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5540 inherited-tags ts-date)
5541 (goto-char (point-min))
5542 (while (setq end-of-match (re-search-forward regexp nil t))
5543 (setq b0 (match-beginning 0)
5544 b3 (match-beginning 3) e3 (match-end 3)
5545 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5546 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5547 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5548 (member todo-state
5549 org-agenda-repeating-timestamp-show-all)))
5550 (catch :skip
5551 (and (org-at-date-range-p) (throw :skip nil))
5552 (org-agenda-skip)
5553 (if (and (match-end 1)
5554 (not (= d1 (org-time-string-to-absolute
5555 (match-string 1) d1 nil show-all
5556 (current-buffer) b0))))
5557 (throw :skip nil))
5558 (if (and e3
5559 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5560 (throw :skip nil))
5561 (setq tmp (buffer-substring (max (point-min)
5562 (- b0 org-ds-keyword-length))
5564 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5565 inactivep (= (char-after b0) ?\[)
5566 deadlinep (string-match org-deadline-regexp tmp)
5567 scheduledp (string-match org-scheduled-regexp tmp)
5568 closedp (and org-agenda-include-inactive-timestamps
5569 (string-match org-closed-string tmp))
5570 clockp (and org-agenda-include-inactive-timestamps
5571 (or (string-match org-clock-string tmp)
5572 (string-match "]-+\\'" tmp)))
5573 warntime (get-text-property (point) 'org-appt-warntime)
5574 donep (member todo-state org-done-keywords))
5575 (if (or scheduledp deadlinep closedp clockp
5576 (and donep org-agenda-skip-timestamp-if-done))
5577 (throw :skip t))
5578 (if (string-match ">" timestr)
5579 ;; substring should only run to end of time stamp
5580 (setq timestr (substring timestr 0 (match-end 0))))
5581 (setq marker (org-agenda-new-marker b0)
5582 category (org-get-category b0)
5583 category-pos (get-text-property b0 'org-category-position))
5584 (save-excursion
5585 (if (not (re-search-backward org-outline-regexp-bol nil t))
5586 (throw :skip nil)
5587 (goto-char (match-beginning 0))
5588 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5589 (assoc (point) deadline-position-alist))
5590 (throw :skip nil))
5591 (setq hdmarker (org-agenda-new-marker)
5592 inherited-tags
5593 (or (eq org-agenda-show-inherited-tags 'always)
5594 (and (listp org-agenda-show-inherited-tags)
5595 (memq 'agenda org-agenda-show-inherited-tags))
5596 (and (eq org-agenda-show-inherited-tags t)
5597 (or (eq org-agenda-use-tag-inheritance t)
5598 (memq 'agenda org-agenda-use-tag-inheritance))))
5599 tags (org-get-tags-at nil (not inherited-tags))
5600 level (make-string (org-reduced-level (org-outline-level)) ? ))
5601 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5602 (setq head (or (match-string 1) ""))
5603 (setq txt (org-agenda-format-item
5604 (if inactivep org-agenda-inactive-leader nil)
5605 head level category tags timestr
5606 remove-re habitp)))
5607 (setq priority (org-get-priority txt))
5608 (org-add-props txt props 'priority priority
5609 'org-marker marker 'org-hd-marker hdmarker
5610 'org-category category 'date date
5611 'level level
5612 'ts-date
5613 (ignore-errors (org-time-string-to-absolute timestr))
5614 'org-category-position category-pos
5615 'todo-state todo-state
5616 'warntime warntime
5617 'type "timestamp")
5618 (push txt ee))
5619 (if org-agenda-skip-additional-timestamps-same-entry
5620 (outline-next-heading)
5621 (goto-char end-of-match))))
5622 (nreverse ee)))
5624 (defun org-agenda-get-sexps ()
5625 "Return the sexp information for agenda display."
5626 (require 'diary-lib)
5627 (let* ((props (list 'face 'org-agenda-calendar-sexp
5628 'mouse-face 'highlight
5629 'help-echo
5630 (format "mouse-2 or RET jump to org file %s"
5631 (abbreviate-file-name buffer-file-name))))
5632 (regexp "^&?%%(")
5633 marker category extra category-pos level ee txt tags entry
5634 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5635 (goto-char (point-min))
5636 (while (re-search-forward regexp nil t)
5637 (catch :skip
5638 (org-agenda-skip)
5639 (setq beg (match-beginning 0))
5640 (goto-char (1- (match-end 0)))
5641 (setq b (point))
5642 (forward-sexp 1)
5643 (setq sexp (buffer-substring b (point)))
5644 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5645 (org-trim (match-string 1))
5646 ""))
5647 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5648 (when result
5649 (setq marker (org-agenda-new-marker beg)
5650 level (make-string (org-reduced-level (org-outline-level)) ? )
5651 category (org-get-category beg)
5652 category-pos (get-text-property beg 'org-category-position)
5653 inherited-tags
5654 (or (eq org-agenda-show-inherited-tags 'always)
5655 (and (listp org-agenda-show-inherited-tags)
5656 (memq 'agenda org-agenda-show-inherited-tags))
5657 (and (eq org-agenda-show-inherited-tags t)
5658 (or (eq org-agenda-use-tag-inheritance t)
5659 (memq 'agenda org-agenda-use-tag-inheritance))))
5660 tags (org-get-tags-at nil (not inherited-tags))
5661 todo-state (org-get-todo-state)
5662 warntime (get-text-property (point) 'org-appt-warntime)
5663 extra nil)
5665 (dolist (r (if (stringp result)
5666 (list result)
5667 result)) ;; we expect a list here
5668 (when (and org-agenda-diary-sexp-prefix
5669 (string-match org-agenda-diary-sexp-prefix r))
5670 (setq extra (match-string 0 r)
5671 r (replace-match "" nil nil r)))
5672 (if (string-match "\\S-" r)
5673 (setq txt r)
5674 (setq txt "SEXP entry returned empty string"))
5675 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5676 (org-add-props txt props 'org-marker marker
5677 'org-category category 'date date 'todo-state todo-state
5678 'org-category-position category-pos 'tags tags
5679 'level level
5680 'type "sexp" 'warntime warntime)
5681 (push txt ee)))))
5682 (nreverse ee)))
5684 ;; Calendar sanity: define some functions that are independent of
5685 ;; `calendar-date-style'.
5686 ;; Normally I would like to use ISO format when calling the diary functions,
5687 ;; but to make sure we still have Emacs 22 compatibility we bind
5688 ;; also `european-calendar-style' and use european format
5689 (defun org-anniversary (year month day &optional mark)
5690 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5691 (org-no-warnings
5692 (let ((calendar-date-style 'european) (european-calendar-style t))
5693 (diary-anniversary day month year mark))))
5694 (defun org-cyclic (N year month day &optional mark)
5695 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5696 (org-no-warnings
5697 (let ((calendar-date-style 'european) (european-calendar-style t))
5698 (diary-cyclic N day month year mark))))
5699 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5700 "Like `diary-block', but with fixed (ISO) order of arguments."
5701 (org-no-warnings
5702 (let ((calendar-date-style 'european) (european-calendar-style t))
5703 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5704 (defun org-date (year month day &optional mark)
5705 "Like `diary-date', but with fixed (ISO) order of arguments."
5706 (org-no-warnings
5707 (let ((calendar-date-style 'european) (european-calendar-style t))
5708 (diary-date day month year mark))))
5710 ;; Define the` org-class' function
5711 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5712 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5713 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5714 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5715 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5716 `holidays', then any date that is known by the Emacs calendar to be a
5717 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5718 then those holidays will be skipped."
5719 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5720 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5721 (d (calendar-absolute-from-gregorian date))
5722 (h (when skip-weeks (calendar-check-holidays date))))
5723 (and
5724 (<= date1 d)
5725 (<= d date2)
5726 (= (calendar-day-of-week date) dayname)
5727 (or (not skip-weeks)
5728 (progn
5729 (require 'cal-iso)
5730 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5731 (not (or (and h (memq 'holidays skip-weeks))
5732 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5733 entry)))
5735 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5736 "Like `org-class', but honor `calendar-date-style'.
5737 The order of the first 2 times 3 arguments depends on the variable
5738 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5739 So for American calendars, give this as MONTH DAY YEAR, for European as
5740 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5741 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5742 is any number of ISO weeks in the block period for which the item should
5743 be skipped.
5745 This function is here only for backward compatibility and it is deprecated,
5746 please use `org-class' instead."
5747 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5748 (date2 (org-order-calendar-date-args m2 d2 y2)))
5749 (org-class
5750 (nth 2 date1) (car date1) (nth 1 date1)
5751 (nth 2 date2) (car date2) (nth 1 date2)
5752 dayname skip-weeks)))
5753 (make-obsolete 'org-diary-class 'org-class "")
5755 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5756 (defalias 'org-get-closed 'org-agenda-get-progress)
5757 (defun org-agenda-get-progress ()
5758 "Return the logged TODO entries for agenda display."
5759 (let* ((props (list 'mouse-face 'highlight
5760 'org-not-done-regexp org-not-done-regexp
5761 'org-todo-regexp org-todo-regexp
5762 'org-complex-heading-regexp org-complex-heading-regexp
5763 'help-echo
5764 (format "mouse-2 or RET jump to org file %s"
5765 (abbreviate-file-name buffer-file-name))))
5766 (items (if (consp org-agenda-show-log-scoped)
5767 org-agenda-show-log-scoped
5768 (if (eq org-agenda-show-log-scoped 'clockcheck)
5769 '(clock)
5770 org-agenda-log-mode-items)))
5771 (parts
5772 (delq nil
5773 (list
5774 (if (memq 'closed items) (concat "\\<" org-closed-string))
5775 (if (memq 'clock items) (concat "\\<" org-clock-string))
5776 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5777 (parts-re (if parts (mapconcat 'identity parts "\\|")
5778 (error "`org-agenda-log-mode-items' is empty")))
5779 (regexp (concat
5780 "\\(" parts-re "\\)"
5781 " *\\["
5782 (regexp-quote
5783 (substring
5784 (format-time-string
5785 (car org-time-stamp-formats)
5786 (apply 'encode-time ; DATE bound by calendar
5787 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5788 1 11))))
5789 (org-agenda-search-headline-for-time nil)
5790 marker hdmarker priority category category-pos level tags closedp
5791 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5792 (goto-char (point-min))
5793 (while (re-search-forward regexp nil t)
5794 (catch :skip
5795 (org-agenda-skip)
5796 (setq marker (org-agenda-new-marker (match-beginning 0))
5797 closedp (equal (match-string 1) org-closed-string)
5798 statep (equal (string-to-char (match-string 1)) ?-)
5799 clockp (not (or closedp statep))
5800 state (and statep (match-string 2))
5801 category (org-get-category (match-beginning 0))
5802 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5803 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5804 (when (string-match "\\]" timestr)
5805 ;; substring should only run to end of time stamp
5806 (setq rest (substring timestr (match-end 0))
5807 timestr (substring timestr 0 (match-end 0)))
5808 (if (and (not closedp) (not statep)
5809 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5810 rest))
5811 (progn (setq timestr (concat (substring timestr 0 -1)
5812 "-" (match-string 1 rest) "]"))
5813 (setq clocked (match-string 2 rest)))
5814 (setq clocked "-")))
5815 (save-excursion
5816 (setq extra
5817 (cond
5818 ((not org-agenda-log-mode-add-notes) nil)
5819 (statep
5820 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5821 (match-string 1)))
5822 (clockp
5823 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5824 (match-string 1)))))
5825 (if (not (re-search-backward org-outline-regexp-bol nil t))
5826 (throw :skip nil)
5827 (goto-char (match-beginning 0))
5828 (setq hdmarker (org-agenda-new-marker)
5829 inherited-tags
5830 (or (eq org-agenda-show-inherited-tags 'always)
5831 (and (listp org-agenda-show-inherited-tags)
5832 (memq 'todo org-agenda-show-inherited-tags))
5833 (and (eq org-agenda-show-inherited-tags t)
5834 (or (eq org-agenda-use-tag-inheritance t)
5835 (memq 'todo org-agenda-use-tag-inheritance))))
5836 tags (org-get-tags-at nil (not inherited-tags))
5837 level (make-string (org-reduced-level (org-outline-level)) ? ))
5838 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5839 (setq txt (match-string 1))
5840 (when extra
5841 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5842 (setq txt (concat (substring txt 0 (match-beginning 1))
5843 " - " extra " " (match-string 2 txt)))
5844 (setq txt (concat txt " - " extra))))
5845 (setq txt (org-agenda-format-item
5846 (cond
5847 (closedp "Closed: ")
5848 (statep (concat "State: (" state ")"))
5849 (t (concat "Clocked: (" clocked ")")))
5850 txt level category tags timestr)))
5851 (setq priority 100000)
5852 (org-add-props txt props
5853 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5854 'priority priority 'org-category category
5855 'org-category-position category-pos
5856 'level level
5857 'type "closed" 'date date
5858 'undone-face 'org-warning 'done-face 'org-agenda-done)
5859 (push txt ee))
5860 (goto-char (point-at-eol))))
5861 (nreverse ee)))
5863 (defun org-agenda-show-clocking-issues ()
5864 "Add overlays, showing issues with clocking.
5865 See also the user option `org-agenda-clock-consistency-checks'."
5866 (interactive)
5867 (let* ((org-time-clocksum-use-effort-durations nil)
5868 (pl org-agenda-clock-consistency-checks)
5869 (re (concat "^[ \t]*"
5870 org-clock-string
5871 "[ \t]+"
5872 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5873 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5874 (tlstart 0.)
5875 (tlend 0.)
5876 (maxtime (org-hh:mm-string-to-minutes
5877 (or (plist-get pl :max-duration) "24:00")))
5878 (mintime (org-hh:mm-string-to-minutes
5879 (or (plist-get pl :min-duration) 0)))
5880 (maxgap (org-hh:mm-string-to-minutes
5881 ;; default 30:00 means never complain
5882 (or (plist-get pl :max-gap) "30:00")))
5883 (gapok (mapcar 'org-hh:mm-string-to-minutes
5884 (plist-get pl :gap-ok-around)))
5885 (def-face (or (plist-get pl :default-face)
5886 '((:background "DarkRed") (:foreground "white"))))
5887 issue face m te ts dt ov)
5888 (goto-char (point-min))
5889 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5890 (setq issue nil face def-face)
5891 (catch 'next
5892 (setq m (org-get-at-bol 'org-marker)
5893 te nil ts nil)
5894 (unless (and m (markerp m))
5895 (setq issue "No valid clock line") (throw 'next t))
5896 (org-with-point-at m
5897 (save-excursion
5898 (goto-char (point-at-bol))
5899 (unless (looking-at re)
5900 (error "No valid Clock line")
5901 (throw 'next t))
5902 (unless (match-end 3)
5903 (setq issue "No end time"
5904 face (or (plist-get pl :no-end-time-face) face))
5905 (throw 'next t))
5906 (setq ts (match-string 1)
5907 te (match-string 3)
5908 ts (org-float-time
5909 (apply 'encode-time (org-parse-time-string ts)))
5910 te (org-float-time
5911 (apply 'encode-time (org-parse-time-string te)))
5912 dt (- te ts))))
5913 (cond
5914 ((> dt (* 60 maxtime))
5915 ;; a very long clocking chunk
5916 (setq issue (format "Clocking interval is very long: %s"
5917 (org-minutes-to-clocksum-string
5918 (floor (/ (float dt) 60.))))
5919 face (or (plist-get pl :long-face) face)))
5920 ((< dt (* 60 mintime))
5921 ;; a very short clocking chunk
5922 (setq issue (format "Clocking interval is very short: %s"
5923 (org-minutes-to-clocksum-string
5924 (floor (/ (float dt) 60.))))
5925 face (or (plist-get pl :short-face) face)))
5926 ((and (> tlend 0) (< ts tlend))
5927 ;; Two clock entries are overlapping
5928 (setq issue (format "Clocking overlap: %d minutes"
5929 (/ (- tlend ts) 60))
5930 face (or (plist-get pl :overlap-face) face)))
5931 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5932 ;; There is a gap, lets see if we need to report it
5933 (unless (org-agenda-check-clock-gap tlend ts gapok)
5934 (setq issue (format "Clocking gap: %d minutes"
5935 (/ (- ts tlend) 60))
5936 face (or (plist-get pl :gap-face) face))))
5937 (t nil)))
5938 (setq tlend (or te tlend) tlstart (or ts tlstart))
5939 (when issue
5940 ;; OK, there was some issue, add an overlay to show the issue
5941 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5942 (overlay-put ov 'before-string
5943 (concat
5944 (org-add-props
5945 (format "%-43s" (concat " " issue))
5947 'face face)
5948 "\n"))
5949 (overlay-put ov 'evaporate t)))))
5951 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5952 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5953 (catch 'exit
5954 (unless ok-list
5955 ;; there are no OK times for gaps...
5956 (throw 'exit nil))
5957 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5958 ;; This is more than 24 hours, so it is OK.
5959 ;; because we have at least one OK time, that must be in the
5960 ;; 24 hour interval.
5961 (throw 'exit t))
5962 ;; We have a shorter gap.
5963 ;; Now we have to get the minute of the day when these times are
5964 (let* ((t1dec (decode-time (seconds-to-time t1)))
5965 (t2dec (decode-time (seconds-to-time t2)))
5966 ;; compute the minute on the day
5967 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5968 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5969 (when (< min2 min1)
5970 ;; if min2 is smaller than min1, this means it is on the next day.
5971 ;; Wrap it to after midnight.
5972 (setq min2 (+ min2 1440)))
5973 ;; Now check if any of the OK times is in the gap
5974 (mapc (lambda (x)
5975 ;; Wrap the time to after midnight if necessary
5976 (if (< x min1) (setq x (+ x 1440)))
5977 ;; Check if in interval
5978 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5979 ok-list)
5980 ;; Nope, this gap is not OK
5981 nil)))
5983 (defun org-agenda-get-deadlines (&optional with-hour)
5984 "Return the deadline information for agenda display.
5985 When WITH-HOUR is non-nil, only return deadlines with an hour
5986 specification like [h]h:mm."
5987 (let* ((props (list 'mouse-face 'highlight
5988 'org-not-done-regexp org-not-done-regexp
5989 'org-todo-regexp org-todo-regexp
5990 'org-complex-heading-regexp org-complex-heading-regexp
5991 'help-echo
5992 (format "mouse-2 or RET jump to org file %s"
5993 (abbreviate-file-name buffer-file-name))))
5994 (regexp (if with-hour
5995 org-deadline-time-hour-regexp
5996 org-deadline-time-regexp))
5997 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5998 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5999 (dl0 (car org-agenda-deadline-leaders))
6000 (dl1 (nth 1 org-agenda-deadline-leaders))
6001 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6002 d2 diff dfrac wdays pos pos1 category category-pos level
6003 tags suppress-prewarning ee txt head face s todo-state
6004 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6005 (goto-char (point-min))
6006 (while (re-search-forward regexp nil t)
6007 (catch :skip
6008 (org-agenda-skip)
6009 (setq s (match-string 1)
6010 txt nil
6011 pos (1- (match-beginning 1))
6012 todo-state (save-match-data (org-get-todo-state))
6013 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6014 (member todo-state
6015 org-agenda-repeating-timestamp-show-all))
6016 d2 (org-time-string-to-absolute
6017 s d1 'past show-all (current-buffer) pos)
6018 diff (- d2 d1))
6019 (setq suppress-prewarning
6020 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6021 (let ((item (buffer-substring (point-at-bol)
6022 (point-at-eol))))
6023 (save-match-data
6024 (and (string-match
6025 org-scheduled-time-regexp item)
6026 (match-string 1 item)))))))
6027 (cond
6028 ((not ds) nil)
6029 ;; The current item has a scheduled date (in ds), so
6030 ;; evaluate its prewarning lead time.
6031 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6032 ;; Use global prewarning-restart lead time.
6033 org-agenda-skip-deadline-prewarning-if-scheduled)
6034 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6035 'pre-scheduled)
6036 ;; Set prewarning to no earlier than scheduled.
6037 (min (- d2 (org-time-string-to-absolute
6038 ds d1 'past show-all (current-buffer) pos))
6039 org-deadline-warning-days))
6040 ;; Set prewarning to deadline.
6041 (t 0))))
6042 (setq wdays (if suppress-prewarning
6043 (let ((org-deadline-warning-days suppress-prewarning))
6044 (org-get-wdays s))
6045 (org-get-wdays s))
6046 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6047 upcomingp (and todayp (> diff 0)))
6048 ;; When to show a deadline in the calendar:
6049 ;; If the expiration is within wdays warning time.
6050 ;; Past-due deadlines are only shown on the current date
6051 (if (and (or (and (<= diff wdays)
6052 (and todayp (not org-agenda-only-exact-dates)))
6053 (= diff 0)))
6054 (save-excursion
6055 ;; (setq todo-state (org-get-todo-state))
6056 (setq donep (member todo-state org-done-keywords))
6057 (if (and donep
6058 (or org-agenda-skip-deadline-if-done
6059 (not (= diff 0))))
6060 (setq txt nil)
6061 (setq category (org-get-category)
6062 warntime (get-text-property (point) 'org-appt-warntime)
6063 category-pos (get-text-property (point) 'org-category-position))
6064 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6065 (throw :skip nil)
6066 (goto-char (match-end 0))
6067 (setq pos1 (match-beginning 0))
6068 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6069 (setq inherited-tags
6070 (or (eq org-agenda-show-inherited-tags 'always)
6071 (and (listp org-agenda-show-inherited-tags)
6072 (memq 'agenda org-agenda-show-inherited-tags))
6073 (and (eq org-agenda-show-inherited-tags t)
6074 (or (eq org-agenda-use-tag-inheritance t)
6075 (memq 'agenda org-agenda-use-tag-inheritance))))
6076 tags (org-get-tags-at pos1 (not inherited-tags)))
6077 (setq head (buffer-substring
6078 (point)
6079 (progn (skip-chars-forward "^\r\n")
6080 (point))))
6081 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6082 (setq timestr
6083 (concat (substring s (match-beginning 1)) " "))
6084 (setq timestr 'time))
6085 (setq txt (org-agenda-format-item
6086 (cond ((= diff 0) dl0)
6087 ((> diff 0)
6088 (if (functionp dl1)
6089 (funcall dl1 diff date)
6090 (format dl1 diff)))
6092 (if (functionp dl2)
6093 (funcall dl2 diff date)
6094 (format dl2 (if (string= dl2 dl1)
6095 diff (abs diff))))))
6096 head level category tags
6097 (if (not (= diff 0)) nil timestr)))))
6098 (when txt
6099 (setq face (org-agenda-deadline-face dfrac))
6100 (org-add-props txt props
6101 'org-marker (org-agenda-new-marker pos)
6102 'warntime warntime
6103 'level level
6104 'ts-date d2
6105 'org-hd-marker (org-agenda-new-marker pos1)
6106 'priority (+ (- diff)
6107 (org-get-priority txt))
6108 'org-category category
6109 'org-category-position category-pos
6110 'todo-state todo-state
6111 'type (if upcomingp "upcoming-deadline" "deadline")
6112 'date (if upcomingp date d2)
6113 'face (if donep 'org-agenda-done face)
6114 'undone-face face 'done-face 'org-agenda-done)
6115 (push txt ee))))))
6116 (nreverse ee)))
6118 (defun org-agenda-deadline-face (fraction)
6119 "Return the face to displaying a deadline item.
6120 FRACTION is what fraction of the head-warning time has passed."
6121 (let ((faces org-agenda-deadline-faces) f)
6122 (catch 'exit
6123 (while (setq f (pop faces))
6124 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6126 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6127 "Return the scheduled information for agenda display.
6128 When WITH-HOUR is non-nil, only return scheduled items with
6129 an hour specification like [h]h:mm."
6130 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6131 'org-todo-regexp org-todo-regexp
6132 'org-complex-heading-regexp org-complex-heading-regexp
6133 'done-face 'org-agenda-done
6134 'mouse-face 'highlight
6135 'help-echo
6136 (format "mouse-2 or RET jump to org file %s"
6137 (abbreviate-file-name buffer-file-name))))
6138 (regexp (if with-hour
6139 org-scheduled-time-hour-regexp
6140 org-scheduled-time-regexp))
6141 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6142 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6144 (deadline-position-alist
6145 (mapcar (lambda (a) (and (setq mm (get-text-property
6146 0 'org-hd-marker a))
6147 (cons (marker-position mm) a)))
6148 deadline-results))
6149 d2 diff pos pos1 category category-pos level tags donep
6150 ee txt head pastschedp todo-state face timestr s habitp show-all
6151 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6152 ddays)
6153 (goto-char (point-min))
6154 (while (re-search-forward regexp nil t)
6155 (catch :skip
6156 (org-agenda-skip)
6157 (setq s (match-string 1)
6158 txt nil
6159 pos (1- (match-beginning 1))
6160 todo-state (save-match-data (org-get-todo-state))
6161 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6162 (member todo-state
6163 org-agenda-repeating-timestamp-show-all))
6164 d2 (org-time-string-to-absolute
6165 s d1 'past show-all (current-buffer) pos)
6166 diff (- d2 d1)
6167 warntime (get-text-property (point) 'org-appt-warntime))
6168 (setq pastschedp (and todayp (< diff 0)))
6169 (setq did-habit-check-p nil)
6170 (setq suppress-delay
6171 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6172 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6173 (save-match-data
6174 (and (string-match
6175 org-deadline-time-regexp item)
6176 (match-string 1 item)))))))
6177 (cond
6178 ((not ds) nil)
6179 ;; The current item has a deadline date (in ds), so
6180 ;; evaluate its delay time.
6181 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6182 ;; Use global delay time.
6183 (- org-agenda-skip-scheduled-delay-if-deadline))
6184 ((eq org-agenda-skip-scheduled-delay-if-deadline
6185 'post-deadline)
6186 ;; Set delay to no later than deadline.
6187 (min (- d2 (org-time-string-to-absolute
6188 ds d1 'past show-all (current-buffer) pos))
6189 org-scheduled-delay-days))
6190 (t 0))))
6191 (setq ddays (if suppress-delay
6192 (let ((org-scheduled-delay-days suppress-delay))
6193 (org-get-wdays s t t))
6194 (org-get-wdays s t)))
6195 ;; Use a delay of 0 when there is a repeater and the delay is
6196 ;; of the form --3d
6197 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6198 (< (org-time-string-to-absolute s)
6199 (org-time-string-to-absolute
6200 s d2 'past nil (current-buffer) pos)))
6201 (setq ddays 0))
6202 ;; When to show a scheduled item in the calendar:
6203 ;; If it is on or past the date.
6204 (when (or (and (> ddays 0) (= diff (- ddays)))
6205 (and (zerop ddays) (= diff 0))
6206 (and (< (+ diff ddays) 0)
6207 (< (abs diff) org-scheduled-past-days)
6208 (and todayp (not org-agenda-only-exact-dates)))
6209 ;; org-is-habit-p uses org-entry-get, which is expansive
6210 ;; so we go extra mile to only call it once
6211 (and todayp
6212 (boundp 'org-habit-show-all-today)
6213 org-habit-show-all-today
6214 (setq did-habit-check-p t)
6215 (setq habitp (and (functionp 'org-is-habit-p)
6216 (org-is-habit-p)))))
6217 (save-excursion
6218 (setq donep (member todo-state org-done-keywords))
6219 (if (and donep
6220 (or org-agenda-skip-scheduled-if-done
6221 (not (= diff 0))
6222 (and (functionp 'org-is-habit-p)
6223 (org-is-habit-p))))
6224 (setq txt nil)
6225 (setq habitp (if did-habit-check-p habitp
6226 (and (functionp 'org-is-habit-p)
6227 (org-is-habit-p))))
6228 (setq category (org-get-category)
6229 category-pos (get-text-property (point) 'org-category-position))
6230 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6231 'repeated-after-deadline)
6232 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6233 (throw :skip nil))
6234 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6235 (throw :skip nil)
6236 (goto-char (match-end 0))
6237 (setq pos1 (match-beginning 0))
6238 (if habitp
6239 (if (or (not org-habit-show-habits)
6240 (and (not todayp)
6241 (boundp 'org-habit-show-habits-only-for-today)
6242 org-habit-show-habits-only-for-today))
6243 (throw :skip nil))
6244 (if (and
6245 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6246 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6247 pastschedp))
6248 (setq mm (assoc pos1 deadline-position-alist)))
6249 (throw :skip nil)))
6250 (setq inherited-tags
6251 (or (eq org-agenda-show-inherited-tags 'always)
6252 (and (listp org-agenda-show-inherited-tags)
6253 (memq 'agenda org-agenda-show-inherited-tags))
6254 (and (eq org-agenda-show-inherited-tags t)
6255 (or (eq org-agenda-use-tag-inheritance t)
6256 (memq 'agenda org-agenda-use-tag-inheritance))))
6258 tags (org-get-tags-at nil (not inherited-tags)))
6259 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6260 (setq head (buffer-substring
6261 (point)
6262 (progn (skip-chars-forward "^\r\n") (point))))
6263 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6264 (setq timestr
6265 (concat (substring s (match-beginning 1)) " "))
6266 (setq timestr 'time))
6267 (setq txt (org-agenda-format-item
6268 (if (= diff 0)
6269 (car org-agenda-scheduled-leaders)
6270 (format (nth 1 org-agenda-scheduled-leaders)
6271 (- 1 diff)))
6272 head level category tags
6273 (if (not (= diff 0)) nil timestr)
6274 nil habitp))))
6275 (when txt
6276 (setq face
6277 (cond
6278 ((and (not habitp) pastschedp)
6279 'org-scheduled-previously)
6280 (todayp 'org-scheduled-today)
6281 (t 'org-scheduled))
6282 habitp (and habitp (org-habit-parse-todo)))
6283 (org-add-props txt props
6284 'undone-face face
6285 'face (if donep 'org-agenda-done face)
6286 'org-marker (org-agenda-new-marker pos)
6287 'org-hd-marker (org-agenda-new-marker pos1)
6288 'type (if pastschedp "past-scheduled" "scheduled")
6289 'date (if pastschedp d2 date)
6290 'ts-date d2
6291 'warntime warntime
6292 'level level
6293 'priority (if habitp
6294 (org-habit-get-priority habitp)
6295 (+ 94 (- 5 diff) (org-get-priority txt)))
6296 'org-category category
6297 'category-position category-pos
6298 'org-habit-p habitp
6299 'todo-state todo-state)
6300 (push txt ee))))))
6301 (nreverse ee)))
6303 (defun org-agenda-get-blocks ()
6304 "Return the date-range information for agenda display."
6305 (let* ((props (list 'face nil
6306 'org-not-done-regexp org-not-done-regexp
6307 'org-todo-regexp org-todo-regexp
6308 'org-complex-heading-regexp org-complex-heading-regexp
6309 'mouse-face 'highlight
6310 'help-echo
6311 (format "mouse-2 or RET jump to org file %s"
6312 (abbreviate-file-name buffer-file-name))))
6313 (regexp org-tr-regexp)
6314 (d0 (calendar-absolute-from-gregorian date))
6315 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6316 level todo-state tags pos head donep inherited-tags)
6317 (goto-char (point-min))
6318 (while (re-search-forward regexp nil t)
6319 (catch :skip
6320 (org-agenda-skip)
6321 (setq pos (point))
6322 (let ((start-time (match-string 1))
6323 (end-time (match-string 2)))
6324 (setq s1 (match-string 1)
6325 s2 (match-string 2)
6326 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6327 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6328 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6329 ;; Only allow days between the limits, because the normal
6330 ;; date stamps will catch the limits.
6331 (save-excursion
6332 (setq todo-state (org-get-todo-state))
6333 (setq donep (member todo-state org-done-keywords))
6334 (if (and donep org-agenda-skip-timestamp-if-done)
6335 (throw :skip t))
6336 (setq marker (org-agenda-new-marker (point)))
6337 (setq category (org-get-category)
6338 category-pos (get-text-property (point) 'org-category-position))
6339 (if (not (re-search-backward org-outline-regexp-bol nil t))
6340 (throw :skip nil)
6341 (goto-char (match-beginning 0))
6342 (setq hdmarker (org-agenda-new-marker (point))
6343 inherited-tags
6344 (or (eq org-agenda-show-inherited-tags 'always)
6345 (and (listp org-agenda-show-inherited-tags)
6346 (memq 'agenda org-agenda-show-inherited-tags))
6347 (and (eq org-agenda-show-inherited-tags t)
6348 (or (eq org-agenda-use-tag-inheritance t)
6349 (memq 'agenda org-agenda-use-tag-inheritance))))
6351 tags (org-get-tags-at nil (not inherited-tags)))
6352 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6353 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6354 (setq head (match-string 1))
6355 (let ((remove-re
6356 (if org-agenda-remove-timeranges-from-blocks
6357 (concat
6358 "<" (regexp-quote s1) ".*?>"
6359 "--"
6360 "<" (regexp-quote s2) ".*?>")
6361 nil)))
6362 (setq txt (org-agenda-format-item
6363 (format
6364 (nth (if (= d1 d2) 0 1)
6365 org-agenda-timerange-leaders)
6366 (1+ (- d0 d1)) (1+ (- d2 d1)))
6367 head level category tags
6368 (cond ((and (= d1 d0) (= d2 d0))
6369 (concat "<" start-time ">--<" end-time ">"))
6370 ((= d1 d0)
6371 (concat "<" start-time ">"))
6372 ((= d2 d0)
6373 (concat "<" end-time ">")))
6374 remove-re))))
6375 (org-add-props txt props
6376 'org-marker marker 'org-hd-marker hdmarker
6377 'type "block" 'date date
6378 'level level
6379 'todo-state todo-state
6380 'priority (org-get-priority txt) 'org-category category
6381 'org-category-position category-pos)
6382 (push txt ee))))
6383 (goto-char pos)))
6384 ;; Sort the entries by expiration date.
6385 (nreverse ee)))
6387 ;;; Agenda presentation and sorting
6389 (defvar org-prefix-has-time nil
6390 "A flag, set by `org-compile-prefix-format'.
6391 The flag is set if the currently compiled format contains a `%t'.")
6392 (defvar org-prefix-has-tag nil
6393 "A flag, set by `org-compile-prefix-format'.
6394 The flag is set if the currently compiled format contains a `%T'.")
6395 (defvar org-prefix-has-effort nil
6396 "A flag, set by `org-compile-prefix-format'.
6397 The flag is set if the currently compiled format contains a `%e'.")
6398 (defvar org-prefix-has-breadcrumbs nil
6399 "A flag, set by `org-compile-prefix-format'.
6400 The flag is set if the currently compiled format contains a `%b'.")
6401 (defvar org-prefix-category-length nil
6402 "Used by `org-compile-prefix-format' to remember the category field width.")
6403 (defvar org-prefix-category-max-length nil
6404 "Used by `org-compile-prefix-format' to remember the category field width.")
6406 (defun org-agenda-get-category-icon (category)
6407 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6408 (dolist (entry org-agenda-category-icon-alist)
6409 (when (org-string-match-p (car entry) category)
6410 (if (listp (cadr entry))
6411 (return (cadr entry))
6412 (return (apply 'create-image (cdr entry)))))))
6414 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6415 remove-re habitp)
6416 "Format TXT to be inserted into the agenda buffer.
6417 In particular, add the prefix and corresponding text properties.
6419 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6420 LEVEL may be a string to replace the `%l' specifier.
6421 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6422 category taken from local variable or file name. It will replace the `%c'
6423 specifier in the format.
6424 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6425 TXT for sorting of this entry, and for the `%t' specifier in the format.
6426 When DOTIME is a string, this string is searched for a time before TXT is.
6427 TAGS can be the tags of the headline.
6428 Any match of REMOVE-RE will be removed from TXT."
6429 ;; We keep the org-prefix-* variable values along with a compiled
6430 ;; formatter, so that multiple agendas existing at the same time do
6431 ;; not step on each other toes.
6433 ;; It was inconvenient to make these variables buffer local in
6434 ;; Agenda buffers, because this function expects to be called with
6435 ;; the buffer where item comes from being current, and not agenda
6436 ;; buffer
6437 (let* ((bindings (car org-prefix-format-compiled))
6438 (formatter (cadr org-prefix-format-compiled)))
6439 (loop for (var value) in bindings
6440 do (set var value))
6441 (save-match-data
6442 ;; Diary entries sometimes have extra whitespace at the beginning
6443 (setq txt (org-trim txt))
6445 ;; Fix the tags part in txt
6446 (setq txt (org-agenda-fix-displayed-tags
6447 txt tags
6448 org-agenda-show-inherited-tags
6449 org-agenda-hide-tags-regexp))
6451 (let* ((category (or category
6452 (if (stringp org-category)
6453 org-category
6454 (and org-category (symbol-name org-category)))
6455 (if buffer-file-name
6456 (file-name-sans-extension
6457 (file-name-nondirectory buffer-file-name))
6458 "")))
6459 (category-icon (org-agenda-get-category-icon category))
6460 (category-icon (if category-icon
6461 (propertize " " 'display category-icon)
6462 ""))
6463 ;; time, tag, effort are needed for the eval of the prefix format
6464 (tag (if tags (nth (1- (length tags)) tags) ""))
6465 time effort neffort
6466 (ts (if dotime (concat
6467 (if (stringp dotime) dotime "")
6468 (and org-agenda-search-headline-for-time txt))))
6469 (time-of-day (and dotime (org-get-time-of-day ts)))
6470 stamp plain s0 s1 s2 rtn srp l
6471 duration thecategory breadcrumbs)
6472 (and (derived-mode-p 'org-mode) buffer-file-name
6473 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6474 (when (and dotime time-of-day)
6475 ;; Extract starting and ending time and move them to prefix
6476 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6477 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6478 (setq s0 (match-string 0 ts)
6479 srp (and stamp (match-end 3))
6480 s1 (match-string (if plain 1 2) ts)
6481 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6483 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6484 ;; them, we might want to remove them there to avoid duplication.
6485 ;; The user can turn this off with a variable.
6486 (if (and org-prefix-has-time
6487 org-agenda-remove-times-when-in-prefix (or stamp plain)
6488 (string-match (concat (regexp-quote s0) " *") txt)
6489 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6490 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6491 (= (match-beginning 0) 0)
6493 (setq txt (replace-match "" nil nil txt))))
6494 ;; Normalize the time(s) to 24 hour
6495 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6496 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6498 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6499 (let (org-time-clocksum-use-effort-durations)
6500 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6501 (setq s2
6502 (org-minutes-to-clocksum-string
6503 (+ (org-hh:mm-string-to-minutes s1)
6504 org-agenda-default-appointment-duration)))))
6506 ;; Compute the duration
6507 (when s2
6508 (setq duration (- (org-hh:mm-string-to-minutes s2)
6509 (org-hh:mm-string-to-minutes s1)))))
6511 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6512 txt)
6513 ;; Tags are in the string
6514 (if (or (eq org-agenda-remove-tags t)
6515 (and org-agenda-remove-tags
6516 org-prefix-has-tag))
6517 (setq txt (replace-match "" t t txt))
6518 (setq txt (replace-match
6519 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6520 (match-string 2 txt))
6521 t t txt))))
6522 (when (derived-mode-p 'org-mode)
6523 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6525 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6526 ;; current buffer, so move this check outside of above
6527 (if effort
6528 (setq neffort (org-duration-string-to-minutes effort)
6529 effort (setq effort (concat "[" effort "]")))
6530 ;; prevent erroring out with %e format when there is no effort
6531 (setq effort ""))
6533 (when remove-re
6534 (while (string-match remove-re txt)
6535 (setq txt (replace-match "" t t txt))))
6537 ;; Set org-heading property on `txt' to mark the start of the
6538 ;; heading.
6539 (add-text-properties 0 (length txt) '(org-heading t) txt)
6541 ;; Prepare the variables needed in the eval of the compiled format
6542 (if org-prefix-has-breadcrumbs
6543 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6544 (let ((s (org-display-outline-path nil nil "->" t)))
6545 (if (eq "" s) "" (concat s "->"))))))
6546 (setq time (cond (s2 (concat
6547 (org-agenda-time-of-day-to-ampm-maybe s1)
6548 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6549 (if org-agenda-timegrid-use-ampm " ")))
6550 (s1 (concat
6551 (org-agenda-time-of-day-to-ampm-maybe s1)
6552 (if org-agenda-timegrid-use-ampm
6553 "........ "
6554 "......")))
6555 (t ""))
6556 extra (or (and (not habitp) extra) "")
6557 category (if (symbolp category) (symbol-name category) category)
6558 thecategory (copy-sequence category)
6559 level (or level ""))
6560 (if (string-match org-bracket-link-regexp category)
6561 (progn
6562 (setq l (if (match-end 3)
6563 (- (match-end 3) (match-beginning 3))
6564 (- (match-end 1) (match-beginning 1))))
6565 (when (< l (or org-prefix-category-length 0))
6566 (setq category (copy-sequence category))
6567 (org-add-props category nil
6568 'extra-space (make-string
6569 (- org-prefix-category-length l 1) ?\ ))))
6570 (if (and org-prefix-category-max-length
6571 (>= (length category) org-prefix-category-max-length))
6572 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6573 ;; Evaluate the compiled format
6574 (setq rtn (concat (eval formatter) txt))
6576 ;; And finally add the text properties
6577 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6578 (org-add-props rtn nil
6579 'org-category (if thecategory (downcase thecategory) category)
6580 'tags (mapcar 'org-downcase-keep-props tags)
6581 'org-highest-priority org-highest-priority
6582 'org-lowest-priority org-lowest-priority
6583 'time-of-day time-of-day
6584 'duration duration
6585 'effort effort
6586 'effort-minutes neffort
6587 'breadcrumbs breadcrumbs
6588 'txt txt
6589 'level level
6590 'time time
6591 'extra extra
6592 'format org-prefix-format-compiled
6593 'dotime dotime)))))
6595 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6596 "Remove tags string from TXT, and add a modified list of tags.
6597 The modified list may contain inherited tags, and tags matched by
6598 `org-agenda-hide-tags-regexp' will be removed."
6599 (when (or add-inherited hide-re)
6600 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6601 (setq txt (substring txt 0 (match-beginning 0))))
6602 (setq tags
6603 (delq nil
6604 (mapcar (lambda (tg)
6605 (if (or (and hide-re (string-match hide-re tg))
6606 (and (not add-inherited)
6607 (get-text-property 0 'inherited tg)))
6609 tg))
6610 tags)))
6611 (when tags
6612 (let ((have-i (get-text-property 0 'inherited (car tags)))
6614 (setq txt (concat txt " :"
6615 (mapconcat
6616 (lambda (x)
6617 (setq i (get-text-property 0 'inherited x))
6618 (if (and have-i (not i))
6619 (progn
6620 (setq have-i nil)
6621 (concat ":" x))
6623 tags ":")
6624 (if have-i "::" ":"))))))
6625 txt)
6627 (defun org-downcase-keep-props (s)
6628 (let ((props (text-properties-at 0 s)))
6629 (setq s (downcase s))
6630 (add-text-properties 0 (length s) props s)
6633 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6635 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6636 "Add a time-grid for agenda items which need it.
6638 LIST is the list of agenda items formatted by `org-agenda-list'.
6639 NDAYS is the span of the current agenda view.
6640 TODAYP is `t' when the current agenda view is on today."
6641 (catch 'exit
6642 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6643 ((and todayp (member 'today (car org-agenda-time-grid))))
6644 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6645 ((member 'weekly (car org-agenda-time-grid)))
6646 (t (throw 'exit list)))
6647 (let* ((have (delq nil (mapcar
6648 (lambda (x) (get-text-property 1 'time-of-day x))
6649 list)))
6650 (string (nth 1 org-agenda-time-grid))
6651 (gridtimes (nth 2 org-agenda-time-grid))
6652 (req (car org-agenda-time-grid))
6653 (remove (member 'remove-match req))
6654 new time)
6655 (if (and (member 'require-timed req) (not have))
6656 ;; don't show empty grid
6657 (throw 'exit list))
6658 (while (setq time (pop gridtimes))
6659 (unless (and remove (member time have))
6660 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6661 (push (org-agenda-format-item
6662 nil string nil "" nil
6663 (concat (substring time 0 -2) ":" (substring time -2)))
6664 new)
6665 (put-text-property
6666 2 (length (car new)) 'face 'org-time-grid (car new))))
6667 (when (and todayp org-agenda-show-current-time-in-grid)
6668 (push (org-agenda-format-item
6669 nil org-agenda-current-time-string nil "" nil
6670 (format-time-string "%H:%M "))
6671 new)
6672 (put-text-property
6673 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6675 (if (member 'time-up org-agenda-sorting-strategy-selected)
6676 (append new list)
6677 (append list new)))))
6679 (defun org-compile-prefix-format (key)
6680 "Compile the prefix format into a Lisp form that can be evaluated.
6681 The resulting form and associated variable bindings is returned
6682 and stored in the variable `org-prefix-format-compiled'."
6683 (setq org-prefix-has-time nil
6684 org-prefix-has-tag nil
6685 org-prefix-category-length nil
6686 org-prefix-has-effort nil
6687 org-prefix-has-breadcrumbs nil)
6688 (let ((s (cond
6689 ((stringp org-agenda-prefix-format)
6690 org-agenda-prefix-format)
6691 ((assq key org-agenda-prefix-format)
6692 (cdr (assq key org-agenda-prefix-format)))
6693 (t " %-12:c%?-12t% s")))
6694 (start 0)
6695 varform vars var e c f opt)
6696 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6697 s start)
6698 (setq var (or (cdr (assoc (match-string 4 s)
6699 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6700 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6701 'eval)
6702 c (or (match-string 3 s) "")
6703 opt (match-beginning 1)
6704 start (1+ (match-beginning 0)))
6705 (if (equal var 'time) (setq org-prefix-has-time t))
6706 (if (equal var 'tag) (setq org-prefix-has-tag t))
6707 (if (equal var 'effort) (setq org-prefix-has-effort t))
6708 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6709 (setq f (concat "%" (match-string 2 s) "s"))
6710 (when (equal var 'category)
6711 (setq org-prefix-category-length
6712 (floor (abs (string-to-number (match-string 2 s)))))
6713 (setq org-prefix-category-max-length
6714 (let ((x (match-string 2 s)))
6715 (save-match-data
6716 (if (string-match "\\.[0-9]+" x)
6717 (string-to-number (substring (match-string 0 x) 1)))))))
6718 (if (eq var 'eval)
6719 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6720 (if opt
6721 (setq varform
6722 `(if (equal "" ,var)
6724 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6725 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6726 (setq s (replace-match "%s" t nil s))
6727 (push varform vars))
6728 (setq vars (nreverse vars))
6729 (with-current-buffer (or org-agenda-buffer (current-buffer))
6730 (setq org-prefix-format-compiled
6731 (list
6732 `((org-prefix-has-time ,org-prefix-has-time)
6733 (org-prefix-has-tag ,org-prefix-has-tag)
6734 (org-prefix-category-length ,org-prefix-category-length)
6735 (org-prefix-has-effort ,org-prefix-has-effort)
6736 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6737 `(format ,s ,@vars))))))
6739 (defun org-set-sorting-strategy (key)
6740 (if (symbolp (car org-agenda-sorting-strategy))
6741 ;; the old format
6742 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6743 (setq org-agenda-sorting-strategy-selected
6744 (or (cdr (assq key org-agenda-sorting-strategy))
6745 (cdr (assq 'agenda org-agenda-sorting-strategy))
6746 '(time-up category-keep priority-down)))))
6748 (defun org-get-time-of-day (s &optional string mod24)
6749 "Check string S for a time of day.
6750 If found, return it as a military time number between 0 and 2400.
6751 If not found, return nil.
6752 The optional STRING argument forces conversion into a 5 character wide string
6753 HH:MM."
6754 (save-match-data
6755 (when
6756 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6757 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6758 (let* ((h (string-to-number (match-string 1 s)))
6759 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6760 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6761 (am-p (equal ampm "am"))
6762 (h1 (cond ((not ampm) h)
6763 ((= h 12) (if am-p 0 12))
6764 (t (+ h (if am-p 0 12)))))
6765 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6766 (mod h1 24) h1))
6767 (t0 (+ (* 100 h2) m))
6768 (t1 (concat (if (>= h1 24) "+" " ")
6769 (if (and org-agenda-time-leading-zero
6770 (< t0 1000)) "0" "")
6771 (if (< t0 100) "0" "")
6772 (if (< t0 10) "0" "")
6773 (int-to-string t0))))
6774 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6776 (defvar org-agenda-before-sorting-filter-function nil
6777 "Function to be applied to agenda items prior to sorting.
6778 Prior to sorting also means just before they are inserted into the agenda.
6780 To aid sorting, you may revisit the original entries and add more text
6781 properties which will later be used by the sorting functions.
6783 The function should take a string argument, an agenda line.
6784 It has access to the text properties in that line, which contain among
6785 other things, the property `org-hd-marker' that points to the entry
6786 where the line comes from. Note that not all lines going into the agenda
6787 have this property, only most.
6789 The function should return the modified string. It is probably best
6790 to ONLY change text properties.
6792 You can also use this function as a filter, by returning nil for lines
6793 you don't want to have in the agenda at all. For this application, you
6794 could bind the variable in the options section of a custom command.")
6796 (defun org-agenda-finalize-entries (list &optional type)
6797 "Sort, limit and concatenate the LIST of agenda items.
6798 The optional argument TYPE tells the agenda type."
6799 (let ((max-effort (cond ((listp org-agenda-max-effort)
6800 (cdr (assoc type org-agenda-max-effort)))
6801 (t org-agenda-max-effort)))
6802 (max-todo (cond ((listp org-agenda-max-todos)
6803 (cdr (assoc type org-agenda-max-todos)))
6804 (t org-agenda-max-todos)))
6805 (max-tags (cond ((listp org-agenda-max-tags)
6806 (cdr (assoc type org-agenda-max-tags)))
6807 (t org-agenda-max-tags)))
6808 (max-entries (cond ((listp org-agenda-max-entries)
6809 (cdr (assoc type org-agenda-max-entries)))
6810 (t org-agenda-max-entries))) l)
6811 (when org-agenda-before-sorting-filter-function
6812 (setq list
6813 (delq nil
6814 (mapcar
6815 org-agenda-before-sorting-filter-function list))))
6816 (setq list (mapcar 'org-agenda-highlight-todo list)
6817 list (mapcar 'identity (sort list 'org-entries-lessp)))
6818 (when max-effort
6819 (setq list (org-agenda-limit-entries
6820 list 'effort-minutes max-effort 'identity)))
6821 (when max-todo
6822 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6823 (when max-tags
6824 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6825 (when max-entries
6826 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6827 (mapconcat 'identity list "\n")))
6829 (defun org-agenda-limit-entries (list prop limit &optional fn)
6830 "Limit the number of agenda entries."
6831 (let ((include (and limit (< limit 0))))
6832 (if limit
6833 (let ((fun (or fn (lambda (p) (if p 1))))
6834 (lim 0))
6835 (delq nil
6836 (mapcar
6837 (lambda (e)
6838 (let ((pval (funcall fun (get-text-property 1 prop e))))
6839 (if pval (setq lim (+ lim pval)))
6840 (cond ((and pval (<= lim (abs limit))) e)
6841 ((and include (not pval)) e))))
6842 list)))
6843 list)))
6845 (defun org-agenda-limit-interactively ()
6846 "In agenda, interactively limit entries to various maximums."
6847 (interactive)
6848 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6849 (num (string-to-number (read-from-minibuffer "How many? "))))
6850 (cond ((equal max ?e)
6851 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6852 ((equal max ?t)
6853 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6854 ((equal max ?T)
6855 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6856 ((equal max ?E)
6857 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6858 (org-agenda-fit-window-to-buffer))
6860 (defun org-agenda-highlight-todo (x)
6861 (let ((org-done-keywords org-done-keywords-for-agenda)
6862 (case-fold-search nil)
6864 (if (eq x 'line)
6865 (save-excursion
6866 (beginning-of-line 1)
6867 (setq re (org-get-at-bol 'org-todo-regexp))
6868 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6869 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6870 (add-text-properties (match-beginning 0) (match-end 1)
6871 (list 'face (org-get-todo-face 1)))
6872 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6873 (delete-region (match-beginning 1) (1- (match-end 0)))
6874 (goto-char (match-beginning 1))
6875 (insert (format org-agenda-todo-keyword-format s)))))
6876 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6877 (setq re (get-text-property 0 'org-todo-regexp x))
6878 (when (and re
6879 ;; Test `pl' because if there's no heading content,
6880 ;; there's no point matching to highlight. Note
6881 ;; that if we didn't test `pl' first, and there
6882 ;; happened to be no keyword from `org-todo-regexp'
6883 ;; on this heading line, then the `equal' comparison
6884 ;; afterwards would spuriously succeed in the case
6885 ;; where `pl' is nil -- causing an args-out-of-range
6886 ;; error when we try to add text properties to text
6887 ;; that isn't there.
6889 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6890 x pl) pl))
6891 (add-text-properties
6892 (or (match-end 1) (match-end 0)) (match-end 0)
6893 (list 'face (org-get-todo-face (match-string 2 x)))
6895 (when (match-end 1)
6896 (setq x (concat (substring x 0 (match-end 1))
6897 (format org-agenda-todo-keyword-format
6898 (match-string 2 x))
6899 (org-add-props " " (text-properties-at 0 x))
6900 (substring x (match-end 3)))))))
6901 x)))
6903 (defsubst org-cmp-priority (a b)
6904 "Compare the priorities of string A and B."
6905 (let ((pa (or (get-text-property 1 'priority a) 0))
6906 (pb (or (get-text-property 1 'priority b) 0)))
6907 (cond ((> pa pb) +1)
6908 ((< pa pb) -1))))
6910 (defsubst org-cmp-effort (a b)
6911 "Compare the effort values of string A and B."
6912 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6913 (ea (or (get-text-property 1 'effort-minutes a) def))
6914 (eb (or (get-text-property 1 'effort-minutes b) def)))
6915 (cond ((> ea eb) +1)
6916 ((< ea eb) -1))))
6918 (defsubst org-cmp-category (a b)
6919 "Compare the string values of categories of strings A and B."
6920 (let ((ca (or (get-text-property 1 'org-category a) ""))
6921 (cb (or (get-text-property 1 'org-category b) "")))
6922 (cond ((string-lessp ca cb) -1)
6923 ((string-lessp cb ca) +1))))
6925 (defsubst org-cmp-todo-state (a b)
6926 "Compare the todo states of strings A and B."
6927 (let* ((ma (or (get-text-property 1 'org-marker a)
6928 (get-text-property 1 'org-hd-marker a)))
6929 (mb (or (get-text-property 1 'org-marker b)
6930 (get-text-property 1 'org-hd-marker b)))
6931 (fa (and ma (marker-buffer ma)))
6932 (fb (and mb (marker-buffer mb)))
6933 (todo-kwds
6934 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6935 (and fb (with-current-buffer fb org-todo-keywords-1))))
6936 (ta (or (get-text-property 1 'todo-state a) ""))
6937 (tb (or (get-text-property 1 'todo-state b) ""))
6938 (la (- (length (member ta todo-kwds))))
6939 (lb (- (length (member tb todo-kwds))))
6940 (donepa (member ta org-done-keywords-for-agenda))
6941 (donepb (member tb org-done-keywords-for-agenda)))
6942 (cond ((and donepa (not donepb)) -1)
6943 ((and (not donepa) donepb) +1)
6944 ((< la lb) -1)
6945 ((< lb la) +1))))
6947 (defsubst org-cmp-alpha (a b)
6948 "Compare the headlines, alphabetically."
6949 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6950 (plb (text-property-any 0 (length b) 'org-heading t b))
6951 (ta (and pla (substring a pla)))
6952 (tb (and plb (substring b plb))))
6953 (when pla
6954 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6955 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6956 (setq ta (substring ta (match-end 0))))
6957 (setq ta (downcase ta)))
6958 (when plb
6959 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6960 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6961 (setq tb (substring tb (match-end 0))))
6962 (setq tb (downcase tb)))
6963 (cond ((not ta) +1)
6964 ((not tb) -1)
6965 ((string-lessp ta tb) -1)
6966 ((string-lessp tb ta) +1))))
6968 (defsubst org-cmp-tag (a b)
6969 "Compare the string values of the first tags of A and B."
6970 (let ((ta (car (last (get-text-property 1 'tags a))))
6971 (tb (car (last (get-text-property 1 'tags b)))))
6972 (cond ((not ta) +1)
6973 ((not tb) -1)
6974 ((string-lessp ta tb) -1)
6975 ((string-lessp tb ta) +1))))
6977 (defsubst org-cmp-time (a b)
6978 "Compare the time-of-day values of strings A and B."
6979 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6980 (ta (or (get-text-property 1 'time-of-day a) def))
6981 (tb (or (get-text-property 1 'time-of-day b) def)))
6982 (cond ((< ta tb) -1)
6983 ((< tb ta) +1))))
6985 (defsubst org-cmp-ts (a b &optional type)
6986 "Compare the timestamps values of entries A and B.
6987 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6988 \"timestamp_ia\", compare within each of these type. When TYPE
6989 is the empty string, compare all timestamps without respect of
6990 their type."
6991 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6992 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6993 (get-text-property 1 'ts-date a)) def))
6994 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6995 (get-text-property 1 'ts-date b)) def)))
6996 (cond ((< ta tb) -1)
6997 ((< tb ta) +1))))
6999 (defsubst org-cmp-habit-p (a b)
7000 "Compare the todo states of strings A and B."
7001 (let ((ha (get-text-property 1 'org-habit-p a))
7002 (hb (get-text-property 1 'org-habit-p b)))
7003 (cond ((and ha (not hb)) -1)
7004 ((and (not ha) hb) +1))))
7006 (defun org-entries-lessp (a b)
7007 "Predicate for sorting agenda entries."
7008 ;; The following variables will be used when the form is evaluated.
7009 ;; So even though the compiler complains, keep them.
7010 (let* ((ss org-agenda-sorting-strategy-selected)
7011 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7012 (org-cmp-ts a b "")))
7013 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7014 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7015 (org-cmp-ts a b "scheduled")))
7016 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7017 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7018 (org-cmp-ts a b "deadline")))
7019 (deadline-down (if deadline-up (- deadline-up) nil))
7020 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7021 (org-cmp-ts a b "iatimestamp_ia")))
7022 (tsia-down (if tsia-up (- tsia-up) nil))
7023 (ts-up (and (org-em 'ts-up 'ts-down ss)
7024 (org-cmp-ts a b "timestamp")))
7025 (ts-down (if ts-up (- ts-up) nil))
7026 (time-up (and (org-em 'time-up 'time-down ss)
7027 (org-cmp-time a b)))
7028 (time-down (if time-up (- time-up) nil))
7029 (priority-up (and (org-em 'priority-up 'priority-down ss)
7030 (org-cmp-priority a b)))
7031 (priority-down (if priority-up (- priority-up) nil))
7032 (effort-up (and (org-em 'effort-up 'effort-down ss)
7033 (org-cmp-effort a b)))
7034 (effort-down (if effort-up (- effort-up) nil))
7035 (category-up (and (or (org-em 'category-up 'category-down ss)
7036 (memq 'category-keep ss))
7037 (org-cmp-category a b)))
7038 (category-down (if category-up (- category-up) nil))
7039 (category-keep (if category-up +1 nil))
7040 (tag-up (and (org-em 'tag-up 'tag-down ss)
7041 (org-cmp-tag a b)))
7042 (tag-down (if tag-up (- tag-up) nil))
7043 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7044 (org-cmp-todo-state a b)))
7045 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7046 (habit-up (and (org-em 'habit-up 'habit-down ss)
7047 (org-cmp-habit-p a b)))
7048 (habit-down (if habit-up (- habit-up) nil))
7049 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7050 (org-cmp-alpha a b)))
7051 (alpha-down (if alpha-up (- alpha-up) nil))
7052 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7053 user-defined-up user-defined-down)
7054 (if (and need-user-cmp org-agenda-cmp-user-defined
7055 (functionp org-agenda-cmp-user-defined))
7056 (setq user-defined-up
7057 (funcall org-agenda-cmp-user-defined a b)
7058 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7059 (cdr (assoc
7060 (eval (cons 'or org-agenda-sorting-strategy-selected))
7061 '((-1 . t) (1 . nil) (nil . nil))))))
7063 ;;; Agenda restriction lock
7065 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7066 "Overlay to mark the headline to which agenda commands are restricted.")
7067 (overlay-put org-agenda-restriction-lock-overlay
7068 'face 'org-agenda-restriction-lock)
7069 (overlay-put org-agenda-restriction-lock-overlay
7070 'help-echo "Agendas are currently limited to this subtree.")
7071 (org-detach-overlay org-agenda-restriction-lock-overlay)
7073 (defun org-agenda-set-restriction-lock (&optional type)
7074 "Set restriction lock for agenda, to current subtree or file.
7075 Restriction will be the file if TYPE is `file', or if type is the
7076 universal prefix '(4), or if the cursor is before the first headline
7077 in the file. Otherwise, restriction will be to the current subtree."
7078 (interactive "P")
7079 (and (equal type '(4)) (setq type 'file))
7080 (setq type (cond
7081 (type type)
7082 ((org-at-heading-p) 'subtree)
7083 ((condition-case nil (org-back-to-heading t) (error nil))
7084 'subtree)
7085 (t 'file)))
7086 (if (eq type 'subtree)
7087 (progn
7088 (setq org-agenda-restrict t)
7089 (setq org-agenda-overriding-restriction 'subtree)
7090 (put 'org-agenda-files 'org-restrict
7091 (list (buffer-file-name (buffer-base-buffer))))
7092 (org-back-to-heading t)
7093 (move-overlay org-agenda-restriction-lock-overlay
7094 (point)
7095 (if org-agenda-restriction-lock-highlight-subtree
7096 (save-excursion (org-end-of-subtree t t) (point))
7097 (point-at-eol)))
7098 (move-marker org-agenda-restrict-begin (point))
7099 (move-marker org-agenda-restrict-end
7100 (save-excursion (org-end-of-subtree t t)))
7101 (message "Locking agenda restriction to subtree"))
7102 (put 'org-agenda-files 'org-restrict
7103 (list (buffer-file-name (buffer-base-buffer))))
7104 (setq org-agenda-restrict nil)
7105 (setq org-agenda-overriding-restriction 'file)
7106 (move-marker org-agenda-restrict-begin nil)
7107 (move-marker org-agenda-restrict-end nil)
7108 (message "Locking agenda restriction to file"))
7109 (setq current-prefix-arg nil)
7110 (org-agenda-maybe-redo))
7112 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7113 "Remove the agenda restriction lock."
7114 (interactive "P")
7115 (org-detach-overlay org-agenda-restriction-lock-overlay)
7116 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7117 (setq org-agenda-overriding-restriction nil)
7118 (setq org-agenda-restrict nil)
7119 (put 'org-agenda-files 'org-restrict nil)
7120 (move-marker org-agenda-restrict-begin nil)
7121 (move-marker org-agenda-restrict-end nil)
7122 (setq current-prefix-arg nil)
7123 (message "Agenda restriction lock removed")
7124 (or noupdate (org-agenda-maybe-redo)))
7126 (defun org-agenda-maybe-redo ()
7127 "If there is any window showing the agenda view, update it."
7128 (let ((w (get-buffer-window org-agenda-buffer-name t))
7129 (w0 (selected-window)))
7130 (when w
7131 (select-window w)
7132 (org-agenda-redo)
7133 (select-window w0)
7134 (if org-agenda-overriding-restriction
7135 (message "Agenda view shifted to new %s restriction"
7136 org-agenda-overriding-restriction)
7137 (message "Agenda restriction lock removed")))))
7139 ;;; Agenda commands
7141 (defun org-agenda-check-type (error &rest types)
7142 "Check if agenda buffer is of allowed type.
7143 If ERROR is non-nil, throw an error, otherwise just return nil.
7144 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7145 (if (not org-agenda-type)
7146 (error "No Org agenda currently displayed")
7147 (if (memq org-agenda-type types)
7149 (if error
7150 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7151 nil))))
7153 (defun org-agenda-Quit ()
7154 "Exit the agenda and kill buffers loaded by `org-agenda'.
7155 Also restore the window configuration."
7156 (interactive)
7157 (if org-agenda-columns-active
7158 (org-columns-quit)
7159 (let ((buf (current-buffer)))
7160 (if (eq org-agenda-window-setup 'other-frame)
7161 (progn
7162 (org-agenda-reset-markers)
7163 (kill-buffer buf)
7164 (org-columns-remove-overlays)
7165 (setq org-agenda-archives-mode nil)
7166 (delete-frame))
7167 (and (not (eq org-agenda-window-setup 'current-window))
7168 (not (one-window-p))
7169 (delete-window))
7170 (org-agenda-reset-markers)
7171 (kill-buffer buf)
7172 (org-columns-remove-overlays)
7173 (setq org-agenda-archives-mode nil)))
7174 (setq org-agenda-buffer nil)
7175 ;; Maybe restore the pre-agenda window configuration.
7176 (and org-agenda-restore-windows-after-quit
7177 (not (eq org-agenda-window-setup 'other-frame))
7178 org-agenda-pre-window-conf
7179 (set-window-configuration org-agenda-pre-window-conf)
7180 (setq org-agenda-pre-window-conf nil))))
7182 (defun org-agenda-quit ()
7183 "Exit the agenda and restore the window configuration.
7184 When `org-agenda-sticky' is non-nil, only bury the agenda."
7185 (interactive)
7186 (if (and (eq org-indirect-buffer-display 'other-window)
7187 org-last-indirect-buffer)
7188 (let ((org-last-indirect-window
7189 (get-buffer-window org-last-indirect-buffer)))
7190 (if org-last-indirect-window
7191 (delete-window org-last-indirect-window))))
7192 (if org-agenda-columns-active
7193 (org-columns-quit)
7194 (if org-agenda-sticky
7195 (let ((buf (current-buffer)))
7196 (if (eq org-agenda-window-setup 'other-frame)
7197 (progn
7198 (delete-frame))
7199 (and (not (eq org-agenda-window-setup 'current-window))
7200 (not (one-window-p))
7201 (delete-window)))
7202 (with-current-buffer buf
7203 (bury-buffer)
7204 ;; Maybe restore the pre-agenda window configuration.
7205 (and org-agenda-restore-windows-after-quit
7206 (not (eq org-agenda-window-setup 'other-frame))
7207 org-agenda-pre-window-conf
7208 (set-window-configuration org-agenda-pre-window-conf)
7209 (setq org-agenda-pre-window-conf nil))))
7210 (org-agenda-Quit))))
7212 (defun org-agenda-exit ()
7213 "Exit the agenda and restore the window configuration.
7214 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7215 buffers visited directly by the user will not be touched."
7216 (interactive)
7217 (org-release-buffers org-agenda-new-buffers)
7218 (setq org-agenda-new-buffers nil)
7219 (org-agenda-Quit))
7221 (defun org-agenda-kill-all-agenda-buffers ()
7222 "Kill all buffers in `org-agenda-mode'.
7223 This is used when toggling sticky agendas.
7224 You can also explicitly invoke it with `C-c a C-k'."
7225 (interactive)
7226 (let (blist)
7227 (dolist (buf (buffer-list))
7228 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7229 (push buf blist)))
7230 (mapc 'kill-buffer blist)))
7232 (defun org-agenda-execute (arg)
7233 "Execute another agenda command, keeping same window.
7234 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7235 in the agenda."
7236 (interactive "P")
7237 (let ((org-agenda-window-setup 'current-window))
7238 (org-agenda arg)))
7240 (defun org-agenda-redo (&optional all)
7241 "Rebuild possibly ALL agenda view(s) in the current buffer."
7242 (interactive "P")
7243 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7244 (cpa (unless (eq all t) current-prefix-arg))
7245 (org-agenda-doing-sticky-redo org-agenda-sticky)
7246 (org-agenda-sticky nil)
7247 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7248 org-agenda-buffer-name))
7249 (org-agenda-keep-modes t)
7250 (tag-filter org-agenda-tag-filter)
7251 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7252 (top-hl-filter org-agenda-top-headline-filter)
7253 (cat-filter org-agenda-category-filter)
7254 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7255 (re-filter org-agenda-regexp-filter)
7256 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7257 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7258 (cols org-agenda-columns-active)
7259 (line (org-current-line))
7260 (window-line (- line (org-current-line (window-start))))
7261 (lprops (get 'org-agenda-redo-command 'org-lprops))
7262 (redo-cmd (get-text-property p 'org-redo-cmd))
7263 (last-args (get-text-property p 'org-last-args))
7264 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7265 (org-agenda-overriding-cmd-arguments
7266 (unless (eq all t)
7267 (cond ((listp last-args)
7268 (cons (or cpa (car last-args)) (cdr last-args)))
7269 ((stringp last-args)
7270 last-args))))
7271 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7272 (put 'org-agenda-tag-filter :preset-filter nil)
7273 (put 'org-agenda-category-filter :preset-filter nil)
7274 (put 'org-agenda-regexp-filter :preset-filter nil)
7275 (and cols (org-columns-quit))
7276 (message "Rebuilding agenda buffer...")
7277 (if series-redo-cmd
7278 (eval series-redo-cmd)
7279 (org-let lprops redo-cmd))
7280 (setq org-agenda-undo-list nil
7281 org-agenda-pending-undo-list nil
7282 org-agenda-tag-filter tag-filter
7283 org-agenda-category-filter cat-filter
7284 org-agenda-regexp-filter re-filter
7285 org-agenda-top-headline-filter top-hl-filter)
7286 (message "Rebuilding agenda buffer...done")
7287 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7288 (put 'org-agenda-category-filter :preset-filter cat-preset)
7289 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7290 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7291 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7292 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7293 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7294 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7295 (org-goto-line line)
7296 (recenter window-line)))
7298 (defvar org-global-tags-completion-table nil)
7299 (defvar org-agenda-filter-form nil)
7300 (defvar org-agenda-filtered-by-category nil)
7302 (defun org-agenda-filter-by-category (strip)
7303 "Keep only those lines in the agenda buffer that have a specific category.
7304 The category is that of the current line."
7305 (interactive "P")
7306 (if (and org-agenda-filtered-by-category
7307 org-agenda-category-filter)
7308 (org-agenda-filter-show-all-cat)
7309 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7310 (if (and cat (not (string= "" cat)))
7311 (org-agenda-filter-apply
7312 (setq org-agenda-category-filter
7313 (list (concat (if strip "-" "+") cat)))
7314 'category)
7315 (error "No category at point")))))
7317 (defun org-find-top-headline (&optional pos)
7318 "Find the topmost parent headline and return it."
7319 (save-excursion
7320 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7321 (if pos (goto-char pos))
7322 ;; Skip up to the topmost parent
7323 (while (ignore-errors (outline-up-heading 1) t))
7324 (ignore-errors
7325 (nth 4 (org-heading-components))))))
7327 (defvar org-agenda-filtered-by-top-headline nil)
7328 (defun org-agenda-filter-by-top-headline (strip)
7329 "Keep only those lines that are descendants from the same top headline.
7330 The top headline is that of the current line."
7331 (interactive "P")
7332 (if org-agenda-filtered-by-top-headline
7333 (progn
7334 (setq org-agenda-filtered-by-top-headline nil
7335 org-agenda-top-headline-filter nil)
7336 (org-agenda-filter-show-all-cat))
7337 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7338 (if cat (org-agenda-filter-top-headline-apply cat strip)
7339 (error "No top-level category at point")))))
7341 (defvar org-agenda-regexp-filter nil)
7342 (defun org-agenda-filter-by-regexp (strip)
7343 "Filter agenda entries by a regular expression.
7344 Regexp filters are cumulative.
7345 With no prefix argument, keep entries matching the regexp.
7346 With one prefix argument, filter out entries matching the regexp.
7347 With two prefix arguments, remove the regexp filters."
7348 (interactive "P")
7349 (if (not (equal strip '(16)))
7350 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7351 (read-from-minibuffer
7352 (if (equal strip '(4))
7353 "Filter out entries matching regexp: "
7354 "Narrow to entries matching regexp: ")))))
7355 (push flt org-agenda-regexp-filter)
7356 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7357 (org-agenda-filter-show-all-re)
7358 (message "Regexp filter removed")))
7360 (defun org-agenda-filter-remove-all ()
7361 "Remove all filters from the current agenda buffer."
7362 (interactive)
7363 (when org-agenda-tag-filter
7364 (org-agenda-filter-show-all-tag))
7365 (when org-agenda-category-filter
7366 (org-agenda-filter-show-all-cat))
7367 (when org-agenda-regexp-filter
7368 (org-agenda-filter-show-all-re)))
7370 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7371 "Keep only those lines in the agenda buffer that have a specific tag.
7372 The tag is selected with its fast selection letter, as configured.
7373 With prefix argument STRIP, remove all lines that do have the tag.
7374 A lisp caller can specify CHAR. NARROW means that the new tag should be
7375 used to narrow the search - the interactive user can also press `-' or `+'
7376 to switch to narrowing."
7377 (interactive "P")
7378 (let* ((alist org-tag-alist-for-agenda)
7379 (tag-chars (mapconcat
7380 (lambda (x) (if (and (not (symbolp (car x)))
7381 (cdr x))
7382 (char-to-string (cdr x))
7383 ""))
7384 alist ""))
7385 (efforts (org-split-string
7386 (or (cdr (assoc (concat org-effort-property "_ALL")
7387 org-global-properties))
7388 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7389 "")))
7390 (effort-op org-agenda-filter-effort-default-operator)
7391 (effort-prompt "")
7392 (inhibit-read-only t)
7393 (current org-agenda-tag-filter)
7394 maybe-refresh a n tag)
7395 (unless char
7396 (message
7397 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7398 (if narrow "Narrow" "Filter") tag-chars
7399 (if org-agenda-auto-exclude-function "[RET], " ""))
7400 (setq char (read-char-exclusive)))
7401 (when (member char '(?+ ?-))
7402 ;; Narrowing down
7403 (cond ((equal char ?-) (setq strip t narrow t))
7404 ((equal char ?+) (setq strip nil narrow t)))
7405 (message
7406 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7407 (setq char (read-char-exclusive)))
7408 (when (member char '(?< ?> ?= ??))
7409 ;; An effort operator
7410 (setq effort-op (char-to-string char))
7411 (setq alist nil) ; to make sure it will be interpreted as effort.
7412 (unless (equal char ??)
7413 (loop for i from 0 to 9 do
7414 (setq effort-prompt
7415 (concat
7416 effort-prompt " ["
7417 (if (= i 9) "0" (int-to-string (1+ i)))
7418 "]" (nth i efforts))))
7419 (message "Effort%s: %s " effort-op effort-prompt)
7420 (setq char (read-char-exclusive))
7421 (when (or (< char ?0) (> char ?9))
7422 (error "Need 1-9,0 to select effort"))))
7423 (when (equal char ?\t)
7424 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7425 (org-set-local 'org-global-tags-completion-table
7426 (org-global-tags-completion-table)))
7427 (let ((completion-ignore-case t))
7428 (setq tag (org-icompleting-read
7429 "Tag: " org-global-tags-completion-table))))
7430 (cond
7431 ((equal char ?\r)
7432 (org-agenda-filter-show-all-tag)
7433 (when org-agenda-auto-exclude-function
7434 (setq org-agenda-tag-filter nil)
7435 (dolist (tag (org-agenda-get-represented-tags))
7436 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7437 (if modifier
7438 (push modifier org-agenda-tag-filter))))
7439 (if (not (null org-agenda-tag-filter))
7440 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7441 (setq maybe-refresh t))
7442 ((equal char ?/)
7443 (org-agenda-filter-show-all-tag)
7444 (when (get 'org-agenda-tag-filter :preset-filter)
7445 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7446 (setq maybe-refresh t))
7447 ((equal char ?. )
7448 (setq org-agenda-tag-filter
7449 (mapcar (lambda(tag) (concat "+" tag))
7450 (org-get-at-bol 'tags)))
7451 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7452 (setq maybe-refresh t))
7453 ((or (equal char ?\ )
7454 (setq a (rassoc char alist))
7455 (and (>= char ?0) (<= char ?9)
7456 (setq n (if (= char ?0) 9 (- char ?0 1))
7457 tag (concat effort-op (nth n efforts))
7458 a (cons tag nil)))
7459 (and (= char ??)
7460 (setq tag "?eff")
7461 a (cons tag nil))
7462 (and tag (setq a (cons tag nil))))
7463 (org-agenda-filter-show-all-tag)
7464 (setq tag (car a))
7465 (setq org-agenda-tag-filter
7466 (cons (concat (if strip "-" "+") tag)
7467 (if narrow current nil)))
7468 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7469 (setq maybe-refresh t))
7470 (t (error "Invalid tag selection character %c" char)))
7471 (when (and maybe-refresh
7472 (eq org-agenda-clockreport-mode 'with-filter))
7473 (org-agenda-redo))))
7475 (defun org-agenda-get-represented-tags ()
7476 "Get a list of all tags currently represented in the agenda."
7477 (let (p tags)
7478 (save-excursion
7479 (goto-char (point-min))
7480 (while (setq p (next-single-property-change (point) 'tags))
7481 (goto-char p)
7482 (mapc (lambda (x) (add-to-list 'tags x))
7483 (get-text-property (point) 'tags))))
7484 tags))
7486 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7487 "Refine the current filter. See `org-agenda-filter-by-tag'."
7488 (interactive "P")
7489 (org-agenda-filter-by-tag strip char 'refine))
7491 (defun org-agenda-filter-make-matcher (filter type)
7492 "Create the form that tests a line for agenda filter."
7493 (let (f f1)
7494 (cond
7495 ;; Tag filter
7496 ((eq type 'tag)
7497 (setq filter
7498 (delete-dups
7499 (append (get 'org-agenda-tag-filter :preset-filter)
7500 filter)))
7501 (dolist (x filter)
7502 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7503 (ffunc
7504 (lambda (nf0 nf01 fltr notgroup op)
7505 (dolist (x fltr)
7506 (if (member x '("-" "+"))
7507 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7508 (if (string-match "[<=>?]" x)
7509 (setq nf01 (org-agenda-filter-effort-form x))
7510 (setq nf01 (list 'member (downcase (substring x 1))
7511 'tags)))
7512 (when (equal (string-to-char x) ?-)
7513 (setq nf01 (list 'not nf01))
7514 (when (not notgroup) (setq op 'and))))
7515 (push nf01 nf0))
7516 (if notgroup
7517 (push (cons 'and nf0) f)
7518 (push (cons (or op 'or) nf0) f)))))
7519 (if (equal nfilter filter)
7520 (funcall ffunc f1 f filter t nil)
7521 (funcall ffunc nf1 nf nfilter nil nil)))))
7522 ;; Category filter
7523 ((eq type 'category)
7524 (setq filter
7525 (delete-dups
7526 (append (get 'org-agenda-category-filter :preset-filter)
7527 filter)))
7528 (dolist (x filter)
7529 (if (equal "-" (substring x 0 1))
7530 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7531 (setq f1 (list 'equal (substring x 1) 'cat)))
7532 (push f1 f)))
7533 ;; Regexp filter
7534 ((eq type 'regexp)
7535 (setq filter
7536 (delete-dups
7537 (append (get 'org-agenda-regexp-filter :preset-filter)
7538 filter)))
7539 (dolist (x filter)
7540 (if (equal "-" (substring x 0 1))
7541 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7542 (setq f1 (list 'string-match (substring x 1) 'txt)))
7543 (push f1 f))))
7544 (cons 'and (nreverse f))))
7546 (defun org-agenda-filter-effort-form (e)
7547 "Return the form to compare the effort of the current line with what E says.
7548 E looks like \"+<2:25\"."
7549 (let (op)
7550 (setq e (substring e 1))
7551 (setq op (string-to-char e) e (substring e 1))
7552 (setq op (cond ((equal op ?<) '<=)
7553 ((equal op ?>) '>=)
7554 ((equal op ??) op)
7555 (t '=)))
7556 (list 'org-agenda-compare-effort (list 'quote op)
7557 (org-duration-string-to-minutes e))))
7559 (defun org-agenda-compare-effort (op value)
7560 "Compare the effort of the current line with VALUE, using OP.
7561 If the line does not have an effort defined, return nil."
7562 (let ((eff (org-get-at-bol 'effort-minutes)))
7563 (if (equal op ??)
7564 (not eff)
7565 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7566 value))))
7568 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7569 "Expand group tags in FILTER for the agenda.
7570 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7571 (if org-group-tags
7572 (let ((case-fold-search t) rtn)
7573 (mapc
7574 (lambda (f)
7575 (let (f0 dir)
7576 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7577 (setq dir (match-string 1 f) f0 (match-string 2 f))
7578 (setq dir (if no-operator "" "+") f0 f))
7579 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7580 (org-tags-expand f0 t t))
7581 rtn))))
7582 filter)
7583 (reverse rtn))
7584 filter))
7586 (defun org-agenda-filter-apply (filter type)
7587 "Set FILTER as the new agenda filter and apply it."
7588 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7589 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7590 (let (tags cat txt)
7591 (setq org-agenda-filter-form
7592 (org-agenda-filter-make-matcher filter type))
7593 (if (and (eq type 'category)
7594 (not (equal (substring (car filter) 0 1) "-")))
7595 ;; Only set `org-agenda-filtered-by-category' to t
7596 ;; when a unique category is used as the filter
7597 (setq org-agenda-filtered-by-category t))
7598 (org-agenda-set-mode-name)
7599 (save-excursion
7600 (goto-char (point-min))
7601 (while (not (eobp))
7602 (if (org-get-at-bol 'org-marker)
7603 (progn
7604 (setq tags ; used in eval
7605 (apply 'append
7606 (mapcar (lambda (f)
7607 (org-agenda-filter-expand-tags (list f) t))
7608 (org-get-at-bol 'tags)))
7609 cat (get-text-property (point) 'org-category)
7610 txt (get-text-property (point) 'txt))
7611 (if (not (eval org-agenda-filter-form))
7612 (org-agenda-filter-hide-line type))
7613 (beginning-of-line 2))
7614 (beginning-of-line 2))))
7615 (if (get-char-property (point) 'invisible)
7616 (ignore-errors (org-agenda-previous-line)))))
7618 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7619 "Filter by top headline HL."
7620 (org-agenda-set-mode-name)
7621 (save-excursion
7622 (goto-char (point-min))
7623 (while (not (eobp))
7624 (let* ((pos (org-get-at-bol 'org-hd-marker))
7625 (tophl (and pos (org-find-top-headline pos))))
7626 (if (and tophl (funcall (if negative 'identity 'not)
7627 (string= hl tophl)))
7628 (org-agenda-filter-hide-line 'category)))
7629 (beginning-of-line 2)))
7630 (if (get-char-property (point) 'invisible)
7631 (org-agenda-previous-line))
7632 (setq org-agenda-top-headline-filter hl
7633 org-agenda-filtered-by-top-headline t))
7635 (defun org-agenda-filter-hide-line (type)
7636 "Hide lines with TYPE in the agenda buffer."
7637 (let (ov)
7638 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7639 (point-at-eol)))
7640 (overlay-put ov 'invisible t)
7641 (overlay-put ov 'type type)
7642 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7643 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7644 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7646 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7647 (setq pos (or pos (point)))
7648 (save-excursion
7649 (dolist (ov (overlays-at pos))
7650 (when (and (overlay-get ov 'invisible)
7651 (eq (overlay-get ov 'type) 'tag))
7652 (goto-char pos)
7653 (if (< (overlay-start ov) (point-at-eol))
7654 (move-overlay ov (point-at-eol)
7655 (overlay-end ov)))))))
7657 (defun org-agenda-filter-show-all-tag nil
7658 "Remove tag filter overlays from the agenda buffer."
7659 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7660 (setq org-agenda-tag-filter-overlays nil
7661 org-agenda-tag-filter nil
7662 org-agenda-filter-form nil)
7663 (org-agenda-set-mode-name))
7665 (defun org-agenda-filter-show-all-re nil
7666 "Remove regexp filter overlays from the agenda buffer."
7667 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7668 (setq org-agenda-re-filter-overlays nil
7669 org-agenda-regexp-filter nil
7670 org-agenda-filter-form nil)
7671 (org-agenda-set-mode-name))
7673 (defun org-agenda-filter-show-all-cat nil
7674 "Remove category filter overlays from the agenda buffer."
7675 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7676 (setq org-agenda-cat-filter-overlays nil
7677 org-agenda-filtered-by-category nil
7678 org-agenda-category-filter nil
7679 org-agenda-filter-form nil)
7680 (org-agenda-set-mode-name))
7682 (defun org-agenda-manipulate-query-add ()
7683 "Manipulate the query by adding a search term with positive selection.
7684 Positive selection means the term must be matched for selection of an entry."
7685 (interactive)
7686 (org-agenda-manipulate-query ?\[))
7687 (defun org-agenda-manipulate-query-subtract ()
7688 "Manipulate the query by adding a search term with negative selection.
7689 Negative selection means term must not be matched for selection of an entry."
7690 (interactive)
7691 (org-agenda-manipulate-query ?\]))
7692 (defun org-agenda-manipulate-query-add-re ()
7693 "Manipulate the query by adding a search regexp with positive selection.
7694 Positive selection means the regexp must match for selection of an entry."
7695 (interactive)
7696 (org-agenda-manipulate-query ?\{))
7697 (defun org-agenda-manipulate-query-subtract-re ()
7698 "Manipulate the query by adding a search regexp with negative selection.
7699 Negative selection means regexp must not match for selection of an entry."
7700 (interactive)
7701 (org-agenda-manipulate-query ?\}))
7702 (defun org-agenda-manipulate-query (char)
7703 (cond
7704 ((memq org-agenda-type '(timeline agenda))
7705 (let ((org-agenda-include-inactive-timestamps t))
7706 (org-agenda-redo))
7707 (message "Display now includes inactive timestamps as well"))
7708 ((eq org-agenda-type 'search)
7709 (org-add-to-string
7710 'org-agenda-query-string
7711 (if org-agenda-last-search-view-search-was-boolean
7712 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7713 (?\{ . " +{}") (?\} . " -{}"))))
7714 " "))
7715 (setq org-agenda-redo-command
7716 (list 'org-search-view
7717 (car (get-text-property (min (1- (point-max)) (point))
7718 'org-last-args))
7719 org-agenda-query-string
7720 (+ (length org-agenda-query-string)
7721 (if (member char '(?\{ ?\})) 0 1))))
7722 (set-register org-agenda-query-register org-agenda-query-string)
7723 (let ((org-agenda-overriding-arguments
7724 (cdr org-agenda-redo-command)))
7725 (org-agenda-redo)))
7726 (t (error "Cannot manipulate query for %s-type agenda buffers"
7727 org-agenda-type))))
7729 (defun org-add-to-string (var string)
7730 (set var (concat (symbol-value var) string)))
7732 (defun org-agenda-goto-date (span)
7733 "Jump to DATE in agenda."
7734 (interactive "P")
7735 (let* ((org-read-date-prefer-future
7736 (eval org-agenda-jump-prefer-future))
7737 (date (org-read-date))
7738 (day (time-to-days (org-time-string-to-time date)))
7739 (org-agenda-sticky-orig org-agenda-sticky)
7740 (org-agenda-buffer-tmp-name (buffer-name))
7741 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7742 (0-arg (or current-prefix-arg (car args)))
7743 (2-arg (nth 2 args))
7744 (with-hour-p (nth 4 org-agenda-redo-command))
7745 (newcmd (list 'org-agenda-list 0-arg date
7746 (org-agenda-span-to-ndays
7747 2-arg (org-time-string-to-absolute date))
7748 with-hour-p))
7749 (newargs (cdr newcmd))
7750 (inhibit-read-only t)
7751 org-agenda-sticky)
7752 (if (not (org-agenda-check-type t 'agenda))
7753 (error "Not available in non-agenda views")
7754 (add-text-properties (point-min) (point-max)
7755 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7756 (org-agenda-redo)
7757 (goto-char (point-min))
7758 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7759 (save-excursion (move-beginning-of-line 2) (eobp))))
7760 (move-beginning-of-line 2))
7761 (setq org-agenda-sticky org-agenda-sticky-orig
7762 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7764 (defun org-agenda-goto-today ()
7765 "Go to today."
7766 (interactive)
7767 (org-agenda-check-type t 'timeline 'agenda)
7768 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7769 (curspan (nth 2 args))
7770 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7771 (cond
7772 (tdpos (goto-char tdpos))
7773 ((eq org-agenda-type 'agenda)
7774 (let* ((sd (org-agenda-compute-starting-span
7775 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7776 (org-agenda-overriding-arguments args))
7777 (setf (nth 1 org-agenda-overriding-arguments) sd)
7778 (org-agenda-redo)
7779 (org-agenda-find-same-or-today-or-agenda)))
7780 (t (error "Cannot find today")))))
7782 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7783 (goto-char
7784 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7785 (text-property-any (point-min) (point-max) 'org-today t)
7786 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7787 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7788 (org-agenda-goto-block-beginning))
7789 (point-min))))
7791 (defun org-agenda-goto-block-beginning ()
7792 "Go the agenda block beginning."
7793 (interactive)
7794 (if (not (derived-mode-p 'org-agenda-mode))
7795 (error "Cannot execute this command outside of org-agenda-mode buffers")
7796 (let (dest)
7797 (save-excursion
7798 (unless (looking-at "\\'")
7799 (forward-char))
7800 (let* ((prop 'org-agenda-structural-header)
7801 (p (previous-single-property-change (point) prop))
7802 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7803 (1- (point))) prop)))
7804 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7805 (if (not dest)
7806 (error "Cannot find the beginning of the blog")
7807 (goto-char dest)
7808 (move-beginning-of-line 1)))))
7810 (defun org-agenda-later (arg)
7811 "Go forward in time by the current span.
7812 With prefix ARG, go forward that many times the current span."
7813 (interactive "p")
7814 (org-agenda-check-type t 'agenda)
7815 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7816 (span (or (nth 2 args) org-agenda-current-span))
7817 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7818 (greg (calendar-gregorian-from-absolute sd))
7819 (cnt (org-get-at-bol 'org-day-cnt))
7820 greg2)
7821 (cond
7822 ((numberp span)
7823 (setq sd (+ (* span arg) sd)))
7824 ((eq span 'day)
7825 (setq sd (+ arg sd)))
7826 ((eq span 'week)
7827 (setq sd (+ (* 7 arg) sd)))
7828 ((eq span 'month)
7829 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7830 sd (calendar-absolute-from-gregorian greg2))
7831 (setcar greg2 (1+ (car greg2))))
7832 ((eq span 'year)
7833 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7834 sd (calendar-absolute-from-gregorian greg2))
7835 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7837 (setq sd (+ (* span arg) sd))))
7838 (let ((org-agenda-overriding-cmd
7839 ;; `cmd' may have been set by `org-agenda-run-series' which
7840 ;; uses `org-agenda-overriding-cmd' to decide whether
7841 ;; overriding is allowed for `cmd'
7842 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7843 (org-agenda-overriding-arguments
7844 (list (car args) sd span)))
7845 (org-agenda-redo)
7846 (org-agenda-find-same-or-today-or-agenda cnt))))
7848 (defun org-agenda-earlier (arg)
7849 "Go backward in time by the current span.
7850 With prefix ARG, go backward that many times the current span."
7851 (interactive "p")
7852 (org-agenda-later (- arg)))
7854 (defun org-agenda-view-mode-dispatch ()
7855 "Call one of the view mode commands."
7856 (interactive)
7857 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7858 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7859 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7860 (let ((a (read-char-exclusive)))
7861 (case a
7862 (?\ (call-interactively 'org-agenda-reset-view))
7863 (?d (call-interactively 'org-agenda-day-view))
7864 (?w (call-interactively 'org-agenda-week-view))
7865 (?m (call-interactively 'org-agenda-month-view))
7866 (?y (call-interactively 'org-agenda-year-view))
7867 (?l (call-interactively 'org-agenda-log-mode))
7868 (?L (org-agenda-log-mode '(4)))
7869 (?c (org-agenda-log-mode 'clockcheck))
7870 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7871 (?a (call-interactively 'org-agenda-archives-mode))
7872 (?A (org-agenda-archives-mode 'files))
7873 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7874 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7875 (?G (call-interactively 'org-agenda-toggle-time-grid))
7876 (?D (call-interactively 'org-agenda-toggle-diary))
7877 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7878 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7879 (org-agenda-check-type t 'timeline 'agenda)
7880 (org-agenda-redo))
7881 (message "Display now includes inactive timestamps as well"))
7882 (?q (message "Abort"))
7883 (otherwise (error "Invalid key" )))))
7885 (defun org-agenda-reset-view ()
7886 "Switch to default view for agenda."
7887 (interactive)
7888 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7889 (defun org-agenda-day-view (&optional day-of-month)
7890 "Switch to daily view for agenda.
7891 With argument DAY-OF-MONTH, switch to that day of the month."
7892 (interactive "P")
7893 (org-agenda-change-time-span 'day day-of-month))
7894 (defun org-agenda-week-view (&optional iso-week)
7895 "Switch to daily view for agenda.
7896 With argument ISO-WEEK, switch to the corresponding ISO week.
7897 If ISO-WEEK has more then 2 digits, only the last two encode the
7898 week. Any digits before this encode a year. So 200712 means
7899 week 12 of year 2007. Years in the range 1938-2037 can also be
7900 written as 2-digit years."
7901 (interactive "P")
7902 (org-agenda-change-time-span 'week iso-week))
7903 (defun org-agenda-month-view (&optional month)
7904 "Switch to monthly view for agenda.
7905 With argument MONTH, switch to that month."
7906 (interactive "P")
7907 (org-agenda-change-time-span 'month month))
7908 (defun org-agenda-year-view (&optional year)
7909 "Switch to yearly view for agenda.
7910 With argument YEAR, switch to that year.
7911 If MONTH has more then 2 digits, only the last two encode the
7912 month. Any digits before this encode a year. So 200712 means
7913 December year 2007. Years in the range 1938-2037 can also be
7914 written as 2-digit years."
7915 (interactive "P")
7916 (when year
7917 (setq year (org-small-year-to-year year)))
7918 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7919 (org-agenda-change-time-span 'year year)
7920 (error "Abort")))
7922 (defun org-agenda-change-time-span (span &optional n)
7923 "Change the agenda view to SPAN.
7924 SPAN may be `day', `week', `month', `year'."
7925 (org-agenda-check-type t 'agenda)
7926 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7927 (curspan (nth 2 args)))
7928 (if (and (not n) (equal curspan span))
7929 (error "Viewing span is already \"%s\"" span))
7930 (let* ((sd (or (org-get-at-bol 'day)
7931 (nth 1 args)
7932 org-starting-day))
7933 (sd (org-agenda-compute-starting-span sd span n))
7934 (org-agenda-overriding-cmd
7935 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7936 (org-agenda-overriding-arguments
7937 (list (car args) sd span)))
7938 (org-agenda-redo)
7939 (org-agenda-find-same-or-today-or-agenda))
7940 (org-agenda-set-mode-name)
7941 (message "Switched to %s view" span)))
7943 (defun org-agenda-compute-starting-span (sd span &optional n)
7944 "Compute starting date for agenda.
7945 SPAN may be `day', `week', `month', `year'. The return value
7946 is a cons cell with the starting date and the number of days,
7947 so that the date SD will be in that range."
7948 (let* ((greg (calendar-gregorian-from-absolute sd))
7949 (dg (nth 1 greg))
7950 (mg (car greg))
7951 (yg (nth 2 greg)))
7952 (cond
7953 ((eq span 'day)
7954 (when n
7955 (setq sd (+ (calendar-absolute-from-gregorian
7956 (list mg 1 yg))
7957 n -1))))
7958 ((eq span 'week)
7959 (let* ((nt (calendar-day-of-week
7960 (calendar-gregorian-from-absolute sd)))
7961 (d (if org-agenda-start-on-weekday
7962 (- nt org-agenda-start-on-weekday)
7965 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7966 (when n
7967 (require 'cal-iso)
7968 (when (> n 99)
7969 (setq y1 (org-small-year-to-year (/ n 100))
7970 n (mod n 100)))
7971 (setq sd
7972 (calendar-absolute-from-iso
7973 (list n 1
7974 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7975 ((eq span 'month)
7976 (let (y1)
7977 (when (and n (> n 99))
7978 (setq y1 (org-small-year-to-year (/ n 100))
7979 n (mod n 100)))
7980 (setq sd (calendar-absolute-from-gregorian
7981 (list (or n mg) 1 (or y1 yg))))))
7982 ((eq span 'year)
7983 (setq sd (calendar-absolute-from-gregorian
7984 (list 1 1 (or n yg))))))
7985 sd))
7987 (defun org-agenda-next-date-line (&optional arg)
7988 "Jump to the next line indicating a date in agenda buffer."
7989 (interactive "p")
7990 (org-agenda-check-type t 'agenda 'timeline)
7991 (beginning-of-line 1)
7992 ;; This does not work if user makes date format that starts with a blank
7993 (if (looking-at "^\\S-") (forward-char 1))
7994 (if (not (re-search-forward "^\\S-" nil t arg))
7995 (progn
7996 (backward-char 1)
7997 (error "No next date after this line in this buffer")))
7998 (goto-char (match-beginning 0)))
8000 (defun org-agenda-previous-date-line (&optional arg)
8001 "Jump to the previous line indicating a date in agenda buffer."
8002 (interactive "p")
8003 (org-agenda-check-type t 'agenda 'timeline)
8004 (beginning-of-line 1)
8005 (if (not (re-search-backward "^\\S-" nil t arg))
8006 (error "No previous date before this line in this buffer")))
8008 ;; Initialize the highlight
8009 (defvar org-hl (make-overlay 1 1))
8010 (overlay-put org-hl 'face 'highlight)
8012 (defun org-highlight (begin end &optional buffer)
8013 "Highlight a region with overlay."
8014 (move-overlay org-hl begin end (or buffer (current-buffer))))
8016 (defun org-unhighlight ()
8017 "Detach overlay INDEX."
8018 (org-detach-overlay org-hl))
8020 (defun org-unhighlight-once ()
8021 "Remove the highlight from its position, and this function from the hook."
8022 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8023 (org-unhighlight))
8025 (defvar org-agenda-pre-follow-window-conf nil)
8026 (defun org-agenda-follow-mode ()
8027 "Toggle follow mode in an agenda buffer."
8028 (interactive)
8029 (unless org-agenda-follow-mode
8030 (setq org-agenda-pre-follow-window-conf
8031 (current-window-configuration)))
8032 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8033 (unless org-agenda-follow-mode
8034 (set-window-configuration org-agenda-pre-follow-window-conf))
8035 (org-agenda-set-mode-name)
8036 (org-agenda-do-context-action)
8037 (message "Follow mode is %s"
8038 (if org-agenda-follow-mode "on" "off")))
8040 (defun org-agenda-entry-text-mode (&optional arg)
8041 "Toggle entry text mode in an agenda buffer."
8042 (interactive "P")
8043 (if (or org-agenda-tag-filter
8044 org-agenda-category-filter
8045 org-agenda-regexp-filter
8046 org-agenda-top-headline-filter)
8047 (user-error "Can't show entry text in filtered views")
8048 (setq org-agenda-entry-text-mode (or (integerp arg)
8049 (not org-agenda-entry-text-mode)))
8050 (org-agenda-entry-text-hide)
8051 (and org-agenda-entry-text-mode
8052 (let ((org-agenda-entry-text-maxlines
8053 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8054 (org-agenda-entry-text-show)))
8055 (org-agenda-set-mode-name)
8056 (message "Entry text mode is %s%s"
8057 (if org-agenda-entry-text-mode "on" "off")
8058 (if (not org-agenda-entry-text-mode) ""
8059 (format " (maximum number of lines is %d)"
8060 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8062 (defun org-agenda-clockreport-mode (&optional with-filter)
8063 "Toggle clocktable mode in an agenda buffer.
8064 With prefix arg WITH-FILTER, make the clocktable respect the current
8065 agenda filter."
8066 (interactive "P")
8067 (org-agenda-check-type t 'agenda)
8068 (if with-filter
8069 (setq org-agenda-clockreport-mode 'with-filter)
8070 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8071 (org-agenda-set-mode-name)
8072 (org-agenda-redo)
8073 (message "Clocktable mode is %s"
8074 (if org-agenda-clockreport-mode "on" "off")))
8076 (defun org-agenda-log-mode (&optional special)
8077 "Toggle log mode in an agenda buffer.
8078 With argument SPECIAL, show all possible log items, not only the ones
8079 configured in `org-agenda-log-mode-items'.
8080 With a double `C-u' prefix arg, show *only* log items, nothing else."
8081 (interactive "P")
8082 (org-agenda-check-type t 'agenda 'timeline)
8083 (setq org-agenda-show-log
8084 (cond
8085 ((equal special '(16)) 'only)
8086 ((eq special 'clockcheck)
8087 (if (eq org-agenda-show-log 'clockcheck)
8088 nil 'clockcheck))
8089 (special '(closed clock state))
8090 (t (not org-agenda-show-log))))
8091 (org-agenda-set-mode-name)
8092 (org-agenda-redo)
8093 (message "Log mode is %s"
8094 (if org-agenda-show-log "on" "off")))
8096 (defun org-agenda-archives-mode (&optional with-files)
8097 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8098 When called with a prefix argument, include all archive files as well."
8099 (interactive "P")
8100 (setq org-agenda-archives-mode
8101 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8102 (org-agenda-set-mode-name)
8103 (org-agenda-redo)
8104 (message
8105 "%s"
8106 (cond
8107 ((eq org-agenda-archives-mode nil)
8108 "No archives are included")
8109 ((eq org-agenda-archives-mode 'trees)
8110 (format "Trees with :%s: tag are included" org-archive-tag))
8111 ((eq org-agenda-archives-mode t)
8112 (format "Trees with :%s: tag and all active archive files are included"
8113 org-archive-tag)))))
8115 (defun org-agenda-toggle-diary ()
8116 "Toggle diary inclusion in an agenda buffer."
8117 (interactive)
8118 (org-agenda-check-type t 'agenda)
8119 (setq org-agenda-include-diary (not org-agenda-include-diary))
8120 (org-agenda-redo)
8121 (org-agenda-set-mode-name)
8122 (message "Diary inclusion turned %s"
8123 (if org-agenda-include-diary "on" "off")))
8125 (defun org-agenda-toggle-deadlines ()
8126 "Toggle inclusion of entries with a deadline in an agenda buffer."
8127 (interactive)
8128 (org-agenda-check-type t 'agenda)
8129 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8130 (org-agenda-redo)
8131 (org-agenda-set-mode-name)
8132 (message "Deadlines inclusion turned %s"
8133 (if org-agenda-include-deadlines "on" "off")))
8135 (defun org-agenda-toggle-time-grid ()
8136 "Toggle time grid in an agenda buffer."
8137 (interactive)
8138 (org-agenda-check-type t 'agenda)
8139 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8140 (org-agenda-redo)
8141 (org-agenda-set-mode-name)
8142 (message "Time-grid turned %s"
8143 (if org-agenda-use-time-grid "on" "off")))
8145 (defun org-agenda-set-mode-name ()
8146 "Set the mode name to indicate all the small mode settings."
8147 (setq mode-name
8148 (list "Org-Agenda"
8149 (if (get 'org-agenda-files 'org-restrict) " []" "")
8151 '(:eval (org-agenda-span-name org-agenda-current-span))
8152 (if org-agenda-follow-mode " Follow" "")
8153 (if org-agenda-entry-text-mode " ETxt" "")
8154 (if org-agenda-include-diary " Diary" "")
8155 (if org-agenda-include-deadlines " Ddl" "")
8156 (if org-agenda-use-time-grid " Grid" "")
8157 (if (and (boundp 'org-habit-show-habits)
8158 org-habit-show-habits) " Habit" "")
8159 (cond
8160 ((consp org-agenda-show-log) " LogAll")
8161 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8162 (org-agenda-show-log " Log")
8163 (t ""))
8164 (if (or org-agenda-category-filter
8165 (get 'org-agenda-category-filter :preset-filter))
8166 '(:eval (org-propertize
8167 (concat " <"
8168 (mapconcat
8169 'identity
8170 (append
8171 (get 'org-agenda-category-filter :preset-filter)
8172 org-agenda-category-filter)
8174 ">")
8175 'face 'org-agenda-filter-category
8176 'help-echo "Category used in filtering")) "")
8177 (if (or org-agenda-tag-filter
8178 (get 'org-agenda-tag-filter :preset-filter))
8179 '(:eval (org-propertize
8180 (concat " {"
8181 (mapconcat
8182 'identity
8183 (append
8184 (get 'org-agenda-tag-filter :preset-filter)
8185 org-agenda-tag-filter)
8187 "}")
8188 'face 'org-agenda-filter-tags
8189 'help-echo "Tags used in filtering")) "")
8190 (if (or org-agenda-regexp-filter
8191 (get 'org-agenda-regexp-filter :preset-filter))
8192 '(:eval (org-propertize
8193 (concat " ["
8194 (mapconcat
8195 'identity
8196 (append
8197 (get 'org-agenda-regexp-filter :preset-filter)
8198 org-agenda-regexp-filter)
8200 "]")
8201 'face 'org-agenda-filter-regexp
8202 'help-echo "Regexp used in filtering")) "")
8203 (if org-agenda-archives-mode
8204 (if (eq org-agenda-archives-mode t)
8205 " Archives"
8206 (format " :%s:" org-archive-tag))
8208 (if org-agenda-clockreport-mode
8209 (if (eq org-agenda-clockreport-mode 'with-filter)
8210 " Clock{}" " Clock")
8211 "")))
8212 (force-mode-line-update))
8214 (define-obsolete-function-alias
8215 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8217 (defun org-agenda-update-agenda-type ()
8218 "Update the agenda type after each command."
8219 (setq org-agenda-type
8220 (or (get-text-property (point) 'org-agenda-type)
8221 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8223 (defun org-agenda-next-line ()
8224 "Move cursor to the next line, and show if follow mode is active."
8225 (interactive)
8226 (call-interactively 'next-line)
8227 (org-agenda-do-context-action))
8229 (defun org-agenda-previous-line ()
8230 "Move cursor to the previous line, and show if follow-mode is active."
8231 (interactive)
8232 (call-interactively 'previous-line)
8233 (org-agenda-do-context-action))
8235 (defun org-agenda-next-item (n)
8236 "Move cursor to next agenda item."
8237 (interactive "p")
8238 (let ((col (current-column)))
8239 (dotimes (c n)
8240 (when (next-single-property-change (point-at-eol) 'org-marker)
8241 (move-end-of-line 1)
8242 (goto-char (next-single-property-change (point) 'org-marker))))
8243 (org-move-to-column col))
8244 (org-agenda-do-context-action))
8246 (defun org-agenda-previous-item (n)
8247 "Move cursor to next agenda item."
8248 (interactive "p")
8249 (dotimes (c n)
8250 (let ((col (current-column))
8251 (goto (save-excursion
8252 (move-end-of-line 0)
8253 (previous-single-property-change (point) 'org-marker))))
8254 (if goto (goto-char goto))
8255 (org-move-to-column col)))
8256 (org-agenda-do-context-action))
8258 (defun org-agenda-do-context-action ()
8259 "Show outline path and, maybe, follow mode window."
8260 (let ((m (org-get-at-bol 'org-marker)))
8261 (when (and (markerp m) (marker-buffer m))
8262 (and org-agenda-follow-mode
8263 (if org-agenda-follow-indirect
8264 (org-agenda-tree-to-indirect-buffer nil)
8265 (org-agenda-show)))
8266 (and org-agenda-show-outline-path
8267 (org-with-point-at m (org-display-outline-path t))))))
8269 (defun org-agenda-show-tags ()
8270 "Show the tags applicable to the current item."
8271 (interactive)
8272 (let* ((tags (org-get-at-bol 'tags)))
8273 (if tags
8274 (message "Tags are :%s:"
8275 (org-no-properties (mapconcat 'identity tags ":")))
8276 (message "No tags associated with this line"))))
8278 (defun org-agenda-goto (&optional highlight)
8279 "Go to the Org-mode file which contains the item at point."
8280 (interactive)
8281 (let* ((marker (or (org-get-at-bol 'org-marker)
8282 (org-agenda-error)))
8283 (buffer (marker-buffer marker))
8284 (pos (marker-position marker)))
8285 (switch-to-buffer-other-window buffer)
8286 (widen)
8287 (push-mark)
8288 (goto-char pos)
8289 (when (derived-mode-p 'org-mode)
8290 (org-show-context 'agenda)
8291 (save-excursion
8292 (and (outline-next-heading)
8293 (org-flag-heading nil)))) ; show the next heading
8294 (when (outline-invisible-p)
8295 (show-entry)) ; display invisible text
8296 (recenter (/ (window-height) 2))
8297 (run-hooks 'org-agenda-after-show-hook)
8298 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8300 (defvar org-agenda-after-show-hook nil
8301 "Normal hook run after an item has been shown from the agenda.
8302 Point is in the buffer where the item originated.")
8304 (defun org-agenda-kill ()
8305 "Kill the entry or subtree belonging to the current agenda entry."
8306 (interactive)
8307 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8308 (let* ((bufname-orig (buffer-name))
8309 (marker (or (org-get-at-bol 'org-marker)
8310 (org-agenda-error)))
8311 (buffer (marker-buffer marker))
8312 (pos (marker-position marker))
8313 (type (org-get-at-bol 'type))
8314 dbeg dend (n 0) conf)
8315 (org-with-remote-undo buffer
8316 (with-current-buffer buffer
8317 (save-excursion
8318 (goto-char pos)
8319 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8320 (setq dbeg (progn (org-back-to-heading t) (point))
8321 dend (org-end-of-subtree t t))
8322 (setq dbeg (point-at-bol)
8323 dend (min (point-max) (1+ (point-at-eol)))))
8324 (goto-char dbeg)
8325 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8326 (setq conf (or (eq t org-agenda-confirm-kill)
8327 (and (numberp org-agenda-confirm-kill)
8328 (> n org-agenda-confirm-kill))))
8329 (and conf
8330 (not (y-or-n-p
8331 (format "Delete entry with %d lines in buffer \"%s\"? "
8332 n (buffer-name buffer))))
8333 (error "Abort"))
8334 (let ((org-agenda-buffer-name bufname-orig))
8335 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8336 (with-current-buffer buffer (delete-region dbeg dend))
8337 (message "Agenda item and source killed"))))
8339 (defvar org-archive-default-command) ; defined in org-archive.el
8340 (defun org-agenda-archive-default ()
8341 "Archive the entry or subtree belonging to the current agenda entry."
8342 (interactive)
8343 (require 'org-archive)
8344 (org-agenda-archive-with org-archive-default-command))
8346 (defun org-agenda-archive-default-with-confirmation ()
8347 "Archive the entry or subtree belonging to the current agenda entry."
8348 (interactive)
8349 (require 'org-archive)
8350 (org-agenda-archive-with org-archive-default-command 'confirm))
8352 (defun org-agenda-archive ()
8353 "Archive the entry or subtree belonging to the current agenda entry."
8354 (interactive)
8355 (org-agenda-archive-with 'org-archive-subtree))
8357 (defun org-agenda-archive-to-archive-sibling ()
8358 "Move the entry to the archive sibling."
8359 (interactive)
8360 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8362 (defun org-agenda-archive-with (cmd &optional confirm)
8363 "Move the entry to the archive sibling."
8364 (interactive)
8365 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8366 (let* ((bufname-orig (buffer-name))
8367 (marker (or (org-get-at-bol 'org-marker)
8368 (org-agenda-error)))
8369 (buffer (marker-buffer marker))
8370 (pos (marker-position marker)))
8371 (org-with-remote-undo buffer
8372 (with-current-buffer buffer
8373 (if (derived-mode-p 'org-mode)
8374 (if (and confirm
8375 (not (y-or-n-p "Archive this subtree or entry? ")))
8376 (error "Abort")
8377 (save-excursion
8378 (goto-char pos)
8379 (let ((org-agenda-buffer-name bufname-orig))
8380 (org-remove-subtree-entries-from-agenda))
8381 (org-back-to-heading t)
8382 (funcall cmd)))
8383 (error "Archiving works only in Org-mode files"))))))
8385 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8386 "Remove all lines in the agenda that correspond to a given subtree.
8387 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8388 If this information is not given, the function uses the tree at point."
8389 (let ((buf (or buf (current-buffer))) m p)
8390 (save-excursion
8391 (unless (and beg end)
8392 (org-back-to-heading t)
8393 (setq beg (point))
8394 (org-end-of-subtree t)
8395 (setq end (point)))
8396 (set-buffer (get-buffer org-agenda-buffer-name))
8397 (save-excursion
8398 (goto-char (point-max))
8399 (beginning-of-line 1)
8400 (while (not (bobp))
8401 (when (and (setq m (org-get-at-bol 'org-marker))
8402 (equal buf (marker-buffer m))
8403 (setq p (marker-position m))
8404 (>= p beg)
8405 (< p end))
8406 (let ((inhibit-read-only t))
8407 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8408 (beginning-of-line 0))))))
8410 (defun org-agenda-refile (&optional goto rfloc no-update)
8411 "Refile the item at point.
8413 When GOTO is 0 or '(64), clear the refile cache.
8414 When GOTO is '(16), go to the location of the last refiled item.
8415 RFLOC can be a refile location obtained in a different way.
8416 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8417 (interactive "P")
8418 (cond
8419 ((member goto '(0 (64)))
8420 (org-refile-cache-clear))
8421 ((equal goto '(16))
8422 (org-refile-goto-last-stored))
8424 (let* ((buffer-orig (buffer-name))
8425 (marker (or (org-get-at-bol 'org-hd-marker)
8426 (org-agenda-error)))
8427 (buffer (marker-buffer marker))
8428 (pos (marker-position marker))
8429 (rfloc (or rfloc
8430 (org-refile-get-location
8431 (if goto "Goto" "Refile to") buffer
8432 org-refile-allow-creating-parent-nodes))))
8433 (with-current-buffer buffer
8434 (save-excursion
8435 (save-restriction
8436 (widen)
8437 (goto-char marker)
8438 (let ((org-agenda-buffer-name buffer-orig))
8439 (org-remove-subtree-entries-from-agenda))
8440 (org-refile goto buffer rfloc)))))
8441 (unless no-update (org-agenda-redo)))))
8443 (defun org-agenda-open-link (&optional arg)
8444 "Open the link(s) in the current entry, if any.
8445 This looks for a link in the displayed line in the agenda.
8446 It also looks at the text of the entry itself."
8447 (interactive "P")
8448 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8449 (org-get-at-bol 'org-marker)))
8450 (buffer (and marker (marker-buffer marker)))
8451 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8452 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8453 (lk0 (car lkall))
8454 (lk (if (stringp lk0) (list lk0) lk0))
8455 (lkend (cdr lkall))
8456 trg)
8457 (cond
8458 ((and buffer lk)
8459 (mapcar (lambda(l)
8460 (with-current-buffer buffer
8461 (setq trg (and (string-match org-bracket-link-regexp l)
8462 (match-string 1 l)))
8463 (if (or (not trg) (string-match org-any-link-re trg))
8464 (save-excursion
8465 (save-restriction
8466 (widen)
8467 (goto-char marker)
8468 (when (search-forward l nil lkend)
8469 (goto-char (match-beginning 0))
8470 (org-open-at-point))))
8471 ;; This is an internal link, widen the buffer
8472 (switch-to-buffer-other-window buffer)
8473 (widen)
8474 (goto-char marker)
8475 (when (search-forward l nil lkend)
8476 (goto-char (match-beginning 0))
8477 (org-open-at-point)))))
8478 lk))
8479 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8480 (save-excursion
8481 (beginning-of-line 1)
8482 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8483 (org-open-link-from-string (match-string 1)))
8484 (t (message "No link to open here")))))
8486 (defun org-agenda-copy-local-variable (var)
8487 "Get a variable from a referenced buffer and install it here."
8488 (let ((m (org-get-at-bol 'org-marker)))
8489 (when (and m (buffer-live-p (marker-buffer m)))
8490 (org-set-local var (with-current-buffer (marker-buffer m)
8491 (symbol-value var))))))
8493 (defun org-agenda-switch-to (&optional delete-other-windows)
8494 "Go to the Org-mode file which contains the item at point."
8495 (interactive)
8496 (if (and org-return-follows-link
8497 (not (org-get-at-bol 'org-marker))
8498 (org-in-regexp org-bracket-link-regexp))
8499 (org-open-link-from-string (match-string 0))
8500 (let* ((marker (or (org-get-at-bol 'org-marker)
8501 (org-agenda-error)))
8502 (buffer (marker-buffer marker))
8503 (pos (marker-position marker)))
8504 (org-pop-to-buffer-same-window buffer)
8505 (and delete-other-windows (delete-other-windows))
8506 (widen)
8507 (goto-char pos)
8508 (when (derived-mode-p 'org-mode)
8509 (org-show-context 'agenda)
8510 (save-excursion
8511 (and (outline-next-heading)
8512 (org-flag-heading nil))) ; show the next heading
8513 (when (outline-invisible-p)
8514 (show-entry)) ; display invisible text
8515 (run-hooks 'org-agenda-after-show-hook)))))
8517 (defun org-agenda-goto-mouse (ev)
8518 "Go to the Org-mode file which contains the item at the mouse click."
8519 (interactive "e")
8520 (mouse-set-point ev)
8521 (org-agenda-goto))
8523 (defun org-agenda-show (&optional full-entry)
8524 "Display the Org-mode file which contains the item at point.
8525 With prefix argument FULL-ENTRY, make the entire entry visible
8526 if it was hidden in the outline."
8527 (interactive "P")
8528 (let ((win (selected-window)))
8529 (if full-entry
8530 (let ((org-show-entry-below t))
8531 (org-agenda-goto t))
8532 (org-agenda-goto t))
8533 (select-window win)))
8535 (defvar org-agenda-show-window nil)
8536 (defun org-agenda-show-and-scroll-up (&optional arg)
8537 "Display the Org-mode file which contains the item at point.
8538 When called repeatedly, scroll the window that is displaying the buffer.
8539 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8540 `show-subtree' to display the item, so that drawers and logbooks stay
8541 folded."
8542 (interactive "P")
8543 (let ((win (selected-window)))
8544 (if (and (window-live-p org-agenda-show-window)
8545 (eq this-command last-command))
8546 (progn
8547 (select-window org-agenda-show-window)
8548 (ignore-errors (scroll-up)))
8549 (org-agenda-goto t)
8550 (if arg (org-show-entry) (show-subtree))
8551 (setq org-agenda-show-window (selected-window)))
8552 (select-window win)))
8554 (defun org-agenda-show-scroll-down ()
8555 "Scroll down the window showing the agenda."
8556 (interactive)
8557 (let ((win (selected-window)))
8558 (when (window-live-p org-agenda-show-window)
8559 (select-window org-agenda-show-window)
8560 (ignore-errors (scroll-down))
8561 (select-window win))))
8563 (defun org-agenda-show-1 (&optional more)
8564 "Display the Org-mode file which contains the item at point.
8565 The prefix arg selects the amount of information to display:
8567 0 hide the subtree
8568 1 just show the entry according to defaults.
8569 2 show the children view
8570 3 show the subtree view
8571 4 show the entire subtree and any LOGBOOK drawers
8572 5 show the entire subtree and any drawers
8573 With prefix argument FULL-ENTRY, make the entire entry visible
8574 if it was hidden in the outline."
8575 (interactive "p")
8576 (let ((win (selected-window)))
8577 (org-agenda-goto t)
8578 (org-recenter-heading 1)
8579 (cond
8580 ((= more 0)
8581 (hide-subtree)
8582 (save-excursion
8583 (org-back-to-heading)
8584 (run-hook-with-args 'org-cycle-hook 'folded))
8585 (message "Remote: FOLDED"))
8586 ((and (org-called-interactively-p 'any) (= more 1))
8587 (message "Remote: show with default settings"))
8588 ((= more 2)
8589 (show-entry)
8590 (show-children)
8591 (save-excursion
8592 (org-back-to-heading)
8593 (run-hook-with-args 'org-cycle-hook 'children))
8594 (message "Remote: CHILDREN"))
8595 ((= more 3)
8596 (show-subtree)
8597 (save-excursion
8598 (org-back-to-heading)
8599 (run-hook-with-args 'org-cycle-hook 'subtree))
8600 (message "Remote: SUBTREE"))
8601 ((= more 4)
8602 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8603 (org-drawer-regexp
8604 (concat "^[ \t]*:\\("
8605 (mapconcat 'regexp-quote org-drawers "\\|")
8606 "\\):[ \t]*$")))
8607 (show-subtree)
8608 (save-excursion
8609 (org-back-to-heading)
8610 (org-cycle-hide-drawers 'subtree)))
8611 (message "Remote: SUBTREE AND LOGBOOK"))
8612 ((> more 4)
8613 (show-subtree)
8614 (message "Remote: SUBTREE AND ALL DRAWERS")))
8615 (select-window win)))
8617 (defun org-recenter-heading (n)
8618 (save-excursion
8619 (org-back-to-heading)
8620 (recenter n)))
8622 (defvar org-agenda-cycle-counter nil)
8623 (defun org-agenda-cycle-show (&optional n)
8624 "Show the current entry in another window, with default settings.
8625 Default settings are taken from `org-show-hierarchy-above' and siblings.
8626 When use repeatedly in immediate succession, the remote entry will cycle
8627 through visibility
8629 children -> subtree -> folded
8631 When called with a numeric prefix arg, that arg will be passed through to
8632 `org-agenda-show-1'. For the interpretation of that argument, see the
8633 docstring of `org-agenda-show-1'."
8634 (interactive "P")
8635 (if (integerp n)
8636 (setq org-agenda-cycle-counter n)
8637 (if (not (eq last-command this-command))
8638 (setq org-agenda-cycle-counter 1)
8639 (if (equal org-agenda-cycle-counter 0)
8640 (setq org-agenda-cycle-counter 2)
8641 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8642 (if (> org-agenda-cycle-counter 3)
8643 (setq org-agenda-cycle-counter 0)))))
8644 (org-agenda-show-1 org-agenda-cycle-counter))
8646 (defun org-agenda-recenter (arg)
8647 "Display the Org-mode file which contains the item at point and recenter."
8648 (interactive "P")
8649 (let ((win (selected-window)))
8650 (org-agenda-goto t)
8651 (recenter arg)
8652 (select-window win)))
8654 (defun org-agenda-show-mouse (ev)
8655 "Display the Org-mode file which contains the item at the mouse click."
8656 (interactive "e")
8657 (mouse-set-point ev)
8658 (org-agenda-show))
8660 (defun org-agenda-check-no-diary ()
8661 "Check if the entry is a diary link and abort if yes."
8662 (if (org-get-at-bol 'org-agenda-diary-link)
8663 (org-agenda-error)))
8665 (defun org-agenda-error ()
8666 (error "Command not allowed in this line"))
8668 (defun org-agenda-tree-to-indirect-buffer (arg)
8669 "Show the subtree corresponding to the current entry in an indirect buffer.
8670 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8672 With a numerical prefix ARG, go up to this level and then take that tree.
8673 With a negative numeric ARG, go up by this number of levels.
8674 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8675 use the dedicated frame)."
8676 (interactive "P")
8677 (if current-prefix-arg
8678 (org-agenda-do-tree-to-indirect-buffer arg)
8679 (let ((agenda-buffer (buffer-name))
8680 (agenda-window (selected-window))
8681 (indirect-window
8682 (and org-last-indirect-buffer
8683 (get-buffer-window org-last-indirect-buffer))))
8684 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8685 (unless (or (eq org-indirect-buffer-display 'new-frame)
8686 (eq org-indirect-buffer-display 'dedicated-frame))
8687 (unwind-protect
8688 (unless (and indirect-window (window-live-p indirect-window))
8689 (setq indirect-window (split-window agenda-window)))
8690 (and indirect-window (select-window indirect-window))
8691 (switch-to-buffer org-last-indirect-buffer :norecord)
8692 (fit-window-to-buffer indirect-window)))
8693 (select-window (get-buffer-window agenda-buffer)))))
8695 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8696 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8697 (org-agenda-check-no-diary)
8698 (let* ((marker (or (org-get-at-bol 'org-marker)
8699 (org-agenda-error)))
8700 (buffer (marker-buffer marker))
8701 (pos (marker-position marker)))
8702 (with-current-buffer buffer
8703 (save-excursion
8704 (goto-char pos)
8705 (funcall 'org-tree-to-indirect-buffer arg)))))
8707 (defvar org-last-heading-marker (make-marker)
8708 "Marker pointing to the headline that last changed its TODO state
8709 by a remote command from the agenda.")
8711 (defun org-agenda-todo-nextset ()
8712 "Switch TODO entry to next sequence."
8713 (interactive)
8714 (org-agenda-todo 'nextset))
8716 (defun org-agenda-todo-previousset ()
8717 "Switch TODO entry to previous sequence."
8718 (interactive)
8719 (org-agenda-todo 'previousset))
8721 (defun org-agenda-todo (&optional arg)
8722 "Cycle TODO state of line at point, also in Org-mode file.
8723 This changes the line at point, all other lines in the agenda referring to
8724 the same tree node, and the headline of the tree node in the Org-mode file."
8725 (interactive "P")
8726 (org-agenda-check-no-diary)
8727 (let* ((col (current-column))
8728 (marker (or (org-get-at-bol 'org-marker)
8729 (org-agenda-error)))
8730 (buffer (marker-buffer marker))
8731 (pos (marker-position marker))
8732 (hdmarker (org-get-at-bol 'org-hd-marker))
8733 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8734 (inhibit-read-only t)
8735 org-agenda-headline-snapshot-before-repeat newhead just-one)
8736 (org-with-remote-undo buffer
8737 (with-current-buffer buffer
8738 (widen)
8739 (goto-char pos)
8740 (org-show-context 'agenda)
8741 (save-excursion
8742 (and (outline-next-heading)
8743 (org-flag-heading nil))) ; show the next heading
8744 (let ((current-prefix-arg arg))
8745 (call-interactively 'org-todo))
8746 (and (bolp) (forward-char 1))
8747 (setq newhead (org-get-heading))
8748 (when (and (org-bound-and-true-p
8749 org-agenda-headline-snapshot-before-repeat)
8750 (not (equal org-agenda-headline-snapshot-before-repeat
8751 newhead))
8752 todayp)
8753 (setq newhead org-agenda-headline-snapshot-before-repeat
8754 just-one t))
8755 (save-excursion
8756 (org-back-to-heading)
8757 (move-marker org-last-heading-marker (point))))
8758 (beginning-of-line 1)
8759 (save-excursion
8760 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8761 (org-move-to-column col))))
8763 (defun org-agenda-add-note (&optional arg)
8764 "Add a time-stamped note to the entry at point."
8765 (interactive "P")
8766 (org-agenda-check-no-diary)
8767 (let* ((marker (or (org-get-at-bol 'org-marker)
8768 (org-agenda-error)))
8769 (buffer (marker-buffer marker))
8770 (pos (marker-position marker))
8771 (hdmarker (org-get-at-bol 'org-hd-marker))
8772 (inhibit-read-only t))
8773 (with-current-buffer buffer
8774 (widen)
8775 (goto-char pos)
8776 (org-show-context 'agenda)
8777 (save-excursion
8778 (and (outline-next-heading)
8779 (org-flag-heading nil))) ; show the next heading
8780 (org-add-note))))
8782 (defun org-agenda-change-all-lines (newhead hdmarker
8783 &optional fixface just-this)
8784 "Change all lines in the agenda buffer which match HDMARKER.
8785 The new content of the line will be NEWHEAD (as modified by
8786 `org-agenda-format-item'). HDMARKER is checked with
8787 `equal' against all `org-hd-marker' text properties in the file.
8788 If FIXFACE is non-nil, the face of each item is modified according to
8789 the new TODO state.
8790 If JUST-THIS is non-nil, change just the current line, not all.
8791 If FORCE-TAGS is non nil, the car of it returns the new tags."
8792 (let* ((inhibit-read-only t)
8793 (line (org-current-line))
8794 (org-agenda-buffer (current-buffer))
8795 (thetags (with-current-buffer (marker-buffer hdmarker)
8796 (save-excursion (save-restriction (widen)
8797 (goto-char hdmarker)
8798 (org-get-tags-at)))))
8799 props m pl undone-face done-face finish new dotime level cat tags)
8800 (save-excursion
8801 (goto-char (point-max))
8802 (beginning-of-line 1)
8803 (while (not finish)
8804 (setq finish (bobp))
8805 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8806 (or (not just-this) (= (org-current-line) line))
8807 (equal m hdmarker))
8808 (setq props (text-properties-at (point))
8809 dotime (org-get-at-bol 'dotime)
8810 cat (org-get-at-bol 'org-category)
8811 level (org-get-at-bol 'level)
8812 tags thetags
8814 (let ((org-prefix-format-compiled
8815 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8816 org-prefix-format-compiled))
8817 (extra (org-get-at-bol 'extra)))
8818 (with-current-buffer (marker-buffer hdmarker)
8819 (save-excursion
8820 (save-restriction
8821 (widen)
8822 (org-agenda-format-item extra newhead level cat tags dotime)))))
8823 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8824 undone-face (org-get-at-bol 'undone-face)
8825 done-face (org-get-at-bol 'done-face))
8826 (beginning-of-line 1)
8827 (cond
8828 ((equal new "")
8829 (and (looking-at ".*\n?") (replace-match "")))
8830 ((looking-at ".*")
8831 (replace-match new t t)
8832 (beginning-of-line 1)
8833 (add-text-properties (point-at-bol) (point-at-eol) props)
8834 (when fixface
8835 (add-text-properties
8836 (point-at-bol) (point-at-eol)
8837 (list 'face
8838 (if org-last-todo-state-is-todo
8839 undone-face done-face))))
8840 (org-agenda-highlight-todo 'line)
8841 (beginning-of-line 1))
8842 (t (error "Line update did not work")))
8843 (save-restriction
8844 (narrow-to-region (point-at-bol) (point-at-eol))
8845 (org-agenda-finalize)))
8846 (beginning-of-line 0)))))
8848 (defun org-agenda-align-tags (&optional line)
8849 "Align all tags in agenda items to `org-agenda-tags-column'."
8850 (let ((inhibit-read-only t) l c)
8851 (save-excursion
8852 (goto-char (if line (point-at-bol) (point-min)))
8853 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8854 (if line (point-at-eol) nil) t)
8855 (add-text-properties
8856 (match-beginning 2) (match-end 2)
8857 (list 'face (delq nil (let ((prop (get-text-property
8858 (match-beginning 2) 'face)))
8859 (or (listp prop) (setq prop (list prop)))
8860 (if (memq 'org-tag prop)
8861 prop
8862 (cons 'org-tag prop))))))
8863 (setq l (- (match-end 2) (match-beginning 2))
8864 c (if (< org-agenda-tags-column 0)
8865 (- (abs org-agenda-tags-column) l)
8866 org-agenda-tags-column))
8867 (delete-region (match-beginning 1) (match-end 1))
8868 (goto-char (match-beginning 1))
8869 (insert (org-add-props
8870 (make-string (max 1 (- c (current-column))) ?\ )
8871 (plist-put (copy-sequence (text-properties-at (point)))
8872 'face nil))))
8873 (goto-char (point-min))
8874 (org-font-lock-add-tag-faces (point-max)))))
8876 (defun org-agenda-priority-up ()
8877 "Increase the priority of line at point, also in Org-mode file."
8878 (interactive)
8879 (org-agenda-priority 'up))
8881 (defun org-agenda-priority-down ()
8882 "Decrease the priority of line at point, also in Org-mode file."
8883 (interactive)
8884 (org-agenda-priority 'down))
8886 (defun org-agenda-priority (&optional force-direction)
8887 "Set the priority of line at point, also in Org-mode file.
8888 This changes the line at point, all other lines in the agenda referring to
8889 the same tree node, and the headline of the tree node in the Org-mode file.
8890 Called with a universal prefix arg, show the priority instead of setting it."
8891 (interactive "P")
8892 (if (equal force-direction '(4))
8893 (org-show-priority)
8894 (unless org-enable-priority-commands
8895 (error "Priority commands are disabled"))
8896 (org-agenda-check-no-diary)
8897 (let* ((marker (or (org-get-at-bol 'org-marker)
8898 (org-agenda-error)))
8899 (hdmarker (org-get-at-bol 'org-hd-marker))
8900 (buffer (marker-buffer hdmarker))
8901 (pos (marker-position hdmarker))
8902 (inhibit-read-only t)
8903 newhead)
8904 (org-with-remote-undo buffer
8905 (with-current-buffer buffer
8906 (widen)
8907 (goto-char pos)
8908 (org-show-context 'agenda)
8909 (save-excursion
8910 (and (outline-next-heading)
8911 (org-flag-heading nil))) ; show the next heading
8912 (funcall 'org-priority force-direction)
8913 (end-of-line 1)
8914 (setq newhead (org-get-heading)))
8915 (org-agenda-change-all-lines newhead hdmarker)
8916 (beginning-of-line 1)))))
8918 ;; FIXME: should fix the tags property of the agenda line.
8919 (defun org-agenda-set-tags (&optional tag onoff)
8920 "Set tags for the current headline."
8921 (interactive)
8922 (org-agenda-check-no-diary)
8923 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8924 (call-interactively 'org-change-tag-in-region)
8925 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8926 (org-agenda-error)))
8927 (buffer (marker-buffer hdmarker))
8928 (pos (marker-position hdmarker))
8929 (inhibit-read-only t)
8930 newhead)
8931 (org-with-remote-undo buffer
8932 (with-current-buffer buffer
8933 (widen)
8934 (goto-char pos)
8935 (save-excursion
8936 (org-show-context 'agenda))
8937 (save-excursion
8938 (and (outline-next-heading)
8939 (org-flag-heading nil))) ; show the next heading
8940 (goto-char pos)
8941 (if tag
8942 (org-toggle-tag tag onoff)
8943 (call-interactively 'org-set-tags))
8944 (end-of-line 1)
8945 (setq newhead (org-get-heading)))
8946 (org-agenda-change-all-lines newhead hdmarker)
8947 (beginning-of-line 1)))))
8949 (defun org-agenda-set-property ()
8950 "Set a property for the current headline."
8951 (interactive)
8952 (org-agenda-check-no-diary)
8953 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8954 (org-agenda-error)))
8955 (buffer (marker-buffer hdmarker))
8956 (pos (marker-position hdmarker))
8957 (inhibit-read-only t)
8958 newhead)
8959 (org-with-remote-undo buffer
8960 (with-current-buffer buffer
8961 (widen)
8962 (goto-char pos)
8963 (save-excursion
8964 (org-show-context 'agenda))
8965 (save-excursion
8966 (and (outline-next-heading)
8967 (org-flag-heading nil))) ; show the next heading
8968 (goto-char pos)
8969 (call-interactively 'org-set-property)))))
8971 (defun org-agenda-set-effort ()
8972 "Set the effort property for the current headline."
8973 (interactive)
8974 (org-agenda-check-no-diary)
8975 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8976 (org-agenda-error)))
8977 (buffer (marker-buffer hdmarker))
8978 (pos (marker-position hdmarker))
8979 (inhibit-read-only t)
8980 newhead)
8981 (org-with-remote-undo buffer
8982 (with-current-buffer buffer
8983 (widen)
8984 (goto-char pos)
8985 (save-excursion
8986 (org-show-context 'agenda))
8987 (save-excursion
8988 (and (outline-next-heading)
8989 (org-flag-heading nil))) ; show the next heading
8990 (goto-char pos)
8991 (call-interactively 'org-set-effort)
8992 (end-of-line 1)
8993 (setq newhead (org-get-heading)))
8994 (org-agenda-change-all-lines newhead hdmarker))))
8996 (defun org-agenda-toggle-archive-tag ()
8997 "Toggle the archive tag for the current entry."
8998 (interactive)
8999 (org-agenda-check-no-diary)
9000 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9001 (org-agenda-error)))
9002 (buffer (marker-buffer hdmarker))
9003 (pos (marker-position hdmarker))
9004 (inhibit-read-only t)
9005 newhead)
9006 (org-with-remote-undo buffer
9007 (with-current-buffer buffer
9008 (widen)
9009 (goto-char pos)
9010 (org-show-context 'agenda)
9011 (save-excursion
9012 (and (outline-next-heading)
9013 (org-flag-heading nil))) ; show the next heading
9014 (call-interactively 'org-toggle-archive-tag)
9015 (end-of-line 1)
9016 (setq newhead (org-get-heading)))
9017 (org-agenda-change-all-lines newhead hdmarker)
9018 (beginning-of-line 1))))
9020 (defun org-agenda-do-date-later (arg)
9021 (interactive "P")
9022 (cond
9023 ((or (equal arg '(16))
9024 (memq last-command
9025 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9026 (setq this-command 'org-agenda-date-later-minutes)
9027 (org-agenda-date-later-minutes 1))
9028 ((or (equal arg '(4))
9029 (memq last-command
9030 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9031 (setq this-command 'org-agenda-date-later-hours)
9032 (org-agenda-date-later-hours 1))
9034 (org-agenda-date-later (prefix-numeric-value arg)))))
9036 (defun org-agenda-do-date-earlier (arg)
9037 (interactive "P")
9038 (cond
9039 ((or (equal arg '(16))
9040 (memq last-command
9041 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9042 (setq this-command 'org-agenda-date-earlier-minutes)
9043 (org-agenda-date-earlier-minutes 1))
9044 ((or (equal arg '(4))
9045 (memq last-command
9046 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9047 (setq this-command 'org-agenda-date-earlier-hours)
9048 (org-agenda-date-earlier-hours 1))
9050 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9052 (defun org-agenda-date-later (arg &optional what)
9053 "Change the date of this item to ARG day(s) later."
9054 (interactive "p")
9055 (org-agenda-check-type t 'agenda 'timeline)
9056 (org-agenda-check-no-diary)
9057 (let* ((marker (or (org-get-at-bol 'org-marker)
9058 (org-agenda-error)))
9059 (buffer (marker-buffer marker))
9060 (pos (marker-position marker))
9061 cdate today)
9062 (org-with-remote-undo buffer
9063 (with-current-buffer buffer
9064 (widen)
9065 (goto-char pos)
9066 (if (not (org-at-timestamp-p))
9067 (error "Cannot find time stamp"))
9068 (when (and org-agenda-move-date-from-past-immediately-to-today
9069 (equal arg 1)
9070 (or (not what) (eq what 'day))
9071 (not (save-match-data (org-at-date-range-p))))
9072 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9073 cdate (calendar-absolute-from-gregorian
9074 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9075 today (org-today))
9076 (if (> today cdate)
9077 ;; immediately shift to today
9078 (setq arg (- today cdate))))
9079 (org-timestamp-change arg (or what 'day))
9080 (when (and (org-at-date-range-p)
9081 (re-search-backward org-tr-regexp-both (point-at-bol)))
9082 (let ((end org-last-changed-timestamp))
9083 (org-timestamp-change arg (or what 'day))
9084 (setq org-last-changed-timestamp
9085 (concat org-last-changed-timestamp "--" end)))))
9086 (org-agenda-show-new-time marker org-last-changed-timestamp))
9087 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9089 (defun org-agenda-date-earlier (arg &optional what)
9090 "Change the date of this item to ARG day(s) earlier."
9091 (interactive "p")
9092 (org-agenda-date-later (- arg) what))
9094 (defun org-agenda-date-later-minutes (arg)
9095 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9096 (interactive "p")
9097 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9098 (org-agenda-date-later arg 'minute))
9100 (defun org-agenda-date-earlier-minutes (arg)
9101 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9102 (interactive "p")
9103 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9104 (org-agenda-date-earlier arg 'minute))
9106 (defun org-agenda-date-later-hours (arg)
9107 "Change the time of this item, in hour steps."
9108 (interactive "p")
9109 (org-agenda-date-later arg 'hour))
9111 (defun org-agenda-date-earlier-hours (arg)
9112 "Change the time of this item, in hour steps."
9113 (interactive "p")
9114 (org-agenda-date-earlier arg 'hour))
9116 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9117 "Show new date stamp via text properties."
9118 ;; We use text properties to make this undoable
9119 (let ((inhibit-read-only t))
9120 (setq stamp (concat prefix " => " stamp " "))
9121 (save-excursion
9122 (goto-char (point-max))
9123 (while (not (bobp))
9124 (when (equal marker (org-get-at-bol 'org-marker))
9125 (org-move-to-column (- (window-width) (length stamp)) t)
9126 (org-agenda-fix-tags-filter-overlays-at (point))
9127 (if (featurep 'xemacs)
9128 ;; Use `duplicable' property to trigger undo recording
9129 (let ((ex (make-extent nil nil))
9130 (gl (make-glyph stamp)))
9131 (set-glyph-face gl 'secondary-selection)
9132 (set-extent-properties
9133 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9134 (insert-extent ex (1- (point)) (point-at-eol)))
9135 (add-text-properties
9136 (1- (point)) (point-at-eol)
9137 (list 'display (org-add-props stamp nil
9138 'face 'secondary-selection))))
9139 (beginning-of-line 1))
9140 (beginning-of-line 0)))))
9142 (defun org-agenda-date-prompt (arg)
9143 "Change the date of this item. Date is prompted for, with default today.
9144 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9145 be used to request time specification in the time stamp."
9146 (interactive "P")
9147 (org-agenda-check-type t 'agenda 'timeline)
9148 (org-agenda-check-no-diary)
9149 (let* ((marker (or (org-get-at-bol 'org-marker)
9150 (org-agenda-error)))
9151 (buffer (marker-buffer marker))
9152 (pos (marker-position marker)))
9153 (org-with-remote-undo buffer
9154 (with-current-buffer buffer
9155 (widen)
9156 (goto-char pos)
9157 (if (not (org-at-timestamp-p t))
9158 (error "Cannot find time stamp"))
9159 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9160 (org-agenda-show-new-time marker org-last-changed-timestamp))
9161 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9163 (defun org-agenda-schedule (arg &optional time)
9164 "Schedule the item at point.
9165 ARG is passed through to `org-schedule'."
9166 (interactive "P")
9167 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9168 (org-agenda-check-no-diary)
9169 (let* ((marker (or (org-get-at-bol 'org-marker)
9170 (org-agenda-error)))
9171 (type (marker-insertion-type marker))
9172 (buffer (marker-buffer marker))
9173 (pos (marker-position marker))
9174 (org-insert-labeled-timestamps-at-point nil)
9176 (set-marker-insertion-type marker t)
9177 (org-with-remote-undo buffer
9178 (with-current-buffer buffer
9179 (widen)
9180 (goto-char pos)
9181 (setq ts (org-schedule arg time)))
9182 (org-agenda-show-new-time marker ts " S"))
9183 (message "%s" ts)))
9185 (defun org-agenda-deadline (arg &optional time)
9186 "Schedule the item at point.
9187 ARG is passed through to `org-deadline'."
9188 (interactive "P")
9189 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9190 (org-agenda-check-no-diary)
9191 (let* ((marker (or (org-get-at-bol 'org-marker)
9192 (org-agenda-error)))
9193 (buffer (marker-buffer marker))
9194 (pos (marker-position marker))
9195 (org-insert-labeled-timestamps-at-point nil)
9197 (org-with-remote-undo buffer
9198 (with-current-buffer buffer
9199 (widen)
9200 (goto-char pos)
9201 (setq ts (org-deadline arg time)))
9202 (org-agenda-show-new-time marker ts " D"))
9203 (message "%s" ts)))
9205 (defun org-agenda-clock-in (&optional arg)
9206 "Start the clock on the currently selected item."
9207 (interactive "P")
9208 (org-agenda-check-no-diary)
9209 (if (equal arg '(4))
9210 (org-clock-in arg)
9211 (let* ((marker (or (org-get-at-bol 'org-marker)
9212 (org-agenda-error)))
9213 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9214 (pos (marker-position marker))
9215 (col (current-column))
9216 newhead)
9217 (org-with-remote-undo (marker-buffer marker)
9218 (with-current-buffer (marker-buffer marker)
9219 (widen)
9220 (goto-char pos)
9221 (org-show-context 'agenda)
9222 (org-show-entry)
9223 (org-cycle-hide-drawers 'children)
9224 (org-clock-in arg)
9225 (setq newhead (org-get-heading)))
9226 (org-agenda-change-all-lines newhead hdmarker))
9227 (org-move-to-column col))))
9229 (defun org-agenda-clock-out ()
9230 "Stop the currently running clock."
9231 (interactive)
9232 (unless (marker-buffer org-clock-marker)
9233 (error "No running clock"))
9234 (let ((marker (make-marker)) (col (current-column)) newhead)
9235 (org-with-remote-undo (marker-buffer org-clock-marker)
9236 (with-current-buffer (marker-buffer org-clock-marker)
9237 (save-excursion
9238 (save-restriction
9239 (widen)
9240 (goto-char org-clock-marker)
9241 (org-back-to-heading t)
9242 (move-marker marker (point))
9243 (org-clock-out)
9244 (setq newhead (org-get-heading))))))
9245 (org-agenda-change-all-lines newhead marker)
9246 (move-marker marker nil)
9247 (org-move-to-column col)
9248 (org-agenda-unmark-clocking-task)))
9250 (defun org-agenda-clock-cancel (&optional arg)
9251 "Cancel the currently running clock."
9252 (interactive "P")
9253 (unless (marker-buffer org-clock-marker)
9254 (user-error "No running clock"))
9255 (org-with-remote-undo (marker-buffer org-clock-marker)
9256 (org-clock-cancel)))
9258 (defun org-agenda-clock-goto ()
9259 "Jump to the currently clocked in task within the agenda.
9260 If the currently clocked in task is not listed in the agenda
9261 buffer, display it in another window."
9262 (interactive)
9263 (let (pos)
9264 (mapc (lambda (o)
9265 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9266 (setq pos (overlay-start o))))
9267 (overlays-in (point-min) (point-max)))
9268 (cond (pos (goto-char pos))
9269 ;; If the currently clocked entry is not in the agenda
9270 ;; buffer, we visit it in another window:
9271 (org-clock-current-task
9272 (org-switch-to-buffer-other-window (org-clock-goto)))
9273 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9275 (defun org-agenda-diary-entry-in-org-file ()
9276 "Make a diary entry in the file `org-agenda-diary-file'."
9277 (let (d1 d2 char (text "") dp1 dp2)
9278 (if (equal (buffer-name) "*Calendar*")
9279 (setq d1 (calendar-cursor-to-date t)
9280 d2 (car calendar-mark-ring))
9281 (setq dp1 (get-text-property (point-at-bol) 'day))
9282 (unless dp1 (user-error "No date defined in current line"))
9283 (setq d1 (calendar-gregorian-from-absolute dp1)
9284 d2 (and (ignore-errors (mark))
9285 (save-excursion
9286 (goto-char (mark))
9287 (setq dp2 (get-text-property (point-at-bol) 'day)))
9288 (calendar-gregorian-from-absolute dp2))))
9289 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9290 (setq char (read-char-exclusive))
9291 (cond
9292 ((equal char ?d)
9293 (setq text (read-string "Day entry: "))
9294 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9295 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9296 ((equal char ?a)
9297 (setq d1 (list (car d1) (nth 1 d1)
9298 (read-number (format "Reference year [%d]: " (nth 2 d1))
9299 (nth 2 d1))))
9300 (setq text (read-string "Anniversary (use %d to show years): "))
9301 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9302 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9303 ((equal char ?b)
9304 (setq text (read-string "Block entry: "))
9305 (unless (and d1 d2 (not (equal d1 d2)))
9306 (user-error "No block of days selected"))
9307 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9308 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9309 ((equal char ?j)
9310 (org-switch-to-buffer-other-window
9311 (find-file-noselect org-agenda-diary-file))
9312 (require 'org-datetree)
9313 (org-datetree-find-date-create d1)
9314 (org-reveal t))
9315 (t (user-error "Invalid selection character `%c'" char)))))
9317 (defcustom org-agenda-insert-diary-strategy 'date-tree
9318 "Where in `org-agenda-diary-file' should new entries be added?
9319 Valid values:
9321 date-tree in the date tree, as child of the date
9322 top-level as top-level entries at the end of the file."
9323 :group 'org-agenda
9324 :type '(choice
9325 (const :tag "in a date tree" date-tree)
9326 (const :tag "as top level at end of file" top-level)))
9328 (defcustom org-agenda-insert-diary-extract-time nil
9329 "Non-nil means extract any time specification from the diary entry."
9330 :group 'org-agenda
9331 :version "24.1"
9332 :type 'boolean)
9334 (defcustom org-agenda-bulk-mark-char ">"
9335 "A single-character string to be used as the bulk mark."
9336 :group 'org-agenda
9337 :version "24.1"
9338 :type 'string)
9340 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9341 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9342 If TEXT is not empty, it will become the headline of the new entry, and
9343 the resulting entry will not be shown. When TEXT is empty, switch to
9344 `org-agenda-diary-file' and let the user finish the entry there."
9345 (let ((cw (current-window-configuration)))
9346 (org-switch-to-buffer-other-window
9347 (find-file-noselect org-agenda-diary-file))
9348 (widen)
9349 (goto-char (point-min))
9350 (cond
9351 ((eq type 'anniversary)
9352 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9353 (progn
9354 (or (org-at-heading-p t)
9355 (progn
9356 (outline-next-heading)
9357 (insert "* Anniversaries\n\n")
9358 (beginning-of-line -1)))))
9359 (outline-next-heading)
9360 (org-back-over-empty-lines)
9361 (backward-char 1)
9362 (insert "\n")
9363 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9364 (nth 2 d1) (car d1) (nth 1 d1) text)))
9365 ((eq type 'day)
9366 (let ((org-prefix-has-time t)
9367 (org-agenda-time-leading-zero t)
9368 fmt time time2)
9369 (if org-agenda-insert-diary-extract-time
9370 ;; Use org-agenda-format-item to parse text for a time-range and
9371 ;; remove it. FIXME: This is a hack, we should refactor
9372 ;; that function to make time extraction available separately
9373 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9374 time (get-text-property 0 'time fmt)
9375 time2 (if (> (length time) 0)
9376 ;; split-string removes trailing ...... if
9377 ;; no end time given. First space
9378 ;; separates time from date.
9379 (concat " " (car (split-string time "\\.")))
9380 nil)
9381 text (get-text-property 0 'txt fmt)))
9382 (if (eq org-agenda-insert-diary-strategy 'top-level)
9383 (org-agenda-insert-diary-as-top-level text)
9384 (require 'org-datetree)
9385 (org-datetree-find-date-create d1)
9386 (org-agenda-insert-diary-make-new-entry text))
9387 (org-insert-time-stamp (org-time-from-absolute
9388 (calendar-absolute-from-gregorian d1))
9389 nil nil nil nil time2))
9390 (end-of-line 0))
9391 ((eq type 'block)
9392 (if (> (calendar-absolute-from-gregorian d1)
9393 (calendar-absolute-from-gregorian d2))
9394 (setq d1 (prog1 d2 (setq d2 d1))))
9395 (if (eq org-agenda-insert-diary-strategy 'top-level)
9396 (org-agenda-insert-diary-as-top-level text)
9397 (require 'org-datetree)
9398 (org-datetree-find-date-create d1)
9399 (org-agenda-insert-diary-make-new-entry text))
9400 (org-insert-time-stamp (org-time-from-absolute
9401 (calendar-absolute-from-gregorian d1)))
9402 (insert "--")
9403 (org-insert-time-stamp (org-time-from-absolute
9404 (calendar-absolute-from-gregorian d2)))
9405 (end-of-line 0)))
9406 (if (string-match "\\S-" text)
9407 (progn
9408 (set-window-configuration cw)
9409 (message "%s entry added to %s"
9410 (capitalize (symbol-name type))
9411 (abbreviate-file-name org-agenda-diary-file)))
9412 (org-reveal t)
9413 (message "Please finish entry here"))))
9415 (defun org-agenda-insert-diary-as-top-level (text)
9416 "Make new entry as a top-level entry at the end of the file.
9417 Add TEXT as headline, and position the cursor in the second line so that
9418 a timestamp can be added there."
9419 (widen)
9420 (goto-char (point-max))
9421 (or (bolp) (insert "\n"))
9422 (insert "* " text "\n")
9423 (if org-adapt-indentation (org-indent-to-column 2)))
9425 (defun org-agenda-insert-diary-make-new-entry (text)
9426 "Make new entry as last child of current entry.
9427 Add TEXT as headline, and position the cursor in the second line so that
9428 a timestamp can be added there."
9429 (let ((org-show-following-heading t)
9430 (org-show-siblings t)
9431 (org-show-hierarchy-above t)
9432 (org-show-entry-below t)
9433 col)
9434 (outline-next-heading)
9435 (org-back-over-empty-lines)
9436 (or (looking-at "[ \t]*$")
9437 (progn (insert "\n") (backward-char 1)))
9438 (org-insert-heading nil t)
9439 (org-do-demote)
9440 (setq col (current-column))
9441 (insert text "\n")
9442 (if org-adapt-indentation (org-indent-to-column col))
9443 (let ((org-show-following-heading t)
9444 (org-show-siblings t)
9445 (org-show-hierarchy-above t)
9446 (org-show-entry-below t))
9447 (org-show-context))))
9449 (defun org-agenda-diary-entry ()
9450 "Make a diary entry, like the `i' command from the calendar.
9451 All the standard commands work: block, weekly etc.
9452 When `org-agenda-diary-file' points to a file,
9453 `org-agenda-diary-entry-in-org-file' is called instead to create
9454 entries in that Org-mode file."
9455 (interactive)
9456 (if (not (eq org-agenda-diary-file 'diary-file))
9457 (org-agenda-diary-entry-in-org-file)
9458 (require 'diary-lib)
9459 (let* ((char (progn
9460 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9461 (read-char-exclusive)))
9462 (cmd (cdr (assoc char
9463 '((?d . insert-diary-entry)
9464 (?w . insert-weekly-diary-entry)
9465 (?m . insert-monthly-diary-entry)
9466 (?y . insert-yearly-diary-entry)
9467 (?a . insert-anniversary-diary-entry)
9468 (?b . insert-block-diary-entry)
9469 (?c . insert-cyclic-diary-entry)))))
9470 (oldf (symbol-function 'calendar-cursor-to-date))
9471 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9472 (point (point))
9473 (mark (or (mark t) (point))))
9474 (unless cmd
9475 (user-error "No command associated with <%c>" char))
9476 (unless (and (get-text-property point 'day)
9477 (or (not (equal ?b char))
9478 (get-text-property mark 'day)))
9479 (user-error "Don't know which date to use for diary entry"))
9480 ;; We implement this by hacking the `calendar-cursor-to-date' function
9481 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9482 (let ((calendar-mark-ring
9483 (list (calendar-gregorian-from-absolute
9484 (or (get-text-property mark 'day)
9485 (get-text-property point 'day))))))
9486 (unwind-protect
9487 (progn
9488 (fset 'calendar-cursor-to-date
9489 (lambda (&optional error dummy)
9490 (calendar-gregorian-from-absolute
9491 (get-text-property point 'day))))
9492 (call-interactively cmd))
9493 (fset 'calendar-cursor-to-date oldf))))))
9495 (defun org-agenda-execute-calendar-command (cmd)
9496 "Execute a calendar command from the agenda with date from cursor."
9497 (org-agenda-check-type t 'agenda 'timeline)
9498 (require 'diary-lib)
9499 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9500 (user-error "Don't know which date to use for the calendar command"))
9501 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9502 (point (point))
9503 (date (calendar-gregorian-from-absolute
9504 (get-text-property point 'day)))
9505 ;; the following 2 vars are needed in the calendar
9506 (displayed-month (car date))
9507 (displayed-year (nth 2 date)))
9508 (unwind-protect
9509 (progn
9510 (fset 'calendar-cursor-to-date
9511 (lambda (&optional error dummy)
9512 (calendar-gregorian-from-absolute
9513 (get-text-property point 'day))))
9514 (call-interactively cmd))
9515 (fset 'calendar-cursor-to-date oldf))))
9517 (defun org-agenda-phases-of-moon ()
9518 "Display the phases of the moon for the 3 months around the cursor date."
9519 (interactive)
9520 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9522 (defun org-agenda-holidays ()
9523 "Display the holidays for the 3 months around the cursor date."
9524 (interactive)
9525 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9527 (defvar calendar-longitude) ; defined in calendar.el
9528 (defvar calendar-latitude) ; defined in calendar.el
9529 (defvar calendar-location-name) ; defined in calendar.el
9531 (defun org-agenda-sunrise-sunset (arg)
9532 "Display sunrise and sunset for the cursor date.
9533 Latitude and longitude can be specified with the variables
9534 `calendar-latitude' and `calendar-longitude'. When called with prefix
9535 argument, latitude and longitude will be prompted for."
9536 (interactive "P")
9537 (require 'solar)
9538 (let ((calendar-longitude (if arg nil calendar-longitude))
9539 (calendar-latitude (if arg nil calendar-latitude))
9540 (calendar-location-name
9541 (if arg "the given coordinates" calendar-location-name)))
9542 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9544 (defun org-agenda-goto-calendar ()
9545 "Open the Emacs calendar with the date at the cursor."
9546 (interactive)
9547 (org-agenda-check-type t 'agenda 'timeline)
9548 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9549 (user-error "Don't know which date to open in calendar")))
9550 (date (calendar-gregorian-from-absolute day))
9551 (calendar-move-hook nil)
9552 (calendar-view-holidays-initially-flag nil)
9553 (calendar-view-diary-initially-flag nil))
9554 (calendar)
9555 (calendar-goto-date date)))
9557 ;;;###autoload
9558 (defun org-calendar-goto-agenda ()
9559 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9560 This is a command that has to be installed in `calendar-mode-map'."
9561 (interactive)
9562 (org-agenda-list nil (calendar-absolute-from-gregorian
9563 (calendar-cursor-to-date))
9564 nil))
9566 (defun org-agenda-convert-date ()
9567 (interactive)
9568 (org-agenda-check-type t 'agenda 'timeline)
9569 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9570 date s)
9571 (unless day
9572 (user-error "Don't know which date to convert"))
9573 (setq date (calendar-gregorian-from-absolute day))
9574 (setq s (concat
9575 "Gregorian: " (calendar-date-string date) "\n"
9576 "ISO: " (calendar-iso-date-string date) "\n"
9577 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9578 "Julian: " (calendar-julian-date-string date) "\n"
9579 "Astron. JD: " (calendar-astro-date-string date)
9580 " (Julian date number at noon UTC)\n"
9581 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9582 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9583 "French: " (calendar-french-date-string date) "\n"
9584 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9585 "Mayan: " (calendar-mayan-date-string date) "\n"
9586 "Coptic: " (calendar-coptic-date-string date) "\n"
9587 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9588 "Persian: " (calendar-persian-date-string date) "\n"
9589 "Chinese: " (calendar-chinese-date-string date) "\n"))
9590 (with-output-to-temp-buffer "*Dates*"
9591 (princ s))
9592 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9594 ;;; Bulk commands
9596 (defun org-agenda-bulk-marked-p ()
9597 (eq (get-char-property (point-at-bol) 'type)
9598 'org-marked-entry-overlay))
9600 (defun org-agenda-bulk-mark (&optional arg)
9601 "Mark the entry at point for future bulk action."
9602 (interactive "p")
9603 (dotimes (i (or arg 1))
9604 (unless (org-get-at-bol 'org-agenda-diary-link)
9605 (let* ((m (org-get-at-bol 'org-hd-marker))
9607 (unless (org-agenda-bulk-marked-p)
9608 (unless m (user-error "Nothing to mark at point"))
9609 (push m org-agenda-bulk-marked-entries)
9610 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9611 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9612 (org-get-todo-face "TODO")
9613 'evaporate)
9614 (overlay-put ov 'type 'org-marked-entry-overlay))
9615 (end-of-line 1)
9616 (or (ignore-errors
9617 (goto-char (next-single-property-change (point) 'txt)))
9618 (beginning-of-line 2))
9619 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9620 (beginning-of-line 2))
9621 (message "%d entries marked for bulk action"
9622 (length org-agenda-bulk-marked-entries))))))
9624 (defun org-agenda-bulk-mark-all ()
9625 "Mark all entries for future agenda bulk action."
9626 (interactive)
9627 (org-agenda-bulk-mark-regexp "."))
9629 (defun org-agenda-bulk-mark-regexp (regexp)
9630 "Mark entries matching REGEXP for future agenda bulk action."
9631 (interactive "sMark entries matching regexp: ")
9632 (let ((entries-marked 0) txt-at-point)
9633 (save-excursion
9634 (goto-char (point-min))
9635 (goto-char (next-single-property-change (point) 'txt))
9636 (while (and (re-search-forward regexp nil t)
9637 (setq txt-at-point (get-text-property (point) 'txt)))
9638 (when (string-match regexp txt-at-point)
9639 (setq entries-marked (1+ entries-marked))
9640 (call-interactively 'org-agenda-bulk-mark))))
9641 (if (not entries-marked)
9642 (message "No entry matching this regexp."))))
9644 (defun org-agenda-bulk-unmark (&optional arg)
9645 "Unmark the entry at point for future bulk action."
9646 (interactive "P")
9647 (if arg
9648 (org-agenda-bulk-unmark-all)
9649 (cond ((org-agenda-bulk-marked-p)
9650 (org-agenda-bulk-remove-overlays
9651 (point-at-bol) (+ 2 (point-at-bol)))
9652 (setq org-agenda-bulk-marked-entries
9653 (delete (org-get-at-bol 'org-hd-marker)
9654 org-agenda-bulk-marked-entries))
9655 (end-of-line 1)
9656 (or (ignore-errors
9657 (goto-char (next-single-property-change (point) 'txt)))
9658 (beginning-of-line 2))
9659 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9660 (beginning-of-line 2))
9661 (message "%d entries left marked for bulk action"
9662 (length org-agenda-bulk-marked-entries)))
9663 (t (message "No entry to unmark here")))))
9665 (defun org-agenda-bulk-toggle-all ()
9666 "Toggle all marks for bulk action."
9667 (interactive)
9668 (save-excursion
9669 (goto-char (point-min))
9670 (while (ignore-errors
9671 (goto-char (next-single-property-change (point) 'txt)))
9672 (org-agenda-bulk-toggle))))
9674 (defun org-agenda-bulk-toggle ()
9675 "Toggle the mark at point for bulk action."
9676 (interactive)
9677 (if (org-agenda-bulk-marked-p)
9678 (org-agenda-bulk-unmark)
9679 (org-agenda-bulk-mark)))
9681 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9682 "Remove the mark overlays between BEG and END in the agenda buffer.
9683 BEG and END default to the buffer limits.
9685 This only removes the overlays, it does not remove the markers
9686 from the list in `org-agenda-bulk-marked-entries'."
9687 (interactive)
9688 (mapc (lambda (ov)
9689 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9690 (delete-overlay ov)))
9691 (overlays-in (or beg (point-min)) (or end (point-max)))))
9693 (defun org-agenda-bulk-unmark-all ()
9694 "Remove all marks in the agenda buffer.
9695 This will remove the markers and the overlays."
9696 (interactive)
9697 (if (null org-agenda-bulk-marked-entries)
9698 (message "No entry to unmark")
9699 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9700 (setq org-agenda-bulk-marked-entries nil)
9701 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9703 (defcustom org-agenda-persistent-marks nil
9704 "Non-nil means marked items will stay marked after a bulk action.
9705 You can toggle this interactively by typing `p' when prompted for a
9706 bulk action."
9707 :group 'org-agenda
9708 :version "24.1"
9709 :type 'boolean)
9711 (defun org-agenda-bulk-action (&optional arg)
9712 "Execute an remote-editing action on all marked entries.
9713 The prefix arg is passed through to the command if possible."
9714 (interactive "P")
9715 ;; Make sure we have markers, and only valid ones
9716 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9717 (mapc
9718 (lambda (m)
9719 (unless (and (markerp m)
9720 (marker-buffer m)
9721 (buffer-live-p (marker-buffer m))
9722 (marker-position m))
9723 (user-error "Marker %s for bulk command is invalid" m)))
9724 org-agenda-bulk-marked-entries)
9726 ;; Prompt for the bulk command
9727 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9728 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9729 "[S]catter [f]unction "
9730 (when org-agenda-bulk-custom-functions
9731 (concat " Custom: ["
9732 (mapconcat (lambda(f) (char-to-string (car f)))
9733 org-agenda-bulk-custom-functions "")
9734 "]"))))
9735 (catch 'exit
9736 (let* ((action (read-char-exclusive))
9737 (org-log-refile (if org-log-refile 'time nil))
9738 (entries (reverse org-agenda-bulk-marked-entries))
9739 (org-overriding-default-time
9740 (if (get-text-property (point) 'org-agenda-date-header)
9741 (org-get-cursor-date)))
9742 redo-at-end
9743 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9744 (cond
9745 ((equal action ?p)
9746 (let ((org-agenda-persistent-marks
9747 (not org-agenda-persistent-marks)))
9748 (org-agenda-bulk-action)
9749 (throw 'exit nil)))
9751 ((equal action ?$)
9752 (setq cmd '(org-agenda-archive)))
9754 ((equal action ?A)
9755 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9757 ((member action '(?r ?w))
9758 (setq rfloc (org-refile-get-location
9759 "Refile to"
9760 (marker-buffer (car entries))
9761 org-refile-allow-creating-parent-nodes))
9762 (if (nth 3 rfloc)
9763 (setcar (nthcdr 3 rfloc)
9764 (move-marker (make-marker) (nth 3 rfloc)
9765 (or (get-file-buffer (nth 1 rfloc))
9766 (find-buffer-visiting (nth 1 rfloc))
9767 (error "This should not happen")))))
9769 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9770 redo-at-end t))
9772 ((equal action ?t)
9773 (setq state (org-icompleting-read
9774 "Todo state: "
9775 (with-current-buffer (marker-buffer (car entries))
9776 (mapcar 'list org-todo-keywords-1))))
9777 (setq cmd `(let ((org-inhibit-blocking t)
9778 (org-inhibit-logging 'note))
9779 (org-agenda-todo ,state))))
9781 ((memq action '(?- ?+))
9782 (setq tag (org-icompleting-read
9783 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9784 (with-current-buffer (marker-buffer (car entries))
9785 (delq nil
9786 (mapcar (lambda (x)
9787 (if (stringp (car x)) x)) org-tag-alist)))))
9788 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9790 ((memq action '(?s ?d))
9791 (let* ((time
9792 (unless arg
9793 (org-read-date
9794 nil nil nil
9795 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9796 org-overriding-default-time)))
9797 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9798 (setq cmd `(eval '(,c1 arg ,time)))))
9800 ((equal action ?S)
9801 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9802 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9803 (let ((days (read-number
9804 (format "Scatter tasks across how many %sdays: "
9805 (if arg "week" "")) 7)))
9806 (setq cmd
9807 `(let ((distance (1+ (random ,days))))
9808 (if arg
9809 (let ((dist distance)
9810 (day-of-week
9811 (calendar-day-of-week
9812 (calendar-gregorian-from-absolute (org-today)))))
9813 (dotimes (i (1+ dist))
9814 (while (member day-of-week org-agenda-weekend-days)
9815 (incf distance)
9816 (incf day-of-week)
9817 (if (= day-of-week 7)
9818 (setq day-of-week 0)))
9819 (incf day-of-week)
9820 (if (= day-of-week 7)
9821 (setq day-of-week 0)))))
9822 ;; silently fail when try to replan a sexp entry
9823 (condition-case nil
9824 (let* ((date (calendar-gregorian-from-absolute
9825 (+ (org-today) distance)))
9826 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9827 (nth 2 date))))
9828 (org-agenda-schedule nil time))
9829 (error nil)))))))
9831 ((assoc action org-agenda-bulk-custom-functions)
9832 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9833 redo-at-end t))
9835 ((equal action ?f)
9836 (setq cmd (list (intern
9837 (org-icompleting-read "Function: "
9838 obarray 'fboundp t nil nil)))))
9840 (t (user-error "Invalid bulk action")))
9842 ;; Sort the markers, to make sure that parents are handled before children
9843 (setq entries (sort entries
9844 (lambda (a b)
9845 (cond
9846 ((equal (marker-buffer a) (marker-buffer b))
9847 (< (marker-position a) (marker-position b)))
9849 (string< (buffer-name (marker-buffer a))
9850 (buffer-name (marker-buffer b))))))))
9852 ;; Now loop over all markers and apply cmd
9853 (while (setq e (pop entries))
9854 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9855 (if (not pos)
9856 (progn (message "Skipping removed entry at %s" e)
9857 (setq cntskip (1+ cntskip)))
9858 (goto-char pos)
9859 (let (org-loop-over-headlines-in-active-region)
9860 (eval cmd))
9861 (setq cnt (1+ cnt))))
9862 (when redo-at-end (org-agenda-redo))
9863 (unless org-agenda-persistent-marks
9864 (org-agenda-bulk-unmark-all))
9865 (message "Acted on %d entries%s%s"
9867 (if (= cntskip 0)
9869 (format ", skipped %d (disappeared before their turn)"
9870 cntskip))
9871 (if (not org-agenda-persistent-marks)
9872 "" " (kept marked)"))))))
9874 (defun org-agenda-capture (&optional with-time)
9875 "Call `org-capture' with the date at point.
9876 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9877 current HH:MM time."
9878 (interactive "P")
9879 (if (not (eq major-mode 'org-agenda-mode))
9880 (user-error "You cannot do this outside of agenda buffers")
9881 (let ((org-overriding-default-time
9882 (org-get-cursor-date (equal with-time 1))))
9883 (call-interactively 'org-capture))))
9885 ;;; Dragging agenda lines forward/backward
9887 (defun org-agenda-drag-line-forward (arg)
9888 "Drag an agenda line forward by ARG lines."
9889 (interactive "p")
9890 (let ((inhibit-read-only t) lst)
9891 (if (save-excursion
9892 (dotimes (n arg)
9893 (beginning-of-line 2)
9894 (push (not (get-text-property (point) 'txt)) lst))
9895 (delq nil lst))
9896 (message "Cannot move line forward")
9897 (org-drag-line-forward arg))))
9899 (defun org-agenda-drag-line-backward (arg)
9900 "Drag an agenda line backward by ARG lines."
9901 (interactive "p")
9902 (let ((inhibit-read-only t) lst)
9903 (if (save-excursion
9904 (dotimes (n arg)
9905 (beginning-of-line 0)
9906 (push (not (get-text-property (point) 'txt)) lst))
9907 (delq nil lst))
9908 (message "Cannot move line backward")
9909 (org-drag-line-backward arg))))
9911 ;;; Flagging notes
9913 (defun org-agenda-show-the-flagging-note ()
9914 "Display the flagging note in the other window.
9915 When called a second time in direct sequence, offer to remove the FLAGGING
9916 tag and (if present) the flagging note."
9917 (interactive)
9918 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9919 (win (selected-window))
9920 note heading newhead)
9921 (unless hdmarker
9922 (user-error "No linked entry at point"))
9923 (if (and (eq this-command last-command)
9924 (y-or-n-p "Unflag and remove any flagging note? "))
9925 (progn
9926 (org-agenda-remove-flag hdmarker)
9927 (let ((win (get-buffer-window "*Flagging Note*")))
9928 (and win (delete-window win)))
9929 (message "Entry unflagged"))
9930 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9931 (unless note
9932 (user-error "No flagging note"))
9933 (org-kill-new note)
9934 (org-switch-to-buffer-other-window "*Flagging Note*")
9935 (erase-buffer)
9936 (insert note)
9937 (goto-char (point-min))
9938 (while (re-search-forward "\\\\n" nil t)
9939 (replace-match "\n" t t))
9940 (goto-char (point-min))
9941 (select-window win)
9942 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9944 (defun org-agenda-remove-flag (marker)
9945 "Remove the FLAGGED tag and any flagging note in the entry."
9946 (let (newhead)
9947 (org-with-point-at marker
9948 (org-toggle-tag "FLAGGED" 'off)
9949 (org-entry-delete nil "THEFLAGGINGNOTE")
9950 (setq newhead (org-get-heading)))
9951 (org-agenda-change-all-lines newhead marker)
9952 (message "Entry unflagged")))
9954 (defun org-agenda-get-any-marker (&optional pos)
9955 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9956 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9958 ;;; Appointment reminders
9960 (defvar appt-time-msg-list) ; defined in appt.el
9962 ;;;###autoload
9963 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9964 "Activate appointments found in `org-agenda-files'.
9965 With a \\[universal-argument] prefix, refresh the list of
9966 appointments.
9968 If FILTER is t, interactively prompt the user for a regular
9969 expression, and filter out entries that don't match it.
9971 If FILTER is a string, use this string as a regular expression
9972 for filtering entries out.
9974 If FILTER is a function, filter out entries against which
9975 calling the function returns nil. This function takes one
9976 argument: an entry from `org-agenda-get-day-entries'.
9978 FILTER can also be an alist with the car of each cell being
9979 either 'headline or 'category. For example:
9981 '((headline \"IMPORTANT\")
9982 (category \"Work\"))
9984 will only add headlines containing IMPORTANT or headlines
9985 belonging to the \"Work\" category.
9987 ARGS are symbols indicating what kind of entries to consider.
9988 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9989 \(i.e., deadlines and scheduled items with a hh:mm specification)
9990 and :timestamp entries. See the docstring of `org-diary' for
9991 details and examples.
9993 If an entry has a APPT_WARNTIME property, its value will be used
9994 to override `appt-message-warning-time'."
9995 (interactive "P")
9996 (if refresh (setq appt-time-msg-list nil))
9997 (if (eq filter t)
9998 (setq filter (read-from-minibuffer "Regexp filter: ")))
9999 (let* ((cnt 0) ; count added events
10000 (scope (or args '(:deadline* :scheduled* :timestamp)))
10001 (org-agenda-new-buffers nil)
10002 (org-deadline-warning-days 0)
10003 ;; Do not use `org-today' here because appt only takes
10004 ;; time and without date as argument, so it may pass wrong
10005 ;; information otherwise
10006 (today (org-date-to-gregorian
10007 (time-to-days (current-time))))
10008 (org-agenda-restrict nil)
10009 (files (org-agenda-files 'unrestricted)) entries file
10010 (org-agenda-buffer nil))
10011 ;; Get all entries which may contain an appt
10012 (org-agenda-prepare-buffers files)
10013 (while (setq file (pop files))
10014 (setq entries
10015 (delq nil
10016 (append entries
10017 (apply 'org-agenda-get-day-entries
10018 file today scope)))))
10019 ;; Map thru entries and find if we should filter them out
10020 (mapc
10021 (lambda(x)
10022 (let* ((evt (org-trim
10023 (replace-regexp-in-string
10024 org-bracket-link-regexp "\\3"
10025 (or (get-text-property 1 'txt x) ""))))
10026 (cat (get-text-property 1 'org-category x))
10027 (tod (get-text-property 1 'time-of-day x))
10028 (ok (or (null filter)
10029 (and (stringp filter) (string-match filter evt))
10030 (and (functionp filter) (funcall filter x))
10031 (and (listp filter)
10032 (let ((cat-filter (cadr (assoc 'category filter)))
10033 (evt-filter (cadr (assoc 'headline filter))))
10034 (or (and (stringp cat-filter)
10035 (string-match cat-filter cat))
10036 (and (stringp evt-filter)
10037 (string-match evt-filter evt)))))))
10038 (wrn (get-text-property 1 'warntime x)))
10039 ;; FIXME: Shall we remove text-properties for the appt text?
10040 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10041 (when (and ok tod)
10042 (setq tod (concat "00" (number-to-string tod))
10043 tod (when (string-match
10044 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10045 (concat (match-string 1 tod) ":"
10046 (match-string 2 tod))))
10047 (if (version< emacs-version "23.3")
10048 (appt-add tod evt)
10049 (appt-add tod evt wrn))
10050 (setq cnt (1+ cnt))))) entries)
10051 (org-release-buffers org-agenda-new-buffers)
10052 (if (eq cnt 0)
10053 (message "No event to add")
10054 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10056 (defun org-agenda-todayp (date)
10057 "Does DATE mean today, when considering `org-extend-today-until'?"
10058 (let ((today (org-today))
10059 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10060 date)))
10061 (eq date today)))
10063 (defun org-agenda-todo-yesterday (&optional arg)
10064 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10065 (interactive "P")
10066 (let* ((hour (third (decode-time
10067 (org-current-time))))
10068 (org-extend-today-until (1+ hour)))
10069 (org-agenda-todo arg)))
10071 (provide 'org-agenda)
10073 ;;; org-agenda.el ends here