Merge branch 'maint'
[org-mode.git] / lisp / org-agenda.el
blob67050119b7dc9ba328c885e72288b4c5bf20b827
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 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-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-pop-to-buffer-same-window "org-compat"
84 (&optional buffer-or-name norecord label))
85 (declare-function org-agenda-columns "org-colview" ())
86 (declare-function org-add-archive-files "org-archive" (files))
87 (declare-function org-capture "org-capture" (&optional goto keys))
89 (defvar calendar-mode-map) ; defined in calendar.el
90 (defvar org-clock-current-task nil) ; defined in org-clock.el
91 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
92 (defvar org-habit-show-habits) ; defined in org-habit.el
93 (defvar org-habit-show-habits-only-for-today)
94 (defvar org-habit-show-all-today)
96 ;; Defined somewhere in this file, but used before definition.
97 (defvar org-agenda-buffer-name "*Org Agenda*")
98 (defvar org-agenda-overriding-header nil)
99 (defvar org-agenda-title-append nil)
100 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
101 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defvar org-agenda-undo-list nil
105 "List of undoable operations in the agenda since last refresh.")
106 (defvar org-agenda-pending-undo-list nil
107 "In a series of undo commands, this is the list of remaining undo items.")
109 (defcustom org-agenda-confirm-kill 1
110 "When set, remote killing from the agenda buffer needs confirmation.
111 When t, a confirmation is always needed. When a number N, confirmation is
112 only needed when the text to be killed contains more than N non-white lines."
113 :group 'org-agenda
114 :type '(choice
115 (const :tag "Never" nil)
116 (const :tag "Always" t)
117 (integer :tag "When more than N lines")))
119 (defcustom org-agenda-compact-blocks nil
120 "Non-nil means make the block agenda more compact.
121 This is done globally by leaving out lines like the agenda span
122 name and week number or the separator lines."
123 :group 'org-agenda
124 :type 'boolean)
126 (defcustom org-agenda-block-separator ?=
127 "The separator between blocks in the agenda.
128 If this is a string, it will be used as the separator, with a newline added.
129 If it is a character, it will be repeated to fill the window width.
130 If nil the separator is disabled. In `org-agenda-custom-commands' this
131 addresses the separator between the current and the previous block."
132 :group 'org-agenda
133 :type '(choice
134 (const :tag "Disabled" nil)
135 (character)
136 (string)))
138 (defgroup org-agenda-export nil
139 "Options concerning exporting agenda views in Org-mode."
140 :tag "Org Agenda Export"
141 :group 'org-agenda)
143 (defcustom org-agenda-with-colors t
144 "Non-nil means use colors in agenda views."
145 :group 'org-agenda-export
146 :type 'boolean)
148 (defcustom org-agenda-exporter-settings nil
149 "Alist of variable/value pairs that should be active during agenda export.
150 This is a good place to set options for ps-print and for htmlize.
151 Note that the way this is implemented, the values will be evaluated
152 before assigned to the variables. So make sure to quote values you do
153 *not* want evaluated, for example
155 (setq org-agenda-exporter-settings
156 '((ps-print-color-p 'black-white)))"
157 :group 'org-agenda-export
158 :type '(repeat
159 (list
160 (variable)
161 (sexp :tag "Value"))))
163 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
164 "Hook run in a temporary buffer before writing the agenda to an export file.
165 A useful function for this hook is `org-agenda-add-entry-text'."
166 :group 'org-agenda-export
167 :type 'hook
168 :options '(org-agenda-add-entry-text))
170 (defcustom org-agenda-add-entry-text-maxlines 0
171 "Maximum number of entry text lines to be added to agenda.
172 This is only relevant when `org-agenda-add-entry-text' is part of
173 `org-agenda-before-write-hook', which is the default.
174 When this is 0, nothing will happen. When it is greater than 0, it
175 specifies the maximum number of lines that will be added for each entry
176 that is listed in the agenda view.
178 Note that this variable is not used during display, only when exporting
179 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
180 and `org-agenda-entry-text-maxlines'."
181 :group 'org-agenda
182 :type 'integer)
184 (defcustom org-agenda-add-entry-text-descriptive-links t
185 "Non-nil means export org-links as descriptive links in agenda added text.
186 This variable applies to the text added to the agenda when
187 `org-agenda-add-entry-text-maxlines' is larger than 0.
188 When this variable nil, the URL will (also) be shown."
189 :group 'org-agenda
190 :type 'boolean)
192 (defcustom org-agenda-export-html-style nil
193 "The style specification for exported HTML Agenda files.
194 If this variable contains a string, it will replace the default <style>
195 section as produced by `htmlize'.
196 Since there are different ways of setting style information, this variable
197 needs to contain the full HTML structure to provide a style, including the
198 surrounding HTML tags. The style specifications should include definitions
199 the fonts used by the agenda, here is an example:
201 <style type=\"text/css\">
202 p { font-weight: normal; color: gray; }
203 .org-agenda-structure {
204 font-size: 110%;
205 color: #003399;
206 font-weight: 600;
208 .org-todo {
209 color: #cc6666;
210 font-weight: bold;
212 .org-agenda-done {
213 color: #339933;
215 .org-done {
216 color: #339933;
218 .title { text-align: center; }
219 .todo, .deadline { color: red; }
220 .done { color: green; }
221 </style>
223 or, if you want to keep the style in a file,
225 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
227 As the value of this option simply gets inserted into the HTML <head> header,
228 you can \"misuse\" it to also add other text to the header."
229 :group 'org-agenda-export
230 :group 'org-export-html
231 :type '(choice
232 (const nil)
233 (string)))
235 (defcustom org-agenda-persistent-filter nil
236 "When set, keep filters from one agenda view to the next."
237 :group 'org-agenda
238 :type 'boolean)
240 (defgroup org-agenda-custom-commands nil
241 "Options concerning agenda views in Org-mode."
242 :tag "Org Agenda Custom Commands"
243 :group 'org-agenda)
245 (defconst org-sorting-choice
246 '(choice
247 (const time-up) (const time-down)
248 (const timestamp-up) (const timestamp-down)
249 (const scheduled-up) (const scheduled-down)
250 (const deadline-up) (const deadline-down)
251 (const ts-up) (const ts-down)
252 (const tsia-up) (const tsia-down)
253 (const category-keep) (const category-up) (const category-down)
254 (const tag-down) (const tag-up)
255 (const priority-up) (const priority-down)
256 (const todo-state-up) (const todo-state-down)
257 (const effort-up) (const effort-down)
258 (const habit-up) (const habit-down)
259 (const alpha-up) (const alpha-down)
260 (const user-defined-up) (const user-defined-down))
261 "Sorting choices.")
263 ;; Keep custom values for `org-agenda-filter-preset' compatible with
264 ;; the new variable `org-agenda-tag-filter-preset'.
265 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
266 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
268 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
269 "List of types searched for when creating the daily/weekly agenda.
270 This variable is a list of symbols that controls the types of
271 items that appear in the daily/weekly agenda. Allowed symbols in this
272 list are are
274 :timestamp List items containing a date stamp or date range matching
275 the selected date. This includes sexp entries in angular
276 brackets.
278 :sexp List entries resulting from plain diary-like sexps.
280 :deadline List deadline due on that date. When the date is today,
281 also list any deadlines past due, or due within
282 `org-deadline-warning-days'. `:deadline' must appear before
283 `:scheduled' if the setting of
284 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
285 any effect.
287 :deadline* Same as above, but only include the deadline if it has an
288 hour specification as [h]h:mm.
290 :scheduled List all items which are scheduled for the given date.
291 The diary for *today* also contains items which were
292 scheduled earlier and are not yet marked DONE.
294 :scheduled* Same as above, but only include the scheduled item if it
295 has an hour specification as [h]h:mm.
297 By default, all four non-starred types are turned on.
299 When :scheduled* or :deadline* are included, :schedule or :deadline
300 will be ignored.
302 Never set this variable globally using `setq', because then it
303 will apply to all future agenda commands. Instead, bind it with
304 `let' to scope it dynamically into the agenda-constructing
305 command. A good way to set it is through options in
306 `org-agenda-custom-commands'. For a more flexible (though
307 somewhat less efficient) way of determining what is included in
308 the daily/weekly agenda, see `org-agenda-skip-function'.")
310 (defconst org-agenda-custom-commands-local-options
311 `(repeat :tag "Local settings for this command. Remember to quote values"
312 (choice :tag "Setting"
313 (list :tag "Heading for this block"
314 (const org-agenda-overriding-header)
315 (string :tag "Headline"))
316 (list :tag "Files to be searched"
317 (const org-agenda-files)
318 (list
319 (const :format "" quote)
320 (repeat (file))))
321 (list :tag "Sorting strategy"
322 (const org-agenda-sorting-strategy)
323 (list
324 (const :format "" quote)
325 (repeat
326 ,org-sorting-choice)))
327 (list :tag "Prefix format"
328 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
329 (string))
330 (list :tag "Number of days in agenda"
331 (const org-agenda-span)
332 (choice (const :tag "Day" day)
333 (const :tag "Week" week)
334 (const :tag "Fortnight" fortnight)
335 (const :tag "Month" month)
336 (const :tag "Year" year)
337 (integer :tag "Custom")))
338 (list :tag "Fixed starting date"
339 (const org-agenda-start-day)
340 (string :value "2007-11-01"))
341 (list :tag "Start on day of week"
342 (const org-agenda-start-on-weekday)
343 (choice :value 1
344 (const :tag "Today" nil)
345 (integer :tag "Weekday No.")))
346 (list :tag "Include data from diary"
347 (const org-agenda-include-diary)
348 (boolean))
349 (list :tag "Deadline Warning days"
350 (const org-deadline-warning-days)
351 (integer :value 1))
352 (list :tag "Category filter preset"
353 (const org-agenda-category-filter-preset)
354 (list
355 (const :format "" quote)
356 (repeat
357 (string :tag "+category or -category"))))
358 (list :tag "Tags filter preset"
359 (const org-agenda-tag-filter-preset)
360 (list
361 (const :format "" quote)
362 (repeat
363 (string :tag "+tag or -tag"))))
364 (list :tag "Effort filter preset"
365 (const org-agenda-effort-filter-preset)
366 (list
367 (const :format "" quote)
368 (repeat
369 (string :tag "+=10 or -=10 or +<10 or ->10"))))
370 (list :tag "Regexp filter preset"
371 (const org-agenda-regexp-filter-preset)
372 (list
373 (const :format "" quote)
374 (repeat
375 (string :tag "+regexp or -regexp"))))
376 (list :tag "Set daily/weekly entry types"
377 (const org-agenda-entry-types)
378 (list
379 (const :format "" quote)
380 (set :greedy t :value ,org-agenda-entry-types
381 (const :deadline)
382 (const :scheduled)
383 (const :deadline*)
384 (const :scheduled*)
385 (const :timestamp)
386 (const :sexp))))
387 (list :tag "Standard skipping condition"
388 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
389 (const org-agenda-skip-function)
390 (list
391 (const :format "" quote)
392 (list
393 (choice
394 :tag "Skipping range"
395 (const :tag "Skip entry" org-agenda-skip-entry-if)
396 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
397 (repeat :inline t :tag "Conditions for skipping"
398 (choice
399 :tag "Condition type"
400 (list :tag "Regexp matches" :inline t
401 (const :format "" 'regexp)
402 (regexp))
403 (list :tag "Regexp does not match" :inline t
404 (const :format "" 'notregexp)
405 (regexp))
406 (list :tag "TODO state is" :inline t
407 (const 'todo)
408 (choice
409 (const :tag "Any not-done state" 'todo)
410 (const :tag "Any done state" 'done)
411 (const :tag "Any state" 'any)
412 (list :tag "Keyword list"
413 (const :format "" quote)
414 (repeat (string :tag "Keyword")))))
415 (list :tag "TODO state is not" :inline t
416 (const 'nottodo)
417 (choice
418 (const :tag "Any not-done state" 'todo)
419 (const :tag "Any done state" 'done)
420 (const :tag "Any state" 'any)
421 (list :tag "Keyword list"
422 (const :format "" quote)
423 (repeat (string :tag "Keyword")))))
424 (const :tag "scheduled" 'scheduled)
425 (const :tag "not scheduled" 'notscheduled)
426 (const :tag "deadline" 'deadline)
427 (const :tag "no deadline" 'notdeadline)
428 (const :tag "timestamp" 'timestamp)
429 (const :tag "no timestamp" 'nottimestamp))))))
430 (list :tag "Non-standard skipping condition"
431 :value (org-agenda-skip-function)
432 (const org-agenda-skip-function)
433 (sexp :tag "Function or form (quoted!)"))
434 (list :tag "Any variable"
435 (variable :tag "Variable")
436 (sexp :tag "Value (sexp)"))))
437 "Selection of examples for agenda command settings.
438 This will be spliced into the custom type of
439 `org-agenda-custom-commands'.")
442 (defcustom org-agenda-custom-commands
443 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
444 "Custom commands for the agenda.
445 These commands will be offered on the splash screen displayed by the
446 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
448 (key desc type match settings files)
450 key The key (one or more characters as a string) to be associated
451 with the command.
452 desc A description of the command, when omitted or nil, a default
453 description is built using MATCH.
454 type The command type, any of the following symbols:
455 agenda The daily/weekly agenda.
456 todo Entries with a specific TODO keyword, in all agenda files.
457 search Entries containing search words entry or headline.
458 tags Tags/Property/TODO match in all agenda files.
459 tags-todo Tags/P/T match in all agenda files, TODO entries only.
460 todo-tree Sparse tree of specific TODO keyword in *current* file.
461 tags-tree Sparse tree with all tags matches in *current* file.
462 occur-tree Occur sparse tree for *current* file.
463 ... A user-defined function.
464 match What to search for:
465 - a single keyword for TODO keyword searches
466 - a tags match expression for tags searches
467 - a word search expression for text searches.
468 - a regular expression for occur searches
469 For all other commands, this should be the empty string.
470 settings A list of option settings, similar to that in a let form, so like
471 this: ((opt1 val1) (opt2 val2) ...). The values will be
472 evaluated at the moment of execution, so quote them when needed.
473 files A list of files file to write the produced agenda buffer to
474 with the command `org-store-agenda-views'.
475 If a file name ends in \".html\", an HTML version of the buffer
476 is written out. If it ends in \".ps\", a postscript version is
477 produced. Otherwise, only the plain text is written to the file.
479 You can also define a set of commands, to create a composite agenda buffer.
480 In this case, an entry looks like this:
482 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
484 where
486 desc A description string to be displayed in the dispatcher menu.
487 cmd An agenda command, similar to the above. However, tree commands
488 are not allowed, but instead you can get agenda and global todo list.
489 So valid commands for a set are:
490 (agenda \"\" settings)
491 (alltodo \"\" settings)
492 (stuck \"\" settings)
493 (todo \"match\" settings files)
494 (search \"match\" settings files)
495 (tags \"match\" settings files)
496 (tags-todo \"match\" settings files)
498 Each command can carry a list of options, and another set of options can be
499 given for the whole set of commands. Individual command options take
500 precedence over the general options.
502 When using several characters as key to a command, the first characters
503 are prefix commands. For the dispatcher to display useful information, you
504 should provide a description for the prefix, like
506 (setq org-agenda-custom-commands
507 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
508 (\"hl\" tags \"+HOME+Lisa\")
509 (\"hp\" tags \"+HOME+Peter\")
510 (\"hk\" tags \"+HOME+Kim\")))"
511 :group 'org-agenda-custom-commands
512 :type `(repeat
513 (choice :value ("x" "Describe command here" tags "" nil)
514 (list :tag "Single command"
515 (string :tag "Access Key(s) ")
516 (option (string :tag "Description"))
517 (choice
518 (const :tag "Agenda" agenda)
519 (const :tag "TODO list" alltodo)
520 (const :tag "Search words" search)
521 (const :tag "Stuck projects" stuck)
522 (const :tag "Tags/Property match (all agenda files)" tags)
523 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
524 (const :tag "TODO keyword search (all agenda files)" todo)
525 (const :tag "Tags sparse tree (current buffer)" tags-tree)
526 (const :tag "TODO keyword tree (current buffer)" todo-tree)
527 (const :tag "Occur tree (current buffer)" occur-tree)
528 (sexp :tag "Other, user-defined function"))
529 (string :tag "Match (only for some commands)")
530 ,org-agenda-custom-commands-local-options
531 (option (repeat :tag "Export" (file :tag "Export to"))))
532 (list :tag "Command series, all agenda files"
533 (string :tag "Access Key(s)")
534 (string :tag "Description ")
535 (repeat :tag "Component"
536 (choice
537 (list :tag "Agenda"
538 (const :format "" agenda)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "TODO list (all keywords)"
542 (const :format "" alltodo)
543 (const :tag "" :format "" "")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Search words"
546 (const :format "" search)
547 (string :tag "Match")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Stuck projects"
550 (const :format "" stuck)
551 (const :tag "" :format "" "")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search"
554 (const :format "" tags)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "Tags search, TODO entries only"
558 (const :format "" tags-todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "TODO keyword search"
562 (const :format "" todo)
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)
565 (list :tag "Other, user-defined function"
566 (symbol :tag "function")
567 (string :tag "Match")
568 ,org-agenda-custom-commands-local-options)))
570 (repeat :tag "Settings for entire command set"
571 (list (variable :tag "Any variable")
572 (sexp :tag "Value")))
573 (option (repeat :tag "Export" (file :tag "Export to"))))
574 (cons :tag "Prefix key documentation"
575 (string :tag "Access Key(s)")
576 (string :tag "Description ")))))
578 (defcustom org-agenda-query-register ?o
579 "The register holding the current query string.
580 The purpose of this is that if you construct a query string interactively,
581 you can then use it to define a custom command."
582 :group 'org-agenda-custom-commands
583 :type 'character)
585 (defcustom org-stuck-projects
586 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
587 "How to identify stuck projects.
588 This is a list of four items:
589 1. A tags/todo/property matcher string that is used to identify a project.
590 See the manual for a description of tag and property searches.
591 The entire tree below a headline matched by this is considered one project.
592 2. A list of TODO keywords identifying non-stuck projects.
593 If the project subtree contains any headline with one of these todo
594 keywords, the project is considered to be not stuck. If you specify
595 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
596 3. A list of tags identifying non-stuck projects.
597 If the project subtree contains any headline with one of these tags,
598 the project is considered to be not stuck. If you specify \"*\" as
599 a tag, any tag will mark the project unstuck. Note that this is about
600 the explicit presence of a tag somewhere in the subtree, inherited
601 tags do not count here. If inherited tags make a project not stuck,
602 use \"-TAG\" in the tags part of the matcher under (1.) above.
603 4. An arbitrary regular expression matching non-stuck projects.
605 If the project turns out to be not stuck, search continues also in the
606 subtree to see if any of the subtasks have project status.
608 See also the variable `org-tags-match-list-sublevels' which applies
609 to projects matched by this search as well.
611 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
612 or `C-c a #' to produce the list."
613 :group 'org-agenda-custom-commands
614 :type '(list
615 (string :tag "Tags/TODO match to identify a project")
616 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
620 (defgroup org-agenda-skip nil
621 "Options concerning skipping parts of agenda files."
622 :tag "Org Agenda Skip"
623 :group 'org-agenda)
625 (defcustom org-agenda-skip-function-global nil
626 "Function to be called at each match during agenda construction.
627 If this function returns nil, the current match should not be skipped.
628 If the function decided to skip an agenda match, is must return the
629 buffer position from which the search should be continued.
630 This may also be a Lisp form, which will be evaluated.
632 This variable will be applied to every agenda match, including
633 tags/property searches and TODO lists. So try to make the test function
634 do its checking as efficiently as possible. To implement a skipping
635 condition just for specific agenda commands, use the variable
636 `org-agenda-skip-function' which can be set in the options section
637 of custom agenda commands."
638 :group 'org-agenda-skip
639 :type 'sexp)
641 (defgroup org-agenda-daily/weekly nil
642 "Options concerning the daily/weekly agenda."
643 :tag "Org Agenda Daily/Weekly"
644 :group 'org-agenda)
645 (defgroup org-agenda-todo-list nil
646 "Options concerning the global todo list agenda view."
647 :tag "Org Agenda Todo List"
648 :group 'org-agenda)
649 (defgroup org-agenda-match-view nil
650 "Options concerning the general tags/property/todo match agenda view."
651 :tag "Org Agenda Match View"
652 :group 'org-agenda)
653 (defgroup org-agenda-search-view nil
654 "Options concerning the search agenda view."
655 :tag "Org Agenda Search View"
656 :group 'org-agenda)
658 (defvar org-agenda-archives-mode nil
659 "Non-nil means the agenda will include archived items.
660 If this is the symbol `trees', trees in the selected agenda scope
661 that are marked with the ARCHIVE tag will be included anyway. When this is
662 t, also all archive files associated with the current selection of agenda
663 files will be included.")
665 (defcustom org-agenda-restriction-lock-highlight-subtree t
666 "Non-nil means highlight the whole subtree when restriction is active.
667 Otherwise only highlight the headline. Highlighting the whole subtree is
668 useful to ensure no edits happen beyond the restricted region."
669 :group 'org-agenda
670 :type 'boolean)
672 (defcustom org-agenda-skip-comment-trees t
673 "Non-nil means skip trees that start with the COMMENT keyword.
674 When nil, these trees are also scanned by agenda commands."
675 :group 'org-agenda-skip
676 :type 'boolean)
678 (defcustom org-agenda-todo-list-sublevels t
679 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
680 When nil, the sublevels of a TODO entry are not checked, resulting in
681 potentially much shorter TODO lists."
682 :group 'org-agenda-skip
683 :group 'org-agenda-todo-list
684 :type 'boolean)
686 (defcustom org-agenda-todo-ignore-with-date nil
687 "Non-nil means don't show entries with a date in the global todo list.
688 You can use this if you prefer to mark mere appointments with a TODO keyword,
689 but don't want them to show up in the TODO list.
690 When this is set, it also covers deadlines and scheduled items, the settings
691 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
692 will be ignored.
693 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
694 :group 'org-agenda-skip
695 :group 'org-agenda-todo-list
696 :type 'boolean)
698 (defcustom org-agenda-todo-ignore-timestamp nil
699 "Non-nil means don't show entries with a timestamp.
700 This applies when creating the global todo list.
701 Valid values are:
703 past Don't show entries for today or in the past.
705 future Don't show entries with a timestamp in the future.
706 The idea behind this is that if it has a future
707 timestamp, you don't want to think about it until the
708 date.
710 all Don't show any entries with a timestamp in the global todo list.
711 The idea behind this is that by setting a timestamp, you
712 have already \"taken care\" of this item.
714 This variable can also have an integer as a value. If positive (N),
715 todos with a timestamp N or more days in the future will be ignored. If
716 negative (-N), todos with a timestamp N or more days in the past will be
717 ignored. If 0, todos with a timestamp either today or in the future will
718 be ignored. For example, a value of -1 will exclude todos with a
719 timestamp in the past (yesterday or earlier), while a value of 7 will
720 exclude todos with a timestamp a week or more in the future.
722 See also `org-agenda-todo-ignore-with-date'.
723 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
724 to make his option also apply to the tags-todo list."
725 :group 'org-agenda-skip
726 :group 'org-agenda-todo-list
727 :version "24.1"
728 :type '(choice
729 (const :tag "Ignore future timestamp todos" future)
730 (const :tag "Ignore past or present timestamp todos" past)
731 (const :tag "Ignore all timestamp todos" all)
732 (const :tag "Show timestamp todos" nil)
733 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
735 (defcustom org-agenda-todo-ignore-scheduled nil
736 "Non-nil means, ignore some scheduled TODO items when making TODO list.
737 This applies when creating the global todo list.
738 Valid values are:
740 past Don't show entries scheduled today or in the past.
742 future Don't show entries scheduled in the future.
743 The idea behind this is that by scheduling it, you don't want to
744 think about it until the scheduled date.
746 all Don't show any scheduled entries in the global todo list.
747 The idea behind this is that by scheduling it, you have already
748 \"taken care\" of this item.
750 t Same as `all', for backward compatibility.
752 This variable can also have an integer as a value. See
753 `org-agenda-todo-ignore-timestamp' for more details.
755 See also `org-agenda-todo-ignore-with-date'.
756 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
757 to make his option also apply to the tags-todo list."
758 :group 'org-agenda-skip
759 :group 'org-agenda-todo-list
760 :type '(choice
761 (const :tag "Ignore future-scheduled todos" future)
762 (const :tag "Ignore past- or present-scheduled todos" past)
763 (const :tag "Ignore all scheduled todos" all)
764 (const :tag "Ignore all scheduled todos (compatibility)" t)
765 (const :tag "Show scheduled todos" nil)
766 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
768 (defcustom org-agenda-todo-ignore-deadlines nil
769 "Non-nil means ignore some deadline TODO items when making TODO list.
770 There are different motivations for using different values, please think
771 carefully when configuring this variable.
773 This applies when creating the global todo list.
774 Valid values are:
776 near Don't show near deadline entries. A deadline is near when it is
777 closer than `org-deadline-warning-days' days. The idea behind this
778 is that such items will appear in the agenda anyway.
780 far Don't show TODO entries where a deadline has been defined, but
781 the deadline is not near. This is useful if you don't want to
782 use the todo list to figure out what to do now.
784 past Don't show entries with a deadline timestamp for today or in the past.
786 future Don't show entries with a deadline timestamp in the future, not even
787 when they become `near' ones. Use it with caution.
789 all Ignore all TODO entries that do have a deadline.
791 t Same as `near', for backward compatibility.
793 This variable can also have an integer as a value. See
794 `org-agenda-todo-ignore-timestamp' for more details.
796 See also `org-agenda-todo-ignore-with-date'.
797 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
798 to make his option also apply to the tags-todo list."
799 :group 'org-agenda-skip
800 :group 'org-agenda-todo-list
801 :type '(choice
802 (const :tag "Ignore near deadlines" near)
803 (const :tag "Ignore near deadlines (compatibility)" t)
804 (const :tag "Ignore far deadlines" far)
805 (const :tag "Ignore all TODOs with a deadlines" all)
806 (const :tag "Show all TODOs, even if they have a deadline" nil)
807 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
809 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
810 "Time unit to use when possibly ignoring an agenda item.
812 See the docstring of various `org-agenda-todo-ignore-*' options.
813 The default is to compare time stamps using days. An item is thus
814 considered to be in the future if it is at least one day after today.
815 Non-nil means to compare time stamps using seconds. An item is then
816 considered future if it has a time value later than current time."
817 :group 'org-agenda-skip
818 :group 'org-agenda-todo-list
819 :version "24.4"
820 :package-version '(Org . "8.0")
821 :type '(choice
822 (const :tag "Compare time with days" nil)
823 (const :tag "Compare time with seconds" t)))
825 (defcustom org-agenda-tags-todo-honor-ignore-options nil
826 "Non-nil means honor todo-list ignores options also in tags-todo search.
827 The variables
828 `org-agenda-todo-ignore-with-date',
829 `org-agenda-todo-ignore-timestamp',
830 `org-agenda-todo-ignore-scheduled',
831 `org-agenda-todo-ignore-deadlines'
832 make the global TODO list skip entries that have time stamps of certain
833 kinds. If this option is set, the same options will also apply for the
834 tags-todo search, which is the general tags/property matcher
835 restricted to unfinished TODO entries only."
836 :group 'org-agenda-skip
837 :group 'org-agenda-todo-list
838 :group 'org-agenda-match-view
839 :type 'boolean)
841 (defcustom org-agenda-skip-scheduled-if-done nil
842 "Non-nil means don't show scheduled items in agenda when they are done.
843 This is relevant for the daily/weekly agenda, not for the TODO list. And
844 it applies only to the actual date of the scheduling. Warnings about
845 an item with a past scheduling dates are always turned off when the item
846 is DONE."
847 :group 'org-agenda-skip
848 :group 'org-agenda-daily/weekly
849 :type 'boolean)
851 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
852 "Non-nil means skip scheduling line if same entry shows because of deadline.
854 In the agenda of today, an entry can show up multiple times
855 because it is both scheduled and has a nearby deadline, and maybe
856 a plain time stamp as well.
858 When this variable is nil, the entry will be shown several times.
860 When set to t, then only the deadline is shown and the fact that
861 the entry is scheduled today or was scheduled previously is not
862 shown.
864 When set to the symbol `not-today', skip scheduled previously,
865 but not scheduled today.
867 When set to the symbol `repeated-after-deadline', skip scheduled
868 items if they are repeated beyond the current deadline."
869 :group 'org-agenda-skip
870 :group 'org-agenda-daily/weekly
871 :type '(choice
872 (const :tag "Never" nil)
873 (const :tag "Always" t)
874 (const :tag "Not when scheduled today" not-today)
875 (const :tag "When repeated past deadline" repeated-after-deadline)))
877 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
878 "Non-nil means skip timestamp line if same entry shows because of deadline.
879 In the agenda of today, an entry can show up multiple times
880 because it has both a plain timestamp and has a nearby deadline.
881 When this variable is t, then only the deadline is shown and the
882 fact that the entry has a timestamp for or including today is not
883 shown. When this variable is nil, the entry will be shown
884 several times."
885 :group 'org-agenda-skip
886 :group 'org-agenda-daily/weekly
887 :version "24.1"
888 :type '(choice
889 (const :tag "Never" nil)
890 (const :tag "Always" t)))
892 (defcustom org-agenda-skip-deadline-if-done nil
893 "Non-nil means don't show deadlines when the corresponding item is done.
894 When nil, the deadline is still shown and should give you a happy feeling.
895 This is relevant for the daily/weekly agenda. And it applied only to the
896 actually date of the deadline. Warnings about approaching and past-due
897 deadlines are always turned off when the item is DONE."
898 :group 'org-agenda-skip
899 :group 'org-agenda-daily/weekly
900 :type 'boolean)
902 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
903 "Non-nil means skip deadline prewarning when entry is also scheduled.
904 This will apply on all days where a prewarning for the deadline would
905 be shown, but not at the day when the entry is actually due. On that day,
906 the deadline will be shown anyway.
907 This variable may be set to nil, t, the symbol `pre-scheduled',
908 or a number which will then give the number of days before the actual
909 deadline when the prewarnings should resume. The symbol `pre-scheduled'
910 eliminates the deadline prewarning only prior to the scheduled date.
911 This can be used in a workflow where the first showing of the deadline will
912 trigger you to schedule it, and then you don't want to be reminded of it
913 because you will take care of it on the day when scheduled."
914 :group 'org-agenda-skip
915 :group 'org-agenda-daily/weekly
916 :version "24.1"
917 :type '(choice
918 (const :tag "Always show prewarning" nil)
919 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
920 (const :tag "Remove prewarning if entry is scheduled" t)
921 (integer :tag "Restart prewarning N days before deadline")))
923 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
924 "Non-nil means skip scheduled delay when entry also has a deadline.
925 This variable may be set to nil, t, the symbol `post-deadline',
926 or a number which will then give the number of days after the actual
927 scheduled date when the delay should expire. The symbol `post-deadline'
928 eliminates the schedule delay when the date is posterior to the deadline."
929 :group 'org-agenda-skip
930 :group 'org-agenda-daily/weekly
931 :version "24.4"
932 :package-version '(Org . "8.0")
933 :type '(choice
934 (const :tag "Always honor delay" nil)
935 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
936 (const :tag "Ignore delay if entry has a deadline" t)
937 (integer :tag "Honor delay up until N days after the scheduled date")))
939 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
940 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
941 When non-nil, after the search for timestamps has matched once in an
942 entry, the rest of the entry will not be searched."
943 :group 'org-agenda-skip
944 :type 'boolean)
946 (defcustom org-agenda-skip-timestamp-if-done nil
947 "Non-nil means don't select item by timestamp or -range if it is DONE."
948 :group 'org-agenda-skip
949 :group 'org-agenda-daily/weekly
950 :type 'boolean)
952 (defcustom org-agenda-dim-blocked-tasks t
953 "Non-nil means dim blocked tasks in the agenda display.
954 This causes some overhead during agenda construction, but if you
955 have turned on `org-enforce-todo-dependencies',
956 `org-enforce-todo-checkbox-dependencies', or any other blocking
957 mechanism, this will create useful feedback in the agenda.
959 Instead of t, this variable can also have the value `invisible'.
960 Then blocked tasks will be invisible and only become visible when
961 they become unblocked. An exemption to this behavior is when a task is
962 blocked because of unchecked checkboxes below it. Since checkboxes do
963 not show up in the agenda views, making this task invisible you remove any
964 trace from agenda views that there is something to do. Therefore, a task
965 that is blocked because of checkboxes will never be made invisible, it
966 will only be dimmed."
967 :group 'org-agenda-daily/weekly
968 :group 'org-agenda-todo-list
969 :version "24.3"
970 :type '(choice
971 (const :tag "Do not dim" nil)
972 (const :tag "Dim to a gray face" t)
973 (const :tag "Make invisible" invisible)))
975 (defcustom org-timeline-show-empty-dates 3
976 "Non-nil means `org-timeline' also shows dates without an entry.
977 When nil, only the days which actually have entries are shown.
978 When t, all days between the first and the last date are shown.
979 When an integer, show also empty dates, but if there is a gap of more than
980 N days, just insert a special line indicating the size of the gap."
981 :group 'org-agenda-skip
982 :type '(choice
983 (const :tag "None" nil)
984 (const :tag "All" t)
985 (integer :tag "at most")))
987 (defgroup org-agenda-startup nil
988 "Options concerning initial settings in the Agenda in Org Mode."
989 :tag "Org Agenda Startup"
990 :group 'org-agenda)
992 (defcustom org-agenda-menu-show-matcher t
993 "Non-nil means show the match string in the agenda dispatcher menu.
994 When nil, the matcher string is not shown, but is put into the help-echo
995 property so than moving the mouse over the command shows it.
996 Setting it to nil is good if matcher strings are very long and/or if
997 you want to use two-columns display (see `org-agenda-menu-two-columns')."
998 :group 'org-agenda
999 :version "24.1"
1000 :type 'boolean)
1002 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1004 (defcustom org-agenda-menu-two-columns nil
1005 "Non-nil means, use two columns to show custom commands in the dispatcher.
1006 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1007 to nil."
1008 :group 'org-agenda
1009 :version "24.1"
1010 :type 'boolean)
1012 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1013 (defcustom org-agenda-finalize-hook nil
1014 "Hook run just before displaying an agenda buffer.
1015 The buffer is still writable when the hook is called.
1017 You can modify some of the buffer substrings but you should be
1018 extra careful not to modify the text properties of the agenda
1019 headlines as the agenda display heavily relies on them."
1020 :group 'org-agenda-startup
1021 :type 'hook)
1023 (defcustom org-agenda-mouse-1-follows-link nil
1024 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1025 A longer mouse click will still set point. Does not work on XEmacs.
1026 Needs to be set before org.el is loaded."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-start-with-follow-mode nil
1031 "The initial value of follow mode in a newly created agenda window."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-follow-indirect nil
1036 "Non-nil means `org-agenda-follow-mode' displays only the
1037 current item's tree, in an indirect buffer."
1038 :group 'org-agenda
1039 :version "24.1"
1040 :type 'boolean)
1042 (defcustom org-agenda-show-outline-path t
1043 "Non-nil means show outline path in echo area after line motion."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-start-with-entry-text-mode nil
1048 "The initial value of entry-text-mode in a newly created agenda window."
1049 :group 'org-agenda-startup
1050 :type 'boolean)
1052 (defcustom org-agenda-entry-text-maxlines 5
1053 "Number of text lines to be added when `E' is pressed in the agenda.
1055 Note that this variable only used during agenda display. Add add entry text
1056 when exporting the agenda, configure the variable
1057 `org-agenda-add-entry-ext-maxlines'."
1058 :group 'org-agenda
1059 :type 'integer)
1061 (defcustom org-agenda-entry-text-exclude-regexps nil
1062 "List of regular expressions to clean up entry text.
1063 The complete matches of all regular expressions in this list will be
1064 removed from entry text before it is shown in the agenda."
1065 :group 'org-agenda
1066 :type '(repeat (regexp)))
1068 (defcustom org-agenda-entry-text-leaders " > "
1069 "Text prepended to the entry text in agenda buffers."
1070 :version "24.4"
1071 :package-version '(Org . "8.0")
1072 :group 'org-agenda
1073 :type 'string)
1075 (defvar org-agenda-entry-text-cleanup-hook nil
1076 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1077 This cleanup is done in a temporary buffer, so the function may inspect and
1078 change the entire buffer.
1079 Some default stuff like drawers and scheduling/deadline dates will already
1080 have been removed when this is called, as will any matches for regular
1081 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1083 (defvar org-agenda-include-inactive-timestamps nil
1084 "Non-nil means include inactive time stamps in agenda and timeline.
1085 Dynamically scoped.")
1087 (defgroup org-agenda-windows nil
1088 "Options concerning the windows used by the Agenda in Org Mode."
1089 :tag "Org Agenda Windows"
1090 :group 'org-agenda)
1092 (defcustom org-agenda-window-setup 'reorganize-frame
1093 "How the agenda buffer should be displayed.
1094 Possible values for this option are:
1096 current-window Show agenda in the current window, keeping all other windows.
1097 other-window Use `switch-to-buffer-other-window' to display agenda.
1098 reorganize-frame Show only two windows on the current frame, the current
1099 window and the agenda.
1100 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1101 Also, when exiting the agenda, kill that frame.
1102 See also the variable `org-agenda-restore-windows-after-quit'."
1103 :group 'org-agenda-windows
1104 :type '(choice
1105 (const current-window)
1106 (const other-frame)
1107 (const other-window)
1108 (const reorganize-frame)))
1110 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1111 "The min and max height of the agenda window as a fraction of frame height.
1112 The value of the variable is a cons cell with two numbers between 0 and 1.
1113 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1114 :group 'org-agenda-windows
1115 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1117 (defcustom org-agenda-restore-windows-after-quit nil
1118 "Non-nil means restore window configuration upon exiting agenda.
1119 Before the window configuration is changed for displaying the agenda,
1120 the current status is recorded. When the agenda is exited with
1121 `q' or `x' and this option is set, the old state is restored. If
1122 `org-agenda-window-setup' is `other-frame', the value of this
1123 option will be ignored."
1124 :group 'org-agenda-windows
1125 :type 'boolean)
1127 (defcustom org-agenda-ndays nil
1128 "Number of days to include in overview display.
1129 Should be 1 or 7.
1130 Obsolete, see `org-agenda-span'."
1131 :group 'org-agenda-daily/weekly
1132 :type '(choice (const nil)
1133 (integer)))
1135 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1137 (defcustom org-agenda-span 'week
1138 "Number of days to include in overview display.
1139 Can be day, week, month, year, or any number of days.
1140 Custom commands can set this variable in the options section."
1141 :group 'org-agenda-daily/weekly
1142 :type '(choice (const :tag "Day" day)
1143 (const :tag "Week" week)
1144 (const :tag "Fortnight" fortnight)
1145 (const :tag "Month" month)
1146 (const :tag "Year" year)
1147 (integer :tag "Custom")))
1149 (defcustom org-agenda-start-on-weekday 1
1150 "Non-nil means start the overview always on the specified weekday.
1151 0 denotes Sunday, 1 denotes Monday, etc.
1152 When nil, always start on the current day.
1153 Custom commands can set this variable in the options section."
1154 :group 'org-agenda-daily/weekly
1155 :type '(choice (const :tag "Today" nil)
1156 (integer :tag "Weekday No.")))
1158 (defcustom org-agenda-show-all-dates t
1159 "Non-nil means `org-agenda' shows every day in the selected range.
1160 When nil, only the days which actually have entries are shown."
1161 :group 'org-agenda-daily/weekly
1162 :type 'boolean)
1164 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1165 "Format string for displaying dates in the agenda.
1166 Used by the daily/weekly agenda and by the timeline. This should be
1167 a format string understood by `format-time-string', or a function returning
1168 the formatted date as a string. The function must take a single argument,
1169 a calendar-style date list like (month day year)."
1170 :group 'org-agenda-daily/weekly
1171 :type '(choice
1172 (string :tag "Format string")
1173 (function :tag "Function")))
1175 (defun org-agenda-format-date-aligned (date)
1176 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1177 This function makes sure that dates are aligned for easy reading."
1178 (require 'cal-iso)
1179 (let* ((dayname (calendar-day-name date))
1180 (day (cadr date))
1181 (day-of-week (calendar-day-of-week date))
1182 (month (car date))
1183 (monthname (calendar-month-name month))
1184 (year (nth 2 date))
1185 (iso-week (org-days-to-iso-week
1186 (calendar-absolute-from-gregorian date)))
1187 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1188 (1- year))
1189 ((and (= month 12) (<= iso-week 1))
1190 (1+ year))
1191 (t year)))
1192 (weekstring (if (= day-of-week 1)
1193 (format " W%02d" iso-week)
1194 "")))
1195 (format "%-10s %2d %s %4d%s"
1196 dayname day monthname year weekstring)))
1198 (defcustom org-agenda-time-leading-zero nil
1199 "Non-nil means use leading zero for military times in agenda.
1200 For example, 9:30am would become 09:30 rather than 9:30."
1201 :group 'org-agenda-daily/weekly
1202 :version "24.1"
1203 :type 'boolean)
1205 (defcustom org-agenda-timegrid-use-ampm nil
1206 "When set, show AM/PM style timestamps on the timegrid."
1207 :group 'org-agenda
1208 :version "24.1"
1209 :type 'boolean)
1211 (defun org-agenda-time-of-day-to-ampm (time)
1212 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1213 (let* ((hour-number (string-to-number (substring time 0 -3)))
1214 (minute (substring time -2))
1215 (ampm "am"))
1216 (cond
1217 ((equal hour-number 12)
1218 (setq ampm "pm"))
1219 ((> hour-number 12)
1220 (setq ampm "pm")
1221 (setq hour-number (- hour-number 12))))
1222 (concat
1223 (if org-agenda-time-leading-zero
1224 (format "%02d" hour-number)
1225 (format "%02s" (number-to-string hour-number)))
1226 ":" minute ampm)))
1228 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1229 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1230 (if org-agenda-timegrid-use-ampm
1231 (org-agenda-time-of-day-to-ampm time)
1232 time))
1234 (defcustom org-agenda-weekend-days '(6 0)
1235 "Which days are weekend?
1236 These days get the special face `org-agenda-date-weekend' in the agenda
1237 and timeline buffers."
1238 :group 'org-agenda-daily/weekly
1239 :type '(set :greedy t
1240 (const :tag "Monday" 1)
1241 (const :tag "Tuesday" 2)
1242 (const :tag "Wednesday" 3)
1243 (const :tag "Thursday" 4)
1244 (const :tag "Friday" 5)
1245 (const :tag "Saturday" 6)
1246 (const :tag "Sunday" 0)))
1248 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1249 "Non-nil means jump to today when moving a past date forward in time.
1250 When using S-right in the agenda to move a a date forward, and the date
1251 stamp currently points to the past, the first key press will move it
1252 to today. WHen nil, just move one day forward even if the date stays
1253 in the past."
1254 :group 'org-agenda-daily/weekly
1255 :version "24.1"
1256 :type 'boolean)
1258 (defcustom org-agenda-include-diary nil
1259 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1260 Custom commands can set this variable in the options section."
1261 :group 'org-agenda-daily/weekly
1262 :type 'boolean)
1264 (defcustom org-agenda-include-deadlines t
1265 "If non-nil, include entries within their deadline warning period.
1266 Custom commands can set this variable in the options section."
1267 :group 'org-agenda-daily/weekly
1268 :version "24.1"
1269 :type 'boolean)
1271 (defcustom org-agenda-repeating-timestamp-show-all t
1272 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1273 When set to a list of strings, only show occurrences of repeating
1274 stamps for these TODO keywords. When nil, only one occurrence is
1275 shown, either today or the nearest into the future."
1276 :group 'org-agenda-daily/weekly
1277 :type '(choice
1278 (const :tag "Show repeating stamps" t)
1279 (repeat :tag "Show repeating stamps for these TODO keywords"
1280 (string :tag "TODO Keyword"))
1281 (const :tag "Don't show repeating stamps" nil)))
1283 (defcustom org-scheduled-past-days 10000
1284 "Number of days to continue listing scheduled items not marked DONE.
1285 When an item is scheduled on a date, it shows up in the agenda on this
1286 day and will be listed until it is marked done for the number of days
1287 given here."
1288 :group 'org-agenda-daily/weekly
1289 :type 'integer)
1291 (defcustom org-agenda-log-mode-items '(closed clock)
1292 "List of items that should be shown in agenda log mode.
1293 This list may contain the following symbols:
1295 closed Show entries that have been closed on that day.
1296 clock Show entries that have received clocked time on that day.
1297 state Show all logged state changes.
1298 Note that instead of changing this variable, you can also press `C-u l' in
1299 the agenda to display all available LOG items temporarily."
1300 :group 'org-agenda-daily/weekly
1301 :type '(set :greedy t (const closed) (const clock) (const state)))
1303 (defcustom org-agenda-clock-consistency-checks
1304 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1305 :gap-ok-around ("4:00")
1306 :default-face ((:background "DarkRed") (:foreground "white"))
1307 :overlap-face nil :gap-face nil :no-end-time-face nil
1308 :long-face nil :short-face nil)
1309 "This is a property list, with the following keys:
1311 :max-duration Mark clocking chunks that are longer than this time.
1312 This is a time string like \"HH:MM\", or the number
1313 of minutes as an integer.
1315 :min-duration Mark clocking chunks that are shorter that this.
1316 This is a time string like \"HH:MM\", or the number
1317 of minutes as an integer.
1319 :max-gap Mark gaps between clocking chunks that are longer than
1320 this duration. A number of minutes, or a string
1321 like \"HH:MM\".
1323 :gap-ok-around List of times during the day which are usually not working
1324 times. When a gap is detected, but the gap contains any
1325 of these times, the gap is *not* reported. For example,
1326 if this is (\"4:00\" \"13:00\") then gaps that contain
1327 4:00 in the morning (i.e. the night) and 13:00
1328 (i.e. a typical lunch time) do not cause a warning.
1329 You should have at least one time during the night in this
1330 list, or otherwise the first task each morning will trigger
1331 a warning because it follows a long gap.
1333 Furthermore, the following properties can be used to define faces for
1334 issue display.
1336 :default-face the default face, if the specific face is undefined
1337 :overlap-face face for overlapping clocks
1338 :gap-face face for gaps between clocks
1339 :no-end-time-face face for incomplete clocks
1340 :long-face face for clock intervals that are too long
1341 :short-face face for clock intervals that are too short"
1342 :group 'org-agenda-daily/weekly
1343 :group 'org-clock
1344 :version "24.1"
1345 :type 'plist)
1347 (defcustom org-agenda-log-mode-add-notes t
1348 "Non-nil means add first line of notes to log entries in agenda views.
1349 If a log item like a state change or a clock entry is associated with
1350 notes, the first line of these notes will be added to the entry in the
1351 agenda display."
1352 :group 'org-agenda-daily/weekly
1353 :type 'boolean)
1355 (defcustom org-agenda-start-with-log-mode nil
1356 "The initial value of log-mode in a newly created agenda window.
1357 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1358 explanations on the possible values."
1359 :group 'org-agenda-startup
1360 :group 'org-agenda-daily/weekly
1361 :type '(choice (const :tag "Don't show log items" nil)
1362 (const :tag "Show only log items" only)
1363 (const :tag "Show all possible log items" clockcheck)
1364 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1365 (choice (const :tag "Show closed log items" closed)
1366 (const :tag "Show clocked log items" clock)
1367 (const :tag "Show all logged state changes" state)))))
1369 (defcustom org-agenda-start-with-clockreport-mode nil
1370 "The initial value of clockreport-mode in a newly created agenda window."
1371 :group 'org-agenda-startup
1372 :group 'org-agenda-daily/weekly
1373 :type 'boolean)
1375 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1376 "Property list with parameters for the clocktable in clockreport mode.
1377 This is the display mode that shows a clock table in the daily/weekly
1378 agenda, the properties for this dynamic block can be set here.
1379 The usual clocktable parameters are allowed here, but you cannot set
1380 the properties :name, :tstart, :tend, :block, and :scope - these will
1381 be overwritten to make sure the content accurately reflects the
1382 current display in the agenda."
1383 :group 'org-agenda-daily/weekly
1384 :type 'plist)
1386 (defcustom org-agenda-search-view-always-boolean nil
1387 "Non-nil means the search string is interpreted as individual parts.
1389 The search string for search view can either be interpreted as a phrase,
1390 or as a list of snippets that define a boolean search for a number of
1391 strings.
1393 When this is non-nil, the string will be split on whitespace, and each
1394 snippet will be searched individually, and all must match in order to
1395 select an entry. A snippet is then a single string of non-white
1396 characters, or a string in double quotes, or a regexp in {} braces.
1397 If a snippet is preceded by \"-\", the snippet must *not* match.
1398 \"+\" is syntactic sugar for positive selection. Each snippet may
1399 be found as a full word or a partial word, but see the variable
1400 `org-agenda-search-view-force-full-words'.
1402 When this is nil, search will look for the entire search phrase as one,
1403 with each space character matching any amount of whitespace, including
1404 line breaks.
1406 Even when this is nil, you can still switch to Boolean search dynamically
1407 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1408 is a regexp marked with braces like \"{abc}\", this will also switch to
1409 boolean search."
1410 :group 'org-agenda-search-view
1411 :version "24.1"
1412 :type 'boolean)
1414 (org-defvaralias 'org-agenda-search-view-search-words-only
1415 'org-agenda-search-view-always-boolean)
1417 (defcustom org-agenda-search-view-force-full-words nil
1418 "Non-nil means, search words must be matches as complete words.
1419 When nil, they may also match part of a word."
1420 :group 'org-agenda-search-view
1421 :version "24.1"
1422 :type 'boolean)
1424 (defcustom org-agenda-search-view-max-outline-level 0
1425 "Maximum outline level to display in search view.
1426 E.g. when this is set to 1, the search view will only
1427 show headlines of level 1. When set to 0, the default
1428 value, don't limit agenda view by outline level."
1429 :group 'org-agenda-search-view
1430 :version "24.4"
1431 :package-version '(Org . "8.3")
1432 :type 'integer)
1434 (defgroup org-agenda-time-grid nil
1435 "Options concerning the time grid in the Org-mode Agenda."
1436 :tag "Org Agenda Time Grid"
1437 :group 'org-agenda)
1439 (defcustom org-agenda-search-headline-for-time t
1440 "Non-nil means search headline for a time-of-day.
1441 If the headline contains a time-of-day in one format or another, it will
1442 be used to sort the entry into the time sequence of items for a day.
1443 Some people have time stamps in the headline that refer to the creation
1444 time or so, and then this produces an unwanted side effect. If this is
1445 the case for your, use this variable to turn off searching the headline
1446 for a time."
1447 :group 'org-agenda-time-grid
1448 :type 'boolean)
1450 (defcustom org-agenda-use-time-grid t
1451 "Non-nil means show a time grid in the agenda schedule.
1452 A time grid is a set of lines for specific times (like every two hours between
1453 8:00 and 20:00). The items scheduled for a day at specific times are
1454 sorted in between these lines.
1455 For details about when the grid will be shown, and what it will look like, see
1456 the variable `org-agenda-time-grid'."
1457 :group 'org-agenda-time-grid
1458 :type 'boolean)
1460 (defcustom org-agenda-time-grid
1461 '((daily today require-timed)
1462 "----------------"
1463 (800 1000 1200 1400 1600 1800 2000))
1465 "The settings for time grid for agenda display.
1466 This is a list of three items. The first item is again a list. It contains
1467 symbols specifying conditions when the grid should be displayed:
1469 daily if the agenda shows a single day
1470 weekly if the agenda shows an entire week
1471 today show grid on current date, independent of daily/weekly display
1472 require-timed show grid only if at least one item has a time specification
1474 The second item is a string which will be placed behind the grid time.
1476 The third item is a list of integers, indicating the times that should have
1477 a grid line."
1478 :group 'org-agenda-time-grid
1479 :type
1480 '(list
1481 (set :greedy t :tag "Grid Display Options"
1482 (const :tag "Show grid in single day agenda display" daily)
1483 (const :tag "Show grid in weekly agenda display" weekly)
1484 (const :tag "Always show grid for today" today)
1485 (const :tag "Show grid only if any timed entries are present"
1486 require-timed)
1487 (const :tag "Skip grid times already present in an entry"
1488 remove-match))
1489 (string :tag "Grid String")
1490 (repeat :tag "Grid Times" (integer :tag "Time"))))
1492 (defcustom org-agenda-show-current-time-in-grid t
1493 "Non-nil means show the current time in the time grid."
1494 :group 'org-agenda-time-grid
1495 :version "24.1"
1496 :type 'boolean)
1498 (defcustom org-agenda-current-time-string
1499 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1500 "The string for the current time marker in the agenda."
1501 :group 'org-agenda-time-grid
1502 :version "24.1"
1503 :type 'string)
1505 (defgroup org-agenda-sorting nil
1506 "Options concerning sorting in the Org-mode Agenda."
1507 :tag "Org Agenda Sorting"
1508 :group 'org-agenda)
1510 (defcustom org-agenda-sorting-strategy
1511 '((agenda habit-down time-up priority-down category-keep)
1512 (todo priority-down category-keep)
1513 (tags priority-down category-keep)
1514 (search category-keep))
1515 "Sorting structure for the agenda items of a single day.
1516 This is a list of symbols which will be used in sequence to determine
1517 if an entry should be listed before another entry. The following
1518 symbols are recognized:
1520 time-up Put entries with time-of-day indications first, early first
1521 time-down Put entries with time-of-day indications first, late first
1522 timestamp-up Sort by any timestamp, early first
1523 timestamp-down Sort by any timestamp, late first
1524 scheduled-up Sort by scheduled timestamp, early first
1525 scheduled-down Sort by scheduled timestamp, late first
1526 deadline-up Sort by deadline timestamp, early first
1527 deadline-down Sort by deadline timestamp, late first
1528 ts-up Sort by active timestamp, early first
1529 ts-down Sort by active timestamp, late first
1530 tsia-up Sort by inactive timestamp, early first
1531 tsia-down Sort by inactive timestamp, late first
1532 category-keep Keep the default order of categories, corresponding to the
1533 sequence in `org-agenda-files'.
1534 category-up Sort alphabetically by category, A-Z.
1535 category-down Sort alphabetically by category, Z-A.
1536 tag-up Sort alphabetically by last tag, A-Z.
1537 tag-down Sort alphabetically by last tag, Z-A.
1538 priority-up Sort numerically by priority, high priority last.
1539 priority-down Sort numerically by priority, high priority first.
1540 todo-state-up Sort by todo state, tasks that are done last.
1541 todo-state-down Sort by todo state, tasks that are done first.
1542 effort-up Sort numerically by estimated effort, high effort last.
1543 effort-down Sort numerically by estimated effort, high effort first.
1544 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1545 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1546 habit-up Put entries that are habits first
1547 habit-down Put entries that are habits last
1548 alpha-up Sort headlines alphabetically
1549 alpha-down Sort headlines alphabetically, reversed
1551 The different possibilities will be tried in sequence, and testing stops
1552 if one comparison returns a \"not-equal\". For example, the default
1553 '(time-up category-keep priority-down)
1554 means: Pull out all entries having a specified time of day and sort them,
1555 in order to make a time schedule for the current day the first thing in the
1556 agenda listing for the day. Of the entries without a time indication, keep
1557 the grouped in categories, don't sort the categories, but keep them in
1558 the sequence given in `org-agenda-files'. Within each category sort by
1559 priority.
1561 Leaving out `category-keep' would mean that items will be sorted across
1562 categories by priority.
1564 Instead of a single list, this can also be a set of list for specific
1565 contents, with a context symbol in the car of the list, any of
1566 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1568 Custom commands can bind this variable in the options section."
1569 :group 'org-agenda-sorting
1570 :type `(choice
1571 (repeat :tag "General" ,org-sorting-choice)
1572 (list :tag "Individually"
1573 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for TODO lists" todo)
1576 (repeat ,org-sorting-choice))
1577 (cons (const :tag "Strategy for Tags matches" tags)
1578 (repeat ,org-sorting-choice))
1579 (cons (const :tag "Strategy for search matches" search)
1580 (repeat ,org-sorting-choice)))))
1582 (defcustom org-agenda-cmp-user-defined nil
1583 "A function to define the comparison `user-defined'.
1584 This function must receive two arguments, agenda entry a and b.
1585 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1586 the user comparison, return nil.
1587 When this is defined, you can make `user-defined-up' and `user-defined-down'
1588 part of an agenda sorting strategy."
1589 :group 'org-agenda-sorting
1590 :type 'symbol)
1592 (defcustom org-sort-agenda-notime-is-late t
1593 "Non-nil means items without time are considered late.
1594 This is only relevant for sorting. When t, items which have no explicit
1595 time like 15:30 will be considered as 99:01, i.e. later than any items which
1596 do have a time. When nil, the default time is before 0:00. You can use this
1597 option to decide if the schedule for today should come before or after timeless
1598 agenda entries."
1599 :group 'org-agenda-sorting
1600 :type 'boolean)
1602 (defcustom org-sort-agenda-noeffort-is-high t
1603 "Non-nil means items without effort estimate are sorted as high effort.
1604 This also applies when filtering an agenda view with respect to the
1605 < or > effort operator. Then, tasks with no effort defined will be treated
1606 as tasks with high effort.
1607 When nil, such items are sorted as 0 minutes effort."
1608 :group 'org-agenda-sorting
1609 :type 'boolean)
1611 (defgroup org-agenda-line-format nil
1612 "Options concerning the entry prefix in the Org-mode agenda display."
1613 :tag "Org Agenda Line Format"
1614 :group 'org-agenda)
1616 (defcustom org-agenda-prefix-format
1617 '((agenda . " %i %-12:c%?-12t% s")
1618 (timeline . " % s")
1619 (todo . " %i %-12:c")
1620 (tags . " %i %-12:c")
1621 (search . " %i %-12:c"))
1622 "Format specifications for the prefix of items in the agenda views.
1623 An alist with five entries, each for the different agenda types. The
1624 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1625 The values are format strings.
1627 This format works similar to a printf format, with the following meaning:
1629 %c the category of the item, \"Diary\" for entries from the diary,
1630 or as given by the CATEGORY keyword or derived from the file name
1631 %e the effort required by the item
1632 %l the level of the item (insert X space(s) if item is of level X)
1633 %i the icon category of the item, see `org-agenda-category-icon-alist'
1634 %T the last tag of the item (ignore inherited tags, which come first)
1635 %t the HH:MM time-of-day specification if one applies to the entry
1636 %s Scheduling/Deadline information, a short string
1637 %b show breadcrumbs, i.e., the names of the higher levels
1638 %(expression) Eval EXPRESSION and replace the control string
1639 by the result
1641 All specifiers work basically like the standard `%s' of printf, but may
1642 contain two additional characters: a question mark just after the `%'
1643 and a whitespace/punctuation character just before the final letter.
1645 If the first character after `%' is a question mark, the entire field
1646 will only be included if the corresponding value applies to the current
1647 entry. This is useful for fields which should have fixed width when
1648 present, but zero width when absent. For example, \"%?-12t\" will
1649 result in a 12 character time field if a time of the day is specified,
1650 but will completely disappear in entries which do not contain a time.
1652 If there is punctuation or whitespace character just before the
1653 final format letter, this character will be appended to the field
1654 value if the value is not empty. For example, the format
1655 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1656 the category is empty, no additional colon is inserted.
1658 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1659 which means:
1661 - Indent the line with two space characters
1662 - Give the category a 12 chars wide field, padded with whitespace on
1663 the right (because of `-'). Append a colon if there is a category
1664 (because of `:').
1665 - If there is a time-of-day, put it into a 12 chars wide field. If no
1666 time, don't put in an empty field, just skip it (because of '?').
1667 - Finally, put the scheduling information.
1669 See also the variables `org-agenda-remove-times-when-in-prefix' and
1670 `org-agenda-remove-tags'.
1672 Custom commands can set this variable in the options section."
1673 :type '(choice
1674 (string :tag "General format")
1675 (list :greedy t :tag "View dependent"
1676 (cons (const agenda) (string :tag "Format"))
1677 (cons (const timeline) (string :tag "Format"))
1678 (cons (const todo) (string :tag "Format"))
1679 (cons (const tags) (string :tag "Format"))
1680 (cons (const search) (string :tag "Format"))))
1681 :group 'org-agenda-line-format)
1683 (defvar org-prefix-format-compiled nil
1684 "The compiled prefix format and associated variables.
1685 This is a list where first element is a list of variable bindings, and second
1686 element is the compiled format expression. See the variable
1687 `org-agenda-prefix-format'.")
1689 (defcustom org-agenda-todo-keyword-format "%-1s"
1690 "Format for the TODO keyword in agenda lines.
1691 Set this to something like \"%-12s\" if you want all TODO keywords
1692 to occupy a fixed space in the agenda display."
1693 :group 'org-agenda-line-format
1694 :type 'string)
1696 (defcustom org-agenda-diary-sexp-prefix nil
1697 "A regexp that matches part of a diary sexp entry
1698 which should be treated as scheduling/deadline information in
1699 `org-agenda'.
1701 For example, you can use this to extract the `diary-remind-message' from
1702 `diary-remind' entries."
1703 :group 'org-agenda-line-format
1704 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1706 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1707 "Text preceding timerange entries in the agenda view.
1708 This is a list with two strings. The first applies when the range
1709 is entirely on one day. The second applies if the range spans several days.
1710 The strings may have two \"%d\" format specifiers which will be filled
1711 with the sequence number of the days, and the total number of days in the
1712 range, respectively."
1713 :group 'org-agenda-line-format
1714 :type '(list
1715 (string :tag "Deadline today ")
1716 (choice :tag "Deadline relative"
1717 (string :tag "Format string")
1718 (function))))
1720 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1721 "Text preceding scheduled items in the agenda view.
1722 This is a list with two strings. The first applies when the item is
1723 scheduled on the current day. The second applies when it has been scheduled
1724 previously, it may contain a %d indicating that this is the nth time that
1725 this item is scheduled, due to automatic rescheduling of unfinished items
1726 for the following day. So this number is one larger than the number of days
1727 that passed since this item was scheduled first."
1728 :group 'org-agenda-line-format
1729 :version "24.4"
1730 :package-version '(Org . "8.0")
1731 :type '(list
1732 (string :tag "Scheduled today ")
1733 (string :tag "Scheduled previously")))
1735 (defcustom org-agenda-inactive-leader "["
1736 "Text preceding item pulled into the agenda by inactive time stamps.
1737 These entries are added to the agenda when pressing \"[\"."
1738 :group 'org-agenda-line-format
1739 :version "24.1"
1740 :type 'string)
1742 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1743 "Text preceding deadline items in the agenda view.
1744 This is a list with three strings. The first applies when the item has its
1745 deadline on the current day. The second applies when the deadline is in the
1746 future, the third one when it is in the past. The strings may contain %d
1747 to capture the number of days."
1748 :group 'org-agenda-line-format
1749 :version "24.4"
1750 :package-version '(Org . "8.0")
1751 :type '(list
1752 (string :tag "Deadline today ")
1753 (string :tag "Deadline in the future ")
1754 (string :tag "Deadline in the past ")))
1756 (defcustom org-agenda-remove-times-when-in-prefix t
1757 "Non-nil means remove duplicate time specifications in agenda items.
1758 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1759 time-of-day specification in a headline or diary entry is extracted and
1760 placed into the prefix. If this option is non-nil, the original specification
1761 \(a timestamp or -range, or just a plain time(range) specification like
1762 11:30-4pm) will be removed for agenda display. This makes the agenda less
1763 cluttered.
1764 The option can be t or nil. It may also be the symbol `beg', indicating
1765 that the time should only be removed when it is located at the beginning of
1766 the headline/diary entry."
1767 :group 'org-agenda-line-format
1768 :type '(choice
1769 (const :tag "Always" t)
1770 (const :tag "Never" nil)
1771 (const :tag "When at beginning of entry" beg)))
1773 (defcustom org-agenda-remove-timeranges-from-blocks nil
1774 "Non-nil means remove time ranges specifications in agenda
1775 items that span on several days."
1776 :group 'org-agenda-line-format
1777 :version "24.1"
1778 :type 'boolean)
1780 (defcustom org-agenda-default-appointment-duration nil
1781 "Default duration for appointments that only have a starting time.
1782 When nil, no duration is specified in such cases.
1783 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1784 :group 'org-agenda-line-format
1785 :type '(choice
1786 (integer :tag "Minutes")
1787 (const :tag "No default duration")))
1789 (defcustom org-agenda-show-inherited-tags t
1790 "Non-nil means show inherited tags in each agenda line.
1792 When this option is set to 'always, it take precedences over
1793 `org-agenda-use-tag-inheritance' and inherited tags are shown
1794 in every agenda.
1796 When this option is set to t (the default), inherited tags are
1797 shown when they are available, i.e. when the value of
1798 `org-agenda-use-tag-inheritance' has been taken into account.
1800 This can be set to a list of agenda types in which the agenda
1801 must display the inherited tags. Available types are 'todo,
1802 'agenda, 'search and 'timeline.
1804 When set to nil, never show inherited tags in agenda lines."
1805 :group 'org-agenda-line-format
1806 :group 'org-agenda
1807 :version "24.3"
1808 :type '(choice
1809 (const :tag "Show inherited tags when available" t)
1810 (const :tag "Always show inherited tags" always)
1811 (repeat :tag "Show inherited tags only in selected agenda types"
1812 (symbol :tag "Agenda type"))))
1814 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1815 "List of agenda view types where to use tag inheritance.
1817 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1818 controlled by `org-use-tag-inheritance'. In other agenda types,
1819 `org-use-tag-inheritance' is not used for the selection of the
1820 agenda entries. Still, you may want the agenda to be aware of
1821 the inherited tags anyway, e.g. for later tag filtering.
1823 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1825 This variable has no effect if `org-agenda-show-inherited-tags'
1826 is set to 'always. In that case, the agenda is aware of those
1827 tags.
1829 The default value sets tags in every agenda type. Setting this
1830 option to nil will speed up non-tags agenda view a lot."
1831 :group 'org-agenda
1832 :version "24.3"
1833 :type '(choice
1834 (const :tag "Use tag inheritance in all agenda types" t)
1835 (repeat :tag "Use tag inheritance in selected agenda types"
1836 (symbol :tag "Agenda type"))))
1838 (defcustom org-agenda-hide-tags-regexp nil
1839 "Regular expression used to filter away specific tags in agenda views.
1840 This means that these tags will be present, but not be shown in the agenda
1841 line. Secondary filtering will still work on the hidden tags.
1842 Nil means don't hide any tags."
1843 :group 'org-agenda-line-format
1844 :type '(choice
1845 (const :tag "Hide none" nil)
1846 (string :tag "Regexp ")))
1848 (defcustom org-agenda-remove-tags nil
1849 "Non-nil means remove the tags from the headline copy in the agenda.
1850 When this is the symbol `prefix', only remove tags when
1851 `org-agenda-prefix-format' contains a `%T' specifier."
1852 :group 'org-agenda-line-format
1853 :type '(choice
1854 (const :tag "Always" t)
1855 (const :tag "Never" nil)
1856 (const :tag "When prefix format contains %T" prefix)))
1858 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1859 'org-agenda-remove-tags)
1861 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1862 "Shift tags in agenda items to this column.
1863 If this number is positive, it specifies the column. If it is negative,
1864 it means that the tags should be flushright to that column. For example,
1865 -80 works well for a normal 80 character screen."
1866 :group 'org-agenda-line-format
1867 :type 'integer)
1869 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1871 (defcustom org-agenda-fontify-priorities 'cookies
1872 "Non-nil means highlight low and high priorities in agenda.
1873 When t, the highest priority entries are bold, lowest priority italic.
1874 However, settings in `org-priority-faces' will overrule these faces.
1875 When this variable is the symbol `cookies', only fontify the
1876 cookies, not the entire task.
1877 This may also be an association list of priority faces, whose
1878 keys are the character values of `org-highest-priority',
1879 `org-default-priority', and `org-lowest-priority' (the default values
1880 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1881 color as a string, or a list like `(:background \"Red\")'.
1882 If it is a color, the variable `org-faces-easy-properties'
1883 determines if it is a foreground or a background color."
1884 :group 'org-agenda-line-format
1885 :type '(choice
1886 (const :tag "Never" nil)
1887 (const :tag "Defaults" t)
1888 (const :tag "Cookies only" cookies)
1889 (repeat :tag "Specify"
1890 (list (character :tag "Priority" :value ?A)
1891 (choice :tag "Face "
1892 (string :tag "Color")
1893 (sexp :tag "Face"))))))
1895 (defcustom org-agenda-day-face-function nil
1896 "Function called to determine what face should be used to display a day.
1897 The only argument passed to that function is the day. It should
1898 returns a face, or nil if does not want to specify a face and let
1899 the normal rules apply."
1900 :group 'org-agenda-line-format
1901 :version "24.1"
1902 :type '(choice (const nil) (function)))
1904 (defcustom org-agenda-category-icon-alist nil
1905 "Alist of category icon to be displayed in agenda views.
1907 Each entry should have the following format:
1909 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1911 Where CATEGORY-REGEXP is a regexp matching the categories where
1912 the icon should be displayed.
1913 FILE-OR-DATA either a file path or a string containing image data.
1915 The other fields can be omitted safely if not needed:
1916 TYPE indicates the image type.
1917 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1918 image data.
1919 PROPS are additional image attributes to assign to the image,
1920 like, e.g. `:ascent center'.
1922 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1924 If you want to set the display properties yourself, just put a
1925 list as second element:
1927 (CATEGORY-REGEXP (MY PROPERTY LIST))
1929 For example, to display a 16px horizontal space for Emacs
1930 category, you can use:
1932 (\"Emacs\" '(space . (:width (16))))"
1933 :group 'org-agenda-line-format
1934 :version "24.1"
1935 :type '(alist :key-type (string :tag "Regexp matching category")
1936 :value-type (choice (list :tag "Icon"
1937 (string :tag "File or data")
1938 (symbol :tag "Type")
1939 (boolean :tag "Data?")
1940 (repeat :tag "Extra image properties" :inline t symbol))
1941 (list :tag "Display properties" sexp))))
1943 (defgroup org-agenda-column-view nil
1944 "Options concerning column view in the agenda."
1945 :tag "Org Agenda Column View"
1946 :group 'org-agenda)
1948 (defcustom org-agenda-columns-show-summaries t
1949 "Non-nil means show summaries for columns displayed in the agenda view."
1950 :group 'org-agenda-column-view
1951 :type 'boolean)
1953 (defcustom org-agenda-columns-compute-summary-properties t
1954 "Non-nil means recompute all summary properties before column view.
1955 When column view in the agenda is listing properties that have a summary
1956 operator, it can go to all relevant buffers and recompute the summaries
1957 there. This can mean overhead for the agenda column view, but is necessary
1958 to have thing up to date.
1959 As a special case, a CLOCKSUM property also makes sure that the clock
1960 computations are current."
1961 :group 'org-agenda-column-view
1962 :type 'boolean)
1964 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1965 "Non-nil means the duration of an appointment will add to day effort.
1966 The property to which appointment durations will be added is the one given
1967 in the option `org-effort-property'. If an appointment does not have
1968 an end time, `org-agenda-default-appointment-duration' will be used. If that
1969 is not set, an appointment without end time will not contribute to the time
1970 estimate."
1971 :group 'org-agenda-column-view
1972 :type 'boolean)
1974 (defcustom org-agenda-auto-exclude-function nil
1975 "A function called with a tag to decide if it is filtered on '/ RET'.
1976 The sole argument to the function, which is called once for each
1977 possible tag, is a string giving the name of the tag. The
1978 function should return either nil if the tag should be included
1979 as normal, or \"-<TAG>\" to exclude the tag.
1980 Note that for the purpose of tag filtering, only the lower-case version of
1981 all tags will be considered, so that this function will only ever see
1982 the lower-case version of all tags."
1983 :group 'org-agenda
1984 :type '(choice (const nil) (function)))
1986 (defcustom org-agenda-bulk-custom-functions nil
1987 "Alist of characters and custom functions for bulk actions.
1988 For example, this value makes those two functions available:
1990 '((?R set-category)
1991 (?C bulk-cut))
1993 With selected entries in an agenda buffer, `B R' will call
1994 the custom function `set-category' on the selected entries.
1995 Note that functions in this alist don't need to be quoted."
1996 :type 'alist
1997 :version "24.1"
1998 :group 'org-agenda)
2000 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2001 "Execute BODY with point at location given by `org-hd-marker' property.
2002 If STRING is non-nil, the text property will be fetched from position 0
2003 in that string. If STRING is nil, it will be fetched from the beginning
2004 of the current line."
2005 (org-with-gensyms (marker)
2006 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2007 'org-hd-marker ,string)))
2008 (with-current-buffer (marker-buffer ,marker)
2009 (save-excursion
2010 (goto-char ,marker)
2011 ,@body)))))
2012 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2014 (defun org-add-agenda-custom-command (entry)
2015 "Replace or add a command in `org-agenda-custom-commands'.
2016 This is mostly for hacking and trying a new command - once the command
2017 works you probably want to add it to `org-agenda-custom-commands' for good."
2018 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2019 (if ass
2020 (setcdr ass (cdr entry))
2021 (push entry org-agenda-custom-commands))))
2023 ;;; Define the org-agenda-mode
2025 (defvar org-agenda-mode-map (make-sparse-keymap)
2026 "Keymap for `org-agenda-mode'.")
2027 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2029 (defvar org-agenda-menu) ; defined later in this file.
2030 (defvar org-agenda-restrict nil) ; defined later in this file.
2031 (defvar org-agenda-follow-mode nil)
2032 (defvar org-agenda-entry-text-mode nil)
2033 (defvar org-agenda-clockreport-mode nil)
2034 (defvar org-agenda-show-log nil)
2035 (defvar org-agenda-redo-command nil)
2036 (defvar org-agenda-query-string nil)
2037 (defvar org-agenda-mode-hook nil
2038 "Hook run after `org-agenda-mode' is turned on.
2039 The buffer is still writable when this hook is called.")
2040 (defvar org-agenda-type nil)
2041 (defvar org-agenda-force-single-file nil)
2042 (defvar org-agenda-bulk-marked-entries nil
2043 "List of markers that refer to marked entries in the agenda.")
2045 ;;; Multiple agenda buffers support
2047 (defcustom org-agenda-sticky nil
2048 "Non-nil means agenda q key will bury agenda buffers.
2049 Agenda commands will then show existing buffer instead of generating new ones.
2050 When nil, `q' will kill the single agenda buffer."
2051 :group 'org-agenda
2052 :version "24.3"
2053 :type 'boolean)
2056 ;;;###autoload
2057 (defun org-toggle-sticky-agenda (&optional arg)
2058 "Toggle `org-agenda-sticky'."
2059 (interactive "P")
2060 (let ((new-value (if arg
2061 (> (prefix-numeric-value arg) 0)
2062 (not org-agenda-sticky))))
2063 (if (equal new-value org-agenda-sticky)
2064 (and (org-called-interactively-p 'interactive)
2065 (message "Sticky agenda was already %s"
2066 (if org-agenda-sticky "enabled" "disabled")))
2067 (setq org-agenda-sticky new-value)
2068 (org-agenda-kill-all-agenda-buffers)
2069 (and (org-called-interactively-p 'interactive)
2070 (message "Sticky agenda was %s"
2071 (if org-agenda-sticky "enabled" "disabled"))))))
2073 (defvar org-agenda-buffer nil
2074 "Agenda buffer currently being generated.")
2076 (defvar org-agenda-last-prefix-arg nil)
2077 (defvar org-agenda-this-buffer-name nil)
2078 (defvar org-agenda-doing-sticky-redo nil)
2079 (defvar org-agenda-this-buffer-is-sticky nil)
2081 (defconst org-agenda-local-vars
2082 '(org-agenda-this-buffer-name
2083 org-agenda-undo-list
2084 org-agenda-pending-undo-list
2085 org-agenda-follow-mode
2086 org-agenda-entry-text-mode
2087 org-agenda-clockreport-mode
2088 org-agenda-show-log
2089 org-agenda-redo-command
2090 org-agenda-query-string
2091 org-agenda-type
2092 org-agenda-bulk-marked-entries
2093 org-agenda-undo-has-started-in
2094 org-agenda-info
2095 org-agenda-pre-window-conf
2096 org-agenda-columns-active
2097 org-agenda-tag-filter
2098 org-agenda-category-filter
2099 org-agenda-top-headline-filter
2100 org-agenda-regexp-filter
2101 org-agenda-effort-filter
2102 org-agenda-markers
2103 org-agenda-last-search-view-search-was-boolean
2104 org-agenda-filtered-by-category
2105 org-agenda-filter-form
2106 org-agenda-cycle-counter
2107 org-agenda-last-prefix-arg)
2108 "Variables that must be local in agenda buffers to allow multiple buffers.")
2110 (defun org-agenda-mode ()
2111 "Mode for time-sorted view on action items in Org-mode files.
2113 The following commands are available:
2115 \\{org-agenda-mode-map}"
2116 (interactive)
2117 (cond (org-agenda-doing-sticky-redo
2118 ;; Refreshing sticky agenda-buffer
2120 ;; Preserve the value of `org-agenda-local-vars' variables,
2121 ;; while letting `kill-all-local-variables' kill the rest
2122 (let ((save (buffer-local-variables)))
2123 (kill-all-local-variables)
2124 (mapc 'make-local-variable org-agenda-local-vars)
2125 (dolist (elem save)
2126 (let ((var (car elem))
2127 (val (cdr elem)))
2128 (when (and val
2129 (member var org-agenda-local-vars))
2130 (set var val)))))
2131 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2132 (org-agenda-sticky
2133 ;; Creating a sticky Agenda buffer for the first time
2134 (kill-all-local-variables)
2135 (mapc 'make-local-variable org-agenda-local-vars)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2138 ;; Creating a non-sticky agenda buffer
2139 (kill-all-local-variables)
2140 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2141 (setq org-agenda-undo-list nil
2142 org-agenda-pending-undo-list nil
2143 org-agenda-bulk-marked-entries nil)
2144 (setq major-mode 'org-agenda-mode)
2145 ;; Keep global-font-lock-mode from turning on font-lock-mode
2146 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2147 (setq mode-name "Org-Agenda")
2148 (setq indent-tabs-mode nil)
2149 (use-local-map org-agenda-mode-map)
2150 (easy-menu-add org-agenda-menu)
2151 (if org-startup-truncated (setq truncate-lines t))
2152 (org-set-local 'line-move-visual nil)
2153 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2154 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2155 ;; Make sure properties are removed when copying text
2156 (org-add-hook 'filter-buffer-substring-functions
2157 (lambda (fun start end delete)
2158 (substring-no-properties (funcall fun start end delete)))
2159 nil t)
2160 (unless org-agenda-keep-modes
2161 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2162 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2163 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2164 org-agenda-show-log org-agenda-start-with-log-mode))
2165 (add-to-invisibility-spec '(org-filtered))
2166 (add-to-invisibility-spec '(org-link))
2167 (easy-menu-change
2168 '("Agenda") "Agenda Files"
2169 (append
2170 (list
2171 (vector
2172 (if (get 'org-agenda-files 'org-restrict)
2173 "Restricted to single file"
2174 "Edit File List")
2175 '(org-edit-agenda-file-list)
2176 (not (get 'org-agenda-files 'org-restrict)))
2177 "--")
2178 (mapcar 'org-file-menu-entry (org-agenda-files))))
2179 (org-agenda-set-mode-name)
2180 (apply
2181 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2182 (list 'org-agenda-mode-hook)))
2184 (substitute-key-definition 'undo 'org-agenda-undo
2185 org-agenda-mode-map global-map)
2186 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2187 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2188 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2189 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2190 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2191 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2192 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2193 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2194 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2195 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2196 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2197 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2198 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2199 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2200 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2201 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2202 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2203 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2204 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2205 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2206 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2207 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2208 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2209 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2210 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2211 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2212 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2213 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2214 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2215 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2216 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2217 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2218 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2219 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2220 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2221 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2222 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2223 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2224 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2225 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2226 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2227 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2228 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2229 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2230 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2231 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2232 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2233 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2234 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2235 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2237 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2238 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2239 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2240 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2241 (while l (org-defkey org-agenda-mode-map
2242 (int-to-string (pop l)) 'digit-argument)))
2244 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2245 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2246 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2247 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2248 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2249 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2250 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2251 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2252 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2253 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2254 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2255 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2256 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2257 'org-clock-modify-effort-estimate)
2258 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2259 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2260 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2261 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2262 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2263 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2264 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2265 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2266 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2267 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2268 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2269 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2270 (substitute-key-definition 'next-line 'org-agenda-next-line
2271 org-agenda-mode-map global-map)
2272 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2273 org-agenda-mode-map global-map)
2274 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2275 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2276 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2277 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2278 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2279 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2280 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2281 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2282 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2283 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2284 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2285 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2287 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2289 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2291 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2293 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2294 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2297 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2298 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2299 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2300 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2301 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2302 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2303 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2305 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2306 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2307 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2308 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2309 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2310 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2311 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2312 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2313 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2314 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2315 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2316 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2317 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2318 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2319 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2320 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2322 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2323 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2325 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2326 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2328 (when org-agenda-mouse-1-follows-link
2329 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2330 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2331 '("Agenda"
2332 ("Agenda Files")
2333 "--"
2334 ("Agenda Dates"
2335 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2336 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2337 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2338 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2339 "--"
2340 ("View"
2341 ["Day View" org-agenda-day-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'day)
2344 :keys "v d (or just d)"]
2345 ["Week View" org-agenda-week-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'week)
2348 :keys "v w"]
2349 ["Fortnight View" org-agenda-fortnight-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'fortnight)
2352 :keys "v f"]
2353 ["Month View" org-agenda-month-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'month)
2356 :keys "v m"]
2357 ["Year View" org-agenda-year-view
2358 :active (org-agenda-check-type nil 'agenda)
2359 :style radio :selected (eq org-agenda-current-span 'year)
2360 :keys "v y"]
2361 "--"
2362 ["Include Diary" org-agenda-toggle-diary
2363 :style toggle :selected org-agenda-include-diary
2364 :active (org-agenda-check-type nil 'agenda)]
2365 ["Include Deadlines" org-agenda-toggle-deadlines
2366 :style toggle :selected org-agenda-include-deadlines
2367 :active (org-agenda-check-type nil 'agenda)]
2368 ["Use Time Grid" org-agenda-toggle-time-grid
2369 :style toggle :selected org-agenda-use-time-grid
2370 :active (org-agenda-check-type nil 'agenda)]
2371 "--"
2372 ["Show clock report" org-agenda-clockreport-mode
2373 :style toggle :selected org-agenda-clockreport-mode
2374 :active (org-agenda-check-type nil 'agenda)]
2375 ["Show some entry text" org-agenda-entry-text-mode
2376 :style toggle :selected org-agenda-entry-text-mode
2377 :active t]
2378 "--"
2379 ["Show Logbook entries" org-agenda-log-mode
2380 :style toggle :selected org-agenda-show-log
2381 :active (org-agenda-check-type nil 'agenda 'timeline)
2382 :keys "v l (or just l)"]
2383 ["Include archived trees" org-agenda-archives-mode
2384 :style toggle :selected org-agenda-archives-mode :active t
2385 :keys "v a"]
2386 ["Include archive files" (org-agenda-archives-mode t)
2387 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2388 :keys "v A"]
2389 "--"
2390 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2391 ["Write view to file" org-agenda-write t]
2392 ["Rebuild buffer" org-agenda-redo t]
2393 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2394 "--"
2395 ["Show original entry" org-agenda-show t]
2396 ["Go To (other window)" org-agenda-goto t]
2397 ["Go To (this window)" org-agenda-switch-to t]
2398 ["Capture with cursor date" org-agenda-capture t]
2399 ["Follow Mode" org-agenda-follow-mode
2400 :style toggle :selected org-agenda-follow-mode :active t]
2401 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2402 "--"
2403 ("TODO"
2404 ["Cycle TODO" org-agenda-todo t]
2405 ["Next TODO set" org-agenda-todo-nextset t]
2406 ["Previous TODO set" org-agenda-todo-previousset t]
2407 ["Add note" org-agenda-add-note t])
2408 ("Archive/Refile/Delete"
2409 ["Archive default" org-agenda-archive-default t]
2410 ["Archive default" org-agenda-archive-default-with-confirmation t]
2411 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2412 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2413 ["Archive subtree" org-agenda-archive t]
2414 "--"
2415 ["Refile" org-agenda-refile t]
2416 "--"
2417 ["Delete subtree" org-agenda-kill t])
2418 ("Bulk action"
2419 ["Mark entry" org-agenda-bulk-mark t]
2420 ["Mark all" org-agenda-bulk-mark-all t]
2421 ["Unmark entry" org-agenda-bulk-unmark t]
2422 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2423 ["Toggle mark" org-agenda-bulk-toggle t]
2424 ["Toggle all" org-agenda-bulk-toggle-all t]
2425 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2426 ["Act on all marked" org-agenda-bulk-action t]
2427 "--"
2428 ("Tags and Properties"
2429 ["Show all Tags" org-agenda-show-tags t]
2430 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2431 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2432 "--"
2433 ["Column View" org-columns t])
2434 ("Deadline/Schedule"
2435 ["Schedule" org-agenda-schedule t]
2436 ["Set Deadline" org-agenda-deadline t]
2437 "--"
2438 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2439 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2440 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2441 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2442 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2443 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2444 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2445 ("Clock and Effort"
2446 ["Clock in" org-agenda-clock-in t]
2447 ["Clock out" org-agenda-clock-out t]
2448 ["Clock cancel" org-agenda-clock-cancel t]
2449 ["Goto running clock" org-clock-goto t]
2450 "--"
2451 ["Set Effort" org-agenda-set-effort t]
2452 ["Change clocked effort" org-clock-modify-effort-estimate
2453 (org-clock-is-active)])
2454 ("Priority"
2455 ["Set Priority" org-agenda-priority t]
2456 ["Increase Priority" org-agenda-priority-up t]
2457 ["Decrease Priority" org-agenda-priority-down t]
2458 ["Show Priority" org-show-priority t])
2459 ("Calendar/Diary"
2460 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2462 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2463 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2464 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2465 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2466 "--"
2467 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2468 "--"
2469 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2470 "--"
2471 ("MobileOrg"
2472 ["Push Files and Views" org-mobile-push t]
2473 ["Get Captured and Flagged" org-mobile-pull t]
2474 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2475 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2476 "--"
2477 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2478 "--"
2479 ["Quit" org-agenda-quit t]
2480 ["Exit and Release Buffers" org-agenda-exit t]
2483 ;;; Agenda undo
2485 (defvar org-agenda-allow-remote-undo t
2486 "Non-nil means allow remote undo from the agenda buffer.")
2487 (defvar org-agenda-undo-has-started-in nil
2488 "Buffers that have already seen `undo-start' in the current undo sequence.")
2490 (defun org-agenda-undo ()
2491 "Undo a remote editing step in the agenda.
2492 This undoes changes both in the agenda buffer and in the remote buffer
2493 that have been changed along."
2494 (interactive)
2495 (or org-agenda-allow-remote-undo
2496 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2497 (if (not (eq this-command last-command))
2498 (setq org-agenda-undo-has-started-in nil
2499 org-agenda-pending-undo-list org-agenda-undo-list))
2500 (if (not org-agenda-pending-undo-list)
2501 (user-error "No further undo information"))
2502 (let* ((entry (pop org-agenda-pending-undo-list))
2503 buf line cmd rembuf)
2504 (setq cmd (pop entry) line (pop entry))
2505 (setq rembuf (nth 2 entry))
2506 (org-with-remote-undo rembuf
2507 (while (bufferp (setq buf (pop entry)))
2508 (if (pop entry)
2509 (with-current-buffer buf
2510 (let ((last-undo-buffer buf)
2511 (inhibit-read-only t))
2512 (unless (memq buf org-agenda-undo-has-started-in)
2513 (push buf org-agenda-undo-has-started-in)
2514 (make-local-variable 'pending-undo-list)
2515 (undo-start))
2516 (while (and pending-undo-list
2517 (listp pending-undo-list)
2518 (not (car pending-undo-list)))
2519 (pop pending-undo-list))
2520 (undo-more 1))))))
2521 (org-goto-line line)
2522 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2524 (defun org-verify-change-for-undo (l1 l2)
2525 "Verify that a real change occurred between the undo lists L1 and L2."
2526 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2527 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2528 (not (eq l1 l2)))
2530 ;;; Agenda dispatch
2532 (defvar org-agenda-restrict-begin (make-marker))
2533 (defvar org-agenda-restrict-end (make-marker))
2534 (defvar org-agenda-last-dispatch-buffer nil)
2535 (defvar org-agenda-overriding-restriction nil)
2537 (defcustom org-agenda-custom-commands-contexts nil
2538 "Alist of custom agenda keys and contextual rules.
2540 For example, if you have a custom agenda command \"p\" and you
2541 want this command to be accessible only from plain text files,
2542 use this:
2544 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2546 Here are the available contexts definitions:
2548 in-file: command displayed only in matching files
2549 in-mode: command displayed only in matching modes
2550 not-in-file: command not displayed in matching files
2551 not-in-mode: command not displayed in matching modes
2552 in-buffer: command displayed only in matching buffers
2553 not-in-buffer: command not displayed in matching buffers
2554 [function]: a custom function taking no argument
2556 If you define several checks, the agenda command will be
2557 accessible if there is at least one valid check.
2559 You can also bind a key to another agenda custom command
2560 depending on contextual rules.
2562 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2564 Here it means: in .txt files, use \"p\" as the key for the
2565 agenda command otherwise associated with \"q\". (The command
2566 originally associated with \"q\" is not displayed to avoid
2567 duplicates.)"
2568 :version "24.3"
2569 :group 'org-agenda-custom-commands
2570 :type '(repeat (list :tag "Rule"
2571 (string :tag " Agenda key")
2572 (string :tag "Replace by command")
2573 (repeat :tag "Available when"
2574 (choice
2575 (cons :tag "Condition"
2576 (choice
2577 (const :tag "In file" in-file)
2578 (const :tag "Not in file" not-in-file)
2579 (const :tag "In buffer" in-buffer)
2580 (const :tag "Not in buffer" not-in-buffer)
2581 (const :tag "In mode" in-mode)
2582 (const :tag "Not in mode" not-in-mode))
2583 (regexp))
2584 (function :tag "Custom function"))))))
2586 (defcustom org-agenda-max-entries nil
2587 "Maximum number of entries to display in an agenda.
2588 This can be nil (no limit) or an integer or an alist of agenda
2589 types with an associated number of entries to display in this
2590 type."
2591 :version "24.4"
2592 :package-version '(Org . "8.0")
2593 :group 'org-agenda-custom-commands
2594 :type '(choice (symbol :tag "No limit" nil)
2595 (integer :tag "Max number of entries")
2596 (repeat
2597 (cons (choice :tag "Agenda type"
2598 (const agenda)
2599 (const todo)
2600 (const tags)
2601 (const search)
2602 (const timeline))
2603 (integer :tag "Max number of entries")))))
2605 (defcustom org-agenda-max-todos nil
2606 "Maximum number of TODOs to display in an agenda.
2607 This can be nil (no limit) or an integer or an alist of agenda
2608 types with an associated number of entries to display in this
2609 type."
2610 :version "24.4"
2611 :package-version '(Org . "8.0")
2612 :group 'org-agenda-custom-commands
2613 :type '(choice (symbol :tag "No limit" nil)
2614 (integer :tag "Max number of TODOs")
2615 (repeat
2616 (cons (choice :tag "Agenda type"
2617 (const agenda)
2618 (const todo)
2619 (const tags)
2620 (const search)
2621 (const timeline))
2622 (integer :tag "Max number of TODOs")))))
2624 (defcustom org-agenda-max-tags nil
2625 "Maximum number of tagged entries to display in an agenda.
2626 This can be nil (no limit) or an integer or an alist of agenda
2627 types with an associated number of entries to display in this
2628 type."
2629 :version "24.4"
2630 :package-version '(Org . "8.0")
2631 :group 'org-agenda-custom-commands
2632 :type '(choice (symbol :tag "No limit" nil)
2633 (integer :tag "Max number of tagged entries")
2634 (repeat
2635 (cons (choice :tag "Agenda type"
2636 (const agenda)
2637 (const todo)
2638 (const tags)
2639 (const search)
2640 (const timeline))
2641 (integer :tag "Max number of tagged entries")))))
2643 (defcustom org-agenda-max-effort nil
2644 "Maximum cumulated effort duration for the agenda.
2645 This can be nil (no limit) or a number of minutes (as an integer)
2646 or an alist of agenda types with an associated number of minutes
2647 to limit entries to in this type."
2648 :version "24.4"
2649 :package-version '(Org . "8.0")
2650 :group 'org-agenda-custom-commands
2651 :type '(choice (symbol :tag "No limit" nil)
2652 (integer :tag "Max number of minutes")
2653 (repeat
2654 (cons (choice :tag "Agenda type"
2655 (const agenda)
2656 (const todo)
2657 (const tags)
2658 (const search)
2659 (const timeline))
2660 (integer :tag "Max number of minutes")))))
2662 (defvar org-keys nil)
2663 (defvar org-match nil)
2664 ;;;###autoload
2665 (defun org-agenda (&optional arg org-keys restriction)
2666 "Dispatch agenda commands to collect entries to the agenda buffer.
2667 Prompts for a command to execute. Any prefix arg will be passed
2668 on to the selected command. The default selections are:
2670 a Call `org-agenda-list' to display the agenda for current day or week.
2671 t Call `org-todo-list' to display the global todo list.
2672 T Call `org-todo-list' to display the global todo list, select only
2673 entries with a specific TODO keyword (the user gets a prompt).
2674 m Call `org-tags-view' to display headlines with tags matching
2675 a condition (the user is prompted for the condition).
2676 M Like `m', but select only TODO entries, no ordinary headlines.
2677 L Create a timeline for the current buffer.
2678 e Export views to associated files.
2679 s Search entries for keywords.
2680 S Search entries for keywords, only with TODO keywords.
2681 / Multi occur across all agenda files and also files listed
2682 in `org-agenda-text-search-extra-files'.
2683 < Restrict agenda commands to buffer, subtree, or region.
2684 Press several times to get the desired effect.
2685 > Remove a previous restriction.
2686 # List \"stuck\" projects.
2687 ! Configure what \"stuck\" means.
2688 C Configure custom agenda commands.
2690 More commands can be added by configuring the variable
2691 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2692 searches can be pre-defined in this way.
2694 If the current buffer is in Org-mode and visiting a file, you can also
2695 first press `<' once to indicate that the agenda should be temporarily
2696 \(until the next use of \\[org-agenda]) restricted to the current file.
2697 Pressing `<' twice means to restrict to the current subtree or region
2698 \(if active)."
2699 (interactive "P")
2700 (catch 'exit
2701 (let* ((prefix-descriptions nil)
2702 (org-agenda-buffer-name org-agenda-buffer-name)
2703 (org-agenda-window-setup (if (equal (buffer-name)
2704 org-agenda-buffer-name)
2705 'current-window
2706 org-agenda-window-setup))
2707 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2708 (org-agenda-custom-commands
2709 ;; normalize different versions
2710 (delq nil
2711 (mapcar
2712 (lambda (x)
2713 (cond ((stringp (cdr x))
2714 (push x prefix-descriptions)
2715 nil)
2716 ((stringp (nth 1 x)) x)
2717 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2718 (t (cons (car x) (cons "" (cdr x))))))
2719 org-agenda-custom-commands)))
2720 (org-agenda-custom-commands
2721 (org-contextualize-keys
2722 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2723 (buf (current-buffer))
2724 (bfn (buffer-file-name (buffer-base-buffer)))
2725 entry key type org-match lprops ans)
2726 ;; Turn off restriction unless there is an overriding one,
2727 (unless org-agenda-overriding-restriction
2728 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2729 ;; There is a request to keep the file list in place
2730 (put 'org-agenda-files 'org-restrict nil))
2731 (setq org-agenda-restrict nil)
2732 (move-marker org-agenda-restrict-begin nil)
2733 (move-marker org-agenda-restrict-end nil))
2734 ;; Delete old local properties
2735 (put 'org-agenda-redo-command 'org-lprops nil)
2736 ;; Delete previously set last-arguments
2737 (put 'org-agenda-redo-command 'last-args nil)
2738 ;; Remember where this call originated
2739 (setq org-agenda-last-dispatch-buffer (current-buffer))
2740 (unless org-keys
2741 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2742 org-keys (car ans)
2743 restriction (cdr ans)))
2744 ;; If we have sticky agenda buffers, set a name for the buffer,
2745 ;; depending on the invoking keys. The user may still set this
2746 ;; as a command option, which will overwrite what we do here.
2747 (if org-agenda-sticky
2748 (setq org-agenda-buffer-name
2749 (format "*Org Agenda(%s)*" org-keys)))
2750 ;; Establish the restriction, if any
2751 (when (and (not org-agenda-overriding-restriction) restriction)
2752 (put 'org-agenda-files 'org-restrict (list bfn))
2753 (cond
2754 ((eq restriction 'region)
2755 (setq org-agenda-restrict (current-buffer))
2756 (move-marker org-agenda-restrict-begin (region-beginning))
2757 (move-marker org-agenda-restrict-end (region-end)))
2758 ((eq restriction 'subtree)
2759 (save-excursion
2760 (setq org-agenda-restrict (current-buffer))
2761 (org-back-to-heading t)
2762 (move-marker org-agenda-restrict-begin (point))
2763 (move-marker org-agenda-restrict-end
2764 (progn (org-end-of-subtree t)))))))
2766 ;; For example the todo list should not need it (but does...)
2767 (cond
2768 ((setq entry (assoc org-keys org-agenda-custom-commands))
2769 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2770 (progn
2771 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2772 lprops (nth 4 entry))
2773 (if org-agenda-sticky
2774 (setq org-agenda-buffer-name
2775 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2776 (format "*Org Agenda(%s)*" org-keys))))
2777 (put 'org-agenda-redo-command 'org-lprops lprops)
2778 (cond
2779 ((eq type 'agenda)
2780 (org-let lprops '(org-agenda-list current-prefix-arg)))
2781 ((eq type 'agenda*)
2782 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2783 ((eq type 'alltodo)
2784 (org-let lprops '(org-todo-list current-prefix-arg)))
2785 ((eq type 'search)
2786 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2787 ((eq type 'stuck)
2788 (org-let lprops '(org-agenda-list-stuck-projects
2789 current-prefix-arg)))
2790 ((eq type 'tags)
2791 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2792 ((eq type 'tags-todo)
2793 (org-let lprops '(org-tags-view '(4) org-match)))
2794 ((eq type 'todo)
2795 (org-let lprops '(org-todo-list org-match)))
2796 ((eq type 'tags-tree)
2797 (org-check-for-org-mode)
2798 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2799 ((eq type 'todo-tree)
2800 (org-check-for-org-mode)
2801 (org-let lprops
2802 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2803 (regexp-quote org-match) "\\>"))))
2804 ((eq type 'occur-tree)
2805 (org-check-for-org-mode)
2806 (org-let lprops '(org-occur org-match)))
2807 ((functionp type)
2808 (org-let lprops '(funcall type org-match)))
2809 ((fboundp type)
2810 (org-let lprops '(funcall type org-match)))
2811 (t (user-error "Invalid custom agenda command type %s" type))))
2812 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2813 ((equal org-keys "C")
2814 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2815 (customize-variable 'org-agenda-custom-commands))
2816 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2817 ((equal org-keys "s") (call-interactively 'org-search-view))
2818 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2819 ((equal org-keys "t") (call-interactively 'org-todo-list))
2820 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2821 ((equal org-keys "m") (call-interactively 'org-tags-view))
2822 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2823 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2824 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2825 (org-add-hook
2826 'post-command-hook
2827 (lambda ()
2828 (unless (current-message)
2829 (let* ((m (org-agenda-get-any-marker))
2830 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2831 (when note
2832 (message (concat
2833 "FLAGGING-NOTE ([?] for more info): "
2834 (org-add-props
2835 (replace-regexp-in-string
2836 "\\\\n" "//"
2837 (copy-sequence note))
2838 nil 'face 'org-warning)))))))
2839 t t))
2840 ((equal org-keys "L")
2841 (unless (derived-mode-p 'org-mode)
2842 (user-error "This is not an Org-mode file"))
2843 (unless restriction
2844 (put 'org-agenda-files 'org-restrict (list bfn))
2845 (org-call-with-arg 'org-timeline arg)))
2846 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2847 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2848 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2849 (t (user-error "Invalid agenda key"))))))
2851 (defvar org-agenda-multi)
2853 (defun org-agenda-append-agenda ()
2854 "Append another agenda view to the current one.
2855 This function allows interactive building of block agendas.
2856 Agenda views are separated by `org-agenda-block-separator'."
2857 (interactive)
2858 (unless (derived-mode-p 'org-agenda-mode)
2859 (user-error "Can only append from within agenda buffer"))
2860 (let ((org-agenda-multi t))
2861 (org-agenda)
2862 (widen)
2863 (org-agenda-finalize)
2864 (setq buffer-read-only t)
2865 (org-agenda-fit-window-to-buffer)))
2867 (defun org-agenda-normalize-custom-commands (cmds)
2868 "Normalize custom commands CMDS."
2869 (delq nil
2870 (mapcar
2871 (lambda (x)
2872 (cond ((stringp (cdr x)) nil)
2873 ((stringp (nth 1 x)) x)
2874 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2875 (t (cons (car x) (cons "" (cdr x))))))
2876 cmds)))
2878 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2879 "The user interface for selecting an agenda command."
2880 (catch 'exit
2881 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2882 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2883 (region-p (org-region-active-p))
2884 (custom org-agenda-custom-commands)
2885 (selstring "")
2886 restriction second-time
2887 c entry key type match prefixes rmheader header-end custom1 desc
2888 line lines left right n n1)
2889 (save-window-excursion
2890 (delete-other-windows)
2891 (org-switch-to-buffer-other-window " *Agenda Commands*")
2892 (erase-buffer)
2893 (insert (eval-when-compile
2894 (let ((header
2895 "Press key for an agenda command: < Buffer, subtree/region restriction
2896 -------------------------------- > Remove restriction
2897 a Agenda for current week or day e Export agenda views
2898 t List of all TODO entries T Entries with special TODO kwd
2899 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2900 s Search for keywords S Like s, but only TODO entries
2901 L Timeline for current buffer # List stuck projects (!=configure)
2902 / Multi-occur C Configure custom agenda commands
2903 ? Find :FLAGGED: entries * Toggle sticky agenda views
2905 (start 0))
2906 (while (string-match
2907 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2908 header start)
2909 (setq start (match-end 0))
2910 (add-text-properties (match-beginning 2) (match-end 2)
2911 '(face bold) header))
2912 header)))
2913 (setq header-end (point-marker))
2914 (while t
2915 (setq custom1 custom)
2916 (when (eq rmheader t)
2917 (org-goto-line 1)
2918 (re-search-forward ":" nil t)
2919 (delete-region (match-end 0) (point-at-eol))
2920 (forward-char 1)
2921 (looking-at "-+")
2922 (delete-region (match-end 0) (point-at-eol))
2923 (move-marker header-end (match-end 0)))
2924 (goto-char header-end)
2925 (delete-region (point) (point-max))
2927 ;; Produce all the lines that describe custom commands and prefixes
2928 (setq lines nil)
2929 (while (setq entry (pop custom1))
2930 (setq key (car entry) desc (nth 1 entry)
2931 type (nth 2 entry)
2932 match (nth 3 entry))
2933 (if (> (length key) 1)
2934 (add-to-list 'prefixes (string-to-char key))
2935 (setq line
2936 (format
2937 "%-4s%-14s"
2938 (org-add-props (copy-sequence key)
2939 '(face bold))
2940 (cond
2941 ((string-match "\\S-" desc) desc)
2942 ((eq type 'agenda) "Agenda for current week or day")
2943 ((eq type 'agenda*) "Appointments for current week or day")
2944 ((eq type 'alltodo) "List of all TODO entries")
2945 ((eq type 'search) "Word search")
2946 ((eq type 'stuck) "List of stuck projects")
2947 ((eq type 'todo) "TODO keyword")
2948 ((eq type 'tags) "Tags query")
2949 ((eq type 'tags-todo) "Tags (TODO)")
2950 ((eq type 'tags-tree) "Tags tree")
2951 ((eq type 'todo-tree) "TODO kwd tree")
2952 ((eq type 'occur-tree) "Occur tree")
2953 ((functionp type) (if (symbolp type)
2954 (symbol-name type)
2955 "Lambda expression"))
2956 (t "???"))))
2957 (if org-agenda-menu-show-matcher
2958 (setq line
2959 (concat line ": "
2960 (cond
2961 ((stringp match)
2962 (setq match (copy-sequence match))
2963 (org-add-props match nil 'face 'org-warning))
2964 ((listp type)
2965 (format "set of %d commands" (length type))))))
2966 (if (org-string-nw-p match)
2967 (add-text-properties
2968 0 (length line) (list 'help-echo
2969 (concat "Matcher: " match)) line)))
2970 (push line lines)))
2971 (setq lines (nreverse lines))
2972 (when prefixes
2973 (mapc (lambda (x)
2974 (push
2975 (format "%s %s"
2976 (org-add-props (char-to-string x)
2977 nil 'face 'bold)
2978 (or (cdr (assoc (concat selstring
2979 (char-to-string x))
2980 prefix-descriptions))
2981 "Prefix key"))
2982 lines))
2983 prefixes))
2985 ;; Check if we should display in two columns
2986 (if org-agenda-menu-two-columns
2987 (progn
2988 (setq n (length lines)
2989 n1 (+ (/ n 2) (mod n 2))
2990 right (nthcdr n1 lines)
2991 left (copy-sequence lines))
2992 (setcdr (nthcdr (1- n1) left) nil))
2993 (setq left lines right nil))
2994 (while left
2995 (insert "\n" (pop left))
2996 (when right
2997 (if (< (current-column) 40)
2998 (move-to-column 40 t)
2999 (insert " "))
3000 (insert (pop right))))
3002 ;; Make the window the right size
3003 (goto-char (point-min))
3004 (if second-time
3005 (if (not (pos-visible-in-window-p (point-max)))
3006 (org-fit-window-to-buffer))
3007 (setq second-time t)
3008 (org-fit-window-to-buffer))
3010 ;; Ask for selection
3011 (message "Press key for agenda command%s:"
3012 (if (or restrict-ok org-agenda-overriding-restriction)
3013 (if org-agenda-overriding-restriction
3014 " (restriction lock active)"
3015 (if restriction
3016 (format " (restricted to %s)" restriction)
3017 " (unrestricted)"))
3018 ""))
3019 (setq c (read-char-exclusive))
3020 (message "")
3021 (cond
3022 ((assoc (char-to-string c) custom)
3023 (setq selstring (concat selstring (char-to-string c)))
3024 (throw 'exit (cons selstring restriction)))
3025 ((memq c prefixes)
3026 (setq selstring (concat selstring (char-to-string c))
3027 prefixes nil
3028 rmheader (or rmheader t)
3029 custom (delq nil (mapcar
3030 (lambda (x)
3031 (if (or (= (length (car x)) 1)
3032 (/= (string-to-char (car x)) c))
3034 (cons (substring (car x) 1) (cdr x))))
3035 custom))))
3036 ((eq c ?*)
3037 (call-interactively 'org-toggle-sticky-agenda)
3038 (sit-for 2))
3039 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3040 (message "Restriction is only possible in Org-mode buffers")
3041 (ding) (sit-for 1))
3042 ((eq c ?1)
3043 (org-agenda-remove-restriction-lock 'noupdate)
3044 (setq restriction 'buffer))
3045 ((eq c ?0)
3046 (org-agenda-remove-restriction-lock 'noupdate)
3047 (setq restriction (if region-p 'region 'subtree)))
3048 ((eq c ?<)
3049 (org-agenda-remove-restriction-lock 'noupdate)
3050 (setq restriction
3051 (cond
3052 ((eq restriction 'buffer)
3053 (if region-p 'region 'subtree))
3054 ((memq restriction '(subtree region))
3055 nil)
3056 (t 'buffer))))
3057 ((eq c ?>)
3058 (org-agenda-remove-restriction-lock 'noupdate)
3059 (setq restriction nil))
3060 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3061 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3062 ((and (> (length selstring) 0) (eq c ?\d))
3063 (delete-window)
3064 (org-agenda-get-restriction-and-command prefix-descriptions))
3066 ((equal c ?q) (error "Abort"))
3067 (t (user-error "Invalid key %c" c))))))))
3069 (defun org-agenda-fit-window-to-buffer ()
3070 "Fit the window to the buffer size."
3071 (and (memq org-agenda-window-setup '(reorganize-frame))
3072 (fboundp 'fit-window-to-buffer)
3073 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3074 (= (car org-agenda-window-frame-fractions) 1.0))
3075 (delete-other-windows)
3076 (org-fit-window-to-buffer
3078 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3079 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3081 (defvar org-cmd nil)
3082 (defvar org-agenda-overriding-cmd nil)
3083 (defvar org-agenda-overriding-arguments nil)
3084 (defvar org-agenda-overriding-cmd-arguments nil)
3085 (defun org-agenda-run-series (name series)
3086 "Run agenda NAME as a SERIES of agenda commands."
3087 (org-let (nth 1 series) '(org-agenda-prepare name))
3088 ;; We need to reset agenda markers here, because when constructing a
3089 ;; block agenda, the individual blocks do not do that.
3090 (org-agenda-reset-markers)
3091 (let* ((org-agenda-multi t)
3092 (redo (list 'org-agenda-run-series name (list 'quote series)))
3093 (cmds (car series))
3094 (gprops (nth 1 series))
3095 match ;; The byte compiler incorrectly complains about this. Keep it!
3096 org-cmd type lprops)
3097 (while (setq org-cmd (pop cmds))
3098 (setq type (car org-cmd)
3099 match (eval (nth 1 org-cmd))
3100 lprops (nth 2 org-cmd))
3101 (let ((org-agenda-overriding-arguments
3102 (if (eq org-agenda-overriding-cmd org-cmd)
3103 (or org-agenda-overriding-arguments
3104 org-agenda-overriding-cmd-arguments))))
3105 (cond
3106 ((eq type 'agenda)
3107 (org-let2 gprops lprops
3108 '(call-interactively 'org-agenda-list)))
3109 ((eq type 'agenda*)
3110 (org-let2 gprops lprops
3111 '(funcall 'org-agenda-list nil nil t)))
3112 ((eq type 'alltodo)
3113 (org-let2 gprops lprops
3114 '(call-interactively 'org-todo-list)))
3115 ((eq type 'search)
3116 (org-let2 gprops lprops
3117 '(org-search-view current-prefix-arg match nil)))
3118 ((eq type 'stuck)
3119 (org-let2 gprops lprops
3120 '(call-interactively 'org-agenda-list-stuck-projects)))
3121 ((eq type 'tags)
3122 (org-let2 gprops lprops
3123 '(org-tags-view current-prefix-arg match)))
3124 ((eq type 'tags-todo)
3125 (org-let2 gprops lprops
3126 '(org-tags-view '(4) match)))
3127 ((eq type 'todo)
3128 (org-let2 gprops lprops
3129 '(org-todo-list match)))
3130 ((fboundp type)
3131 (org-let2 gprops lprops
3132 '(funcall type match)))
3133 (t (error "Invalid type in command series")))))
3134 (widen)
3135 (let ((inhibit-read-only t))
3136 (add-text-properties (point-min) (point-max)
3137 `(org-series t org-series-redo-cmd ,redo)))
3138 (setq org-agenda-redo-command redo)
3139 (goto-char (point-min)))
3140 (org-agenda-fit-window-to-buffer)
3141 (org-let (nth 1 series) '(org-agenda-finalize)))
3143 ;;;###autoload
3144 (defmacro org-batch-agenda (cmd-key &rest parameters)
3145 "Run an agenda command in batch mode and send the result to STDOUT.
3146 If CMD-KEY is a string of length 1, it is used as a key in
3147 `org-agenda-custom-commands' and triggers this command. If it is a
3148 longer string it is used as a tags/todo match string.
3149 Parameters are alternating variable names and values that will be bound
3150 before running the agenda command."
3151 (org-eval-in-environment (org-make-parameter-alist parameters)
3152 (let (org-agenda-sticky)
3153 (if (> (length cmd-key) 2)
3154 (org-tags-view nil cmd-key)
3155 (org-agenda nil cmd-key))))
3156 (set-buffer org-agenda-buffer-name)
3157 (princ (buffer-string)))
3159 (defvar org-agenda-info nil)
3161 ;;;###autoload
3162 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3163 "Run an agenda command in batch mode and send the result to STDOUT.
3164 If CMD-KEY is a string of length 1, it is used as a key in
3165 `org-agenda-custom-commands' and triggers this command. If it is a
3166 longer string it is used as a tags/todo match string.
3167 Parameters are alternating variable names and values that will be bound
3168 before running the agenda command.
3170 The output gives a line for each selected agenda item. Each
3171 item is a list of comma-separated values, like this:
3173 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3175 category The category of the item
3176 head The headline, without TODO kwd, TAGS and PRIORITY
3177 type The type of the agenda entry, can be
3178 todo selected in TODO match
3179 tagsmatch selected in tags match
3180 diary imported from diary
3181 deadline a deadline on given date
3182 scheduled scheduled on given date
3183 timestamp entry has timestamp on given date
3184 closed entry was closed on given date
3185 upcoming-deadline warning about deadline
3186 past-scheduled forwarded scheduled item
3187 block entry has date block including g. date
3188 todo The todo keyword, if any
3189 tags All tags including inherited ones, separated by colons
3190 date The relevant date, like 2007-2-14
3191 time The time, like 15:00-16:50
3192 extra Sting with extra planning info
3193 priority-l The priority letter if any was given
3194 priority-n The computed numerical priority
3195 agenda-day The day in the agenda where this is listed"
3196 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3197 (org-make-parameter-alist parameters))
3198 (if (> (length cmd-key) 2)
3199 (org-tags-view nil cmd-key)
3200 (org-agenda nil cmd-key)))
3201 (set-buffer org-agenda-buffer-name)
3202 (let* ((lines (org-split-string (buffer-string) "\n"))
3203 line)
3204 (while (setq line (pop lines))
3205 (catch 'next
3206 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3207 (setq org-agenda-info
3208 (org-fix-agenda-info (text-properties-at 0 line)))
3209 (princ
3210 (mapconcat 'org-agenda-export-csv-mapper
3211 '(org-category txt type todo tags date time extra
3212 priority-letter priority agenda-day)
3213 ","))
3214 (princ "\n")))))
3216 (defun org-fix-agenda-info (props)
3217 "Make sure all properties on an agenda item have a canonical form.
3218 This ensures the export commands can easily use it."
3219 (let (tmp re)
3220 (when (setq tmp (plist-get props 'tags))
3221 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3222 (when (setq tmp (plist-get props 'date))
3223 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3224 (let ((calendar-date-display-form '(year "-" month "-" day)))
3225 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3227 (setq tmp (calendar-date-string tmp)))
3228 (setq props (plist-put props 'date tmp)))
3229 (when (setq tmp (plist-get props 'day))
3230 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3231 (let ((calendar-date-display-form '(year "-" month "-" day)))
3232 (setq tmp (calendar-date-string tmp)))
3233 (setq props (plist-put props 'day tmp))
3234 (setq props (plist-put props 'agenda-day tmp)))
3235 (when (setq tmp (plist-get props 'txt))
3236 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3237 (plist-put props 'priority-letter (match-string 1 tmp))
3238 (setq tmp (replace-match "" t t tmp)))
3239 (when (and (setq re (plist-get props 'org-todo-regexp))
3240 (setq re (concat "\\`\\.*" re " ?"))
3241 (string-match re tmp))
3242 (plist-put props 'todo (match-string 1 tmp))
3243 (setq tmp (replace-match "" t t tmp)))
3244 (plist-put props 'txt tmp)))
3245 props)
3247 (defun org-agenda-export-csv-mapper (prop)
3248 (let ((res (plist-get org-agenda-info prop)))
3249 (setq res
3250 (cond
3251 ((not res) "")
3252 ((stringp res) res)
3253 (t (prin1-to-string res))))
3254 (while (string-match "," res)
3255 (setq res (replace-match ";" t t res)))
3256 (org-trim res)))
3258 ;;;###autoload
3259 (defun org-store-agenda-views (&rest parameters)
3260 "Store agenda views."
3261 (interactive)
3262 (eval (list 'org-batch-store-agenda-views)))
3264 ;;;###autoload
3265 (defmacro org-batch-store-agenda-views (&rest parameters)
3266 "Run all custom agenda commands that have a file argument."
3267 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3268 (pop-up-frames nil)
3269 (dir default-directory)
3270 (pars (org-make-parameter-alist parameters))
3271 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3272 (save-window-excursion
3273 (while cmds
3274 (setq cmd (pop cmds)
3275 thiscmdkey (car cmd)
3276 thiscmdcmd (cdr cmd)
3277 match (nth 2 thiscmdcmd)
3278 bufname (if org-agenda-sticky
3279 (or (and (stringp match)
3280 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3281 (format "*Org Agenda(%s)*" thiscmdkey))
3282 org-agenda-buffer-name)
3283 cmd-or-set (nth 2 cmd)
3284 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3285 files (nth (if (listp cmd-or-set) 4 5) cmd))
3286 (if (stringp files) (setq files (list files)))
3287 (when files
3288 (org-eval-in-environment (append org-agenda-exporter-settings
3289 opts pars)
3290 (org-agenda nil thiscmdkey))
3291 (set-buffer bufname)
3292 (while files
3293 (org-eval-in-environment (append org-agenda-exporter-settings
3294 opts pars)
3295 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3296 (and (get-buffer bufname)
3297 (kill-buffer bufname)))))))
3299 (defvar org-agenda-current-span nil
3300 "The current span used in the agenda view.") ; local variable in the agenda buffer
3301 (defun org-agenda-mark-header-line (pos)
3302 "Mark the line at POS as an agenda structure header."
3303 (save-excursion
3304 (goto-char pos)
3305 (put-text-property (point-at-bol) (point-at-eol)
3306 'org-agenda-structural-header t)
3307 (when org-agenda-title-append
3308 (put-text-property (point-at-bol) (point-at-eol)
3309 'org-agenda-title-append org-agenda-title-append))))
3311 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3312 (defvar org-agenda-write-buffer-name "Agenda View")
3313 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3314 "Write the current buffer (an agenda view) as a file.
3315 Depending on the extension of the file name, plain text (.txt),
3316 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3317 If the extension is .ics, run icalendar export over all files used
3318 to construct the agenda and limit the export to entries listed in the
3319 agenda now.
3320 If the extension is .org, collect all subtrees corresponding to the
3321 agenda entries and add them in an .org file.
3322 With prefix argument OPEN, open the new file immediately.
3323 If NOSETTINGS is given, do not scope the settings of
3324 `org-agenda-exporter-settings' into the export commands. This is used when
3325 the settings have already been scoped and we do not wish to overrule other,
3326 higher priority settings.
3327 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3328 (interactive "FWrite agenda to file: \nP")
3329 (if (or (not (file-writable-p file))
3330 (and (file-exists-p file)
3331 (if (org-called-interactively-p 'any)
3332 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3333 (user-error "Cannot write agenda to file %s" file))
3334 (org-let (if nosettings nil org-agenda-exporter-settings)
3335 '(save-excursion
3336 (save-window-excursion
3337 (let ((bs (copy-sequence (buffer-string))) beg content)
3338 (with-temp-buffer
3339 (rename-buffer org-agenda-write-buffer-name t)
3340 (set-buffer-modified-p nil)
3341 (insert bs)
3342 (org-agenda-remove-marked-text 'org-filtered)
3343 (run-hooks 'org-agenda-before-write-hook)
3344 (cond
3345 ((org-bound-and-true-p org-mobile-creating-agendas)
3346 (org-mobile-write-agenda-for-mobile file))
3347 ((string-match "\\.org\\'" file)
3348 (let (content p m message-log-max)
3349 (goto-char (point-min))
3350 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3351 (goto-char p)
3352 (setq m (get-text-property (point) 'org-hd-marker))
3353 (when m
3354 (push (save-excursion
3355 (set-buffer (marker-buffer m))
3356 (goto-char m)
3357 (org-copy-subtree 1 nil t t)
3358 org-subtree-clip)
3359 content)))
3360 (find-file file)
3361 (erase-buffer)
3362 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3363 (write-file file)
3364 (kill-buffer (current-buffer))
3365 (message "Org file written to %s" file)))
3366 ((string-match "\\.html?\\'" file)
3367 (require 'htmlize)
3368 (set-buffer (htmlize-buffer (current-buffer)))
3369 (when org-agenda-export-html-style
3370 ;; replace <style> section with org-agenda-export-html-style
3371 (goto-char (point-min))
3372 (kill-region (- (search-forward "<style") 6)
3373 (search-forward "</style>"))
3374 (insert org-agenda-export-html-style))
3375 (write-file file)
3376 (kill-buffer (current-buffer))
3377 (message "HTML written to %s" file))
3378 ((string-match "\\.ps\\'" file)
3379 (require 'ps-print)
3380 (ps-print-buffer-with-faces file)
3381 (message "Postscript written to %s" file))
3382 ((string-match "\\.pdf\\'" file)
3383 (require 'ps-print)
3384 (ps-print-buffer-with-faces
3385 (concat (file-name-sans-extension file) ".ps"))
3386 (call-process "ps2pdf" nil nil nil
3387 (expand-file-name
3388 (concat (file-name-sans-extension file) ".ps"))
3389 (expand-file-name file))
3390 (delete-file (concat (file-name-sans-extension file) ".ps"))
3391 (message "PDF written to %s" file))
3392 ((string-match "\\.ics\\'" file)
3393 (require 'ox-icalendar)
3394 (org-icalendar-export-current-agenda (expand-file-name file)))
3396 (let ((bs (buffer-string)))
3397 (find-file file)
3398 (erase-buffer)
3399 (insert bs)
3400 (save-buffer 0)
3401 (kill-buffer (current-buffer))
3402 (message "Plain text written to %s" file))))))))
3403 (set-buffer (or agenda-bufname
3404 (and (org-called-interactively-p 'any) (buffer-name))
3405 org-agenda-buffer-name)))
3406 (when open (org-open-file file)))
3408 (defun org-agenda-remove-marked-text (property &optional value)
3409 "Delete all text marked with VALUE of PROPERTY.
3410 VALUE defaults to t."
3411 (let (beg)
3412 (setq value (or value t))
3413 (while (setq beg (text-property-any (point-min) (point-max)
3414 property value))
3415 (delete-region
3416 beg (or (next-single-property-change beg property)
3417 (point-max))))))
3419 (defun org-agenda-add-entry-text ()
3420 "Add entry text to agenda lines.
3421 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3422 entry text following headings shown in the agenda.
3423 Drawers will be excluded, also the line with scheduling/deadline info."
3424 (when (and (> org-agenda-add-entry-text-maxlines 0)
3425 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3426 (let (m txt)
3427 (goto-char (point-min))
3428 (while (not (eobp))
3429 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3430 (beginning-of-line 2)
3431 (setq txt (org-agenda-get-some-entry-text
3432 m org-agenda-add-entry-text-maxlines " > "))
3433 (end-of-line 1)
3434 (if (string-match "\\S-" txt)
3435 (insert "\n" txt)
3436 (or (eobp) (forward-char 1))))))))
3438 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3439 &rest keep)
3440 "Extract entry text from MARKER, at most N-LINES lines.
3441 This will ignore drawers etc, just get the text.
3442 If INDENT is given, prefix every line with this string. If KEEP is
3443 given, it is a list of symbols, defining stuff that should not be
3444 removed from the entry content. Currently only `planning' is allowed here."
3445 (let (txt drawer-re kwd-time-re ind)
3446 (save-excursion
3447 (with-current-buffer (marker-buffer marker)
3448 (if (not (derived-mode-p 'org-mode))
3449 (setq txt "")
3450 (save-excursion
3451 (save-restriction
3452 (widen)
3453 (goto-char marker)
3454 (end-of-line 1)
3455 (setq txt (buffer-substring
3456 (min (1+ (point)) (point-max))
3457 (progn (outline-next-heading) (point)))
3458 drawer-re org-drawer-regexp
3459 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3460 ".*\n?"))
3461 (with-temp-buffer
3462 (insert txt)
3463 (when org-agenda-add-entry-text-descriptive-links
3464 (goto-char (point-min))
3465 (while (org-activate-bracket-links (point-max))
3466 (add-text-properties (match-beginning 0) (match-end 0)
3467 '(face org-link))))
3468 (goto-char (point-min))
3469 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3470 (set-text-properties (match-beginning 0) (match-end 0)
3471 nil))
3472 (goto-char (point-min))
3473 (while (re-search-forward drawer-re nil t)
3474 (delete-region
3475 (match-beginning 0)
3476 (progn (re-search-forward
3477 "^[ \t]*:END:.*\n?" nil 'move)
3478 (point))))
3479 (unless (member 'planning keep)
3480 (goto-char (point-min))
3481 (while (re-search-forward kwd-time-re nil t)
3482 (replace-match "")))
3483 (goto-char (point-min))
3484 (when org-agenda-entry-text-exclude-regexps
3485 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3486 (while (setq re (pop re-list))
3487 (goto-char (point-min))
3488 (while (re-search-forward re nil t)
3489 (replace-match "")))))
3490 (goto-char (point-max))
3491 (skip-chars-backward " \t\n")
3492 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3494 ;; find and remove min common indentation
3495 (goto-char (point-min))
3496 (untabify (point-min) (point-max))
3497 (setq ind (org-get-indentation))
3498 (while (not (eobp))
3499 (unless (looking-at "[ \t]*$")
3500 (setq ind (min ind (org-get-indentation))))
3501 (beginning-of-line 2))
3502 (goto-char (point-min))
3503 (while (not (eobp))
3504 (unless (looking-at "[ \t]*$")
3505 (move-to-column ind)
3506 (delete-region (point-at-bol) (point)))
3507 (beginning-of-line 2))
3509 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3511 (goto-char (point-min))
3512 (when indent
3513 (while (and (not (eobp)) (re-search-forward "^" nil t))
3514 (replace-match indent t t)))
3515 (goto-char (point-min))
3516 (while (looking-at "[ \t]*\n") (replace-match ""))
3517 (goto-char (point-max))
3518 (when (> (org-current-line)
3519 n-lines)
3520 (org-goto-line (1+ n-lines))
3521 (backward-char 1))
3522 (setq txt (buffer-substring (point-min) (point)))))))))
3523 txt))
3525 (defun org-check-for-org-mode ()
3526 "Make sure current buffer is in org-mode. Error if not."
3527 (or (derived-mode-p 'org-mode)
3528 (error "Cannot execute org-mode agenda command on buffer in %s"
3529 major-mode)))
3531 ;;; Agenda prepare and finalize
3533 (defvar org-agenda-multi nil) ; dynamically scoped
3534 (defvar org-agenda-pre-window-conf nil)
3535 (defvar org-agenda-columns-active nil)
3536 (defvar org-agenda-name nil)
3537 (defvar org-agenda-tag-filter nil)
3538 (defvar org-agenda-category-filter nil)
3539 (defvar org-agenda-regexp-filter nil)
3540 (defvar org-agenda-effort-filter nil)
3541 (defvar org-agenda-top-headline-filter nil)
3542 (defvar org-agenda-tag-filter-while-redo nil)
3543 (defvar org-agenda-tag-filter-preset nil
3544 "A preset of the tags filter used for secondary agenda filtering.
3545 This must be a list of strings, each string must be a single tag preceded
3546 by \"+\" or \"-\".
3547 This variable should not be set directly, but agenda custom commands can
3548 bind it in the options section. The preset filter is a global property of
3549 the entire agenda view. In a block agenda, it will not work reliably to
3550 define a filter for one of the individual blocks. You need to set it in
3551 the global options and expect it to be applied to the entire view.")
3553 (defvar org-agenda-category-filter-preset nil
3554 "A preset of the category filter used for secondary agenda filtering.
3555 This must be a list of strings, each string must be a single category
3556 preceded by \"+\" or \"-\".
3557 This variable should not be set directly, but agenda custom commands can
3558 bind it in the options section. The preset filter is a global property of
3559 the entire agenda view. In a block agenda, it will not work reliably to
3560 define a filter for one of the individual blocks. You need to set it in
3561 the global options and expect it to be applied to the entire view.")
3563 (defvar org-agenda-regexp-filter-preset nil
3564 "A preset of the regexp filter used for secondary agenda filtering.
3565 This must be a list of strings, each string must be a single regexp
3566 preceded by \"+\" or \"-\".
3567 This variable should not be set directly, but agenda custom commands can
3568 bind it in the options section. The preset filter is a global property of
3569 the entire agenda view. In a block agenda, it will not work reliably to
3570 define a filter for one of the individual blocks. You need to set it in
3571 the global options and expect it to be applied to the entire view.")
3573 (defvar org-agenda-effort-filter-preset nil
3574 "A preset of the effort condition used for secondary agenda filtering.
3575 This must be a list of strings, each string must be a single regexp
3576 preceded by \"+\" or \"-\".
3577 This variable should not be set directly, but agenda custom commands can
3578 bind it in the options section. The preset filter is a global property of
3579 the entire agenda view. In a block agenda, it will not work reliably to
3580 define a filter for one of the individual blocks. You need to set it in
3581 the global options and expect it to be applied to the entire view.")
3583 (defun org-agenda-use-sticky-p ()
3584 "Return non-nil if an agenda buffer named
3585 `org-agenda-buffer-name' exists and should be shown instead of
3586 generating a new one."
3587 (and
3588 ;; turned off by user
3589 org-agenda-sticky
3590 ;; For multi-agenda buffer already exists
3591 (not org-agenda-multi)
3592 ;; buffer found
3593 (get-buffer org-agenda-buffer-name)
3594 ;; C-u parameter is same as last call
3595 (with-current-buffer (get-buffer org-agenda-buffer-name)
3596 (and
3597 (equal current-prefix-arg
3598 org-agenda-last-prefix-arg)
3599 ;; In case user turned stickiness on, while having existing
3600 ;; Agenda buffer active, don't reuse that buffer, because it
3601 ;; does not have org variables local
3602 org-agenda-this-buffer-is-sticky))))
3604 (defun org-agenda-prepare-window (abuf filter-alist)
3605 "Setup agenda buffer in the window.
3606 ABUF is the buffer for the agenda window.
3607 FILTER-ALIST is an alist of filters we need to apply when
3608 `org-agenda-persistent-filter' is non-nil."
3609 (let* ((awin (get-buffer-window abuf)) wconf)
3610 (cond
3611 ((equal (current-buffer) abuf) nil)
3612 (awin (select-window awin))
3613 ((not (setq wconf (current-window-configuration))))
3614 ((equal org-agenda-window-setup 'current-window)
3615 (org-pop-to-buffer-same-window abuf))
3616 ((equal org-agenda-window-setup 'other-window)
3617 (org-switch-to-buffer-other-window abuf))
3618 ((equal org-agenda-window-setup 'other-frame)
3619 (switch-to-buffer-other-frame abuf))
3620 ((equal org-agenda-window-setup 'reorganize-frame)
3621 (delete-other-windows)
3622 (org-switch-to-buffer-other-window abuf)))
3623 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3624 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3625 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3626 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3627 ;; Additional test in case agenda is invoked from within agenda
3628 ;; buffer via elisp link.
3629 (unless (equal (current-buffer) abuf)
3630 (org-pop-to-buffer-same-window abuf))
3631 (setq org-agenda-pre-window-conf
3632 (or org-agenda-pre-window-conf wconf))))
3634 (defun org-agenda-prepare (&optional name)
3635 (let ((filter-alist (if org-agenda-persistent-filter
3636 (list `(tag . ,org-agenda-tag-filter)
3637 `(re . ,org-agenda-regexp-filter)
3638 `(effort . ,org-agenda-effort-filter)
3639 `(car . ,org-agenda-category-filter)))))
3640 (if (org-agenda-use-sticky-p)
3641 (progn
3642 ;; Popup existing buffer
3643 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3644 filter-alist)
3645 (message "Sticky Agenda buffer, use `r' to refresh")
3646 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3647 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3648 (setq org-todo-keywords-for-agenda nil)
3649 (put 'org-agenda-tag-filter :preset-filter
3650 org-agenda-tag-filter-preset)
3651 (put 'org-agenda-category-filter :preset-filter
3652 org-agenda-category-filter-preset)
3653 (put 'org-agenda-regexp-filter :preset-filter
3654 org-agenda-regexp-filter-preset)
3655 (put 'org-agenda-effort-filter :preset-filter
3656 org-agenda-effort-filter-preset)
3657 (if org-agenda-multi
3658 (progn
3659 (setq buffer-read-only nil)
3660 (goto-char (point-max))
3661 (unless (or (bobp) org-agenda-compact-blocks
3662 (not org-agenda-block-separator))
3663 (insert "\n"
3664 (if (stringp org-agenda-block-separator)
3665 org-agenda-block-separator
3666 (make-string (window-width) org-agenda-block-separator))
3667 "\n"))
3668 (narrow-to-region (point) (point-max)))
3669 (setq org-done-keywords-for-agenda nil)
3670 ;; Setting any org variables that are in org-agenda-local-vars
3671 ;; list need to be done after the prepare call
3672 (org-agenda-prepare-window
3673 (get-buffer-create org-agenda-buffer-name) filter-alist)
3674 (setq buffer-read-only nil)
3675 (org-agenda-reset-markers)
3676 (let ((inhibit-read-only t)) (erase-buffer))
3677 (org-agenda-mode)
3678 (setq org-agenda-buffer (current-buffer))
3679 (setq org-agenda-contributing-files nil)
3680 (setq org-agenda-columns-active nil)
3681 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3682 (setq org-todo-keywords-for-agenda
3683 (org-uniquify org-todo-keywords-for-agenda))
3684 (setq org-done-keywords-for-agenda
3685 (org-uniquify org-done-keywords-for-agenda))
3686 (setq org-agenda-last-prefix-arg current-prefix-arg)
3687 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3688 (and name (not org-agenda-name)
3689 (org-set-local 'org-agenda-name name)))
3690 (setq buffer-read-only nil))))
3692 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3693 (defun org-agenda-finalize ()
3694 "Finishing touch for the agenda buffer, called just before displaying it."
3695 (unless org-agenda-multi
3696 (save-excursion
3697 (let ((inhibit-read-only t))
3698 (goto-char (point-min))
3699 (save-excursion
3700 (while (org-activate-bracket-links (point-max))
3701 (add-text-properties (match-beginning 0) (match-end 0)
3702 '(face org-link))))
3703 (save-excursion
3704 (while (org-activate-plain-links (point-max))
3705 (add-text-properties (match-beginning 0) (match-end 0)
3706 '(face org-link))))
3707 (unless (eq org-agenda-remove-tags t)
3708 (org-agenda-align-tags))
3709 (unless org-agenda-with-colors
3710 (remove-text-properties (point-min) (point-max) '(face nil)))
3711 (if (and (boundp 'org-agenda-overriding-columns-format)
3712 org-agenda-overriding-columns-format)
3713 (org-set-local 'org-agenda-overriding-columns-format
3714 org-agenda-overriding-columns-format))
3715 (if (and (boundp 'org-agenda-view-columns-initially)
3716 org-agenda-view-columns-initially)
3717 (org-agenda-columns))
3718 (when org-agenda-fontify-priorities
3719 (org-agenda-fontify-priorities))
3720 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3721 (org-agenda-dim-blocked-tasks))
3722 (org-agenda-mark-clocking-task)
3723 (when org-agenda-entry-text-mode
3724 (org-agenda-entry-text-hide)
3725 (org-agenda-entry-text-show))
3726 (if (and (functionp 'org-habit-insert-consistency-graphs)
3727 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3728 (org-habit-insert-consistency-graphs))
3729 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3730 (unless (or (eq org-agenda-show-inherited-tags 'always)
3731 (and (listp org-agenda-show-inherited-tags)
3732 (memq org-agenda-type org-agenda-show-inherited-tags))
3733 (and (eq org-agenda-show-inherited-tags t)
3734 (or (eq org-agenda-use-tag-inheritance t)
3735 (and (listp org-agenda-use-tag-inheritance)
3736 (not (memq org-agenda-type
3737 org-agenda-use-tag-inheritance))))))
3738 (let (mrk)
3739 (save-excursion
3740 (goto-char (point-min))
3741 (while (equal (forward-line) 0)
3742 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3743 (put-text-property (point-at-bol) (point-at-eol)
3744 'tags (org-with-point-at mrk
3745 (delete-dups
3746 (mapcar 'downcase (org-get-tags-at))))))))))
3747 (run-hooks 'org-agenda-finalize-hook)
3748 (when org-agenda-top-headline-filter
3749 (org-agenda-filter-top-headline-apply
3750 org-agenda-top-headline-filter))
3751 (when org-agenda-tag-filter
3752 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3753 (when (get 'org-agenda-tag-filter :preset-filter)
3754 (org-agenda-filter-apply
3755 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3756 (when org-agenda-category-filter
3757 (org-agenda-filter-apply org-agenda-category-filter 'category))
3758 (when (get 'org-agenda-category-filter :preset-filter)
3759 (org-agenda-filter-apply
3760 (get 'org-agenda-category-filter :preset-filter) 'category))
3761 (when org-agenda-regexp-filter
3762 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3763 (when (get 'org-agenda-regexp-filter :preset-filter)
3764 (org-agenda-filter-apply
3765 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3766 (when org-agenda-effort-filter
3767 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3768 (when (get 'org-agenda-effort-filter :preset-filter)
3769 (org-agenda-filter-apply
3770 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3771 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3773 (defun org-agenda-mark-clocking-task ()
3774 "Mark the current clock entry in the agenda if it is present."
3775 ;; We need to widen when `org-agenda-finalize' is called from
3776 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3777 (when org-clock-current-task
3778 (save-restriction
3779 (widen)
3780 (org-agenda-unmark-clocking-task)
3781 (when (marker-buffer org-clock-hd-marker)
3782 (save-excursion
3783 (goto-char (point-min))
3784 (let (s ov)
3785 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3786 (goto-char s)
3787 (when (equal (org-get-at-bol 'org-hd-marker)
3788 org-clock-hd-marker)
3789 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3790 (overlay-put ov 'type 'org-agenda-clocking)
3791 (overlay-put ov 'face 'org-agenda-clocking)
3792 (overlay-put ov 'help-echo
3793 "The clock is running in this item")))))))))
3795 (defun org-agenda-unmark-clocking-task ()
3796 "Unmark the current clocking task."
3797 (mapc (lambda (o)
3798 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3799 (delete-overlay o)))
3800 (overlays-in (point-min) (point-max))))
3802 (defun org-agenda-fontify-priorities ()
3803 "Make highest priority lines bold, and lowest italic."
3804 (interactive)
3805 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3806 (delete-overlay o)))
3807 (overlays-in (point-min) (point-max)))
3808 (save-excursion
3809 (let (b e p ov h l)
3810 (goto-char (point-min))
3811 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3812 (setq h (or (get-char-property (point) 'org-highest-priority)
3813 org-highest-priority)
3814 l (or (get-char-property (point) 'org-lowest-priority)
3815 org-lowest-priority)
3816 p (string-to-char (match-string 1))
3817 b (match-beginning 0)
3818 e (if (eq org-agenda-fontify-priorities 'cookies)
3819 (match-end 0)
3820 (point-at-eol))
3821 ov (make-overlay b e))
3822 (overlay-put
3823 ov 'face
3824 (cons (cond ((org-face-from-face-or-color
3825 'priority nil
3826 (cdr (assoc p org-priority-faces))))
3827 ((and (listp org-agenda-fontify-priorities)
3828 (org-face-from-face-or-color
3829 'priority nil
3830 (cdr (assoc p org-agenda-fontify-priorities)))))
3831 ((equal p l) 'italic)
3832 ((equal p h) 'bold))
3833 'org-priority))
3834 (overlay-put ov 'org-type 'org-priority)))))
3836 (defvar org-depend-tag-blocked)
3838 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3839 "Dim currently blocked TODO's in the agenda display.
3840 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3841 dimming them."
3842 (interactive "P")
3843 (when (org-called-interactively-p 'interactive)
3844 (message "Dim or hide blocked tasks..."))
3845 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3846 (delete-overlay o)))
3847 (overlays-in (point-min) (point-max)))
3848 (save-excursion
3849 (let ((inhibit-read-only t)
3850 (org-depend-tag-blocked nil)
3851 (invis (or (not (null invisible))
3852 (eq org-agenda-dim-blocked-tasks 'invisible)))
3853 org-blocked-by-checkboxes
3854 invis1 b e p ov h l)
3855 (goto-char (point-min))
3856 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3857 (and pos (goto-char (1+ pos))))
3858 (setq org-blocked-by-checkboxes nil invis1 invis)
3859 (let ((marker (org-get-at-bol 'org-hd-marker)))
3860 (when (and marker
3861 (with-current-buffer (marker-buffer marker)
3862 (save-excursion (goto-char marker)
3863 (org-entry-blocked-p))))
3864 (if org-blocked-by-checkboxes (setq invis1 nil))
3865 (setq b (if invis1
3866 (max (point-min) (1- (point-at-bol)))
3867 (point-at-bol))
3868 e (point-at-eol)
3869 ov (make-overlay b e))
3870 (if invis1
3871 (progn (overlay-put ov 'invisible t)
3872 (overlay-put ov 'intangible t))
3873 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3874 (overlay-put ov 'org-type 'org-blocked-todo))))))
3875 (when (org-called-interactively-p 'interactive)
3876 (message "Dim or hide blocked tasks...done")))
3878 (defvar org-agenda-skip-function nil
3879 "Function to be called at each match during agenda construction.
3880 If this function returns nil, the current match should not be skipped.
3881 Otherwise, the function must return a position from where the search
3882 should be continued.
3883 This may also be a Lisp form, it will be evaluated.
3884 Never set this variable using `setq' or so, because then it will apply
3885 to all future agenda commands. If you do want a global skipping condition,
3886 use the option `org-agenda-skip-function-global' instead.
3887 The correct usage for `org-agenda-skip-function' is to bind it with
3888 `let' to scope it dynamically into the agenda-constructing command.
3889 A good way to set it is through options in `org-agenda-custom-commands'.")
3891 (defun org-agenda-skip ()
3892 "Throw to `:skip' in places that should be skipped.
3893 Also moves point to the end of the skipped region, so that search can
3894 continue from there."
3895 (let ((p (point-at-bol)) to)
3896 (when (or
3897 (save-excursion (goto-char p) (looking-at comment-start-skip))
3898 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3899 (get-text-property p :org-archived)
3900 (org-end-of-subtree t))
3901 (and org-agenda-skip-comment-trees
3902 (get-text-property p :org-comment)
3903 (org-end-of-subtree t))
3904 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3905 (org-agenda-skip-eval org-agenda-skip-function)))
3906 (goto-char to))
3907 (org-in-src-block-p t))
3908 (throw :skip t))))
3910 (defun org-agenda-skip-eval (form)
3911 "If FORM is a function or a list, call (or eval) it and return the result.
3912 `save-excursion' and `save-match-data' are wrapped around the call, so point
3913 and match data are returned to the previous state no matter what these
3914 functions do."
3915 (let (fp)
3916 (and form
3917 (or (setq fp (functionp form))
3918 (consp form))
3919 (save-excursion
3920 (save-match-data
3921 (if fp
3922 (funcall form)
3923 (eval form)))))))
3925 (defvar org-agenda-markers nil
3926 "List of all currently active markers created by `org-agenda'.")
3927 (defvar org-agenda-last-marker-time (org-float-time)
3928 "Creation time of the last agenda marker.")
3930 (defun org-agenda-new-marker (&optional pos)
3931 "Return a new agenda marker.
3932 Org-mode keeps a list of these markers and resets them when they are
3933 no longer in use."
3934 (let ((m (copy-marker (or pos (point)))))
3935 (setq org-agenda-last-marker-time (org-float-time))
3936 (if org-agenda-buffer
3937 (with-current-buffer org-agenda-buffer
3938 (push m org-agenda-markers))
3939 (push m org-agenda-markers))
3942 (defun org-agenda-reset-markers ()
3943 "Reset markers created by `org-agenda'."
3944 (while org-agenda-markers
3945 (move-marker (pop org-agenda-markers) nil)))
3947 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3948 "Save relative positions of markers in region.
3949 This check for agenda markers in all agenda buffers currently active."
3950 (dolist (buf (buffer-list))
3951 (with-current-buffer buf
3952 (when (eq major-mode 'org-agenda-mode)
3953 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3954 org-agenda-markers)))))
3956 ;;; Entry text mode
3958 (defun org-agenda-entry-text-show-here ()
3959 "Add some text from the entry as context to the current line."
3960 (let (m txt o)
3961 (setq m (org-get-at-bol 'org-hd-marker))
3962 (unless (marker-buffer m)
3963 (error "No marker points to an entry here"))
3964 (setq txt (concat "\n" (org-no-properties
3965 (org-agenda-get-some-entry-text
3966 m org-agenda-entry-text-maxlines
3967 org-agenda-entry-text-leaders))))
3968 (when (string-match "\\S-" txt)
3969 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3970 (overlay-put o 'evaporate t)
3971 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3972 (overlay-put o 'after-string txt))))
3974 (defun org-agenda-entry-text-show ()
3975 "Add entry context for all agenda lines."
3976 (interactive)
3977 (save-excursion
3978 (goto-char (point-max))
3979 (beginning-of-line 1)
3980 (while (not (bobp))
3981 (when (org-get-at-bol 'org-hd-marker)
3982 (org-agenda-entry-text-show-here))
3983 (beginning-of-line 0))))
3985 (defun org-agenda-entry-text-hide ()
3986 "Remove any shown entry context."
3987 (delq nil
3988 (mapcar (lambda (o)
3989 (if (eq (overlay-get o 'org-overlay-type)
3990 'agenda-entry-content)
3991 (progn (delete-overlay o) t)))
3992 (overlays-in (point-min) (point-max)))))
3994 (defun org-agenda-get-day-face (date)
3995 "Return the face DATE should be displayed with."
3996 (or (and (functionp org-agenda-day-face-function)
3997 (funcall org-agenda-day-face-function date))
3998 (cond ((org-agenda-todayp date)
3999 'org-agenda-date-today)
4000 ((member (calendar-day-of-week date) org-agenda-weekend-days)
4001 'org-agenda-date-weekend)
4002 (t 'org-agenda-date))))
4004 ;;; Agenda timeline
4006 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4007 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4009 (defun org-timeline (&optional dotodo)
4010 "Show a time-sorted view of the entries in the current org file.
4011 Only entries with a time stamp of today or later will be listed. With
4012 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4013 under the current date.
4014 If the buffer contains an active region, only check the region for
4015 dates."
4016 (interactive "P")
4017 (let* ((dopast t)
4018 (org-agenda-show-log-scoped org-agenda-show-log)
4019 (org-agenda-show-log org-agenda-show-log-scoped)
4020 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4021 (current-buffer))))
4022 (date (calendar-current-date))
4023 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4024 (end (if (org-region-active-p) (region-end) (point-max)))
4025 (day-numbers (org-get-all-dates
4026 beg end 'no-ranges
4027 t org-agenda-show-log-scoped ; always include today
4028 org-timeline-show-empty-dates))
4029 (org-deadline-warning-days 0)
4030 (org-agenda-only-exact-dates t)
4031 (today (org-today))
4032 (past t)
4033 args
4034 s e rtn d emptyp)
4035 (setq org-agenda-redo-command
4036 (list 'let
4037 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4038 (list 'org-switch-to-buffer-other-window (current-buffer))
4039 (list 'org-timeline (list 'quote dotodo))))
4040 (put 'org-agenda-redo-command 'org-lprops nil)
4041 (if (not dopast)
4042 ;; Remove past dates from the list of dates.
4043 (setq day-numbers (delq nil (mapcar (lambda(x)
4044 (if (>= x today) x nil))
4045 day-numbers))))
4046 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4047 (org-compile-prefix-format 'timeline)
4048 (org-set-sorting-strategy 'timeline)
4049 (if org-agenda-show-log-scoped (push :closed args))
4050 (push :timestamp args)
4051 (push :deadline args)
4052 (push :scheduled args)
4053 (push :sexp args)
4054 (if dotodo (push :todo args))
4055 (insert "Timeline of file " entry "\n")
4056 (add-text-properties (point-min) (point)
4057 (list 'face 'org-agenda-structure))
4058 (org-agenda-mark-header-line (point-min))
4059 (while (setq d (pop day-numbers))
4060 (if (and (listp d) (eq (car d) :omitted))
4061 (progn
4062 (setq s (point))
4063 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4064 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4065 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4066 (if (and (>= d today)
4067 dopast
4068 past)
4069 (progn
4070 (setq past nil)
4071 (insert (make-string 79 ?-) "\n")))
4072 (setq date (calendar-gregorian-from-absolute d))
4073 (setq s (point))
4074 (setq rtn (and (not emptyp)
4075 (apply 'org-agenda-get-day-entries entry
4076 date args)))
4077 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4078 (progn
4079 (insert
4080 (if (stringp org-agenda-format-date)
4081 (format-time-string org-agenda-format-date
4082 (org-time-from-absolute date))
4083 (funcall org-agenda-format-date date))
4084 "\n")
4085 (put-text-property s (1- (point)) 'face
4086 (org-agenda-get-day-face date))
4087 (put-text-property s (1- (point)) 'org-date-line t)
4088 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4089 (if (equal d today)
4090 (put-text-property s (1- (point)) 'org-today t))
4091 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4092 (put-text-property s (1- (point)) 'day d)))))
4093 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4094 (point-min)))
4095 (add-text-properties
4096 (point-min) (point-max)
4097 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4098 (org-agenda-finalize)
4099 (setq buffer-read-only t)))
4101 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4102 "Return a list of all relevant day numbers from BEG to END buffer positions.
4103 If NO-RANGES is non-nil, include only the start and end dates of a range,
4104 not every single day in the range. If FORCE-TODAY is non-nil, make
4105 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4106 inactive time stamps (those in square brackets) are included.
4107 When EMPTY is non-nil, also include days without any entries."
4108 (let ((re (concat
4109 (if pre-re pre-re "")
4110 (if inactive org-ts-regexp-both org-ts-regexp)))
4111 dates dates1 date day day1 day2 ts1 ts2 pos)
4112 (if force-today
4113 (setq dates (list (org-today))))
4114 (save-excursion
4115 (goto-char beg)
4116 (while (re-search-forward re end t)
4117 (setq day (time-to-days (org-time-string-to-time
4118 (substring (match-string 1) 0 10)
4119 (current-buffer) (match-beginning 0))))
4120 (or (memq day dates) (push day dates)))
4121 (unless no-ranges
4122 (goto-char beg)
4123 (while (re-search-forward org-tr-regexp end t)
4124 (setq pos (match-beginning 0))
4125 (setq ts1 (substring (match-string 1) 0 10)
4126 ts2 (substring (match-string 2) 0 10)
4127 day1 (time-to-days (org-time-string-to-time
4128 ts1 (current-buffer) pos))
4129 day2 (time-to-days (org-time-string-to-time
4130 ts2 (current-buffer) pos)))
4131 (while (< (setq day1 (1+ day1)) day2)
4132 (or (memq day1 dates) (push day1 dates)))))
4133 (setq dates (sort dates '<))
4134 (when empty
4135 (while (setq day (pop dates))
4136 (setq day2 (car dates))
4137 (push day dates1)
4138 (when (and day2 empty)
4139 (if (or (eq empty t)
4140 (and (numberp empty) (<= (- day2 day) empty)))
4141 (while (< (setq day (1+ day)) day2)
4142 (push (list day) dates1))
4143 (push (cons :omitted (- day2 day)) dates1))))
4144 (setq dates (nreverse dates1)))
4145 dates)))
4147 ;;; Agenda Daily/Weekly
4149 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4150 "Start day for the agenda view.
4151 Custom commands can set this variable in the options section.
4152 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4153 input allowed when reading a date through the Org calendar.
4154 See the docstring of `org-read-date' for details.")
4155 (defvar org-starting-day nil) ; local variable in the agenda buffer
4156 (defvar org-arg-loc nil) ; local variable
4158 (defvar org-agenda-buffer-tmp-name nil)
4159 ;;;###autoload
4160 (defun org-agenda-list (&optional arg start-day span with-hour)
4161 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4162 The view will be for the current day or week, but from the overview buffer
4163 you will be able to go to other days/weeks.
4165 With a numeric prefix argument in an interactive call, the agenda will
4166 span ARG days. Lisp programs should instead specify SPAN to change
4167 the number of days. SPAN defaults to `org-agenda-span'.
4169 START-DAY defaults to TODAY, or to the most recent match for the weekday
4170 given in `org-agenda-start-on-weekday'.
4172 When WITH-HOUR is non-nil, only include scheduled and deadline
4173 items if they have an hour specification like [h]h:mm."
4174 (interactive "P")
4175 (if org-agenda-overriding-arguments
4176 (setq arg (car org-agenda-overriding-arguments)
4177 start-day (nth 1 org-agenda-overriding-arguments)
4178 span (nth 2 org-agenda-overriding-arguments)))
4179 (if (and (integerp arg) (> arg 0))
4180 (setq span arg arg nil))
4181 (catch 'exit
4182 (setq org-agenda-buffer-name
4183 (or org-agenda-buffer-tmp-name
4184 (if org-agenda-sticky
4185 (cond ((and org-keys (stringp org-match))
4186 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4187 (org-keys
4188 (format "*Org Agenda(%s)*" org-keys))
4189 (t "*Org Agenda(a)*")))
4190 org-agenda-buffer-name))
4191 (org-agenda-prepare "Day/Week")
4192 (setq start-day (or start-day org-agenda-start-day))
4193 (if (stringp start-day)
4194 ;; Convert to an absolute day number
4195 (setq start-day (time-to-days (org-read-date nil t start-day))))
4196 (org-compile-prefix-format 'agenda)
4197 (org-set-sorting-strategy 'agenda)
4198 (let* ((span (org-agenda-ndays-to-span
4199 (or span org-agenda-ndays org-agenda-span)))
4200 (today (org-today))
4201 (sd (or start-day today))
4202 (ndays (org-agenda-span-to-ndays span sd))
4203 (org-agenda-start-on-weekday
4204 (if (or (eq ndays 7) (eq ndays 14))
4205 org-agenda-start-on-weekday))
4206 (thefiles (org-agenda-files nil 'ifmode))
4207 (files thefiles)
4208 (start (if (or (null org-agenda-start-on-weekday)
4209 (< ndays 7))
4211 (let* ((nt (calendar-day-of-week
4212 (calendar-gregorian-from-absolute sd)))
4213 (n1 org-agenda-start-on-weekday)
4214 (d (- nt n1)))
4215 (- sd (+ (if (< d 0) 7 0) d)))))
4216 (day-numbers (list start))
4217 (day-cnt 0)
4218 (inhibit-redisplay (not debug-on-error))
4219 (org-agenda-show-log-scoped org-agenda-show-log)
4220 s e rtn rtnall file date d start-pos end-pos todayp
4221 clocktable-start clocktable-end filter)
4222 (setq org-agenda-redo-command
4223 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4224 (dotimes (n (1- ndays))
4225 (push (1+ (car day-numbers)) day-numbers))
4226 (setq day-numbers (nreverse day-numbers))
4227 (setq clocktable-start (car day-numbers)
4228 clocktable-end (1+ (or (org-last day-numbers) 0)))
4229 (org-set-local 'org-starting-day (car day-numbers))
4230 (org-set-local 'org-arg-loc arg)
4231 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4232 (unless org-agenda-compact-blocks
4233 (let* ((d1 (car day-numbers))
4234 (d2 (org-last day-numbers))
4235 (w1 (org-days-to-iso-week d1))
4236 (w2 (org-days-to-iso-week d2)))
4237 (setq s (point))
4238 (if org-agenda-overriding-header
4239 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4240 nil 'face 'org-agenda-structure) "\n")
4241 (insert (org-agenda-span-name span)
4242 "-agenda"
4243 (if (< (- d2 d1) 350)
4244 (if (= w1 w2)
4245 (format " (W%02d)" w1)
4246 (format " (W%02d-W%02d)" w1 w2))
4248 ":\n")))
4249 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4250 'org-date-line t))
4251 (org-agenda-mark-header-line s))
4252 (while (setq d (pop day-numbers))
4253 (setq date (calendar-gregorian-from-absolute d)
4254 s (point))
4255 (if (or (setq todayp (= d today))
4256 (and (not start-pos) (= d sd)))
4257 (setq start-pos (point))
4258 (if (and start-pos (not end-pos))
4259 (setq end-pos (point))))
4260 (setq files thefiles
4261 rtnall nil)
4262 (while (setq file (pop files))
4263 (catch 'nextfile
4264 (org-check-agenda-file file)
4265 (let ((org-agenda-entry-types org-agenda-entry-types))
4266 ;; Starred types override non-starred equivalents
4267 (when (member :deadline* org-agenda-entry-types)
4268 (setq org-agenda-entry-types
4269 (delq :deadline org-agenda-entry-types)))
4270 (when (member :scheduled* org-agenda-entry-types)
4271 (setq org-agenda-entry-types
4272 (delq :scheduled org-agenda-entry-types)))
4273 ;; Honor with-hour
4274 (when with-hour
4275 (when (member :deadline org-agenda-entry-types)
4276 (setq org-agenda-entry-types
4277 (delq :deadline org-agenda-entry-types))
4278 (push :deadline* org-agenda-entry-types))
4279 (when (member :scheduled org-agenda-entry-types)
4280 (setq org-agenda-entry-types
4281 (delq :scheduled org-agenda-entry-types))
4282 (push :scheduled* org-agenda-entry-types)))
4283 (unless org-agenda-include-deadlines
4284 (setq org-agenda-entry-types
4285 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4286 (cond
4287 ((memq org-agenda-show-log-scoped '(only clockcheck))
4288 (setq rtn (org-agenda-get-day-entries
4289 file date :closed)))
4290 (org-agenda-show-log-scoped
4291 (setq rtn (apply 'org-agenda-get-day-entries
4292 file date
4293 (append '(:closed) org-agenda-entry-types))))
4295 (setq rtn (apply 'org-agenda-get-day-entries
4296 file date
4297 org-agenda-entry-types)))))
4298 (setq rtnall (append rtnall rtn)))) ;; all entries
4299 (if org-agenda-include-diary
4300 (let ((org-agenda-search-headline-for-time t))
4301 (require 'diary-lib)
4302 (setq rtn (org-get-entries-from-diary date))
4303 (setq rtnall (append rtnall rtn))))
4304 (if (or rtnall org-agenda-show-all-dates)
4305 (progn
4306 (setq day-cnt (1+ day-cnt))
4307 (insert
4308 (if (stringp org-agenda-format-date)
4309 (format-time-string org-agenda-format-date
4310 (org-time-from-absolute date))
4311 (funcall org-agenda-format-date date))
4312 "\n")
4313 (put-text-property s (1- (point)) 'face
4314 (org-agenda-get-day-face date))
4315 (put-text-property s (1- (point)) 'org-date-line t)
4316 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4317 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4318 (when todayp
4319 (put-text-property s (1- (point)) 'org-today t))
4320 (setq rtnall
4321 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4322 (if rtnall (insert ;; all entries
4323 (org-agenda-finalize-entries rtnall 'agenda)
4324 "\n"))
4325 (put-text-property s (1- (point)) 'day d)
4326 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4327 (when (and org-agenda-clockreport-mode clocktable-start)
4328 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4329 ;; the above line is to ensure the restricted range!
4330 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4331 tbl)
4332 (setq p (org-plist-delete p :block))
4333 (setq p (plist-put p :tstart clocktable-start))
4334 (setq p (plist-put p :tend clocktable-end))
4335 (setq p (plist-put p :scope 'agenda))
4336 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4337 (setq filter (or org-agenda-tag-filter-while-redo
4338 (get 'org-agenda-tag-filter :preset-filter))))
4339 (setq p (plist-put p :tags (mapconcat (lambda (x)
4340 (if (string-match "[<>=]" x)
4343 filter ""))))
4344 (setq tbl (apply 'org-clock-get-clocktable p))
4345 (insert tbl)))
4346 (goto-char (point-min))
4347 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4348 (unless (and (pos-visible-in-window-p (point-min))
4349 (pos-visible-in-window-p (point-max)))
4350 (goto-char (1- (point-max)))
4351 (recenter -1)
4352 (if (not (pos-visible-in-window-p (or start-pos 1)))
4353 (progn
4354 (goto-char (or start-pos 1))
4355 (recenter 1))))
4356 (goto-char (or start-pos 1))
4357 (add-text-properties (point-min) (point-max)
4358 `(org-agenda-type agenda
4359 org-last-args (,arg ,start-day ,span)
4360 org-redo-cmd ,org-agenda-redo-command
4361 org-series-cmd ,org-cmd))
4362 (if (eq org-agenda-show-log-scoped 'clockcheck)
4363 (org-agenda-show-clocking-issues))
4364 (org-agenda-finalize)
4365 (setq buffer-read-only t)
4366 (message ""))))
4368 (defun org-agenda-ndays-to-span (n)
4369 "Return a span symbol for a span of N days, or N if none matches."
4370 (cond ((symbolp n) n)
4371 ((= n 1) 'day)
4372 ((= n 7) 'week)
4373 ((= n 14) 'fortnight)
4374 (t n)))
4376 (defun org-agenda-span-to-ndays (span &optional start-day)
4377 "Return ndays from SPAN, possibly starting at START-DAY.
4378 START-DAY is an absolute time value."
4379 (cond ((numberp span) span)
4380 ((eq span 'day) 1)
4381 ((eq span 'week) 7)
4382 ((eq span 'fortnight) 14)
4383 ((eq span 'month)
4384 (let ((date (calendar-gregorian-from-absolute start-day)))
4385 (calendar-last-day-of-month (car date) (caddr date))))
4386 ((eq span 'year)
4387 (let ((date (calendar-gregorian-from-absolute start-day)))
4388 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4390 (defun org-agenda-span-name (span)
4391 "Return a SPAN name."
4392 (if (null span)
4394 (if (symbolp span)
4395 (capitalize (symbol-name span))
4396 (format "%d days" span))))
4398 ;;; Agenda word search
4400 (defvar org-agenda-search-history nil)
4402 (defvar org-search-syntax-table nil
4403 "Special syntax table for org-mode search.
4404 In this table, we have single quotes not as word constituents, to
4405 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4407 (defvar org-mode-syntax-table) ; From org.el
4408 (defun org-search-syntax-table ()
4409 (unless org-search-syntax-table
4410 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4411 (modify-syntax-entry ?' "." org-search-syntax-table)
4412 (modify-syntax-entry ?` "." org-search-syntax-table))
4413 org-search-syntax-table)
4415 (defvar org-agenda-last-search-view-search-was-boolean nil)
4417 ;;;###autoload
4418 (defun org-search-view (&optional todo-only string edit-at)
4419 "Show all entries that contain a phrase or words or regular expressions.
4421 With optional prefix argument TODO-ONLY, only consider entries that are
4422 TODO entries. The argument STRING can be used to pass a default search
4423 string into this function. If EDIT-AT is non-nil, it means that the
4424 user should get a chance to edit this string, with cursor at position
4425 EDIT-AT.
4427 The search string can be viewed either as a phrase that should be found as
4428 is, or it can be broken into a number of snippets, each of which must match
4429 in a Boolean way to select an entry. The default depends on the variable
4430 `org-agenda-search-view-always-boolean'.
4431 Even if this is turned off (the default) you can always switch to
4432 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4434 The default is a direct search of the whole phrase, where each space in
4435 the search string can expand to an arbitrary amount of whitespace,
4436 including newlines.
4438 If using a Boolean search, the search string is split on whitespace and
4439 each snippet is searched separately, with logical AND to select an entry.
4440 Words prefixed with a minus must *not* occur in the entry. Words without
4441 a prefix or prefixed with a plus must occur in the entry. Matching is
4442 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4443 match whole words, not parts of a word) if
4444 `org-agenda-search-view-force-full-words' is set (default is nil).
4446 Boolean search snippets enclosed by curly braces are interpreted as
4447 regular expressions that must or (when preceded with \"-\") must not
4448 match in the entry. Snippets enclosed into double quotes will be taken
4449 as a whole, to include whitespace.
4451 - If the search string starts with an asterisk, search only in headlines.
4452 - If (possibly after the leading star) the search string starts with an
4453 exclamation mark, this also means to look at TODO entries only, an effect
4454 that can also be achieved with a prefix argument.
4455 - If (possibly after star and exclamation mark) the search string starts
4456 with a colon, this will mean that the (non-regexp) snippets of the
4457 Boolean search must match as full words.
4459 This command searches the agenda files, and in addition the files listed
4460 in `org-agenda-text-search-extra-files'."
4461 (interactive "P")
4462 (if org-agenda-overriding-arguments
4463 (setq todo-only (car org-agenda-overriding-arguments)
4464 string (nth 1 org-agenda-overriding-arguments)
4465 edit-at (nth 2 org-agenda-overriding-arguments)))
4466 (let* ((props (list 'face nil
4467 'done-face 'org-agenda-done
4468 'org-not-done-regexp org-not-done-regexp
4469 'org-todo-regexp org-todo-regexp
4470 'org-complex-heading-regexp org-complex-heading-regexp
4471 'mouse-face 'highlight
4472 'help-echo (format "mouse-2 or RET jump to location")))
4473 (full-words org-agenda-search-view-force-full-words)
4474 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4475 regexp rtn rtnall files file pos inherited-tags
4476 marker category level tags c neg re boolean
4477 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4478 (unless (and (not edit-at)
4479 (stringp string)
4480 (string-match "\\S-" string))
4481 (setq string (read-string
4482 (if org-agenda-search-view-always-boolean
4483 "[+-]Word/{Regexp} ...: "
4484 "Phrase or [+-]Word/{Regexp} ...: ")
4485 (cond
4486 ((integerp edit-at) (cons string edit-at))
4487 (edit-at string))
4488 'org-agenda-search-history)))
4489 (catch 'exit
4490 (if org-agenda-sticky
4491 (setq org-agenda-buffer-name
4492 (if (stringp string)
4493 (format "*Org Agenda(%s:%s)*"
4494 (or org-keys (or (and todo-only "S") "s")) string)
4495 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4496 (org-agenda-prepare "SEARCH")
4497 (org-compile-prefix-format 'search)
4498 (org-set-sorting-strategy 'search)
4499 (setq org-agenda-redo-command
4500 (list 'org-search-view (if todo-only t nil)
4501 (list 'if 'current-prefix-arg nil string)))
4502 (setq org-agenda-query-string string)
4503 (if (equal (string-to-char string) ?*)
4504 (setq hdl-only t
4505 words (substring string 1))
4506 (setq words string))
4507 (when (equal (string-to-char words) ?!)
4508 (setq todo-only t
4509 words (substring words 1)))
4510 (when (equal (string-to-char words) ?:)
4511 (setq full-words t
4512 words (substring words 1)))
4513 (if (or org-agenda-search-view-always-boolean
4514 (member (string-to-char words) '(?- ?+ ?\{)))
4515 (setq boolean t))
4516 (setq words (org-split-string words))
4517 (let (www w)
4518 (while (setq w (pop words))
4519 (while (and (string-match "\\\\\\'" w) words)
4520 (setq w (concat (substring w 0 -1) " " (pop words))))
4521 (push w www))
4522 (setq words (nreverse www) www nil)
4523 (while (setq w (pop words))
4524 (when (and (string-match "\\`[-+]?{" w)
4525 (not (string-match "}\\'" w)))
4526 (while (and words (not (string-match "}\\'" (car words))))
4527 (setq w (concat w " " (pop words))))
4528 (setq w (concat w " " (pop words))))
4529 (push w www))
4530 (setq words (nreverse www)))
4531 (setq org-agenda-last-search-view-search-was-boolean boolean)
4532 (when boolean
4533 (let (wds w)
4534 (while (setq w (pop words))
4535 (if (or (equal (substring w 0 1) "\"")
4536 (and (> (length w) 1)
4537 (member (substring w 0 1) '("+" "-"))
4538 (equal (substring w 1 2) "\"")))
4539 (while (and words (not (equal (substring w -1) "\"")))
4540 (setq w (concat w " " (pop words)))))
4541 (and (string-match "\\`\\([-+]?\\)\"" w)
4542 (setq w (replace-match "\\1" nil nil w)))
4543 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4544 (push w wds))
4545 (setq words (nreverse wds))))
4546 (if boolean
4547 (mapc (lambda (w)
4548 (setq c (string-to-char w))
4549 (if (equal c ?-)
4550 (setq neg t w (substring w 1))
4551 (if (equal c ?+)
4552 (setq neg nil w (substring w 1))
4553 (setq neg nil)))
4554 (if (string-match "\\`{.*}\\'" w)
4555 (setq re (substring w 1 -1))
4556 (if full-words
4557 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4558 (setq re (regexp-quote (downcase w)))))
4559 (if neg (push re regexps-) (push re regexps+)))
4560 words)
4561 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4562 regexps+))
4563 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4564 (if (not regexps+)
4565 (setq regexp org-outline-regexp-bol)
4566 (setq regexp (pop regexps+))
4567 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4568 regexp))))
4569 (setq files (org-agenda-files nil 'ifmode))
4570 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4571 (pop org-agenda-text-search-extra-files)
4572 (setq files (org-add-archive-files files)))
4573 (setq files (append files org-agenda-text-search-extra-files)
4574 rtnall nil)
4575 (while (setq file (pop files))
4576 (setq ee nil)
4577 (catch 'nextfile
4578 (org-check-agenda-file file)
4579 (setq buffer (if (file-exists-p file)
4580 (org-get-agenda-file-buffer file)
4581 (error "No such file %s" file)))
4582 (if (not buffer)
4583 ;; If file does not exist, make sure an error message is sent
4584 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4585 file))))
4586 (with-current-buffer buffer
4587 (with-syntax-table (org-search-syntax-table)
4588 (unless (derived-mode-p 'org-mode)
4589 (error "Agenda file %s is not in `org-mode'" file))
4590 (let ((case-fold-search t))
4591 (save-excursion
4592 (save-restriction
4593 (if (eq buffer org-agenda-restrict)
4594 (narrow-to-region org-agenda-restrict-begin
4595 org-agenda-restrict-end)
4596 (widen))
4597 (goto-char (point-min))
4598 (unless (or (org-at-heading-p)
4599 (outline-next-heading))
4600 (throw 'nextfile t))
4601 (goto-char (max (point-min) (1- (point))))
4602 (while (re-search-forward regexp nil t)
4603 (org-back-to-heading t)
4604 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4605 (> (org-reduced-level (org-outline-level))
4606 org-agenda-search-view-max-outline-level)
4607 (forward-line -1)
4608 (outline-back-to-heading t)))
4609 (skip-chars-forward "* ")
4610 (setq beg (point-at-bol)
4611 beg1 (point)
4612 end (progn
4613 (outline-next-heading)
4614 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4615 (> (org-reduced-level (org-outline-level))
4616 org-agenda-search-view-max-outline-level)
4617 (forward-line 1)
4618 (outline-next-heading)))
4619 (point)))
4621 (catch :skip
4622 (goto-char beg)
4623 (org-agenda-skip)
4624 (setq str (buffer-substring-no-properties
4625 (point-at-bol)
4626 (if hdl-only (point-at-eol) end)))
4627 (mapc (lambda (wr) (when (string-match wr str)
4628 (goto-char (1- end))
4629 (throw :skip t)))
4630 regexps-)
4631 (mapc (lambda (wr) (unless (string-match wr str)
4632 (goto-char (1- end))
4633 (throw :skip t)))
4634 (if todo-only
4635 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4636 regexps+)
4637 regexps+))
4638 (goto-char beg)
4639 (setq marker (org-agenda-new-marker (point))
4640 category (org-get-category)
4641 level (make-string (org-reduced-level (org-outline-level)) ? )
4642 inherited-tags
4643 (or (eq org-agenda-show-inherited-tags 'always)
4644 (and (listp org-agenda-show-inherited-tags)
4645 (memq 'todo org-agenda-show-inherited-tags))
4646 (and (eq org-agenda-show-inherited-tags t)
4647 (or (eq org-agenda-use-tag-inheritance t)
4648 (memq 'todo org-agenda-use-tag-inheritance))))
4649 tags (org-get-tags-at nil (not inherited-tags))
4650 txt (org-agenda-format-item
4652 (buffer-substring-no-properties
4653 beg1 (point-at-eol))
4654 level category tags t))
4655 (org-add-props txt props
4656 'org-marker marker 'org-hd-marker marker
4657 'org-todo-regexp org-todo-regexp
4658 'level level
4659 'org-complex-heading-regexp org-complex-heading-regexp
4660 'priority 1000
4661 'type "search")
4662 (push txt ee)
4663 (goto-char (1- end))))))))))
4664 (setq rtn (nreverse ee))
4665 (setq rtnall (append rtnall rtn)))
4666 (if org-agenda-overriding-header
4667 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4668 nil 'face 'org-agenda-structure) "\n")
4669 (insert "Search words: ")
4670 (add-text-properties (point-min) (1- (point))
4671 (list 'face 'org-agenda-structure))
4672 (setq pos (point))
4673 (insert string "\n")
4674 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4675 (setq pos (point))
4676 (unless org-agenda-multi
4677 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4678 (add-text-properties pos (1- (point))
4679 (list 'face 'org-agenda-structure))))
4680 (org-agenda-mark-header-line (point-min))
4681 (when rtnall
4682 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4683 (goto-char (point-min))
4684 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4685 (add-text-properties (point-min) (point-max)
4686 `(org-agenda-type search
4687 org-last-args (,todo-only ,string ,edit-at)
4688 org-redo-cmd ,org-agenda-redo-command
4689 org-series-cmd ,org-cmd))
4690 (org-agenda-finalize)
4691 (setq buffer-read-only t))))
4693 ;;; Agenda TODO list
4695 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4696 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4697 (concat
4698 (if (or (equal keywords "ALL") (not keywords))
4699 (propertize "ALL" 'face 'warning)
4700 (mapconcat
4701 (lambda (kw)
4702 (propertize kw 'face (org-get-todo-face kw)))
4703 (org-split-string keywords "|")
4704 "|"))
4705 "\n"))
4707 (defvar org-select-this-todo-keyword nil)
4708 (defvar org-last-arg nil)
4710 ;;;###autoload
4711 (defun org-todo-list (&optional arg)
4712 "Show all (not done) TODO entries from all agenda file in a single list.
4713 The prefix arg can be used to select a specific TODO keyword and limit
4714 the list to these. When using \\[universal-argument], you will be prompted
4715 for a keyword. A numeric prefix directly selects the Nth keyword in
4716 `org-todo-keywords-1'."
4717 (interactive "P")
4718 (if org-agenda-overriding-arguments
4719 (setq arg org-agenda-overriding-arguments))
4720 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4721 (let* ((today (org-today))
4722 (date (calendar-gregorian-from-absolute today))
4723 (kwds org-todo-keywords-for-agenda)
4724 (completion-ignore-case t)
4725 (org-select-this-todo-keyword
4726 (if (stringp arg) arg
4727 (and arg (integerp arg) (> arg 0)
4728 (nth (1- arg) kwds))))
4729 rtn rtnall files file pos)
4730 (when (equal arg '(4))
4731 (setq org-select-this-todo-keyword
4732 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4733 (mapcar 'list kwds) nil nil)))
4734 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4735 (catch 'exit
4736 (if org-agenda-sticky
4737 (setq org-agenda-buffer-name
4738 (if (stringp org-select-this-todo-keyword)
4739 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4740 org-select-this-todo-keyword)
4741 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4742 (org-agenda-prepare "TODO")
4743 (org-compile-prefix-format 'todo)
4744 (org-set-sorting-strategy 'todo)
4745 (setq org-agenda-redo-command
4746 `(org-todo-list (or (and (numberp current-prefix-arg)
4747 current-prefix-arg)
4748 ,org-select-this-todo-keyword
4749 current-prefix-arg ,arg)))
4750 (setq files (org-agenda-files nil 'ifmode)
4751 rtnall nil)
4752 (while (setq file (pop files))
4753 (catch 'nextfile
4754 (org-check-agenda-file file)
4755 (setq rtn (org-agenda-get-day-entries file date :todo))
4756 (setq rtnall (append rtnall rtn))))
4757 (if org-agenda-overriding-header
4758 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4759 nil 'face 'org-agenda-structure) "\n")
4760 (insert "Global list of TODO items of type: ")
4761 (add-text-properties (point-min) (1- (point))
4762 (list 'face 'org-agenda-structure
4763 'short-heading
4764 (concat "ToDo: "
4765 (or org-select-this-todo-keyword "ALL"))))
4766 (org-agenda-mark-header-line (point-min))
4767 (insert (org-agenda-propertize-selected-todo-keywords
4768 org-select-this-todo-keyword))
4769 (setq pos (point))
4770 (unless org-agenda-multi
4771 (insert "Available with `N r': (0)[ALL]")
4772 (let ((n 0) s)
4773 (mapc (lambda (x)
4774 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4775 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4776 (insert "\n "))
4777 (insert " " s))
4778 kwds))
4779 (insert "\n"))
4780 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4781 (org-agenda-mark-header-line (point-min))
4782 (when rtnall
4783 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4784 (goto-char (point-min))
4785 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4786 (add-text-properties (point-min) (point-max)
4787 `(org-agenda-type todo
4788 org-last-args ,arg
4789 org-redo-cmd ,org-agenda-redo-command
4790 org-series-cmd ,org-cmd))
4791 (org-agenda-finalize)
4792 (setq buffer-read-only t))))
4794 ;;; Agenda tags match
4796 ;;;###autoload
4797 (defun org-tags-view (&optional todo-only match)
4798 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4799 The prefix arg TODO-ONLY limits the search to TODO entries."
4800 (interactive "P")
4801 (if org-agenda-overriding-arguments
4802 (setq todo-only (car org-agenda-overriding-arguments)
4803 match (nth 1 org-agenda-overriding-arguments)))
4804 (let* ((org-tags-match-list-sublevels
4805 org-tags-match-list-sublevels)
4806 (completion-ignore-case t)
4807 rtn rtnall files file pos matcher
4808 buffer)
4809 (when (and (stringp match) (not (string-match "\\S-" match)))
4810 (setq match nil))
4811 (catch 'exit
4812 (if org-agenda-sticky
4813 (setq org-agenda-buffer-name
4814 (if (stringp match)
4815 (format "*Org Agenda(%s:%s)*"
4816 (or org-keys (or (and todo-only "M") "m")) match)
4817 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4818 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4819 ;; expanding tags within `org-make-tags-matcher'
4820 (org-agenda-prepare (concat "TAGS " match))
4821 (setq matcher (org-make-tags-matcher match)
4822 match (car matcher) matcher (cdr matcher))
4823 (org-compile-prefix-format 'tags)
4824 (org-set-sorting-strategy 'tags)
4825 (setq org-agenda-query-string match)
4826 (setq org-agenda-redo-command
4827 (list 'org-tags-view `(quote ,todo-only)
4828 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4829 (setq files (org-agenda-files nil 'ifmode)
4830 rtnall nil)
4831 (while (setq file (pop files))
4832 (catch 'nextfile
4833 (org-check-agenda-file file)
4834 (setq buffer (if (file-exists-p file)
4835 (org-get-agenda-file-buffer file)
4836 (error "No such file %s" file)))
4837 (if (not buffer)
4838 ;; If file does not exist, error message to agenda
4839 (setq rtn (list
4840 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4841 rtnall (append rtnall rtn))
4842 (with-current-buffer buffer
4843 (unless (derived-mode-p 'org-mode)
4844 (error "Agenda file %s is not in `org-mode'" file))
4845 (save-excursion
4846 (save-restriction
4847 (if (eq buffer org-agenda-restrict)
4848 (narrow-to-region org-agenda-restrict-begin
4849 org-agenda-restrict-end)
4850 (widen))
4851 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4852 (setq rtnall (append rtnall rtn))))))))
4853 (if org-agenda-overriding-header
4854 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4855 nil 'face 'org-agenda-structure) "\n")
4856 (insert "Headlines with TAGS match: ")
4857 (add-text-properties (point-min) (1- (point))
4858 (list 'face 'org-agenda-structure
4859 'short-heading
4860 (concat "Match: " match)))
4861 (setq pos (point))
4862 (insert match "\n")
4863 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4864 (setq pos (point))
4865 (unless org-agenda-multi
4866 (insert "Press `C-u r' to search again with new search string\n"))
4867 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4868 (org-agenda-mark-header-line (point-min))
4869 (when rtnall
4870 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4871 (goto-char (point-min))
4872 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4873 (add-text-properties (point-min) (point-max)
4874 `(org-agenda-type tags
4875 org-last-args (,todo-only ,match)
4876 org-redo-cmd ,org-agenda-redo-command
4877 org-series-cmd ,org-cmd))
4878 (org-agenda-finalize)
4879 (setq buffer-read-only t))))
4881 ;;; Agenda Finding stuck projects
4883 (defvar org-agenda-skip-regexp nil
4884 "Regular expression used in skipping subtrees for the agenda.
4885 This is basically a temporary global variable that can be set and then
4886 used by user-defined selections using `org-agenda-skip-function'.")
4888 (defvar org-agenda-overriding-header nil
4889 "When set during agenda, todo and tags searches it replaces the header.
4890 This variable should not be set directly, but custom commands can bind it
4891 in the options section.")
4893 (defun org-agenda-skip-entry-when-regexp-matches ()
4894 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4895 If yes, it returns the end position of this entry, causing agenda commands
4896 to skip the entry but continuing the search in the subtree. This is a
4897 function that can be put into `org-agenda-skip-function' for the duration
4898 of a command."
4899 (let ((end (save-excursion (org-end-of-subtree t)))
4900 skip)
4901 (save-excursion
4902 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4903 (and skip end)))
4905 (defun org-agenda-skip-subtree-when-regexp-matches ()
4906 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4907 If yes, it returns the end position of this tree, causing agenda commands
4908 to skip this subtree. This is a function that can be put into
4909 `org-agenda-skip-function' for the duration of a command."
4910 (let ((end (save-excursion (org-end-of-subtree t)))
4911 skip)
4912 (save-excursion
4913 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4914 (and skip end)))
4916 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4917 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4918 If yes, it returns the end position of the current entry (NOT the tree),
4919 causing agenda commands to skip the entry but continuing the search in
4920 the subtree. This is a function that can be put into
4921 `org-agenda-skip-function' for the duration of a command. An important
4922 use of this function is for the stuck project list."
4923 (let ((end (save-excursion (org-end-of-subtree t)))
4924 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4925 skip)
4926 (save-excursion
4927 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4928 (and skip entry-end)))
4930 (defun org-agenda-skip-entry-if (&rest conditions)
4931 "Skip entry if any of CONDITIONS is true.
4932 See `org-agenda-skip-if' for details."
4933 (org-agenda-skip-if nil conditions))
4935 (defun org-agenda-skip-subtree-if (&rest conditions)
4936 "Skip subtree if any of CONDITIONS is true.
4937 See `org-agenda-skip-if' for details."
4938 (org-agenda-skip-if t conditions))
4940 (defun org-agenda-skip-if (subtree conditions)
4941 "Checks current entity for CONDITIONS.
4942 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4943 the entry (i.e. the text before the next heading) is checked.
4945 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4946 from different tests. Valid conditions are:
4948 scheduled Check if there is a scheduled cookie
4949 notscheduled Check if there is no scheduled cookie
4950 deadline Check if there is a deadline
4951 notdeadline Check if there is no deadline
4952 timestamp Check if there is a timestamp (also deadline or scheduled)
4953 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4954 regexp Check if regexp matches
4955 notregexp Check if regexp does not match.
4956 todo Check if TODO keyword matches
4957 nottodo Check if TODO keyword does not match
4959 The regexp is taken from the conditions list, it must come right after
4960 the `regexp' or `notregexp' element.
4962 `todo' and `nottodo' accept as an argument a list of todo
4963 keywords, which may include \"*\" to match any todo keyword.
4965 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4967 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4969 Instead of a list, a keyword class may be given. For example:
4971 (org-agenda-skip-entry-if 'nottodo 'done)
4973 would skip entries that haven't been marked with any of \"DONE\"
4974 keywords. Possible classes are: `todo', `done', `any'.
4976 If any of these conditions is met, this function returns the end point of
4977 the entity, causing the search to continue from there. This is a function
4978 that can be put into `org-agenda-skip-function' for the duration of a command."
4979 (let (beg end m)
4980 (org-back-to-heading t)
4981 (setq beg (point)
4982 end (if subtree
4983 (progn (org-end-of-subtree t) (point))
4984 (progn (outline-next-heading) (1- (point)))))
4985 (goto-char beg)
4986 (and
4988 (and (memq 'scheduled conditions)
4989 (re-search-forward org-scheduled-time-regexp end t))
4990 (and (memq 'notscheduled conditions)
4991 (not (re-search-forward org-scheduled-time-regexp end t)))
4992 (and (memq 'deadline conditions)
4993 (re-search-forward org-deadline-time-regexp end t))
4994 (and (memq 'notdeadline conditions)
4995 (not (re-search-forward org-deadline-time-regexp end t)))
4996 (and (memq 'timestamp conditions)
4997 (re-search-forward org-ts-regexp end t))
4998 (and (memq 'nottimestamp conditions)
4999 (not (re-search-forward org-ts-regexp end t)))
5000 (and (setq m (memq 'regexp conditions))
5001 (stringp (nth 1 m))
5002 (re-search-forward (nth 1 m) end t))
5003 (and (setq m (memq 'notregexp conditions))
5004 (stringp (nth 1 m))
5005 (not (re-search-forward (nth 1 m) end t)))
5006 (and (or
5007 (setq m (memq 'nottodo conditions))
5008 (setq m (memq 'todo-unblocked conditions))
5009 (setq m (memq 'nottodo-unblocked conditions))
5010 (setq m (memq 'todo conditions)))
5011 (org-agenda-skip-if-todo m end)))
5012 end)))
5014 (defun org-agenda-skip-if-todo (args end)
5015 "Helper function for `org-agenda-skip-if', do not use it directly.
5016 ARGS is a list with first element either `todo', `nottodo',
5017 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5018 a list of TODO keywords, or a state symbol `todo' or `done' or
5019 `any'."
5020 (let ((kw (car args))
5021 (arg (cadr args))
5022 todo-wds todo-re)
5023 (setq todo-wds
5024 (org-uniquify
5025 (cond
5026 ((listp arg) ;; list of keywords
5027 (if (member "*" arg)
5028 (mapcar 'substring-no-properties org-todo-keywords-1)
5029 arg))
5030 ((symbolp arg) ;; keyword class name
5031 (cond
5032 ((eq arg 'todo)
5033 (org-delete-all org-done-keywords
5034 (mapcar 'substring-no-properties
5035 org-todo-keywords-1)))
5036 ((eq arg 'done) org-done-keywords)
5037 ((eq arg 'any)
5038 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5039 (setq todo-re
5040 (concat "^\\*+[ \t]+\\<\\("
5041 (mapconcat 'identity todo-wds "\\|")
5042 "\\)\\>"))
5043 (cond
5044 ((eq kw 'todo) (re-search-forward todo-re end t))
5045 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5046 ((eq kw 'todo-unblocked)
5047 (catch 'unblocked
5048 (while (re-search-forward todo-re end t)
5049 (or (org-entry-blocked-p) (throw 'unblocked t)))
5050 nil))
5051 ((eq kw 'nottodo-unblocked)
5052 (catch 'unblocked
5053 (while (re-search-forward todo-re end t)
5054 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5058 ;;;###autoload
5059 (defun org-agenda-list-stuck-projects (&rest ignore)
5060 "Create agenda view for projects that are stuck.
5061 Stuck projects are project that have no next actions. For the definitions
5062 of what a project is and how to check if it stuck, customize the variable
5063 `org-stuck-projects'."
5064 (interactive)
5065 (let* ((org-agenda-skip-function
5066 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5067 ;; We could have used org-agenda-skip-if here.
5068 (org-agenda-overriding-header
5069 (or org-agenda-overriding-header "List of stuck projects: "))
5070 (matcher (nth 0 org-stuck-projects))
5071 (todo (nth 1 org-stuck-projects))
5072 (todo-wds (if (member "*" todo)
5073 (progn
5074 (org-agenda-prepare-buffers (org-agenda-files
5075 nil 'ifmode))
5076 (org-delete-all
5077 org-done-keywords-for-agenda
5078 (copy-sequence org-todo-keywords-for-agenda)))
5079 todo))
5080 (todo-re (concat "^\\*+[ \t]+\\("
5081 (mapconcat 'identity todo-wds "\\|")
5082 "\\)\\>"))
5083 (tags (nth 2 org-stuck-projects))
5084 (tags-re (if (member "*" tags)
5085 (concat org-outline-regexp-bol
5086 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5087 (if tags
5088 (concat org-outline-regexp-bol
5089 ".*:\\("
5090 (mapconcat 'identity tags "\\|")
5091 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5092 (gen-re (nth 3 org-stuck-projects))
5093 (re-list
5094 (delq nil
5095 (list
5096 (if todo todo-re)
5097 (if tags tags-re)
5098 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5099 gen-re)))))
5100 (setq org-agenda-skip-regexp
5101 (if re-list
5102 (mapconcat 'identity re-list "\\|")
5103 (error "No information how to identify unstuck projects")))
5104 (org-tags-view nil matcher)
5105 (setq org-agenda-buffer-name (buffer-name))
5106 (with-current-buffer org-agenda-buffer-name
5107 (setq org-agenda-redo-command
5108 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5110 ;;; Diary integration
5112 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5113 (defvar diary-list-entries-hook)
5114 (defvar diary-time-regexp)
5115 (defun org-get-entries-from-diary (date)
5116 "Get the (Emacs Calendar) diary entries for DATE."
5117 (require 'diary-lib)
5118 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5119 (diary-display-hook '(fancy-diary-display))
5120 (diary-display-function 'fancy-diary-display)
5121 (pop-up-frames nil)
5122 (diary-list-entries-hook
5123 (cons 'org-diary-default-entry diary-list-entries-hook))
5124 (diary-file-name-prefix nil) ; turn this feature off
5125 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5126 entries
5127 (org-disable-agenda-to-diary t))
5128 (save-excursion
5129 (save-window-excursion
5130 (funcall (if (fboundp 'diary-list-entries)
5131 'diary-list-entries 'list-diary-entries)
5132 date 1)))
5133 (if (not (get-buffer diary-fancy-buffer))
5134 (setq entries nil)
5135 (with-current-buffer diary-fancy-buffer
5136 (setq buffer-read-only nil)
5137 (if (zerop (buffer-size))
5138 ;; No entries
5139 (setq entries nil)
5140 ;; Omit the date and other unnecessary stuff
5141 (org-agenda-cleanup-fancy-diary)
5142 ;; Add prefix to each line and extend the text properties
5143 (if (zerop (buffer-size))
5144 (setq entries nil)
5145 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5146 (setq entries
5147 (with-temp-buffer
5148 (insert entries) (goto-char (point-min))
5149 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5150 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5151 (replace-match (concat "; " (match-string 1)))))
5152 (buffer-string)))))
5153 (set-buffer-modified-p nil)
5154 (kill-buffer diary-fancy-buffer)))
5155 (when entries
5156 (setq entries (org-split-string entries "\n"))
5157 (setq entries
5158 (mapcar
5159 (lambda (x)
5160 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5161 ;; Extend the text properties to the beginning of the line
5162 (org-add-props x (text-properties-at (1- (length x)) x)
5163 'type "diary" 'date date 'face 'org-agenda-diary))
5164 entries)))))
5166 (defvar org-agenda-cleanup-fancy-diary-hook nil
5167 "Hook run when the fancy diary buffer is cleaned up.")
5169 (defun org-agenda-cleanup-fancy-diary ()
5170 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5171 This gets rid of the date, the underline under the date, and
5172 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5173 date. It also removes lines that contain only whitespace."
5174 (goto-char (point-min))
5175 (if (looking-at ".*?:[ \t]*")
5176 (progn
5177 (replace-match "")
5178 (re-search-forward "\n=+$" nil t)
5179 (replace-match "")
5180 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5181 (re-search-forward "\n=+$" nil t)
5182 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5183 (goto-char (point-min))
5184 (while (re-search-forward "^ +\n" nil t)
5185 (replace-match ""))
5186 (goto-char (point-min))
5187 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5188 (replace-match ""))
5189 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5191 ;; Make sure entries from the diary have the right text properties.
5192 (eval-after-load "diary-lib"
5193 '(if (boundp 'diary-modify-entry-list-string-function)
5194 ;; We can rely on the hook, nothing to do
5196 ;; Hook not available, must use advice to make this work
5197 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5198 "Make the position visible."
5199 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5200 (stringp string)
5201 buffer-file-name)
5202 (setq string (org-modify-diary-entry-string string))))))
5204 (defun org-modify-diary-entry-string (string)
5205 "Add text properties to string, allowing org-mode to act on it."
5206 (org-add-props string nil
5207 'mouse-face 'highlight
5208 'help-echo (if buffer-file-name
5209 (format "mouse-2 or RET jump to diary file %s"
5210 (abbreviate-file-name buffer-file-name))
5212 'org-agenda-diary-link t
5213 'org-marker (org-agenda-new-marker (point-at-bol))))
5215 (defun org-diary-default-entry ()
5216 "Add a dummy entry to the diary.
5217 Needed to avoid empty dates which mess up holiday display."
5218 ;; Catch the error if dealing with the new add-to-diary-alist
5219 (when org-disable-agenda-to-diary
5220 (condition-case nil
5221 (org-add-to-diary-list original-date "Org-mode dummy" "")
5222 (error
5223 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5225 (defun org-add-to-diary-list (&rest args)
5226 (if (fboundp 'diary-add-to-list)
5227 (apply 'diary-add-to-list args)
5228 (apply 'add-to-diary-list args)))
5230 (defvar org-diary-last-run-time nil)
5232 ;;;###autoload
5233 (defun org-diary (&rest args)
5234 "Return diary information from org files.
5235 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5236 It accesses org files and extracts information from those files to be
5237 listed in the diary. The function accepts arguments specifying what
5238 items should be listed. For a list of arguments allowed here, see the
5239 variable `org-agenda-entry-types'.
5241 The call in the diary file should look like this:
5243 &%%(org-diary) ~/path/to/some/orgfile.org
5245 Use a separate line for each org file to check. Or, if you omit the file name,
5246 all files listed in `org-agenda-files' will be checked automatically:
5248 &%%(org-diary)
5250 If you don't give any arguments (as in the example above), the default value
5251 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5252 So the example above may also be written as
5254 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5256 The function expects the lisp variables `entry' and `date' to be provided
5257 by the caller, because this is how the calendar works. Don't use this
5258 function from a program - use `org-agenda-get-day-entries' instead."
5259 (when (> (- (org-float-time)
5260 org-agenda-last-marker-time)
5262 ;; I am not sure if this works with sticky agendas, because the marker
5263 ;; list is then no longer a global variable.
5264 (org-agenda-reset-markers))
5265 (org-compile-prefix-format 'agenda)
5266 (org-set-sorting-strategy 'agenda)
5267 (setq args (or args org-agenda-entry-types))
5268 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5269 (list entry)
5270 (org-agenda-files t)))
5271 (time (org-float-time))
5272 file rtn results)
5273 (when (or (not org-diary-last-run-time)
5274 (> (- time
5275 org-diary-last-run-time)
5277 (org-agenda-prepare-buffers files))
5278 (setq org-diary-last-run-time time)
5279 ;; If this is called during org-agenda, don't return any entries to
5280 ;; the calendar. Org Agenda will list these entries itself.
5281 (if org-disable-agenda-to-diary (setq files nil))
5282 (while (setq file (pop files))
5283 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5284 (setq results (append results rtn)))
5285 (when results
5286 (setq results
5287 (mapcar (lambda (i) (replace-regexp-in-string
5288 org-bracket-link-regexp "\\3" i)) results))
5289 (concat (org-agenda-finalize-entries results) "\n"))))
5291 ;;; Agenda entry finders
5293 (defun org-agenda-get-day-entries (file date &rest args)
5294 "Does the work for `org-diary' and `org-agenda'.
5295 FILE is the path to a file to be checked for entries. DATE is date like
5296 the one returned by `calendar-current-date'. ARGS are symbols indicating
5297 which kind of entries should be extracted. For details about these, see
5298 the documentation of `org-diary'."
5299 (setq args (or args org-agenda-entry-types))
5300 (let* ((org-startup-folded nil)
5301 (org-startup-align-all-tables nil)
5302 (buffer (if (file-exists-p file)
5303 (org-get-agenda-file-buffer file)
5304 (error "No such file %s" file)))
5305 arg results rtn deadline-results)
5306 (if (not buffer)
5307 ;; If file does not exist, make sure an error message ends up in diary
5308 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5309 (with-current-buffer buffer
5310 (unless (derived-mode-p 'org-mode)
5311 (error "Agenda file %s is not in `org-mode'" file))
5312 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5313 (let ((case-fold-search nil))
5314 (save-excursion
5315 (save-restriction
5316 (if (eq buffer org-agenda-restrict)
5317 (narrow-to-region org-agenda-restrict-begin
5318 org-agenda-restrict-end)
5319 (widen))
5320 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5321 (while (setq arg (pop args))
5322 (cond
5323 ((and (eq arg :todo)
5324 (equal date (calendar-gregorian-from-absolute
5325 (org-today))))
5326 (setq rtn (org-agenda-get-todos))
5327 (setq results (append results rtn)))
5328 ((eq arg :timestamp)
5329 (setq rtn (org-agenda-get-blocks))
5330 (setq results (append results rtn))
5331 (setq rtn (org-agenda-get-timestamps deadline-results))
5332 (setq results (append results rtn)))
5333 ((eq arg :sexp)
5334 (setq rtn (org-agenda-get-sexps))
5335 (setq results (append results rtn)))
5336 ((eq arg :scheduled)
5337 (setq rtn (org-agenda-get-scheduled deadline-results))
5338 (setq results (append results rtn)))
5339 ((eq arg :scheduled*)
5340 (setq rtn (org-agenda-get-scheduled deadline-results t))
5341 (setq results (append results rtn)))
5342 ((eq arg :closed)
5343 (setq rtn (org-agenda-get-progress))
5344 (setq results (append results rtn)))
5345 ((eq arg :deadline)
5346 (setq rtn (org-agenda-get-deadlines))
5347 (setq deadline-results (copy-sequence rtn))
5348 (setq results (append results rtn)))
5349 ((eq arg :deadline*)
5350 (setq rtn (org-agenda-get-deadlines t))
5351 (setq deadline-results (copy-sequence rtn))
5352 (setq results (append results rtn))))))))
5353 results))))
5355 (defsubst org-em (x y list)
5356 "Is X or Y a member of LIST?"
5357 (or (memq x list) (memq y list)))
5359 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5360 (defvar org-agenda-sorting-strategy-selected nil)
5362 (defun org-agenda-get-todos ()
5363 "Return the TODO information for agenda display."
5364 (let* ((props (list 'face nil
5365 'done-face 'org-agenda-done
5366 'org-not-done-regexp org-not-done-regexp
5367 'org-todo-regexp org-todo-regexp
5368 'org-complex-heading-regexp org-complex-heading-regexp
5369 'mouse-face 'highlight
5370 'help-echo
5371 (format "mouse-2 or RET jump to org file %s"
5372 (abbreviate-file-name buffer-file-name))))
5373 (regexp (format org-heading-keyword-regexp-format
5374 (cond
5375 ((and org-select-this-todo-keyword
5376 (equal org-select-this-todo-keyword "*"))
5377 org-todo-regexp)
5378 (org-select-this-todo-keyword
5379 (concat "\\("
5380 (mapconcat 'identity
5381 (org-split-string
5382 org-select-this-todo-keyword
5383 "|")
5384 "\\|") "\\)"))
5385 (t org-not-done-regexp))))
5386 marker priority category level tags todo-state ts-date ts-date-type
5387 ee txt beg end inherited-tags todo-state-end-pos)
5388 (goto-char (point-min))
5389 (while (re-search-forward regexp nil t)
5390 (catch :skip
5391 (save-match-data
5392 (beginning-of-line)
5393 (org-agenda-skip)
5394 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5395 (unless (and (setq todo-state (org-get-todo-state))
5396 (setq todo-state-end-pos (match-end 2)))
5397 (goto-char end)
5398 (throw :skip nil))
5399 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5400 (goto-char (1+ beg))
5401 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5402 (throw :skip nil)))
5403 (goto-char (match-beginning 2))
5404 (setq marker (org-agenda-new-marker (match-beginning 0))
5405 category (org-get-category)
5406 ts-date (let (ts)
5407 (save-match-data
5408 (cond ((org-em 'scheduled-up 'scheduled-down
5409 org-agenda-sorting-strategy-selected)
5410 (setq ts (org-entry-get (point) "SCHEDULED")
5411 ts-date-type " scheduled"))
5412 ((org-em 'deadline-up 'deadline-down
5413 org-agenda-sorting-strategy-selected)
5414 (setq ts (org-entry-get (point) "DEADLINE")
5415 ts-date-type " deadline"))
5416 ((org-em 'ts-up 'ts-down
5417 org-agenda-sorting-strategy-selected)
5418 (setq ts (org-entry-get (point) "TIMESTAMP")
5419 ts-date-type " timestamp"))
5420 ((org-em 'tsia-up 'tsia-down
5421 org-agenda-sorting-strategy-selected)
5422 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5423 ts-date-type " timestamp_ia"))
5424 ((org-em 'timestamp-up 'timestamp-down
5425 org-agenda-sorting-strategy-selected)
5426 (setq ts (or (org-entry-get (point) "SCHEDULED")
5427 (org-entry-get (point) "DEADLINE")
5428 (org-entry-get (point) "TIMESTAMP")
5429 (org-entry-get (point) "TIMESTAMP_IA"))
5430 ts-date-type ""))
5431 (t (setq ts-date-type "")))
5432 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5433 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5434 inherited-tags
5435 (or (eq org-agenda-show-inherited-tags 'always)
5436 (and (listp org-agenda-show-inherited-tags)
5437 (memq 'todo org-agenda-show-inherited-tags))
5438 (and (eq org-agenda-show-inherited-tags t)
5439 (or (eq org-agenda-use-tag-inheritance t)
5440 (memq 'todo org-agenda-use-tag-inheritance))))
5441 tags (org-get-tags-at nil (not inherited-tags))
5442 level (make-string (org-reduced-level (org-outline-level)) ? )
5443 txt (org-agenda-format-item "" txt level category tags t)
5444 priority (1+ (org-get-priority txt)))
5445 (org-add-props txt props
5446 'org-marker marker 'org-hd-marker marker
5447 'priority priority
5448 'level level
5449 'ts-date ts-date
5450 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5451 (push txt ee)
5452 (if org-agenda-todo-list-sublevels
5453 (goto-char todo-state-end-pos)
5454 (org-end-of-subtree 'invisible))))
5455 (nreverse ee)))
5457 (defun org-agenda-todo-custom-ignore-p (time n)
5458 "Check whether timestamp is farther away than n number of days.
5459 This function is invoked if `org-agenda-todo-ignore-deadlines',
5460 `org-agenda-todo-ignore-scheduled' or
5461 `org-agenda-todo-ignore-timestamp' is set to an integer."
5462 (let ((days (org-time-stamp-to-now
5463 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5464 (if (>= n 0)
5465 (>= days n)
5466 (<= days n))))
5468 ;;;###autoload
5469 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5470 (&optional end)
5471 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5472 (when (or org-agenda-todo-ignore-with-date
5473 org-agenda-todo-ignore-scheduled
5474 org-agenda-todo-ignore-deadlines
5475 org-agenda-todo-ignore-timestamp)
5476 (setq end (or end (save-excursion (outline-next-heading) (point))))
5477 (save-excursion
5478 (or (and org-agenda-todo-ignore-with-date
5479 (re-search-forward org-ts-regexp end t))
5480 (and org-agenda-todo-ignore-scheduled
5481 (re-search-forward org-scheduled-time-regexp end t)
5482 (cond
5483 ((eq org-agenda-todo-ignore-scheduled 'future)
5484 (> (org-time-stamp-to-now
5485 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5486 ((eq org-agenda-todo-ignore-scheduled 'past)
5487 (<= (org-time-stamp-to-now
5488 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5489 ((numberp org-agenda-todo-ignore-scheduled)
5490 (org-agenda-todo-custom-ignore-p
5491 (match-string 1) org-agenda-todo-ignore-scheduled))
5492 (t)))
5493 (and org-agenda-todo-ignore-deadlines
5494 (re-search-forward org-deadline-time-regexp end t)
5495 (cond
5496 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5497 ((eq org-agenda-todo-ignore-deadlines 'far)
5498 (not (org-deadline-close (match-string 1))))
5499 ((eq org-agenda-todo-ignore-deadlines 'future)
5500 (> (org-time-stamp-to-now
5501 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5502 ((eq org-agenda-todo-ignore-deadlines 'past)
5503 (<= (org-time-stamp-to-now
5504 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5505 ((numberp org-agenda-todo-ignore-deadlines)
5506 (org-agenda-todo-custom-ignore-p
5507 (match-string 1) org-agenda-todo-ignore-deadlines))
5508 (t (org-deadline-close (match-string 1)))))
5509 (and org-agenda-todo-ignore-timestamp
5510 (let ((buffer (current-buffer))
5511 (regexp
5512 (concat
5513 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5514 (start (point)))
5515 ;; Copy current buffer into a temporary one
5516 (with-temp-buffer
5517 (insert-buffer-substring buffer start end)
5518 (goto-char (point-min))
5519 ;; Delete SCHEDULED and DEADLINE items
5520 (while (re-search-forward regexp end t)
5521 (delete-region (match-beginning 0) (match-end 0)))
5522 (goto-char (point-min))
5523 ;; No search for timestamp left
5524 (when (re-search-forward org-ts-regexp nil t)
5525 (cond
5526 ((eq org-agenda-todo-ignore-timestamp 'future)
5527 (> (org-time-stamp-to-now
5528 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5529 ((eq org-agenda-todo-ignore-timestamp 'past)
5530 (<= (org-time-stamp-to-now
5531 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5532 ((numberp org-agenda-todo-ignore-timestamp)
5533 (org-agenda-todo-custom-ignore-p
5534 (match-string 1) org-agenda-todo-ignore-timestamp))
5535 (t))))))))))
5537 (defun org-agenda-get-timestamps (&optional deadline-results)
5538 "Return the date stamp information for agenda display."
5539 (let* ((props (list 'face 'org-agenda-calendar-event
5540 'org-not-done-regexp org-not-done-regexp
5541 'org-todo-regexp org-todo-regexp
5542 'org-complex-heading-regexp org-complex-heading-regexp
5543 'mouse-face 'highlight
5544 'help-echo
5545 (format "mouse-2 or RET jump to org file %s"
5546 (abbreviate-file-name buffer-file-name))))
5547 (d1 (calendar-absolute-from-gregorian date))
5549 (deadline-position-alist
5550 (mapcar (lambda (a) (and (setq mm (get-text-property
5551 0 'org-hd-marker a))
5552 (cons (marker-position mm) a)))
5553 deadline-results))
5554 (remove-re org-ts-regexp)
5555 (regexp
5556 (concat
5557 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5558 (regexp-quote
5559 (substring
5560 (format-time-string
5561 (car org-time-stamp-formats)
5562 (apply 'encode-time ; DATE bound by calendar
5563 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5564 1 11))
5565 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5566 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5567 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5568 donep tmp priority category level ee txt timestr tags
5569 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5570 inherited-tags ts-date)
5571 (goto-char (point-min))
5572 (while (setq end-of-match (re-search-forward regexp nil t))
5573 (setq b0 (match-beginning 0)
5574 b3 (match-beginning 3) e3 (match-end 3)
5575 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5576 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5577 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5578 (member todo-state
5579 org-agenda-repeating-timestamp-show-all)))
5580 (catch :skip
5581 (and (org-at-date-range-p) (throw :skip nil))
5582 (org-agenda-skip)
5583 (if (and (match-end 1)
5584 (not (= d1 (org-time-string-to-absolute
5585 (match-string 1) d1 nil show-all
5586 (current-buffer) b0))))
5587 (throw :skip nil))
5588 (if (and e3
5589 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5590 (throw :skip nil))
5591 (setq tmp (buffer-substring (max (point-min)
5592 (- b0 org-ds-keyword-length))
5594 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5595 inactivep (= (char-after b0) ?\[)
5596 deadlinep (string-match org-deadline-regexp tmp)
5597 scheduledp (string-match org-scheduled-regexp tmp)
5598 closedp (and org-agenda-include-inactive-timestamps
5599 (string-match org-closed-string tmp))
5600 clockp (and org-agenda-include-inactive-timestamps
5601 (or (string-match org-clock-string tmp)
5602 (string-match "]-+\\'" tmp)))
5603 warntime (get-text-property (point) 'org-appt-warntime)
5604 donep (member todo-state org-done-keywords))
5605 (if (or scheduledp deadlinep closedp clockp
5606 (and donep org-agenda-skip-timestamp-if-done))
5607 (throw :skip t))
5608 (if (string-match ">" timestr)
5609 ;; substring should only run to end of time stamp
5610 (setq timestr (substring timestr 0 (match-end 0))))
5611 (setq marker (org-agenda-new-marker b0)
5612 category (org-get-category b0))
5613 (save-excursion
5614 (if (not (re-search-backward org-outline-regexp-bol nil t))
5615 (throw :skip nil)
5616 (goto-char (match-beginning 0))
5617 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5618 (assoc (point) deadline-position-alist))
5619 (throw :skip nil))
5620 (setq hdmarker (org-agenda-new-marker)
5621 inherited-tags
5622 (or (eq org-agenda-show-inherited-tags 'always)
5623 (and (listp org-agenda-show-inherited-tags)
5624 (memq 'agenda org-agenda-show-inherited-tags))
5625 (and (eq org-agenda-show-inherited-tags t)
5626 (or (eq org-agenda-use-tag-inheritance t)
5627 (memq 'agenda org-agenda-use-tag-inheritance))))
5628 tags (org-get-tags-at nil (not inherited-tags))
5629 level (make-string (org-reduced-level (org-outline-level)) ? ))
5630 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5631 (setq head (or (match-string 1) ""))
5632 (setq txt (org-agenda-format-item
5633 (if inactivep org-agenda-inactive-leader nil)
5634 head level category tags timestr
5635 remove-re habitp)))
5636 (setq priority (org-get-priority txt))
5637 (org-add-props txt props 'priority priority
5638 'org-marker marker 'org-hd-marker hdmarker
5639 'date date
5640 'level level
5641 'ts-date
5642 (ignore-errors (org-time-string-to-absolute timestr))
5643 'todo-state todo-state
5644 'warntime warntime
5645 'type "timestamp")
5646 (push txt ee))
5647 (if org-agenda-skip-additional-timestamps-same-entry
5648 (outline-next-heading)
5649 (goto-char end-of-match))))
5650 (nreverse ee)))
5652 (defun org-agenda-get-sexps ()
5653 "Return the sexp information for agenda display."
5654 (require 'diary-lib)
5655 (let* ((props (list 'face 'org-agenda-calendar-sexp
5656 'mouse-face 'highlight
5657 'help-echo
5658 (format "mouse-2 or RET jump to org file %s"
5659 (abbreviate-file-name buffer-file-name))))
5660 (regexp "^&?%%(")
5661 marker category extra level ee txt tags entry
5662 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5663 (goto-char (point-min))
5664 (while (re-search-forward regexp nil t)
5665 (catch :skip
5666 (org-agenda-skip)
5667 (setq beg (match-beginning 0))
5668 (goto-char (1- (match-end 0)))
5669 (setq b (point))
5670 (forward-sexp 1)
5671 (setq sexp (buffer-substring b (point)))
5672 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5673 (org-trim (match-string 1))
5674 ""))
5675 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5676 (when result
5677 (setq marker (org-agenda-new-marker beg)
5678 level (make-string (org-reduced-level (org-outline-level)) ? )
5679 category (org-get-category beg)
5680 inherited-tags
5681 (or (eq org-agenda-show-inherited-tags 'always)
5682 (and (listp org-agenda-show-inherited-tags)
5683 (memq 'agenda org-agenda-show-inherited-tags))
5684 (and (eq org-agenda-show-inherited-tags t)
5685 (or (eq org-agenda-use-tag-inheritance t)
5686 (memq 'agenda org-agenda-use-tag-inheritance))))
5687 tags (org-get-tags-at nil (not inherited-tags))
5688 todo-state (org-get-todo-state)
5689 warntime (get-text-property (point) 'org-appt-warntime)
5690 extra nil)
5692 (dolist (r (if (stringp result)
5693 (list result)
5694 result)) ;; we expect a list here
5695 (when (and org-agenda-diary-sexp-prefix
5696 (string-match org-agenda-diary-sexp-prefix r))
5697 (setq extra (match-string 0 r)
5698 r (replace-match "" nil nil r)))
5699 (if (string-match "\\S-" r)
5700 (setq txt r)
5701 (setq txt "SEXP entry returned empty string"))
5702 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5703 (org-add-props txt props 'org-marker marker
5704 'date date 'todo-state todo-state
5705 'tags tags 'level level
5706 'type "sexp" 'warntime warntime)
5707 (push txt ee)))))
5708 (nreverse ee)))
5710 ;; Calendar sanity: define some functions that are independent of
5711 ;; `calendar-date-style'.
5712 ;; Normally I would like to use ISO format when calling the diary functions,
5713 ;; but to make sure we still have Emacs 22 compatibility we bind
5714 ;; also `european-calendar-style' and use european format
5715 (defun org-anniversary (year month day &optional mark)
5716 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5717 (org-no-warnings
5718 (let ((calendar-date-style 'european) (european-calendar-style t))
5719 (diary-anniversary day month year mark))))
5720 (defun org-cyclic (N year month day &optional mark)
5721 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5722 (org-no-warnings
5723 (let ((calendar-date-style 'european) (european-calendar-style t))
5724 (diary-cyclic N day month year mark))))
5725 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5726 "Like `diary-block', but with fixed (ISO) order of arguments."
5727 (org-no-warnings
5728 (let ((calendar-date-style 'european) (european-calendar-style t))
5729 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5730 (defun org-date (year month day &optional mark)
5731 "Like `diary-date', but with fixed (ISO) order of arguments."
5732 (org-no-warnings
5733 (let ((calendar-date-style 'european) (european-calendar-style t))
5734 (diary-date day month year mark))))
5736 ;; Define the` org-class' function
5737 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5738 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5739 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5740 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5741 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5742 `holidays', then any date that is known by the Emacs calendar to be a
5743 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5744 then those holidays will be skipped."
5745 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5746 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5747 (d (calendar-absolute-from-gregorian date))
5748 (h (when skip-weeks (calendar-check-holidays date))))
5749 (and
5750 (<= date1 d)
5751 (<= d date2)
5752 (= (calendar-day-of-week date) dayname)
5753 (or (not skip-weeks)
5754 (progn
5755 (require 'cal-iso)
5756 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5757 (not (or (and h (memq 'holidays skip-weeks))
5758 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5759 entry)))
5761 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5762 "Like `org-class', but honor `calendar-date-style'.
5763 The order of the first 2 times 3 arguments depends on the variable
5764 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5765 So for American calendars, give this as MONTH DAY YEAR, for European as
5766 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5767 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5768 is any number of ISO weeks in the block period for which the item should
5769 be skipped.
5771 This function is here only for backward compatibility and it is deprecated,
5772 please use `org-class' instead."
5773 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5774 (date2 (org-order-calendar-date-args m2 d2 y2)))
5775 (org-class
5776 (nth 2 date1) (car date1) (nth 1 date1)
5777 (nth 2 date2) (car date2) (nth 1 date2)
5778 dayname skip-weeks)))
5779 (make-obsolete 'org-diary-class 'org-class "")
5781 (defalias 'org-get-closed 'org-agenda-get-progress)
5782 (defun org-agenda-get-progress ()
5783 "Return the logged TODO entries for agenda display."
5784 (let* ((props (list 'mouse-face 'highlight
5785 'org-not-done-regexp org-not-done-regexp
5786 'org-todo-regexp org-todo-regexp
5787 'org-complex-heading-regexp org-complex-heading-regexp
5788 'help-echo
5789 (format "mouse-2 or RET jump to org file %s"
5790 (abbreviate-file-name buffer-file-name))))
5791 (items (if (consp org-agenda-show-log-scoped)
5792 org-agenda-show-log-scoped
5793 (if (eq org-agenda-show-log-scoped 'clockcheck)
5794 '(clock)
5795 org-agenda-log-mode-items)))
5796 (parts
5797 (delq nil
5798 (list
5799 (if (memq 'closed items) (concat "\\<" org-closed-string))
5800 (if (memq 'clock items) (concat "\\<" org-clock-string))
5801 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5802 (parts-re (if parts (mapconcat 'identity parts "\\|")
5803 (error "`org-agenda-log-mode-items' is empty")))
5804 (regexp (concat
5805 "\\(" parts-re "\\)"
5806 " *\\["
5807 (regexp-quote
5808 (substring
5809 (format-time-string
5810 (car org-time-stamp-formats)
5811 (apply 'encode-time ; DATE bound by calendar
5812 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5813 1 11))))
5814 (org-agenda-search-headline-for-time nil)
5815 marker hdmarker priority category level tags closedp
5816 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5817 (goto-char (point-min))
5818 (while (re-search-forward regexp nil t)
5819 (catch :skip
5820 (org-agenda-skip)
5821 (setq marker (org-agenda-new-marker (match-beginning 0))
5822 closedp (equal (match-string 1) org-closed-string)
5823 statep (equal (string-to-char (match-string 1)) ?-)
5824 clockp (not (or closedp statep))
5825 state (and statep (match-string 2))
5826 category (org-get-category (match-beginning 0))
5827 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5828 (when (string-match "\\]" timestr)
5829 ;; substring should only run to end of time stamp
5830 (setq rest (substring timestr (match-end 0))
5831 timestr (substring timestr 0 (match-end 0)))
5832 (if (and (not closedp) (not statep)
5833 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5834 rest))
5835 (progn (setq timestr (concat (substring timestr 0 -1)
5836 "-" (match-string 1 rest) "]"))
5837 (setq clocked (match-string 2 rest)))
5838 (setq clocked "-")))
5839 (save-excursion
5840 (setq extra
5841 (cond
5842 ((not org-agenda-log-mode-add-notes) nil)
5843 (statep
5844 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5845 (match-string 1)))
5846 (clockp
5847 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5848 (match-string 1)))))
5849 (if (not (re-search-backward org-outline-regexp-bol nil t))
5850 (throw :skip nil)
5851 (goto-char (match-beginning 0))
5852 (setq hdmarker (org-agenda-new-marker)
5853 inherited-tags
5854 (or (eq org-agenda-show-inherited-tags 'always)
5855 (and (listp org-agenda-show-inherited-tags)
5856 (memq 'todo org-agenda-show-inherited-tags))
5857 (and (eq org-agenda-show-inherited-tags t)
5858 (or (eq org-agenda-use-tag-inheritance t)
5859 (memq 'todo org-agenda-use-tag-inheritance))))
5860 tags (org-get-tags-at nil (not inherited-tags))
5861 level (make-string (org-reduced-level (org-outline-level)) ? ))
5862 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5863 (setq txt (match-string 1))
5864 (when extra
5865 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5866 (setq txt (concat (substring txt 0 (match-beginning 1))
5867 " - " extra " " (match-string 2 txt)))
5868 (setq txt (concat txt " - " extra))))
5869 (setq txt (org-agenda-format-item
5870 (cond
5871 (closedp "Closed: ")
5872 (statep (concat "State: (" state ")"))
5873 (t (concat "Clocked: (" clocked ")")))
5874 txt level category tags timestr)))
5875 (setq priority 100000)
5876 (org-add-props txt props
5877 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5878 'priority priority 'level level
5879 'type "closed" 'date date
5880 'undone-face 'org-warning 'done-face 'org-agenda-done)
5881 (push txt ee))
5882 (goto-char (point-at-eol))))
5883 (nreverse ee)))
5885 (defun org-agenda-show-clocking-issues ()
5886 "Add overlays, showing issues with clocking.
5887 See also the user option `org-agenda-clock-consistency-checks'."
5888 (interactive)
5889 (let* ((org-time-clocksum-use-effort-durations nil)
5890 (pl org-agenda-clock-consistency-checks)
5891 (re (concat "^[ \t]*"
5892 org-clock-string
5893 "[ \t]+"
5894 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5895 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5896 (tlstart 0.)
5897 (tlend 0.)
5898 (maxtime (org-hh:mm-string-to-minutes
5899 (or (plist-get pl :max-duration) "24:00")))
5900 (mintime (org-hh:mm-string-to-minutes
5901 (or (plist-get pl :min-duration) 0)))
5902 (maxgap (org-hh:mm-string-to-minutes
5903 ;; default 30:00 means never complain
5904 (or (plist-get pl :max-gap) "30:00")))
5905 (gapok (mapcar 'org-hh:mm-string-to-minutes
5906 (plist-get pl :gap-ok-around)))
5907 (def-face (or (plist-get pl :default-face)
5908 '((:background "DarkRed") (:foreground "white"))))
5909 issue face m te ts dt ov)
5910 (goto-char (point-min))
5911 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5912 (setq issue nil face def-face)
5913 (catch 'next
5914 (setq m (org-get-at-bol 'org-marker)
5915 te nil ts nil)
5916 (unless (and m (markerp m))
5917 (setq issue "No valid clock line") (throw 'next t))
5918 (org-with-point-at m
5919 (save-excursion
5920 (goto-char (point-at-bol))
5921 (unless (looking-at re)
5922 (error "No valid Clock line")
5923 (throw 'next t))
5924 (unless (match-end 3)
5925 (setq issue "No end time"
5926 face (or (plist-get pl :no-end-time-face) face))
5927 (throw 'next t))
5928 (setq ts (match-string 1)
5929 te (match-string 3)
5930 ts (org-float-time
5931 (apply 'encode-time (org-parse-time-string ts)))
5932 te (org-float-time
5933 (apply 'encode-time (org-parse-time-string te)))
5934 dt (- te ts))))
5935 (cond
5936 ((> dt (* 60 maxtime))
5937 ;; a very long clocking chunk
5938 (setq issue (format "Clocking interval is very long: %s"
5939 (org-minutes-to-clocksum-string
5940 (floor (/ (float dt) 60.))))
5941 face (or (plist-get pl :long-face) face)))
5942 ((< dt (* 60 mintime))
5943 ;; a very short clocking chunk
5944 (setq issue (format "Clocking interval is very short: %s"
5945 (org-minutes-to-clocksum-string
5946 (floor (/ (float dt) 60.))))
5947 face (or (plist-get pl :short-face) face)))
5948 ((and (> tlend 0) (< ts tlend))
5949 ;; Two clock entries are overlapping
5950 (setq issue (format "Clocking overlap: %d minutes"
5951 (/ (- tlend ts) 60))
5952 face (or (plist-get pl :overlap-face) face)))
5953 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5954 ;; There is a gap, lets see if we need to report it
5955 (unless (org-agenda-check-clock-gap tlend ts gapok)
5956 (setq issue (format "Clocking gap: %d minutes"
5957 (/ (- ts tlend) 60))
5958 face (or (plist-get pl :gap-face) face))))
5959 (t nil)))
5960 (setq tlend (or te tlend) tlstart (or ts tlstart))
5961 (when issue
5962 ;; OK, there was some issue, add an overlay to show the issue
5963 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5964 (overlay-put ov 'before-string
5965 (concat
5966 (org-add-props
5967 (format "%-43s" (concat " " issue))
5969 'face face)
5970 "\n"))
5971 (overlay-put ov 'evaporate t)))))
5973 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5974 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5975 (catch 'exit
5976 (unless ok-list
5977 ;; there are no OK times for gaps...
5978 (throw 'exit nil))
5979 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5980 ;; This is more than 24 hours, so it is OK.
5981 ;; because we have at least one OK time, that must be in the
5982 ;; 24 hour interval.
5983 (throw 'exit t))
5984 ;; We have a shorter gap.
5985 ;; Now we have to get the minute of the day when these times are
5986 (let* ((t1dec (decode-time (seconds-to-time t1)))
5987 (t2dec (decode-time (seconds-to-time t2)))
5988 ;; compute the minute on the day
5989 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5990 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5991 (when (< min2 min1)
5992 ;; if min2 is smaller than min1, this means it is on the next day.
5993 ;; Wrap it to after midnight.
5994 (setq min2 (+ min2 1440)))
5995 ;; Now check if any of the OK times is in the gap
5996 (mapc (lambda (x)
5997 ;; Wrap the time to after midnight if necessary
5998 (if (< x min1) (setq x (+ x 1440)))
5999 ;; Check if in interval
6000 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6001 ok-list)
6002 ;; Nope, this gap is not OK
6003 nil)))
6005 (defun org-agenda-get-deadlines (&optional with-hour)
6006 "Return the deadline information for agenda display.
6007 When WITH-HOUR is non-nil, only return deadlines with an hour
6008 specification like [h]h:mm."
6009 (let* ((props (list 'mouse-face 'highlight
6010 'org-not-done-regexp org-not-done-regexp
6011 'org-todo-regexp org-todo-regexp
6012 'org-complex-heading-regexp org-complex-heading-regexp
6013 'help-echo
6014 (format "mouse-2 or RET jump to org file %s"
6015 (abbreviate-file-name buffer-file-name))))
6016 (regexp (if with-hour
6017 org-deadline-time-hour-regexp
6018 org-deadline-time-regexp))
6019 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6020 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6021 (dl0 (car org-agenda-deadline-leaders))
6022 (dl1 (nth 1 org-agenda-deadline-leaders))
6023 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6024 d2 diff dfrac wdays pos pos1 category level
6025 tags suppress-prewarning ee txt head face s todo-state
6026 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6027 (goto-char (point-min))
6028 (while (re-search-forward regexp nil t)
6029 (catch :skip
6030 (org-agenda-skip)
6031 (setq s (match-string 1)
6032 txt nil
6033 pos (1- (match-beginning 1))
6034 todo-state (save-match-data (org-get-todo-state))
6035 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6036 (member todo-state
6037 org-agenda-repeating-timestamp-show-all))
6038 d2 (org-time-string-to-absolute
6039 s d1 'past show-all (current-buffer) pos)
6040 diff (- d2 d1))
6041 (setq suppress-prewarning
6042 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6043 (let ((item (buffer-substring (point-at-bol)
6044 (point-at-eol))))
6045 (save-match-data
6046 (and (string-match
6047 org-scheduled-time-regexp item)
6048 (match-string 1 item)))))))
6049 (cond
6050 ((not ds) nil)
6051 ;; The current item has a scheduled date (in ds), so
6052 ;; evaluate its prewarning lead time.
6053 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6054 ;; Use global prewarning-restart lead time.
6055 org-agenda-skip-deadline-prewarning-if-scheduled)
6056 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6057 'pre-scheduled)
6058 ;; Set prewarning to no earlier than scheduled.
6059 (min (- d2 (org-time-string-to-absolute
6060 ds d1 'past show-all (current-buffer) pos))
6061 org-deadline-warning-days))
6062 ;; Set prewarning to deadline.
6063 (t 0))))
6064 (setq wdays (if suppress-prewarning
6065 (let ((org-deadline-warning-days suppress-prewarning))
6066 (org-get-wdays s))
6067 (org-get-wdays s))
6068 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6069 upcomingp (and todayp (> diff 0)))
6070 ;; When to show a deadline in the calendar:
6071 ;; If the expiration is within wdays warning time.
6072 ;; Past-due deadlines are only shown on the current date
6073 (if (and (or (and (<= diff wdays)
6074 (and todayp (not org-agenda-only-exact-dates)))
6075 (= diff 0)))
6076 (save-excursion
6077 ;; (setq todo-state (org-get-todo-state))
6078 (setq donep (member todo-state org-done-keywords))
6079 (if (and donep
6080 (or org-agenda-skip-deadline-if-done
6081 (not (= diff 0))))
6082 (setq txt nil)
6083 (setq category (org-get-category)
6084 warntime (get-text-property (point) 'org-appt-warntime))
6085 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6086 (throw :skip nil)
6087 (goto-char (match-end 0))
6088 (setq pos1 (match-beginning 0))
6089 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6090 (setq inherited-tags
6091 (or (eq org-agenda-show-inherited-tags 'always)
6092 (and (listp org-agenda-show-inherited-tags)
6093 (memq 'agenda org-agenda-show-inherited-tags))
6094 (and (eq org-agenda-show-inherited-tags t)
6095 (or (eq org-agenda-use-tag-inheritance t)
6096 (memq 'agenda org-agenda-use-tag-inheritance))))
6097 tags (org-get-tags-at pos1 (not inherited-tags)))
6098 (setq head (buffer-substring
6099 (point)
6100 (progn (skip-chars-forward "^\r\n")
6101 (point))))
6102 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6103 (setq timestr
6104 (concat (substring s (match-beginning 1)) " "))
6105 (setq timestr 'time))
6106 (setq txt (org-agenda-format-item
6107 (cond ((= diff 0) dl0)
6108 ((> diff 0)
6109 (if (functionp dl1)
6110 (funcall dl1 diff date)
6111 (format dl1 diff)))
6113 (if (functionp dl2)
6114 (funcall dl2 diff date)
6115 (format dl2 (if (string= dl2 dl1)
6116 diff (abs diff))))))
6117 head level category tags
6118 (if (not (= diff 0)) nil timestr)))))
6119 (when txt
6120 (setq face (org-agenda-deadline-face dfrac))
6121 (org-add-props txt props
6122 'org-marker (org-agenda-new-marker pos)
6123 'warntime warntime
6124 'level level
6125 'ts-date d2
6126 'org-hd-marker (org-agenda-new-marker pos1)
6127 'priority (+ (- diff)
6128 (org-get-priority txt))
6129 'todo-state todo-state
6130 'type (if upcomingp "upcoming-deadline" "deadline")
6131 'date (if upcomingp date d2)
6132 'face (if donep 'org-agenda-done face)
6133 'undone-face face 'done-face 'org-agenda-done)
6134 (push txt ee))))))
6135 (nreverse ee)))
6137 (defun org-agenda-deadline-face (fraction)
6138 "Return the face to displaying a deadline item.
6139 FRACTION is what fraction of the head-warning time has passed."
6140 (let ((faces org-agenda-deadline-faces) f)
6141 (catch 'exit
6142 (while (setq f (pop faces))
6143 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6145 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6146 "Return the scheduled information for agenda display.
6147 When WITH-HOUR is non-nil, only return scheduled items with
6148 an hour specification like [h]h:mm."
6149 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6150 'org-todo-regexp org-todo-regexp
6151 'org-complex-heading-regexp org-complex-heading-regexp
6152 'done-face 'org-agenda-done
6153 'mouse-face 'highlight
6154 'help-echo
6155 (format "mouse-2 or RET jump to org file %s"
6156 (abbreviate-file-name buffer-file-name))))
6157 (regexp (if with-hour
6158 org-scheduled-time-hour-regexp
6159 org-scheduled-time-regexp))
6160 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6161 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6163 (deadline-position-alist
6164 (mapcar (lambda (a) (and (setq mm (get-text-property
6165 0 'org-hd-marker a))
6166 (cons (marker-position mm) a)))
6167 deadline-results))
6168 d2 diff pos pos1 category level tags donep
6169 ee txt head pastschedp todo-state face timestr s habitp show-all
6170 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6171 ddays)
6172 (goto-char (point-min))
6173 (while (re-search-forward regexp nil t)
6174 (catch :skip
6175 (org-agenda-skip)
6176 (setq s (match-string 1)
6177 txt nil
6178 pos (1- (match-beginning 1))
6179 todo-state (save-match-data (org-get-todo-state))
6180 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6181 (member todo-state
6182 org-agenda-repeating-timestamp-show-all))
6183 d2 (org-time-string-to-absolute
6184 s d1 'past show-all (current-buffer) pos)
6185 diff (- d2 d1)
6186 warntime (get-text-property (point) 'org-appt-warntime))
6187 (setq pastschedp (and todayp (< diff 0)))
6188 (setq did-habit-check-p nil)
6189 (setq suppress-delay
6190 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6191 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6192 (save-match-data
6193 (and (string-match
6194 org-deadline-time-regexp item)
6195 (match-string 1 item)))))))
6196 (cond
6197 ((not ds) nil)
6198 ;; The current item has a deadline date (in ds), so
6199 ;; evaluate its delay time.
6200 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6201 ;; Use global delay time.
6202 (- org-agenda-skip-scheduled-delay-if-deadline))
6203 ((eq org-agenda-skip-scheduled-delay-if-deadline
6204 'post-deadline)
6205 ;; Set delay to no later than deadline.
6206 (min (- d2 (org-time-string-to-absolute
6207 ds d1 'past show-all (current-buffer) pos))
6208 org-scheduled-delay-days))
6209 (t 0))))
6210 (setq ddays (if suppress-delay
6211 (let ((org-scheduled-delay-days suppress-delay))
6212 (org-get-wdays s t t))
6213 (org-get-wdays s t)))
6214 ;; Use a delay of 0 when there is a repeater and the delay is
6215 ;; of the form --3d
6216 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6217 (< (org-time-string-to-absolute s)
6218 (org-time-string-to-absolute
6219 s d2 'past nil (current-buffer) pos)))
6220 (setq ddays 0))
6221 ;; When to show a scheduled item in the calendar:
6222 ;; If it is on or past the date.
6223 (when (or (and (> ddays 0) (= diff (- ddays)))
6224 (and (zerop ddays) (= diff 0))
6225 (and (< (+ diff ddays) 0)
6226 (< (abs diff) org-scheduled-past-days)
6227 (and todayp (not org-agenda-only-exact-dates)))
6228 ;; org-is-habit-p uses org-entry-get, which is expansive
6229 ;; so we go extra mile to only call it once
6230 (and todayp
6231 (boundp 'org-habit-show-all-today)
6232 org-habit-show-all-today
6233 (setq did-habit-check-p t)
6234 (setq habitp (and (functionp 'org-is-habit-p)
6235 (org-is-habit-p)))))
6236 (save-excursion
6237 (setq donep (member todo-state org-done-keywords))
6238 (if (and donep
6239 (or org-agenda-skip-scheduled-if-done
6240 (not (= diff 0))
6241 (and (functionp 'org-is-habit-p)
6242 (org-is-habit-p))))
6243 (setq txt nil)
6244 (setq habitp (if did-habit-check-p habitp
6245 (and (functionp 'org-is-habit-p)
6246 (org-is-habit-p))))
6247 (setq category (org-get-category))
6248 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6249 'repeated-after-deadline)
6250 (org-get-deadline-time (point))
6251 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6252 (throw :skip nil))
6253 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6254 (throw :skip nil)
6255 (goto-char (match-end 0))
6256 (setq pos1 (match-beginning 0))
6257 (if habitp
6258 (if (or (not org-habit-show-habits)
6259 (and (not todayp)
6260 (boundp 'org-habit-show-habits-only-for-today)
6261 org-habit-show-habits-only-for-today))
6262 (throw :skip nil))
6263 (if (and
6264 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6265 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6266 pastschedp))
6267 (setq mm (assoc pos1 deadline-position-alist)))
6268 (throw :skip nil)))
6269 (setq inherited-tags
6270 (or (eq org-agenda-show-inherited-tags 'always)
6271 (and (listp org-agenda-show-inherited-tags)
6272 (memq 'agenda org-agenda-show-inherited-tags))
6273 (and (eq org-agenda-show-inherited-tags t)
6274 (or (eq org-agenda-use-tag-inheritance t)
6275 (memq 'agenda org-agenda-use-tag-inheritance))))
6277 tags (org-get-tags-at nil (not inherited-tags)))
6278 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6279 (setq head (buffer-substring
6280 (point)
6281 (progn (skip-chars-forward "^\r\n") (point))))
6282 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6283 (setq timestr
6284 (concat (substring s (match-beginning 1)) " "))
6285 (setq timestr 'time))
6286 (setq txt (org-agenda-format-item
6287 (if (= diff 0)
6288 (car org-agenda-scheduled-leaders)
6289 (format (nth 1 org-agenda-scheduled-leaders)
6290 (- 1 diff)))
6291 head level category tags
6292 (if (not (= diff 0)) nil timestr)
6293 nil habitp))))
6294 (when txt
6295 (setq face
6296 (cond
6297 ((and (not habitp) pastschedp)
6298 'org-scheduled-previously)
6299 (todayp 'org-scheduled-today)
6300 (t 'org-scheduled))
6301 habitp (and habitp (org-habit-parse-todo)))
6302 (org-add-props txt props
6303 'undone-face face
6304 'face (if donep 'org-agenda-done face)
6305 'org-marker (org-agenda-new-marker pos)
6306 'org-hd-marker (org-agenda-new-marker pos1)
6307 'type (if pastschedp "past-scheduled" "scheduled")
6308 'date (if pastschedp d2 date)
6309 'ts-date d2
6310 'warntime warntime
6311 'level level
6312 'priority (if habitp
6313 (org-habit-get-priority habitp)
6314 (+ 94 (- 5 diff) (org-get-priority txt)))
6315 'org-habit-p habitp
6316 'todo-state todo-state)
6317 (push txt ee))))))
6318 (nreverse ee)))
6320 (defun org-agenda-get-blocks ()
6321 "Return the date-range information for agenda display."
6322 (let* ((props (list 'face nil
6323 'org-not-done-regexp org-not-done-regexp
6324 'org-todo-regexp org-todo-regexp
6325 'org-complex-heading-regexp org-complex-heading-regexp
6326 'mouse-face 'highlight
6327 'help-echo
6328 (format "mouse-2 or RET jump to org file %s"
6329 (abbreviate-file-name buffer-file-name))))
6330 (regexp org-tr-regexp)
6331 (d0 (calendar-absolute-from-gregorian date))
6332 marker hdmarker ee txt d1 d2 s1 s2 category
6333 level todo-state tags pos head donep inherited-tags)
6334 (goto-char (point-min))
6335 (while (re-search-forward regexp nil t)
6336 (catch :skip
6337 (org-agenda-skip)
6338 (setq pos (point))
6339 (let ((start-time (match-string 1))
6340 (end-time (match-string 2)))
6341 (setq s1 (match-string 1)
6342 s2 (match-string 2)
6343 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6344 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6345 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6346 ;; Only allow days between the limits, because the normal
6347 ;; date stamps will catch the limits.
6348 (save-excursion
6349 (setq todo-state (org-get-todo-state))
6350 (setq donep (member todo-state org-done-keywords))
6351 (if (and donep org-agenda-skip-timestamp-if-done)
6352 (throw :skip t))
6353 (setq marker (org-agenda-new-marker (point))
6354 category (org-get-category))
6355 (if (not (re-search-backward org-outline-regexp-bol nil t))
6356 (throw :skip nil)
6357 (goto-char (match-beginning 0))
6358 (setq hdmarker (org-agenda-new-marker (point))
6359 inherited-tags
6360 (or (eq org-agenda-show-inherited-tags 'always)
6361 (and (listp org-agenda-show-inherited-tags)
6362 (memq 'agenda org-agenda-show-inherited-tags))
6363 (and (eq org-agenda-show-inherited-tags t)
6364 (or (eq org-agenda-use-tag-inheritance t)
6365 (memq 'agenda org-agenda-use-tag-inheritance))))
6367 tags (org-get-tags-at nil (not inherited-tags)))
6368 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6369 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6370 (setq head (match-string 1))
6371 (let ((remove-re
6372 (if org-agenda-remove-timeranges-from-blocks
6373 (concat
6374 "<" (regexp-quote s1) ".*?>"
6375 "--"
6376 "<" (regexp-quote s2) ".*?>")
6377 nil)))
6378 (setq txt (org-agenda-format-item
6379 (format
6380 (nth (if (= d1 d2) 0 1)
6381 org-agenda-timerange-leaders)
6382 (1+ (- d0 d1)) (1+ (- d2 d1)))
6383 head level category tags
6384 (cond ((and (= d1 d0) (= d2 d0))
6385 (concat "<" start-time ">--<" end-time ">"))
6386 ((= d1 d0)
6387 (concat "<" start-time ">"))
6388 ((= d2 d0)
6389 (concat "<" end-time ">")))
6390 remove-re))))
6391 (org-add-props txt props
6392 'org-marker marker 'org-hd-marker hdmarker
6393 'type "block" 'date date
6394 'level level
6395 'todo-state todo-state
6396 'priority (org-get-priority txt))
6397 (push txt ee))))
6398 (goto-char pos)))
6399 ;; Sort the entries by expiration date.
6400 (nreverse ee)))
6402 ;;; Agenda presentation and sorting
6404 (defvar org-prefix-has-time nil
6405 "A flag, set by `org-compile-prefix-format'.
6406 The flag is set if the currently compiled format contains a `%t'.")
6407 (defvar org-prefix-has-tag nil
6408 "A flag, set by `org-compile-prefix-format'.
6409 The flag is set if the currently compiled format contains a `%T'.")
6410 (defvar org-prefix-has-effort nil
6411 "A flag, set by `org-compile-prefix-format'.
6412 The flag is set if the currently compiled format contains a `%e'.")
6413 (defvar org-prefix-has-breadcrumbs nil
6414 "A flag, set by `org-compile-prefix-format'.
6415 The flag is set if the currently compiled format contains a `%b'.")
6416 (defvar org-prefix-category-length nil
6417 "Used by `org-compile-prefix-format' to remember the category field width.")
6418 (defvar org-prefix-category-max-length nil
6419 "Used by `org-compile-prefix-format' to remember the category field width.")
6421 (defun org-agenda-get-category-icon (category)
6422 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6423 (dolist (entry org-agenda-category-icon-alist)
6424 (when (org-string-match-p (car entry) category)
6425 (if (listp (cadr entry))
6426 (return (cadr entry))
6427 (return (apply 'create-image (cdr entry)))))))
6429 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6430 remove-re habitp)
6431 "Format TXT to be inserted into the agenda buffer.
6432 In particular, add the prefix and corresponding text properties.
6434 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6435 LEVEL may be a string to replace the `%l' specifier.
6436 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6437 category taken from local variable or file name. It will replace the `%c'
6438 specifier in the format.
6439 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6440 TXT for sorting of this entry, and for the `%t' specifier in the format.
6441 When DOTIME is a string, this string is searched for a time before TXT is.
6442 TAGS can be the tags of the headline.
6443 Any match of REMOVE-RE will be removed from TXT."
6444 ;; We keep the org-prefix-* variable values along with a compiled
6445 ;; formatter, so that multiple agendas existing at the same time do
6446 ;; not step on each other toes.
6448 ;; It was inconvenient to make these variables buffer local in
6449 ;; Agenda buffers, because this function expects to be called with
6450 ;; the buffer where item comes from being current, and not agenda
6451 ;; buffer
6452 (let* ((bindings (car org-prefix-format-compiled))
6453 (formatter (cadr org-prefix-format-compiled)))
6454 (loop for (var value) in bindings
6455 do (set var value))
6456 (save-match-data
6457 ;; Diary entries sometimes have extra whitespace at the beginning
6458 (setq txt (org-trim txt))
6460 ;; Fix the tags part in txt
6461 (setq txt (org-agenda-fix-displayed-tags
6462 txt tags
6463 org-agenda-show-inherited-tags
6464 org-agenda-hide-tags-regexp))
6466 (let* ((category (or category
6467 (if buffer-file-name
6468 (file-name-sans-extension
6469 (file-name-nondirectory buffer-file-name))
6470 "")))
6471 (category-icon (org-agenda-get-category-icon category))
6472 (category-icon (if category-icon
6473 (propertize " " 'display category-icon)
6474 ""))
6475 (effort (and (not (string= txt ""))
6476 (get-text-property 1 'effort txt)))
6477 ;; time, tag, effort are needed for the eval of the prefix format
6478 (tag (if tags (nth (1- (length tags)) tags) ""))
6479 time
6480 (ts (if dotime (concat
6481 (if (stringp dotime) dotime "")
6482 (and org-agenda-search-headline-for-time txt))))
6483 (time-of-day (and dotime (org-get-time-of-day ts)))
6484 stamp plain s0 s1 s2 rtn srp l
6485 duration breadcrumbs)
6486 (and (derived-mode-p 'org-mode) buffer-file-name
6487 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6488 (when (and dotime time-of-day)
6489 ;; Extract starting and ending time and move them to prefix
6490 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6491 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6492 (setq s0 (match-string 0 ts)
6493 srp (and stamp (match-end 3))
6494 s1 (match-string (if plain 1 2) ts)
6495 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6497 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6498 ;; them, we might want to remove them there to avoid duplication.
6499 ;; The user can turn this off with a variable.
6500 (if (and org-prefix-has-time
6501 org-agenda-remove-times-when-in-prefix (or stamp plain)
6502 (string-match (concat (regexp-quote s0) " *") txt)
6503 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6504 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6505 (= (match-beginning 0) 0)
6507 (setq txt (replace-match "" nil nil txt))))
6508 ;; Normalize the time(s) to 24 hour
6509 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6510 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6512 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6513 (let (org-time-clocksum-use-effort-durations)
6514 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6515 (setq s2
6516 (org-minutes-to-clocksum-string
6517 (+ (org-hh:mm-string-to-minutes s1)
6518 org-agenda-default-appointment-duration)))))
6520 ;; Compute the duration
6521 (when s2
6522 (setq duration (- (org-hh:mm-string-to-minutes s2)
6523 (org-hh:mm-string-to-minutes s1)))))
6525 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6526 txt)
6527 ;; Tags are in the string
6528 (if (or (eq org-agenda-remove-tags t)
6529 (and org-agenda-remove-tags
6530 org-prefix-has-tag))
6531 (setq txt (replace-match "" t t txt))
6532 (setq txt (replace-match
6533 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6534 (match-string 2 txt))
6535 t t txt))))
6537 (when remove-re
6538 (while (string-match remove-re txt)
6539 (setq txt (replace-match "" t t txt))))
6541 ;; Set org-heading property on `txt' to mark the start of the
6542 ;; heading.
6543 (add-text-properties 0 (length txt) '(org-heading t) txt)
6545 ;; Prepare the variables needed in the eval of the compiled format
6546 (if org-prefix-has-breadcrumbs
6547 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6548 (let ((s (org-display-outline-path nil nil "->" t)))
6549 (if (eq "" s) "" (concat s "->"))))))
6550 (setq time (cond (s2 (concat
6551 (org-agenda-time-of-day-to-ampm-maybe s1)
6552 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6553 (if org-agenda-timegrid-use-ampm " ")))
6554 (s1 (concat
6555 (org-agenda-time-of-day-to-ampm-maybe s1)
6556 (if org-agenda-timegrid-use-ampm
6557 "........ "
6558 "......")))
6559 (t ""))
6560 extra (or (and (not habitp) extra) "")
6561 category (if (symbolp category) (symbol-name category) category)
6562 level (or level ""))
6563 (if (string-match org-bracket-link-regexp category)
6564 (progn
6565 (setq l (if (match-end 3)
6566 (- (match-end 3) (match-beginning 3))
6567 (- (match-end 1) (match-beginning 1))))
6568 (when (< l (or org-prefix-category-length 0))
6569 (setq category (copy-sequence category))
6570 (org-add-props category nil
6571 'extra-space (make-string
6572 (- org-prefix-category-length l 1) ?\ ))))
6573 (if (and org-prefix-category-max-length
6574 (>= (length category) org-prefix-category-max-length))
6575 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6576 ;; Evaluate the compiled format
6577 (setq rtn (concat (eval formatter) txt))
6579 ;; And finally add the text properties
6580 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6581 (org-add-props rtn nil
6582 'org-category category
6583 'tags (mapcar 'org-downcase-keep-props tags)
6584 'org-highest-priority org-highest-priority
6585 'org-lowest-priority org-lowest-priority
6586 'time-of-day time-of-day
6587 'duration duration
6588 'breadcrumbs breadcrumbs
6589 'txt txt
6590 'level level
6591 'time time
6592 'extra extra
6593 'format org-prefix-format-compiled
6594 'dotime dotime)))))
6596 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6597 "Remove tags string from TXT, and add a modified list of tags.
6598 The modified list may contain inherited tags, and tags matched by
6599 `org-agenda-hide-tags-regexp' will be removed."
6600 (when (or add-inherited hide-re)
6601 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6602 (setq txt (substring txt 0 (match-beginning 0))))
6603 (setq tags
6604 (delq nil
6605 (mapcar (lambda (tg)
6606 (if (or (and hide-re (string-match hide-re tg))
6607 (and (not add-inherited)
6608 (get-text-property 0 'inherited tg)))
6610 tg))
6611 tags)))
6612 (when tags
6613 (let ((have-i (get-text-property 0 'inherited (car tags)))
6615 (setq txt (concat txt " :"
6616 (mapconcat
6617 (lambda (x)
6618 (setq i (get-text-property 0 'inherited x))
6619 (if (and have-i (not i))
6620 (progn
6621 (setq have-i nil)
6622 (concat ":" x))
6624 tags ":")
6625 (if have-i "::" ":"))))))
6626 txt)
6628 (defun org-downcase-keep-props (s)
6629 (let ((props (text-properties-at 0 s)))
6630 (setq s (downcase s))
6631 (add-text-properties 0 (length s) props s)
6634 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6636 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6637 "Add a time-grid for agenda items which need it.
6639 LIST is the list of agenda items formatted by `org-agenda-list'.
6640 NDAYS is the span of the current agenda view.
6641 TODAYP is `t' when the current agenda view is on today."
6642 (catch 'exit
6643 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6644 ((and todayp (member 'today (car org-agenda-time-grid))))
6645 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6646 ((member 'weekly (car org-agenda-time-grid)))
6647 (t (throw 'exit list)))
6648 (let* ((have (delq nil (mapcar
6649 (lambda (x) (get-text-property 1 'time-of-day x))
6650 list)))
6651 (string (nth 1 org-agenda-time-grid))
6652 (gridtimes (nth 2 org-agenda-time-grid))
6653 (req (car org-agenda-time-grid))
6654 (remove (member 'remove-match req))
6655 new time)
6656 (if (and (member 'require-timed req) (not have))
6657 ;; don't show empty grid
6658 (throw 'exit list))
6659 (while (setq time (pop gridtimes))
6660 (unless (and remove (member time have))
6661 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6662 (push (org-agenda-format-item
6663 nil string nil "" nil
6664 (concat (substring time 0 -2) ":" (substring time -2)))
6665 new)
6666 (put-text-property
6667 2 (length (car new)) 'face 'org-time-grid (car new))))
6668 (when (and todayp org-agenda-show-current-time-in-grid)
6669 (push (org-agenda-format-item
6670 nil org-agenda-current-time-string nil "" nil
6671 (format-time-string "%H:%M "))
6672 new)
6673 (put-text-property
6674 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6676 (if (member 'time-up org-agenda-sorting-strategy-selected)
6677 (append new list)
6678 (append list new)))))
6680 (defun org-compile-prefix-format (key)
6681 "Compile the prefix format into a Lisp form that can be evaluated.
6682 The resulting form and associated variable bindings is returned
6683 and stored in the variable `org-prefix-format-compiled'."
6684 (setq org-prefix-has-time nil
6685 org-prefix-has-tag nil
6686 org-prefix-category-length nil
6687 org-prefix-has-effort nil
6688 org-prefix-has-breadcrumbs nil)
6689 (let ((s (cond
6690 ((stringp org-agenda-prefix-format)
6691 org-agenda-prefix-format)
6692 ((assq key org-agenda-prefix-format)
6693 (cdr (assq key org-agenda-prefix-format)))
6694 (t " %-12:c%?-12t% s")))
6695 (start 0)
6696 varform vars var e c f opt)
6697 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6698 s start)
6699 (setq var (or (cdr (assoc (match-string 4 s)
6700 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6701 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6702 'eval)
6703 c (or (match-string 3 s) "")
6704 opt (match-beginning 1)
6705 start (1+ (match-beginning 0)))
6706 (if (equal var 'time) (setq org-prefix-has-time t))
6707 (if (equal var 'tag) (setq org-prefix-has-tag t))
6708 (if (equal var 'effort) (setq org-prefix-has-effort t))
6709 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6710 (setq f (concat "%" (match-string 2 s) "s"))
6711 (when (equal var 'category)
6712 (setq org-prefix-category-length
6713 (floor (abs (string-to-number (match-string 2 s)))))
6714 (setq org-prefix-category-max-length
6715 (let ((x (match-string 2 s)))
6716 (save-match-data
6717 (if (string-match "\\.[0-9]+" x)
6718 (string-to-number (substring (match-string 0 x) 1)))))))
6719 (if (eq var 'eval)
6720 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6721 (if opt
6722 (setq varform
6723 `(if (or (equal "" ,var) (equal nil ,var))
6725 (format ,f (concat ,var ,c))))
6726 (setq varform
6727 `(format ,f (if (or (equal ,var "")
6728 (equal ,var nil)) ""
6729 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6730 (setq s (replace-match "%s" t nil s))
6731 (push varform vars))
6732 (setq vars (nreverse vars))
6733 (with-current-buffer (or org-agenda-buffer (current-buffer))
6734 (setq org-prefix-format-compiled
6735 (list
6736 `((org-prefix-has-time ,org-prefix-has-time)
6737 (org-prefix-has-tag ,org-prefix-has-tag)
6738 (org-prefix-category-length ,org-prefix-category-length)
6739 (org-prefix-has-effort ,org-prefix-has-effort)
6740 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6741 `(format ,s ,@vars))))))
6743 (defun org-set-sorting-strategy (key)
6744 (if (symbolp (car org-agenda-sorting-strategy))
6745 ;; the old format
6746 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6747 (setq org-agenda-sorting-strategy-selected
6748 (or (cdr (assq key org-agenda-sorting-strategy))
6749 (cdr (assq 'agenda org-agenda-sorting-strategy))
6750 '(time-up category-keep priority-down)))))
6752 (defun org-get-time-of-day (s &optional string mod24)
6753 "Check string S for a time of day.
6754 If found, return it as a military time number between 0 and 2400.
6755 If not found, return nil.
6756 The optional STRING argument forces conversion into a 5 character wide string
6757 HH:MM."
6758 (save-match-data
6759 (when
6760 (and
6761 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6762 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6763 (not (eq (get-text-property 1 'face s) 'org-link)))
6764 (let* ((h (string-to-number (match-string 1 s)))
6765 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6766 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6767 (am-p (equal ampm "am"))
6768 (h1 (cond ((not ampm) h)
6769 ((= h 12) (if am-p 0 12))
6770 (t (+ h (if am-p 0 12)))))
6771 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6772 (mod h1 24) h1))
6773 (t0 (+ (* 100 h2) m))
6774 (t1 (concat (if (>= h1 24) "+" " ")
6775 (if (and org-agenda-time-leading-zero
6776 (< t0 1000)) "0" "")
6777 (if (< t0 100) "0" "")
6778 (if (< t0 10) "0" "")
6779 (int-to-string t0))))
6780 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6782 (defvar org-agenda-before-sorting-filter-function nil
6783 "Function to be applied to agenda items prior to sorting.
6784 Prior to sorting also means just before they are inserted into the agenda.
6786 To aid sorting, you may revisit the original entries and add more text
6787 properties which will later be used by the sorting functions.
6789 The function should take a string argument, an agenda line.
6790 It has access to the text properties in that line, which contain among
6791 other things, the property `org-hd-marker' that points to the entry
6792 where the line comes from. Note that not all lines going into the agenda
6793 have this property, only most.
6795 The function should return the modified string. It is probably best
6796 to ONLY change text properties.
6798 You can also use this function as a filter, by returning nil for lines
6799 you don't want to have in the agenda at all. For this application, you
6800 could bind the variable in the options section of a custom command.")
6802 (defun org-agenda-finalize-entries (list &optional type)
6803 "Sort, limit and concatenate the LIST of agenda items.
6804 The optional argument TYPE tells the agenda type."
6805 (let ((max-effort (cond ((listp org-agenda-max-effort)
6806 (cdr (assoc type org-agenda-max-effort)))
6807 (t org-agenda-max-effort)))
6808 (max-todo (cond ((listp org-agenda-max-todos)
6809 (cdr (assoc type org-agenda-max-todos)))
6810 (t org-agenda-max-todos)))
6811 (max-tags (cond ((listp org-agenda-max-tags)
6812 (cdr (assoc type org-agenda-max-tags)))
6813 (t org-agenda-max-tags)))
6814 (max-entries (cond ((listp org-agenda-max-entries)
6815 (cdr (assoc type org-agenda-max-entries)))
6816 (t org-agenda-max-entries))))
6817 (when org-agenda-before-sorting-filter-function
6818 (setq list
6819 (delq nil
6820 (mapcar
6821 org-agenda-before-sorting-filter-function list))))
6822 (setq list (mapcar 'org-agenda-highlight-todo list)
6823 list (mapcar 'identity (sort list 'org-entries-lessp)))
6824 (when max-effort
6825 (setq list (org-agenda-limit-entries
6826 list 'effort-minutes max-effort
6827 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6828 32767 -1))))))
6829 (when max-todo
6830 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6831 (when max-tags
6832 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6833 (when max-entries
6834 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6835 (mapconcat 'identity list "\n")))
6837 (defun org-agenda-limit-entries (list prop limit &optional fn)
6838 "Limit the number of agenda entries."
6839 (let ((include (and limit (< limit 0))))
6840 (if limit
6841 (let ((fun (or fn (lambda (p) (if p 1))))
6842 (lim 0))
6843 (delq nil
6844 (mapcar
6845 (lambda (e)
6846 (let ((pval (funcall
6847 fun (get-text-property (1- (length e))
6848 prop e))))
6849 (if pval (setq lim (+ lim pval)))
6850 (cond ((and pval (<= lim (abs limit))) e)
6851 ((and include (not pval)) e))))
6852 list)))
6853 list)))
6855 (defun org-agenda-limit-interactively (remove)
6856 "In agenda, interactively limit entries to various maximums."
6857 (interactive "P")
6858 (if remove
6859 (progn (setq org-agenda-max-entries nil
6860 org-agenda-max-todos nil
6861 org-agenda-max-tags nil
6862 org-agenda-max-effort nil)
6863 (org-agenda-redo))
6864 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6865 (msg (cond ((= max ?E) "How many minutes? ")
6866 ((= max ?e) "How many entries? ")
6867 ((= max ?t) "How many TODO entries? ")
6868 ((= max ?T) "How many tagged entries? ")
6869 (t (user-error "Wrong input"))))
6870 (num (string-to-number (read-from-minibuffer msg))))
6871 (cond ((equal max ?e)
6872 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6873 ((equal max ?t)
6874 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6875 ((equal max ?T)
6876 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6877 ((equal max ?E)
6878 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6879 (org-agenda-fit-window-to-buffer))
6881 (defun org-agenda-highlight-todo (x)
6882 (let ((org-done-keywords org-done-keywords-for-agenda)
6883 (case-fold-search nil)
6885 (if (eq x 'line)
6886 (save-excursion
6887 (beginning-of-line 1)
6888 (setq re (org-get-at-bol 'org-todo-regexp))
6889 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6890 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6891 (add-text-properties (match-beginning 0) (match-end 1)
6892 (list 'face (org-get-todo-face 1)))
6893 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6894 (delete-region (match-beginning 1) (1- (match-end 0)))
6895 (goto-char (match-beginning 1))
6896 (insert (format org-agenda-todo-keyword-format s)))))
6897 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6898 (setq re (get-text-property 0 'org-todo-regexp x))
6899 (when (and re
6900 ;; Test `pl' because if there's no heading content,
6901 ;; there's no point matching to highlight. Note
6902 ;; that if we didn't test `pl' first, and there
6903 ;; happened to be no keyword from `org-todo-regexp'
6904 ;; on this heading line, then the `equal' comparison
6905 ;; afterwards would spuriously succeed in the case
6906 ;; where `pl' is nil -- causing an args-out-of-range
6907 ;; error when we try to add text properties to text
6908 ;; that isn't there.
6910 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6911 x pl) pl))
6912 (add-text-properties
6913 (or (match-end 1) (match-end 0)) (match-end 0)
6914 (list 'face (org-get-todo-face (match-string 2 x)))
6916 (when (match-end 1)
6917 (setq x (concat (substring x 0 (match-end 1))
6918 (format org-agenda-todo-keyword-format
6919 (match-string 2 x))
6920 (org-add-props " " (text-properties-at 0 x))
6921 (substring x (match-end 3)))))))
6922 x)))
6924 (defsubst org-cmp-values (a b property)
6925 "Compare the numeric value of text PROPERTY for string A and B."
6926 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6927 (pb (or (get-text-property (1- (length b)) property b) 0)))
6928 (cond ((> pa pb) +1)
6929 ((< pa pb) -1))))
6931 (defsubst org-cmp-effort (a b)
6932 "Compare the effort values of string A and B."
6933 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6934 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6935 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6936 (cond ((> ea eb) +1)
6937 ((< ea eb) -1))))
6939 (defsubst org-cmp-category (a b)
6940 "Compare the string values of categories of strings A and B."
6941 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6942 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6943 (cond ((string-lessp ca cb) -1)
6944 ((string-lessp cb ca) +1))))
6946 (defsubst org-cmp-todo-state (a b)
6947 "Compare the todo states of strings A and B."
6948 (let* ((ma (or (get-text-property 1 'org-marker a)
6949 (get-text-property 1 'org-hd-marker a)))
6950 (mb (or (get-text-property 1 'org-marker b)
6951 (get-text-property 1 'org-hd-marker b)))
6952 (fa (and ma (marker-buffer ma)))
6953 (fb (and mb (marker-buffer mb)))
6954 (todo-kwds
6955 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6956 (and fb (with-current-buffer fb org-todo-keywords-1))))
6957 (ta (or (get-text-property 1 'todo-state a) ""))
6958 (tb (or (get-text-property 1 'todo-state b) ""))
6959 (la (- (length (member ta todo-kwds))))
6960 (lb (- (length (member tb todo-kwds))))
6961 (donepa (member ta org-done-keywords-for-agenda))
6962 (donepb (member tb org-done-keywords-for-agenda)))
6963 (cond ((and donepa (not donepb)) -1)
6964 ((and (not donepa) donepb) +1)
6965 ((< la lb) -1)
6966 ((< lb la) +1))))
6968 (defsubst org-cmp-alpha (a b)
6969 "Compare the headlines, alphabetically."
6970 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6971 (plb (text-property-any 0 (length b) 'org-heading t b))
6972 (ta (and pla (substring a pla)))
6973 (tb (and plb (substring b plb))))
6974 (when pla
6975 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6976 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6977 (setq ta (substring ta (match-end 0))))
6978 (setq ta (downcase ta)))
6979 (when plb
6980 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6981 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6982 (setq tb (substring tb (match-end 0))))
6983 (setq tb (downcase tb)))
6984 (cond ((not ta) +1)
6985 ((not tb) -1)
6986 ((string-lessp ta tb) -1)
6987 ((string-lessp tb ta) +1))))
6989 (defsubst org-cmp-tag (a b)
6990 "Compare the string values of the first tags of A and B."
6991 (let ((ta (car (last (get-text-property 1 'tags a))))
6992 (tb (car (last (get-text-property 1 'tags b)))))
6993 (cond ((not ta) +1)
6994 ((not tb) -1)
6995 ((string-lessp ta tb) -1)
6996 ((string-lessp tb ta) +1))))
6998 (defsubst org-cmp-time (a b)
6999 "Compare the time-of-day values of strings A and B."
7000 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7001 (ta (or (get-text-property 1 'time-of-day a) def))
7002 (tb (or (get-text-property 1 'time-of-day b) def)))
7003 (cond ((< ta tb) -1)
7004 ((< tb ta) +1))))
7006 (defsubst org-cmp-ts (a b type)
7007 "Compare the timestamps values of entries A and B.
7008 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7009 \"timestamp_ia\", compare within each of these type. When TYPE
7010 is the empty string, compare all timestamps without respect of
7011 their type."
7012 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7013 (ta (or (and (string-match type (or (get-text-property 1 type a) ""))
7014 (get-text-property 1 'ts-date a)) def))
7015 (tb (or (and (string-match type (or (get-text-property 1 type b) ""))
7016 (get-text-property 1 'ts-date b)) def)))
7017 (cond ((< ta tb) -1)
7018 ((< tb ta) +1))))
7020 (defsubst org-cmp-habit-p (a b)
7021 "Compare the todo states of strings A and B."
7022 (let ((ha (get-text-property 1 'org-habit-p a))
7023 (hb (get-text-property 1 'org-habit-p b)))
7024 (cond ((and ha (not hb)) -1)
7025 ((and (not ha) hb) +1))))
7027 (defun org-entries-lessp (a b)
7028 "Predicate for sorting agenda entries."
7029 ;; The following variables will be used when the form is evaluated.
7030 ;; So even though the compiler complains, keep them.
7031 (let* ((ss org-agenda-sorting-strategy-selected)
7032 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7033 (org-cmp-ts a b "")))
7034 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7035 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7036 (org-cmp-ts a b "scheduled")))
7037 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7038 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7039 (org-cmp-ts a b "deadline")))
7040 (deadline-down (if deadline-up (- deadline-up) nil))
7041 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7042 (org-cmp-ts a b "iatimestamp_ia")))
7043 (tsia-down (if tsia-up (- tsia-up) nil))
7044 (ts-up (and (org-em 'ts-up 'ts-down ss)
7045 (org-cmp-ts a b "timestamp")))
7046 (ts-down (if ts-up (- ts-up) nil))
7047 (time-up (and (org-em 'time-up 'time-down ss)
7048 (org-cmp-time a b)))
7049 (time-down (if time-up (- time-up) nil))
7050 (stats-up (and (org-em 'stats-up 'stats-down ss)
7051 (org-cmp-values a b 'org-stats)))
7052 (stats-down (if stats-up (- stats-up) nil))
7053 (priority-up (and (org-em 'priority-up 'priority-down ss)
7054 (org-cmp-values a b 'priority)))
7055 (priority-down (if priority-up (- priority-up) nil))
7056 (effort-up (and (org-em 'effort-up 'effort-down ss)
7057 (org-cmp-effort a b)))
7058 (effort-down (if effort-up (- effort-up) nil))
7059 (category-up (and (or (org-em 'category-up 'category-down ss)
7060 (memq 'category-keep ss))
7061 (org-cmp-category a b)))
7062 (category-down (if category-up (- category-up) nil))
7063 (category-keep (if category-up +1 nil))
7064 (tag-up (and (org-em 'tag-up 'tag-down ss)
7065 (org-cmp-tag a b)))
7066 (tag-down (if tag-up (- tag-up) nil))
7067 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7068 (org-cmp-todo-state a b)))
7069 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7070 (habit-up (and (org-em 'habit-up 'habit-down ss)
7071 (org-cmp-habit-p a b)))
7072 (habit-down (if habit-up (- habit-up) nil))
7073 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7074 (org-cmp-alpha a b)))
7075 (alpha-down (if alpha-up (- alpha-up) nil))
7076 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7077 user-defined-up user-defined-down)
7078 (if (and need-user-cmp org-agenda-cmp-user-defined
7079 (functionp org-agenda-cmp-user-defined))
7080 (setq user-defined-up
7081 (funcall org-agenda-cmp-user-defined a b)
7082 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7083 (cdr (assoc
7084 (eval (cons 'or org-agenda-sorting-strategy-selected))
7085 '((-1 . t) (1 . nil) (nil . nil))))))
7087 ;;; Agenda restriction lock
7089 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7090 "Overlay to mark the headline to which agenda commands are restricted.")
7091 (overlay-put org-agenda-restriction-lock-overlay
7092 'face 'org-agenda-restriction-lock)
7093 (overlay-put org-agenda-restriction-lock-overlay
7094 'help-echo "Agendas are currently limited to this subtree.")
7095 (org-detach-overlay org-agenda-restriction-lock-overlay)
7097 ;;;###autoload
7098 (defun org-agenda-set-restriction-lock (&optional type)
7099 "Set restriction lock for agenda, to current subtree or file.
7100 Restriction will be the file if TYPE is `file', or if type is the
7101 universal prefix '(4), or if the cursor is before the first headline
7102 in the file. Otherwise, restriction will be to the current subtree."
7103 (interactive "P")
7104 (org-agenda-remove-restriction-lock 'noupdate)
7105 (and (equal type '(4)) (setq type 'file))
7106 (setq type (cond
7107 (type type)
7108 ((org-at-heading-p) 'subtree)
7109 ((condition-case nil (org-back-to-heading t) (error nil))
7110 'subtree)
7111 (t 'file)))
7112 (if (eq type 'subtree)
7113 (progn
7114 (setq org-agenda-restrict (current-buffer))
7115 (setq org-agenda-overriding-restriction 'subtree)
7116 (put 'org-agenda-files 'org-restrict
7117 (list (buffer-file-name (buffer-base-buffer))))
7118 (org-back-to-heading t)
7119 (move-overlay org-agenda-restriction-lock-overlay
7120 (point)
7121 (if org-agenda-restriction-lock-highlight-subtree
7122 (save-excursion (org-end-of-subtree t t) (point))
7123 (point-at-eol)))
7124 (move-marker org-agenda-restrict-begin (point))
7125 (move-marker org-agenda-restrict-end
7126 (save-excursion (org-end-of-subtree t t)))
7127 (message "Locking agenda restriction to subtree"))
7128 (put 'org-agenda-files 'org-restrict
7129 (list (buffer-file-name (buffer-base-buffer))))
7130 (setq org-agenda-restrict nil)
7131 (setq org-agenda-overriding-restriction 'file)
7132 (move-marker org-agenda-restrict-begin nil)
7133 (move-marker org-agenda-restrict-end nil)
7134 (message "Locking agenda restriction to file"))
7135 (setq current-prefix-arg nil)
7136 (org-agenda-maybe-redo))
7138 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7139 "Remove the agenda restriction lock."
7140 (interactive "P")
7141 (org-detach-overlay org-agenda-restriction-lock-overlay)
7142 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7143 (setq org-agenda-overriding-restriction nil)
7144 (setq org-agenda-restrict nil)
7145 (put 'org-agenda-files 'org-restrict nil)
7146 (move-marker org-agenda-restrict-begin nil)
7147 (move-marker org-agenda-restrict-end nil)
7148 (setq current-prefix-arg nil)
7149 (message "Agenda restriction lock removed")
7150 (or noupdate (org-agenda-maybe-redo)))
7152 (defun org-agenda-maybe-redo ()
7153 "If there is any window showing the agenda view, update it."
7154 (let ((w (get-buffer-window org-agenda-buffer-name t))
7155 (w0 (selected-window)))
7156 (when w
7157 (select-window w)
7158 (org-agenda-redo)
7159 (select-window w0)
7160 (if org-agenda-overriding-restriction
7161 (message "Agenda view shifted to new %s restriction"
7162 org-agenda-overriding-restriction)
7163 (message "Agenda restriction lock removed")))))
7165 ;;; Agenda commands
7167 (defun org-agenda-check-type (error &rest types)
7168 "Check if agenda buffer is of allowed type.
7169 If ERROR is non-nil, throw an error, otherwise just return nil.
7170 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7171 (if (not org-agenda-type)
7172 (error "No Org agenda currently displayed")
7173 (if (memq org-agenda-type types)
7175 (if error
7176 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7177 nil))))
7179 (defun org-agenda-Quit ()
7180 "Exit the agenda and kill buffers loaded by `org-agenda'.
7181 Also restore the window configuration."
7182 (interactive)
7183 (if org-agenda-columns-active
7184 (org-columns-quit)
7185 (let ((buf (current-buffer)))
7186 (if (eq org-agenda-window-setup 'other-frame)
7187 (progn
7188 (org-agenda-reset-markers)
7189 (kill-buffer buf)
7190 (org-columns-remove-overlays)
7191 (setq org-agenda-archives-mode nil)
7192 (delete-frame))
7193 (and (not (eq org-agenda-window-setup 'current-window))
7194 (not (one-window-p))
7195 (delete-window))
7196 (org-agenda-reset-markers)
7197 (kill-buffer buf)
7198 (org-columns-remove-overlays)
7199 (setq org-agenda-archives-mode nil)))
7200 (setq org-agenda-buffer nil)
7201 ;; Maybe restore the pre-agenda window configuration.
7202 (and org-agenda-restore-windows-after-quit
7203 (not (eq org-agenda-window-setup 'other-frame))
7204 org-agenda-pre-window-conf
7205 (set-window-configuration org-agenda-pre-window-conf)
7206 (setq org-agenda-pre-window-conf nil))))
7208 (defun org-agenda-quit ()
7209 "Exit the agenda and restore the window configuration.
7210 When `org-agenda-sticky' is non-nil, only bury the agenda."
7211 (interactive)
7212 (if (and (eq org-indirect-buffer-display 'other-window)
7213 org-last-indirect-buffer)
7214 (let ((org-last-indirect-window
7215 (get-buffer-window org-last-indirect-buffer)))
7216 (if org-last-indirect-window
7217 (delete-window org-last-indirect-window))))
7218 (if org-agenda-columns-active
7219 (org-columns-quit)
7220 (if org-agenda-sticky
7221 (let ((buf (current-buffer)))
7222 (if (eq org-agenda-window-setup 'other-frame)
7223 (progn
7224 (delete-frame))
7225 (and (not (eq org-agenda-window-setup 'current-window))
7226 (not (one-window-p))
7227 (delete-window)))
7228 (with-current-buffer buf
7229 (bury-buffer)
7230 ;; Maybe restore the pre-agenda window configuration.
7231 (and org-agenda-restore-windows-after-quit
7232 (not (eq org-agenda-window-setup 'other-frame))
7233 org-agenda-pre-window-conf
7234 (set-window-configuration org-agenda-pre-window-conf)
7235 (setq org-agenda-pre-window-conf nil))))
7236 (org-agenda-Quit))))
7238 (defun org-agenda-exit ()
7239 "Exit the agenda and restore the window configuration.
7240 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7241 buffers visited directly by the user will not be touched."
7242 (interactive)
7243 (org-release-buffers org-agenda-new-buffers)
7244 (setq org-agenda-new-buffers nil)
7245 (org-agenda-Quit))
7247 (defun org-agenda-kill-all-agenda-buffers ()
7248 "Kill all buffers in `org-agenda-mode'.
7249 This is used when toggling sticky agendas.
7250 You can also explicitly invoke it with `C-c a C-k'."
7251 (interactive)
7252 (let (blist)
7253 (dolist (buf (buffer-list))
7254 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7255 (push buf blist)))
7256 (mapc 'kill-buffer blist)))
7258 (defun org-agenda-execute (arg)
7259 "Execute another agenda command, keeping same window.
7260 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7261 in the agenda."
7262 (interactive "P")
7263 (let ((org-agenda-window-setup 'current-window))
7264 (org-agenda arg)))
7266 (defun org-agenda-redo (&optional all)
7267 "Rebuild possibly ALL agenda view(s) in the current buffer."
7268 (interactive "P")
7269 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7270 (cpa (unless (eq all t) current-prefix-arg))
7271 (org-agenda-doing-sticky-redo org-agenda-sticky)
7272 (org-agenda-sticky nil)
7273 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7274 org-agenda-buffer-name))
7275 (org-agenda-keep-modes t)
7276 (tag-filter org-agenda-tag-filter)
7277 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7278 (top-hl-filter org-agenda-top-headline-filter)
7279 (cat-filter org-agenda-category-filter)
7280 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7281 (re-filter org-agenda-regexp-filter)
7282 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7283 (effort-filter org-agenda-effort-filter)
7284 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7285 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7286 (cols org-agenda-columns-active)
7287 (line (org-current-line))
7288 (window-line (- line (org-current-line (window-start))))
7289 (lprops (get 'org-agenda-redo-command 'org-lprops))
7290 (redo-cmd (get-text-property p 'org-redo-cmd))
7291 (last-args (get-text-property p 'org-last-args))
7292 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7293 (org-agenda-overriding-cmd-arguments
7294 (unless (eq all t)
7295 (cond ((listp last-args)
7296 (cons (or cpa (car last-args)) (cdr last-args)))
7297 ((stringp last-args)
7298 last-args))))
7299 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7300 (put 'org-agenda-tag-filter :preset-filter nil)
7301 (put 'org-agenda-category-filter :preset-filter nil)
7302 (put 'org-agenda-regexp-filter :preset-filter nil)
7303 (put 'org-agenda-effort-filter :preset-filter nil)
7304 (and cols (org-columns-quit))
7305 (message "Rebuilding agenda buffer...")
7306 (if series-redo-cmd
7307 (eval series-redo-cmd)
7308 (org-let lprops redo-cmd))
7309 (setq org-agenda-undo-list nil
7310 org-agenda-pending-undo-list nil
7311 org-agenda-tag-filter tag-filter
7312 org-agenda-category-filter cat-filter
7313 org-agenda-regexp-filter re-filter
7314 org-agenda-effort-filter effort-filter
7315 org-agenda-top-headline-filter top-hl-filter)
7316 (message "Rebuilding agenda buffer...done")
7317 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7318 (put 'org-agenda-category-filter :preset-filter cat-preset)
7319 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7320 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7321 (let ((tag (or tag-filter tag-preset))
7322 (cat (or cat-filter cat-preset))
7323 (effort (or effort-filter effort-preset))
7324 (re (or re-filter re-preset)))
7325 (when tag (org-agenda-filter-apply tag 'tag))
7326 (when cat (org-agenda-filter-apply cat 'category))
7327 (when effort (org-agenda-filter-apply effort 'effort))
7328 (when re (org-agenda-filter-apply re 'regexp)))
7329 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7330 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7331 (org-goto-line line)
7332 (recenter window-line)))
7334 (defvar org-global-tags-completion-table nil)
7335 (defvar org-agenda-filter-form nil)
7336 (defvar org-agenda-filtered-by-category nil)
7338 (defun org-agenda-filter-by-category (strip)
7339 "Keep only those lines in the agenda buffer that have a specific category.
7340 The category is that of the current line."
7341 (interactive "P")
7342 (if (and org-agenda-filtered-by-category
7343 org-agenda-category-filter)
7344 (org-agenda-filter-show-all-cat)
7345 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7346 (cond
7347 ((and cat strip)
7348 (org-agenda-filter-apply
7349 (push (concat "-" cat) org-agenda-category-filter) 'category))
7350 ((and cat)
7351 (org-agenda-filter-apply
7352 (setq org-agenda-category-filter
7353 (list (concat "+" cat))) 'category))
7354 (t (error "No category at point"))))))
7356 (defun org-find-top-headline (&optional pos)
7357 "Find the topmost parent headline and return it."
7358 (save-excursion
7359 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7360 (if pos (goto-char pos))
7361 ;; Skip up to the topmost parent
7362 (while (ignore-errors (outline-up-heading 1) t))
7363 (ignore-errors
7364 (nth 4 (org-heading-components))))))
7366 (defvar org-agenda-filtered-by-top-headline nil)
7367 (defun org-agenda-filter-by-top-headline (strip)
7368 "Keep only those lines that are descendants from the same top headline.
7369 The top headline is that of the current line."
7370 (interactive "P")
7371 (if org-agenda-filtered-by-top-headline
7372 (progn
7373 (setq org-agenda-filtered-by-top-headline nil
7374 org-agenda-top-headline-filter nil)
7375 (org-agenda-filter-show-all-top-filter))
7376 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7377 (if toph (org-agenda-filter-top-headline-apply toph strip)
7378 (error "No top-level headline at point")))))
7380 (defvar org-agenda-regexp-filter nil)
7381 (defun org-agenda-filter-by-regexp (strip)
7382 "Filter agenda entries by a regular expression.
7383 Regexp filters are cumulative.
7384 With no prefix argument, keep entries matching the regexp.
7385 With one prefix argument, filter out entries matching the regexp.
7386 With two prefix arguments, remove the regexp filters."
7387 (interactive "P")
7388 (if (not (equal strip '(16)))
7389 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7390 (read-from-minibuffer
7391 (if (equal strip '(4))
7392 "Filter out entries matching regexp: "
7393 "Narrow to entries matching regexp: ")))))
7394 (push flt org-agenda-regexp-filter)
7395 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7396 (org-agenda-filter-show-all-re)
7397 (message "Regexp filter removed")))
7399 (defvar org-agenda-effort-filter nil)
7400 (defun org-agenda-filter-by-effort (strip)
7401 "Filter agenda entries by effort.
7402 With no prefix argument, keep entries matching the effort condition.
7403 With one prefix argument, filter out entries matching the condition.
7404 With two prefix arguments, remove the effort filters."
7405 (interactive "P")
7406 (cond ((member strip '(nil 4))
7407 (let ((efforts (org-split-string
7408 (or (cdr (assoc (concat org-effort-property "_ALL")
7409 org-global-properties))
7410 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7411 "")))
7412 (eff -1)
7413 effort-prompt op)
7414 (while (not (member op '(?< ?> ?=)))
7415 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7416 (loop for i from 0 to 9 do
7417 (setq effort-prompt
7418 (concat
7419 effort-prompt " ["
7420 (if (= i 9) "0" (int-to-string (1+ i)))
7421 "]" (nth i efforts))))
7422 (message "Effort %s%s" (char-to-string op) effort-prompt)
7423 (while (or (< eff 0) (> eff 9))
7424 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7425 (setq org-agenda-effort-filter
7426 (list (concat (if strip "-" "+")
7427 (char-to-string op) (nth (1- eff) efforts))))
7428 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7429 (t (org-agenda-filter-show-all-effort)
7430 (message "Effort filter removed"))))
7432 (defun org-agenda-filter-remove-all ()
7433 "Remove all filters from the current agenda buffer."
7434 (interactive)
7435 (when org-agenda-tag-filter
7436 (org-agenda-filter-show-all-tag))
7437 (when org-agenda-category-filter
7438 (org-agenda-filter-show-all-cat))
7439 (when org-agenda-regexp-filter
7440 (org-agenda-filter-show-all-re))
7441 (when org-agenda-top-headline-filter
7442 (org-agenda-filter-show-all-top-filter))
7443 (when org-agenda-effort-filter
7444 (org-agenda-filter-show-all-effort))
7445 (org-agenda-finalize))
7447 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7448 "Keep only those lines in the agenda buffer that have a specific tag.
7449 The tag is selected with its fast selection letter, as configured.
7450 With prefix argument STRIP, remove all lines that do have the tag.
7451 A lisp caller can specify CHAR. NARROW means that the new tag should be
7452 used to narrow the search - the interactive user can also press `-' or `+'
7453 to switch to narrowing."
7454 (interactive "P")
7455 (let* ((alist org-tag-alist-for-agenda)
7456 (tag-chars (mapconcat
7457 (lambda (x) (if (and (not (symbolp (car x)))
7458 (cdr x))
7459 (char-to-string (cdr x))
7460 ""))
7461 alist ""))
7462 (inhibit-read-only t)
7463 (current org-agenda-tag-filter)
7464 maybe-refresh a n tag)
7465 (unless char
7466 (message
7467 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow"
7468 (if narrow "Narrow" "Filter") tag-chars
7469 (if org-agenda-auto-exclude-function "[RET], " ""))
7470 (setq char (read-char-exclusive)))
7471 (when (member char '(?+ ?-))
7472 ;; Narrowing down
7473 (cond ((equal char ?-) (setq strip t narrow t))
7474 ((equal char ?+) (setq strip nil narrow t)))
7475 (message
7476 "Narrow by tag [%s ], [TAB], [/]:off" tag-chars)
7477 (setq char (read-char-exclusive)))
7478 (when (equal char ?\t)
7479 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7480 (org-set-local 'org-global-tags-completion-table
7481 (org-global-tags-completion-table)))
7482 (let ((completion-ignore-case t))
7483 (setq tag (org-icompleting-read
7484 "Tag: " org-global-tags-completion-table))))
7485 (cond
7486 ((equal char ?\r)
7487 (org-agenda-filter-show-all-tag)
7488 (when org-agenda-auto-exclude-function
7489 (setq org-agenda-tag-filter nil)
7490 (dolist (tag (org-agenda-get-represented-tags))
7491 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7492 (if modifier
7493 (push modifier org-agenda-tag-filter))))
7494 (if (not (null org-agenda-tag-filter))
7495 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7496 (setq maybe-refresh t))
7497 ((equal char ?/)
7498 (org-agenda-filter-show-all-tag)
7499 (when (get 'org-agenda-tag-filter :preset-filter)
7500 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7501 (setq maybe-refresh t))
7502 ((equal char ?. )
7503 (setq org-agenda-tag-filter
7504 (mapcar (lambda(tag) (concat "+" tag))
7505 (org-get-at-bol 'tags)))
7506 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7507 (setq maybe-refresh t))
7508 ((or (equal char ?\ )
7509 (setq a (rassoc char alist))
7510 (and tag (setq a (cons tag nil))))
7511 (org-agenda-filter-show-all-tag)
7512 (setq tag (car a))
7513 (setq org-agenda-tag-filter
7514 (cons (concat (if strip "-" "+") tag)
7515 (if narrow current nil)))
7516 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7517 (setq maybe-refresh t))
7518 (t (error "Invalid tag selection character %c" char)))
7519 (when (and maybe-refresh
7520 (eq org-agenda-clockreport-mode 'with-filter))
7521 (org-agenda-redo))))
7523 (defun org-agenda-get-represented-tags ()
7524 "Get a list of all tags currently represented in the agenda."
7525 (let (p tags)
7526 (save-excursion
7527 (goto-char (point-min))
7528 (while (setq p (next-single-property-change (point) 'tags))
7529 (goto-char p)
7530 (mapc (lambda (x) (add-to-list 'tags x))
7531 (get-text-property (point) 'tags))))
7532 tags))
7534 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7535 "Refine the current filter. See `org-agenda-filter-by-tag'."
7536 (interactive "P")
7537 (org-agenda-filter-by-tag strip char 'refine))
7539 (defun org-agenda-filter-make-matcher (filter type)
7540 "Create the form that tests a line for agenda filter."
7541 (let (f f1)
7542 (cond
7543 ;; Tag filter
7544 ((eq type 'tag)
7545 (setq filter
7546 (delete-dups
7547 (append (get 'org-agenda-tag-filter :preset-filter)
7548 filter)))
7549 (dolist (x filter)
7550 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7551 (ffunc
7552 (lambda (nf0 nf01 fltr notgroup op)
7553 (dolist (x fltr)
7554 (if (member x '("-" "+"))
7555 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7556 (setq nf01 (list 'member (downcase (substring x 1))
7557 'tags))
7558 (when (equal (string-to-char x) ?-)
7559 (setq nf01 (list 'not nf01))
7560 (when (not notgroup) (setq op 'and))))
7561 (push nf01 nf0))
7562 (if notgroup
7563 (push (cons 'and nf0) f)
7564 (push (cons (or op 'or) nf0) f)))))
7565 (cond ((equal filter '("+"))
7566 (setq f (list (list 'not 'tags))))
7567 ((equal nfilter filter)
7568 (funcall ffunc f1 f filter t nil))
7569 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7570 ;; Category filter
7571 ((eq type 'category)
7572 (setq filter
7573 (delete-dups
7574 (append (get 'org-agenda-category-filter :preset-filter)
7575 filter)))
7576 (dolist (x filter)
7577 (if (equal "-" (substring x 0 1))
7578 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7579 (setq f1 (list 'equal (substring x 1) 'cat)))
7580 (push f1 f)))
7581 ;; Regexp filter
7582 ((eq type 'regexp)
7583 (setq filter
7584 (delete-dups
7585 (append (get 'org-agenda-regexp-filter :preset-filter)
7586 filter)))
7587 (dolist (x filter)
7588 (if (equal "-" (substring x 0 1))
7589 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7590 (setq f1 (list 'string-match (substring x 1) 'txt)))
7591 (push f1 f)))
7592 ;; Effort filter
7593 ((eq type 'effort)
7594 (setq filter
7595 (delete-dups
7596 (append (get 'org-agenda-effort-filter :preset-filter)
7597 filter)))
7598 (dolist (x filter)
7599 (push (org-agenda-filter-effort-form x) f))))
7600 (cons 'and (nreverse f))))
7602 (defun org-agenda-filter-effort-form (e)
7603 "Return the form to compare the effort of the current line with what E says.
7604 E looks like \"+<2:25\"."
7605 (let (op)
7606 (setq e (substring e 1))
7607 (setq op (string-to-char e) e (substring e 1))
7608 (setq op (cond ((equal op ?<) '<=)
7609 ((equal op ?>) '>=)
7610 ((equal op ??) op)
7611 (t '=)))
7612 (list 'org-agenda-compare-effort (list 'quote op)
7613 (org-duration-string-to-minutes e))))
7615 (defun org-agenda-compare-effort (op value)
7616 "Compare the effort of the current line with VALUE, using OP.
7617 If the line does not have an effort defined, return nil."
7618 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7619 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7620 value)))
7622 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7623 "Expand group tags in FILTER for the agenda.
7624 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7625 (if org-group-tags
7626 (let ((case-fold-search t) rtn)
7627 (mapc
7628 (lambda (f)
7629 (let (f0 dir)
7630 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7631 (setq dir (match-string 1 f) f0 (match-string 2 f))
7632 (setq dir (if no-operator "" "+") f0 f))
7633 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7634 (org-tags-expand f0 t t))
7635 rtn))))
7636 filter)
7637 (reverse rtn))
7638 filter))
7640 (defun org-agenda-filter-apply (filter type)
7641 "Set FILTER as the new agenda filter and apply it."
7642 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7643 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7644 (let (tags cat txt)
7645 (setq org-agenda-filter-form
7646 (org-agenda-filter-make-matcher filter type))
7647 (if (and (eq type 'category)
7648 (not (equal (substring (car filter) 0 1) "-")))
7649 ;; Only set `org-agenda-filtered-by-category' to t
7650 ;; when a unique category is used as the filter
7651 (setq org-agenda-filtered-by-category t))
7652 (org-agenda-set-mode-name)
7653 (save-excursion
7654 (goto-char (point-min))
7655 (while (not (eobp))
7656 (if (org-get-at-bol 'org-marker)
7657 (progn
7658 (setq tags ; used in eval
7659 (apply 'append
7660 (mapcar (lambda (f)
7661 (org-agenda-filter-expand-tags (list f) t))
7662 (org-get-at-bol 'tags)))
7663 cat (org-get-at-eol 'org-category 1)
7664 txt (org-get-at-eol 'txt 1))
7665 (if (not (eval org-agenda-filter-form))
7666 (org-agenda-filter-hide-line type))
7667 (beginning-of-line 2))
7668 (beginning-of-line 2))))
7669 (if (get-char-property (point) 'invisible)
7670 (ignore-errors (org-agenda-previous-line)))))
7672 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7673 "Filter by top headline HL."
7674 (org-agenda-set-mode-name)
7675 (save-excursion
7676 (goto-char (point-min))
7677 (while (not (eobp))
7678 (let* ((pos (org-get-at-bol 'org-hd-marker))
7679 (tophl (and pos (org-find-top-headline pos))))
7680 (if (and tophl (funcall (if negative 'identity 'not)
7681 (string= hl tophl)))
7682 (org-agenda-filter-hide-line 'top-headline)))
7683 (beginning-of-line 2)))
7684 (if (get-char-property (point) 'invisible)
7685 (org-agenda-previous-line))
7686 (setq org-agenda-top-headline-filter hl
7687 org-agenda-filtered-by-top-headline t))
7689 (defun org-agenda-filter-hide-line (type)
7690 "Hide lines with TYPE in the agenda buffer."
7691 (let* ((b (max (point-min) (1- (point-at-bol))))
7692 (e (point-at-eol)))
7693 (let ((inhibit-read-only t))
7694 (add-text-properties
7695 b e `(invisible org-filtered org-filter-type ,type)))))
7697 (defun org-agenda-remove-filter (type)
7698 (interactive)
7699 "Remove filter of type TYPE from the agenda buffer."
7700 (save-excursion
7701 (goto-char (point-min))
7702 (let ((inhibit-read-only t) pos)
7703 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7704 (goto-char pos)
7705 (remove-text-properties
7706 (point) (next-single-property-change (point) 'org-filter-type)
7707 `(invisible org-filtered org-filter-type ,type))))
7708 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7709 (setq org-agenda-filter-form nil)
7710 (org-agenda-set-mode-name)
7711 (org-agenda-finalize)))
7713 (defun org-agenda-filter-show-all-tag nil
7714 (org-agenda-remove-filter 'tag))
7715 (defun org-agenda-filter-show-all-re nil
7716 (org-agenda-remove-filter 'regexp))
7717 (defun org-agenda-filter-show-all-effort nil
7718 (org-agenda-remove-filter 'effort))
7719 (defun org-agenda-filter-show-all-cat nil
7720 (org-agenda-remove-filter 'category))
7721 (defun org-agenda-filter-show-all-top-filter nil
7722 (org-agenda-remove-filter 'top-headline))
7724 (defun org-agenda-manipulate-query-add ()
7725 "Manipulate the query by adding a search term with positive selection.
7726 Positive selection means the term must be matched for selection of an entry."
7727 (interactive)
7728 (org-agenda-manipulate-query ?\[))
7729 (defun org-agenda-manipulate-query-subtract ()
7730 "Manipulate the query by adding a search term with negative selection.
7731 Negative selection means term must not be matched for selection of an entry."
7732 (interactive)
7733 (org-agenda-manipulate-query ?\]))
7734 (defun org-agenda-manipulate-query-add-re ()
7735 "Manipulate the query by adding a search regexp with positive selection.
7736 Positive selection means the regexp must match for selection of an entry."
7737 (interactive)
7738 (org-agenda-manipulate-query ?\{))
7739 (defun org-agenda-manipulate-query-subtract-re ()
7740 "Manipulate the query by adding a search regexp with negative selection.
7741 Negative selection means regexp must not match for selection of an entry."
7742 (interactive)
7743 (org-agenda-manipulate-query ?\}))
7744 (defun org-agenda-manipulate-query (char)
7745 (cond
7746 ((memq org-agenda-type '(timeline agenda))
7747 (let ((org-agenda-include-inactive-timestamps t))
7748 (org-agenda-redo))
7749 (message "Display now includes inactive timestamps as well"))
7750 ((eq org-agenda-type 'search)
7751 (org-add-to-string
7752 'org-agenda-query-string
7753 (if org-agenda-last-search-view-search-was-boolean
7754 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7755 (?\{ . " +{}") (?\} . " -{}"))))
7756 " "))
7757 (setq org-agenda-redo-command
7758 (list 'org-search-view
7759 (car (get-text-property (min (1- (point-max)) (point))
7760 'org-last-args))
7761 org-agenda-query-string
7762 (+ (length org-agenda-query-string)
7763 (if (member char '(?\{ ?\})) 0 1))))
7764 (set-register org-agenda-query-register org-agenda-query-string)
7765 (let ((org-agenda-overriding-arguments
7766 (cdr org-agenda-redo-command)))
7767 (org-agenda-redo)))
7768 (t (error "Cannot manipulate query for %s-type agenda buffers"
7769 org-agenda-type))))
7771 (defun org-add-to-string (var string)
7772 (set var (concat (symbol-value var) string)))
7774 (defun org-agenda-goto-date (span)
7775 "Jump to DATE in agenda."
7776 (interactive "P")
7777 (let* ((org-read-date-prefer-future
7778 (eval org-agenda-jump-prefer-future))
7779 (date (org-read-date))
7780 (day (time-to-days (org-time-string-to-time date)))
7781 (org-agenda-sticky-orig org-agenda-sticky)
7782 (org-agenda-buffer-tmp-name (buffer-name))
7783 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7784 (0-arg (or current-prefix-arg (car args)))
7785 (2-arg (nth 2 args))
7786 (with-hour-p (nth 4 org-agenda-redo-command))
7787 (newcmd (list 'org-agenda-list 0-arg date
7788 (org-agenda-span-to-ndays
7789 2-arg (org-time-string-to-absolute date))
7790 with-hour-p))
7791 (newargs (cdr newcmd))
7792 (inhibit-read-only t)
7793 org-agenda-sticky)
7794 (if (not (org-agenda-check-type t 'agenda))
7795 (error "Not available in non-agenda views")
7796 (add-text-properties (point-min) (point-max)
7797 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7798 (org-agenda-redo)
7799 (goto-char (point-min))
7800 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7801 (save-excursion (move-beginning-of-line 2) (eobp))))
7802 (move-beginning-of-line 2))
7803 (setq org-agenda-sticky org-agenda-sticky-orig
7804 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7806 (defun org-agenda-goto-today ()
7807 "Go to today."
7808 (interactive)
7809 (org-agenda-check-type t 'timeline 'agenda)
7810 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7811 (curspan (nth 2 args))
7812 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7813 (cond
7814 (tdpos (goto-char tdpos))
7815 ((eq org-agenda-type 'agenda)
7816 (let* ((sd (org-agenda-compute-starting-span
7817 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7818 (org-agenda-overriding-arguments args))
7819 (setf (nth 1 org-agenda-overriding-arguments) sd)
7820 (org-agenda-redo)
7821 (org-agenda-find-same-or-today-or-agenda)))
7822 (t (error "Cannot find today")))))
7824 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7825 (goto-char
7826 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7827 (text-property-any (point-min) (point-max) 'org-today t)
7828 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7829 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7830 (org-agenda-backward-block))
7831 (point-min))))
7833 (defun org-agenda-backward-block ()
7834 "Move backward by one agenda block."
7835 (interactive)
7836 (org-agenda-forward-block 'backward))
7838 (defun org-agenda-forward-block (&optional backward)
7839 "Move forward by one agenda block.
7840 When optional argument BACKWARD is set, go backward"
7841 (interactive)
7842 (cond ((not (derived-mode-p 'org-agenda-mode))
7843 (user-error
7844 "Cannot execute this command outside of org-agenda-mode buffers"))
7845 ((looking-at (if backward "\\`" "\\'"))
7846 (message "Already at the %s block" (if backward "first" "last")))
7847 (t (let ((pos (prog1 (point)
7848 (ignore-errors (if backward (backward-char 1)
7849 (move-end-of-line 1)))))
7850 (f (if backward
7851 'previous-single-property-change
7852 'next-single-property-change))
7853 moved dest)
7854 (while (and (setq dest (funcall
7855 f (point) 'org-agenda-structural-header))
7856 (not (get-text-property
7857 (point) 'org-agenda-structural-header)))
7858 (setq moved t)
7859 (goto-char dest))
7860 (if moved (move-beginning-of-line 1)
7861 (goto-char (if backward (point-min) (point-max)))
7862 (move-beginning-of-line 1)
7863 (message "No %s block" (if backward "previous" "further")))))))
7865 (defun org-agenda-later (arg)
7866 "Go forward in time by the current span.
7867 With prefix ARG, go forward that many times the current span."
7868 (interactive "p")
7869 (org-agenda-check-type t 'agenda)
7870 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7871 (span (or (nth 2 args) org-agenda-current-span))
7872 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7873 (greg (calendar-gregorian-from-absolute sd))
7874 (cnt (org-get-at-bol 'org-day-cnt))
7875 greg2)
7876 (cond
7877 ((numberp span)
7878 (setq sd (+ (* span arg) sd)))
7879 ((eq span 'day)
7880 (setq sd (+ arg sd)))
7881 ((eq span 'week)
7882 (setq sd (+ (* 7 arg) sd)))
7883 ((eq span 'fortnight)
7884 (setq sd (+ (* 14 arg) sd)))
7885 ((eq span 'month)
7886 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7887 sd (calendar-absolute-from-gregorian greg2))
7888 (setcar greg2 (1+ (car greg2))))
7889 ((eq span 'year)
7890 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7891 sd (calendar-absolute-from-gregorian greg2))
7892 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7894 (setq sd (+ (* span arg) sd))))
7895 (let ((org-agenda-overriding-cmd
7896 ;; `cmd' may have been set by `org-agenda-run-series' which
7897 ;; uses `org-agenda-overriding-cmd' to decide whether
7898 ;; overriding is allowed for `cmd'
7899 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7900 (org-agenda-overriding-arguments
7901 (list (car args) sd span)))
7902 (org-agenda-redo)
7903 (org-agenda-find-same-or-today-or-agenda cnt))))
7905 (defun org-agenda-earlier (arg)
7906 "Go backward in time by the current span.
7907 With prefix ARG, go backward that many times the current span."
7908 (interactive "p")
7909 (org-agenda-later (- arg)))
7911 (defun org-agenda-view-mode-dispatch ()
7912 "Call one of the view mode commands."
7913 (interactive)
7914 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7915 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7916 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7917 (let ((a (read-char-exclusive)))
7918 (case a
7919 (?\ (call-interactively 'org-agenda-reset-view))
7920 (?d (call-interactively 'org-agenda-day-view))
7921 (?w (call-interactively 'org-agenda-week-view))
7922 (?t (call-interactively 'org-agenda-fortnight-view))
7923 (?m (call-interactively 'org-agenda-month-view))
7924 (?y (call-interactively 'org-agenda-year-view))
7925 (?l (call-interactively 'org-agenda-log-mode))
7926 (?L (org-agenda-log-mode '(4)))
7927 (?c (org-agenda-log-mode 'clockcheck))
7928 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7929 (?a (call-interactively 'org-agenda-archives-mode))
7930 (?A (org-agenda-archives-mode 'files))
7931 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7932 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7933 (?G (call-interactively 'org-agenda-toggle-time-grid))
7934 (?D (call-interactively 'org-agenda-toggle-diary))
7935 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7936 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7937 (org-agenda-check-type t 'timeline 'agenda)
7938 (org-agenda-redo))
7939 (message "Display now includes inactive timestamps as well"))
7940 (?q (message "Abort"))
7941 (otherwise (error "Invalid key" )))))
7943 (defun org-agenda-reset-view ()
7944 "Switch to default view for agenda."
7945 (interactive)
7946 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7947 (defun org-agenda-day-view (&optional day-of-month)
7948 "Switch to daily view for agenda.
7949 With argument DAY-OF-MONTH, switch to that day of the month."
7950 (interactive "P")
7951 (org-agenda-change-time-span 'day day-of-month))
7952 (defun org-agenda-week-view (&optional iso-week)
7953 "Switch to daily view for agenda.
7954 With argument ISO-WEEK, switch to the corresponding ISO week.
7955 If ISO-WEEK has more then 2 digits, only the last two encode the
7956 week. Any digits before this encode a year. So 200712 means
7957 week 12 of year 2007. Years in the range 1938-2037 can also be
7958 written as 2-digit years."
7959 (interactive "P")
7960 (org-agenda-change-time-span 'week iso-week))
7961 (defun org-agenda-fortnight-view (&optional iso-week)
7962 "Switch to daily view for agenda.
7963 With argument ISO-WEEK, switch to the corresponding ISO week.
7964 If ISO-WEEK has more then 2 digits, only the last two encode the
7965 week. Any digits before this encode a year. So 200712 means
7966 week 12 of year 2007. Years in the range 1938-2037 can also be
7967 written as 2-digit years."
7968 (interactive "P")
7969 (org-agenda-change-time-span 'fortnight iso-week))
7970 (defun org-agenda-month-view (&optional month)
7971 "Switch to monthly view for agenda.
7972 With argument MONTH, switch to that month."
7973 (interactive "P")
7974 (org-agenda-change-time-span 'month month))
7975 (defun org-agenda-year-view (&optional year)
7976 "Switch to yearly view for agenda.
7977 With argument YEAR, switch to that year.
7978 If MONTH has more then 2 digits, only the last two encode the
7979 month. Any digits before this encode a year. So 200712 means
7980 December year 2007. Years in the range 1938-2037 can also be
7981 written as 2-digit years."
7982 (interactive "P")
7983 (when year
7984 (setq year (org-small-year-to-year year)))
7985 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7986 (org-agenda-change-time-span 'year year)
7987 (error "Abort")))
7989 (defun org-agenda-change-time-span (span &optional n)
7990 "Change the agenda view to SPAN.
7991 SPAN may be `day', `week', `fortnight', `month', `year'."
7992 (org-agenda-check-type t 'agenda)
7993 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7994 (curspan (nth 2 args)))
7995 (if (and (not n) (equal curspan span))
7996 (error "Viewing span is already \"%s\"" span))
7997 (let* ((sd (or (org-get-at-bol 'day)
7998 (nth 1 args)
7999 org-starting-day))
8000 (sd (org-agenda-compute-starting-span sd span n))
8001 (org-agenda-overriding-cmd
8002 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8003 (org-agenda-overriding-arguments
8004 (list (car args) sd span)))
8005 (org-agenda-redo)
8006 (org-agenda-find-same-or-today-or-agenda))
8007 (org-agenda-set-mode-name)
8008 (message "Switched to %s view" span)))
8010 (defun org-agenda-compute-starting-span (sd span &optional n)
8011 "Compute starting date for agenda.
8012 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8013 is a cons cell with the starting date and the number of days,
8014 so that the date SD will be in that range."
8015 (let* ((greg (calendar-gregorian-from-absolute sd))
8016 (dg (nth 1 greg))
8017 (mg (car greg))
8018 (yg (nth 2 greg)))
8019 (cond
8020 ((eq span 'day)
8021 (when n
8022 (setq sd (+ (calendar-absolute-from-gregorian
8023 (list mg 1 yg))
8024 n -1))))
8025 ((or (eq span 'week) (eq span 'fortnight))
8026 (let* ((nt (calendar-day-of-week
8027 (calendar-gregorian-from-absolute sd)))
8028 (d (if org-agenda-start-on-weekday
8029 (- nt org-agenda-start-on-weekday)
8032 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8033 (when n
8034 (require 'cal-iso)
8035 (when (> n 99)
8036 (setq y1 (org-small-year-to-year (/ n 100))
8037 n (mod n 100)))
8038 (setq sd
8039 (calendar-absolute-from-iso
8040 (list n 1
8041 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8042 ((eq span 'month)
8043 (let (y1)
8044 (when (and n (> n 99))
8045 (setq y1 (org-small-year-to-year (/ n 100))
8046 n (mod n 100)))
8047 (setq sd (calendar-absolute-from-gregorian
8048 (list (or n mg) 1 (or y1 yg))))))
8049 ((eq span 'year)
8050 (setq sd (calendar-absolute-from-gregorian
8051 (list 1 1 (or n yg))))))
8052 sd))
8054 (defun org-agenda-next-date-line (&optional arg)
8055 "Jump to the next line indicating a date in agenda buffer."
8056 (interactive "p")
8057 (org-agenda-check-type t 'agenda 'timeline)
8058 (beginning-of-line 1)
8059 ;; This does not work if user makes date format that starts with a blank
8060 (if (looking-at "^\\S-") (forward-char 1))
8061 (if (not (re-search-forward "^\\S-" nil t arg))
8062 (progn
8063 (backward-char 1)
8064 (error "No next date after this line in this buffer")))
8065 (goto-char (match-beginning 0)))
8067 (defun org-agenda-previous-date-line (&optional arg)
8068 "Jump to the previous line indicating a date in agenda buffer."
8069 (interactive "p")
8070 (org-agenda-check-type t 'agenda 'timeline)
8071 (beginning-of-line 1)
8072 (if (not (re-search-backward "^\\S-" nil t arg))
8073 (error "No previous date before this line in this buffer")))
8075 ;; Initialize the highlight
8076 (defvar org-hl (make-overlay 1 1))
8077 (overlay-put org-hl 'face 'highlight)
8079 (defun org-highlight (begin end &optional buffer)
8080 "Highlight a region with overlay."
8081 (move-overlay org-hl begin end (or buffer (current-buffer))))
8083 (defun org-unhighlight ()
8084 "Detach overlay INDEX."
8085 (org-detach-overlay org-hl))
8087 (defun org-unhighlight-once ()
8088 "Remove the highlight from its position, and this function from the hook."
8089 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8090 (org-unhighlight))
8092 (defvar org-agenda-pre-follow-window-conf nil)
8093 (defun org-agenda-follow-mode ()
8094 "Toggle follow mode in an agenda buffer."
8095 (interactive)
8096 (unless org-agenda-follow-mode
8097 (setq org-agenda-pre-follow-window-conf
8098 (current-window-configuration)))
8099 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8100 (unless org-agenda-follow-mode
8101 (set-window-configuration org-agenda-pre-follow-window-conf))
8102 (org-agenda-set-mode-name)
8103 (org-agenda-do-context-action)
8104 (message "Follow mode is %s"
8105 (if org-agenda-follow-mode "on" "off")))
8107 (defun org-agenda-entry-text-mode (&optional arg)
8108 "Toggle entry text mode in an agenda buffer."
8109 (interactive "P")
8110 (if (or org-agenda-tag-filter
8111 org-agenda-category-filter
8112 org-agenda-regexp-filter
8113 org-agenda-top-headline-filter)
8114 (user-error "Can't show entry text in filtered views")
8115 (setq org-agenda-entry-text-mode (or (integerp arg)
8116 (not org-agenda-entry-text-mode)))
8117 (org-agenda-entry-text-hide)
8118 (and org-agenda-entry-text-mode
8119 (let ((org-agenda-entry-text-maxlines
8120 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8121 (org-agenda-entry-text-show)))
8122 (org-agenda-set-mode-name)
8123 (message "Entry text mode is %s%s"
8124 (if org-agenda-entry-text-mode "on" "off")
8125 (if (not org-agenda-entry-text-mode) ""
8126 (format " (maximum number of lines is %d)"
8127 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8129 (defun org-agenda-clockreport-mode (&optional with-filter)
8130 "Toggle clocktable mode in an agenda buffer.
8131 With prefix arg WITH-FILTER, make the clocktable respect the current
8132 agenda filter."
8133 (interactive "P")
8134 (org-agenda-check-type t 'agenda)
8135 (if with-filter
8136 (setq org-agenda-clockreport-mode 'with-filter)
8137 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8138 (org-agenda-set-mode-name)
8139 (org-agenda-redo)
8140 (message "Clocktable mode is %s"
8141 (if org-agenda-clockreport-mode "on" "off")))
8143 (defun org-agenda-log-mode (&optional special)
8144 "Toggle log mode in an agenda buffer.
8145 With argument SPECIAL, show all possible log items, not only the ones
8146 configured in `org-agenda-log-mode-items'.
8147 With a double `C-u' prefix arg, show *only* log items, nothing else."
8148 (interactive "P")
8149 (org-agenda-check-type t 'agenda 'timeline)
8150 (setq org-agenda-show-log
8151 (cond
8152 ((equal special '(16)) 'only)
8153 ((eq special 'clockcheck)
8154 (if (eq org-agenda-show-log 'clockcheck)
8155 nil 'clockcheck))
8156 (special '(closed clock state))
8157 (t (not org-agenda-show-log))))
8158 (org-agenda-set-mode-name)
8159 (org-agenda-redo)
8160 (message "Log mode is %s"
8161 (if org-agenda-show-log "on" "off")))
8163 (defun org-agenda-archives-mode (&optional with-files)
8164 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8165 When called with a prefix argument, include all archive files as well."
8166 (interactive "P")
8167 (setq org-agenda-archives-mode
8168 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8169 (org-agenda-set-mode-name)
8170 (org-agenda-redo)
8171 (message
8172 "%s"
8173 (cond
8174 ((eq org-agenda-archives-mode nil)
8175 "No archives are included")
8176 ((eq org-agenda-archives-mode 'trees)
8177 (format "Trees with :%s: tag are included" org-archive-tag))
8178 ((eq org-agenda-archives-mode t)
8179 (format "Trees with :%s: tag and all active archive files are included"
8180 org-archive-tag)))))
8182 (defun org-agenda-toggle-diary ()
8183 "Toggle diary inclusion in an agenda buffer."
8184 (interactive)
8185 (org-agenda-check-type t 'agenda)
8186 (setq org-agenda-include-diary (not org-agenda-include-diary))
8187 (org-agenda-redo)
8188 (org-agenda-set-mode-name)
8189 (message "Diary inclusion turned %s"
8190 (if org-agenda-include-diary "on" "off")))
8192 (defun org-agenda-toggle-deadlines ()
8193 "Toggle inclusion of entries with a deadline in an agenda buffer."
8194 (interactive)
8195 (org-agenda-check-type t 'agenda)
8196 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8197 (org-agenda-redo)
8198 (org-agenda-set-mode-name)
8199 (message "Deadlines inclusion turned %s"
8200 (if org-agenda-include-deadlines "on" "off")))
8202 (defun org-agenda-toggle-time-grid ()
8203 "Toggle time grid in an agenda buffer."
8204 (interactive)
8205 (org-agenda-check-type t 'agenda)
8206 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8207 (org-agenda-redo)
8208 (org-agenda-set-mode-name)
8209 (message "Time-grid turned %s"
8210 (if org-agenda-use-time-grid "on" "off")))
8212 (defun org-agenda-set-mode-name ()
8213 "Set the mode name to indicate all the small mode settings."
8214 (setq mode-name
8215 (list "Org-Agenda"
8216 (if (get 'org-agenda-files 'org-restrict) " []" "")
8218 '(:eval (org-agenda-span-name org-agenda-current-span))
8219 (if org-agenda-follow-mode " Follow" "")
8220 (if org-agenda-entry-text-mode " ETxt" "")
8221 (if org-agenda-include-diary " Diary" "")
8222 (if org-agenda-include-deadlines " Ddl" "")
8223 (if org-agenda-use-time-grid " Grid" "")
8224 (if (and (boundp 'org-habit-show-habits)
8225 org-habit-show-habits) " Habit" "")
8226 (cond
8227 ((consp org-agenda-show-log) " LogAll")
8228 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8229 (org-agenda-show-log " Log")
8230 (t ""))
8231 (if (or org-agenda-category-filter
8232 (get 'org-agenda-category-filter :preset-filter))
8233 '(:eval (org-propertize
8234 (concat " <"
8235 (mapconcat
8236 'identity
8237 (append
8238 (get 'org-agenda-category-filter :preset-filter)
8239 org-agenda-category-filter)
8241 ">")
8242 'face 'org-agenda-filter-category
8243 'help-echo "Category used in filtering")) "")
8244 (if (or org-agenda-tag-filter
8245 (get 'org-agenda-tag-filter :preset-filter))
8246 '(:eval (org-propertize
8247 (concat " {"
8248 (mapconcat
8249 'identity
8250 (append
8251 (get 'org-agenda-tag-filter :preset-filter)
8252 org-agenda-tag-filter)
8254 "}")
8255 'face 'org-agenda-filter-tags
8256 'help-echo "Tags used in filtering")) "")
8257 (if (or org-agenda-effort-filter
8258 (get 'org-agenda-effort-filter :preset-filter))
8259 '(:eval (org-propertize
8260 (concat " {"
8261 (mapconcat
8262 'identity
8263 (append
8264 (get 'org-agenda-effort-filter :preset-filter)
8265 org-agenda-effort-filter)
8267 "}")
8268 'face 'org-agenda-filter-effort
8269 'help-echo "Effort conditions used in filtering")) "")
8270 (if (or org-agenda-regexp-filter
8271 (get 'org-agenda-regexp-filter :preset-filter))
8272 '(:eval (org-propertize
8273 (concat " ["
8274 (mapconcat
8275 'identity
8276 (append
8277 (get 'org-agenda-regexp-filter :preset-filter)
8278 org-agenda-regexp-filter)
8280 "]")
8281 'face 'org-agenda-filter-regexp
8282 'help-echo "Regexp used in filtering")) "")
8283 (if org-agenda-archives-mode
8284 (if (eq org-agenda-archives-mode t)
8285 " Archives"
8286 (format " :%s:" org-archive-tag))
8288 (if org-agenda-clockreport-mode
8289 (if (eq org-agenda-clockreport-mode 'with-filter)
8290 " Clock{}" " Clock")
8291 "")))
8292 (force-mode-line-update))
8294 (define-obsolete-function-alias
8295 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8297 (defun org-agenda-update-agenda-type ()
8298 "Update the agenda type after each command."
8299 (setq org-agenda-type
8300 (or (get-text-property (point) 'org-agenda-type)
8301 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8303 (defun org-agenda-next-line ()
8304 "Move cursor to the next line, and show if follow mode is active."
8305 (interactive)
8306 (call-interactively 'next-line)
8307 (org-agenda-do-context-action))
8309 (defun org-agenda-previous-line ()
8310 "Move cursor to the previous line, and show if follow-mode is active."
8311 (interactive)
8312 (call-interactively 'previous-line)
8313 (org-agenda-do-context-action))
8315 (defun org-agenda-next-item (n)
8316 "Move cursor to next agenda item."
8317 (interactive "p")
8318 (let ((col (current-column)))
8319 (dotimes (c n)
8320 (when (next-single-property-change (point-at-eol) 'org-marker)
8321 (move-end-of-line 1)
8322 (goto-char (next-single-property-change (point) 'org-marker))))
8323 (org-move-to-column col))
8324 (org-agenda-do-context-action))
8326 (defun org-agenda-previous-item (n)
8327 "Move cursor to next agenda item."
8328 (interactive "p")
8329 (dotimes (c n)
8330 (let ((col (current-column))
8331 (goto (save-excursion
8332 (move-end-of-line 0)
8333 (previous-single-property-change (point) 'org-marker))))
8334 (if goto (goto-char goto))
8335 (org-move-to-column col)))
8336 (org-agenda-do-context-action))
8338 (defun org-agenda-do-context-action ()
8339 "Show outline path and, maybe, follow mode window."
8340 (let ((m (org-get-at-bol 'org-marker)))
8341 (when (and (markerp m) (marker-buffer m))
8342 (and org-agenda-follow-mode
8343 (if org-agenda-follow-indirect
8344 (org-agenda-tree-to-indirect-buffer nil)
8345 (org-agenda-show)))
8346 (and org-agenda-show-outline-path
8347 (org-with-point-at m (org-display-outline-path t))))))
8349 (defun org-agenda-show-tags ()
8350 "Show the tags applicable to the current item."
8351 (interactive)
8352 (let* ((tags (org-get-at-bol 'tags)))
8353 (if tags
8354 (message "Tags are :%s:"
8355 (org-no-properties (mapconcat 'identity tags ":")))
8356 (message "No tags associated with this line"))))
8358 (defun org-agenda-goto (&optional highlight)
8359 "Go to the entry at point in the corresponding Org-mode file."
8360 (interactive)
8361 (let* ((marker (or (org-get-at-bol 'org-marker)
8362 (org-agenda-error)))
8363 (buffer (marker-buffer marker))
8364 (pos (marker-position marker)))
8365 (switch-to-buffer-other-window buffer)
8366 (widen)
8367 (push-mark)
8368 (goto-char pos)
8369 (when (derived-mode-p 'org-mode)
8370 (org-show-context 'agenda)
8371 (save-excursion
8372 (and (outline-next-heading)
8373 (org-flag-heading nil)))) ; show the next heading
8374 (when (outline-invisible-p)
8375 (show-entry)) ; display invisible text
8376 (recenter (/ (window-height) 2))
8377 (org-back-to-heading t)
8378 (if (re-search-forward org-complex-heading-regexp nil t)
8379 (goto-char (match-beginning 4)))
8380 (run-hooks 'org-agenda-after-show-hook)
8381 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8383 (defvar org-agenda-after-show-hook nil
8384 "Normal hook run after an item has been shown from the agenda.
8385 Point is in the buffer where the item originated.")
8387 (defun org-agenda-kill ()
8388 "Kill the entry or subtree belonging to the current agenda entry."
8389 (interactive)
8390 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8391 (let* ((bufname-orig (buffer-name))
8392 (marker (or (org-get-at-bol 'org-marker)
8393 (org-agenda-error)))
8394 (buffer (marker-buffer marker))
8395 (pos (marker-position marker))
8396 (type (org-get-at-bol 'type))
8397 dbeg dend (n 0) conf)
8398 (org-with-remote-undo buffer
8399 (with-current-buffer buffer
8400 (save-excursion
8401 (goto-char pos)
8402 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8403 (setq dbeg (progn (org-back-to-heading t) (point))
8404 dend (org-end-of-subtree t t))
8405 (setq dbeg (point-at-bol)
8406 dend (min (point-max) (1+ (point-at-eol)))))
8407 (goto-char dbeg)
8408 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8409 (setq conf (or (eq t org-agenda-confirm-kill)
8410 (and (numberp org-agenda-confirm-kill)
8411 (> n org-agenda-confirm-kill))))
8412 (and conf
8413 (not (y-or-n-p
8414 (format "Delete entry with %d lines in buffer \"%s\"? "
8415 n (buffer-name buffer))))
8416 (error "Abort"))
8417 (let ((org-agenda-buffer-name bufname-orig))
8418 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8419 (with-current-buffer buffer (delete-region dbeg dend))
8420 (message "Agenda item and source killed"))))
8422 (defvar org-archive-default-command) ; defined in org-archive.el
8423 (defun org-agenda-archive-default ()
8424 "Archive the entry or subtree belonging to the current agenda entry."
8425 (interactive)
8426 (require 'org-archive)
8427 (org-agenda-archive-with org-archive-default-command))
8429 (defun org-agenda-archive-default-with-confirmation ()
8430 "Archive the entry or subtree belonging to the current agenda entry."
8431 (interactive)
8432 (require 'org-archive)
8433 (org-agenda-archive-with org-archive-default-command 'confirm))
8435 (defun org-agenda-archive ()
8436 "Archive the entry or subtree belonging to the current agenda entry."
8437 (interactive)
8438 (org-agenda-archive-with 'org-archive-subtree))
8440 (defun org-agenda-archive-to-archive-sibling ()
8441 "Move the entry to the archive sibling."
8442 (interactive)
8443 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8445 (defun org-agenda-archive-with (cmd &optional confirm)
8446 "Move the entry to the archive sibling."
8447 (interactive)
8448 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8449 (let* ((bufname-orig (buffer-name))
8450 (marker (or (org-get-at-bol 'org-marker)
8451 (org-agenda-error)))
8452 (buffer (marker-buffer marker))
8453 (pos (marker-position marker)))
8454 (org-with-remote-undo buffer
8455 (with-current-buffer buffer
8456 (if (derived-mode-p 'org-mode)
8457 (if (and confirm
8458 (not (y-or-n-p "Archive this subtree or entry? ")))
8459 (error "Abort")
8460 (save-window-excursion
8461 (goto-char pos)
8462 (let ((org-agenda-buffer-name bufname-orig))
8463 (org-remove-subtree-entries-from-agenda))
8464 (org-back-to-heading t)
8465 (funcall cmd)))
8466 (error "Archiving works only in Org-mode files"))))))
8468 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8469 "Remove all lines in the agenda that correspond to a given subtree.
8470 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8471 If this information is not given, the function uses the tree at point."
8472 (let ((buf (or buf (current-buffer))) m p)
8473 (save-excursion
8474 (unless (and beg end)
8475 (org-back-to-heading t)
8476 (setq beg (point))
8477 (org-end-of-subtree t)
8478 (setq end (point)))
8479 (set-buffer (get-buffer org-agenda-buffer-name))
8480 (save-excursion
8481 (goto-char (point-max))
8482 (beginning-of-line 1)
8483 (while (not (bobp))
8484 (when (and (setq m (org-get-at-bol 'org-marker))
8485 (equal buf (marker-buffer m))
8486 (setq p (marker-position m))
8487 (>= p beg)
8488 (< p end))
8489 (let ((inhibit-read-only t))
8490 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8491 (beginning-of-line 0))))))
8493 (defun org-agenda-refile (&optional goto rfloc no-update)
8494 "Refile the item at point.
8496 When GOTO is 0 or '(64), clear the refile cache.
8497 When GOTO is '(16), go to the location of the last refiled item.
8498 RFLOC can be a refile location obtained in a different way.
8499 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8500 (interactive "P")
8501 (cond
8502 ((member goto '(0 (64)))
8503 (org-refile-cache-clear))
8504 ((equal goto '(16))
8505 (org-refile-goto-last-stored))
8507 (let* ((buffer-orig (buffer-name))
8508 (marker (or (org-get-at-bol 'org-hd-marker)
8509 (org-agenda-error)))
8510 (buffer (marker-buffer marker))
8511 (pos (marker-position marker))
8512 (rfloc (or rfloc
8513 (org-refile-get-location
8514 (if goto "Goto" "Refile to") buffer
8515 org-refile-allow-creating-parent-nodes))))
8516 (with-current-buffer buffer
8517 (save-excursion
8518 (save-restriction
8519 (widen)
8520 (goto-char marker)
8521 (let ((org-agenda-buffer-name buffer-orig))
8522 (org-remove-subtree-entries-from-agenda))
8523 (org-refile goto buffer rfloc)))))
8524 (unless no-update (org-agenda-redo)))))
8526 (defun org-agenda-open-link (&optional arg)
8527 "Open the link(s) in the current entry, if any.
8528 This looks for a link in the displayed line in the agenda.
8529 It also looks at the text of the entry itself."
8530 (interactive "P")
8531 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8532 (org-get-at-bol 'org-marker)))
8533 (buffer (and marker (marker-buffer marker)))
8534 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8535 (lkall (and buffer (org-offer-links-in-entry
8536 buffer marker arg prefix)))
8537 (lk0 (car lkall))
8538 (lk (if (stringp lk0) (list lk0) lk0))
8539 (lkend (cdr lkall))
8540 trg)
8541 (cond
8542 ((and buffer lk)
8543 (mapcar (lambda(l)
8544 (with-current-buffer buffer
8545 (setq trg (and (string-match org-bracket-link-regexp l)
8546 (match-string 1 l)))
8547 (if (or (not trg) (string-match org-any-link-re trg))
8548 (save-excursion
8549 (save-restriction
8550 (widen)
8551 (goto-char marker)
8552 (when (search-forward l nil lkend)
8553 (goto-char (match-beginning 0))
8554 (org-open-at-point))))
8555 ;; This is an internal link, widen the buffer
8556 (switch-to-buffer-other-window buffer)
8557 (widen)
8558 (goto-char marker)
8559 (when (search-forward l nil lkend)
8560 (goto-char (match-beginning 0))
8561 (org-open-at-point)))))
8562 lk))
8563 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8564 (save-excursion
8565 (beginning-of-line 1)
8566 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8567 (org-open-link-from-string (match-string 1)))
8568 (t (message "No link to open here")))))
8570 (defun org-agenda-copy-local-variable (var)
8571 "Get a variable from a referenced buffer and install it here."
8572 (let ((m (org-get-at-bol 'org-marker)))
8573 (when (and m (buffer-live-p (marker-buffer m)))
8574 (org-set-local var (with-current-buffer (marker-buffer m)
8575 (symbol-value var))))))
8577 (defun org-agenda-switch-to (&optional delete-other-windows)
8578 "Go to the Org-mode file which contains the item at point."
8579 (interactive)
8580 (if (and org-return-follows-link
8581 (not (org-get-at-bol 'org-marker))
8582 (org-in-regexp org-bracket-link-regexp))
8583 (org-open-link-from-string (match-string 0))
8584 (let* ((marker (or (org-get-at-bol 'org-marker)
8585 (org-agenda-error)))
8586 (buffer (marker-buffer marker))
8587 (pos (marker-position marker)))
8588 (org-pop-to-buffer-same-window buffer)
8589 (and delete-other-windows (delete-other-windows))
8590 (widen)
8591 (goto-char pos)
8592 (org-back-to-heading t)
8593 (when (derived-mode-p 'org-mode)
8594 (org-show-context 'agenda)
8595 (save-excursion
8596 (and (outline-next-heading)
8597 (org-flag-heading nil))) ; show the next heading
8598 (when (outline-invisible-p)
8599 (show-entry)) ; display invisible text
8600 (run-hooks 'org-agenda-after-show-hook)))))
8602 (defun org-agenda-goto-mouse (ev)
8603 "Go to the Org-mode file which contains the item at the mouse click."
8604 (interactive "e")
8605 (mouse-set-point ev)
8606 (org-agenda-goto))
8608 (defun org-agenda-show (&optional full-entry)
8609 "Display the Org-mode file which contains the item at point.
8610 With prefix argument FULL-ENTRY, make the entire entry visible
8611 if it was hidden in the outline."
8612 (interactive "P")
8613 (let ((win (selected-window)))
8614 (if full-entry
8615 (let ((org-show-entry-below t))
8616 (org-agenda-goto t))
8617 (org-agenda-goto t))
8618 (select-window win)))
8620 (defvar org-agenda-show-window nil)
8621 (defun org-agenda-show-and-scroll-up (&optional arg)
8622 "Display the Org-mode file which contains the item at point.
8623 When called repeatedly, scroll the window that is displaying the buffer.
8624 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8625 `show-subtree' to display the item, so that drawers and logbooks stay
8626 folded."
8627 (interactive "P")
8628 (let ((win (selected-window)))
8629 (if (and (window-live-p org-agenda-show-window)
8630 (eq this-command last-command))
8631 (progn
8632 (select-window org-agenda-show-window)
8633 (ignore-errors (scroll-up)))
8634 (org-agenda-goto t)
8635 (if arg (org-show-entry) (show-subtree))
8636 (setq org-agenda-show-window (selected-window)))
8637 (select-window win)))
8639 (defun org-agenda-show-scroll-down ()
8640 "Scroll down the window showing the agenda."
8641 (interactive)
8642 (let ((win (selected-window)))
8643 (when (window-live-p org-agenda-show-window)
8644 (select-window org-agenda-show-window)
8645 (ignore-errors (scroll-down))
8646 (select-window win))))
8648 (defun org-agenda-show-1 (&optional more)
8649 "Display the Org-mode file which contains the item at point.
8650 The prefix arg selects the amount of information to display:
8652 0 hide the subtree
8653 1 just show the entry according to defaults.
8654 2 show the children view
8655 3 show the subtree view
8656 4 show the entire subtree and any LOGBOOK drawers
8657 5 show the entire subtree and any drawers
8658 With prefix argument FULL-ENTRY, make the entire entry visible
8659 if it was hidden in the outline."
8660 (interactive "p")
8661 (let ((win (selected-window)))
8662 (org-agenda-goto t)
8663 (org-back-to-heading)
8664 (set-window-start (selected-window) (point-at-bol))
8665 (cond
8666 ((= more 0)
8667 (hide-subtree)
8668 (save-excursion
8669 (org-back-to-heading)
8670 (run-hook-with-args 'org-cycle-hook 'folded))
8671 (message "Remote: FOLDED"))
8672 ((and (org-called-interactively-p 'any) (= more 1))
8673 (message "Remote: show with default settings"))
8674 ((= more 2)
8675 (show-entry)
8676 (show-children)
8677 (save-excursion
8678 (org-back-to-heading)
8679 (run-hook-with-args 'org-cycle-hook 'children))
8680 (message "Remote: CHILDREN"))
8681 ((= more 3)
8682 (show-subtree)
8683 (save-excursion
8684 (org-back-to-heading)
8685 (run-hook-with-args 'org-cycle-hook 'subtree))
8686 (message "Remote: SUBTREE"))
8687 ((= more 4)
8688 (show-subtree)
8689 (save-excursion
8690 (org-back-to-heading)
8691 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8692 (message "Remote: SUBTREE AND LOGBOOK"))
8693 ((> more 4)
8694 (show-subtree)
8695 (message "Remote: SUBTREE AND ALL DRAWERS")))
8696 (select-window win)))
8698 (defvar org-agenda-cycle-counter nil)
8699 (defun org-agenda-cycle-show (&optional n)
8700 "Show the current entry in another window, with default settings.
8701 Default settings are taken from `org-show-hierarchy-above' and siblings.
8702 When use repeatedly in immediate succession, the remote entry will cycle
8703 through visibility
8705 children -> subtree -> folded
8707 When called with a numeric prefix arg, that arg will be passed through to
8708 `org-agenda-show-1'. For the interpretation of that argument, see the
8709 docstring of `org-agenda-show-1'."
8710 (interactive "P")
8711 (if (integerp n)
8712 (setq org-agenda-cycle-counter n)
8713 (if (not (eq last-command this-command))
8714 (setq org-agenda-cycle-counter 1)
8715 (if (equal org-agenda-cycle-counter 0)
8716 (setq org-agenda-cycle-counter 2)
8717 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8718 (if (> org-agenda-cycle-counter 3)
8719 (setq org-agenda-cycle-counter 0)))))
8720 (org-agenda-show-1 org-agenda-cycle-counter))
8722 (defun org-agenda-recenter (arg)
8723 "Display the Org-mode file which contains the item at point and recenter."
8724 (interactive "P")
8725 (let ((win (selected-window)))
8726 (org-agenda-goto t)
8727 (recenter arg)
8728 (select-window win)))
8730 (defun org-agenda-show-mouse (ev)
8731 "Display the Org-mode file which contains the item at the mouse click."
8732 (interactive "e")
8733 (mouse-set-point ev)
8734 (org-agenda-show))
8736 (defun org-agenda-check-no-diary ()
8737 "Check if the entry is a diary link and abort if yes."
8738 (if (org-get-at-bol 'org-agenda-diary-link)
8739 (org-agenda-error)))
8741 (defun org-agenda-error ()
8742 "Throw an error when a command is not allowed in the agenda."
8743 (user-error "Command not allowed in this line"))
8745 (defun org-agenda-tree-to-indirect-buffer (arg)
8746 "Show the subtree corresponding to the current entry in an indirect buffer.
8747 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8749 With a numerical prefix ARG, go up to this level and then take that tree.
8750 With a negative numeric ARG, go up by this number of levels.
8751 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8752 use the dedicated frame)."
8753 (interactive "P")
8754 (if current-prefix-arg
8755 (org-agenda-do-tree-to-indirect-buffer arg)
8756 (let ((agenda-buffer (buffer-name))
8757 (agenda-window (selected-window))
8758 (indirect-window
8759 (and org-last-indirect-buffer
8760 (get-buffer-window org-last-indirect-buffer))))
8761 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8762 (unless (or (eq org-indirect-buffer-display 'new-frame)
8763 (eq org-indirect-buffer-display 'dedicated-frame))
8764 (unwind-protect
8765 (unless (and indirect-window (window-live-p indirect-window))
8766 (setq indirect-window (split-window agenda-window)))
8767 (and indirect-window (select-window indirect-window))
8768 (switch-to-buffer org-last-indirect-buffer :norecord)
8769 (fit-window-to-buffer indirect-window)))
8770 (select-window (get-buffer-window agenda-buffer)))))
8772 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8773 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8774 (org-agenda-check-no-diary)
8775 (let* ((marker (or (org-get-at-bol 'org-marker)
8776 (org-agenda-error)))
8777 (buffer (marker-buffer marker))
8778 (pos (marker-position marker)))
8779 (with-current-buffer buffer
8780 (save-excursion
8781 (goto-char pos)
8782 (funcall 'org-tree-to-indirect-buffer arg)))))
8784 (defvar org-last-heading-marker (make-marker)
8785 "Marker pointing to the headline that last changed its TODO state
8786 by a remote command from the agenda.")
8788 (defun org-agenda-todo-nextset ()
8789 "Switch TODO entry to next sequence."
8790 (interactive)
8791 (org-agenda-todo 'nextset))
8793 (defun org-agenda-todo-previousset ()
8794 "Switch TODO entry to previous sequence."
8795 (interactive)
8796 (org-agenda-todo 'previousset))
8798 (defun org-agenda-todo (&optional arg)
8799 "Cycle TODO state of line at point, also in Org-mode file.
8800 This changes the line at point, all other lines in the agenda referring to
8801 the same tree node, and the headline of the tree node in the Org-mode file."
8802 (interactive "P")
8803 (org-agenda-check-no-diary)
8804 (let* ((col (current-column))
8805 (marker (or (org-get-at-bol 'org-marker)
8806 (org-agenda-error)))
8807 (buffer (marker-buffer marker))
8808 (pos (marker-position marker))
8809 (hdmarker (org-get-at-bol 'org-hd-marker))
8810 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8811 (inhibit-read-only t)
8812 org-agenda-headline-snapshot-before-repeat newhead just-one)
8813 (org-with-remote-undo buffer
8814 (with-current-buffer buffer
8815 (widen)
8816 (goto-char pos)
8817 (org-show-context 'agenda)
8818 (save-excursion
8819 (and (outline-next-heading)
8820 (org-flag-heading nil))) ; show the next heading
8821 (let ((current-prefix-arg arg))
8822 (call-interactively 'org-todo))
8823 (and (bolp) (forward-char 1))
8824 (setq newhead (org-get-heading))
8825 (when (and (org-bound-and-true-p
8826 org-agenda-headline-snapshot-before-repeat)
8827 (not (equal org-agenda-headline-snapshot-before-repeat
8828 newhead))
8829 todayp)
8830 (setq newhead org-agenda-headline-snapshot-before-repeat
8831 just-one t))
8832 (save-excursion
8833 (org-back-to-heading)
8834 (move-marker org-last-heading-marker (point))))
8835 (beginning-of-line 1)
8836 (save-window-excursion
8837 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8838 (when (org-bound-and-true-p org-clock-out-when-done)
8839 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8840 newhead)
8841 (org-agenda-unmark-clocking-task))
8842 (org-move-to-column col)
8843 (org-agenda-mark-clocking-task))))
8845 (defun org-agenda-add-note (&optional arg)
8846 "Add a time-stamped note to the entry at point."
8847 (interactive "P")
8848 (org-agenda-check-no-diary)
8849 (let* ((marker (or (org-get-at-bol 'org-marker)
8850 (org-agenda-error)))
8851 (buffer (marker-buffer marker))
8852 (pos (marker-position marker))
8853 (hdmarker (org-get-at-bol 'org-hd-marker))
8854 (inhibit-read-only t))
8855 (with-current-buffer buffer
8856 (widen)
8857 (goto-char pos)
8858 (org-show-context 'agenda)
8859 (save-excursion
8860 (and (outline-next-heading)
8861 (org-flag-heading nil))) ; show the next heading
8862 (org-add-note))))
8864 (defun org-agenda-change-all-lines (newhead hdmarker
8865 &optional fixface just-this)
8866 "Change all lines in the agenda buffer which match HDMARKER.
8867 The new content of the line will be NEWHEAD (as modified by
8868 `org-agenda-format-item'). HDMARKER is checked with
8869 `equal' against all `org-hd-marker' text properties in the file.
8870 If FIXFACE is non-nil, the face of each item is modified according to
8871 the new TODO state.
8872 If JUST-THIS is non-nil, change just the current line, not all.
8873 If FORCE-TAGS is non nil, the car of it returns the new tags."
8874 (let* ((inhibit-read-only t)
8875 (line (org-current-line))
8876 (org-agenda-buffer (current-buffer))
8877 (thetags (with-current-buffer (marker-buffer hdmarker)
8878 (save-excursion (save-restriction (widen)
8879 (goto-char hdmarker)
8880 (org-get-tags-at)))))
8881 props m pl undone-face done-face finish new dotime level cat tags)
8882 (save-excursion
8883 (goto-char (point-max))
8884 (beginning-of-line 1)
8885 (while (not finish)
8886 (setq finish (bobp))
8887 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8888 (or (not just-this) (= (org-current-line) line))
8889 (equal m hdmarker))
8890 (setq props (text-properties-at (point))
8891 dotime (org-get-at-bol 'dotime)
8892 cat (org-get-at-eol 'org-category 1)
8893 level (org-get-at-bol 'level)
8894 tags thetags
8896 (let ((org-prefix-format-compiled
8897 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8898 org-prefix-format-compiled))
8899 (extra (org-get-at-bol 'extra)))
8900 (with-current-buffer (marker-buffer hdmarker)
8901 (save-excursion
8902 (save-restriction
8903 (widen)
8904 (org-agenda-format-item extra newhead level cat tags dotime)))))
8905 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8906 undone-face (org-get-at-bol 'undone-face)
8907 done-face (org-get-at-bol 'done-face))
8908 (beginning-of-line 1)
8909 (cond
8910 ((equal new "")
8911 (and (looking-at ".*\n?") (replace-match "")))
8912 ((looking-at ".*")
8913 (replace-match new t t)
8914 (beginning-of-line 1)
8915 (add-text-properties (point-at-bol) (point-at-eol) props)
8916 (when fixface
8917 (add-text-properties
8918 (point-at-bol) (point-at-eol)
8919 (list 'face
8920 (if org-last-todo-state-is-todo
8921 undone-face done-face))))
8922 (org-agenda-highlight-todo 'line)
8923 (beginning-of-line 1))
8924 (t (error "Line update did not work")))
8925 (save-restriction
8926 (narrow-to-region (point-at-bol) (point-at-eol))
8927 (org-agenda-finalize)))
8928 (beginning-of-line 0)))))
8930 (defun org-agenda-align-tags (&optional line)
8931 "Align all tags in agenda items to `org-agenda-tags-column'."
8932 (let ((inhibit-read-only t) l c)
8933 (save-excursion
8934 (goto-char (if line (point-at-bol) (point-min)))
8935 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8936 (if line (point-at-eol) nil) t)
8937 (add-text-properties
8938 (match-beginning 2) (match-end 2)
8939 (list 'face (delq nil (let ((prop (get-text-property
8940 (match-beginning 2) 'face)))
8941 (or (listp prop) (setq prop (list prop)))
8942 (if (memq 'org-tag prop)
8943 prop
8944 (cons 'org-tag prop))))))
8945 (setq l (- (match-end 2) (match-beginning 2))
8946 c (if (< org-agenda-tags-column 0)
8947 (- (abs org-agenda-tags-column) l)
8948 org-agenda-tags-column))
8949 (delete-region (match-beginning 1) (match-end 1))
8950 (goto-char (match-beginning 1))
8951 (insert (org-add-props
8952 (make-string (max 1 (- c (current-column))) ?\ )
8953 (plist-put (copy-sequence (text-properties-at (point)))
8954 'face nil))))
8955 (goto-char (point-min))
8956 (org-font-lock-add-tag-faces (point-max)))))
8958 (defun org-agenda-priority-up ()
8959 "Increase the priority of line at point, also in Org-mode file."
8960 (interactive)
8961 (org-agenda-priority 'up))
8963 (defun org-agenda-priority-down ()
8964 "Decrease the priority of line at point, also in Org-mode file."
8965 (interactive)
8966 (org-agenda-priority 'down))
8968 (defun org-agenda-priority (&optional force-direction)
8969 "Set the priority of line at point, also in Org-mode file.
8970 This changes the line at point, all other lines in the agenda referring to
8971 the same tree node, and the headline of the tree node in the Org-mode file.
8972 Called with a universal prefix arg, show the priority instead of setting it."
8973 (interactive "P")
8974 (if (equal force-direction '(4))
8975 (org-show-priority)
8976 (unless org-enable-priority-commands
8977 (error "Priority commands are disabled"))
8978 (org-agenda-check-no-diary)
8979 (let* ((col (current-column))
8980 (marker (or (org-get-at-bol 'org-marker)
8981 (org-agenda-error)))
8982 (hdmarker (org-get-at-bol 'org-hd-marker))
8983 (buffer (marker-buffer hdmarker))
8984 (pos (marker-position hdmarker))
8985 (inhibit-read-only t)
8986 newhead)
8987 (org-with-remote-undo buffer
8988 (with-current-buffer buffer
8989 (widen)
8990 (goto-char pos)
8991 (org-show-context 'agenda)
8992 (save-excursion
8993 (and (outline-next-heading)
8994 (org-flag-heading nil))) ; show the next heading
8995 (funcall 'org-priority force-direction)
8996 (end-of-line 1)
8997 (setq newhead (org-get-heading)))
8998 (org-agenda-change-all-lines newhead hdmarker)
8999 (org-move-to-column col)))))
9001 ;; FIXME: should fix the tags property of the agenda line.
9002 (defun org-agenda-set-tags (&optional tag onoff)
9003 "Set tags for the current headline."
9004 (interactive)
9005 (org-agenda-check-no-diary)
9006 (if (and (org-region-active-p) (org-called-interactively-p 'any))
9007 (call-interactively 'org-change-tag-in-region)
9008 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9009 (org-agenda-error)))
9010 (buffer (marker-buffer hdmarker))
9011 (pos (marker-position hdmarker))
9012 (inhibit-read-only t)
9013 newhead)
9014 (org-with-remote-undo buffer
9015 (with-current-buffer buffer
9016 (widen)
9017 (goto-char pos)
9018 (save-excursion
9019 (org-show-context 'agenda))
9020 (save-excursion
9021 (and (outline-next-heading)
9022 (org-flag-heading nil))) ; show the next heading
9023 (goto-char pos)
9024 (if tag
9025 (org-toggle-tag tag onoff)
9026 (call-interactively 'org-set-tags))
9027 (end-of-line 1)
9028 (setq newhead (org-get-heading)))
9029 (org-agenda-change-all-lines newhead hdmarker)
9030 (beginning-of-line 1)))))
9032 (defun org-agenda-set-property ()
9033 "Set a property for the current headline."
9034 (interactive)
9035 (org-agenda-check-no-diary)
9036 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9037 (org-agenda-error)))
9038 (buffer (marker-buffer hdmarker))
9039 (pos (marker-position hdmarker))
9040 (inhibit-read-only t)
9041 newhead)
9042 (org-with-remote-undo buffer
9043 (with-current-buffer buffer
9044 (widen)
9045 (goto-char pos)
9046 (save-excursion
9047 (org-show-context 'agenda))
9048 (save-excursion
9049 (and (outline-next-heading)
9050 (org-flag-heading nil))) ; show the next heading
9051 (goto-char pos)
9052 (call-interactively 'org-set-property)))))
9054 (defun org-agenda-set-effort ()
9055 "Set the effort property for the current headline."
9056 (interactive)
9057 (org-agenda-check-no-diary)
9058 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9059 (org-agenda-error)))
9060 (buffer (marker-buffer hdmarker))
9061 (pos (marker-position hdmarker))
9062 (inhibit-read-only t)
9063 newhead)
9064 (org-with-remote-undo buffer
9065 (with-current-buffer buffer
9066 (widen)
9067 (goto-char pos)
9068 (save-excursion
9069 (org-show-context 'agenda))
9070 (save-excursion
9071 (and (outline-next-heading)
9072 (org-flag-heading nil))) ; show the next heading
9073 (goto-char pos)
9074 (call-interactively 'org-set-effort)
9075 (end-of-line 1)
9076 (setq newhead (org-get-heading)))
9077 (org-agenda-change-all-lines newhead hdmarker))))
9079 (defun org-agenda-toggle-archive-tag ()
9080 "Toggle the archive tag for the current entry."
9081 (interactive)
9082 (org-agenda-check-no-diary)
9083 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9084 (org-agenda-error)))
9085 (buffer (marker-buffer hdmarker))
9086 (pos (marker-position hdmarker))
9087 (inhibit-read-only t)
9088 newhead)
9089 (org-with-remote-undo buffer
9090 (with-current-buffer buffer
9091 (widen)
9092 (goto-char pos)
9093 (org-show-context 'agenda)
9094 (save-excursion
9095 (and (outline-next-heading)
9096 (org-flag-heading nil))) ; show the next heading
9097 (call-interactively 'org-toggle-archive-tag)
9098 (end-of-line 1)
9099 (setq newhead (org-get-heading)))
9100 (org-agenda-change-all-lines newhead hdmarker)
9101 (beginning-of-line 1))))
9103 (defun org-agenda-do-date-later (arg)
9104 (interactive "P")
9105 (cond
9106 ((or (equal arg '(16))
9107 (memq last-command
9108 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9109 (setq this-command 'org-agenda-date-later-minutes)
9110 (org-agenda-date-later-minutes 1))
9111 ((or (equal arg '(4))
9112 (memq last-command
9113 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9114 (setq this-command 'org-agenda-date-later-hours)
9115 (org-agenda-date-later-hours 1))
9117 (org-agenda-date-later (prefix-numeric-value arg)))))
9119 (defun org-agenda-do-date-earlier (arg)
9120 (interactive "P")
9121 (cond
9122 ((or (equal arg '(16))
9123 (memq last-command
9124 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9125 (setq this-command 'org-agenda-date-earlier-minutes)
9126 (org-agenda-date-earlier-minutes 1))
9127 ((or (equal arg '(4))
9128 (memq last-command
9129 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9130 (setq this-command 'org-agenda-date-earlier-hours)
9131 (org-agenda-date-earlier-hours 1))
9133 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9135 (defun org-agenda-date-later (arg &optional what)
9136 "Change the date of this item to ARG day(s) later."
9137 (interactive "p")
9138 (org-agenda-check-type t 'agenda 'timeline)
9139 (org-agenda-check-no-diary)
9140 (let* ((marker (or (org-get-at-bol 'org-marker)
9141 (org-agenda-error)))
9142 (buffer (marker-buffer marker))
9143 (pos (marker-position marker))
9144 cdate today)
9145 (org-with-remote-undo buffer
9146 (with-current-buffer buffer
9147 (widen)
9148 (goto-char pos)
9149 (if (not (org-at-timestamp-p))
9150 (error "Cannot find time stamp"))
9151 (when (and org-agenda-move-date-from-past-immediately-to-today
9152 (equal arg 1)
9153 (or (not what) (eq what 'day))
9154 (not (save-match-data (org-at-date-range-p))))
9155 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9156 cdate (calendar-absolute-from-gregorian
9157 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9158 today (org-today))
9159 (if (> today cdate)
9160 ;; immediately shift to today
9161 (setq arg (- today cdate))))
9162 (org-timestamp-change arg (or what 'day))
9163 (when (and (org-at-date-range-p)
9164 (re-search-backward org-tr-regexp-both (point-at-bol)))
9165 (let ((end org-last-changed-timestamp))
9166 (org-timestamp-change arg (or what 'day))
9167 (setq org-last-changed-timestamp
9168 (concat org-last-changed-timestamp "--" end)))))
9169 (org-agenda-show-new-time marker org-last-changed-timestamp))
9170 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9172 (defun org-agenda-date-earlier (arg &optional what)
9173 "Change the date of this item to ARG day(s) earlier."
9174 (interactive "p")
9175 (org-agenda-date-later (- arg) what))
9177 (defun org-agenda-date-later-minutes (arg)
9178 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9179 (interactive "p")
9180 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9181 (org-agenda-date-later arg 'minute))
9183 (defun org-agenda-date-earlier-minutes (arg)
9184 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9185 (interactive "p")
9186 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9187 (org-agenda-date-earlier arg 'minute))
9189 (defun org-agenda-date-later-hours (arg)
9190 "Change the time of this item, in hour steps."
9191 (interactive "p")
9192 (org-agenda-date-later arg 'hour))
9194 (defun org-agenda-date-earlier-hours (arg)
9195 "Change the time of this item, in hour steps."
9196 (interactive "p")
9197 (org-agenda-date-earlier arg 'hour))
9199 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9200 "Show new date stamp via text properties."
9201 ;; We use text properties to make this undoable
9202 (let ((inhibit-read-only t))
9203 (setq stamp (concat prefix " => " stamp " "))
9204 (save-excursion
9205 (goto-char (point-max))
9206 (while (not (bobp))
9207 (when (equal marker (org-get-at-bol 'org-marker))
9208 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9209 (org-move-to-column (- (window-width) (length stamp)) t)
9210 (if (featurep 'xemacs)
9211 ;; Use `duplicable' property to trigger undo recording
9212 (let ((ex (make-extent nil nil))
9213 (gl (make-glyph stamp)))
9214 (set-glyph-face gl 'secondary-selection)
9215 (set-extent-properties
9216 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9217 (insert-extent ex (1- (point)) (point-at-eol)))
9218 (add-text-properties
9219 (1- (point)) (point-at-eol)
9220 (list 'display (org-add-props stamp nil
9221 'face '(secondary-selection default)))))
9222 (beginning-of-line 1))
9223 (beginning-of-line 0)))))
9225 (defun org-agenda-date-prompt (arg)
9226 "Change the date of this item. Date is prompted for, with default today.
9227 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9228 be used to request time specification in the time stamp."
9229 (interactive "P")
9230 (org-agenda-check-type t 'agenda 'timeline)
9231 (org-agenda-check-no-diary)
9232 (let* ((marker (or (org-get-at-bol 'org-marker)
9233 (org-agenda-error)))
9234 (buffer (marker-buffer marker))
9235 (pos (marker-position marker)))
9236 (org-with-remote-undo buffer
9237 (with-current-buffer buffer
9238 (widen)
9239 (goto-char pos)
9240 (if (not (org-at-timestamp-p t))
9241 (error "Cannot find time stamp"))
9242 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9243 (org-agenda-show-new-time marker org-last-changed-timestamp))
9244 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9246 (defun org-agenda-schedule (arg &optional time)
9247 "Schedule the item at point.
9248 ARG is passed through to `org-schedule'."
9249 (interactive "P")
9250 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9251 (org-agenda-check-no-diary)
9252 (let* ((marker (or (org-get-at-bol 'org-marker)
9253 (org-agenda-error)))
9254 (type (marker-insertion-type marker))
9255 (buffer (marker-buffer marker))
9256 (pos (marker-position marker))
9257 (org-insert-labeled-timestamps-at-point nil)
9259 (set-marker-insertion-type marker t)
9260 (org-with-remote-undo buffer
9261 (with-current-buffer buffer
9262 (widen)
9263 (goto-char pos)
9264 (setq ts (org-schedule arg time)))
9265 (org-agenda-show-new-time marker ts " S"))
9266 (message "%s" ts)))
9268 (defun org-agenda-deadline (arg &optional time)
9269 "Schedule the item at point.
9270 ARG is passed through to `org-deadline'."
9271 (interactive "P")
9272 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9273 (org-agenda-check-no-diary)
9274 (let* ((marker (or (org-get-at-bol 'org-marker)
9275 (org-agenda-error)))
9276 (buffer (marker-buffer marker))
9277 (pos (marker-position marker))
9278 (org-insert-labeled-timestamps-at-point nil)
9280 (org-with-remote-undo buffer
9281 (with-current-buffer buffer
9282 (widen)
9283 (goto-char pos)
9284 (setq ts (org-deadline arg time)))
9285 (org-agenda-show-new-time marker ts " D"))
9286 (message "%s" ts)))
9288 (defun org-agenda-clock-in (&optional arg)
9289 "Start the clock on the currently selected item."
9290 (interactive "P")
9291 (org-agenda-check-no-diary)
9292 (if (equal arg '(4))
9293 (org-clock-in arg)
9294 (let* ((marker (or (org-get-at-bol 'org-marker)
9295 (org-agenda-error)))
9296 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9297 (pos (marker-position marker))
9298 (col (current-column))
9299 newhead)
9300 (org-with-remote-undo (marker-buffer marker)
9301 (with-current-buffer (marker-buffer marker)
9302 (widen)
9303 (goto-char pos)
9304 (org-show-context 'agenda)
9305 (org-show-entry)
9306 (org-cycle-hide-drawers 'children)
9307 (org-clock-in arg)
9308 (setq newhead (org-get-heading)))
9309 (org-agenda-change-all-lines newhead hdmarker))
9310 (org-move-to-column col))))
9312 (defun org-agenda-clock-out ()
9313 "Stop the currently running clock."
9314 (interactive)
9315 (unless (marker-buffer org-clock-marker)
9316 (error "No running clock"))
9317 (let ((marker (make-marker)) (col (current-column)) newhead)
9318 (org-with-remote-undo (marker-buffer org-clock-marker)
9319 (with-current-buffer (marker-buffer org-clock-marker)
9320 (save-excursion
9321 (save-restriction
9322 (widen)
9323 (goto-char org-clock-marker)
9324 (org-back-to-heading t)
9325 (move-marker marker (point))
9326 (org-clock-out)
9327 (setq newhead (org-get-heading))))))
9328 (org-agenda-change-all-lines newhead marker)
9329 (move-marker marker nil)
9330 (org-move-to-column col)
9331 (org-agenda-unmark-clocking-task)))
9333 (defun org-agenda-clock-cancel (&optional arg)
9334 "Cancel the currently running clock."
9335 (interactive "P")
9336 (unless (marker-buffer org-clock-marker)
9337 (user-error "No running clock"))
9338 (org-with-remote-undo (marker-buffer org-clock-marker)
9339 (org-clock-cancel)))
9341 (defun org-agenda-clock-goto ()
9342 "Jump to the currently clocked in task within the agenda.
9343 If the currently clocked in task is not listed in the agenda
9344 buffer, display it in another window."
9345 (interactive)
9346 (let (pos)
9347 (mapc (lambda (o)
9348 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9349 (setq pos (overlay-start o))))
9350 (overlays-in (point-min) (point-max)))
9351 (cond (pos (goto-char pos))
9352 ;; If the currently clocked entry is not in the agenda
9353 ;; buffer, we visit it in another window:
9354 (org-clock-current-task
9355 (org-switch-to-buffer-other-window (org-clock-goto)))
9356 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9358 (defun org-agenda-diary-entry-in-org-file ()
9359 "Make a diary entry in the file `org-agenda-diary-file'."
9360 (let (d1 d2 char (text "") dp1 dp2)
9361 (if (equal (buffer-name) "*Calendar*")
9362 (setq d1 (calendar-cursor-to-date t)
9363 d2 (car calendar-mark-ring))
9364 (setq dp1 (get-text-property (point-at-bol) 'day))
9365 (unless dp1 (user-error "No date defined in current line"))
9366 (setq d1 (calendar-gregorian-from-absolute dp1)
9367 d2 (and (ignore-errors (mark))
9368 (save-excursion
9369 (goto-char (mark))
9370 (setq dp2 (get-text-property (point-at-bol) 'day)))
9371 (calendar-gregorian-from-absolute dp2))))
9372 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9373 (setq char (read-char-exclusive))
9374 (cond
9375 ((equal char ?d)
9376 (setq text (read-string "Day entry: "))
9377 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9378 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9379 ((equal char ?a)
9380 (setq d1 (list (car d1) (nth 1 d1)
9381 (read-number (format "Reference year [%d]: " (nth 2 d1))
9382 (nth 2 d1))))
9383 (setq text (read-string "Anniversary (use %d to show years): "))
9384 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9385 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9386 ((equal char ?b)
9387 (setq text (read-string "Block entry: "))
9388 (unless (and d1 d2 (not (equal d1 d2)))
9389 (user-error "No block of days selected"))
9390 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9391 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9392 ((equal char ?j)
9393 (org-switch-to-buffer-other-window
9394 (find-file-noselect org-agenda-diary-file))
9395 (require 'org-datetree)
9396 (org-datetree-find-date-create d1)
9397 (org-reveal t))
9398 (t (user-error "Invalid selection character `%c'" char)))))
9400 (defcustom org-agenda-insert-diary-strategy 'date-tree
9401 "Where in `org-agenda-diary-file' should new entries be added?
9402 Valid values:
9404 date-tree in the date tree, as child of the date
9405 top-level as top-level entries at the end of the file."
9406 :group 'org-agenda
9407 :type '(choice
9408 (const :tag "in a date tree" date-tree)
9409 (const :tag "as top level at end of file" top-level)))
9411 (defcustom org-agenda-insert-diary-extract-time nil
9412 "Non-nil means extract any time specification from the diary entry."
9413 :group 'org-agenda
9414 :version "24.1"
9415 :type 'boolean)
9417 (defcustom org-agenda-bulk-mark-char ">"
9418 "A single-character string to be used as the bulk mark."
9419 :group 'org-agenda
9420 :version "24.1"
9421 :type 'string)
9423 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9424 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9425 If TEXT is not empty, it will become the headline of the new entry, and
9426 the resulting entry will not be shown. When TEXT is empty, switch to
9427 `org-agenda-diary-file' and let the user finish the entry there."
9428 (let ((cw (current-window-configuration)))
9429 (org-switch-to-buffer-other-window
9430 (find-file-noselect org-agenda-diary-file))
9431 (widen)
9432 (goto-char (point-min))
9433 (cond
9434 ((eq type 'anniversary)
9435 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9436 (progn
9437 (or (org-at-heading-p t)
9438 (progn
9439 (outline-next-heading)
9440 (insert "* Anniversaries\n\n")
9441 (beginning-of-line -1)))))
9442 (outline-next-heading)
9443 (org-back-over-empty-lines)
9444 (backward-char 1)
9445 (insert "\n")
9446 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9447 (nth 2 d1) (car d1) (nth 1 d1) text)))
9448 ((eq type 'day)
9449 (let ((org-prefix-has-time t)
9450 (org-agenda-time-leading-zero t)
9451 fmt time time2)
9452 (if org-agenda-insert-diary-extract-time
9453 ;; Use org-agenda-format-item to parse text for a time-range and
9454 ;; remove it. FIXME: This is a hack, we should refactor
9455 ;; that function to make time extraction available separately
9456 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9457 time (get-text-property 0 'time fmt)
9458 time2 (if (> (length time) 0)
9459 ;; split-string removes trailing ...... if
9460 ;; no end time given. First space
9461 ;; separates time from date.
9462 (concat " " (car (split-string time "\\.")))
9463 nil)
9464 text (get-text-property 0 'txt fmt)))
9465 (if (eq org-agenda-insert-diary-strategy 'top-level)
9466 (org-agenda-insert-diary-as-top-level text)
9467 (require 'org-datetree)
9468 (org-datetree-find-date-create d1)
9469 (org-agenda-insert-diary-make-new-entry text))
9470 (org-insert-time-stamp (org-time-from-absolute
9471 (calendar-absolute-from-gregorian d1))
9472 nil nil nil nil time2))
9473 (end-of-line 0))
9474 ((eq type 'block)
9475 (if (> (calendar-absolute-from-gregorian d1)
9476 (calendar-absolute-from-gregorian d2))
9477 (setq d1 (prog1 d2 (setq d2 d1))))
9478 (if (eq org-agenda-insert-diary-strategy 'top-level)
9479 (org-agenda-insert-diary-as-top-level text)
9480 (require 'org-datetree)
9481 (org-datetree-find-date-create d1)
9482 (org-agenda-insert-diary-make-new-entry text))
9483 (org-insert-time-stamp (org-time-from-absolute
9484 (calendar-absolute-from-gregorian d1)))
9485 (insert "--")
9486 (org-insert-time-stamp (org-time-from-absolute
9487 (calendar-absolute-from-gregorian d2)))
9488 (end-of-line 0)))
9489 (if (string-match "\\S-" text)
9490 (progn
9491 (set-window-configuration cw)
9492 (message "%s entry added to %s"
9493 (capitalize (symbol-name type))
9494 (abbreviate-file-name org-agenda-diary-file)))
9495 (org-reveal t)
9496 (message "Please finish entry here"))))
9498 (defun org-agenda-insert-diary-as-top-level (text)
9499 "Make new entry as a top-level entry at the end of the file.
9500 Add TEXT as headline, and position the cursor in the second line so that
9501 a timestamp can be added there."
9502 (widen)
9503 (goto-char (point-max))
9504 (or (bolp) (insert "\n"))
9505 (insert "* " text "\n")
9506 (if org-adapt-indentation (org-indent-to-column 2)))
9508 (defun org-agenda-insert-diary-make-new-entry (text)
9509 "Make a new entry with TEXT as the first child of the current subtree.
9510 Position the point in the line right after the new heading so
9511 that a timestamp can be added there."
9512 (let ((org-show-following-heading t)
9513 (org-show-siblings t)
9514 (org-show-hierarchy-above t)
9515 (org-show-entry-below t)
9516 col)
9517 (outline-next-heading)
9518 (org-back-over-empty-lines)
9519 (or (looking-at "[ \t]*$")
9520 (progn (insert "\n") (backward-char 1)))
9521 (org-insert-heading nil t)
9522 (org-do-demote)
9523 (setq col (current-column))
9524 (insert text "\n")
9525 (if org-adapt-indentation (org-indent-to-column col))
9526 (let ((org-show-following-heading t)
9527 (org-show-siblings t)
9528 (org-show-hierarchy-above t)
9529 (org-show-entry-below t))
9530 (org-show-context))))
9532 (defun org-agenda-diary-entry ()
9533 "Make a diary entry, like the `i' command from the calendar.
9534 All the standard commands work: block, weekly etc.
9535 When `org-agenda-diary-file' points to a file,
9536 `org-agenda-diary-entry-in-org-file' is called instead to create
9537 entries in that Org-mode file."
9538 (interactive)
9539 (if (not (eq org-agenda-diary-file 'diary-file))
9540 (org-agenda-diary-entry-in-org-file)
9541 (require 'diary-lib)
9542 (let* ((char (progn
9543 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9544 (read-char-exclusive)))
9545 (cmd (cdr (assoc char
9546 '((?d . insert-diary-entry)
9547 (?w . insert-weekly-diary-entry)
9548 (?m . insert-monthly-diary-entry)
9549 (?y . insert-yearly-diary-entry)
9550 (?a . insert-anniversary-diary-entry)
9551 (?b . insert-block-diary-entry)
9552 (?c . insert-cyclic-diary-entry)))))
9553 (oldf (symbol-function 'calendar-cursor-to-date))
9554 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9555 (point (point))
9556 (mark (or (mark t) (point))))
9557 (unless cmd
9558 (user-error "No command associated with <%c>" char))
9559 (unless (and (get-text-property point 'day)
9560 (or (not (equal ?b char))
9561 (get-text-property mark 'day)))
9562 (user-error "Don't know which date to use for diary entry"))
9563 ;; We implement this by hacking the `calendar-cursor-to-date' function
9564 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9565 (let ((calendar-mark-ring
9566 (list (calendar-gregorian-from-absolute
9567 (or (get-text-property mark 'day)
9568 (get-text-property point 'day))))))
9569 (unwind-protect
9570 (progn
9571 (fset 'calendar-cursor-to-date
9572 (lambda (&optional error dummy)
9573 (calendar-gregorian-from-absolute
9574 (get-text-property point 'day))))
9575 (call-interactively cmd))
9576 (fset 'calendar-cursor-to-date oldf))))))
9578 (defun org-agenda-execute-calendar-command (cmd)
9579 "Execute a calendar command from the agenda with date from cursor."
9580 (org-agenda-check-type t 'agenda 'timeline)
9581 (require 'diary-lib)
9582 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9583 (user-error "Don't know which date to use for the calendar command"))
9584 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9585 (point (point))
9586 (date (calendar-gregorian-from-absolute
9587 (get-text-property point 'day)))
9588 ;; the following 2 vars are needed in the calendar
9589 (displayed-month (car date))
9590 (displayed-year (nth 2 date)))
9591 (unwind-protect
9592 (progn
9593 (fset 'calendar-cursor-to-date
9594 (lambda (&optional error dummy)
9595 (calendar-gregorian-from-absolute
9596 (get-text-property point 'day))))
9597 (call-interactively cmd))
9598 (fset 'calendar-cursor-to-date oldf))))
9600 (defun org-agenda-phases-of-moon ()
9601 "Display the phases of the moon for the 3 months around the cursor date."
9602 (interactive)
9603 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9605 (defun org-agenda-holidays ()
9606 "Display the holidays for the 3 months around the cursor date."
9607 (interactive)
9608 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9610 (defvar calendar-longitude) ; defined in calendar.el
9611 (defvar calendar-latitude) ; defined in calendar.el
9612 (defvar calendar-location-name) ; defined in calendar.el
9614 (defun org-agenda-sunrise-sunset (arg)
9615 "Display sunrise and sunset for the cursor date.
9616 Latitude and longitude can be specified with the variables
9617 `calendar-latitude' and `calendar-longitude'. When called with prefix
9618 argument, latitude and longitude will be prompted for."
9619 (interactive "P")
9620 (require 'solar)
9621 (let ((calendar-longitude (if arg nil calendar-longitude))
9622 (calendar-latitude (if arg nil calendar-latitude))
9623 (calendar-location-name
9624 (if arg "the given coordinates" calendar-location-name)))
9625 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9627 (defun org-agenda-goto-calendar ()
9628 "Open the Emacs calendar with the date at the cursor."
9629 (interactive)
9630 (org-agenda-check-type t 'agenda 'timeline)
9631 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9632 (user-error "Don't know which date to open in calendar")))
9633 (date (calendar-gregorian-from-absolute day))
9634 (calendar-move-hook nil)
9635 (calendar-view-holidays-initially-flag nil)
9636 (calendar-view-diary-initially-flag nil))
9637 (calendar)
9638 (calendar-goto-date date)))
9640 ;;;###autoload
9641 (defun org-calendar-goto-agenda ()
9642 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9643 This is a command that has to be installed in `calendar-mode-map'."
9644 (interactive)
9645 (org-agenda-list nil (calendar-absolute-from-gregorian
9646 (calendar-cursor-to-date))
9647 nil))
9649 (defun org-agenda-convert-date ()
9650 (interactive)
9651 (org-agenda-check-type t 'agenda 'timeline)
9652 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9653 date s)
9654 (unless day
9655 (user-error "Don't know which date to convert"))
9656 (setq date (calendar-gregorian-from-absolute day))
9657 (setq s (concat
9658 "Gregorian: " (calendar-date-string date) "\n"
9659 "ISO: " (calendar-iso-date-string date) "\n"
9660 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9661 "Julian: " (calendar-julian-date-string date) "\n"
9662 "Astron. JD: " (calendar-astro-date-string date)
9663 " (Julian date number at noon UTC)\n"
9664 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9665 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9666 "French: " (calendar-french-date-string date) "\n"
9667 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9668 "Mayan: " (calendar-mayan-date-string date) "\n"
9669 "Coptic: " (calendar-coptic-date-string date) "\n"
9670 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9671 "Persian: " (calendar-persian-date-string date) "\n"
9672 "Chinese: " (calendar-chinese-date-string date) "\n"))
9673 (with-output-to-temp-buffer "*Dates*"
9674 (princ s))
9675 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9677 ;;; Bulk commands
9679 (defun org-agenda-bulk-marked-p ()
9680 (eq (get-char-property (point-at-bol) 'type)
9681 'org-marked-entry-overlay))
9683 (defun org-agenda-bulk-mark (&optional arg)
9684 "Mark the entry at point for future bulk action."
9685 (interactive "p")
9686 (dotimes (i (or arg 1))
9687 (unless (org-get-at-bol 'org-agenda-diary-link)
9688 (let* ((m (org-get-at-bol 'org-hd-marker))
9690 (unless (org-agenda-bulk-marked-p)
9691 (unless m (user-error "Nothing to mark at point"))
9692 (push m org-agenda-bulk-marked-entries)
9693 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9694 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9695 (org-get-todo-face "TODO")
9696 'evaporate)
9697 (overlay-put ov 'type 'org-marked-entry-overlay))
9698 (end-of-line 1)
9699 (or (ignore-errors
9700 (goto-char (next-single-property-change (point) 'txt)))
9701 (beginning-of-line 2))
9702 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9703 (beginning-of-line 2))
9704 (message "%d entries marked for bulk action"
9705 (length org-agenda-bulk-marked-entries))))))
9707 (defun org-agenda-bulk-mark-all ()
9708 "Mark all entries for future agenda bulk action."
9709 (interactive)
9710 (org-agenda-bulk-mark-regexp "."))
9712 (defun org-agenda-bulk-mark-regexp (regexp)
9713 "Mark entries matching REGEXP for future agenda bulk action."
9714 (interactive "sMark entries matching regexp: ")
9715 (let ((entries-marked 0) txt-at-point)
9716 (save-excursion
9717 (goto-char (point-min))
9718 (goto-char (next-single-property-change (point) 'txt))
9719 (while (and (re-search-forward regexp nil t)
9720 (setq txt-at-point (get-text-property (point) 'txt)))
9721 (when (string-match regexp txt-at-point)
9722 (setq entries-marked (1+ entries-marked))
9723 (call-interactively 'org-agenda-bulk-mark))))
9724 (if (not entries-marked)
9725 (message "No entry matching this regexp."))))
9727 (defun org-agenda-bulk-unmark (&optional arg)
9728 "Unmark the entry at point for future bulk action."
9729 (interactive "P")
9730 (if arg
9731 (org-agenda-bulk-unmark-all)
9732 (cond ((org-agenda-bulk-marked-p)
9733 (org-agenda-bulk-remove-overlays
9734 (point-at-bol) (+ 2 (point-at-bol)))
9735 (setq org-agenda-bulk-marked-entries
9736 (delete (org-get-at-bol 'org-hd-marker)
9737 org-agenda-bulk-marked-entries))
9738 (end-of-line 1)
9739 (or (ignore-errors
9740 (goto-char (next-single-property-change (point) 'txt)))
9741 (beginning-of-line 2))
9742 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9743 (beginning-of-line 2))
9744 (message "%d entries left marked for bulk action"
9745 (length org-agenda-bulk-marked-entries)))
9746 (t (message "No entry to unmark here")))))
9748 (defun org-agenda-bulk-toggle-all ()
9749 "Toggle all marks for bulk action."
9750 (interactive)
9751 (save-excursion
9752 (goto-char (point-min))
9753 (while (ignore-errors
9754 (goto-char (next-single-property-change (point) 'txt)))
9755 (org-agenda-bulk-toggle))))
9757 (defun org-agenda-bulk-toggle ()
9758 "Toggle the mark at point for bulk action."
9759 (interactive)
9760 (if (org-agenda-bulk-marked-p)
9761 (org-agenda-bulk-unmark)
9762 (org-agenda-bulk-mark)))
9764 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9765 "Remove the mark overlays between BEG and END in the agenda buffer.
9766 BEG and END default to the buffer limits.
9768 This only removes the overlays, it does not remove the markers
9769 from the list in `org-agenda-bulk-marked-entries'."
9770 (interactive)
9771 (mapc (lambda (ov)
9772 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9773 (delete-overlay ov)))
9774 (overlays-in (or beg (point-min)) (or end (point-max)))))
9776 (defun org-agenda-bulk-unmark-all ()
9777 "Remove all marks in the agenda buffer.
9778 This will remove the markers and the overlays."
9779 (interactive)
9780 (if (null org-agenda-bulk-marked-entries)
9781 (message "No entry to unmark")
9782 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9783 (setq org-agenda-bulk-marked-entries nil)
9784 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9786 (defcustom org-agenda-persistent-marks nil
9787 "Non-nil means marked items will stay marked after a bulk action.
9788 You can toggle this interactively by typing `p' when prompted for a
9789 bulk action."
9790 :group 'org-agenda
9791 :version "24.1"
9792 :type 'boolean)
9794 (defun org-agenda-bulk-action (&optional arg)
9795 "Execute an remote-editing action on all marked entries.
9796 The prefix arg is passed through to the command if possible."
9797 (interactive "P")
9798 ;; Make sure we have markers, and only valid ones
9799 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9800 (mapc
9801 (lambda (m)
9802 (unless (and (markerp m)
9803 (marker-buffer m)
9804 (buffer-live-p (marker-buffer m))
9805 (marker-position m))
9806 (user-error "Marker %s for bulk command is invalid" m)))
9807 org-agenda-bulk-marked-entries)
9809 ;; Prompt for the bulk command
9810 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9811 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9812 "[S]catter [f]unction "
9813 (when org-agenda-bulk-custom-functions
9814 (concat " Custom: ["
9815 (mapconcat (lambda(f) (char-to-string (car f)))
9816 org-agenda-bulk-custom-functions "")
9817 "]"))))
9818 (catch 'exit
9819 (let* ((action (read-char-exclusive))
9820 (org-log-refile (if org-log-refile 'time nil))
9821 (entries (reverse org-agenda-bulk-marked-entries))
9822 (org-overriding-default-time
9823 (if (get-text-property (point) 'org-agenda-date-header)
9824 (org-get-cursor-date)))
9825 redo-at-end
9826 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9827 (cond
9828 ((equal action ?p)
9829 (let ((org-agenda-persistent-marks
9830 (not org-agenda-persistent-marks)))
9831 (org-agenda-bulk-action)
9832 (throw 'exit nil)))
9834 ((equal action ?$)
9835 (setq cmd '(org-agenda-archive)))
9837 ((equal action ?A)
9838 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9840 ((member action '(?r ?w))
9841 (setq rfloc (org-refile-get-location
9842 "Refile to"
9843 (marker-buffer (car entries))
9844 org-refile-allow-creating-parent-nodes))
9845 (if (nth 3 rfloc)
9846 (setcar (nthcdr 3 rfloc)
9847 (move-marker (make-marker) (nth 3 rfloc)
9848 (or (get-file-buffer (nth 1 rfloc))
9849 (find-buffer-visiting (nth 1 rfloc))
9850 (error "This should not happen")))))
9852 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9853 redo-at-end t))
9855 ((equal action ?t)
9856 (setq state (org-icompleting-read
9857 "Todo state: "
9858 (with-current-buffer (marker-buffer (car entries))
9859 (mapcar 'list org-todo-keywords-1))))
9860 (setq cmd `(let ((org-inhibit-blocking t)
9861 (org-inhibit-logging 'note))
9862 (org-agenda-todo ,state))))
9864 ((memq action '(?- ?+))
9865 (setq tag (org-icompleting-read
9866 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9867 (with-current-buffer (marker-buffer (car entries))
9868 (delq nil
9869 (mapcar (lambda (x)
9870 (if (stringp (car x)) x)) org-tag-alist)))))
9871 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9873 ((memq action '(?s ?d))
9874 (let* ((time
9875 (unless arg
9876 (org-read-date
9877 nil nil nil
9878 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9879 org-overriding-default-time)))
9880 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9881 (setq cmd `(eval '(,c1 arg ,time)))))
9883 ((equal action ?S)
9884 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9885 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9886 (let ((days (read-number
9887 (format "Scatter tasks across how many %sdays: "
9888 (if arg "week" "")) 7)))
9889 (setq cmd
9890 `(let ((distance (1+ (random ,days))))
9891 (if arg
9892 (let ((dist distance)
9893 (day-of-week
9894 (calendar-day-of-week
9895 (calendar-gregorian-from-absolute (org-today)))))
9896 (dotimes (i (1+ dist))
9897 (while (member day-of-week org-agenda-weekend-days)
9898 (incf distance)
9899 (incf day-of-week)
9900 (if (= day-of-week 7)
9901 (setq day-of-week 0)))
9902 (incf day-of-week)
9903 (if (= day-of-week 7)
9904 (setq day-of-week 0)))))
9905 ;; silently fail when try to replan a sexp entry
9906 (condition-case nil
9907 (let* ((date (calendar-gregorian-from-absolute
9908 (+ (org-today) distance)))
9909 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9910 (nth 2 date))))
9911 (org-agenda-schedule nil time))
9912 (error nil)))))))
9914 ((assoc action org-agenda-bulk-custom-functions)
9915 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9916 redo-at-end t))
9918 ((equal action ?f)
9919 (setq cmd (list (intern
9920 (org-icompleting-read "Function: "
9921 obarray 'fboundp t nil nil)))))
9923 (t (user-error "Invalid bulk action")))
9925 ;; Sort the markers, to make sure that parents are handled before children
9926 (setq entries (sort entries
9927 (lambda (a b)
9928 (cond
9929 ((equal (marker-buffer a) (marker-buffer b))
9930 (< (marker-position a) (marker-position b)))
9932 (string< (buffer-name (marker-buffer a))
9933 (buffer-name (marker-buffer b))))))))
9935 ;; Now loop over all markers and apply cmd
9936 (while (setq e (pop entries))
9937 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9938 (if (not pos)
9939 (progn (message "Skipping removed entry at %s" e)
9940 (setq cntskip (1+ cntskip)))
9941 (goto-char pos)
9942 (let (org-loop-over-headlines-in-active-region)
9943 (eval cmd))
9944 (setq cnt (1+ cnt))))
9945 (when redo-at-end (org-agenda-redo))
9946 (unless org-agenda-persistent-marks
9947 (org-agenda-bulk-unmark-all))
9948 (message "Acted on %d entries%s%s"
9950 (if (= cntskip 0)
9952 (format ", skipped %d (disappeared before their turn)"
9953 cntskip))
9954 (if (not org-agenda-persistent-marks)
9955 "" " (kept marked)"))))))
9957 (defun org-agenda-capture (&optional with-time)
9958 "Call `org-capture' with the date at point.
9959 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9960 current HH:MM time."
9961 (interactive "P")
9962 (if (not (eq major-mode 'org-agenda-mode))
9963 (user-error "You cannot do this outside of agenda buffers")
9964 (let ((org-overriding-default-time
9965 (org-get-cursor-date (equal with-time 1))))
9966 (call-interactively 'org-capture))))
9968 ;;; Dragging agenda lines forward/backward
9970 (defun org-agenda-reapply-filters ()
9971 "Re-apply all agenda filters."
9972 (mapcar
9973 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9974 `((,org-agenda-tag-filter tag)
9975 (,org-agenda-category-filter category)
9976 (,org-agenda-regexp-filter regexp)
9977 (,org-agenda-effort-filter effort)
9978 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9979 (,(get 'org-agenda-category-filter :preset-filter) category)
9980 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9981 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9983 (defun org-agenda-drag-line-forward (arg &optional backward)
9984 "Drag an agenda line forward by ARG lines.
9985 When the optional argument `backward' is non-nil, move backward."
9986 (interactive "p")
9987 (let ((inhibit-read-only t) lst line)
9988 (if (or (not (get-text-property (point) 'txt))
9989 (save-excursion
9990 (dotimes (n arg)
9991 (move-beginning-of-line (if backward 0 2))
9992 (push (not (get-text-property (point) 'txt)) lst))
9993 (delq nil lst)))
9994 (message "Cannot move line forward")
9995 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9996 (move-beginning-of-line 1)
9997 (setq line (buffer-substring (point) end))
9998 (delete-region (point) end)
9999 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10000 (insert line)
10001 (org-agenda-reapply-filters)
10002 (org-agenda-mark-clocking-task)
10003 (move-beginning-of-line 0)))))
10005 (defun org-agenda-drag-line-backward (arg)
10006 "Drag an agenda line backward by ARG lines."
10007 (interactive "p")
10008 (org-agenda-drag-line-forward arg t))
10010 ;;; Flagging notes
10012 (defun org-agenda-show-the-flagging-note ()
10013 "Display the flagging note in the other window.
10014 When called a second time in direct sequence, offer to remove the FLAGGING
10015 tag and (if present) the flagging note."
10016 (interactive)
10017 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10018 (win (selected-window))
10019 note heading newhead)
10020 (unless hdmarker
10021 (user-error "No linked entry at point"))
10022 (if (and (eq this-command last-command)
10023 (y-or-n-p "Unflag and remove any flagging note? "))
10024 (progn
10025 (org-agenda-remove-flag hdmarker)
10026 (let ((win (get-buffer-window "*Flagging Note*")))
10027 (and win (delete-window win)))
10028 (message "Entry unflagged"))
10029 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10030 (unless note
10031 (user-error "No flagging note"))
10032 (org-kill-new note)
10033 (org-switch-to-buffer-other-window "*Flagging Note*")
10034 (erase-buffer)
10035 (insert note)
10036 (goto-char (point-min))
10037 (while (re-search-forward "\\\\n" nil t)
10038 (replace-match "\n" t t))
10039 (goto-char (point-min))
10040 (select-window win)
10041 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10043 (defun org-agenda-remove-flag (marker)
10044 "Remove the FLAGGED tag and any flagging note in the entry."
10045 (let (newhead)
10046 (org-with-point-at marker
10047 (org-toggle-tag "FLAGGED" 'off)
10048 (org-entry-delete nil "THEFLAGGINGNOTE")
10049 (setq newhead (org-get-heading)))
10050 (org-agenda-change-all-lines newhead marker)
10051 (message "Entry unflagged")))
10053 (defun org-agenda-get-any-marker (&optional pos)
10054 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10055 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10057 ;;; Appointment reminders
10059 (defvar appt-time-msg-list) ; defined in appt.el
10061 ;;;###autoload
10062 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10063 "Activate appointments found in `org-agenda-files'.
10064 With a \\[universal-argument] prefix, refresh the list of
10065 appointments.
10067 If FILTER is t, interactively prompt the user for a regular
10068 expression, and filter out entries that don't match it.
10070 If FILTER is a string, use this string as a regular expression
10071 for filtering entries out.
10073 If FILTER is a function, filter out entries against which
10074 calling the function returns nil. This function takes one
10075 argument: an entry from `org-agenda-get-day-entries'.
10077 FILTER can also be an alist with the car of each cell being
10078 either 'headline or 'category. For example:
10080 '((headline \"IMPORTANT\")
10081 (category \"Work\"))
10083 will only add headlines containing IMPORTANT or headlines
10084 belonging to the \"Work\" category.
10086 ARGS are symbols indicating what kind of entries to consider.
10087 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10088 \(i.e., deadlines and scheduled items with a hh:mm specification)
10089 and :timestamp entries. See the docstring of `org-diary' for
10090 details and examples.
10092 If an entry has a APPT_WARNTIME property, its value will be used
10093 to override `appt-message-warning-time'."
10094 (interactive "P")
10095 (if refresh (setq appt-time-msg-list nil))
10096 (if (eq filter t)
10097 (setq filter (read-from-minibuffer "Regexp filter: ")))
10098 (let* ((cnt 0) ; count added events
10099 (scope (or args '(:deadline* :scheduled* :timestamp)))
10100 (org-agenda-new-buffers nil)
10101 (org-deadline-warning-days 0)
10102 ;; Do not use `org-today' here because appt only takes
10103 ;; time and without date as argument, so it may pass wrong
10104 ;; information otherwise
10105 (today (org-date-to-gregorian
10106 (time-to-days (current-time))))
10107 (org-agenda-restrict nil)
10108 (files (org-agenda-files 'unrestricted)) entries file
10109 (org-agenda-buffer nil))
10110 ;; Get all entries which may contain an appt
10111 (org-agenda-prepare-buffers files)
10112 (while (setq file (pop files))
10113 (setq entries
10114 (delq nil
10115 (append entries
10116 (apply 'org-agenda-get-day-entries
10117 file today scope)))))
10118 ;; Map thru entries and find if we should filter them out
10119 (mapc
10120 (lambda(x)
10121 (let* ((evt (org-trim
10122 (replace-regexp-in-string
10123 org-bracket-link-regexp "\\3"
10124 (or (get-text-property 1 'txt x) ""))))
10125 (cat (get-text-property (1- (length x)) 'org-category x))
10126 (tod (get-text-property 1 'time-of-day x))
10127 (ok (or (null filter)
10128 (and (stringp filter) (string-match filter evt))
10129 (and (functionp filter) (funcall filter x))
10130 (and (listp filter)
10131 (let ((cat-filter (cadr (assoc 'category filter)))
10132 (evt-filter (cadr (assoc 'headline filter))))
10133 (or (and (stringp cat-filter)
10134 (string-match cat-filter cat))
10135 (and (stringp evt-filter)
10136 (string-match evt-filter evt)))))))
10137 (wrn (get-text-property 1 'warntime x)))
10138 ;; FIXME: Shall we remove text-properties for the appt text?
10139 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10140 (when (and ok tod)
10141 (setq tod (concat "00" (number-to-string tod))
10142 tod (when (string-match
10143 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10144 (concat (match-string 1 tod) ":"
10145 (match-string 2 tod))))
10146 (if (version< emacs-version "23.3")
10147 (appt-add tod evt)
10148 (appt-add tod evt wrn))
10149 (setq cnt (1+ cnt))))) entries)
10150 (org-release-buffers org-agenda-new-buffers)
10151 (if (eq cnt 0)
10152 (message "No event to add")
10153 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10155 (defun org-agenda-todayp (date)
10156 "Does DATE mean today, when considering `org-extend-today-until'?"
10157 (let ((today (org-today))
10158 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10159 date)))
10160 (eq date today)))
10162 (defun org-agenda-todo-yesterday (&optional arg)
10163 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10164 (interactive "P")
10165 (let* ((hour (third (decode-time
10166 (org-current-time))))
10167 (org-extend-today-until (1+ hour)))
10168 (org-agenda-todo arg)))
10170 (provide 'org-agenda)
10172 ;;; org-agenda.el ends here