org: forward declare functions
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blobbf8287f77afe248c56807702edb29367e15e23db
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 (const :format "" regexp) (regexp))
401 (list :tag "Regexp does not match" :inline t (const :format "" notregexp) (regexp))
402 (list :tag "TODO state is" :inline t
403 (const todo)
404 (choice
405 (const :tag "any not-done state" todo)
406 (const :tag "any done state" done)
407 (const :tag "any state" any)
408 (list :tag "Keyword list"
409 (const :format "" quote)
410 (repeat (string :tag "Keyword")))))
411 (list :tag "TODO state is not" :inline t
412 (const nottodo)
413 (choice
414 (const :tag "any not-done state" todo)
415 (const :tag "any done state" done)
416 (const :tag "any state" any)
417 (list :tag "Keyword list"
418 (const :format "" quote)
419 (repeat (string :tag "Keyword")))))
420 (const :tag "scheduled" scheduled)
421 (const :tag "not scheduled" notscheduled)
422 (const :tag "deadline" deadline)
423 (const :tag "no deadline" notdeadline)
424 (const :tag "timestamp" timestamp)
425 (const :tag "no timestamp" nottimestamp))))))
426 (list :tag "Non-standard skipping condition"
427 :value (org-agenda-skip-function)
428 (const org-agenda-skip-function)
429 (sexp :tag "Function or form (quoted!)"))
430 (list :tag "Any variable"
431 (variable :tag "Variable")
432 (sexp :tag "Value (sexp)"))))
433 "Selection of examples for agenda command settings.
434 This will be spliced into the custom type of
435 `org-agenda-custom-commands'.")
438 (defcustom org-agenda-custom-commands
439 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
440 "Custom commands for the agenda.
441 These commands will be offered on the splash screen displayed by the
442 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
444 (key desc type match settings files)
446 key The key (one or more characters as a string) to be associated
447 with the command.
448 desc A description of the command, when omitted or nil, a default
449 description is built using MATCH.
450 type The command type, any of the following symbols:
451 agenda The daily/weekly agenda.
452 todo Entries with a specific TODO keyword, in all agenda files.
453 search Entries containing search words entry or headline.
454 tags Tags/Property/TODO match in all agenda files.
455 tags-todo Tags/P/T match in all agenda files, TODO entries only.
456 todo-tree Sparse tree of specific TODO keyword in *current* file.
457 tags-tree Sparse tree with all tags matches in *current* file.
458 occur-tree Occur sparse tree for *current* file.
459 ... A user-defined function.
460 match What to search for:
461 - a single keyword for TODO keyword searches
462 - a tags match expression for tags searches
463 - a word search expression for text searches.
464 - a regular expression for occur searches
465 For all other commands, this should be the empty string.
466 settings A list of option settings, similar to that in a let form, so like
467 this: ((opt1 val1) (opt2 val2) ...). The values will be
468 evaluated at the moment of execution, so quote them when needed.
469 files A list of files file to write the produced agenda buffer to
470 with the command `org-store-agenda-views'.
471 If a file name ends in \".html\", an HTML version of the buffer
472 is written out. If it ends in \".ps\", a postscript version is
473 produced. Otherwise, only the plain text is written to the file.
475 You can also define a set of commands, to create a composite agenda buffer.
476 In this case, an entry looks like this:
478 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
480 where
482 desc A description string to be displayed in the dispatcher menu.
483 cmd An agenda command, similar to the above. However, tree commands
484 are not allowed, but instead you can get agenda and global todo list.
485 So valid commands for a set are:
486 (agenda \"\" settings)
487 (alltodo \"\" settings)
488 (stuck \"\" settings)
489 (todo \"match\" settings files)
490 (search \"match\" settings files)
491 (tags \"match\" settings files)
492 (tags-todo \"match\" settings files)
494 Each command can carry a list of options, and another set of options can be
495 given for the whole set of commands. Individual command options take
496 precedence over the general options.
498 When using several characters as key to a command, the first characters
499 are prefix commands. For the dispatcher to display useful information, you
500 should provide a description for the prefix, like
502 (setq org-agenda-custom-commands
503 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
504 (\"hl\" tags \"+HOME+Lisa\")
505 (\"hp\" tags \"+HOME+Peter\")
506 (\"hk\" tags \"+HOME+Kim\")))"
507 :group 'org-agenda-custom-commands
508 :type `(repeat
509 (choice :value ("x" "Describe command here" tags "" nil)
510 (list :tag "Single command"
511 (string :tag "Access Key(s) ")
512 (option (string :tag "Description"))
513 (choice
514 (const :tag "Agenda" agenda)
515 (const :tag "TODO list" alltodo)
516 (const :tag "Search words" search)
517 (const :tag "Stuck projects" stuck)
518 (const :tag "Tags/Property match (all agenda files)" tags)
519 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
520 (const :tag "TODO keyword search (all agenda files)" todo)
521 (const :tag "Tags sparse tree (current buffer)" tags-tree)
522 (const :tag "TODO keyword tree (current buffer)" todo-tree)
523 (const :tag "Occur tree (current buffer)" occur-tree)
524 (sexp :tag "Other, user-defined function"))
525 (string :tag "Match (only for some commands)")
526 ,org-agenda-custom-commands-local-options
527 (option (repeat :tag "Export" (file :tag "Export to"))))
528 (list :tag "Command series, all agenda files"
529 (string :tag "Access Key(s)")
530 (string :tag "Description ")
531 (repeat :tag "Component"
532 (choice
533 (list :tag "Agenda"
534 (const :format "" agenda)
535 (const :tag "" :format "" "")
536 ,org-agenda-custom-commands-local-options)
537 (list :tag "TODO list (all keywords)"
538 (const :format "" alltodo)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "Search words"
542 (const :format "" search)
543 (string :tag "Match")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Stuck projects"
546 (const :format "" stuck)
547 (const :tag "" :format "" "")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Tags search"
550 (const :format "" tags)
551 (string :tag "Match")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search, TODO entries only"
554 (const :format "" tags-todo)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "TODO keyword search"
558 (const :format "" todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "Other, user-defined function"
562 (symbol :tag "function")
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)))
566 (repeat :tag "Settings for entire command set"
567 (list (variable :tag "Any variable")
568 (sexp :tag "Value")))
569 (option (repeat :tag "Export" (file :tag "Export to"))))
570 (cons :tag "Prefix key documentation"
571 (string :tag "Access Key(s)")
572 (string :tag "Description ")))))
574 (defcustom org-agenda-query-register ?o
575 "The register holding the current query string.
576 The purpose of this is that if you construct a query string interactively,
577 you can then use it to define a custom command."
578 :group 'org-agenda-custom-commands
579 :type 'character)
581 (defcustom org-stuck-projects
582 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
583 "How to identify stuck projects.
584 This is a list of four items:
585 1. A tags/todo/property matcher string that is used to identify a project.
586 See the manual for a description of tag and property searches.
587 The entire tree below a headline matched by this is considered one project.
588 2. A list of TODO keywords identifying non-stuck projects.
589 If the project subtree contains any headline with one of these todo
590 keywords, the project is considered to be not stuck. If you specify
591 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
592 3. A list of tags identifying non-stuck projects.
593 If the project subtree contains any headline with one of these tags,
594 the project is considered to be not stuck. If you specify \"*\" as
595 a tag, any tag will mark the project unstuck. Note that this is about
596 the explicit presence of a tag somewhere in the subtree, inherited
597 tags do not count here. If inherited tags make a project not stuck,
598 use \"-TAG\" in the tags part of the matcher under (1.) above.
599 4. An arbitrary regular expression matching non-stuck projects.
601 If the project turns out to be not stuck, search continues also in the
602 subtree to see if any of the subtasks have project status.
604 See also the variable `org-tags-match-list-sublevels' which applies
605 to projects matched by this search as well.
607 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
608 or `C-c a #' to produce the list."
609 :group 'org-agenda-custom-commands
610 :type '(list
611 (string :tag "Tags/TODO match to identify a project")
612 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
613 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
614 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
616 (defgroup org-agenda-skip nil
617 "Options concerning skipping parts of agenda files."
618 :tag "Org Agenda Skip"
619 :group 'org-agenda)
621 (defcustom org-agenda-skip-function-global nil
622 "Function to be called at each match during agenda construction.
623 If this function returns nil, the current match should not be skipped.
624 If the function decided to skip an agenda match, is must return the
625 buffer position from which the search should be continued.
626 This may also be a Lisp form, which will be evaluated.
628 This variable will be applied to every agenda match, including
629 tags/property searches and TODO lists. So try to make the test function
630 do its checking as efficiently as possible. To implement a skipping
631 condition just for specific agenda commands, use the variable
632 `org-agenda-skip-function' which can be set in the options section
633 of custom agenda commands."
634 :group 'org-agenda-skip
635 :type 'sexp)
637 (defgroup org-agenda-daily/weekly nil
638 "Options concerning the daily/weekly agenda."
639 :tag "Org Agenda Daily/Weekly"
640 :group 'org-agenda)
641 (defgroup org-agenda-todo-list nil
642 "Options concerning the global todo list agenda view."
643 :tag "Org Agenda Todo List"
644 :group 'org-agenda)
645 (defgroup org-agenda-match-view nil
646 "Options concerning the general tags/property/todo match agenda view."
647 :tag "Org Agenda Match View"
648 :group 'org-agenda)
649 (defgroup org-agenda-search-view nil
650 "Options concerning the search agenda view."
651 :tag "Org Agenda Search View"
652 :group 'org-agenda)
654 (defvar org-agenda-archives-mode nil
655 "Non-nil means the agenda will include archived items.
656 If this is the symbol `trees', trees in the selected agenda scope
657 that are marked with the ARCHIVE tag will be included anyway. When this is
658 t, also all archive files associated with the current selection of agenda
659 files will be included.")
661 (defcustom org-agenda-restriction-lock-highlight-subtree t
662 "Non-nil means highlight the whole subtree when restriction is active.
663 Otherwise only highlight the headline. Highlighting the whole subtree is
664 useful to ensure no edits happen beyond the restricted region."
665 :group 'org-agenda
666 :type 'boolean)
668 (defcustom org-agenda-skip-comment-trees t
669 "Non-nil means skip trees that start with the COMMENT keyword.
670 When nil, these trees are also scanned by agenda commands."
671 :group 'org-agenda-skip
672 :type 'boolean)
674 (defcustom org-agenda-todo-list-sublevels t
675 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
676 When nil, the sublevels of a TODO entry are not checked, resulting in
677 potentially much shorter TODO lists."
678 :group 'org-agenda-skip
679 :group 'org-agenda-todo-list
680 :type 'boolean)
682 (defcustom org-agenda-todo-ignore-with-date nil
683 "Non-nil means don't show entries with a date in the global todo list.
684 You can use this if you prefer to mark mere appointments with a TODO keyword,
685 but don't want them to show up in the TODO list.
686 When this is set, it also covers deadlines and scheduled items, the settings
687 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
688 will be ignored.
689 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
690 :group 'org-agenda-skip
691 :group 'org-agenda-todo-list
692 :type 'boolean)
694 (defcustom org-agenda-todo-ignore-timestamp nil
695 "Non-nil means don't show entries with a timestamp.
696 This applies when creating the global todo list.
697 Valid values are:
699 past Don't show entries for today or in the past.
701 future Don't show entries with a timestamp in the future.
702 The idea behind this is that if it has a future
703 timestamp, you don't want to think about it until the
704 date.
706 all Don't show any entries with a timestamp in the global todo list.
707 The idea behind this is that by setting a timestamp, you
708 have already \"taken care\" of this item.
710 This variable can also have an integer as a value. If positive (N),
711 todos with a timestamp N or more days in the future will be ignored. If
712 negative (-N), todos with a timestamp N or more days in the past will be
713 ignored. If 0, todos with a timestamp either today or in the future will
714 be ignored. For example, a value of -1 will exclude todos with a
715 timestamp in the past (yesterday or earlier), while a value of 7 will
716 exclude todos with a timestamp a week or more in the future.
718 See also `org-agenda-todo-ignore-with-date'.
719 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
720 to make his option also apply to the tags-todo list."
721 :group 'org-agenda-skip
722 :group 'org-agenda-todo-list
723 :version "24.1"
724 :type '(choice
725 (const :tag "Ignore future timestamp todos" future)
726 (const :tag "Ignore past or present timestamp todos" past)
727 (const :tag "Ignore all timestamp todos" all)
728 (const :tag "Show timestamp todos" nil)
729 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
731 (defcustom org-agenda-todo-ignore-scheduled nil
732 "Non-nil means, ignore some scheduled TODO items when making TODO list.
733 This applies when creating the global todo list.
734 Valid values are:
736 past Don't show entries scheduled today or in the past.
738 future Don't show entries scheduled in the future.
739 The idea behind this is that by scheduling it, you don't want to
740 think about it until the scheduled date.
742 all Don't show any scheduled entries in the global todo list.
743 The idea behind this is that by scheduling it, you have already
744 \"taken care\" of this item.
746 t Same as `all', for backward compatibility.
748 This variable can also have an integer as a value. See
749 `org-agenda-todo-ignore-timestamp' for more details.
751 See also `org-agenda-todo-ignore-with-date'.
752 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
753 to make his option also apply to the tags-todo list."
754 :group 'org-agenda-skip
755 :group 'org-agenda-todo-list
756 :type '(choice
757 (const :tag "Ignore future-scheduled todos" future)
758 (const :tag "Ignore past- or present-scheduled todos" past)
759 (const :tag "Ignore all scheduled todos" all)
760 (const :tag "Ignore all scheduled todos (compatibility)" t)
761 (const :tag "Show scheduled todos" nil)
762 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
764 (defcustom org-agenda-todo-ignore-deadlines nil
765 "Non-nil means ignore some deadline TODO items when making TODO list.
766 There are different motivations for using different values, please think
767 carefully when configuring this variable.
769 This applies when creating the global todo list.
770 Valid values are:
772 near Don't show near deadline entries. A deadline is near when it is
773 closer than `org-deadline-warning-days' days. The idea behind this
774 is that such items will appear in the agenda anyway.
776 far Don't show TODO entries where a deadline has been defined, but
777 the deadline is not near. This is useful if you don't want to
778 use the todo list to figure out what to do now.
780 past Don't show entries with a deadline timestamp for today or in the past.
782 future Don't show entries with a deadline timestamp in the future, not even
783 when they become `near' ones. Use it with caution.
785 all Ignore all TODO entries that do have a deadline.
787 t Same as `near', for backward compatibility.
789 This variable can also have an integer as a value. See
790 `org-agenda-todo-ignore-timestamp' for more details.
792 See also `org-agenda-todo-ignore-with-date'.
793 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
794 to make his option also apply to the tags-todo list."
795 :group 'org-agenda-skip
796 :group 'org-agenda-todo-list
797 :type '(choice
798 (const :tag "Ignore near deadlines" near)
799 (const :tag "Ignore near deadlines (compatibility)" t)
800 (const :tag "Ignore far deadlines" far)
801 (const :tag "Ignore all TODOs with a deadlines" all)
802 (const :tag "Show all TODOs, even if they have a deadline" nil)
803 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
805 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
806 "Time unit to use when possibly ignoring an agenda item.
808 See the docstring of various `org-agenda-todo-ignore-*' options.
809 The default is to compare time stamps using days. An item is thus
810 considered to be in the future if it is at least one day after today.
811 Non-nil means to compare time stamps using seconds. An item is then
812 considered future if it has a time value later than current time."
813 :group 'org-agenda-skip
814 :group 'org-agenda-todo-list
815 :version "24.4"
816 :package-version '(Org . "8.0")
817 :type '(choice
818 (const :tag "Compare time with days" nil)
819 (const :tag "Compare time with seconds" t)))
821 (defcustom org-agenda-tags-todo-honor-ignore-options nil
822 "Non-nil means honor todo-list ignores options also in tags-todo search.
823 The variables
824 `org-agenda-todo-ignore-with-date',
825 `org-agenda-todo-ignore-timestamp',
826 `org-agenda-todo-ignore-scheduled',
827 `org-agenda-todo-ignore-deadlines'
828 make the global TODO list skip entries that have time stamps of certain
829 kinds. If this option is set, the same options will also apply for the
830 tags-todo search, which is the general tags/property matcher
831 restricted to unfinished TODO entries only."
832 :group 'org-agenda-skip
833 :group 'org-agenda-todo-list
834 :group 'org-agenda-match-view
835 :type 'boolean)
837 (defcustom org-agenda-skip-scheduled-if-done nil
838 "Non-nil means don't show scheduled items in agenda when they are done.
839 This is relevant for the daily/weekly agenda, not for the TODO list. And
840 it applies only to the actual date of the scheduling. Warnings about
841 an item with a past scheduling dates are always turned off when the item
842 is DONE."
843 :group 'org-agenda-skip
844 :group 'org-agenda-daily/weekly
845 :type 'boolean)
847 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
848 "Non-nil means skip scheduling line if same entry shows because of deadline.
850 In the agenda of today, an entry can show up multiple times
851 because it is both scheduled and has a nearby deadline, and maybe
852 a plain time stamp as well.
854 When this variable is nil, the entry will be shown several times.
856 When set to t, then only the deadline is shown and the fact that
857 the entry is scheduled today or was scheduled previously is not
858 shown.
860 When set to the symbol `not-today', skip scheduled previously,
861 but not scheduled today.
863 When set to the symbol `repeated-after-deadline', skip scheduled
864 items if they are repeated beyond the current deadline."
865 :group 'org-agenda-skip
866 :group 'org-agenda-daily/weekly
867 :type '(choice
868 (const :tag "Never" nil)
869 (const :tag "Always" t)
870 (const :tag "Not when scheduled today" not-today)
871 (const :tag "When repeated past deadline" repeated-after-deadline)))
873 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
874 "Non-nil means skip timestamp line if same entry shows because of deadline.
875 In the agenda of today, an entry can show up multiple times
876 because it has both a plain timestamp and has a nearby deadline.
877 When this variable is t, then only the deadline is shown and the
878 fact that the entry has a timestamp for or including today is not
879 shown. When this variable is nil, the entry will be shown
880 several times."
881 :group 'org-agenda-skip
882 :group 'org-agenda-daily/weekly
883 :version "24.1"
884 :type '(choice
885 (const :tag "Never" nil)
886 (const :tag "Always" t)))
888 (defcustom org-agenda-skip-deadline-if-done nil
889 "Non-nil means don't show deadlines when the corresponding item is done.
890 When nil, the deadline is still shown and should give you a happy feeling.
891 This is relevant for the daily/weekly agenda. And it applied only to the
892 actually date of the deadline. Warnings about approaching and past-due
893 deadlines are always turned off when the item is DONE."
894 :group 'org-agenda-skip
895 :group 'org-agenda-daily/weekly
896 :type 'boolean)
898 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
899 "Non-nil means skip deadline prewarning when entry is also scheduled.
900 This will apply on all days where a prewarning for the deadline would
901 be shown, but not at the day when the entry is actually due. On that day,
902 the deadline will be shown anyway.
903 This variable may be set to nil, t, the symbol `pre-scheduled',
904 or a number which will then give the number of days before the actual
905 deadline when the prewarnings should resume. The symbol `pre-scheduled'
906 eliminates the deadline prewarning only prior to the scheduled date.
907 This can be used in a workflow where the first showing of the deadline will
908 trigger you to schedule it, and then you don't want to be reminded of it
909 because you will take care of it on the day when scheduled."
910 :group 'org-agenda-skip
911 :group 'org-agenda-daily/weekly
912 :version "24.1"
913 :type '(choice
914 (const :tag "Always show prewarning" nil)
915 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
916 (const :tag "Remove prewarning if entry is scheduled" t)
917 (integer :tag "Restart prewarning N days before deadline")))
919 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
920 "Non-nil means skip scheduled delay when entry also has a deadline.
921 This variable may be set to nil, t, the symbol `post-deadline',
922 or a number which will then give the number of days after the actual
923 scheduled date when the delay should expire. The symbol `post-deadline'
924 eliminates the schedule delay when the date is posterior to the deadline."
925 :group 'org-agenda-skip
926 :group 'org-agenda-daily/weekly
927 :version "24.4"
928 :package-version '(Org . "8.0")
929 :type '(choice
930 (const :tag "Always honor delay" nil)
931 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
932 (const :tag "Ignore delay if entry has a deadline" t)
933 (integer :tag "Honor delay up until N days after the scheduled date")))
935 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
936 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
937 When non-nil, after the search for timestamps has matched once in an
938 entry, the rest of the entry will not be searched."
939 :group 'org-agenda-skip
940 :type 'boolean)
942 (defcustom org-agenda-skip-timestamp-if-done nil
943 "Non-nil means don't select item by timestamp or -range if it is DONE."
944 :group 'org-agenda-skip
945 :group 'org-agenda-daily/weekly
946 :type 'boolean)
948 (defcustom org-agenda-dim-blocked-tasks t
949 "Non-nil means dim blocked tasks in the agenda display.
950 This causes some overhead during agenda construction, but if you
951 have turned on `org-enforce-todo-dependencies',
952 `org-enforce-todo-checkbox-dependencies', or any other blocking
953 mechanism, this will create useful feedback in the agenda.
955 Instead of t, this variable can also have the value `invisible'.
956 Then blocked tasks will be invisible and only become visible when
957 they become unblocked. An exemption to this behavior is when a task is
958 blocked because of unchecked checkboxes below it. Since checkboxes do
959 not show up in the agenda views, making this task invisible you remove any
960 trace from agenda views that there is something to do. Therefore, a task
961 that is blocked because of checkboxes will never be made invisible, it
962 will only be dimmed."
963 :group 'org-agenda-daily/weekly
964 :group 'org-agenda-todo-list
965 :version "24.3"
966 :type '(choice
967 (const :tag "Do not dim" nil)
968 (const :tag "Dim to a gray face" t)
969 (const :tag "Make invisible" invisible)))
971 (defcustom org-timeline-show-empty-dates 3
972 "Non-nil means `org-timeline' also shows dates without an entry.
973 When nil, only the days which actually have entries are shown.
974 When t, all days between the first and the last date are shown.
975 When an integer, show also empty dates, but if there is a gap of more than
976 N days, just insert a special line indicating the size of the gap."
977 :group 'org-agenda-skip
978 :type '(choice
979 (const :tag "None" nil)
980 (const :tag "All" t)
981 (integer :tag "at most")))
983 (defgroup org-agenda-startup nil
984 "Options concerning initial settings in the Agenda in Org Mode."
985 :tag "Org Agenda Startup"
986 :group 'org-agenda)
988 (defcustom org-agenda-menu-show-matcher t
989 "Non-nil means show the match string in the agenda dispatcher menu.
990 When nil, the matcher string is not shown, but is put into the help-echo
991 property so than moving the mouse over the command shows it.
992 Setting it to nil is good if matcher strings are very long and/or if
993 you want to use two-columns display (see `org-agenda-menu-two-columns')."
994 :group 'org-agenda
995 :version "24.1"
996 :type 'boolean)
998 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1000 (defcustom org-agenda-menu-two-columns nil
1001 "Non-nil means, use two columns to show custom commands in the dispatcher.
1002 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1003 to nil."
1004 :group 'org-agenda
1005 :version "24.1"
1006 :type 'boolean)
1008 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1009 (defcustom org-agenda-finalize-hook nil
1010 "Hook run just before displaying an agenda buffer.
1011 The buffer is still writable when the hook is called.
1013 You can modify some of the buffer substrings but you should be
1014 extra careful not to modify the text properties of the agenda
1015 headlines as the agenda display heavily relies on them."
1016 :group 'org-agenda-startup
1017 :type 'hook)
1019 (defcustom org-agenda-mouse-1-follows-link nil
1020 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1021 A longer mouse click will still set point. Does not work on XEmacs.
1022 Needs to be set before org.el is loaded."
1023 :group 'org-agenda-startup
1024 :type 'boolean)
1026 (defcustom org-agenda-start-with-follow-mode nil
1027 "The initial value of follow mode in a newly created agenda window."
1028 :group 'org-agenda-startup
1029 :type 'boolean)
1031 (defcustom org-agenda-follow-indirect nil
1032 "Non-nil means `org-agenda-follow-mode' displays only the
1033 current item's tree, in an indirect buffer."
1034 :group 'org-agenda
1035 :version "24.1"
1036 :type 'boolean)
1038 (defcustom org-agenda-show-outline-path t
1039 "Non-nil means show outline path in echo area after line motion."
1040 :group 'org-agenda-startup
1041 :type 'boolean)
1043 (defcustom org-agenda-start-with-entry-text-mode nil
1044 "The initial value of entry-text-mode in a newly created agenda window."
1045 :group 'org-agenda-startup
1046 :type 'boolean)
1048 (defcustom org-agenda-entry-text-maxlines 5
1049 "Number of text lines to be added when `E' is pressed in the agenda.
1051 Note that this variable only used during agenda display. Add add entry text
1052 when exporting the agenda, configure the variable
1053 `org-agenda-add-entry-ext-maxlines'."
1054 :group 'org-agenda
1055 :type 'integer)
1057 (defcustom org-agenda-entry-text-exclude-regexps nil
1058 "List of regular expressions to clean up entry text.
1059 The complete matches of all regular expressions in this list will be
1060 removed from entry text before it is shown in the agenda."
1061 :group 'org-agenda
1062 :type '(repeat (regexp)))
1064 (defcustom org-agenda-entry-text-leaders " > "
1065 "Text prepended to the entry text in agenda buffers."
1066 :version "24.4"
1067 :package-version '(Org . "8.0")
1068 :group 'org-agenda
1069 :type 'string)
1071 (defvar org-agenda-entry-text-cleanup-hook nil
1072 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1073 This cleanup is done in a temporary buffer, so the function may inspect and
1074 change the entire buffer.
1075 Some default stuff like drawers and scheduling/deadline dates will already
1076 have been removed when this is called, as will any matches for regular
1077 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1079 (defvar org-agenda-include-inactive-timestamps nil
1080 "Non-nil means include inactive time stamps in agenda and timeline.
1081 Dynamically scoped.")
1083 (defgroup org-agenda-windows nil
1084 "Options concerning the windows used by the Agenda in Org Mode."
1085 :tag "Org Agenda Windows"
1086 :group 'org-agenda)
1088 (defcustom org-agenda-window-setup 'reorganize-frame
1089 "How the agenda buffer should be displayed.
1090 Possible values for this option are:
1092 current-window Show agenda in the current window, keeping all other windows.
1093 other-window Use `switch-to-buffer-other-window' to display agenda.
1094 reorganize-frame Show only two windows on the current frame, the current
1095 window and the agenda.
1096 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1097 Also, when exiting the agenda, kill that frame.
1098 See also the variable `org-agenda-restore-windows-after-quit'."
1099 :group 'org-agenda-windows
1100 :type '(choice
1101 (const current-window)
1102 (const other-frame)
1103 (const other-window)
1104 (const reorganize-frame)))
1106 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1107 "The min and max height of the agenda window as a fraction of frame height.
1108 The value of the variable is a cons cell with two numbers between 0 and 1.
1109 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1110 :group 'org-agenda-windows
1111 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1113 (defcustom org-agenda-restore-windows-after-quit nil
1114 "Non-nil means restore window configuration upon exiting agenda.
1115 Before the window configuration is changed for displaying the agenda,
1116 the current status is recorded. When the agenda is exited with
1117 `q' or `x' and this option is set, the old state is restored. If
1118 `org-agenda-window-setup' is `other-frame', the value of this
1119 option will be ignored."
1120 :group 'org-agenda-windows
1121 :type 'boolean)
1123 (defcustom org-agenda-ndays nil
1124 "Number of days to include in overview display.
1125 Should be 1 or 7.
1126 Obsolete, see `org-agenda-span'."
1127 :group 'org-agenda-daily/weekly
1128 :type '(choice (const nil)
1129 (integer)))
1131 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1133 (defcustom org-agenda-span 'week
1134 "Number of days to include in overview display.
1135 Can be day, week, month, year, or any number of days.
1136 Custom commands can set this variable in the options section."
1137 :group 'org-agenda-daily/weekly
1138 :type '(choice (const :tag "Day" day)
1139 (const :tag "Week" week)
1140 (const :tag "Fortnight" fortnight)
1141 (const :tag "Month" month)
1142 (const :tag "Year" year)
1143 (integer :tag "Custom")))
1145 (defcustom org-agenda-start-on-weekday 1
1146 "Non-nil means start the overview always on the specified weekday.
1147 0 denotes Sunday, 1 denotes Monday, etc.
1148 When nil, always start on the current day.
1149 Custom commands can set this variable in the options section."
1150 :group 'org-agenda-daily/weekly
1151 :type '(choice (const :tag "Today" nil)
1152 (integer :tag "Weekday No.")))
1154 (defcustom org-agenda-show-all-dates t
1155 "Non-nil means `org-agenda' shows every day in the selected range.
1156 When nil, only the days which actually have entries are shown."
1157 :group 'org-agenda-daily/weekly
1158 :type 'boolean)
1160 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1161 "Format string for displaying dates in the agenda.
1162 Used by the daily/weekly agenda and by the timeline. This should be
1163 a format string understood by `format-time-string', or a function returning
1164 the formatted date as a string. The function must take a single argument,
1165 a calendar-style date list like (month day year)."
1166 :group 'org-agenda-daily/weekly
1167 :type '(choice
1168 (string :tag "Format string")
1169 (function :tag "Function")))
1171 (defun org-agenda-format-date-aligned (date)
1172 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1173 This function makes sure that dates are aligned for easy reading."
1174 (require 'cal-iso)
1175 (let* ((dayname (calendar-day-name date))
1176 (day (cadr date))
1177 (day-of-week (calendar-day-of-week date))
1178 (month (car date))
1179 (monthname (calendar-month-name month))
1180 (year (nth 2 date))
1181 (iso-week (org-days-to-iso-week
1182 (calendar-absolute-from-gregorian date)))
1183 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1184 (1- year))
1185 ((and (= month 12) (<= iso-week 1))
1186 (1+ year))
1187 (t year)))
1188 (weekstring (if (= day-of-week 1)
1189 (format " W%02d" iso-week)
1190 "")))
1191 (format "%-10s %2d %s %4d%s"
1192 dayname day monthname year weekstring)))
1194 (defcustom org-agenda-time-leading-zero nil
1195 "Non-nil means use leading zero for military times in agenda.
1196 For example, 9:30am would become 09:30 rather than 9:30."
1197 :group 'org-agenda-daily/weekly
1198 :version "24.1"
1199 :type 'boolean)
1201 (defcustom org-agenda-timegrid-use-ampm nil
1202 "When set, show AM/PM style timestamps on the timegrid."
1203 :group 'org-agenda
1204 :version "24.1"
1205 :type 'boolean)
1207 (defun org-agenda-time-of-day-to-ampm (time)
1208 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1209 (let* ((hour-number (string-to-number (substring time 0 -3)))
1210 (minute (substring time -2))
1211 (ampm "am"))
1212 (cond
1213 ((equal hour-number 12)
1214 (setq ampm "pm"))
1215 ((> hour-number 12)
1216 (setq ampm "pm")
1217 (setq hour-number (- hour-number 12))))
1218 (concat
1219 (if org-agenda-time-leading-zero
1220 (format "%02d" hour-number)
1221 (format "%02s" (number-to-string hour-number)))
1222 ":" minute ampm)))
1224 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1225 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1226 (if org-agenda-timegrid-use-ampm
1227 (org-agenda-time-of-day-to-ampm time)
1228 time))
1230 (defcustom org-agenda-weekend-days '(6 0)
1231 "Which days are weekend?
1232 These days get the special face `org-agenda-date-weekend' in the agenda
1233 and timeline buffers."
1234 :group 'org-agenda-daily/weekly
1235 :type '(set :greedy t
1236 (const :tag "Monday" 1)
1237 (const :tag "Tuesday" 2)
1238 (const :tag "Wednesday" 3)
1239 (const :tag "Thursday" 4)
1240 (const :tag "Friday" 5)
1241 (const :tag "Saturday" 6)
1242 (const :tag "Sunday" 0)))
1244 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1245 "Non-nil means jump to today when moving a past date forward in time.
1246 When using S-right in the agenda to move a a date forward, and the date
1247 stamp currently points to the past, the first key press will move it
1248 to today. WHen nil, just move one day forward even if the date stays
1249 in the past."
1250 :group 'org-agenda-daily/weekly
1251 :version "24.1"
1252 :type 'boolean)
1254 (defcustom org-agenda-include-diary nil
1255 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1256 Custom commands can set this variable in the options section."
1257 :group 'org-agenda-daily/weekly
1258 :type 'boolean)
1260 (defcustom org-agenda-include-deadlines t
1261 "If non-nil, include entries within their deadline warning period.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :version "24.1"
1265 :type 'boolean)
1267 (defcustom org-agenda-repeating-timestamp-show-all t
1268 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1269 When set to a list of strings, only show occurrences of repeating
1270 stamps for these TODO keywords. When nil, only one occurrence is
1271 shown, either today or the nearest into the future."
1272 :group 'org-agenda-daily/weekly
1273 :type '(choice
1274 (const :tag "Show repeating stamps" t)
1275 (repeat :tag "Show repeating stamps for these TODO keywords"
1276 (string :tag "TODO Keyword"))
1277 (const :tag "Don't show repeating stamps" nil)))
1279 (defcustom org-scheduled-past-days 10000
1280 "Number of days to continue listing scheduled items not marked DONE.
1281 When an item is scheduled on a date, it shows up in the agenda on this
1282 day and will be listed until it is marked done for the number of days
1283 given here."
1284 :group 'org-agenda-daily/weekly
1285 :type 'integer)
1287 (defcustom org-agenda-log-mode-items '(closed clock)
1288 "List of items that should be shown in agenda log mode.
1289 This list may contain the following symbols:
1291 closed Show entries that have been closed on that day.
1292 clock Show entries that have received clocked time on that day.
1293 state Show all logged state changes.
1294 Note that instead of changing this variable, you can also press `C-u l' in
1295 the agenda to display all available LOG items temporarily."
1296 :group 'org-agenda-daily/weekly
1297 :type '(set :greedy t (const closed) (const clock) (const state)))
1299 (defcustom org-agenda-clock-consistency-checks
1300 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1301 :gap-ok-around ("4:00")
1302 :default-face ((:background "DarkRed") (:foreground "white"))
1303 :overlap-face nil :gap-face nil :no-end-time-face nil
1304 :long-face nil :short-face nil)
1305 "This is a property list, with the following keys:
1307 :max-duration Mark clocking chunks that are longer than this time.
1308 This is a time string like \"HH:MM\", or the number
1309 of minutes as an integer.
1311 :min-duration Mark clocking chunks that are shorter that this.
1312 This is a time string like \"HH:MM\", or the number
1313 of minutes as an integer.
1315 :max-gap Mark gaps between clocking chunks that are longer than
1316 this duration. A number of minutes, or a string
1317 like \"HH:MM\".
1319 :gap-ok-around List of times during the day which are usually not working
1320 times. When a gap is detected, but the gap contains any
1321 of these times, the gap is *not* reported. For example,
1322 if this is (\"4:00\" \"13:00\") then gaps that contain
1323 4:00 in the morning (i.e. the night) and 13:00
1324 (i.e. a typical lunch time) do not cause a warning.
1325 You should have at least one time during the night in this
1326 list, or otherwise the first task each morning will trigger
1327 a warning because it follows a long gap.
1329 Furthermore, the following properties can be used to define faces for
1330 issue display.
1332 :default-face the default face, if the specific face is undefined
1333 :overlap-face face for overlapping clocks
1334 :gap-face face for gaps between clocks
1335 :no-end-time-face face for incomplete clocks
1336 :long-face face for clock intervals that are too long
1337 :short-face face for clock intervals that are too short"
1338 :group 'org-agenda-daily/weekly
1339 :group 'org-clock
1340 :version "24.1"
1341 :type 'plist)
1343 (defcustom org-agenda-log-mode-add-notes t
1344 "Non-nil means add first line of notes to log entries in agenda views.
1345 If a log item like a state change or a clock entry is associated with
1346 notes, the first line of these notes will be added to the entry in the
1347 agenda display."
1348 :group 'org-agenda-daily/weekly
1349 :type 'boolean)
1351 (defcustom org-agenda-start-with-log-mode nil
1352 "The initial value of log-mode in a newly created agenda window.
1353 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1354 explanations on the possible values."
1355 :group 'org-agenda-startup
1356 :group 'org-agenda-daily/weekly
1357 :type '(choice (const :tag "Don't show log items" nil)
1358 (const :tag "Show only log items" only)
1359 (const :tag "Show all possible log items" clockcheck)
1360 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1361 (choice (const :tag "Show closed log items" closed)
1362 (const :tag "Show clocked log items" clock)
1363 (const :tag "Show all logged state changes" state)))))
1365 (defcustom org-agenda-start-with-clockreport-mode nil
1366 "The initial value of clockreport-mode in a newly created agenda window."
1367 :group 'org-agenda-startup
1368 :group 'org-agenda-daily/weekly
1369 :type 'boolean)
1371 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1372 "Property list with parameters for the clocktable in clockreport mode.
1373 This is the display mode that shows a clock table in the daily/weekly
1374 agenda, the properties for this dynamic block can be set here.
1375 The usual clocktable parameters are allowed here, but you cannot set
1376 the properties :name, :tstart, :tend, :block, and :scope - these will
1377 be overwritten to make sure the content accurately reflects the
1378 current display in the agenda."
1379 :group 'org-agenda-daily/weekly
1380 :type 'plist)
1382 (defcustom org-agenda-search-view-always-boolean nil
1383 "Non-nil means the search string is interpreted as individual parts.
1385 The search string for search view can either be interpreted as a phrase,
1386 or as a list of snippets that define a boolean search for a number of
1387 strings.
1389 When this is non-nil, the string will be split on whitespace, and each
1390 snippet will be searched individually, and all must match in order to
1391 select an entry. A snippet is then a single string of non-white
1392 characters, or a string in double quotes, or a regexp in {} braces.
1393 If a snippet is preceded by \"-\", the snippet must *not* match.
1394 \"+\" is syntactic sugar for positive selection. Each snippet may
1395 be found as a full word or a partial word, but see the variable
1396 `org-agenda-search-view-force-full-words'.
1398 When this is nil, search will look for the entire search phrase as one,
1399 with each space character matching any amount of whitespace, including
1400 line breaks.
1402 Even when this is nil, you can still switch to Boolean search dynamically
1403 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1404 is a regexp marked with braces like \"{abc}\", this will also switch to
1405 boolean search."
1406 :group 'org-agenda-search-view
1407 :version "24.1"
1408 :type 'boolean)
1410 (org-defvaralias 'org-agenda-search-view-search-words-only
1411 'org-agenda-search-view-always-boolean)
1413 (defcustom org-agenda-search-view-force-full-words nil
1414 "Non-nil means, search words must be matches as complete words.
1415 When nil, they may also match part of a word."
1416 :group 'org-agenda-search-view
1417 :version "24.1"
1418 :type 'boolean)
1420 (defcustom org-agenda-search-view-max-outline-level 0
1421 "Maximum outline level to display in search view.
1422 E.g. when this is set to 1, the search view will only
1423 show headlines of level 1. When set to 0, the default
1424 value, don't limit agenda view by outline level."
1425 :group 'org-agenda-search-view
1426 :version "24.4"
1427 :package-version '(Org . "8.3")
1428 :type 'integer)
1430 (defgroup org-agenda-time-grid nil
1431 "Options concerning the time grid in the Org-mode Agenda."
1432 :tag "Org Agenda Time Grid"
1433 :group 'org-agenda)
1435 (defcustom org-agenda-search-headline-for-time t
1436 "Non-nil means search headline for a time-of-day.
1437 If the headline contains a time-of-day in one format or another, it will
1438 be used to sort the entry into the time sequence of items for a day.
1439 Some people have time stamps in the headline that refer to the creation
1440 time or so, and then this produces an unwanted side effect. If this is
1441 the case for your, use this variable to turn off searching the headline
1442 for a time."
1443 :group 'org-agenda-time-grid
1444 :type 'boolean)
1446 (defcustom org-agenda-use-time-grid t
1447 "Non-nil means show a time grid in the agenda schedule.
1448 A time grid is a set of lines for specific times (like every two hours between
1449 8:00 and 20:00). The items scheduled for a day at specific times are
1450 sorted in between these lines.
1451 For details about when the grid will be shown, and what it will look like, see
1452 the variable `org-agenda-time-grid'."
1453 :group 'org-agenda-time-grid
1454 :type 'boolean)
1456 (defcustom org-agenda-time-grid
1457 '((daily today require-timed)
1458 "----------------"
1459 (800 1000 1200 1400 1600 1800 2000))
1461 "The settings for time grid for agenda display.
1462 This is a list of three items. The first item is again a list. It contains
1463 symbols specifying conditions when the grid should be displayed:
1465 daily if the agenda shows a single day
1466 weekly if the agenda shows an entire week
1467 today show grid on current date, independent of daily/weekly display
1468 require-timed show grid only if at least one item has a time specification
1470 The second item is a string which will be placed behind the grid time.
1472 The third item is a list of integers, indicating the times that should have
1473 a grid line."
1474 :group 'org-agenda-time-grid
1475 :type
1476 '(list
1477 (set :greedy t :tag "Grid Display Options"
1478 (const :tag "Show grid in single day agenda display" daily)
1479 (const :tag "Show grid in weekly agenda display" weekly)
1480 (const :tag "Always show grid for today" today)
1481 (const :tag "Show grid only if any timed entries are present"
1482 require-timed)
1483 (const :tag "Skip grid times already present in an entry"
1484 remove-match))
1485 (string :tag "Grid String")
1486 (repeat :tag "Grid Times" (integer :tag "Time"))))
1488 (defcustom org-agenda-show-current-time-in-grid t
1489 "Non-nil means show the current time in the time grid."
1490 :group 'org-agenda-time-grid
1491 :version "24.1"
1492 :type 'boolean)
1494 (defcustom org-agenda-current-time-string
1495 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1496 "The string for the current time marker in the agenda."
1497 :group 'org-agenda-time-grid
1498 :version "24.1"
1499 :type 'string)
1501 (defgroup org-agenda-sorting nil
1502 "Options concerning sorting in the Org-mode Agenda."
1503 :tag "Org Agenda Sorting"
1504 :group 'org-agenda)
1506 (defcustom org-agenda-sorting-strategy
1507 '((agenda habit-down time-up priority-down category-keep)
1508 (todo priority-down category-keep)
1509 (tags priority-down category-keep)
1510 (search category-keep))
1511 "Sorting structure for the agenda items of a single day.
1512 This is a list of symbols which will be used in sequence to determine
1513 if an entry should be listed before another entry. The following
1514 symbols are recognized:
1516 time-up Put entries with time-of-day indications first, early first
1517 time-down Put entries with time-of-day indications first, late first
1518 timestamp-up Sort by any timestamp, early first
1519 timestamp-down Sort by any timestamp, late first
1520 scheduled-up Sort by scheduled timestamp, early first
1521 scheduled-down Sort by scheduled timestamp, late first
1522 deadline-up Sort by deadline timestamp, early first
1523 deadline-down Sort by deadline timestamp, late first
1524 ts-up Sort by active timestamp, early first
1525 ts-down Sort by active timestamp, late first
1526 tsia-up Sort by inactive timestamp, early first
1527 tsia-down Sort by inactive timestamp, late first
1528 category-keep Keep the default order of categories, corresponding to the
1529 sequence in `org-agenda-files'.
1530 category-up Sort alphabetically by category, A-Z.
1531 category-down Sort alphabetically by category, Z-A.
1532 tag-up Sort alphabetically by last tag, A-Z.
1533 tag-down Sort alphabetically by last tag, Z-A.
1534 priority-up Sort numerically by priority, high priority last.
1535 priority-down Sort numerically by priority, high priority first.
1536 todo-state-up Sort by todo state, tasks that are done last.
1537 todo-state-down Sort by todo state, tasks that are done first.
1538 effort-up Sort numerically by estimated effort, high effort last.
1539 effort-down Sort numerically by estimated effort, high effort first.
1540 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1541 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1542 habit-up Put entries that are habits first
1543 habit-down Put entries that are habits last
1544 alpha-up Sort headlines alphabetically
1545 alpha-down Sort headlines alphabetically, reversed
1547 The different possibilities will be tried in sequence, and testing stops
1548 if one comparison returns a \"not-equal\". For example, the default
1549 '(time-up category-keep priority-down)
1550 means: Pull out all entries having a specified time of day and sort them,
1551 in order to make a time schedule for the current day the first thing in the
1552 agenda listing for the day. Of the entries without a time indication, keep
1553 the grouped in categories, don't sort the categories, but keep them in
1554 the sequence given in `org-agenda-files'. Within each category sort by
1555 priority.
1557 Leaving out `category-keep' would mean that items will be sorted across
1558 categories by priority.
1560 Instead of a single list, this can also be a set of list for specific
1561 contents, with a context symbol in the car of the list, any of
1562 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1564 Custom commands can bind this variable in the options section."
1565 :group 'org-agenda-sorting
1566 :type `(choice
1567 (repeat :tag "General" ,org-sorting-choice)
1568 (list :tag "Individually"
1569 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for TODO lists" todo)
1572 (repeat ,org-sorting-choice))
1573 (cons (const :tag "Strategy for Tags matches" tags)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for search matches" search)
1576 (repeat ,org-sorting-choice)))))
1578 (defcustom org-agenda-cmp-user-defined nil
1579 "A function to define the comparison `user-defined'.
1580 This function must receive two arguments, agenda entry a and b.
1581 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1582 the user comparison, return nil.
1583 When this is defined, you can make `user-defined-up' and `user-defined-down'
1584 part of an agenda sorting strategy."
1585 :group 'org-agenda-sorting
1586 :type 'symbol)
1588 (defcustom org-sort-agenda-notime-is-late t
1589 "Non-nil means items without time are considered late.
1590 This is only relevant for sorting. When t, items which have no explicit
1591 time like 15:30 will be considered as 99:01, i.e. later than any items which
1592 do have a time. When nil, the default time is before 0:00. You can use this
1593 option to decide if the schedule for today should come before or after timeless
1594 agenda entries."
1595 :group 'org-agenda-sorting
1596 :type 'boolean)
1598 (defcustom org-sort-agenda-noeffort-is-high t
1599 "Non-nil means items without effort estimate are sorted as high effort.
1600 This also applies when filtering an agenda view with respect to the
1601 < or > effort operator. Then, tasks with no effort defined will be treated
1602 as tasks with high effort.
1603 When nil, such items are sorted as 0 minutes effort."
1604 :group 'org-agenda-sorting
1605 :type 'boolean)
1607 (defgroup org-agenda-line-format nil
1608 "Options concerning the entry prefix in the Org-mode agenda display."
1609 :tag "Org Agenda Line Format"
1610 :group 'org-agenda)
1612 (defcustom org-agenda-prefix-format
1613 '((agenda . " %i %-12:c%?-12t% s")
1614 (timeline . " % s")
1615 (todo . " %i %-12:c")
1616 (tags . " %i %-12:c")
1617 (search . " %i %-12:c"))
1618 "Format specifications for the prefix of items in the agenda views.
1619 An alist with five entries, each for the different agenda types. The
1620 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1621 The values are format strings.
1623 This format works similar to a printf format, with the following meaning:
1625 %c the category of the item, \"Diary\" for entries from the diary,
1626 or as given by the CATEGORY keyword or derived from the file name
1627 %e the effort required by the item
1628 %l the level of the item (insert X space(s) if item is of level X)
1629 %i the icon category of the item, see `org-agenda-category-icon-alist'
1630 %T the last tag of the item (ignore inherited tags, which come first)
1631 %t the HH:MM time-of-day specification if one applies to the entry
1632 %s Scheduling/Deadline information, a short string
1633 %b show breadcrumbs, i.e., the names of the higher levels
1634 %(expression) Eval EXPRESSION and replace the control string
1635 by the result
1637 All specifiers work basically like the standard `%s' of printf, but may
1638 contain two additional characters: a question mark just after the `%'
1639 and a whitespace/punctuation character just before the final letter.
1641 If the first character after `%' is a question mark, the entire field
1642 will only be included if the corresponding value applies to the current
1643 entry. This is useful for fields which should have fixed width when
1644 present, but zero width when absent. For example, \"%?-12t\" will
1645 result in a 12 character time field if a time of the day is specified,
1646 but will completely disappear in entries which do not contain a time.
1648 If there is punctuation or whitespace character just before the
1649 final format letter, this character will be appended to the field
1650 value if the value is not empty. For example, the format
1651 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1652 the category is empty, no additional colon is inserted.
1654 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1655 which means:
1657 - Indent the line with two space characters
1658 - Give the category a 12 chars wide field, padded with whitespace on
1659 the right (because of `-'). Append a colon if there is a category
1660 (because of `:').
1661 - If there is a time-of-day, put it into a 12 chars wide field. If no
1662 time, don't put in an empty field, just skip it (because of '?').
1663 - Finally, put the scheduling information.
1665 See also the variables `org-agenda-remove-times-when-in-prefix' and
1666 `org-agenda-remove-tags'.
1668 Custom commands can set this variable in the options section."
1669 :type '(choice
1670 (string :tag "General format")
1671 (list :greedy t :tag "View dependent"
1672 (cons (const agenda) (string :tag "Format"))
1673 (cons (const timeline) (string :tag "Format"))
1674 (cons (const todo) (string :tag "Format"))
1675 (cons (const tags) (string :tag "Format"))
1676 (cons (const search) (string :tag "Format"))))
1677 :group 'org-agenda-line-format)
1679 (defvar org-prefix-format-compiled nil
1680 "The compiled prefix format and associated variables.
1681 This is a list where first element is a list of variable bindings, and second
1682 element is the compiled format expression. See the variable
1683 `org-agenda-prefix-format'.")
1685 (defcustom org-agenda-todo-keyword-format "%-1s"
1686 "Format for the TODO keyword in agenda lines.
1687 Set this to something like \"%-12s\" if you want all TODO keywords
1688 to occupy a fixed space in the agenda display."
1689 :group 'org-agenda-line-format
1690 :type 'string)
1692 (defcustom org-agenda-diary-sexp-prefix nil
1693 "A regexp that matches part of a diary sexp entry
1694 which should be treated as scheduling/deadline information in
1695 `org-agenda'.
1697 For example, you can use this to extract the `diary-remind-message' from
1698 `diary-remind' entries."
1699 :group 'org-agenda-line-format
1700 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1702 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1703 "Text preceding timerange entries in the agenda view.
1704 This is a list with two strings. The first applies when the range
1705 is entirely on one day. The second applies if the range spans several days.
1706 The strings may have two \"%d\" format specifiers which will be filled
1707 with the sequence number of the days, and the total number of days in the
1708 range, respectively."
1709 :group 'org-agenda-line-format
1710 :type '(list
1711 (string :tag "Deadline today ")
1712 (choice :tag "Deadline relative"
1713 (string :tag "Format string")
1714 (function))))
1716 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1717 "Text preceding scheduled items in the agenda view.
1718 This is a list with two strings. The first applies when the item is
1719 scheduled on the current day. The second applies when it has been scheduled
1720 previously, it may contain a %d indicating that this is the nth time that
1721 this item is scheduled, due to automatic rescheduling of unfinished items
1722 for the following day. So this number is one larger than the number of days
1723 that passed since this item was scheduled first."
1724 :group 'org-agenda-line-format
1725 :version "24.4"
1726 :package-version '(Org . "8.0")
1727 :type '(list
1728 (string :tag "Scheduled today ")
1729 (string :tag "Scheduled previously")))
1731 (defcustom org-agenda-inactive-leader "["
1732 "Text preceding item pulled into the agenda by inactive time stamps.
1733 These entries are added to the agenda when pressing \"[\"."
1734 :group 'org-agenda-line-format
1735 :version "24.1"
1736 :type 'string)
1738 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1739 "Text preceding deadline items in the agenda view.
1740 This is a list with three strings. The first applies when the item has its
1741 deadline on the current day. The second applies when the deadline is in the
1742 future, the third one when it is in the past. The strings may contain %d
1743 to capture the number of days."
1744 :group 'org-agenda-line-format
1745 :version "24.4"
1746 :package-version '(Org . "8.0")
1747 :type '(list
1748 (string :tag "Deadline today ")
1749 (string :tag "Deadline in the future ")
1750 (string :tag "Deadline in the past ")))
1752 (defcustom org-agenda-remove-times-when-in-prefix t
1753 "Non-nil means remove duplicate time specifications in agenda items.
1754 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1755 time-of-day specification in a headline or diary entry is extracted and
1756 placed into the prefix. If this option is non-nil, the original specification
1757 \(a timestamp or -range, or just a plain time(range) specification like
1758 11:30-4pm) will be removed for agenda display. This makes the agenda less
1759 cluttered.
1760 The option can be t or nil. It may also be the symbol `beg', indicating
1761 that the time should only be removed when it is located at the beginning of
1762 the headline/diary entry."
1763 :group 'org-agenda-line-format
1764 :type '(choice
1765 (const :tag "Always" t)
1766 (const :tag "Never" nil)
1767 (const :tag "When at beginning of entry" beg)))
1769 (defcustom org-agenda-remove-timeranges-from-blocks nil
1770 "Non-nil means remove time ranges specifications in agenda
1771 items that span on several days."
1772 :group 'org-agenda-line-format
1773 :version "24.1"
1774 :type 'boolean)
1776 (defcustom org-agenda-default-appointment-duration nil
1777 "Default duration for appointments that only have a starting time.
1778 When nil, no duration is specified in such cases.
1779 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1780 :group 'org-agenda-line-format
1781 :type '(choice
1782 (integer :tag "Minutes")
1783 (const :tag "No default duration")))
1785 (defcustom org-agenda-show-inherited-tags t
1786 "Non-nil means show inherited tags in each agenda line.
1788 When this option is set to 'always, it take precedences over
1789 `org-agenda-use-tag-inheritance' and inherited tags are shown
1790 in every agenda.
1792 When this option is set to t (the default), inherited tags are
1793 shown when they are available, i.e. when the value of
1794 `org-agenda-use-tag-inheritance' has been taken into account.
1796 This can be set to a list of agenda types in which the agenda
1797 must display the inherited tags. Available types are 'todo,
1798 'agenda, 'search and 'timeline.
1800 When set to nil, never show inherited tags in agenda lines."
1801 :group 'org-agenda-line-format
1802 :group 'org-agenda
1803 :version "24.3"
1804 :type '(choice
1805 (const :tag "Show inherited tags when available" t)
1806 (const :tag "Always show inherited tags" always)
1807 (repeat :tag "Show inherited tags only in selected agenda types"
1808 (symbol :tag "Agenda type"))))
1810 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1811 "List of agenda view types where to use tag inheritance.
1813 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1814 controlled by `org-use-tag-inheritance'. In other agenda types,
1815 `org-use-tag-inheritance' is not used for the selection of the
1816 agenda entries. Still, you may want the agenda to be aware of
1817 the inherited tags anyway, e.g. for later tag filtering.
1819 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1821 This variable has no effect if `org-agenda-show-inherited-tags'
1822 is set to 'always. In that case, the agenda is aware of those
1823 tags.
1825 The default value sets tags in every agenda type. Setting this
1826 option to nil will speed up non-tags agenda view a lot."
1827 :group 'org-agenda
1828 :version "24.3"
1829 :type '(choice
1830 (const :tag "Use tag inheritance in all agenda types" t)
1831 (repeat :tag "Use tag inheritance in selected agenda types"
1832 (symbol :tag "Agenda type"))))
1834 (defcustom org-agenda-hide-tags-regexp nil
1835 "Regular expression used to filter away specific tags in agenda views.
1836 This means that these tags will be present, but not be shown in the agenda
1837 line. Secondary filtering will still work on the hidden tags.
1838 Nil means don't hide any tags."
1839 :group 'org-agenda-line-format
1840 :type '(choice
1841 (const :tag "Hide none" nil)
1842 (string :tag "Regexp ")))
1844 (defcustom org-agenda-remove-tags nil
1845 "Non-nil means remove the tags from the headline copy in the agenda.
1846 When this is the symbol `prefix', only remove tags when
1847 `org-agenda-prefix-format' contains a `%T' specifier."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Always" t)
1851 (const :tag "Never" nil)
1852 (const :tag "When prefix format contains %T" prefix)))
1854 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1855 'org-agenda-remove-tags)
1857 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1858 "Shift tags in agenda items to this column.
1859 If this number is positive, it specifies the column. If it is negative,
1860 it means that the tags should be flushright to that column. For example,
1861 -80 works well for a normal 80 character screen."
1862 :group 'org-agenda-line-format
1863 :type 'integer)
1865 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1867 (defcustom org-agenda-fontify-priorities 'cookies
1868 "Non-nil means highlight low and high priorities in agenda.
1869 When t, the highest priority entries are bold, lowest priority italic.
1870 However, settings in `org-priority-faces' will overrule these faces.
1871 When this variable is the symbol `cookies', only fontify the
1872 cookies, not the entire task.
1873 This may also be an association list of priority faces, whose
1874 keys are the character values of `org-highest-priority',
1875 `org-default-priority', and `org-lowest-priority' (the default values
1876 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1877 color as a string, or a list like `(:background \"Red\")'.
1878 If it is a color, the variable `org-faces-easy-properties'
1879 determines if it is a foreground or a background color."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Never" nil)
1883 (const :tag "Defaults" t)
1884 (const :tag "Cookies only" cookies)
1885 (repeat :tag "Specify"
1886 (list (character :tag "Priority" :value ?A)
1887 (choice :tag "Face "
1888 (string :tag "Color")
1889 (sexp :tag "Face"))))))
1891 (defcustom org-agenda-day-face-function nil
1892 "Function called to determine what face should be used to display a day.
1893 The only argument passed to that function is the day. It should
1894 returns a face, or nil if does not want to specify a face and let
1895 the normal rules apply."
1896 :group 'org-agenda-line-format
1897 :version "24.1"
1898 :type '(choice (const nil) (function)))
1900 (defcustom org-agenda-category-icon-alist nil
1901 "Alist of category icon to be displayed in agenda views.
1903 Each entry should have the following format:
1905 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1907 Where CATEGORY-REGEXP is a regexp matching the categories where
1908 the icon should be displayed.
1909 FILE-OR-DATA either a file path or a string containing image data.
1911 The other fields can be omitted safely if not needed:
1912 TYPE indicates the image type.
1913 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1914 image data.
1915 PROPS are additional image attributes to assign to the image,
1916 like, e.g. `:ascent center'.
1918 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1920 If you want to set the display properties yourself, just put a
1921 list as second element:
1923 (CATEGORY-REGEXP (MY PROPERTY LIST))
1925 For example, to display a 16px horizontal space for Emacs
1926 category, you can use:
1928 (\"Emacs\" '(space . (:width (16))))"
1929 :group 'org-agenda-line-format
1930 :version "24.1"
1931 :type '(alist :key-type (string :tag "Regexp matching category")
1932 :value-type (choice (list :tag "Icon"
1933 (string :tag "File or data")
1934 (symbol :tag "Type")
1935 (boolean :tag "Data?")
1936 (repeat :tag "Extra image properties" :inline t symbol))
1937 (list :tag "Display properties" sexp))))
1939 (defgroup org-agenda-column-view nil
1940 "Options concerning column view in the agenda."
1941 :tag "Org Agenda Column View"
1942 :group 'org-agenda)
1944 (defcustom org-agenda-columns-show-summaries t
1945 "Non-nil means show summaries for columns displayed in the agenda view."
1946 :group 'org-agenda-column-view
1947 :type 'boolean)
1949 (defcustom org-agenda-columns-compute-summary-properties t
1950 "Non-nil means recompute all summary properties before column view.
1951 When column view in the agenda is listing properties that have a summary
1952 operator, it can go to all relevant buffers and recompute the summaries
1953 there. This can mean overhead for the agenda column view, but is necessary
1954 to have thing up to date.
1955 As a special case, a CLOCKSUM property also makes sure that the clock
1956 computations are current."
1957 :group 'org-agenda-column-view
1958 :type 'boolean)
1960 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1961 "Non-nil means the duration of an appointment will add to day effort.
1962 The property to which appointment durations will be added is the one given
1963 in the option `org-effort-property'. If an appointment does not have
1964 an end time, `org-agenda-default-appointment-duration' will be used. If that
1965 is not set, an appointment without end time will not contribute to the time
1966 estimate."
1967 :group 'org-agenda-column-view
1968 :type 'boolean)
1970 (defcustom org-agenda-auto-exclude-function nil
1971 "A function called with a tag to decide if it is filtered on '/ RET'.
1972 The sole argument to the function, which is called once for each
1973 possible tag, is a string giving the name of the tag. The
1974 function should return either nil if the tag should be included
1975 as normal, or \"-<TAG>\" to exclude the tag.
1976 Note that for the purpose of tag filtering, only the lower-case version of
1977 all tags will be considered, so that this function will only ever see
1978 the lower-case version of all tags."
1979 :group 'org-agenda
1980 :type '(choice (const nil) (function)))
1982 (defcustom org-agenda-bulk-custom-functions nil
1983 "Alist of characters and custom functions for bulk actions.
1984 For example, this value makes those two functions available:
1986 '((?R set-category)
1987 (?C bulk-cut))
1989 With selected entries in an agenda buffer, `B R' will call
1990 the custom function `set-category' on the selected entries.
1991 Note that functions in this alist don't need to be quoted."
1992 :type 'alist
1993 :version "24.1"
1994 :group 'org-agenda)
1996 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1997 "Execute BODY with point at location given by `org-hd-marker' property.
1998 If STRING is non-nil, the text property will be fetched from position 0
1999 in that string. If STRING is nil, it will be fetched from the beginning
2000 of the current line."
2001 (org-with-gensyms (marker)
2002 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2003 'org-hd-marker ,string)))
2004 (with-current-buffer (marker-buffer ,marker)
2005 (save-excursion
2006 (goto-char ,marker)
2007 ,@body)))))
2008 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2010 (defun org-add-agenda-custom-command (entry)
2011 "Replace or add a command in `org-agenda-custom-commands'.
2012 This is mostly for hacking and trying a new command - once the command
2013 works you probably want to add it to `org-agenda-custom-commands' for good."
2014 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2015 (if ass
2016 (setcdr ass (cdr entry))
2017 (push entry org-agenda-custom-commands))))
2019 ;;; Define the org-agenda-mode
2021 (defvar org-agenda-mode-map (make-sparse-keymap)
2022 "Keymap for `org-agenda-mode'.")
2023 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2025 (defvar org-agenda-menu) ; defined later in this file.
2026 (defvar org-agenda-restrict nil) ; defined later in this file.
2027 (defvar org-agenda-follow-mode nil)
2028 (defvar org-agenda-entry-text-mode nil)
2029 (defvar org-agenda-clockreport-mode nil)
2030 (defvar org-agenda-show-log nil)
2031 (defvar org-agenda-redo-command nil)
2032 (defvar org-agenda-query-string nil)
2033 (defvar org-agenda-mode-hook nil
2034 "Hook run after `org-agenda-mode' is turned on.
2035 The buffer is still writable when this hook is called.")
2036 (defvar org-agenda-type nil)
2037 (defvar org-agenda-force-single-file nil)
2038 (defvar org-agenda-bulk-marked-entries nil
2039 "List of markers that refer to marked entries in the agenda.")
2041 ;;; Multiple agenda buffers support
2043 (defcustom org-agenda-sticky nil
2044 "Non-nil means agenda q key will bury agenda buffers.
2045 Agenda commands will then show existing buffer instead of generating new ones.
2046 When nil, `q' will kill the single agenda buffer."
2047 :group 'org-agenda
2048 :version "24.3"
2049 :type 'boolean)
2052 ;;;###autoload
2053 (defun org-toggle-sticky-agenda (&optional arg)
2054 "Toggle `org-agenda-sticky'."
2055 (interactive "P")
2056 (let ((new-value (if arg
2057 (> (prefix-numeric-value arg) 0)
2058 (not org-agenda-sticky))))
2059 (if (equal new-value org-agenda-sticky)
2060 (and (org-called-interactively-p 'interactive)
2061 (message "Sticky agenda was already %s"
2062 (if org-agenda-sticky "enabled" "disabled")))
2063 (setq org-agenda-sticky new-value)
2064 (org-agenda-kill-all-agenda-buffers)
2065 (and (org-called-interactively-p 'interactive)
2066 (message "Sticky agenda was %s"
2067 (if org-agenda-sticky "enabled" "disabled"))))))
2069 (defvar org-agenda-buffer nil
2070 "Agenda buffer currently being generated.")
2072 (defvar org-agenda-last-prefix-arg nil)
2073 (defvar org-agenda-this-buffer-name nil)
2074 (defvar org-agenda-doing-sticky-redo nil)
2075 (defvar org-agenda-this-buffer-is-sticky nil)
2077 (defconst org-agenda-local-vars
2078 '(org-agenda-this-buffer-name
2079 org-agenda-undo-list
2080 org-agenda-pending-undo-list
2081 org-agenda-follow-mode
2082 org-agenda-entry-text-mode
2083 org-agenda-clockreport-mode
2084 org-agenda-show-log
2085 org-agenda-redo-command
2086 org-agenda-query-string
2087 org-agenda-type
2088 org-agenda-bulk-marked-entries
2089 org-agenda-undo-has-started-in
2090 org-agenda-info
2091 org-agenda-pre-window-conf
2092 org-agenda-columns-active
2093 org-agenda-tag-filter
2094 org-agenda-category-filter
2095 org-agenda-top-headline-filter
2096 org-agenda-regexp-filter
2097 org-agenda-effort-filter
2098 org-agenda-markers
2099 org-agenda-last-search-view-search-was-boolean
2100 org-agenda-filtered-by-category
2101 org-agenda-filter-form
2102 org-agenda-cycle-counter
2103 org-agenda-last-prefix-arg)
2104 "Variables that must be local in agenda buffers to allow multiple buffers.")
2106 (defun org-agenda-mode ()
2107 "Mode for time-sorted view on action items in Org-mode files.
2109 The following commands are available:
2111 \\{org-agenda-mode-map}"
2112 (interactive)
2113 (cond (org-agenda-doing-sticky-redo
2114 ;; Refreshing sticky agenda-buffer
2116 ;; Preserve the value of `org-agenda-local-vars' variables,
2117 ;; while letting `kill-all-local-variables' kill the rest
2118 (let ((save (buffer-local-variables)))
2119 (kill-all-local-variables)
2120 (mapc 'make-local-variable org-agenda-local-vars)
2121 (dolist (elem save)
2122 (let ((var (car elem))
2123 (val (cdr elem)))
2124 (when (and val
2125 (member var org-agenda-local-vars))
2126 (set var val)))))
2127 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2128 (org-agenda-sticky
2129 ;; Creating a sticky Agenda buffer for the first time
2130 (kill-all-local-variables)
2131 (mapc 'make-local-variable org-agenda-local-vars)
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 ;; Creating a non-sticky agenda buffer
2135 (kill-all-local-variables)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2137 (setq org-agenda-undo-list nil
2138 org-agenda-pending-undo-list nil
2139 org-agenda-bulk-marked-entries nil)
2140 (setq major-mode 'org-agenda-mode)
2141 ;; Keep global-font-lock-mode from turning on font-lock-mode
2142 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2143 (setq mode-name "Org-Agenda")
2144 (setq indent-tabs-mode nil)
2145 (use-local-map org-agenda-mode-map)
2146 (easy-menu-add org-agenda-menu)
2147 (if org-startup-truncated (setq truncate-lines t))
2148 (org-set-local 'line-move-visual nil)
2149 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2150 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2151 ;; Make sure properties are removed when copying text
2152 (org-add-hook 'filter-buffer-substring-functions
2153 (lambda (fun start end delete)
2154 (substring-no-properties (funcall fun start end delete)))
2155 nil t)
2156 (unless org-agenda-keep-modes
2157 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2158 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2159 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2160 org-agenda-show-log org-agenda-start-with-log-mode))
2161 (add-to-invisibility-spec '(org-filtered))
2162 (add-to-invisibility-spec '(org-link))
2163 (easy-menu-change
2164 '("Agenda") "Agenda Files"
2165 (append
2166 (list
2167 (vector
2168 (if (get 'org-agenda-files 'org-restrict)
2169 "Restricted to single file"
2170 "Edit File List")
2171 '(org-edit-agenda-file-list)
2172 (not (get 'org-agenda-files 'org-restrict)))
2173 "--")
2174 (mapcar 'org-file-menu-entry (org-agenda-files))))
2175 (org-agenda-set-mode-name)
2176 (apply
2177 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2178 (list 'org-agenda-mode-hook)))
2180 (substitute-key-definition 'undo 'org-agenda-undo
2181 org-agenda-mode-map global-map)
2182 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2183 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2184 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2185 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2186 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2187 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2188 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2189 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2190 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2191 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2192 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2193 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2194 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2195 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2196 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2197 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2198 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2199 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2200 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2201 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2202 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2203 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2204 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2205 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2206 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2207 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2208 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2209 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2210 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2211 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2212 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2213 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2214 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2215 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2216 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2217 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2218 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2219 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2220 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2221 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2222 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2223 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2224 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2225 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2226 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2227 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2228 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2229 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2230 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2231 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2233 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2234 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2235 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2236 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2237 (while l (org-defkey org-agenda-mode-map
2238 (int-to-string (pop l)) 'digit-argument)))
2240 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2241 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2242 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2243 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2244 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2245 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2246 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2247 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2248 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2249 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2250 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2251 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2252 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2253 'org-clock-modify-effort-estimate)
2254 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2255 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2256 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2257 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2258 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2259 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2260 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2261 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2262 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2263 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2264 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2265 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2266 (substitute-key-definition 'next-line 'org-agenda-next-line
2267 org-agenda-mode-map global-map)
2268 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2269 org-agenda-mode-map global-map)
2270 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2271 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2272 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2273 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2274 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2275 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2276 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2277 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2278 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2279 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2280 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2281 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2282 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2283 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2284 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2285 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2287 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2289 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2290 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2291 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2292 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2293 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2294 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2297 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2298 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2301 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2302 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2303 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2304 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2305 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2306 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2307 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2308 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2309 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2310 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2311 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2312 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2313 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2314 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2315 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2316 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2318 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2319 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2321 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2322 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2324 (when org-agenda-mouse-1-follows-link
2325 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2326 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2327 '("Agenda"
2328 ("Agenda Files")
2329 "--"
2330 ("Agenda Dates"
2331 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2332 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2333 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2334 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2335 "--"
2336 ("View"
2337 ["Day View" org-agenda-day-view
2338 :active (org-agenda-check-type nil 'agenda)
2339 :style radio :selected (eq org-agenda-current-span 'day)
2340 :keys "v d (or just d)"]
2341 ["Week View" org-agenda-week-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'week)
2344 :keys "v w"]
2345 ["Fortnight View" org-agenda-fortnight-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'fortnight)
2348 :keys "v f"]
2349 ["Month View" org-agenda-month-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'month)
2352 :keys "v m"]
2353 ["Year View" org-agenda-year-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'year)
2356 :keys "v y"]
2357 "--"
2358 ["Include Diary" org-agenda-toggle-diary
2359 :style toggle :selected org-agenda-include-diary
2360 :active (org-agenda-check-type nil 'agenda)]
2361 ["Include Deadlines" org-agenda-toggle-deadlines
2362 :style toggle :selected org-agenda-include-deadlines
2363 :active (org-agenda-check-type nil 'agenda)]
2364 ["Use Time Grid" org-agenda-toggle-time-grid
2365 :style toggle :selected org-agenda-use-time-grid
2366 :active (org-agenda-check-type nil 'agenda)]
2367 "--"
2368 ["Show clock report" org-agenda-clockreport-mode
2369 :style toggle :selected org-agenda-clockreport-mode
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Show some entry text" org-agenda-entry-text-mode
2372 :style toggle :selected org-agenda-entry-text-mode
2373 :active t]
2374 "--"
2375 ["Show Logbook entries" org-agenda-log-mode
2376 :style toggle :selected org-agenda-show-log
2377 :active (org-agenda-check-type nil 'agenda 'timeline)
2378 :keys "v l (or just l)"]
2379 ["Include archived trees" org-agenda-archives-mode
2380 :style toggle :selected org-agenda-archives-mode :active t
2381 :keys "v a"]
2382 ["Include archive files" (org-agenda-archives-mode t)
2383 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2384 :keys "v A"]
2385 "--"
2386 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2387 ["Write view to file" org-agenda-write t]
2388 ["Rebuild buffer" org-agenda-redo t]
2389 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2390 "--"
2391 ["Show original entry" org-agenda-show t]
2392 ["Go To (other window)" org-agenda-goto t]
2393 ["Go To (this window)" org-agenda-switch-to t]
2394 ["Capture with cursor date" org-agenda-capture t]
2395 ["Follow Mode" org-agenda-follow-mode
2396 :style toggle :selected org-agenda-follow-mode :active t]
2397 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2398 "--"
2399 ("TODO"
2400 ["Cycle TODO" org-agenda-todo t]
2401 ["Next TODO set" org-agenda-todo-nextset t]
2402 ["Previous TODO set" org-agenda-todo-previousset t]
2403 ["Add note" org-agenda-add-note t])
2404 ("Archive/Refile/Delete"
2405 ["Archive default" org-agenda-archive-default t]
2406 ["Archive default" org-agenda-archive-default-with-confirmation t]
2407 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2408 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2409 ["Archive subtree" org-agenda-archive t]
2410 "--"
2411 ["Refile" org-agenda-refile t]
2412 "--"
2413 ["Delete subtree" org-agenda-kill t])
2414 ("Bulk action"
2415 ["Mark entry" org-agenda-bulk-mark t]
2416 ["Mark all" org-agenda-bulk-mark-all t]
2417 ["Unmark entry" org-agenda-bulk-unmark t]
2418 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2419 ["Toggle mark" org-agenda-bulk-toggle t]
2420 ["Toggle all" org-agenda-bulk-toggle-all t]
2421 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2422 ["Act on all marked" org-agenda-bulk-action t]
2423 "--"
2424 ("Tags and Properties"
2425 ["Show all Tags" org-agenda-show-tags t]
2426 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2427 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2428 "--"
2429 ["Column View" org-columns t])
2430 ("Deadline/Schedule"
2431 ["Schedule" org-agenda-schedule t]
2432 ["Set Deadline" org-agenda-deadline t]
2433 "--"
2434 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2435 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2437 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2438 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2439 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2440 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2441 ("Clock and Effort"
2442 ["Clock in" org-agenda-clock-in t]
2443 ["Clock out" org-agenda-clock-out t]
2444 ["Clock cancel" org-agenda-clock-cancel t]
2445 ["Goto running clock" org-clock-goto t]
2446 "--"
2447 ["Set Effort" org-agenda-set-effort t]
2448 ["Change clocked effort" org-clock-modify-effort-estimate
2449 (org-clock-is-active)])
2450 ("Priority"
2451 ["Set Priority" org-agenda-priority t]
2452 ["Increase Priority" org-agenda-priority-up t]
2453 ["Decrease Priority" org-agenda-priority-down t]
2454 ["Show Priority" org-show-priority t])
2455 ("Calendar/Diary"
2456 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2457 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2462 "--"
2463 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2464 "--"
2465 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2466 "--"
2467 ("MobileOrg"
2468 ["Push Files and Views" org-mobile-push t]
2469 ["Get Captured and Flagged" org-mobile-pull t]
2470 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2471 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2472 "--"
2473 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2474 "--"
2475 ["Quit" org-agenda-quit t]
2476 ["Exit and Release Buffers" org-agenda-exit t]
2479 ;;; Agenda undo
2481 (defvar org-agenda-allow-remote-undo t
2482 "Non-nil means allow remote undo from the agenda buffer.")
2483 (defvar org-agenda-undo-has-started-in nil
2484 "Buffers that have already seen `undo-start' in the current undo sequence.")
2486 (defun org-agenda-undo ()
2487 "Undo a remote editing step in the agenda.
2488 This undoes changes both in the agenda buffer and in the remote buffer
2489 that have been changed along."
2490 (interactive)
2491 (or org-agenda-allow-remote-undo
2492 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2493 (if (not (eq this-command last-command))
2494 (setq org-agenda-undo-has-started-in nil
2495 org-agenda-pending-undo-list org-agenda-undo-list))
2496 (if (not org-agenda-pending-undo-list)
2497 (user-error "No further undo information"))
2498 (let* ((entry (pop org-agenda-pending-undo-list))
2499 buf line cmd rembuf)
2500 (setq cmd (pop entry) line (pop entry))
2501 (setq rembuf (nth 2 entry))
2502 (org-with-remote-undo rembuf
2503 (while (bufferp (setq buf (pop entry)))
2504 (if (pop entry)
2505 (with-current-buffer buf
2506 (let ((last-undo-buffer buf)
2507 (inhibit-read-only t))
2508 (unless (memq buf org-agenda-undo-has-started-in)
2509 (push buf org-agenda-undo-has-started-in)
2510 (make-local-variable 'pending-undo-list)
2511 (undo-start))
2512 (while (and pending-undo-list
2513 (listp pending-undo-list)
2514 (not (car pending-undo-list)))
2515 (pop pending-undo-list))
2516 (undo-more 1))))))
2517 (org-goto-line line)
2518 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2520 (defun org-verify-change-for-undo (l1 l2)
2521 "Verify that a real change occurred between the undo lists L1 and L2."
2522 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2523 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2524 (not (eq l1 l2)))
2526 ;;; Agenda dispatch
2528 (defvar org-agenda-restrict-begin (make-marker))
2529 (defvar org-agenda-restrict-end (make-marker))
2530 (defvar org-agenda-last-dispatch-buffer nil)
2531 (defvar org-agenda-overriding-restriction nil)
2533 (defcustom org-agenda-custom-commands-contexts nil
2534 "Alist of custom agenda keys and contextual rules.
2536 For example, if you have a custom agenda command \"p\" and you
2537 want this command to be accessible only from plain text files,
2538 use this:
2540 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2542 Here are the available contexts definitions:
2544 in-file: command displayed only in matching files
2545 in-mode: command displayed only in matching modes
2546 not-in-file: command not displayed in matching files
2547 not-in-mode: command not displayed in matching modes
2548 in-buffer: command displayed only in matching buffers
2549 not-in-buffer: command not displayed in matching buffers
2550 [function]: a custom function taking no argument
2552 If you define several checks, the agenda command will be
2553 accessible if there is at least one valid check.
2555 You can also bind a key to another agenda custom command
2556 depending on contextual rules.
2558 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2560 Here it means: in .txt files, use \"p\" as the key for the
2561 agenda command otherwise associated with \"q\". (The command
2562 originally associated with \"q\" is not displayed to avoid
2563 duplicates.)"
2564 :version "24.3"
2565 :group 'org-agenda-custom-commands
2566 :type '(repeat (list :tag "Rule"
2567 (string :tag " Agenda key")
2568 (string :tag "Replace by command")
2569 (repeat :tag "Available when"
2570 (choice
2571 (cons :tag "Condition"
2572 (choice
2573 (const :tag "In file" in-file)
2574 (const :tag "Not in file" not-in-file)
2575 (const :tag "In buffer" in-buffer)
2576 (const :tag "Not in buffer" not-in-buffer)
2577 (const :tag "In mode" in-mode)
2578 (const :tag "Not in mode" not-in-mode))
2579 (regexp))
2580 (function :tag "Custom function"))))))
2582 (defcustom org-agenda-max-entries nil
2583 "Maximum number of entries to display in an agenda.
2584 This can be nil (no limit) or an integer or an alist of agenda
2585 types with an associated number of entries to display in this
2586 type."
2587 :version "24.4"
2588 :package-version '(Org . "8.0")
2589 :group 'org-agenda-custom-commands
2590 :type '(choice (symbol :tag "No limit" nil)
2591 (integer :tag "Max number of entries")
2592 (repeat
2593 (cons (choice :tag "Agenda type"
2594 (const agenda)
2595 (const todo)
2596 (const tags)
2597 (const search)
2598 (const timeline))
2599 (integer :tag "Max number of entries")))))
2601 (defcustom org-agenda-max-todos nil
2602 "Maximum number of TODOs to display in an agenda.
2603 This can be nil (no limit) or an integer or an alist of agenda
2604 types with an associated number of entries to display in this
2605 type."
2606 :version "24.4"
2607 :package-version '(Org . "8.0")
2608 :group 'org-agenda-custom-commands
2609 :type '(choice (symbol :tag "No limit" nil)
2610 (integer :tag "Max number of TODOs")
2611 (repeat
2612 (cons (choice :tag "Agenda type"
2613 (const agenda)
2614 (const todo)
2615 (const tags)
2616 (const search)
2617 (const timeline))
2618 (integer :tag "Max number of TODOs")))))
2620 (defcustom org-agenda-max-tags nil
2621 "Maximum number of tagged entries to display in an agenda.
2622 This can be nil (no limit) or an integer or an alist of agenda
2623 types with an associated number of entries to display in this
2624 type."
2625 :version "24.4"
2626 :package-version '(Org . "8.0")
2627 :group 'org-agenda-custom-commands
2628 :type '(choice (symbol :tag "No limit" nil)
2629 (integer :tag "Max number of tagged entries")
2630 (repeat
2631 (cons (choice :tag "Agenda type"
2632 (const agenda)
2633 (const todo)
2634 (const tags)
2635 (const search)
2636 (const timeline))
2637 (integer :tag "Max number of tagged entries")))))
2639 (defcustom org-agenda-max-effort nil
2640 "Maximum cumulated effort duration for the agenda.
2641 This can be nil (no limit) or a number of minutes (as an integer)
2642 or an alist of agenda types with an associated number of minutes
2643 to limit entries to in this type."
2644 :version "24.4"
2645 :package-version '(Org . "8.0")
2646 :group 'org-agenda-custom-commands
2647 :type '(choice (symbol :tag "No limit" nil)
2648 (integer :tag "Max number of minutes")
2649 (repeat
2650 (cons (choice :tag "Agenda type"
2651 (const agenda)
2652 (const todo)
2653 (const tags)
2654 (const search)
2655 (const timeline))
2656 (integer :tag "Max number of minutes")))))
2658 (defvar org-keys nil)
2659 (defvar org-match nil)
2660 ;;;###autoload
2661 (defun org-agenda (&optional arg org-keys restriction)
2662 "Dispatch agenda commands to collect entries to the agenda buffer.
2663 Prompts for a command to execute. Any prefix arg will be passed
2664 on to the selected command. The default selections are:
2666 a Call `org-agenda-list' to display the agenda for current day or week.
2667 t Call `org-todo-list' to display the global todo list.
2668 T Call `org-todo-list' to display the global todo list, select only
2669 entries with a specific TODO keyword (the user gets a prompt).
2670 m Call `org-tags-view' to display headlines with tags matching
2671 a condition (the user is prompted for the condition).
2672 M Like `m', but select only TODO entries, no ordinary headlines.
2673 L Create a timeline for the current buffer.
2674 e Export views to associated files.
2675 s Search entries for keywords.
2676 S Search entries for keywords, only with TODO keywords.
2677 / Multi occur across all agenda files and also files listed
2678 in `org-agenda-text-search-extra-files'.
2679 < Restrict agenda commands to buffer, subtree, or region.
2680 Press several times to get the desired effect.
2681 > Remove a previous restriction.
2682 # List \"stuck\" projects.
2683 ! Configure what \"stuck\" means.
2684 C Configure custom agenda commands.
2686 More commands can be added by configuring the variable
2687 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2688 searches can be pre-defined in this way.
2690 If the current buffer is in Org-mode and visiting a file, you can also
2691 first press `<' once to indicate that the agenda should be temporarily
2692 \(until the next use of \\[org-agenda]) restricted to the current file.
2693 Pressing `<' twice means to restrict to the current subtree or region
2694 \(if active)."
2695 (interactive "P")
2696 (catch 'exit
2697 (let* ((prefix-descriptions nil)
2698 (org-agenda-buffer-name org-agenda-buffer-name)
2699 (org-agenda-window-setup (if (equal (buffer-name)
2700 org-agenda-buffer-name)
2701 'current-window
2702 org-agenda-window-setup))
2703 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2704 (org-agenda-custom-commands
2705 ;; normalize different versions
2706 (delq nil
2707 (mapcar
2708 (lambda (x)
2709 (cond ((stringp (cdr x))
2710 (push x prefix-descriptions)
2711 nil)
2712 ((stringp (nth 1 x)) x)
2713 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2714 (t (cons (car x) (cons "" (cdr x))))))
2715 org-agenda-custom-commands)))
2716 (org-agenda-custom-commands
2717 (org-contextualize-keys
2718 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2719 (buf (current-buffer))
2720 (bfn (buffer-file-name (buffer-base-buffer)))
2721 entry key type org-match lprops ans)
2722 ;; Turn off restriction unless there is an overriding one,
2723 (unless org-agenda-overriding-restriction
2724 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2725 ;; There is a request to keep the file list in place
2726 (put 'org-agenda-files 'org-restrict nil))
2727 (setq org-agenda-restrict nil)
2728 (move-marker org-agenda-restrict-begin nil)
2729 (move-marker org-agenda-restrict-end nil))
2730 ;; Delete old local properties
2731 (put 'org-agenda-redo-command 'org-lprops nil)
2732 ;; Delete previously set last-arguments
2733 (put 'org-agenda-redo-command 'last-args nil)
2734 ;; Remember where this call originated
2735 (setq org-agenda-last-dispatch-buffer (current-buffer))
2736 (unless org-keys
2737 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2738 org-keys (car ans)
2739 restriction (cdr ans)))
2740 ;; If we have sticky agenda buffers, set a name for the buffer,
2741 ;; depending on the invoking keys. The user may still set this
2742 ;; as a command option, which will overwrite what we do here.
2743 (if org-agenda-sticky
2744 (setq org-agenda-buffer-name
2745 (format "*Org Agenda(%s)*" org-keys)))
2746 ;; Establish the restriction, if any
2747 (when (and (not org-agenda-overriding-restriction) restriction)
2748 (put 'org-agenda-files 'org-restrict (list bfn))
2749 (cond
2750 ((eq restriction 'region)
2751 (setq org-agenda-restrict (current-buffer))
2752 (move-marker org-agenda-restrict-begin (region-beginning))
2753 (move-marker org-agenda-restrict-end (region-end)))
2754 ((eq restriction 'subtree)
2755 (save-excursion
2756 (setq org-agenda-restrict (current-buffer))
2757 (org-back-to-heading t)
2758 (move-marker org-agenda-restrict-begin (point))
2759 (move-marker org-agenda-restrict-end
2760 (progn (org-end-of-subtree t)))))))
2762 ;; For example the todo list should not need it (but does...)
2763 (cond
2764 ((setq entry (assoc org-keys org-agenda-custom-commands))
2765 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2766 (progn
2767 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2768 lprops (nth 4 entry))
2769 (if org-agenda-sticky
2770 (setq org-agenda-buffer-name
2771 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2772 (format "*Org Agenda(%s)*" org-keys))))
2773 (put 'org-agenda-redo-command 'org-lprops lprops)
2774 (cond
2775 ((eq type 'agenda)
2776 (org-let lprops '(org-agenda-list current-prefix-arg)))
2777 ((eq type 'agenda*)
2778 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2779 ((eq type 'alltodo)
2780 (org-let lprops '(org-todo-list current-prefix-arg)))
2781 ((eq type 'search)
2782 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2783 ((eq type 'stuck)
2784 (org-let lprops '(org-agenda-list-stuck-projects
2785 current-prefix-arg)))
2786 ((eq type 'tags)
2787 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2788 ((eq type 'tags-todo)
2789 (org-let lprops '(org-tags-view '(4) org-match)))
2790 ((eq type 'todo)
2791 (org-let lprops '(org-todo-list org-match)))
2792 ((eq type 'tags-tree)
2793 (org-check-for-org-mode)
2794 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2795 ((eq type 'todo-tree)
2796 (org-check-for-org-mode)
2797 (org-let lprops
2798 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2799 (regexp-quote org-match) "\\>"))))
2800 ((eq type 'occur-tree)
2801 (org-check-for-org-mode)
2802 (org-let lprops '(org-occur org-match)))
2803 ((functionp type)
2804 (org-let lprops '(funcall type org-match)))
2805 ((fboundp type)
2806 (org-let lprops '(funcall type org-match)))
2807 (t (user-error "Invalid custom agenda command type %s" type))))
2808 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2809 ((equal org-keys "C")
2810 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2811 (customize-variable 'org-agenda-custom-commands))
2812 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2813 ((equal org-keys "s") (call-interactively 'org-search-view))
2814 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2815 ((equal org-keys "t") (call-interactively 'org-todo-list))
2816 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2817 ((equal org-keys "m") (call-interactively 'org-tags-view))
2818 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2819 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2820 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2821 (org-add-hook
2822 'post-command-hook
2823 (lambda ()
2824 (unless (current-message)
2825 (let* ((m (org-agenda-get-any-marker))
2826 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2827 (when note
2828 (message (concat
2829 "FLAGGING-NOTE ([?] for more info): "
2830 (org-add-props
2831 (replace-regexp-in-string
2832 "\\\\n" "//"
2833 (copy-sequence note))
2834 nil 'face 'org-warning)))))))
2835 t t))
2836 ((equal org-keys "L")
2837 (unless (derived-mode-p 'org-mode)
2838 (user-error "This is not an Org-mode file"))
2839 (unless restriction
2840 (put 'org-agenda-files 'org-restrict (list bfn))
2841 (org-call-with-arg 'org-timeline arg)))
2842 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2843 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2844 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2845 (t (user-error "Invalid agenda key"))))))
2847 (defvar org-agenda-multi)
2849 (defun org-agenda-append-agenda ()
2850 "Append another agenda view to the current one.
2851 This function allows interactive building of block agendas.
2852 Agenda views are separated by `org-agenda-block-separator'."
2853 (interactive)
2854 (unless (derived-mode-p 'org-agenda-mode)
2855 (user-error "Can only append from within agenda buffer"))
2856 (let ((org-agenda-multi t))
2857 (org-agenda)
2858 (widen)
2859 (org-agenda-finalize)
2860 (setq buffer-read-only t)
2861 (org-agenda-fit-window-to-buffer)))
2863 (defun org-agenda-normalize-custom-commands (cmds)
2864 "Normalize custom commands CMDS."
2865 (delq nil
2866 (mapcar
2867 (lambda (x)
2868 (cond ((stringp (cdr x)) nil)
2869 ((stringp (nth 1 x)) x)
2870 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2871 (t (cons (car x) (cons "" (cdr x))))))
2872 cmds)))
2874 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2875 "The user interface for selecting an agenda command."
2876 (catch 'exit
2877 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2878 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2879 (region-p (org-region-active-p))
2880 (custom org-agenda-custom-commands)
2881 (selstring "")
2882 restriction second-time
2883 c entry key type match prefixes rmheader header-end custom1 desc
2884 line lines left right n n1)
2885 (save-window-excursion
2886 (delete-other-windows)
2887 (org-switch-to-buffer-other-window " *Agenda Commands*")
2888 (erase-buffer)
2889 (insert (eval-when-compile
2890 (let ((header
2891 "Press key for an agenda command: < Buffer, subtree/region restriction
2892 -------------------------------- > Remove restriction
2893 a Agenda for current week or day e Export agenda views
2894 t List of all TODO entries T Entries with special TODO kwd
2895 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2896 s Search for keywords S Like s, but only TODO entries
2897 L Timeline for current buffer # List stuck projects (!=configure)
2898 / Multi-occur C Configure custom agenda commands
2899 ? Find :FLAGGED: entries * Toggle sticky agenda views
2901 (start 0))
2902 (while (string-match
2903 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2904 header start)
2905 (setq start (match-end 0))
2906 (add-text-properties (match-beginning 2) (match-end 2)
2907 '(face bold) header))
2908 header)))
2909 (setq header-end (point-marker))
2910 (while t
2911 (setq custom1 custom)
2912 (when (eq rmheader t)
2913 (org-goto-line 1)
2914 (re-search-forward ":" nil t)
2915 (delete-region (match-end 0) (point-at-eol))
2916 (forward-char 1)
2917 (looking-at "-+")
2918 (delete-region (match-end 0) (point-at-eol))
2919 (move-marker header-end (match-end 0)))
2920 (goto-char header-end)
2921 (delete-region (point) (point-max))
2923 ;; Produce all the lines that describe custom commands and prefixes
2924 (setq lines nil)
2925 (while (setq entry (pop custom1))
2926 (setq key (car entry) desc (nth 1 entry)
2927 type (nth 2 entry)
2928 match (nth 3 entry))
2929 (if (> (length key) 1)
2930 (add-to-list 'prefixes (string-to-char key))
2931 (setq line
2932 (format
2933 "%-4s%-14s"
2934 (org-add-props (copy-sequence key)
2935 '(face bold))
2936 (cond
2937 ((string-match "\\S-" desc) desc)
2938 ((eq type 'agenda) "Agenda for current week or day")
2939 ((eq type 'agenda*) "Appointments for current week or day")
2940 ((eq type 'alltodo) "List of all TODO entries")
2941 ((eq type 'search) "Word search")
2942 ((eq type 'stuck) "List of stuck projects")
2943 ((eq type 'todo) "TODO keyword")
2944 ((eq type 'tags) "Tags query")
2945 ((eq type 'tags-todo) "Tags (TODO)")
2946 ((eq type 'tags-tree) "Tags tree")
2947 ((eq type 'todo-tree) "TODO kwd tree")
2948 ((eq type 'occur-tree) "Occur tree")
2949 ((functionp type) (if (symbolp type)
2950 (symbol-name type)
2951 "Lambda expression"))
2952 (t "???"))))
2953 (if org-agenda-menu-show-matcher
2954 (setq line
2955 (concat line ": "
2956 (cond
2957 ((stringp match)
2958 (setq match (copy-sequence match))
2959 (org-add-props match nil 'face 'org-warning))
2960 ((listp type)
2961 (format "set of %d commands" (length type))))))
2962 (if (org-string-nw-p match)
2963 (add-text-properties
2964 0 (length line) (list 'help-echo
2965 (concat "Matcher: " match)) line)))
2966 (push line lines)))
2967 (setq lines (nreverse lines))
2968 (when prefixes
2969 (mapc (lambda (x)
2970 (push
2971 (format "%s %s"
2972 (org-add-props (char-to-string x)
2973 nil 'face 'bold)
2974 (or (cdr (assoc (concat selstring
2975 (char-to-string x))
2976 prefix-descriptions))
2977 "Prefix key"))
2978 lines))
2979 prefixes))
2981 ;; Check if we should display in two columns
2982 (if org-agenda-menu-two-columns
2983 (progn
2984 (setq n (length lines)
2985 n1 (+ (/ n 2) (mod n 2))
2986 right (nthcdr n1 lines)
2987 left (copy-sequence lines))
2988 (setcdr (nthcdr (1- n1) left) nil))
2989 (setq left lines right nil))
2990 (while left
2991 (insert "\n" (pop left))
2992 (when right
2993 (if (< (current-column) 40)
2994 (move-to-column 40 t)
2995 (insert " "))
2996 (insert (pop right))))
2998 ;; Make the window the right size
2999 (goto-char (point-min))
3000 (if second-time
3001 (if (not (pos-visible-in-window-p (point-max)))
3002 (org-fit-window-to-buffer))
3003 (setq second-time t)
3004 (org-fit-window-to-buffer))
3006 ;; Ask for selection
3007 (message "Press key for agenda command%s:"
3008 (if (or restrict-ok org-agenda-overriding-restriction)
3009 (if org-agenda-overriding-restriction
3010 " (restriction lock active)"
3011 (if restriction
3012 (format " (restricted to %s)" restriction)
3013 " (unrestricted)"))
3014 ""))
3015 (setq c (read-char-exclusive))
3016 (message "")
3017 (cond
3018 ((assoc (char-to-string c) custom)
3019 (setq selstring (concat selstring (char-to-string c)))
3020 (throw 'exit (cons selstring restriction)))
3021 ((memq c prefixes)
3022 (setq selstring (concat selstring (char-to-string c))
3023 prefixes nil
3024 rmheader (or rmheader t)
3025 custom (delq nil (mapcar
3026 (lambda (x)
3027 (if (or (= (length (car x)) 1)
3028 (/= (string-to-char (car x)) c))
3030 (cons (substring (car x) 1) (cdr x))))
3031 custom))))
3032 ((eq c ?*)
3033 (call-interactively 'org-toggle-sticky-agenda)
3034 (sit-for 2))
3035 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3036 (message "Restriction is only possible in Org-mode buffers")
3037 (ding) (sit-for 1))
3038 ((eq c ?1)
3039 (org-agenda-remove-restriction-lock 'noupdate)
3040 (setq restriction 'buffer))
3041 ((eq c ?0)
3042 (org-agenda-remove-restriction-lock 'noupdate)
3043 (setq restriction (if region-p 'region 'subtree)))
3044 ((eq c ?<)
3045 (org-agenda-remove-restriction-lock 'noupdate)
3046 (setq restriction
3047 (cond
3048 ((eq restriction 'buffer)
3049 (if region-p 'region 'subtree))
3050 ((memq restriction '(subtree region))
3051 nil)
3052 (t 'buffer))))
3053 ((eq c ?>)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction nil))
3056 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3057 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3058 ((and (> (length selstring) 0) (eq c ?\d))
3059 (delete-window)
3060 (org-agenda-get-restriction-and-command prefix-descriptions))
3062 ((equal c ?q) (error "Abort"))
3063 (t (user-error "Invalid key %c" c))))))))
3065 (defun org-agenda-fit-window-to-buffer ()
3066 "Fit the window to the buffer size."
3067 (and (memq org-agenda-window-setup '(reorganize-frame))
3068 (fboundp 'fit-window-to-buffer)
3069 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3070 (= (car org-agenda-window-frame-fractions) 1.0))
3071 (delete-other-windows)
3072 (org-fit-window-to-buffer
3074 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3075 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3077 (defvar org-cmd nil)
3078 (defvar org-agenda-overriding-cmd nil)
3079 (defvar org-agenda-overriding-arguments nil)
3080 (defvar org-agenda-overriding-cmd-arguments nil)
3081 (defun org-agenda-run-series (name series)
3082 "Run agenda NAME as a SERIES of agenda commands."
3083 (org-let (nth 1 series) '(org-agenda-prepare name))
3084 ;; We need to reset agenda markers here, because when constructing a
3085 ;; block agenda, the individual blocks do not do that.
3086 (org-agenda-reset-markers)
3087 (let* ((org-agenda-multi t)
3088 (redo (list 'org-agenda-run-series name (list 'quote series)))
3089 (cmds (car series))
3090 (gprops (nth 1 series))
3091 match ;; The byte compiler incorrectly complains about this. Keep it!
3092 org-cmd type lprops)
3093 (while (setq org-cmd (pop cmds))
3094 (setq type (car org-cmd)
3095 match (eval (nth 1 org-cmd))
3096 lprops (nth 2 org-cmd))
3097 (let ((org-agenda-overriding-arguments
3098 (if (eq org-agenda-overriding-cmd org-cmd)
3099 (or org-agenda-overriding-arguments
3100 org-agenda-overriding-cmd-arguments))))
3101 (cond
3102 ((eq type 'agenda)
3103 (org-let2 gprops lprops
3104 '(call-interactively 'org-agenda-list)))
3105 ((eq type 'agenda*)
3106 (org-let2 gprops lprops
3107 '(funcall 'org-agenda-list nil nil t)))
3108 ((eq type 'alltodo)
3109 (org-let2 gprops lprops
3110 '(call-interactively 'org-todo-list)))
3111 ((eq type 'search)
3112 (org-let2 gprops lprops
3113 '(org-search-view current-prefix-arg match nil)))
3114 ((eq type 'stuck)
3115 (org-let2 gprops lprops
3116 '(call-interactively 'org-agenda-list-stuck-projects)))
3117 ((eq type 'tags)
3118 (org-let2 gprops lprops
3119 '(org-tags-view current-prefix-arg match)))
3120 ((eq type 'tags-todo)
3121 (org-let2 gprops lprops
3122 '(org-tags-view '(4) match)))
3123 ((eq type 'todo)
3124 (org-let2 gprops lprops
3125 '(org-todo-list match)))
3126 ((fboundp type)
3127 (org-let2 gprops lprops
3128 '(funcall type match)))
3129 (t (error "Invalid type in command series")))))
3130 (widen)
3131 (let ((inhibit-read-only t))
3132 (add-text-properties (point-min) (point-max)
3133 `(org-series t org-series-redo-cmd ,redo)))
3134 (setq org-agenda-redo-command redo)
3135 (goto-char (point-min)))
3136 (org-agenda-fit-window-to-buffer)
3137 (org-let (nth 1 series) '(org-agenda-finalize)))
3139 ;;;###autoload
3140 (defmacro org-batch-agenda (cmd-key &rest parameters)
3141 "Run an agenda command in batch mode and send the result to STDOUT.
3142 If CMD-KEY is a string of length 1, it is used as a key in
3143 `org-agenda-custom-commands' and triggers this command. If it is a
3144 longer string it is used as a tags/todo match string.
3145 Parameters are alternating variable names and values that will be bound
3146 before running the agenda command."
3147 (org-eval-in-environment (org-make-parameter-alist parameters)
3148 (let (org-agenda-sticky)
3149 (if (> (length cmd-key) 2)
3150 (org-tags-view nil cmd-key)
3151 (org-agenda nil cmd-key))))
3152 (set-buffer org-agenda-buffer-name)
3153 (princ (buffer-string)))
3155 (defvar org-agenda-info nil)
3157 ;;;###autoload
3158 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3159 "Run an agenda command in batch mode and send the result to STDOUT.
3160 If CMD-KEY is a string of length 1, it is used as a key in
3161 `org-agenda-custom-commands' and triggers this command. If it is a
3162 longer string it is used as a tags/todo match string.
3163 Parameters are alternating variable names and values that will be bound
3164 before running the agenda command.
3166 The output gives a line for each selected agenda item. Each
3167 item is a list of comma-separated values, like this:
3169 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3171 category The category of the item
3172 head The headline, without TODO kwd, TAGS and PRIORITY
3173 type The type of the agenda entry, can be
3174 todo selected in TODO match
3175 tagsmatch selected in tags match
3176 diary imported from diary
3177 deadline a deadline on given date
3178 scheduled scheduled on given date
3179 timestamp entry has timestamp on given date
3180 closed entry was closed on given date
3181 upcoming-deadline warning about deadline
3182 past-scheduled forwarded scheduled item
3183 block entry has date block including g. date
3184 todo The todo keyword, if any
3185 tags All tags including inherited ones, separated by colons
3186 date The relevant date, like 2007-2-14
3187 time The time, like 15:00-16:50
3188 extra Sting with extra planning info
3189 priority-l The priority letter if any was given
3190 priority-n The computed numerical priority
3191 agenda-day The day in the agenda where this is listed"
3192 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3193 (org-make-parameter-alist parameters))
3194 (if (> (length cmd-key) 2)
3195 (org-tags-view nil cmd-key)
3196 (org-agenda nil cmd-key)))
3197 (set-buffer org-agenda-buffer-name)
3198 (let* ((lines (org-split-string (buffer-string) "\n"))
3199 line)
3200 (while (setq line (pop lines))
3201 (catch 'next
3202 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3203 (setq org-agenda-info
3204 (org-fix-agenda-info (text-properties-at 0 line)))
3205 (princ
3206 (mapconcat 'org-agenda-export-csv-mapper
3207 '(org-category txt type todo tags date time extra
3208 priority-letter priority agenda-day)
3209 ","))
3210 (princ "\n")))))
3212 (defun org-fix-agenda-info (props)
3213 "Make sure all properties on an agenda item have a canonical form.
3214 This ensures the export commands can easily use it."
3215 (let (tmp re)
3216 (when (setq tmp (plist-get props 'tags))
3217 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3218 (when (setq tmp (plist-get props 'date))
3219 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3220 (let ((calendar-date-display-form '(year "-" month "-" day)))
3221 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3223 (setq tmp (calendar-date-string tmp)))
3224 (setq props (plist-put props 'date tmp)))
3225 (when (setq tmp (plist-get props 'day))
3226 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3227 (let ((calendar-date-display-form '(year "-" month "-" day)))
3228 (setq tmp (calendar-date-string tmp)))
3229 (setq props (plist-put props 'day tmp))
3230 (setq props (plist-put props 'agenda-day tmp)))
3231 (when (setq tmp (plist-get props 'txt))
3232 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3233 (plist-put props 'priority-letter (match-string 1 tmp))
3234 (setq tmp (replace-match "" t t tmp)))
3235 (when (and (setq re (plist-get props 'org-todo-regexp))
3236 (setq re (concat "\\`\\.*" re " ?"))
3237 (string-match re tmp))
3238 (plist-put props 'todo (match-string 1 tmp))
3239 (setq tmp (replace-match "" t t tmp)))
3240 (plist-put props 'txt tmp)))
3241 props)
3243 (defun org-agenda-export-csv-mapper (prop)
3244 (let ((res (plist-get org-agenda-info prop)))
3245 (setq res
3246 (cond
3247 ((not res) "")
3248 ((stringp res) res)
3249 (t (prin1-to-string res))))
3250 (while (string-match "," res)
3251 (setq res (replace-match ";" t t res)))
3252 (org-trim res)))
3254 ;;;###autoload
3255 (defun org-store-agenda-views (&rest parameters)
3256 "Store agenda views."
3257 (interactive)
3258 (eval (list 'org-batch-store-agenda-views)))
3260 ;;;###autoload
3261 (defmacro org-batch-store-agenda-views (&rest parameters)
3262 "Run all custom agenda commands that have a file argument."
3263 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3264 (pop-up-frames nil)
3265 (dir default-directory)
3266 (pars (org-make-parameter-alist parameters))
3267 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3268 (save-window-excursion
3269 (while cmds
3270 (setq cmd (pop cmds)
3271 thiscmdkey (car cmd)
3272 thiscmdcmd (cdr cmd)
3273 match (nth 2 thiscmdcmd)
3274 bufname (if org-agenda-sticky
3275 (or (and (stringp match)
3276 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3277 (format "*Org Agenda(%s)*" thiscmdkey))
3278 org-agenda-buffer-name)
3279 cmd-or-set (nth 2 cmd)
3280 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3281 files (nth (if (listp cmd-or-set) 4 5) cmd))
3282 (if (stringp files) (setq files (list files)))
3283 (when files
3284 (org-eval-in-environment (append org-agenda-exporter-settings
3285 opts pars)
3286 (org-agenda nil thiscmdkey))
3287 (set-buffer bufname)
3288 (while files
3289 (org-eval-in-environment (append org-agenda-exporter-settings
3290 opts pars)
3291 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3292 (and (get-buffer bufname)
3293 (kill-buffer bufname)))))))
3295 (defvar org-agenda-current-span nil
3296 "The current span used in the agenda view.") ; local variable in the agenda buffer
3297 (defun org-agenda-mark-header-line (pos)
3298 "Mark the line at POS as an agenda structure header."
3299 (save-excursion
3300 (goto-char pos)
3301 (put-text-property (point-at-bol) (point-at-eol)
3302 'org-agenda-structural-header t)
3303 (when org-agenda-title-append
3304 (put-text-property (point-at-bol) (point-at-eol)
3305 'org-agenda-title-append org-agenda-title-append))))
3307 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3308 (defvar org-agenda-write-buffer-name "Agenda View")
3309 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3310 "Write the current buffer (an agenda view) as a file.
3311 Depending on the extension of the file name, plain text (.txt),
3312 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3313 If the extension is .ics, run icalendar export over all files used
3314 to construct the agenda and limit the export to entries listed in the
3315 agenda now.
3316 If the extension is .org, collect all subtrees corresponding to the
3317 agenda entries and add them in an .org file.
3318 With prefix argument OPEN, open the new file immediately.
3319 If NOSETTINGS is given, do not scope the settings of
3320 `org-agenda-exporter-settings' into the export commands. This is used when
3321 the settings have already been scoped and we do not wish to overrule other,
3322 higher priority settings.
3323 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3324 (interactive "FWrite agenda to file: \nP")
3325 (if (or (not (file-writable-p file))
3326 (and (file-exists-p file)
3327 (if (org-called-interactively-p 'any)
3328 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3329 (user-error "Cannot write agenda to file %s" file))
3330 (org-let (if nosettings nil org-agenda-exporter-settings)
3331 '(save-excursion
3332 (save-window-excursion
3333 (let ((bs (copy-sequence (buffer-string))) beg content)
3334 (with-temp-buffer
3335 (rename-buffer org-agenda-write-buffer-name t)
3336 (set-buffer-modified-p nil)
3337 (insert bs)
3338 (org-agenda-remove-marked-text 'org-filtered)
3339 (run-hooks 'org-agenda-before-write-hook)
3340 (cond
3341 ((org-bound-and-true-p org-mobile-creating-agendas)
3342 (org-mobile-write-agenda-for-mobile file))
3343 ((string-match "\\.org\\'" file)
3344 (let (content p m message-log-max)
3345 (goto-char (point-min))
3346 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3347 (goto-char p)
3348 (setq m (get-text-property (point) 'org-hd-marker))
3349 (when m
3350 (push (save-excursion
3351 (set-buffer (marker-buffer m))
3352 (goto-char m)
3353 (org-copy-subtree 1 nil t t)
3354 org-subtree-clip)
3355 content)))
3356 (find-file file)
3357 (erase-buffer)
3358 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3359 (write-file file)
3360 (kill-buffer (current-buffer))
3361 (message "Org file written to %s" file)))
3362 ((string-match "\\.html?\\'" file)
3363 (require 'htmlize)
3364 (set-buffer (htmlize-buffer (current-buffer)))
3365 (when org-agenda-export-html-style
3366 ;; replace <style> section with org-agenda-export-html-style
3367 (goto-char (point-min))
3368 (kill-region (- (search-forward "<style") 6)
3369 (search-forward "</style>"))
3370 (insert org-agenda-export-html-style))
3371 (write-file file)
3372 (kill-buffer (current-buffer))
3373 (message "HTML written to %s" file))
3374 ((string-match "\\.ps\\'" file)
3375 (require 'ps-print)
3376 (ps-print-buffer-with-faces file)
3377 (message "Postscript written to %s" file))
3378 ((string-match "\\.pdf\\'" file)
3379 (require 'ps-print)
3380 (ps-print-buffer-with-faces
3381 (concat (file-name-sans-extension file) ".ps"))
3382 (call-process "ps2pdf" nil nil nil
3383 (expand-file-name
3384 (concat (file-name-sans-extension file) ".ps"))
3385 (expand-file-name file))
3386 (delete-file (concat (file-name-sans-extension file) ".ps"))
3387 (message "PDF written to %s" file))
3388 ((string-match "\\.ics\\'" file)
3389 (require 'ox-icalendar)
3390 (org-icalendar-export-current-agenda (expand-file-name file)))
3392 (let ((bs (buffer-string)))
3393 (find-file file)
3394 (erase-buffer)
3395 (insert bs)
3396 (save-buffer 0)
3397 (kill-buffer (current-buffer))
3398 (message "Plain text written to %s" file))))))))
3399 (set-buffer (or agenda-bufname
3400 (and (org-called-interactively-p 'any) (buffer-name))
3401 org-agenda-buffer-name)))
3402 (when open (org-open-file file)))
3404 (defun org-agenda-remove-marked-text (property &optional value)
3405 "Delete all text marked with VALUE of PROPERTY.
3406 VALUE defaults to t."
3407 (let (beg)
3408 (setq value (or value t))
3409 (while (setq beg (text-property-any (point-min) (point-max)
3410 property value))
3411 (delete-region
3412 beg (or (next-single-property-change beg property)
3413 (point-max))))))
3415 (defun org-agenda-add-entry-text ()
3416 "Add entry text to agenda lines.
3417 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3418 entry text following headings shown in the agenda.
3419 Drawers will be excluded, also the line with scheduling/deadline info."
3420 (when (and (> org-agenda-add-entry-text-maxlines 0)
3421 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3422 (let (m txt)
3423 (goto-char (point-min))
3424 (while (not (eobp))
3425 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3426 (beginning-of-line 2)
3427 (setq txt (org-agenda-get-some-entry-text
3428 m org-agenda-add-entry-text-maxlines " > "))
3429 (end-of-line 1)
3430 (if (string-match "\\S-" txt)
3431 (insert "\n" txt)
3432 (or (eobp) (forward-char 1))))))))
3434 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3435 &rest keep)
3436 "Extract entry text from MARKER, at most N-LINES lines.
3437 This will ignore drawers etc, just get the text.
3438 If INDENT is given, prefix every line with this string. If KEEP is
3439 given, it is a list of symbols, defining stuff that should not be
3440 removed from the entry content. Currently only `planning' is allowed here."
3441 (let (txt drawer-re kwd-time-re ind)
3442 (save-excursion
3443 (with-current-buffer (marker-buffer marker)
3444 (if (not (derived-mode-p 'org-mode))
3445 (setq txt "")
3446 (save-excursion
3447 (save-restriction
3448 (widen)
3449 (goto-char marker)
3450 (end-of-line 1)
3451 (setq txt (buffer-substring
3452 (min (1+ (point)) (point-max))
3453 (progn (outline-next-heading) (point)))
3454 drawer-re org-drawer-regexp
3455 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3456 ".*\n?"))
3457 (with-temp-buffer
3458 (insert txt)
3459 (when org-agenda-add-entry-text-descriptive-links
3460 (goto-char (point-min))
3461 (while (org-activate-bracket-links (point-max))
3462 (add-text-properties (match-beginning 0) (match-end 0)
3463 '(face org-link))))
3464 (goto-char (point-min))
3465 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3466 (set-text-properties (match-beginning 0) (match-end 0)
3467 nil))
3468 (goto-char (point-min))
3469 (while (re-search-forward drawer-re nil t)
3470 (delete-region
3471 (match-beginning 0)
3472 (progn (re-search-forward
3473 "^[ \t]*:END:.*\n?" nil 'move)
3474 (point))))
3475 (unless (member 'planning keep)
3476 (goto-char (point-min))
3477 (while (re-search-forward kwd-time-re nil t)
3478 (replace-match "")))
3479 (goto-char (point-min))
3480 (when org-agenda-entry-text-exclude-regexps
3481 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3482 (while (setq re (pop re-list))
3483 (goto-char (point-min))
3484 (while (re-search-forward re nil t)
3485 (replace-match "")))))
3486 (goto-char (point-max))
3487 (skip-chars-backward " \t\n")
3488 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3490 ;; find and remove min common indentation
3491 (goto-char (point-min))
3492 (untabify (point-min) (point-max))
3493 (setq ind (org-get-indentation))
3494 (while (not (eobp))
3495 (unless (looking-at "[ \t]*$")
3496 (setq ind (min ind (org-get-indentation))))
3497 (beginning-of-line 2))
3498 (goto-char (point-min))
3499 (while (not (eobp))
3500 (unless (looking-at "[ \t]*$")
3501 (move-to-column ind)
3502 (delete-region (point-at-bol) (point)))
3503 (beginning-of-line 2))
3505 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3507 (goto-char (point-min))
3508 (when indent
3509 (while (and (not (eobp)) (re-search-forward "^" nil t))
3510 (replace-match indent t t)))
3511 (goto-char (point-min))
3512 (while (looking-at "[ \t]*\n") (replace-match ""))
3513 (goto-char (point-max))
3514 (when (> (org-current-line)
3515 n-lines)
3516 (org-goto-line (1+ n-lines))
3517 (backward-char 1))
3518 (setq txt (buffer-substring (point-min) (point)))))))))
3519 txt))
3521 (defun org-check-for-org-mode ()
3522 "Make sure current buffer is in org-mode. Error if not."
3523 (or (derived-mode-p 'org-mode)
3524 (error "Cannot execute org-mode agenda command on buffer in %s"
3525 major-mode)))
3527 ;;; Agenda prepare and finalize
3529 (defvar org-agenda-multi nil) ; dynamically scoped
3530 (defvar org-agenda-pre-window-conf nil)
3531 (defvar org-agenda-columns-active nil)
3532 (defvar org-agenda-name nil)
3533 (defvar org-agenda-tag-filter nil)
3534 (defvar org-agenda-category-filter nil)
3535 (defvar org-agenda-regexp-filter nil)
3536 (defvar org-agenda-effort-filter nil)
3537 (defvar org-agenda-top-headline-filter nil)
3538 (defvar org-agenda-tag-filter-while-redo nil)
3539 (defvar org-agenda-tag-filter-preset nil
3540 "A preset of the tags filter used for secondary agenda filtering.
3541 This must be a list of strings, each string must be a single tag preceded
3542 by \"+\" or \"-\".
3543 This variable should not be set directly, but agenda custom commands can
3544 bind it in the options section. The preset filter is a global property of
3545 the entire agenda view. In a block agenda, it will not work reliably to
3546 define a filter for one of the individual blocks. You need to set it in
3547 the global options and expect it to be applied to the entire view.")
3549 (defvar org-agenda-category-filter-preset nil
3550 "A preset of the category filter used for secondary agenda filtering.
3551 This must be a list of strings, each string must be a single category
3552 preceded by \"+\" or \"-\".
3553 This variable should not be set directly, but agenda custom commands can
3554 bind it in the options section. The preset filter is a global property of
3555 the entire agenda view. In a block agenda, it will not work reliably to
3556 define a filter for one of the individual blocks. You need to set it in
3557 the global options and expect it to be applied to the entire view.")
3559 (defvar org-agenda-regexp-filter-preset nil
3560 "A preset of the regexp filter used for secondary agenda filtering.
3561 This must be a list of strings, each string must be a single regexp
3562 preceded by \"+\" or \"-\".
3563 This variable should not be set directly, but agenda custom commands can
3564 bind it in the options section. The preset filter is a global property of
3565 the entire agenda view. In a block agenda, it will not work reliably to
3566 define a filter for one of the individual blocks. You need to set it in
3567 the global options and expect it to be applied to the entire view.")
3569 (defvar org-agenda-effort-filter-preset nil
3570 "A preset of the effort condition used for secondary agenda filtering.
3571 This must be a list of strings, each string must be a single regexp
3572 preceded by \"+\" or \"-\".
3573 This variable should not be set directly, but agenda custom commands can
3574 bind it in the options section. The preset filter is a global property of
3575 the entire agenda view. In a block agenda, it will not work reliably to
3576 define a filter for one of the individual blocks. You need to set it in
3577 the global options and expect it to be applied to the entire view.")
3579 (defun org-agenda-use-sticky-p ()
3580 "Return non-nil if an agenda buffer named
3581 `org-agenda-buffer-name' exists and should be shown instead of
3582 generating a new one."
3583 (and
3584 ;; turned off by user
3585 org-agenda-sticky
3586 ;; For multi-agenda buffer already exists
3587 (not org-agenda-multi)
3588 ;; buffer found
3589 (get-buffer org-agenda-buffer-name)
3590 ;; C-u parameter is same as last call
3591 (with-current-buffer (get-buffer org-agenda-buffer-name)
3592 (and
3593 (equal current-prefix-arg
3594 org-agenda-last-prefix-arg)
3595 ;; In case user turned stickiness on, while having existing
3596 ;; Agenda buffer active, don't reuse that buffer, because it
3597 ;; does not have org variables local
3598 org-agenda-this-buffer-is-sticky))))
3600 (defun org-agenda-prepare-window (abuf filter-alist)
3601 "Setup agenda buffer in the window.
3602 ABUF is the buffer for the agenda window.
3603 FILTER-ALIST is an alist of filters we need to apply when
3604 `org-agenda-persistent-filter' is non-nil."
3605 (let* ((awin (get-buffer-window abuf)) wconf)
3606 (cond
3607 ((equal (current-buffer) abuf) nil)
3608 (awin (select-window awin))
3609 ((not (setq wconf (current-window-configuration))))
3610 ((equal org-agenda-window-setup 'current-window)
3611 (org-pop-to-buffer-same-window abuf))
3612 ((equal org-agenda-window-setup 'other-window)
3613 (org-switch-to-buffer-other-window abuf))
3614 ((equal org-agenda-window-setup 'other-frame)
3615 (switch-to-buffer-other-frame abuf))
3616 ((equal org-agenda-window-setup 'reorganize-frame)
3617 (delete-other-windows)
3618 (org-switch-to-buffer-other-window abuf)))
3619 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3620 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3621 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3622 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3623 ;; Additional test in case agenda is invoked from within agenda
3624 ;; buffer via elisp link.
3625 (unless (equal (current-buffer) abuf)
3626 (org-pop-to-buffer-same-window abuf))
3627 (setq org-agenda-pre-window-conf
3628 (or org-agenda-pre-window-conf wconf))))
3630 (defun org-agenda-prepare (&optional name)
3631 (let ((filter-alist (if org-agenda-persistent-filter
3632 (list `(tag . ,org-agenda-tag-filter)
3633 `(re . ,org-agenda-regexp-filter)
3634 `(effort . ,org-agenda-effort-filter)
3635 `(car . ,org-agenda-category-filter)))))
3636 (if (org-agenda-use-sticky-p)
3637 (progn
3638 ;; Popup existing buffer
3639 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3640 filter-alist)
3641 (message "Sticky Agenda buffer, use `r' to refresh")
3642 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3643 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3644 (setq org-todo-keywords-for-agenda nil)
3645 (put 'org-agenda-tag-filter :preset-filter
3646 org-agenda-tag-filter-preset)
3647 (put 'org-agenda-category-filter :preset-filter
3648 org-agenda-category-filter-preset)
3649 (put 'org-agenda-regexp-filter :preset-filter
3650 org-agenda-regexp-filter-preset)
3651 (put 'org-agenda-effort-filter :preset-filter
3652 org-agenda-effort-filter-preset)
3653 (if org-agenda-multi
3654 (progn
3655 (setq buffer-read-only nil)
3656 (goto-char (point-max))
3657 (unless (or (bobp) org-agenda-compact-blocks
3658 (not org-agenda-block-separator))
3659 (insert "\n"
3660 (if (stringp org-agenda-block-separator)
3661 org-agenda-block-separator
3662 (make-string (window-width) org-agenda-block-separator))
3663 "\n"))
3664 (narrow-to-region (point) (point-max)))
3665 (setq org-done-keywords-for-agenda nil)
3666 ;; Setting any org variables that are in org-agenda-local-vars
3667 ;; list need to be done after the prepare call
3668 (org-agenda-prepare-window
3669 (get-buffer-create org-agenda-buffer-name) filter-alist)
3670 (setq buffer-read-only nil)
3671 (org-agenda-reset-markers)
3672 (let ((inhibit-read-only t)) (erase-buffer))
3673 (org-agenda-mode)
3674 (setq org-agenda-buffer (current-buffer))
3675 (setq org-agenda-contributing-files nil)
3676 (setq org-agenda-columns-active nil)
3677 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3678 (setq org-todo-keywords-for-agenda
3679 (org-uniquify org-todo-keywords-for-agenda))
3680 (setq org-done-keywords-for-agenda
3681 (org-uniquify org-done-keywords-for-agenda))
3682 (setq org-agenda-last-prefix-arg current-prefix-arg)
3683 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3684 (and name (not org-agenda-name)
3685 (org-set-local 'org-agenda-name name)))
3686 (setq buffer-read-only nil))))
3688 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3689 (defun org-agenda-finalize ()
3690 "Finishing touch for the agenda buffer, called just before displaying it."
3691 (unless org-agenda-multi
3692 (save-excursion
3693 (let ((inhibit-read-only t))
3694 (goto-char (point-min))
3695 (save-excursion
3696 (while (org-activate-bracket-links (point-max))
3697 (add-text-properties (match-beginning 0) (match-end 0)
3698 '(face org-link))))
3699 (save-excursion
3700 (while (org-activate-plain-links (point-max))
3701 (add-text-properties (match-beginning 0) (match-end 0)
3702 '(face org-link))))
3703 (unless (eq org-agenda-remove-tags t)
3704 (org-agenda-align-tags))
3705 (unless org-agenda-with-colors
3706 (remove-text-properties (point-min) (point-max) '(face nil)))
3707 (if (and (boundp 'org-agenda-overriding-columns-format)
3708 org-agenda-overriding-columns-format)
3709 (org-set-local 'org-agenda-overriding-columns-format
3710 org-agenda-overriding-columns-format))
3711 (if (and (boundp 'org-agenda-view-columns-initially)
3712 org-agenda-view-columns-initially)
3713 (org-agenda-columns))
3714 (when org-agenda-fontify-priorities
3715 (org-agenda-fontify-priorities))
3716 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3717 (org-agenda-dim-blocked-tasks))
3718 (org-agenda-mark-clocking-task)
3719 (when org-agenda-entry-text-mode
3720 (org-agenda-entry-text-hide)
3721 (org-agenda-entry-text-show))
3722 (if (and (functionp 'org-habit-insert-consistency-graphs)
3723 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3724 (org-habit-insert-consistency-graphs))
3725 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3726 (unless (or (eq org-agenda-show-inherited-tags 'always)
3727 (and (listp org-agenda-show-inherited-tags)
3728 (memq org-agenda-type org-agenda-show-inherited-tags))
3729 (and (eq org-agenda-show-inherited-tags t)
3730 (or (eq org-agenda-use-tag-inheritance t)
3731 (and (listp org-agenda-use-tag-inheritance)
3732 (not (memq org-agenda-type
3733 org-agenda-use-tag-inheritance))))))
3734 (let (mrk)
3735 (save-excursion
3736 (goto-char (point-min))
3737 (while (equal (forward-line) 0)
3738 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3739 (put-text-property (point-at-bol) (point-at-eol)
3740 'tags (org-with-point-at mrk
3741 (delete-dups
3742 (mapcar 'downcase (org-get-tags-at))))))))))
3743 (run-hooks 'org-agenda-finalize-hook)
3744 (when org-agenda-top-headline-filter
3745 (org-agenda-filter-top-headline-apply
3746 org-agenda-top-headline-filter))
3747 (when org-agenda-tag-filter
3748 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3749 (when (get 'org-agenda-tag-filter :preset-filter)
3750 (org-agenda-filter-apply
3751 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3752 (when org-agenda-category-filter
3753 (org-agenda-filter-apply org-agenda-category-filter 'category))
3754 (when (get 'org-agenda-category-filter :preset-filter)
3755 (org-agenda-filter-apply
3756 (get 'org-agenda-category-filter :preset-filter) 'category))
3757 (when org-agenda-regexp-filter
3758 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3759 (when (get 'org-agenda-regexp-filter :preset-filter)
3760 (org-agenda-filter-apply
3761 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3762 (when org-agenda-effort-filter
3763 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3764 (when (get 'org-agenda-effort-filter :preset-filter)
3765 (org-agenda-filter-apply
3766 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3767 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3769 (defun org-agenda-mark-clocking-task ()
3770 "Mark the current clock entry in the agenda if it is present."
3771 ;; We need to widen when `org-agenda-finalize' is called from
3772 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3773 (when org-clock-current-task
3774 (save-restriction
3775 (widen)
3776 (org-agenda-unmark-clocking-task)
3777 (when (marker-buffer org-clock-hd-marker)
3778 (save-excursion
3779 (goto-char (point-min))
3780 (let (s ov)
3781 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3782 (goto-char s)
3783 (when (equal (org-get-at-bol 'org-hd-marker)
3784 org-clock-hd-marker)
3785 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3786 (overlay-put ov 'type 'org-agenda-clocking)
3787 (overlay-put ov 'face 'org-agenda-clocking)
3788 (overlay-put ov 'help-echo
3789 "The clock is running in this item")))))))))
3791 (defun org-agenda-unmark-clocking-task ()
3792 "Unmark the current clocking task."
3793 (mapc (lambda (o)
3794 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3795 (delete-overlay o)))
3796 (overlays-in (point-min) (point-max))))
3798 (defun org-agenda-fontify-priorities ()
3799 "Make highest priority lines bold, and lowest italic."
3800 (interactive)
3801 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3802 (delete-overlay o)))
3803 (overlays-in (point-min) (point-max)))
3804 (save-excursion
3805 (let (b e p ov h l)
3806 (goto-char (point-min))
3807 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3808 (setq h (or (get-char-property (point) 'org-highest-priority)
3809 org-highest-priority)
3810 l (or (get-char-property (point) 'org-lowest-priority)
3811 org-lowest-priority)
3812 p (string-to-char (match-string 1))
3813 b (match-beginning 0)
3814 e (if (eq org-agenda-fontify-priorities 'cookies)
3815 (match-end 0)
3816 (point-at-eol))
3817 ov (make-overlay b e))
3818 (overlay-put
3819 ov 'face
3820 (cons (cond ((org-face-from-face-or-color
3821 'priority nil
3822 (cdr (assoc p org-priority-faces))))
3823 ((and (listp org-agenda-fontify-priorities)
3824 (org-face-from-face-or-color
3825 'priority nil
3826 (cdr (assoc p org-agenda-fontify-priorities)))))
3827 ((equal p l) 'italic)
3828 ((equal p h) 'bold))
3829 'org-priority))
3830 (overlay-put ov 'org-type 'org-priority)))))
3832 (defvar org-depend-tag-blocked)
3834 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3835 "Dim currently blocked TODO's in the agenda display.
3836 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3837 dimming them."
3838 (interactive "P")
3839 (when (org-called-interactively-p 'interactive)
3840 (message "Dim or hide blocked tasks..."))
3841 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3842 (delete-overlay o)))
3843 (overlays-in (point-min) (point-max)))
3844 (save-excursion
3845 (let ((inhibit-read-only t)
3846 (org-depend-tag-blocked nil)
3847 (invis (or (not (null invisible))
3848 (eq org-agenda-dim-blocked-tasks 'invisible)))
3849 org-blocked-by-checkboxes
3850 invis1 b e p ov h l)
3851 (goto-char (point-min))
3852 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3853 (and pos (goto-char (1+ pos))))
3854 (setq org-blocked-by-checkboxes nil invis1 invis)
3855 (let ((marker (org-get-at-bol 'org-hd-marker)))
3856 (when (and marker
3857 (with-current-buffer (marker-buffer marker)
3858 (save-excursion (goto-char marker)
3859 (org-entry-blocked-p))))
3860 (if org-blocked-by-checkboxes (setq invis1 nil))
3861 (setq b (if invis1
3862 (max (point-min) (1- (point-at-bol)))
3863 (point-at-bol))
3864 e (point-at-eol)
3865 ov (make-overlay b e))
3866 (if invis1
3867 (progn (overlay-put ov 'invisible t)
3868 (overlay-put ov 'intangible t))
3869 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3870 (overlay-put ov 'org-type 'org-blocked-todo))))))
3871 (when (org-called-interactively-p 'interactive)
3872 (message "Dim or hide blocked tasks...done")))
3874 (defvar org-agenda-skip-function nil
3875 "Function to be called at each match during agenda construction.
3876 If this function returns nil, the current match should not be skipped.
3877 Otherwise, the function must return a position from where the search
3878 should be continued.
3879 This may also be a Lisp form, it will be evaluated.
3880 Never set this variable using `setq' or so, because then it will apply
3881 to all future agenda commands. If you do want a global skipping condition,
3882 use the option `org-agenda-skip-function-global' instead.
3883 The correct usage for `org-agenda-skip-function' is to bind it with
3884 `let' to scope it dynamically into the agenda-constructing command.
3885 A good way to set it is through options in `org-agenda-custom-commands'.")
3887 (defun org-agenda-skip ()
3888 "Throw to `:skip' in places that should be skipped.
3889 Also moves point to the end of the skipped region, so that search can
3890 continue from there."
3891 (let ((p (point-at-bol)) to)
3892 (when (or
3893 (save-excursion (goto-char p) (looking-at comment-start-skip))
3894 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3895 (get-text-property p :org-archived)
3896 (org-end-of-subtree t))
3897 (and org-agenda-skip-comment-trees
3898 (get-text-property p :org-comment)
3899 (org-end-of-subtree t))
3900 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3901 (org-agenda-skip-eval org-agenda-skip-function)))
3902 (goto-char to))
3903 (org-in-src-block-p t))
3904 (throw :skip t))))
3906 (defun org-agenda-skip-eval (form)
3907 "If FORM is a function or a list, call (or eval) it and return the result.
3908 `save-excursion' and `save-match-data' are wrapped around the call, so point
3909 and match data are returned to the previous state no matter what these
3910 functions do."
3911 (let (fp)
3912 (and form
3913 (or (setq fp (functionp form))
3914 (consp form))
3915 (save-excursion
3916 (save-match-data
3917 (if fp
3918 (funcall form)
3919 (eval form)))))))
3921 (defvar org-agenda-markers nil
3922 "List of all currently active markers created by `org-agenda'.")
3923 (defvar org-agenda-last-marker-time (org-float-time)
3924 "Creation time of the last agenda marker.")
3926 (defun org-agenda-new-marker (&optional pos)
3927 "Return a new agenda marker.
3928 Org-mode keeps a list of these markers and resets them when they are
3929 no longer in use."
3930 (let ((m (copy-marker (or pos (point)))))
3931 (setq org-agenda-last-marker-time (org-float-time))
3932 (if org-agenda-buffer
3933 (with-current-buffer org-agenda-buffer
3934 (push m org-agenda-markers))
3935 (push m org-agenda-markers))
3938 (defun org-agenda-reset-markers ()
3939 "Reset markers created by `org-agenda'."
3940 (while org-agenda-markers
3941 (move-marker (pop org-agenda-markers) nil)))
3943 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3944 "Save relative positions of markers in region.
3945 This check for agenda markers in all agenda buffers currently active."
3946 (dolist (buf (buffer-list))
3947 (with-current-buffer buf
3948 (when (eq major-mode 'org-agenda-mode)
3949 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3950 org-agenda-markers)))))
3952 ;;; Entry text mode
3954 (defun org-agenda-entry-text-show-here ()
3955 "Add some text from the entry as context to the current line."
3956 (let (m txt o)
3957 (setq m (org-get-at-bol 'org-hd-marker))
3958 (unless (marker-buffer m)
3959 (error "No marker points to an entry here"))
3960 (setq txt (concat "\n" (org-no-properties
3961 (org-agenda-get-some-entry-text
3962 m org-agenda-entry-text-maxlines
3963 org-agenda-entry-text-leaders))))
3964 (when (string-match "\\S-" txt)
3965 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3966 (overlay-put o 'evaporate t)
3967 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3968 (overlay-put o 'after-string txt))))
3970 (defun org-agenda-entry-text-show ()
3971 "Add entry context for all agenda lines."
3972 (interactive)
3973 (save-excursion
3974 (goto-char (point-max))
3975 (beginning-of-line 1)
3976 (while (not (bobp))
3977 (when (org-get-at-bol 'org-hd-marker)
3978 (org-agenda-entry-text-show-here))
3979 (beginning-of-line 0))))
3981 (defun org-agenda-entry-text-hide ()
3982 "Remove any shown entry context."
3983 (delq nil
3984 (mapcar (lambda (o)
3985 (if (eq (overlay-get o 'org-overlay-type)
3986 'agenda-entry-content)
3987 (progn (delete-overlay o) t)))
3988 (overlays-in (point-min) (point-max)))))
3990 (defun org-agenda-get-day-face (date)
3991 "Return the face DATE should be displayed with."
3992 (or (and (functionp org-agenda-day-face-function)
3993 (funcall org-agenda-day-face-function date))
3994 (cond ((org-agenda-todayp date)
3995 'org-agenda-date-today)
3996 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3997 'org-agenda-date-weekend)
3998 (t 'org-agenda-date))))
4000 ;;; Agenda timeline
4002 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4003 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4005 (defun org-timeline (&optional dotodo)
4006 "Show a time-sorted view of the entries in the current org file.
4007 Only entries with a time stamp of today or later will be listed. With
4008 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4009 under the current date.
4010 If the buffer contains an active region, only check the region for
4011 dates."
4012 (interactive "P")
4013 (let* ((dopast t)
4014 (org-agenda-show-log-scoped org-agenda-show-log)
4015 (org-agenda-show-log org-agenda-show-log-scoped)
4016 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4017 (current-buffer))))
4018 (date (calendar-current-date))
4019 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4020 (end (if (org-region-active-p) (region-end) (point-max)))
4021 (day-numbers (org-get-all-dates
4022 beg end 'no-ranges
4023 t org-agenda-show-log-scoped ; always include today
4024 org-timeline-show-empty-dates))
4025 (org-deadline-warning-days 0)
4026 (org-agenda-only-exact-dates t)
4027 (today (org-today))
4028 (past t)
4029 args
4030 s e rtn d emptyp)
4031 (setq org-agenda-redo-command
4032 (list 'let
4033 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4034 (list 'org-switch-to-buffer-other-window (current-buffer))
4035 (list 'org-timeline (list 'quote dotodo))))
4036 (put 'org-agenda-redo-command 'org-lprops nil)
4037 (if (not dopast)
4038 ;; Remove past dates from the list of dates.
4039 (setq day-numbers (delq nil (mapcar (lambda(x)
4040 (if (>= x today) x nil))
4041 day-numbers))))
4042 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4043 (org-compile-prefix-format 'timeline)
4044 (org-set-sorting-strategy 'timeline)
4045 (if org-agenda-show-log-scoped (push :closed args))
4046 (push :timestamp args)
4047 (push :deadline args)
4048 (push :scheduled args)
4049 (push :sexp args)
4050 (if dotodo (push :todo args))
4051 (insert "Timeline of file " entry "\n")
4052 (add-text-properties (point-min) (point)
4053 (list 'face 'org-agenda-structure))
4054 (org-agenda-mark-header-line (point-min))
4055 (while (setq d (pop day-numbers))
4056 (if (and (listp d) (eq (car d) :omitted))
4057 (progn
4058 (setq s (point))
4059 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4060 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4061 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4062 (if (and (>= d today)
4063 dopast
4064 past)
4065 (progn
4066 (setq past nil)
4067 (insert (make-string 79 ?-) "\n")))
4068 (setq date (calendar-gregorian-from-absolute d))
4069 (setq s (point))
4070 (setq rtn (and (not emptyp)
4071 (apply 'org-agenda-get-day-entries entry
4072 date args)))
4073 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4074 (progn
4075 (insert
4076 (if (stringp org-agenda-format-date)
4077 (format-time-string org-agenda-format-date
4078 (org-time-from-absolute date))
4079 (funcall org-agenda-format-date date))
4080 "\n")
4081 (put-text-property s (1- (point)) 'face
4082 (org-agenda-get-day-face date))
4083 (put-text-property s (1- (point)) 'org-date-line t)
4084 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4085 (if (equal d today)
4086 (put-text-property s (1- (point)) 'org-today t))
4087 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4088 (put-text-property s (1- (point)) 'day d)))))
4089 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4090 (point-min)))
4091 (add-text-properties
4092 (point-min) (point-max)
4093 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4094 (org-agenda-finalize)
4095 (setq buffer-read-only t)))
4097 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4098 "Return a list of all relevant day numbers from BEG to END buffer positions.
4099 If NO-RANGES is non-nil, include only the start and end dates of a range,
4100 not every single day in the range. If FORCE-TODAY is non-nil, make
4101 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4102 inactive time stamps (those in square brackets) are included.
4103 When EMPTY is non-nil, also include days without any entries."
4104 (let ((re (concat
4105 (if pre-re pre-re "")
4106 (if inactive org-ts-regexp-both org-ts-regexp)))
4107 dates dates1 date day day1 day2 ts1 ts2 pos)
4108 (if force-today
4109 (setq dates (list (org-today))))
4110 (save-excursion
4111 (goto-char beg)
4112 (while (re-search-forward re end t)
4113 (setq day (time-to-days (org-time-string-to-time
4114 (substring (match-string 1) 0 10)
4115 (current-buffer) (match-beginning 0))))
4116 (or (memq day dates) (push day dates)))
4117 (unless no-ranges
4118 (goto-char beg)
4119 (while (re-search-forward org-tr-regexp end t)
4120 (setq pos (match-beginning 0))
4121 (setq ts1 (substring (match-string 1) 0 10)
4122 ts2 (substring (match-string 2) 0 10)
4123 day1 (time-to-days (org-time-string-to-time
4124 ts1 (current-buffer) pos))
4125 day2 (time-to-days (org-time-string-to-time
4126 ts2 (current-buffer) pos)))
4127 (while (< (setq day1 (1+ day1)) day2)
4128 (or (memq day1 dates) (push day1 dates)))))
4129 (setq dates (sort dates '<))
4130 (when empty
4131 (while (setq day (pop dates))
4132 (setq day2 (car dates))
4133 (push day dates1)
4134 (when (and day2 empty)
4135 (if (or (eq empty t)
4136 (and (numberp empty) (<= (- day2 day) empty)))
4137 (while (< (setq day (1+ day)) day2)
4138 (push (list day) dates1))
4139 (push (cons :omitted (- day2 day)) dates1))))
4140 (setq dates (nreverse dates1)))
4141 dates)))
4143 ;;; Agenda Daily/Weekly
4145 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4146 "Start day for the agenda view.
4147 Custom commands can set this variable in the options section.
4148 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4149 input allowed when reading a date through the Org calendar.
4150 See the docstring of `org-read-date' for details.")
4151 (defvar org-starting-day nil) ; local variable in the agenda buffer
4152 (defvar org-arg-loc nil) ; local variable
4154 (defvar org-agenda-buffer-tmp-name nil)
4155 ;;;###autoload
4156 (defun org-agenda-list (&optional arg start-day span with-hour)
4157 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4158 The view will be for the current day or week, but from the overview buffer
4159 you will be able to go to other days/weeks.
4161 With a numeric prefix argument in an interactive call, the agenda will
4162 span ARG days. Lisp programs should instead specify SPAN to change
4163 the number of days. SPAN defaults to `org-agenda-span'.
4165 START-DAY defaults to TODAY, or to the most recent match for the weekday
4166 given in `org-agenda-start-on-weekday'.
4168 When WITH-HOUR is non-nil, only include scheduled and deadline
4169 items if they have an hour specification like [h]h:mm."
4170 (interactive "P")
4171 (if org-agenda-overriding-arguments
4172 (setq arg (car org-agenda-overriding-arguments)
4173 start-day (nth 1 org-agenda-overriding-arguments)
4174 span (nth 2 org-agenda-overriding-arguments)))
4175 (if (and (integerp arg) (> arg 0))
4176 (setq span arg arg nil))
4177 (catch 'exit
4178 (setq org-agenda-buffer-name
4179 (or org-agenda-buffer-tmp-name
4180 (if org-agenda-sticky
4181 (cond ((and org-keys (stringp org-match))
4182 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4183 (org-keys
4184 (format "*Org Agenda(%s)*" org-keys))
4185 (t "*Org Agenda(a)*")))
4186 org-agenda-buffer-name))
4187 (org-agenda-prepare "Day/Week")
4188 (setq start-day (or start-day org-agenda-start-day))
4189 (if (stringp start-day)
4190 ;; Convert to an absolute day number
4191 (setq start-day (time-to-days (org-read-date nil t start-day))))
4192 (org-compile-prefix-format 'agenda)
4193 (org-set-sorting-strategy 'agenda)
4194 (let* ((span (org-agenda-ndays-to-span
4195 (or span org-agenda-ndays org-agenda-span)))
4196 (today (org-today))
4197 (sd (or start-day today))
4198 (ndays (org-agenda-span-to-ndays span sd))
4199 (org-agenda-start-on-weekday
4200 (if (or (eq ndays 7) (eq ndays 14))
4201 org-agenda-start-on-weekday))
4202 (thefiles (org-agenda-files nil 'ifmode))
4203 (files thefiles)
4204 (start (if (or (null org-agenda-start-on-weekday)
4205 (< ndays 7))
4207 (let* ((nt (calendar-day-of-week
4208 (calendar-gregorian-from-absolute sd)))
4209 (n1 org-agenda-start-on-weekday)
4210 (d (- nt n1)))
4211 (- sd (+ (if (< d 0) 7 0) d)))))
4212 (day-numbers (list start))
4213 (day-cnt 0)
4214 (inhibit-redisplay (not debug-on-error))
4215 (org-agenda-show-log-scoped org-agenda-show-log)
4216 s e rtn rtnall file date d start-pos end-pos todayp
4217 clocktable-start clocktable-end filter)
4218 (setq org-agenda-redo-command
4219 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4220 (dotimes (n (1- ndays))
4221 (push (1+ (car day-numbers)) day-numbers))
4222 (setq day-numbers (nreverse day-numbers))
4223 (setq clocktable-start (car day-numbers)
4224 clocktable-end (1+ (or (org-last day-numbers) 0)))
4225 (org-set-local 'org-starting-day (car day-numbers))
4226 (org-set-local 'org-arg-loc arg)
4227 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4228 (unless org-agenda-compact-blocks
4229 (let* ((d1 (car day-numbers))
4230 (d2 (org-last day-numbers))
4231 (w1 (org-days-to-iso-week d1))
4232 (w2 (org-days-to-iso-week d2)))
4233 (setq s (point))
4234 (if org-agenda-overriding-header
4235 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4236 nil 'face 'org-agenda-structure) "\n")
4237 (insert (org-agenda-span-name span)
4238 "-agenda"
4239 (if (< (- d2 d1) 350)
4240 (if (= w1 w2)
4241 (format " (W%02d)" w1)
4242 (format " (W%02d-W%02d)" w1 w2))
4244 ":\n")))
4245 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4246 'org-date-line t))
4247 (org-agenda-mark-header-line s))
4248 (while (setq d (pop day-numbers))
4249 (setq date (calendar-gregorian-from-absolute d)
4250 s (point))
4251 (if (or (setq todayp (= d today))
4252 (and (not start-pos) (= d sd)))
4253 (setq start-pos (point))
4254 (if (and start-pos (not end-pos))
4255 (setq end-pos (point))))
4256 (setq files thefiles
4257 rtnall nil)
4258 (while (setq file (pop files))
4259 (catch 'nextfile
4260 (org-check-agenda-file file)
4261 (let ((org-agenda-entry-types org-agenda-entry-types))
4262 ;; Starred types override non-starred equivalents
4263 (when (member :deadline* org-agenda-entry-types)
4264 (setq org-agenda-entry-types
4265 (delq :deadline org-agenda-entry-types)))
4266 (when (member :scheduled* org-agenda-entry-types)
4267 (setq org-agenda-entry-types
4268 (delq :scheduled org-agenda-entry-types)))
4269 ;; Honor with-hour
4270 (when with-hour
4271 (when (member :deadline org-agenda-entry-types)
4272 (setq org-agenda-entry-types
4273 (delq :deadline org-agenda-entry-types))
4274 (push :deadline* org-agenda-entry-types))
4275 (when (member :scheduled org-agenda-entry-types)
4276 (setq org-agenda-entry-types
4277 (delq :scheduled org-agenda-entry-types))
4278 (push :scheduled* org-agenda-entry-types)))
4279 (unless org-agenda-include-deadlines
4280 (setq org-agenda-entry-types
4281 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4282 (cond
4283 ((memq org-agenda-show-log-scoped '(only clockcheck))
4284 (setq rtn (org-agenda-get-day-entries
4285 file date :closed)))
4286 (org-agenda-show-log-scoped
4287 (setq rtn (apply 'org-agenda-get-day-entries
4288 file date
4289 (append '(:closed) org-agenda-entry-types))))
4291 (setq rtn (apply 'org-agenda-get-day-entries
4292 file date
4293 org-agenda-entry-types)))))
4294 (setq rtnall (append rtnall rtn)))) ;; all entries
4295 (if org-agenda-include-diary
4296 (let ((org-agenda-search-headline-for-time t))
4297 (require 'diary-lib)
4298 (setq rtn (org-get-entries-from-diary date))
4299 (setq rtnall (append rtnall rtn))))
4300 (if (or rtnall org-agenda-show-all-dates)
4301 (progn
4302 (setq day-cnt (1+ day-cnt))
4303 (insert
4304 (if (stringp org-agenda-format-date)
4305 (format-time-string org-agenda-format-date
4306 (org-time-from-absolute date))
4307 (funcall org-agenda-format-date date))
4308 "\n")
4309 (put-text-property s (1- (point)) 'face
4310 (org-agenda-get-day-face date))
4311 (put-text-property s (1- (point)) 'org-date-line t)
4312 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4313 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4314 (when todayp
4315 (put-text-property s (1- (point)) 'org-today t))
4316 (setq rtnall
4317 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4318 (if rtnall (insert ;; all entries
4319 (org-agenda-finalize-entries rtnall 'agenda)
4320 "\n"))
4321 (put-text-property s (1- (point)) 'day d)
4322 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4323 (when (and org-agenda-clockreport-mode clocktable-start)
4324 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4325 ;; the above line is to ensure the restricted range!
4326 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4327 tbl)
4328 (setq p (org-plist-delete p :block))
4329 (setq p (plist-put p :tstart clocktable-start))
4330 (setq p (plist-put p :tend clocktable-end))
4331 (setq p (plist-put p :scope 'agenda))
4332 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4333 (setq filter (or org-agenda-tag-filter-while-redo
4334 (get 'org-agenda-tag-filter :preset-filter))))
4335 (setq p (plist-put p :tags (mapconcat (lambda (x)
4336 (if (string-match "[<>=]" x)
4339 filter ""))))
4340 (setq tbl (apply 'org-clock-get-clocktable p))
4341 (insert tbl)))
4342 (goto-char (point-min))
4343 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4344 (unless (and (pos-visible-in-window-p (point-min))
4345 (pos-visible-in-window-p (point-max)))
4346 (goto-char (1- (point-max)))
4347 (recenter -1)
4348 (if (not (pos-visible-in-window-p (or start-pos 1)))
4349 (progn
4350 (goto-char (or start-pos 1))
4351 (recenter 1))))
4352 (goto-char (or start-pos 1))
4353 (add-text-properties (point-min) (point-max)
4354 `(org-agenda-type agenda
4355 org-last-args (,arg ,start-day ,span)
4356 org-redo-cmd ,org-agenda-redo-command
4357 org-series-cmd ,org-cmd))
4358 (if (eq org-agenda-show-log-scoped 'clockcheck)
4359 (org-agenda-show-clocking-issues))
4360 (org-agenda-finalize)
4361 (setq buffer-read-only t)
4362 (message ""))))
4364 (defun org-agenda-ndays-to-span (n)
4365 "Return a span symbol for a span of N days, or N if none matches."
4366 (cond ((symbolp n) n)
4367 ((= n 1) 'day)
4368 ((= n 7) 'week)
4369 ((= n 14) 'fortnight)
4370 (t n)))
4372 (defun org-agenda-span-to-ndays (span &optional start-day)
4373 "Return ndays from SPAN, possibly starting at START-DAY.
4374 START-DAY is an absolute time value."
4375 (cond ((numberp span) span)
4376 ((eq span 'day) 1)
4377 ((eq span 'week) 7)
4378 ((eq span 'fortnight) 14)
4379 ((eq span 'month)
4380 (let ((date (calendar-gregorian-from-absolute start-day)))
4381 (calendar-last-day-of-month (car date) (caddr date))))
4382 ((eq span 'year)
4383 (let ((date (calendar-gregorian-from-absolute start-day)))
4384 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4386 (defun org-agenda-span-name (span)
4387 "Return a SPAN name."
4388 (if (null span)
4390 (if (symbolp span)
4391 (capitalize (symbol-name span))
4392 (format "%d days" span))))
4394 ;;; Agenda word search
4396 (defvar org-agenda-search-history nil)
4398 (defvar org-search-syntax-table nil
4399 "Special syntax table for org-mode search.
4400 In this table, we have single quotes not as word constituents, to
4401 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4403 (defvar org-mode-syntax-table) ; From org.el
4404 (defun org-search-syntax-table ()
4405 (unless org-search-syntax-table
4406 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4407 (modify-syntax-entry ?' "." org-search-syntax-table)
4408 (modify-syntax-entry ?` "." org-search-syntax-table))
4409 org-search-syntax-table)
4411 (defvar org-agenda-last-search-view-search-was-boolean nil)
4413 ;;;###autoload
4414 (defun org-search-view (&optional todo-only string edit-at)
4415 "Show all entries that contain a phrase or words or regular expressions.
4417 With optional prefix argument TODO-ONLY, only consider entries that are
4418 TODO entries. The argument STRING can be used to pass a default search
4419 string into this function. If EDIT-AT is non-nil, it means that the
4420 user should get a chance to edit this string, with cursor at position
4421 EDIT-AT.
4423 The search string can be viewed either as a phrase that should be found as
4424 is, or it can be broken into a number of snippets, each of which must match
4425 in a Boolean way to select an entry. The default depends on the variable
4426 `org-agenda-search-view-always-boolean'.
4427 Even if this is turned off (the default) you can always switch to
4428 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4430 The default is a direct search of the whole phrase, where each space in
4431 the search string can expand to an arbitrary amount of whitespace,
4432 including newlines.
4434 If using a Boolean search, the search string is split on whitespace and
4435 each snippet is searched separately, with logical AND to select an entry.
4436 Words prefixed with a minus must *not* occur in the entry. Words without
4437 a prefix or prefixed with a plus must occur in the entry. Matching is
4438 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4439 match whole words, not parts of a word) if
4440 `org-agenda-search-view-force-full-words' is set (default is nil).
4442 Boolean search snippets enclosed by curly braces are interpreted as
4443 regular expressions that must or (when preceded with \"-\") must not
4444 match in the entry. Snippets enclosed into double quotes will be taken
4445 as a whole, to include whitespace.
4447 - If the search string starts with an asterisk, search only in headlines.
4448 - If (possibly after the leading star) the search string starts with an
4449 exclamation mark, this also means to look at TODO entries only, an effect
4450 that can also be achieved with a prefix argument.
4451 - If (possibly after star and exclamation mark) the search string starts
4452 with a colon, this will mean that the (non-regexp) snippets of the
4453 Boolean search must match as full words.
4455 This command searches the agenda files, and in addition the files listed
4456 in `org-agenda-text-search-extra-files'."
4457 (interactive "P")
4458 (if org-agenda-overriding-arguments
4459 (setq todo-only (car org-agenda-overriding-arguments)
4460 string (nth 1 org-agenda-overriding-arguments)
4461 edit-at (nth 2 org-agenda-overriding-arguments)))
4462 (let* ((props (list 'face nil
4463 'done-face 'org-agenda-done
4464 'org-not-done-regexp org-not-done-regexp
4465 'org-todo-regexp org-todo-regexp
4466 'org-complex-heading-regexp org-complex-heading-regexp
4467 'mouse-face 'highlight
4468 'help-echo (format "mouse-2 or RET jump to location")))
4469 (full-words org-agenda-search-view-force-full-words)
4470 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4471 regexp rtn rtnall files file pos inherited-tags
4472 marker category level tags c neg re boolean
4473 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4474 (unless (and (not edit-at)
4475 (stringp string)
4476 (string-match "\\S-" string))
4477 (setq string (read-string
4478 (if org-agenda-search-view-always-boolean
4479 "[+-]Word/{Regexp} ...: "
4480 "Phrase or [+-]Word/{Regexp} ...: ")
4481 (cond
4482 ((integerp edit-at) (cons string edit-at))
4483 (edit-at string))
4484 'org-agenda-search-history)))
4485 (catch 'exit
4486 (if org-agenda-sticky
4487 (setq org-agenda-buffer-name
4488 (if (stringp string)
4489 (format "*Org Agenda(%s:%s)*"
4490 (or org-keys (or (and todo-only "S") "s")) string)
4491 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4492 (org-agenda-prepare "SEARCH")
4493 (org-compile-prefix-format 'search)
4494 (org-set-sorting-strategy 'search)
4495 (setq org-agenda-redo-command
4496 (list 'org-search-view (if todo-only t nil)
4497 (list 'if 'current-prefix-arg nil string)))
4498 (setq org-agenda-query-string string)
4499 (if (equal (string-to-char string) ?*)
4500 (setq hdl-only t
4501 words (substring string 1))
4502 (setq words string))
4503 (when (equal (string-to-char words) ?!)
4504 (setq todo-only t
4505 words (substring words 1)))
4506 (when (equal (string-to-char words) ?:)
4507 (setq full-words t
4508 words (substring words 1)))
4509 (if (or org-agenda-search-view-always-boolean
4510 (member (string-to-char words) '(?- ?+ ?\{)))
4511 (setq boolean t))
4512 (setq words (org-split-string words))
4513 (let (www w)
4514 (while (setq w (pop words))
4515 (while (and (string-match "\\\\\\'" w) words)
4516 (setq w (concat (substring w 0 -1) " " (pop words))))
4517 (push w www))
4518 (setq words (nreverse www) www nil)
4519 (while (setq w (pop words))
4520 (when (and (string-match "\\`[-+]?{" w)
4521 (not (string-match "}\\'" w)))
4522 (while (and words (not (string-match "}\\'" (car words))))
4523 (setq w (concat w " " (pop words))))
4524 (setq w (concat w " " (pop words))))
4525 (push w www))
4526 (setq words (nreverse www)))
4527 (setq org-agenda-last-search-view-search-was-boolean boolean)
4528 (when boolean
4529 (let (wds w)
4530 (while (setq w (pop words))
4531 (if (or (equal (substring w 0 1) "\"")
4532 (and (> (length w) 1)
4533 (member (substring w 0 1) '("+" "-"))
4534 (equal (substring w 1 2) "\"")))
4535 (while (and words (not (equal (substring w -1) "\"")))
4536 (setq w (concat w " " (pop words)))))
4537 (and (string-match "\\`\\([-+]?\\)\"" w)
4538 (setq w (replace-match "\\1" nil nil w)))
4539 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4540 (push w wds))
4541 (setq words (nreverse wds))))
4542 (if boolean
4543 (mapc (lambda (w)
4544 (setq c (string-to-char w))
4545 (if (equal c ?-)
4546 (setq neg t w (substring w 1))
4547 (if (equal c ?+)
4548 (setq neg nil w (substring w 1))
4549 (setq neg nil)))
4550 (if (string-match "\\`{.*}\\'" w)
4551 (setq re (substring w 1 -1))
4552 (if full-words
4553 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4554 (setq re (regexp-quote (downcase w)))))
4555 (if neg (push re regexps-) (push re regexps+)))
4556 words)
4557 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4558 regexps+))
4559 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4560 (if (not regexps+)
4561 (setq regexp org-outline-regexp-bol)
4562 (setq regexp (pop regexps+))
4563 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4564 regexp))))
4565 (setq files (org-agenda-files nil 'ifmode))
4566 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4567 (pop org-agenda-text-search-extra-files)
4568 (setq files (org-add-archive-files files)))
4569 (setq files (append files org-agenda-text-search-extra-files)
4570 rtnall nil)
4571 (while (setq file (pop files))
4572 (setq ee nil)
4573 (catch 'nextfile
4574 (org-check-agenda-file file)
4575 (setq buffer (if (file-exists-p file)
4576 (org-get-agenda-file-buffer file)
4577 (error "No such file %s" file)))
4578 (if (not buffer)
4579 ;; If file does not exist, make sure an error message is sent
4580 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4581 file))))
4582 (with-current-buffer buffer
4583 (with-syntax-table (org-search-syntax-table)
4584 (unless (derived-mode-p 'org-mode)
4585 (error "Agenda file %s is not in `org-mode'" file))
4586 (let ((case-fold-search t))
4587 (save-excursion
4588 (save-restriction
4589 (if (eq buffer org-agenda-restrict)
4590 (narrow-to-region org-agenda-restrict-begin
4591 org-agenda-restrict-end)
4592 (widen))
4593 (goto-char (point-min))
4594 (unless (or (org-at-heading-p)
4595 (outline-next-heading))
4596 (throw 'nextfile t))
4597 (goto-char (max (point-min) (1- (point))))
4598 (while (re-search-forward regexp nil t)
4599 (org-back-to-heading t)
4600 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4601 (> (org-reduced-level (org-outline-level))
4602 org-agenda-search-view-max-outline-level)
4603 (forward-line -1)
4604 (outline-back-to-heading t)))
4605 (skip-chars-forward "* ")
4606 (setq beg (point-at-bol)
4607 beg1 (point)
4608 end (progn
4609 (outline-next-heading)
4610 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4611 (> (org-reduced-level (org-outline-level))
4612 org-agenda-search-view-max-outline-level)
4613 (forward-line 1)
4614 (outline-next-heading)))
4615 (point)))
4617 (catch :skip
4618 (goto-char beg)
4619 (org-agenda-skip)
4620 (setq str (buffer-substring-no-properties
4621 (point-at-bol)
4622 (if hdl-only (point-at-eol) end)))
4623 (mapc (lambda (wr) (when (string-match wr str)
4624 (goto-char (1- end))
4625 (throw :skip t)))
4626 regexps-)
4627 (mapc (lambda (wr) (unless (string-match wr str)
4628 (goto-char (1- end))
4629 (throw :skip t)))
4630 (if todo-only
4631 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4632 regexps+)
4633 regexps+))
4634 (goto-char beg)
4635 (setq marker (org-agenda-new-marker (point))
4636 category (org-get-category)
4637 level (make-string (org-reduced-level (org-outline-level)) ? )
4638 inherited-tags
4639 (or (eq org-agenda-show-inherited-tags 'always)
4640 (and (listp org-agenda-show-inherited-tags)
4641 (memq 'todo org-agenda-show-inherited-tags))
4642 (and (eq org-agenda-show-inherited-tags t)
4643 (or (eq org-agenda-use-tag-inheritance t)
4644 (memq 'todo org-agenda-use-tag-inheritance))))
4645 tags (org-get-tags-at nil (not inherited-tags))
4646 txt (org-agenda-format-item
4648 (buffer-substring-no-properties
4649 beg1 (point-at-eol))
4650 level category tags t))
4651 (org-add-props txt props
4652 'org-marker marker 'org-hd-marker marker
4653 'org-todo-regexp org-todo-regexp
4654 'level level
4655 'org-complex-heading-regexp org-complex-heading-regexp
4656 'priority 1000
4657 'type "search")
4658 (push txt ee)
4659 (goto-char (1- end))))))))))
4660 (setq rtn (nreverse ee))
4661 (setq rtnall (append rtnall rtn)))
4662 (if org-agenda-overriding-header
4663 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4664 nil 'face 'org-agenda-structure) "\n")
4665 (insert "Search words: ")
4666 (add-text-properties (point-min) (1- (point))
4667 (list 'face 'org-agenda-structure))
4668 (setq pos (point))
4669 (insert string "\n")
4670 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4671 (setq pos (point))
4672 (unless org-agenda-multi
4673 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4674 (add-text-properties pos (1- (point))
4675 (list 'face 'org-agenda-structure))))
4676 (org-agenda-mark-header-line (point-min))
4677 (when rtnall
4678 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4679 (goto-char (point-min))
4680 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4681 (add-text-properties (point-min) (point-max)
4682 `(org-agenda-type search
4683 org-last-args (,todo-only ,string ,edit-at)
4684 org-redo-cmd ,org-agenda-redo-command
4685 org-series-cmd ,org-cmd))
4686 (org-agenda-finalize)
4687 (setq buffer-read-only t))))
4689 ;;; Agenda TODO list
4691 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4692 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4693 (concat
4694 (if (or (equal keywords "ALL") (not keywords))
4695 (propertize "ALL" 'face 'warning)
4696 (mapconcat
4697 (lambda (kw)
4698 (propertize kw 'face (org-get-todo-face kw)))
4699 (org-split-string keywords "|")
4700 "|"))
4701 "\n"))
4703 (defvar org-select-this-todo-keyword nil)
4704 (defvar org-last-arg nil)
4706 ;;;###autoload
4707 (defun org-todo-list (&optional arg)
4708 "Show all (not done) TODO entries from all agenda file in a single list.
4709 The prefix arg can be used to select a specific TODO keyword and limit
4710 the list to these. When using \\[universal-argument], you will be prompted
4711 for a keyword. A numeric prefix directly selects the Nth keyword in
4712 `org-todo-keywords-1'."
4713 (interactive "P")
4714 (if org-agenda-overriding-arguments
4715 (setq arg org-agenda-overriding-arguments))
4716 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4717 (let* ((today (org-today))
4718 (date (calendar-gregorian-from-absolute today))
4719 (kwds org-todo-keywords-for-agenda)
4720 (completion-ignore-case t)
4721 (org-select-this-todo-keyword
4722 (if (stringp arg) arg
4723 (and arg (integerp arg) (> arg 0)
4724 (nth (1- arg) kwds))))
4725 rtn rtnall files file pos)
4726 (when (equal arg '(4))
4727 (setq org-select-this-todo-keyword
4728 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4729 (mapcar 'list kwds) nil nil)))
4730 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4731 (catch 'exit
4732 (if org-agenda-sticky
4733 (setq org-agenda-buffer-name
4734 (if (stringp org-select-this-todo-keyword)
4735 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4736 org-select-this-todo-keyword)
4737 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4738 (org-agenda-prepare "TODO")
4739 (org-compile-prefix-format 'todo)
4740 (org-set-sorting-strategy 'todo)
4741 (setq org-agenda-redo-command
4742 `(org-todo-list (or (and (numberp current-prefix-arg)
4743 current-prefix-arg)
4744 ,org-select-this-todo-keyword
4745 current-prefix-arg ,arg)))
4746 (setq files (org-agenda-files nil 'ifmode)
4747 rtnall nil)
4748 (while (setq file (pop files))
4749 (catch 'nextfile
4750 (org-check-agenda-file file)
4751 (setq rtn (org-agenda-get-day-entries file date :todo))
4752 (setq rtnall (append rtnall rtn))))
4753 (if org-agenda-overriding-header
4754 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4755 nil 'face 'org-agenda-structure) "\n")
4756 (insert "Global list of TODO items of type: ")
4757 (add-text-properties (point-min) (1- (point))
4758 (list 'face 'org-agenda-structure
4759 'short-heading
4760 (concat "ToDo: "
4761 (or org-select-this-todo-keyword "ALL"))))
4762 (org-agenda-mark-header-line (point-min))
4763 (insert (org-agenda-propertize-selected-todo-keywords
4764 org-select-this-todo-keyword))
4765 (setq pos (point))
4766 (unless org-agenda-multi
4767 (insert "Available with `N r': (0)[ALL]")
4768 (let ((n 0) s)
4769 (mapc (lambda (x)
4770 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4771 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4772 (insert "\n "))
4773 (insert " " s))
4774 kwds))
4775 (insert "\n"))
4776 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4777 (org-agenda-mark-header-line (point-min))
4778 (when rtnall
4779 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4780 (goto-char (point-min))
4781 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4782 (add-text-properties (point-min) (point-max)
4783 `(org-agenda-type todo
4784 org-last-args ,arg
4785 org-redo-cmd ,org-agenda-redo-command
4786 org-series-cmd ,org-cmd))
4787 (org-agenda-finalize)
4788 (setq buffer-read-only t))))
4790 ;;; Agenda tags match
4792 ;;;###autoload
4793 (defun org-tags-view (&optional todo-only match)
4794 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4795 The prefix arg TODO-ONLY limits the search to TODO entries."
4796 (interactive "P")
4797 (if org-agenda-overriding-arguments
4798 (setq todo-only (car org-agenda-overriding-arguments)
4799 match (nth 1 org-agenda-overriding-arguments)))
4800 (let* ((org-tags-match-list-sublevels
4801 org-tags-match-list-sublevels)
4802 (completion-ignore-case t)
4803 rtn rtnall files file pos matcher
4804 buffer)
4805 (when (and (stringp match) (not (string-match "\\S-" match)))
4806 (setq match nil))
4807 (catch 'exit
4808 (if org-agenda-sticky
4809 (setq org-agenda-buffer-name
4810 (if (stringp match)
4811 (format "*Org Agenda(%s:%s)*"
4812 (or org-keys (or (and todo-only "M") "m")) match)
4813 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4814 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4815 ;; expanding tags within `org-make-tags-matcher'
4816 (org-agenda-prepare (concat "TAGS " match))
4817 (setq matcher (org-make-tags-matcher match)
4818 match (car matcher) matcher (cdr matcher))
4819 (org-compile-prefix-format 'tags)
4820 (org-set-sorting-strategy 'tags)
4821 (setq org-agenda-query-string match)
4822 (setq org-agenda-redo-command
4823 (list 'org-tags-view `(quote ,todo-only)
4824 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4825 (setq files (org-agenda-files nil 'ifmode)
4826 rtnall nil)
4827 (while (setq file (pop files))
4828 (catch 'nextfile
4829 (org-check-agenda-file file)
4830 (setq buffer (if (file-exists-p file)
4831 (org-get-agenda-file-buffer file)
4832 (error "No such file %s" file)))
4833 (if (not buffer)
4834 ;; If file does not exist, error message to agenda
4835 (setq rtn (list
4836 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4837 rtnall (append rtnall rtn))
4838 (with-current-buffer buffer
4839 (unless (derived-mode-p 'org-mode)
4840 (error "Agenda file %s is not in `org-mode'" file))
4841 (save-excursion
4842 (save-restriction
4843 (if (eq buffer org-agenda-restrict)
4844 (narrow-to-region org-agenda-restrict-begin
4845 org-agenda-restrict-end)
4846 (widen))
4847 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4848 (setq rtnall (append rtnall rtn))))))))
4849 (if org-agenda-overriding-header
4850 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4851 nil 'face 'org-agenda-structure) "\n")
4852 (insert "Headlines with TAGS match: ")
4853 (add-text-properties (point-min) (1- (point))
4854 (list 'face 'org-agenda-structure
4855 'short-heading
4856 (concat "Match: " match)))
4857 (setq pos (point))
4858 (insert match "\n")
4859 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4860 (setq pos (point))
4861 (unless org-agenda-multi
4862 (insert "Press `C-u r' to search again with new search string\n"))
4863 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4864 (org-agenda-mark-header-line (point-min))
4865 (when rtnall
4866 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4867 (goto-char (point-min))
4868 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4869 (add-text-properties (point-min) (point-max)
4870 `(org-agenda-type tags
4871 org-last-args (,todo-only ,match)
4872 org-redo-cmd ,org-agenda-redo-command
4873 org-series-cmd ,org-cmd))
4874 (org-agenda-finalize)
4875 (setq buffer-read-only t))))
4877 ;;; Agenda Finding stuck projects
4879 (defvar org-agenda-skip-regexp nil
4880 "Regular expression used in skipping subtrees for the agenda.
4881 This is basically a temporary global variable that can be set and then
4882 used by user-defined selections using `org-agenda-skip-function'.")
4884 (defvar org-agenda-overriding-header nil
4885 "When set during agenda, todo and tags searches it replaces the header.
4886 This variable should not be set directly, but custom commands can bind it
4887 in the options section.")
4889 (defun org-agenda-skip-entry-when-regexp-matches ()
4890 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4891 If yes, it returns the end position of this entry, causing agenda commands
4892 to skip the entry but continuing the search in the subtree. This is a
4893 function that can be put into `org-agenda-skip-function' for the duration
4894 of a command."
4895 (let ((end (save-excursion (org-end-of-subtree t)))
4896 skip)
4897 (save-excursion
4898 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4899 (and skip end)))
4901 (defun org-agenda-skip-subtree-when-regexp-matches ()
4902 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4903 If yes, it returns the end position of this tree, causing agenda commands
4904 to skip this subtree. This is a function that can be put into
4905 `org-agenda-skip-function' for the duration of a command."
4906 (let ((end (save-excursion (org-end-of-subtree t)))
4907 skip)
4908 (save-excursion
4909 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4910 (and skip end)))
4912 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4913 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4914 If yes, it returns the end position of the current entry (NOT the tree),
4915 causing agenda commands to skip the entry but continuing the search in
4916 the subtree. This is a function that can be put into
4917 `org-agenda-skip-function' for the duration of a command. An important
4918 use of this function is for the stuck project list."
4919 (let ((end (save-excursion (org-end-of-subtree t)))
4920 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4921 skip)
4922 (save-excursion
4923 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4924 (and skip entry-end)))
4926 (defun org-agenda-skip-entry-if (&rest conditions)
4927 "Skip entry if any of CONDITIONS is true.
4928 See `org-agenda-skip-if' for details."
4929 (org-agenda-skip-if nil conditions))
4931 (defun org-agenda-skip-subtree-if (&rest conditions)
4932 "Skip subtree if any of CONDITIONS is true.
4933 See `org-agenda-skip-if' for details."
4934 (org-agenda-skip-if t conditions))
4936 (defun org-agenda-skip-if (subtree conditions)
4937 "Checks current entity for CONDITIONS.
4938 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4939 the entry (i.e. the text before the next heading) is checked.
4941 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4942 from different tests. Valid conditions are:
4944 scheduled Check if there is a scheduled cookie
4945 notscheduled Check if there is no scheduled cookie
4946 deadline Check if there is a deadline
4947 notdeadline Check if there is no deadline
4948 timestamp Check if there is a timestamp (also deadline or scheduled)
4949 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4950 regexp Check if regexp matches
4951 notregexp Check if regexp does not match.
4952 todo Check if TODO keyword matches
4953 nottodo Check if TODO keyword does not match
4955 The regexp is taken from the conditions list, it must come right after
4956 the `regexp' or `notregexp' element.
4958 `todo' and `nottodo' accept as an argument a list of todo
4959 keywords, which may include \"*\" to match any todo keyword.
4961 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4963 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4965 Instead of a list, a keyword class may be given. For example:
4967 (org-agenda-skip-entry-if 'nottodo 'done)
4969 would skip entries that haven't been marked with any of \"DONE\"
4970 keywords. Possible classes are: `todo', `done', `any'.
4972 If any of these conditions is met, this function returns the end point of
4973 the entity, causing the search to continue from there. This is a function
4974 that can be put into `org-agenda-skip-function' for the duration of a command."
4975 (let (beg end m)
4976 (org-back-to-heading t)
4977 (setq beg (point)
4978 end (if subtree
4979 (progn (org-end-of-subtree t) (point))
4980 (progn (outline-next-heading) (1- (point)))))
4981 (goto-char beg)
4982 (and
4984 (and (memq 'scheduled conditions)
4985 (re-search-forward org-scheduled-time-regexp end t))
4986 (and (memq 'notscheduled conditions)
4987 (not (re-search-forward org-scheduled-time-regexp end t)))
4988 (and (memq 'deadline conditions)
4989 (re-search-forward org-deadline-time-regexp end t))
4990 (and (memq 'notdeadline conditions)
4991 (not (re-search-forward org-deadline-time-regexp end t)))
4992 (and (memq 'timestamp conditions)
4993 (re-search-forward org-ts-regexp end t))
4994 (and (memq 'nottimestamp conditions)
4995 (not (re-search-forward org-ts-regexp end t)))
4996 (and (setq m (memq 'regexp conditions))
4997 (stringp (nth 1 m))
4998 (re-search-forward (nth 1 m) end t))
4999 (and (setq m (memq 'notregexp conditions))
5000 (stringp (nth 1 m))
5001 (not (re-search-forward (nth 1 m) end t)))
5002 (and (or
5003 (setq m (memq 'nottodo conditions))
5004 (setq m (memq 'todo-unblocked conditions))
5005 (setq m (memq 'nottodo-unblocked conditions))
5006 (setq m (memq 'todo conditions)))
5007 (org-agenda-skip-if-todo m end)))
5008 end)))
5010 (defun org-agenda-skip-if-todo (args end)
5011 "Helper function for `org-agenda-skip-if', do not use it directly.
5012 ARGS is a list with first element either `todo', `nottodo',
5013 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5014 a list of TODO keywords, or a state symbol `todo' or `done' or
5015 `any'."
5016 (let ((kw (car args))
5017 (arg (cadr args))
5018 todo-wds todo-re)
5019 (setq todo-wds
5020 (org-uniquify
5021 (cond
5022 ((listp arg) ;; list of keywords
5023 (if (member "*" arg)
5024 (mapcar 'substring-no-properties org-todo-keywords-1)
5025 arg))
5026 ((symbolp arg) ;; keyword class name
5027 (cond
5028 ((eq arg 'todo)
5029 (org-delete-all org-done-keywords
5030 (mapcar 'substring-no-properties
5031 org-todo-keywords-1)))
5032 ((eq arg 'done) org-done-keywords)
5033 ((eq arg 'any)
5034 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5035 (setq todo-re
5036 (concat "^\\*+[ \t]+\\<\\("
5037 (mapconcat 'identity todo-wds "\\|")
5038 "\\)\\>"))
5039 (cond
5040 ((eq kw 'todo) (re-search-forward todo-re end t))
5041 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5042 ((eq kw 'todo-unblocked)
5043 (catch 'unblocked
5044 (while (re-search-forward todo-re end t)
5045 (or (org-entry-blocked-p) (throw 'unblocked t)))
5046 nil))
5047 ((eq kw 'nottodo-unblocked)
5048 (catch 'unblocked
5049 (while (re-search-forward todo-re end t)
5050 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5054 ;;;###autoload
5055 (defun org-agenda-list-stuck-projects (&rest ignore)
5056 "Create agenda view for projects that are stuck.
5057 Stuck projects are project that have no next actions. For the definitions
5058 of what a project is and how to check if it stuck, customize the variable
5059 `org-stuck-projects'."
5060 (interactive)
5061 (let* ((org-agenda-skip-function
5062 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5063 ;; We could have used org-agenda-skip-if here.
5064 (org-agenda-overriding-header
5065 (or org-agenda-overriding-header "List of stuck projects: "))
5066 (matcher (nth 0 org-stuck-projects))
5067 (todo (nth 1 org-stuck-projects))
5068 (todo-wds (if (member "*" todo)
5069 (progn
5070 (org-agenda-prepare-buffers (org-agenda-files
5071 nil 'ifmode))
5072 (org-delete-all
5073 org-done-keywords-for-agenda
5074 (copy-sequence org-todo-keywords-for-agenda)))
5075 todo))
5076 (todo-re (concat "^\\*+[ \t]+\\("
5077 (mapconcat 'identity todo-wds "\\|")
5078 "\\)\\>"))
5079 (tags (nth 2 org-stuck-projects))
5080 (tags-re (if (member "*" tags)
5081 (concat org-outline-regexp-bol
5082 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5083 (if tags
5084 (concat org-outline-regexp-bol
5085 ".*:\\("
5086 (mapconcat 'identity tags "\\|")
5087 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5088 (gen-re (nth 3 org-stuck-projects))
5089 (re-list
5090 (delq nil
5091 (list
5092 (if todo todo-re)
5093 (if tags tags-re)
5094 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5095 gen-re)))))
5096 (setq org-agenda-skip-regexp
5097 (if re-list
5098 (mapconcat 'identity re-list "\\|")
5099 (error "No information how to identify unstuck projects")))
5100 (org-tags-view nil matcher)
5101 (setq org-agenda-buffer-name (buffer-name))
5102 (with-current-buffer org-agenda-buffer-name
5103 (setq org-agenda-redo-command
5104 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5106 ;;; Diary integration
5108 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5109 (defvar diary-list-entries-hook)
5110 (defvar diary-time-regexp)
5111 (defun org-get-entries-from-diary (date)
5112 "Get the (Emacs Calendar) diary entries for DATE."
5113 (require 'diary-lib)
5114 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5115 (diary-display-hook '(fancy-diary-display))
5116 (diary-display-function 'fancy-diary-display)
5117 (pop-up-frames nil)
5118 (diary-list-entries-hook
5119 (cons 'org-diary-default-entry diary-list-entries-hook))
5120 (diary-file-name-prefix nil) ; turn this feature off
5121 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5122 entries
5123 (org-disable-agenda-to-diary t))
5124 (save-excursion
5125 (save-window-excursion
5126 (funcall (if (fboundp 'diary-list-entries)
5127 'diary-list-entries 'list-diary-entries)
5128 date 1)))
5129 (if (not (get-buffer diary-fancy-buffer))
5130 (setq entries nil)
5131 (with-current-buffer diary-fancy-buffer
5132 (setq buffer-read-only nil)
5133 (if (zerop (buffer-size))
5134 ;; No entries
5135 (setq entries nil)
5136 ;; Omit the date and other unnecessary stuff
5137 (org-agenda-cleanup-fancy-diary)
5138 ;; Add prefix to each line and extend the text properties
5139 (if (zerop (buffer-size))
5140 (setq entries nil)
5141 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5142 (setq entries
5143 (with-temp-buffer
5144 (insert entries) (goto-char (point-min))
5145 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5146 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5147 (replace-match (concat "; " (match-string 1)))))
5148 (buffer-string)))))
5149 (set-buffer-modified-p nil)
5150 (kill-buffer diary-fancy-buffer)))
5151 (when entries
5152 (setq entries (org-split-string entries "\n"))
5153 (setq entries
5154 (mapcar
5155 (lambda (x)
5156 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5157 ;; Extend the text properties to the beginning of the line
5158 (org-add-props x (text-properties-at (1- (length x)) x)
5159 'type "diary" 'date date 'face 'org-agenda-diary))
5160 entries)))))
5162 (defvar org-agenda-cleanup-fancy-diary-hook nil
5163 "Hook run when the fancy diary buffer is cleaned up.")
5165 (defun org-agenda-cleanup-fancy-diary ()
5166 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5167 This gets rid of the date, the underline under the date, and
5168 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5169 date. It also removes lines that contain only whitespace."
5170 (goto-char (point-min))
5171 (if (looking-at ".*?:[ \t]*")
5172 (progn
5173 (replace-match "")
5174 (re-search-forward "\n=+$" nil t)
5175 (replace-match "")
5176 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5177 (re-search-forward "\n=+$" nil t)
5178 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5179 (goto-char (point-min))
5180 (while (re-search-forward "^ +\n" nil t)
5181 (replace-match ""))
5182 (goto-char (point-min))
5183 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5184 (replace-match ""))
5185 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5187 ;; Make sure entries from the diary have the right text properties.
5188 (eval-after-load "diary-lib"
5189 '(if (boundp 'diary-modify-entry-list-string-function)
5190 ;; We can rely on the hook, nothing to do
5192 ;; Hook not available, must use advice to make this work
5193 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5194 "Make the position visible."
5195 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5196 (stringp string)
5197 buffer-file-name)
5198 (setq string (org-modify-diary-entry-string string))))))
5200 (defun org-modify-diary-entry-string (string)
5201 "Add text properties to string, allowing org-mode to act on it."
5202 (org-add-props string nil
5203 'mouse-face 'highlight
5204 'help-echo (if buffer-file-name
5205 (format "mouse-2 or RET jump to diary file %s"
5206 (abbreviate-file-name buffer-file-name))
5208 'org-agenda-diary-link t
5209 'org-marker (org-agenda-new-marker (point-at-bol))))
5211 (defun org-diary-default-entry ()
5212 "Add a dummy entry to the diary.
5213 Needed to avoid empty dates which mess up holiday display."
5214 ;; Catch the error if dealing with the new add-to-diary-alist
5215 (when org-disable-agenda-to-diary
5216 (condition-case nil
5217 (org-add-to-diary-list original-date "Org-mode dummy" "")
5218 (error
5219 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5221 (defun org-add-to-diary-list (&rest args)
5222 (if (fboundp 'diary-add-to-list)
5223 (apply 'diary-add-to-list args)
5224 (apply 'add-to-diary-list args)))
5226 (defvar org-diary-last-run-time nil)
5228 ;;;###autoload
5229 (defun org-diary (&rest args)
5230 "Return diary information from org files.
5231 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5232 It accesses org files and extracts information from those files to be
5233 listed in the diary. The function accepts arguments specifying what
5234 items should be listed. For a list of arguments allowed here, see the
5235 variable `org-agenda-entry-types'.
5237 The call in the diary file should look like this:
5239 &%%(org-diary) ~/path/to/some/orgfile.org
5241 Use a separate line for each org file to check. Or, if you omit the file name,
5242 all files listed in `org-agenda-files' will be checked automatically:
5244 &%%(org-diary)
5246 If you don't give any arguments (as in the example above), the default value
5247 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5248 So the example above may also be written as
5250 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5252 The function expects the lisp variables `entry' and `date' to be provided
5253 by the caller, because this is how the calendar works. Don't use this
5254 function from a program - use `org-agenda-get-day-entries' instead."
5255 (when (> (- (org-float-time)
5256 org-agenda-last-marker-time)
5258 ;; I am not sure if this works with sticky agendas, because the marker
5259 ;; list is then no longer a global variable.
5260 (org-agenda-reset-markers))
5261 (org-compile-prefix-format 'agenda)
5262 (org-set-sorting-strategy 'agenda)
5263 (setq args (or args org-agenda-entry-types))
5264 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5265 (list entry)
5266 (org-agenda-files t)))
5267 (time (org-float-time))
5268 file rtn results)
5269 (when (or (not org-diary-last-run-time)
5270 (> (- time
5271 org-diary-last-run-time)
5273 (org-agenda-prepare-buffers files))
5274 (setq org-diary-last-run-time time)
5275 ;; If this is called during org-agenda, don't return any entries to
5276 ;; the calendar. Org Agenda will list these entries itself.
5277 (if org-disable-agenda-to-diary (setq files nil))
5278 (while (setq file (pop files))
5279 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5280 (setq results (append results rtn)))
5281 (when results
5282 (setq results
5283 (mapcar (lambda (i) (replace-regexp-in-string
5284 org-bracket-link-regexp "\\3" i)) results))
5285 (concat (org-agenda-finalize-entries results) "\n"))))
5287 ;;; Agenda entry finders
5289 (defun org-agenda-get-day-entries (file date &rest args)
5290 "Does the work for `org-diary' and `org-agenda'.
5291 FILE is the path to a file to be checked for entries. DATE is date like
5292 the one returned by `calendar-current-date'. ARGS are symbols indicating
5293 which kind of entries should be extracted. For details about these, see
5294 the documentation of `org-diary'."
5295 (setq args (or args org-agenda-entry-types))
5296 (let* ((org-startup-folded nil)
5297 (org-startup-align-all-tables nil)
5298 (buffer (if (file-exists-p file)
5299 (org-get-agenda-file-buffer file)
5300 (error "No such file %s" file)))
5301 arg results rtn deadline-results)
5302 (if (not buffer)
5303 ;; If file does not exist, make sure an error message ends up in diary
5304 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5305 (with-current-buffer buffer
5306 (unless (derived-mode-p 'org-mode)
5307 (error "Agenda file %s is not in `org-mode'" file))
5308 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5309 (let ((case-fold-search nil))
5310 (save-excursion
5311 (save-restriction
5312 (if (eq buffer org-agenda-restrict)
5313 (narrow-to-region org-agenda-restrict-begin
5314 org-agenda-restrict-end)
5315 (widen))
5316 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5317 (while (setq arg (pop args))
5318 (cond
5319 ((and (eq arg :todo)
5320 (equal date (calendar-gregorian-from-absolute
5321 (org-today))))
5322 (setq rtn (org-agenda-get-todos))
5323 (setq results (append results rtn)))
5324 ((eq arg :timestamp)
5325 (setq rtn (org-agenda-get-blocks))
5326 (setq results (append results rtn))
5327 (setq rtn (org-agenda-get-timestamps deadline-results))
5328 (setq results (append results rtn)))
5329 ((eq arg :sexp)
5330 (setq rtn (org-agenda-get-sexps))
5331 (setq results (append results rtn)))
5332 ((eq arg :scheduled)
5333 (setq rtn (org-agenda-get-scheduled deadline-results))
5334 (setq results (append results rtn)))
5335 ((eq arg :scheduled*)
5336 (setq rtn (org-agenda-get-scheduled deadline-results t))
5337 (setq results (append results rtn)))
5338 ((eq arg :closed)
5339 (setq rtn (org-agenda-get-progress))
5340 (setq results (append results rtn)))
5341 ((eq arg :deadline)
5342 (setq rtn (org-agenda-get-deadlines))
5343 (setq deadline-results (copy-sequence rtn))
5344 (setq results (append results rtn)))
5345 ((eq arg :deadline*)
5346 (setq rtn (org-agenda-get-deadlines t))
5347 (setq deadline-results (copy-sequence rtn))
5348 (setq results (append results rtn))))))))
5349 results))))
5351 (defsubst org-em (x y list)
5352 "Is X or Y a member of LIST?"
5353 (or (memq x list) (memq y list)))
5355 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5356 (defvar org-agenda-sorting-strategy-selected nil)
5358 (defun org-agenda-get-todos ()
5359 "Return the TODO information for agenda display."
5360 (let* ((props (list 'face nil
5361 'done-face 'org-agenda-done
5362 'org-not-done-regexp org-not-done-regexp
5363 'org-todo-regexp org-todo-regexp
5364 'org-complex-heading-regexp org-complex-heading-regexp
5365 'mouse-face 'highlight
5366 'help-echo
5367 (format "mouse-2 or RET jump to org file %s"
5368 (abbreviate-file-name buffer-file-name))))
5369 (regexp (format org-heading-keyword-regexp-format
5370 (cond
5371 ((and org-select-this-todo-keyword
5372 (equal org-select-this-todo-keyword "*"))
5373 org-todo-regexp)
5374 (org-select-this-todo-keyword
5375 (concat "\\("
5376 (mapconcat 'identity
5377 (org-split-string
5378 org-select-this-todo-keyword
5379 "|")
5380 "\\|") "\\)"))
5381 (t org-not-done-regexp))))
5382 marker priority category level tags todo-state ts-date ts-date-type
5383 ee txt beg end inherited-tags todo-state-end-pos)
5384 (goto-char (point-min))
5385 (while (re-search-forward regexp nil t)
5386 (catch :skip
5387 (save-match-data
5388 (beginning-of-line)
5389 (org-agenda-skip)
5390 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5391 (unless (and (setq todo-state (org-get-todo-state))
5392 (setq todo-state-end-pos (match-end 2)))
5393 (goto-char end)
5394 (throw :skip nil))
5395 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5396 (goto-char (1+ beg))
5397 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5398 (throw :skip nil)))
5399 (goto-char (match-beginning 2))
5400 (setq marker (org-agenda-new-marker (match-beginning 0))
5401 category (org-get-category)
5402 ts-date (let (ts)
5403 (save-match-data
5404 (cond ((org-em 'scheduled-up 'scheduled-down
5405 org-agenda-sorting-strategy-selected)
5406 (setq ts (org-entry-get (point) "SCHEDULED")
5407 ts-date-type " scheduled"))
5408 ((org-em 'deadline-up 'deadline-down
5409 org-agenda-sorting-strategy-selected)
5410 (setq ts (org-entry-get (point) "DEADLINE")
5411 ts-date-type " deadline"))
5412 ((org-em 'ts-up 'ts-down
5413 org-agenda-sorting-strategy-selected)
5414 (setq ts (org-entry-get (point) "TIMESTAMP")
5415 ts-date-type " timestamp"))
5416 ((org-em 'tsia-up 'tsia-down
5417 org-agenda-sorting-strategy-selected)
5418 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5419 ts-date-type " timestamp_ia"))
5420 ((org-em 'timestamp-up 'timestamp-down
5421 org-agenda-sorting-strategy-selected)
5422 (setq ts (or (org-entry-get (point) "SCHEDULED")
5423 (org-entry-get (point) "DEADLINE")
5424 (org-entry-get (point) "TIMESTAMP")
5425 (org-entry-get (point) "TIMESTAMP_IA"))
5426 ts-date-type ""))
5427 (t (setq ts-date-type "")))
5428 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5429 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5430 inherited-tags
5431 (or (eq org-agenda-show-inherited-tags 'always)
5432 (and (listp org-agenda-show-inherited-tags)
5433 (memq 'todo org-agenda-show-inherited-tags))
5434 (and (eq org-agenda-show-inherited-tags t)
5435 (or (eq org-agenda-use-tag-inheritance t)
5436 (memq 'todo org-agenda-use-tag-inheritance))))
5437 tags (org-get-tags-at nil (not inherited-tags))
5438 level (make-string (org-reduced-level (org-outline-level)) ? )
5439 txt (org-agenda-format-item "" txt level category tags t)
5440 priority (1+ (org-get-priority txt)))
5441 (org-add-props txt props
5442 'org-marker marker 'org-hd-marker marker
5443 'priority priority
5444 'level level
5445 'ts-date ts-date
5446 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5447 (push txt ee)
5448 (if org-agenda-todo-list-sublevels
5449 (goto-char todo-state-end-pos)
5450 (org-end-of-subtree 'invisible))))
5451 (nreverse ee)))
5453 (defun org-agenda-todo-custom-ignore-p (time n)
5454 "Check whether timestamp is farther away than n number of days.
5455 This function is invoked if `org-agenda-todo-ignore-deadlines',
5456 `org-agenda-todo-ignore-scheduled' or
5457 `org-agenda-todo-ignore-timestamp' is set to an integer."
5458 (let ((days (org-time-stamp-to-now
5459 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5460 (if (>= n 0)
5461 (>= days n)
5462 (<= days n))))
5464 ;;;###autoload
5465 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5466 (&optional end)
5467 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5468 (when (or org-agenda-todo-ignore-with-date
5469 org-agenda-todo-ignore-scheduled
5470 org-agenda-todo-ignore-deadlines
5471 org-agenda-todo-ignore-timestamp)
5472 (setq end (or end (save-excursion (outline-next-heading) (point))))
5473 (save-excursion
5474 (or (and org-agenda-todo-ignore-with-date
5475 (re-search-forward org-ts-regexp end t))
5476 (and org-agenda-todo-ignore-scheduled
5477 (re-search-forward org-scheduled-time-regexp end t)
5478 (cond
5479 ((eq org-agenda-todo-ignore-scheduled 'future)
5480 (> (org-time-stamp-to-now
5481 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5482 ((eq org-agenda-todo-ignore-scheduled 'past)
5483 (<= (org-time-stamp-to-now
5484 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5485 ((numberp org-agenda-todo-ignore-scheduled)
5486 (org-agenda-todo-custom-ignore-p
5487 (match-string 1) org-agenda-todo-ignore-scheduled))
5488 (t)))
5489 (and org-agenda-todo-ignore-deadlines
5490 (re-search-forward org-deadline-time-regexp end t)
5491 (cond
5492 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5493 ((eq org-agenda-todo-ignore-deadlines 'far)
5494 (not (org-deadline-close (match-string 1))))
5495 ((eq org-agenda-todo-ignore-deadlines 'future)
5496 (> (org-time-stamp-to-now
5497 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5498 ((eq org-agenda-todo-ignore-deadlines 'past)
5499 (<= (org-time-stamp-to-now
5500 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5501 ((numberp org-agenda-todo-ignore-deadlines)
5502 (org-agenda-todo-custom-ignore-p
5503 (match-string 1) org-agenda-todo-ignore-deadlines))
5504 (t (org-deadline-close (match-string 1)))))
5505 (and org-agenda-todo-ignore-timestamp
5506 (let ((buffer (current-buffer))
5507 (regexp
5508 (concat
5509 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5510 (start (point)))
5511 ;; Copy current buffer into a temporary one
5512 (with-temp-buffer
5513 (insert-buffer-substring buffer start end)
5514 (goto-char (point-min))
5515 ;; Delete SCHEDULED and DEADLINE items
5516 (while (re-search-forward regexp end t)
5517 (delete-region (match-beginning 0) (match-end 0)))
5518 (goto-char (point-min))
5519 ;; No search for timestamp left
5520 (when (re-search-forward org-ts-regexp nil t)
5521 (cond
5522 ((eq org-agenda-todo-ignore-timestamp 'future)
5523 (> (org-time-stamp-to-now
5524 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5525 ((eq org-agenda-todo-ignore-timestamp 'past)
5526 (<= (org-time-stamp-to-now
5527 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5528 ((numberp org-agenda-todo-ignore-timestamp)
5529 (org-agenda-todo-custom-ignore-p
5530 (match-string 1) org-agenda-todo-ignore-timestamp))
5531 (t))))))))))
5533 (defun org-agenda-get-timestamps (&optional deadline-results)
5534 "Return the date stamp information for agenda display."
5535 (let* ((props (list 'face 'org-agenda-calendar-event
5536 'org-not-done-regexp org-not-done-regexp
5537 'org-todo-regexp org-todo-regexp
5538 'org-complex-heading-regexp org-complex-heading-regexp
5539 'mouse-face 'highlight
5540 'help-echo
5541 (format "mouse-2 or RET jump to org file %s"
5542 (abbreviate-file-name buffer-file-name))))
5543 (d1 (calendar-absolute-from-gregorian date))
5545 (deadline-position-alist
5546 (mapcar (lambda (a) (and (setq mm (get-text-property
5547 0 'org-hd-marker a))
5548 (cons (marker-position mm) a)))
5549 deadline-results))
5550 (remove-re org-ts-regexp)
5551 (regexp
5552 (concat
5553 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5554 (regexp-quote
5555 (substring
5556 (format-time-string
5557 (car org-time-stamp-formats)
5558 (apply 'encode-time ; DATE bound by calendar
5559 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5560 1 11))
5561 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5562 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5563 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5564 donep tmp priority category level ee txt timestr tags
5565 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5566 inherited-tags ts-date)
5567 (goto-char (point-min))
5568 (while (setq end-of-match (re-search-forward regexp nil t))
5569 (setq b0 (match-beginning 0)
5570 b3 (match-beginning 3) e3 (match-end 3)
5571 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5572 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5573 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5574 (member todo-state
5575 org-agenda-repeating-timestamp-show-all)))
5576 (catch :skip
5577 (and (org-at-date-range-p) (throw :skip nil))
5578 (org-agenda-skip)
5579 (if (and (match-end 1)
5580 (not (= d1 (org-time-string-to-absolute
5581 (match-string 1) d1 nil show-all
5582 (current-buffer) b0))))
5583 (throw :skip nil))
5584 (if (and e3
5585 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5586 (throw :skip nil))
5587 (setq tmp (buffer-substring (max (point-min)
5588 (- b0 org-ds-keyword-length))
5590 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5591 inactivep (= (char-after b0) ?\[)
5592 deadlinep (string-match org-deadline-regexp tmp)
5593 scheduledp (string-match org-scheduled-regexp tmp)
5594 closedp (and org-agenda-include-inactive-timestamps
5595 (string-match org-closed-string tmp))
5596 clockp (and org-agenda-include-inactive-timestamps
5597 (or (string-match org-clock-string tmp)
5598 (string-match "]-+\\'" tmp)))
5599 warntime (get-text-property (point) 'org-appt-warntime)
5600 donep (member todo-state org-done-keywords))
5601 (if (or scheduledp deadlinep closedp clockp
5602 (and donep org-agenda-skip-timestamp-if-done))
5603 (throw :skip t))
5604 (if (string-match ">" timestr)
5605 ;; substring should only run to end of time stamp
5606 (setq timestr (substring timestr 0 (match-end 0))))
5607 (setq marker (org-agenda-new-marker b0)
5608 category (org-get-category b0))
5609 (save-excursion
5610 (if (not (re-search-backward org-outline-regexp-bol nil t))
5611 (throw :skip nil)
5612 (goto-char (match-beginning 0))
5613 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5614 (assoc (point) deadline-position-alist))
5615 (throw :skip nil))
5616 (setq hdmarker (org-agenda-new-marker)
5617 inherited-tags
5618 (or (eq org-agenda-show-inherited-tags 'always)
5619 (and (listp org-agenda-show-inherited-tags)
5620 (memq 'agenda org-agenda-show-inherited-tags))
5621 (and (eq org-agenda-show-inherited-tags t)
5622 (or (eq org-agenda-use-tag-inheritance t)
5623 (memq 'agenda org-agenda-use-tag-inheritance))))
5624 tags (org-get-tags-at nil (not inherited-tags))
5625 level (make-string (org-reduced-level (org-outline-level)) ? ))
5626 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5627 (setq head (or (match-string 1) ""))
5628 (setq txt (org-agenda-format-item
5629 (if inactivep org-agenda-inactive-leader nil)
5630 head level category tags timestr
5631 remove-re habitp)))
5632 (setq priority (org-get-priority txt))
5633 (org-add-props txt props 'priority priority
5634 'org-marker marker 'org-hd-marker hdmarker
5635 'date date
5636 'level level
5637 'ts-date
5638 (ignore-errors (org-time-string-to-absolute timestr))
5639 'todo-state todo-state
5640 'warntime warntime
5641 'type "timestamp")
5642 (push txt ee))
5643 (if org-agenda-skip-additional-timestamps-same-entry
5644 (outline-next-heading)
5645 (goto-char end-of-match))))
5646 (nreverse ee)))
5648 (defun org-agenda-get-sexps ()
5649 "Return the sexp information for agenda display."
5650 (require 'diary-lib)
5651 (let* ((props (list 'face 'org-agenda-calendar-sexp
5652 'mouse-face 'highlight
5653 'help-echo
5654 (format "mouse-2 or RET jump to org file %s"
5655 (abbreviate-file-name buffer-file-name))))
5656 (regexp "^&?%%(")
5657 marker category extra level ee txt tags entry
5658 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5659 (goto-char (point-min))
5660 (while (re-search-forward regexp nil t)
5661 (catch :skip
5662 (org-agenda-skip)
5663 (setq beg (match-beginning 0))
5664 (goto-char (1- (match-end 0)))
5665 (setq b (point))
5666 (forward-sexp 1)
5667 (setq sexp (buffer-substring b (point)))
5668 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5669 (org-trim (match-string 1))
5670 ""))
5671 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5672 (when result
5673 (setq marker (org-agenda-new-marker beg)
5674 level (make-string (org-reduced-level (org-outline-level)) ? )
5675 category (org-get-category beg)
5676 inherited-tags
5677 (or (eq org-agenda-show-inherited-tags 'always)
5678 (and (listp org-agenda-show-inherited-tags)
5679 (memq 'agenda org-agenda-show-inherited-tags))
5680 (and (eq org-agenda-show-inherited-tags t)
5681 (or (eq org-agenda-use-tag-inheritance t)
5682 (memq 'agenda org-agenda-use-tag-inheritance))))
5683 tags (org-get-tags-at nil (not inherited-tags))
5684 todo-state (org-get-todo-state)
5685 warntime (get-text-property (point) 'org-appt-warntime)
5686 extra nil)
5688 (dolist (r (if (stringp result)
5689 (list result)
5690 result)) ;; we expect a list here
5691 (when (and org-agenda-diary-sexp-prefix
5692 (string-match org-agenda-diary-sexp-prefix r))
5693 (setq extra (match-string 0 r)
5694 r (replace-match "" nil nil r)))
5695 (if (string-match "\\S-" r)
5696 (setq txt r)
5697 (setq txt "SEXP entry returned empty string"))
5698 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5699 (org-add-props txt props 'org-marker marker
5700 'date date 'todo-state todo-state
5701 'tags tags 'level level
5702 'type "sexp" 'warntime warntime)
5703 (push txt ee)))))
5704 (nreverse ee)))
5706 ;; Calendar sanity: define some functions that are independent of
5707 ;; `calendar-date-style'.
5708 ;; Normally I would like to use ISO format when calling the diary functions,
5709 ;; but to make sure we still have Emacs 22 compatibility we bind
5710 ;; also `european-calendar-style' and use european format
5711 (defun org-anniversary (year month day &optional mark)
5712 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5713 (org-no-warnings
5714 (let ((calendar-date-style 'european) (european-calendar-style t))
5715 (diary-anniversary day month year mark))))
5716 (defun org-cyclic (N year month day &optional mark)
5717 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5718 (org-no-warnings
5719 (let ((calendar-date-style 'european) (european-calendar-style t))
5720 (diary-cyclic N day month year mark))))
5721 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5722 "Like `diary-block', but with fixed (ISO) order of arguments."
5723 (org-no-warnings
5724 (let ((calendar-date-style 'european) (european-calendar-style t))
5725 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5726 (defun org-date (year month day &optional mark)
5727 "Like `diary-date', but with fixed (ISO) order of arguments."
5728 (org-no-warnings
5729 (let ((calendar-date-style 'european) (european-calendar-style t))
5730 (diary-date day month year mark))))
5732 ;; Define the` org-class' function
5733 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5734 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5735 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5736 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5737 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5738 `holidays', then any date that is known by the Emacs calendar to be a
5739 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5740 then those holidays will be skipped."
5741 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5742 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5743 (d (calendar-absolute-from-gregorian date))
5744 (h (when skip-weeks (calendar-check-holidays date))))
5745 (and
5746 (<= date1 d)
5747 (<= d date2)
5748 (= (calendar-day-of-week date) dayname)
5749 (or (not skip-weeks)
5750 (progn
5751 (require 'cal-iso)
5752 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5753 (not (or (and h (memq 'holidays skip-weeks))
5754 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5755 entry)))
5757 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5758 "Like `org-class', but honor `calendar-date-style'.
5759 The order of the first 2 times 3 arguments depends on the variable
5760 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5761 So for American calendars, give this as MONTH DAY YEAR, for European as
5762 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5763 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5764 is any number of ISO weeks in the block period for which the item should
5765 be skipped.
5767 This function is here only for backward compatibility and it is deprecated,
5768 please use `org-class' instead."
5769 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5770 (date2 (org-order-calendar-date-args m2 d2 y2)))
5771 (org-class
5772 (nth 2 date1) (car date1) (nth 1 date1)
5773 (nth 2 date2) (car date2) (nth 1 date2)
5774 dayname skip-weeks)))
5775 (make-obsolete 'org-diary-class 'org-class "")
5777 (defalias 'org-get-closed 'org-agenda-get-progress)
5778 (defun org-agenda-get-progress ()
5779 "Return the logged TODO entries for agenda display."
5780 (let* ((props (list 'mouse-face 'highlight
5781 'org-not-done-regexp org-not-done-regexp
5782 'org-todo-regexp org-todo-regexp
5783 'org-complex-heading-regexp org-complex-heading-regexp
5784 'help-echo
5785 (format "mouse-2 or RET jump to org file %s"
5786 (abbreviate-file-name buffer-file-name))))
5787 (items (if (consp org-agenda-show-log-scoped)
5788 org-agenda-show-log-scoped
5789 (if (eq org-agenda-show-log-scoped 'clockcheck)
5790 '(clock)
5791 org-agenda-log-mode-items)))
5792 (parts
5793 (delq nil
5794 (list
5795 (if (memq 'closed items) (concat "\\<" org-closed-string))
5796 (if (memq 'clock items) (concat "\\<" org-clock-string))
5797 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5798 (parts-re (if parts (mapconcat 'identity parts "\\|")
5799 (error "`org-agenda-log-mode-items' is empty")))
5800 (regexp (concat
5801 "\\(" parts-re "\\)"
5802 " *\\["
5803 (regexp-quote
5804 (substring
5805 (format-time-string
5806 (car org-time-stamp-formats)
5807 (apply 'encode-time ; DATE bound by calendar
5808 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5809 1 11))))
5810 (org-agenda-search-headline-for-time nil)
5811 marker hdmarker priority category level tags closedp
5812 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5813 (goto-char (point-min))
5814 (while (re-search-forward regexp nil t)
5815 (catch :skip
5816 (org-agenda-skip)
5817 (setq marker (org-agenda-new-marker (match-beginning 0))
5818 closedp (equal (match-string 1) org-closed-string)
5819 statep (equal (string-to-char (match-string 1)) ?-)
5820 clockp (not (or closedp statep))
5821 state (and statep (match-string 2))
5822 category (org-get-category (match-beginning 0))
5823 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5824 (when (string-match "\\]" timestr)
5825 ;; substring should only run to end of time stamp
5826 (setq rest (substring timestr (match-end 0))
5827 timestr (substring timestr 0 (match-end 0)))
5828 (if (and (not closedp) (not statep)
5829 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5830 rest))
5831 (progn (setq timestr (concat (substring timestr 0 -1)
5832 "-" (match-string 1 rest) "]"))
5833 (setq clocked (match-string 2 rest)))
5834 (setq clocked "-")))
5835 (save-excursion
5836 (setq extra
5837 (cond
5838 ((not org-agenda-log-mode-add-notes) nil)
5839 (statep
5840 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5841 (match-string 1)))
5842 (clockp
5843 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5844 (match-string 1)))))
5845 (if (not (re-search-backward org-outline-regexp-bol nil t))
5846 (throw :skip nil)
5847 (goto-char (match-beginning 0))
5848 (setq hdmarker (org-agenda-new-marker)
5849 inherited-tags
5850 (or (eq org-agenda-show-inherited-tags 'always)
5851 (and (listp org-agenda-show-inherited-tags)
5852 (memq 'todo org-agenda-show-inherited-tags))
5853 (and (eq org-agenda-show-inherited-tags t)
5854 (or (eq org-agenda-use-tag-inheritance t)
5855 (memq 'todo org-agenda-use-tag-inheritance))))
5856 tags (org-get-tags-at nil (not inherited-tags))
5857 level (make-string (org-reduced-level (org-outline-level)) ? ))
5858 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5859 (setq txt (match-string 1))
5860 (when extra
5861 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5862 (setq txt (concat (substring txt 0 (match-beginning 1))
5863 " - " extra " " (match-string 2 txt)))
5864 (setq txt (concat txt " - " extra))))
5865 (setq txt (org-agenda-format-item
5866 (cond
5867 (closedp "Closed: ")
5868 (statep (concat "State: (" state ")"))
5869 (t (concat "Clocked: (" clocked ")")))
5870 txt level category tags timestr)))
5871 (setq priority 100000)
5872 (org-add-props txt props
5873 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5874 'priority priority 'level level
5875 'type "closed" 'date date
5876 'undone-face 'org-warning 'done-face 'org-agenda-done)
5877 (push txt ee))
5878 (goto-char (point-at-eol))))
5879 (nreverse ee)))
5881 (defun org-agenda-show-clocking-issues ()
5882 "Add overlays, showing issues with clocking.
5883 See also the user option `org-agenda-clock-consistency-checks'."
5884 (interactive)
5885 (let* ((org-time-clocksum-use-effort-durations nil)
5886 (pl org-agenda-clock-consistency-checks)
5887 (re (concat "^[ \t]*"
5888 org-clock-string
5889 "[ \t]+"
5890 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5891 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5892 (tlstart 0.)
5893 (tlend 0.)
5894 (maxtime (org-hh:mm-string-to-minutes
5895 (or (plist-get pl :max-duration) "24:00")))
5896 (mintime (org-hh:mm-string-to-minutes
5897 (or (plist-get pl :min-duration) 0)))
5898 (maxgap (org-hh:mm-string-to-minutes
5899 ;; default 30:00 means never complain
5900 (or (plist-get pl :max-gap) "30:00")))
5901 (gapok (mapcar 'org-hh:mm-string-to-minutes
5902 (plist-get pl :gap-ok-around)))
5903 (def-face (or (plist-get pl :default-face)
5904 '((:background "DarkRed") (:foreground "white"))))
5905 issue face m te ts dt ov)
5906 (goto-char (point-min))
5907 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5908 (setq issue nil face def-face)
5909 (catch 'next
5910 (setq m (org-get-at-bol 'org-marker)
5911 te nil ts nil)
5912 (unless (and m (markerp m))
5913 (setq issue "No valid clock line") (throw 'next t))
5914 (org-with-point-at m
5915 (save-excursion
5916 (goto-char (point-at-bol))
5917 (unless (looking-at re)
5918 (error "No valid Clock line")
5919 (throw 'next t))
5920 (unless (match-end 3)
5921 (setq issue "No end time"
5922 face (or (plist-get pl :no-end-time-face) face))
5923 (throw 'next t))
5924 (setq ts (match-string 1)
5925 te (match-string 3)
5926 ts (org-float-time
5927 (apply 'encode-time (org-parse-time-string ts)))
5928 te (org-float-time
5929 (apply 'encode-time (org-parse-time-string te)))
5930 dt (- te ts))))
5931 (cond
5932 ((> dt (* 60 maxtime))
5933 ;; a very long clocking chunk
5934 (setq issue (format "Clocking interval is very long: %s"
5935 (org-minutes-to-clocksum-string
5936 (floor (/ (float dt) 60.))))
5937 face (or (plist-get pl :long-face) face)))
5938 ((< dt (* 60 mintime))
5939 ;; a very short clocking chunk
5940 (setq issue (format "Clocking interval is very short: %s"
5941 (org-minutes-to-clocksum-string
5942 (floor (/ (float dt) 60.))))
5943 face (or (plist-get pl :short-face) face)))
5944 ((and (> tlend 0) (< ts tlend))
5945 ;; Two clock entries are overlapping
5946 (setq issue (format "Clocking overlap: %d minutes"
5947 (/ (- tlend ts) 60))
5948 face (or (plist-get pl :overlap-face) face)))
5949 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5950 ;; There is a gap, lets see if we need to report it
5951 (unless (org-agenda-check-clock-gap tlend ts gapok)
5952 (setq issue (format "Clocking gap: %d minutes"
5953 (/ (- ts tlend) 60))
5954 face (or (plist-get pl :gap-face) face))))
5955 (t nil)))
5956 (setq tlend (or te tlend) tlstart (or ts tlstart))
5957 (when issue
5958 ;; OK, there was some issue, add an overlay to show the issue
5959 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5960 (overlay-put ov 'before-string
5961 (concat
5962 (org-add-props
5963 (format "%-43s" (concat " " issue))
5965 'face face)
5966 "\n"))
5967 (overlay-put ov 'evaporate t)))))
5969 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5970 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5971 (catch 'exit
5972 (unless ok-list
5973 ;; there are no OK times for gaps...
5974 (throw 'exit nil))
5975 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5976 ;; This is more than 24 hours, so it is OK.
5977 ;; because we have at least one OK time, that must be in the
5978 ;; 24 hour interval.
5979 (throw 'exit t))
5980 ;; We have a shorter gap.
5981 ;; Now we have to get the minute of the day when these times are
5982 (let* ((t1dec (decode-time (seconds-to-time t1)))
5983 (t2dec (decode-time (seconds-to-time t2)))
5984 ;; compute the minute on the day
5985 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5986 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5987 (when (< min2 min1)
5988 ;; if min2 is smaller than min1, this means it is on the next day.
5989 ;; Wrap it to after midnight.
5990 (setq min2 (+ min2 1440)))
5991 ;; Now check if any of the OK times is in the gap
5992 (mapc (lambda (x)
5993 ;; Wrap the time to after midnight if necessary
5994 (if (< x min1) (setq x (+ x 1440)))
5995 ;; Check if in interval
5996 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5997 ok-list)
5998 ;; Nope, this gap is not OK
5999 nil)))
6001 (defun org-agenda-get-deadlines (&optional with-hour)
6002 "Return the deadline information for agenda display.
6003 When WITH-HOUR is non-nil, only return deadlines with an hour
6004 specification like [h]h:mm."
6005 (let* ((props (list 'mouse-face 'highlight
6006 'org-not-done-regexp org-not-done-regexp
6007 'org-todo-regexp org-todo-regexp
6008 'org-complex-heading-regexp org-complex-heading-regexp
6009 'help-echo
6010 (format "mouse-2 or RET jump to org file %s"
6011 (abbreviate-file-name buffer-file-name))))
6012 (regexp (if with-hour
6013 org-deadline-time-hour-regexp
6014 org-deadline-time-regexp))
6015 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6016 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6017 (dl0 (car org-agenda-deadline-leaders))
6018 (dl1 (nth 1 org-agenda-deadline-leaders))
6019 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6020 d2 diff dfrac wdays pos pos1 category level
6021 tags suppress-prewarning ee txt head face s todo-state
6022 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6023 (goto-char (point-min))
6024 (while (re-search-forward regexp nil t)
6025 (catch :skip
6026 (org-agenda-skip)
6027 (setq s (match-string 1)
6028 txt nil
6029 pos (1- (match-beginning 1))
6030 todo-state (save-match-data (org-get-todo-state))
6031 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6032 (member todo-state
6033 org-agenda-repeating-timestamp-show-all))
6034 d2 (org-time-string-to-absolute
6035 s d1 'past show-all (current-buffer) pos)
6036 diff (- d2 d1))
6037 (setq suppress-prewarning
6038 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6039 (let ((item (buffer-substring (point-at-bol)
6040 (point-at-eol))))
6041 (save-match-data
6042 (and (string-match
6043 org-scheduled-time-regexp item)
6044 (match-string 1 item)))))))
6045 (cond
6046 ((not ds) nil)
6047 ;; The current item has a scheduled date (in ds), so
6048 ;; evaluate its prewarning lead time.
6049 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6050 ;; Use global prewarning-restart lead time.
6051 org-agenda-skip-deadline-prewarning-if-scheduled)
6052 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6053 'pre-scheduled)
6054 ;; Set prewarning to no earlier than scheduled.
6055 (min (- d2 (org-time-string-to-absolute
6056 ds d1 'past show-all (current-buffer) pos))
6057 org-deadline-warning-days))
6058 ;; Set prewarning to deadline.
6059 (t 0))))
6060 (setq wdays (if suppress-prewarning
6061 (let ((org-deadline-warning-days suppress-prewarning))
6062 (org-get-wdays s))
6063 (org-get-wdays s))
6064 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6065 upcomingp (and todayp (> diff 0)))
6066 ;; When to show a deadline in the calendar:
6067 ;; If the expiration is within wdays warning time.
6068 ;; Past-due deadlines are only shown on the current date
6069 (if (and (or (and (<= diff wdays)
6070 (and todayp (not org-agenda-only-exact-dates)))
6071 (= diff 0)))
6072 (save-excursion
6073 ;; (setq todo-state (org-get-todo-state))
6074 (setq donep (member todo-state org-done-keywords))
6075 (if (and donep
6076 (or org-agenda-skip-deadline-if-done
6077 (not (= diff 0))))
6078 (setq txt nil)
6079 (setq category (org-get-category)
6080 warntime (get-text-property (point) 'org-appt-warntime))
6081 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6082 (throw :skip nil)
6083 (goto-char (match-end 0))
6084 (setq pos1 (match-beginning 0))
6085 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6086 (setq inherited-tags
6087 (or (eq org-agenda-show-inherited-tags 'always)
6088 (and (listp org-agenda-show-inherited-tags)
6089 (memq 'agenda org-agenda-show-inherited-tags))
6090 (and (eq org-agenda-show-inherited-tags t)
6091 (or (eq org-agenda-use-tag-inheritance t)
6092 (memq 'agenda org-agenda-use-tag-inheritance))))
6093 tags (org-get-tags-at pos1 (not inherited-tags)))
6094 (setq head (buffer-substring
6095 (point)
6096 (progn (skip-chars-forward "^\r\n")
6097 (point))))
6098 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6099 (setq timestr
6100 (concat (substring s (match-beginning 1)) " "))
6101 (setq timestr 'time))
6102 (setq txt (org-agenda-format-item
6103 (cond ((= diff 0) dl0)
6104 ((> diff 0)
6105 (if (functionp dl1)
6106 (funcall dl1 diff date)
6107 (format dl1 diff)))
6109 (if (functionp dl2)
6110 (funcall dl2 diff date)
6111 (format dl2 (if (string= dl2 dl1)
6112 diff (abs diff))))))
6113 head level category tags
6114 (if (not (= diff 0)) nil timestr)))))
6115 (when txt
6116 (setq face (org-agenda-deadline-face dfrac))
6117 (org-add-props txt props
6118 'org-marker (org-agenda-new-marker pos)
6119 'warntime warntime
6120 'level level
6121 'ts-date d2
6122 'org-hd-marker (org-agenda-new-marker pos1)
6123 'priority (+ (- diff)
6124 (org-get-priority txt))
6125 'todo-state todo-state
6126 'type (if upcomingp "upcoming-deadline" "deadline")
6127 'date (if upcomingp date d2)
6128 'face (if donep 'org-agenda-done face)
6129 'undone-face face 'done-face 'org-agenda-done)
6130 (push txt ee))))))
6131 (nreverse ee)))
6133 (defun org-agenda-deadline-face (fraction)
6134 "Return the face to displaying a deadline item.
6135 FRACTION is what fraction of the head-warning time has passed."
6136 (let ((faces org-agenda-deadline-faces) f)
6137 (catch 'exit
6138 (while (setq f (pop faces))
6139 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6141 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6142 "Return the scheduled information for agenda display.
6143 When WITH-HOUR is non-nil, only return scheduled items with
6144 an hour specification like [h]h:mm."
6145 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6146 'org-todo-regexp org-todo-regexp
6147 'org-complex-heading-regexp org-complex-heading-regexp
6148 'done-face 'org-agenda-done
6149 'mouse-face 'highlight
6150 'help-echo
6151 (format "mouse-2 or RET jump to org file %s"
6152 (abbreviate-file-name buffer-file-name))))
6153 (regexp (if with-hour
6154 org-scheduled-time-hour-regexp
6155 org-scheduled-time-regexp))
6156 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6157 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6159 (deadline-position-alist
6160 (mapcar (lambda (a) (and (setq mm (get-text-property
6161 0 'org-hd-marker a))
6162 (cons (marker-position mm) a)))
6163 deadline-results))
6164 d2 diff pos pos1 category level tags donep
6165 ee txt head pastschedp todo-state face timestr s habitp show-all
6166 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6167 ddays)
6168 (goto-char (point-min))
6169 (while (re-search-forward regexp nil t)
6170 (catch :skip
6171 (org-agenda-skip)
6172 (setq s (match-string 1)
6173 txt nil
6174 pos (1- (match-beginning 1))
6175 todo-state (save-match-data (org-get-todo-state))
6176 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6177 (member todo-state
6178 org-agenda-repeating-timestamp-show-all))
6179 d2 (org-time-string-to-absolute
6180 s d1 'past show-all (current-buffer) pos)
6181 diff (- d2 d1)
6182 warntime (get-text-property (point) 'org-appt-warntime))
6183 (setq pastschedp (and todayp (< diff 0)))
6184 (setq did-habit-check-p nil)
6185 (setq suppress-delay
6186 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6187 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6188 (save-match-data
6189 (and (string-match
6190 org-deadline-time-regexp item)
6191 (match-string 1 item)))))))
6192 (cond
6193 ((not ds) nil)
6194 ;; The current item has a deadline date (in ds), so
6195 ;; evaluate its delay time.
6196 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6197 ;; Use global delay time.
6198 (- org-agenda-skip-scheduled-delay-if-deadline))
6199 ((eq org-agenda-skip-scheduled-delay-if-deadline
6200 'post-deadline)
6201 ;; Set delay to no later than deadline.
6202 (min (- d2 (org-time-string-to-absolute
6203 ds d1 'past show-all (current-buffer) pos))
6204 org-scheduled-delay-days))
6205 (t 0))))
6206 (setq ddays (if suppress-delay
6207 (let ((org-scheduled-delay-days suppress-delay))
6208 (org-get-wdays s t t))
6209 (org-get-wdays s t)))
6210 ;; Use a delay of 0 when there is a repeater and the delay is
6211 ;; of the form --3d
6212 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6213 (< (org-time-string-to-absolute s)
6214 (org-time-string-to-absolute
6215 s d2 'past nil (current-buffer) pos)))
6216 (setq ddays 0))
6217 ;; When to show a scheduled item in the calendar:
6218 ;; If it is on or past the date.
6219 (when (or (and (> ddays 0) (= diff (- ddays)))
6220 (and (zerop ddays) (= diff 0))
6221 (and (< (+ diff ddays) 0)
6222 (< (abs diff) org-scheduled-past-days)
6223 (and todayp (not org-agenda-only-exact-dates)))
6224 ;; org-is-habit-p uses org-entry-get, which is expansive
6225 ;; so we go extra mile to only call it once
6226 (and todayp
6227 (boundp 'org-habit-show-all-today)
6228 org-habit-show-all-today
6229 (setq did-habit-check-p t)
6230 (setq habitp (and (functionp 'org-is-habit-p)
6231 (org-is-habit-p)))))
6232 (save-excursion
6233 (setq donep (member todo-state org-done-keywords))
6234 (if (and donep
6235 (or org-agenda-skip-scheduled-if-done
6236 (not (= diff 0))
6237 (and (functionp 'org-is-habit-p)
6238 (org-is-habit-p))))
6239 (setq txt nil)
6240 (setq habitp (if did-habit-check-p habitp
6241 (and (functionp 'org-is-habit-p)
6242 (org-is-habit-p))))
6243 (setq category (org-get-category))
6244 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6245 'repeated-after-deadline)
6246 (org-get-deadline-time (point))
6247 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6248 (throw :skip nil))
6249 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6250 (throw :skip nil)
6251 (goto-char (match-end 0))
6252 (setq pos1 (match-beginning 0))
6253 (if habitp
6254 (if (or (not org-habit-show-habits)
6255 (and (not todayp)
6256 (boundp 'org-habit-show-habits-only-for-today)
6257 org-habit-show-habits-only-for-today))
6258 (throw :skip nil))
6259 (if (and
6260 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6261 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6262 pastschedp))
6263 (setq mm (assoc pos1 deadline-position-alist)))
6264 (throw :skip nil)))
6265 (setq inherited-tags
6266 (or (eq org-agenda-show-inherited-tags 'always)
6267 (and (listp org-agenda-show-inherited-tags)
6268 (memq 'agenda org-agenda-show-inherited-tags))
6269 (and (eq org-agenda-show-inherited-tags t)
6270 (or (eq org-agenda-use-tag-inheritance t)
6271 (memq 'agenda org-agenda-use-tag-inheritance))))
6273 tags (org-get-tags-at nil (not inherited-tags)))
6274 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6275 (setq head (buffer-substring
6276 (point)
6277 (progn (skip-chars-forward "^\r\n") (point))))
6278 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6279 (setq timestr
6280 (concat (substring s (match-beginning 1)) " "))
6281 (setq timestr 'time))
6282 (setq txt (org-agenda-format-item
6283 (if (= diff 0)
6284 (car org-agenda-scheduled-leaders)
6285 (format (nth 1 org-agenda-scheduled-leaders)
6286 (- 1 diff)))
6287 head level category tags
6288 (if (not (= diff 0)) nil timestr)
6289 nil habitp))))
6290 (when txt
6291 (setq face
6292 (cond
6293 ((and (not habitp) pastschedp)
6294 'org-scheduled-previously)
6295 (todayp 'org-scheduled-today)
6296 (t 'org-scheduled))
6297 habitp (and habitp (org-habit-parse-todo)))
6298 (org-add-props txt props
6299 'undone-face face
6300 'face (if donep 'org-agenda-done face)
6301 'org-marker (org-agenda-new-marker pos)
6302 'org-hd-marker (org-agenda-new-marker pos1)
6303 'type (if pastschedp "past-scheduled" "scheduled")
6304 'date (if pastschedp d2 date)
6305 'ts-date d2
6306 'warntime warntime
6307 'level level
6308 'priority (if habitp
6309 (org-habit-get-priority habitp)
6310 (+ 94 (- 5 diff) (org-get-priority txt)))
6311 'org-habit-p habitp
6312 'todo-state todo-state)
6313 (push txt ee))))))
6314 (nreverse ee)))
6316 (defun org-agenda-get-blocks ()
6317 "Return the date-range information for agenda display."
6318 (let* ((props (list 'face nil
6319 'org-not-done-regexp org-not-done-regexp
6320 'org-todo-regexp org-todo-regexp
6321 'org-complex-heading-regexp org-complex-heading-regexp
6322 'mouse-face 'highlight
6323 'help-echo
6324 (format "mouse-2 or RET jump to org file %s"
6325 (abbreviate-file-name buffer-file-name))))
6326 (regexp org-tr-regexp)
6327 (d0 (calendar-absolute-from-gregorian date))
6328 marker hdmarker ee txt d1 d2 s1 s2 category
6329 level todo-state tags pos head donep inherited-tags)
6330 (goto-char (point-min))
6331 (while (re-search-forward regexp nil t)
6332 (catch :skip
6333 (org-agenda-skip)
6334 (setq pos (point))
6335 (let ((start-time (match-string 1))
6336 (end-time (match-string 2)))
6337 (setq s1 (match-string 1)
6338 s2 (match-string 2)
6339 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6340 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6341 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6342 ;; Only allow days between the limits, because the normal
6343 ;; date stamps will catch the limits.
6344 (save-excursion
6345 (setq todo-state (org-get-todo-state))
6346 (setq donep (member todo-state org-done-keywords))
6347 (if (and donep org-agenda-skip-timestamp-if-done)
6348 (throw :skip t))
6349 (setq marker (org-agenda-new-marker (point))
6350 category (org-get-category))
6351 (if (not (re-search-backward org-outline-regexp-bol nil t))
6352 (throw :skip nil)
6353 (goto-char (match-beginning 0))
6354 (setq hdmarker (org-agenda-new-marker (point))
6355 inherited-tags
6356 (or (eq org-agenda-show-inherited-tags 'always)
6357 (and (listp org-agenda-show-inherited-tags)
6358 (memq 'agenda org-agenda-show-inherited-tags))
6359 (and (eq org-agenda-show-inherited-tags t)
6360 (or (eq org-agenda-use-tag-inheritance t)
6361 (memq 'agenda org-agenda-use-tag-inheritance))))
6363 tags (org-get-tags-at nil (not inherited-tags)))
6364 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6365 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6366 (setq head (match-string 1))
6367 (let ((remove-re
6368 (if org-agenda-remove-timeranges-from-blocks
6369 (concat
6370 "<" (regexp-quote s1) ".*?>"
6371 "--"
6372 "<" (regexp-quote s2) ".*?>")
6373 nil)))
6374 (setq txt (org-agenda-format-item
6375 (format
6376 (nth (if (= d1 d2) 0 1)
6377 org-agenda-timerange-leaders)
6378 (1+ (- d0 d1)) (1+ (- d2 d1)))
6379 head level category tags
6380 (cond ((and (= d1 d0) (= d2 d0))
6381 (concat "<" start-time ">--<" end-time ">"))
6382 ((= d1 d0)
6383 (concat "<" start-time ">"))
6384 ((= d2 d0)
6385 (concat "<" end-time ">")))
6386 remove-re))))
6387 (org-add-props txt props
6388 'org-marker marker 'org-hd-marker hdmarker
6389 'type "block" 'date date
6390 'level level
6391 'todo-state todo-state
6392 'priority (org-get-priority txt))
6393 (push txt ee))))
6394 (goto-char pos)))
6395 ;; Sort the entries by expiration date.
6396 (nreverse ee)))
6398 ;;; Agenda presentation and sorting
6400 (defvar org-prefix-has-time nil
6401 "A flag, set by `org-compile-prefix-format'.
6402 The flag is set if the currently compiled format contains a `%t'.")
6403 (defvar org-prefix-has-tag nil
6404 "A flag, set by `org-compile-prefix-format'.
6405 The flag is set if the currently compiled format contains a `%T'.")
6406 (defvar org-prefix-has-effort nil
6407 "A flag, set by `org-compile-prefix-format'.
6408 The flag is set if the currently compiled format contains a `%e'.")
6409 (defvar org-prefix-has-breadcrumbs nil
6410 "A flag, set by `org-compile-prefix-format'.
6411 The flag is set if the currently compiled format contains a `%b'.")
6412 (defvar org-prefix-category-length nil
6413 "Used by `org-compile-prefix-format' to remember the category field width.")
6414 (defvar org-prefix-category-max-length nil
6415 "Used by `org-compile-prefix-format' to remember the category field width.")
6417 (defun org-agenda-get-category-icon (category)
6418 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6419 (dolist (entry org-agenda-category-icon-alist)
6420 (when (org-string-match-p (car entry) category)
6421 (if (listp (cadr entry))
6422 (return (cadr entry))
6423 (return (apply 'create-image (cdr entry)))))))
6425 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6426 remove-re habitp)
6427 "Format TXT to be inserted into the agenda buffer.
6428 In particular, add the prefix and corresponding text properties.
6430 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6431 LEVEL may be a string to replace the `%l' specifier.
6432 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6433 category taken from local variable or file name. It will replace the `%c'
6434 specifier in the format.
6435 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6436 TXT for sorting of this entry, and for the `%t' specifier in the format.
6437 When DOTIME is a string, this string is searched for a time before TXT is.
6438 TAGS can be the tags of the headline.
6439 Any match of REMOVE-RE will be removed from TXT."
6440 ;; We keep the org-prefix-* variable values along with a compiled
6441 ;; formatter, so that multiple agendas existing at the same time do
6442 ;; not step on each other toes.
6444 ;; It was inconvenient to make these variables buffer local in
6445 ;; Agenda buffers, because this function expects to be called with
6446 ;; the buffer where item comes from being current, and not agenda
6447 ;; buffer
6448 (let* ((bindings (car org-prefix-format-compiled))
6449 (formatter (cadr org-prefix-format-compiled)))
6450 (loop for (var value) in bindings
6451 do (set var value))
6452 (save-match-data
6453 ;; Diary entries sometimes have extra whitespace at the beginning
6454 (setq txt (org-trim txt))
6456 ;; Fix the tags part in txt
6457 (setq txt (org-agenda-fix-displayed-tags
6458 txt tags
6459 org-agenda-show-inherited-tags
6460 org-agenda-hide-tags-regexp))
6462 (let* ((category (or category
6463 (if buffer-file-name
6464 (file-name-sans-extension
6465 (file-name-nondirectory buffer-file-name))
6466 "")))
6467 (category-icon (org-agenda-get-category-icon category))
6468 (category-icon (if category-icon
6469 (propertize " " 'display category-icon)
6470 ""))
6471 (effort (and (not (string= txt ""))
6472 (get-text-property 1 'effort txt)))
6473 ;; time, tag, effort are needed for the eval of the prefix format
6474 (tag (if tags (nth (1- (length tags)) tags) ""))
6475 time
6476 (ts (if dotime (concat
6477 (if (stringp dotime) dotime "")
6478 (and org-agenda-search-headline-for-time txt))))
6479 (time-of-day (and dotime (org-get-time-of-day ts)))
6480 stamp plain s0 s1 s2 rtn srp l
6481 duration breadcrumbs)
6482 (and (derived-mode-p 'org-mode) buffer-file-name
6483 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6484 (when (and dotime time-of-day)
6485 ;; Extract starting and ending time and move them to prefix
6486 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6487 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6488 (setq s0 (match-string 0 ts)
6489 srp (and stamp (match-end 3))
6490 s1 (match-string (if plain 1 2) ts)
6491 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6493 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6494 ;; them, we might want to remove them there to avoid duplication.
6495 ;; The user can turn this off with a variable.
6496 (if (and org-prefix-has-time
6497 org-agenda-remove-times-when-in-prefix (or stamp plain)
6498 (string-match (concat (regexp-quote s0) " *") txt)
6499 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6500 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6501 (= (match-beginning 0) 0)
6503 (setq txt (replace-match "" nil nil txt))))
6504 ;; Normalize the time(s) to 24 hour
6505 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6506 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6508 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6509 (let (org-time-clocksum-use-effort-durations)
6510 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6511 (setq s2
6512 (org-minutes-to-clocksum-string
6513 (+ (org-hh:mm-string-to-minutes s1)
6514 org-agenda-default-appointment-duration)))))
6516 ;; Compute the duration
6517 (when s2
6518 (setq duration (- (org-hh:mm-string-to-minutes s2)
6519 (org-hh:mm-string-to-minutes s1)))))
6521 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6522 txt)
6523 ;; Tags are in the string
6524 (if (or (eq org-agenda-remove-tags t)
6525 (and org-agenda-remove-tags
6526 org-prefix-has-tag))
6527 (setq txt (replace-match "" t t txt))
6528 (setq txt (replace-match
6529 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6530 (match-string 2 txt))
6531 t t txt))))
6533 (when remove-re
6534 (while (string-match remove-re txt)
6535 (setq txt (replace-match "" t t txt))))
6537 ;; Set org-heading property on `txt' to mark the start of the
6538 ;; heading.
6539 (add-text-properties 0 (length txt) '(org-heading t) txt)
6541 ;; Prepare the variables needed in the eval of the compiled format
6542 (if org-prefix-has-breadcrumbs
6543 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6544 (let ((s (org-display-outline-path nil nil "->" t)))
6545 (if (eq "" s) "" (concat s "->"))))))
6546 (setq time (cond (s2 (concat
6547 (org-agenda-time-of-day-to-ampm-maybe s1)
6548 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6549 (if org-agenda-timegrid-use-ampm " ")))
6550 (s1 (concat
6551 (org-agenda-time-of-day-to-ampm-maybe s1)
6552 (if org-agenda-timegrid-use-ampm
6553 "........ "
6554 "......")))
6555 (t ""))
6556 extra (or (and (not habitp) extra) "")
6557 category (if (symbolp category) (symbol-name category) category)
6558 level (or level ""))
6559 (if (string-match org-bracket-link-regexp category)
6560 (progn
6561 (setq l (if (match-end 3)
6562 (- (match-end 3) (match-beginning 3))
6563 (- (match-end 1) (match-beginning 1))))
6564 (when (< l (or org-prefix-category-length 0))
6565 (setq category (copy-sequence category))
6566 (org-add-props category nil
6567 'extra-space (make-string
6568 (- org-prefix-category-length l 1) ?\ ))))
6569 (if (and org-prefix-category-max-length
6570 (>= (length category) org-prefix-category-max-length))
6571 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6572 ;; Evaluate the compiled format
6573 (setq rtn (concat (eval formatter) txt))
6575 ;; And finally add the text properties
6576 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6577 (org-add-props rtn nil
6578 'org-category category
6579 'tags (mapcar 'org-downcase-keep-props tags)
6580 'org-highest-priority org-highest-priority
6581 'org-lowest-priority org-lowest-priority
6582 'time-of-day time-of-day
6583 'duration duration
6584 'breadcrumbs breadcrumbs
6585 'txt txt
6586 'level level
6587 'time time
6588 'extra extra
6589 'format org-prefix-format-compiled
6590 'dotime dotime)))))
6592 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6593 "Remove tags string from TXT, and add a modified list of tags.
6594 The modified list may contain inherited tags, and tags matched by
6595 `org-agenda-hide-tags-regexp' will be removed."
6596 (when (or add-inherited hide-re)
6597 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6598 (setq txt (substring txt 0 (match-beginning 0))))
6599 (setq tags
6600 (delq nil
6601 (mapcar (lambda (tg)
6602 (if (or (and hide-re (string-match hide-re tg))
6603 (and (not add-inherited)
6604 (get-text-property 0 'inherited tg)))
6606 tg))
6607 tags)))
6608 (when tags
6609 (let ((have-i (get-text-property 0 'inherited (car tags)))
6611 (setq txt (concat txt " :"
6612 (mapconcat
6613 (lambda (x)
6614 (setq i (get-text-property 0 'inherited x))
6615 (if (and have-i (not i))
6616 (progn
6617 (setq have-i nil)
6618 (concat ":" x))
6620 tags ":")
6621 (if have-i "::" ":"))))))
6622 txt)
6624 (defun org-downcase-keep-props (s)
6625 (let ((props (text-properties-at 0 s)))
6626 (setq s (downcase s))
6627 (add-text-properties 0 (length s) props s)
6630 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6632 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6633 "Add a time-grid for agenda items which need it.
6635 LIST is the list of agenda items formatted by `org-agenda-list'.
6636 NDAYS is the span of the current agenda view.
6637 TODAYP is `t' when the current agenda view is on today."
6638 (catch 'exit
6639 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6640 ((and todayp (member 'today (car org-agenda-time-grid))))
6641 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6642 ((member 'weekly (car org-agenda-time-grid)))
6643 (t (throw 'exit list)))
6644 (let* ((have (delq nil (mapcar
6645 (lambda (x) (get-text-property 1 'time-of-day x))
6646 list)))
6647 (string (nth 1 org-agenda-time-grid))
6648 (gridtimes (nth 2 org-agenda-time-grid))
6649 (req (car org-agenda-time-grid))
6650 (remove (member 'remove-match req))
6651 new time)
6652 (if (and (member 'require-timed req) (not have))
6653 ;; don't show empty grid
6654 (throw 'exit list))
6655 (while (setq time (pop gridtimes))
6656 (unless (and remove (member time have))
6657 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6658 (push (org-agenda-format-item
6659 nil string nil "" nil
6660 (concat (substring time 0 -2) ":" (substring time -2)))
6661 new)
6662 (put-text-property
6663 2 (length (car new)) 'face 'org-time-grid (car new))))
6664 (when (and todayp org-agenda-show-current-time-in-grid)
6665 (push (org-agenda-format-item
6666 nil org-agenda-current-time-string nil "" nil
6667 (format-time-string "%H:%M "))
6668 new)
6669 (put-text-property
6670 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6672 (if (member 'time-up org-agenda-sorting-strategy-selected)
6673 (append new list)
6674 (append list new)))))
6676 (defun org-compile-prefix-format (key)
6677 "Compile the prefix format into a Lisp form that can be evaluated.
6678 The resulting form and associated variable bindings is returned
6679 and stored in the variable `org-prefix-format-compiled'."
6680 (setq org-prefix-has-time nil
6681 org-prefix-has-tag nil
6682 org-prefix-category-length nil
6683 org-prefix-has-effort nil
6684 org-prefix-has-breadcrumbs nil)
6685 (let ((s (cond
6686 ((stringp org-agenda-prefix-format)
6687 org-agenda-prefix-format)
6688 ((assq key org-agenda-prefix-format)
6689 (cdr (assq key org-agenda-prefix-format)))
6690 (t " %-12:c%?-12t% s")))
6691 (start 0)
6692 varform vars var e c f opt)
6693 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6694 s start)
6695 (setq var (or (cdr (assoc (match-string 4 s)
6696 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6697 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6698 'eval)
6699 c (or (match-string 3 s) "")
6700 opt (match-beginning 1)
6701 start (1+ (match-beginning 0)))
6702 (if (equal var 'time) (setq org-prefix-has-time t))
6703 (if (equal var 'tag) (setq org-prefix-has-tag t))
6704 (if (equal var 'effort) (setq org-prefix-has-effort t))
6705 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6706 (setq f (concat "%" (match-string 2 s) "s"))
6707 (when (equal var 'category)
6708 (setq org-prefix-category-length
6709 (floor (abs (string-to-number (match-string 2 s)))))
6710 (setq org-prefix-category-max-length
6711 (let ((x (match-string 2 s)))
6712 (save-match-data
6713 (if (string-match "\\.[0-9]+" x)
6714 (string-to-number (substring (match-string 0 x) 1)))))))
6715 (if (eq var 'eval)
6716 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6717 (if opt
6718 (setq varform
6719 `(if (or (equal "" ,var) (equal nil ,var))
6721 (format ,f (concat ,var ,c))))
6722 (setq varform
6723 `(format ,f (if (or (equal ,var "")
6724 (equal ,var nil)) ""
6725 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6726 (setq s (replace-match "%s" t nil s))
6727 (push varform vars))
6728 (setq vars (nreverse vars))
6729 (with-current-buffer (or org-agenda-buffer (current-buffer))
6730 (setq org-prefix-format-compiled
6731 (list
6732 `((org-prefix-has-time ,org-prefix-has-time)
6733 (org-prefix-has-tag ,org-prefix-has-tag)
6734 (org-prefix-category-length ,org-prefix-category-length)
6735 (org-prefix-has-effort ,org-prefix-has-effort)
6736 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6737 `(format ,s ,@vars))))))
6739 (defun org-set-sorting-strategy (key)
6740 (if (symbolp (car org-agenda-sorting-strategy))
6741 ;; the old format
6742 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6743 (setq org-agenda-sorting-strategy-selected
6744 (or (cdr (assq key org-agenda-sorting-strategy))
6745 (cdr (assq 'agenda org-agenda-sorting-strategy))
6746 '(time-up category-keep priority-down)))))
6748 (defun org-get-time-of-day (s &optional string mod24)
6749 "Check string S for a time of day.
6750 If found, return it as a military time number between 0 and 2400.
6751 If not found, return nil.
6752 The optional STRING argument forces conversion into a 5 character wide string
6753 HH:MM."
6754 (save-match-data
6755 (when
6756 (and
6757 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6758 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6759 (not (eq (get-text-property 1 'face s) 'org-link)))
6760 (let* ((h (string-to-number (match-string 1 s)))
6761 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6762 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6763 (am-p (equal ampm "am"))
6764 (h1 (cond ((not ampm) h)
6765 ((= h 12) (if am-p 0 12))
6766 (t (+ h (if am-p 0 12)))))
6767 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6768 (mod h1 24) h1))
6769 (t0 (+ (* 100 h2) m))
6770 (t1 (concat (if (>= h1 24) "+" " ")
6771 (if (and org-agenda-time-leading-zero
6772 (< t0 1000)) "0" "")
6773 (if (< t0 100) "0" "")
6774 (if (< t0 10) "0" "")
6775 (int-to-string t0))))
6776 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6778 (defvar org-agenda-before-sorting-filter-function nil
6779 "Function to be applied to agenda items prior to sorting.
6780 Prior to sorting also means just before they are inserted into the agenda.
6782 To aid sorting, you may revisit the original entries and add more text
6783 properties which will later be used by the sorting functions.
6785 The function should take a string argument, an agenda line.
6786 It has access to the text properties in that line, which contain among
6787 other things, the property `org-hd-marker' that points to the entry
6788 where the line comes from. Note that not all lines going into the agenda
6789 have this property, only most.
6791 The function should return the modified string. It is probably best
6792 to ONLY change text properties.
6794 You can also use this function as a filter, by returning nil for lines
6795 you don't want to have in the agenda at all. For this application, you
6796 could bind the variable in the options section of a custom command.")
6798 (defun org-agenda-finalize-entries (list &optional type)
6799 "Sort, limit and concatenate the LIST of agenda items.
6800 The optional argument TYPE tells the agenda type."
6801 (let ((max-effort (cond ((listp org-agenda-max-effort)
6802 (cdr (assoc type org-agenda-max-effort)))
6803 (t org-agenda-max-effort)))
6804 (max-todo (cond ((listp org-agenda-max-todos)
6805 (cdr (assoc type org-agenda-max-todos)))
6806 (t org-agenda-max-todos)))
6807 (max-tags (cond ((listp org-agenda-max-tags)
6808 (cdr (assoc type org-agenda-max-tags)))
6809 (t org-agenda-max-tags)))
6810 (max-entries (cond ((listp org-agenda-max-entries)
6811 (cdr (assoc type org-agenda-max-entries)))
6812 (t org-agenda-max-entries))))
6813 (when org-agenda-before-sorting-filter-function
6814 (setq list
6815 (delq nil
6816 (mapcar
6817 org-agenda-before-sorting-filter-function list))))
6818 (setq list (mapcar 'org-agenda-highlight-todo list)
6819 list (mapcar 'identity (sort list 'org-entries-lessp)))
6820 (when max-effort
6821 (setq list (org-agenda-limit-entries
6822 list 'effort-minutes max-effort
6823 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6824 32767 -1))))))
6825 (when max-todo
6826 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6827 (when max-tags
6828 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6829 (when max-entries
6830 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6831 (mapconcat 'identity list "\n")))
6833 (defun org-agenda-limit-entries (list prop limit &optional fn)
6834 "Limit the number of agenda entries."
6835 (let ((include (and limit (< limit 0))))
6836 (if limit
6837 (let ((fun (or fn (lambda (p) (if p 1))))
6838 (lim 0))
6839 (delq nil
6840 (mapcar
6841 (lambda (e)
6842 (let ((pval (funcall
6843 fun (get-text-property (1- (length e))
6844 prop e))))
6845 (if pval (setq lim (+ lim pval)))
6846 (cond ((and pval (<= lim (abs limit))) e)
6847 ((and include (not pval)) e))))
6848 list)))
6849 list)))
6851 (defun org-agenda-limit-interactively (remove)
6852 "In agenda, interactively limit entries to various maximums."
6853 (interactive "P")
6854 (if remove
6855 (progn (setq org-agenda-max-entries nil
6856 org-agenda-max-todos nil
6857 org-agenda-max-tags nil
6858 org-agenda-max-effort nil)
6859 (org-agenda-redo))
6860 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6861 (msg (cond ((= max ?E) "How many minutes? ")
6862 ((= max ?e) "How many entries? ")
6863 ((= max ?t) "How many TODO entries? ")
6864 ((= max ?T) "How many tagged entries? ")
6865 (t (user-error "Wrong input"))))
6866 (num (string-to-number (read-from-minibuffer msg))))
6867 (cond ((equal max ?e)
6868 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6869 ((equal max ?t)
6870 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6871 ((equal max ?T)
6872 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6873 ((equal max ?E)
6874 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6875 (org-agenda-fit-window-to-buffer))
6877 (defun org-agenda-highlight-todo (x)
6878 (let ((org-done-keywords org-done-keywords-for-agenda)
6879 (case-fold-search nil)
6881 (if (eq x 'line)
6882 (save-excursion
6883 (beginning-of-line 1)
6884 (setq re (org-get-at-bol 'org-todo-regexp))
6885 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6886 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6887 (add-text-properties (match-beginning 0) (match-end 1)
6888 (list 'face (org-get-todo-face 1)))
6889 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6890 (delete-region (match-beginning 1) (1- (match-end 0)))
6891 (goto-char (match-beginning 1))
6892 (insert (format org-agenda-todo-keyword-format s)))))
6893 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6894 (setq re (get-text-property 0 'org-todo-regexp x))
6895 (when (and re
6896 ;; Test `pl' because if there's no heading content,
6897 ;; there's no point matching to highlight. Note
6898 ;; that if we didn't test `pl' first, and there
6899 ;; happened to be no keyword from `org-todo-regexp'
6900 ;; on this heading line, then the `equal' comparison
6901 ;; afterwards would spuriously succeed in the case
6902 ;; where `pl' is nil -- causing an args-out-of-range
6903 ;; error when we try to add text properties to text
6904 ;; that isn't there.
6906 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6907 x pl) pl))
6908 (add-text-properties
6909 (or (match-end 1) (match-end 0)) (match-end 0)
6910 (list 'face (org-get-todo-face (match-string 2 x)))
6912 (when (match-end 1)
6913 (setq x (concat (substring x 0 (match-end 1))
6914 (format org-agenda-todo-keyword-format
6915 (match-string 2 x))
6916 (org-add-props " " (text-properties-at 0 x))
6917 (substring x (match-end 3)))))))
6918 x)))
6920 (defsubst org-cmp-values (a b property)
6921 "Compare the numeric value of text PROPERTY for string A and B."
6922 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6923 (pb (or (get-text-property (1- (length b)) property b) 0)))
6924 (cond ((> pa pb) +1)
6925 ((< pa pb) -1))))
6927 (defsubst org-cmp-effort (a b)
6928 "Compare the effort values of string A and B."
6929 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6930 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6931 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6932 (cond ((> ea eb) +1)
6933 ((< ea eb) -1))))
6935 (defsubst org-cmp-category (a b)
6936 "Compare the string values of categories of strings A and B."
6937 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6938 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6939 (cond ((string-lessp ca cb) -1)
6940 ((string-lessp cb ca) +1))))
6942 (defsubst org-cmp-todo-state (a b)
6943 "Compare the todo states of strings A and B."
6944 (let* ((ma (or (get-text-property 1 'org-marker a)
6945 (get-text-property 1 'org-hd-marker a)))
6946 (mb (or (get-text-property 1 'org-marker b)
6947 (get-text-property 1 'org-hd-marker b)))
6948 (fa (and ma (marker-buffer ma)))
6949 (fb (and mb (marker-buffer mb)))
6950 (todo-kwds
6951 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6952 (and fb (with-current-buffer fb org-todo-keywords-1))))
6953 (ta (or (get-text-property 1 'todo-state a) ""))
6954 (tb (or (get-text-property 1 'todo-state b) ""))
6955 (la (- (length (member ta todo-kwds))))
6956 (lb (- (length (member tb todo-kwds))))
6957 (donepa (member ta org-done-keywords-for-agenda))
6958 (donepb (member tb org-done-keywords-for-agenda)))
6959 (cond ((and donepa (not donepb)) -1)
6960 ((and (not donepa) donepb) +1)
6961 ((< la lb) -1)
6962 ((< lb la) +1))))
6964 (defsubst org-cmp-alpha (a b)
6965 "Compare the headlines, alphabetically."
6966 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6967 (plb (text-property-any 0 (length b) 'org-heading t b))
6968 (ta (and pla (substring a pla)))
6969 (tb (and plb (substring b plb))))
6970 (when pla
6971 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6972 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6973 (setq ta (substring ta (match-end 0))))
6974 (setq ta (downcase ta)))
6975 (when plb
6976 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6977 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6978 (setq tb (substring tb (match-end 0))))
6979 (setq tb (downcase tb)))
6980 (cond ((not ta) +1)
6981 ((not tb) -1)
6982 ((string-lessp ta tb) -1)
6983 ((string-lessp tb ta) +1))))
6985 (defsubst org-cmp-tag (a b)
6986 "Compare the string values of the first tags of A and B."
6987 (let ((ta (car (last (get-text-property 1 'tags a))))
6988 (tb (car (last (get-text-property 1 'tags b)))))
6989 (cond ((not ta) +1)
6990 ((not tb) -1)
6991 ((string-lessp ta tb) -1)
6992 ((string-lessp tb ta) +1))))
6994 (defsubst org-cmp-time (a b)
6995 "Compare the time-of-day values of strings A and B."
6996 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6997 (ta (or (get-text-property 1 'time-of-day a) def))
6998 (tb (or (get-text-property 1 'time-of-day b) def)))
6999 (cond ((< ta tb) -1)
7000 ((< tb ta) +1))))
7002 (defsubst org-cmp-ts (a b type)
7003 "Compare the timestamps values of entries A and B.
7004 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7005 \"timestamp_ia\", compare within each of these type. When TYPE
7006 is the empty string, compare all timestamps without respect of
7007 their type."
7008 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7009 (ta (or (and (string-match type (or (get-text-property 1 type a) ""))
7010 (get-text-property 1 'ts-date a)) def))
7011 (tb (or (and (string-match type (or (get-text-property 1 type b) ""))
7012 (get-text-property 1 'ts-date b)) def)))
7013 (cond ((< ta tb) -1)
7014 ((< tb ta) +1))))
7016 (defsubst org-cmp-habit-p (a b)
7017 "Compare the todo states of strings A and B."
7018 (let ((ha (get-text-property 1 'org-habit-p a))
7019 (hb (get-text-property 1 'org-habit-p b)))
7020 (cond ((and ha (not hb)) -1)
7021 ((and (not ha) hb) +1))))
7023 (defun org-entries-lessp (a b)
7024 "Predicate for sorting agenda entries."
7025 ;; The following variables will be used when the form is evaluated.
7026 ;; So even though the compiler complains, keep them.
7027 (let* ((ss org-agenda-sorting-strategy-selected)
7028 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7029 (org-cmp-ts a b "")))
7030 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7031 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7032 (org-cmp-ts a b "scheduled")))
7033 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7034 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7035 (org-cmp-ts a b "deadline")))
7036 (deadline-down (if deadline-up (- deadline-up) nil))
7037 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7038 (org-cmp-ts a b "iatimestamp_ia")))
7039 (tsia-down (if tsia-up (- tsia-up) nil))
7040 (ts-up (and (org-em 'ts-up 'ts-down ss)
7041 (org-cmp-ts a b "timestamp")))
7042 (ts-down (if ts-up (- ts-up) nil))
7043 (time-up (and (org-em 'time-up 'time-down ss)
7044 (org-cmp-time a b)))
7045 (time-down (if time-up (- time-up) nil))
7046 (stats-up (and (org-em 'stats-up 'stats-down ss)
7047 (org-cmp-values a b 'org-stats)))
7048 (stats-down (if stats-up (- stats-up) nil))
7049 (priority-up (and (org-em 'priority-up 'priority-down ss)
7050 (org-cmp-values a b 'priority)))
7051 (priority-down (if priority-up (- priority-up) nil))
7052 (effort-up (and (org-em 'effort-up 'effort-down ss)
7053 (org-cmp-effort a b)))
7054 (effort-down (if effort-up (- effort-up) nil))
7055 (category-up (and (or (org-em 'category-up 'category-down ss)
7056 (memq 'category-keep ss))
7057 (org-cmp-category a b)))
7058 (category-down (if category-up (- category-up) nil))
7059 (category-keep (if category-up +1 nil))
7060 (tag-up (and (org-em 'tag-up 'tag-down ss)
7061 (org-cmp-tag a b)))
7062 (tag-down (if tag-up (- tag-up) nil))
7063 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7064 (org-cmp-todo-state a b)))
7065 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7066 (habit-up (and (org-em 'habit-up 'habit-down ss)
7067 (org-cmp-habit-p a b)))
7068 (habit-down (if habit-up (- habit-up) nil))
7069 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7070 (org-cmp-alpha a b)))
7071 (alpha-down (if alpha-up (- alpha-up) nil))
7072 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7073 user-defined-up user-defined-down)
7074 (if (and need-user-cmp org-agenda-cmp-user-defined
7075 (functionp org-agenda-cmp-user-defined))
7076 (setq user-defined-up
7077 (funcall org-agenda-cmp-user-defined a b)
7078 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7079 (cdr (assoc
7080 (eval (cons 'or org-agenda-sorting-strategy-selected))
7081 '((-1 . t) (1 . nil) (nil . nil))))))
7083 ;;; Agenda restriction lock
7085 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7086 "Overlay to mark the headline to which agenda commands are restricted.")
7087 (overlay-put org-agenda-restriction-lock-overlay
7088 'face 'org-agenda-restriction-lock)
7089 (overlay-put org-agenda-restriction-lock-overlay
7090 'help-echo "Agendas are currently limited to this subtree.")
7091 (org-detach-overlay org-agenda-restriction-lock-overlay)
7093 ;;;###autoload
7094 (defun org-agenda-set-restriction-lock (&optional type)
7095 "Set restriction lock for agenda, to current subtree or file.
7096 Restriction will be the file if TYPE is `file', or if type is the
7097 universal prefix '(4), or if the cursor is before the first headline
7098 in the file. Otherwise, restriction will be to the current subtree."
7099 (interactive "P")
7100 (org-agenda-remove-restriction-lock 'noupdate)
7101 (and (equal type '(4)) (setq type 'file))
7102 (setq type (cond
7103 (type type)
7104 ((org-at-heading-p) 'subtree)
7105 ((condition-case nil (org-back-to-heading t) (error nil))
7106 'subtree)
7107 (t 'file)))
7108 (if (eq type 'subtree)
7109 (progn
7110 (setq org-agenda-restrict (current-buffer))
7111 (setq org-agenda-overriding-restriction 'subtree)
7112 (put 'org-agenda-files 'org-restrict
7113 (list (buffer-file-name (buffer-base-buffer))))
7114 (org-back-to-heading t)
7115 (move-overlay org-agenda-restriction-lock-overlay
7116 (point)
7117 (if org-agenda-restriction-lock-highlight-subtree
7118 (save-excursion (org-end-of-subtree t t) (point))
7119 (point-at-eol)))
7120 (move-marker org-agenda-restrict-begin (point))
7121 (move-marker org-agenda-restrict-end
7122 (save-excursion (org-end-of-subtree t t)))
7123 (message "Locking agenda restriction to subtree"))
7124 (put 'org-agenda-files 'org-restrict
7125 (list (buffer-file-name (buffer-base-buffer))))
7126 (setq org-agenda-restrict nil)
7127 (setq org-agenda-overriding-restriction 'file)
7128 (move-marker org-agenda-restrict-begin nil)
7129 (move-marker org-agenda-restrict-end nil)
7130 (message "Locking agenda restriction to file"))
7131 (setq current-prefix-arg nil)
7132 (org-agenda-maybe-redo))
7134 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7135 "Remove the agenda restriction lock."
7136 (interactive "P")
7137 (org-detach-overlay org-agenda-restriction-lock-overlay)
7138 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7139 (setq org-agenda-overriding-restriction nil)
7140 (setq org-agenda-restrict nil)
7141 (put 'org-agenda-files 'org-restrict nil)
7142 (move-marker org-agenda-restrict-begin nil)
7143 (move-marker org-agenda-restrict-end nil)
7144 (setq current-prefix-arg nil)
7145 (message "Agenda restriction lock removed")
7146 (or noupdate (org-agenda-maybe-redo)))
7148 (defun org-agenda-maybe-redo ()
7149 "If there is any window showing the agenda view, update it."
7150 (let ((w (get-buffer-window org-agenda-buffer-name t))
7151 (w0 (selected-window)))
7152 (when w
7153 (select-window w)
7154 (org-agenda-redo)
7155 (select-window w0)
7156 (if org-agenda-overriding-restriction
7157 (message "Agenda view shifted to new %s restriction"
7158 org-agenda-overriding-restriction)
7159 (message "Agenda restriction lock removed")))))
7161 ;;; Agenda commands
7163 (defun org-agenda-check-type (error &rest types)
7164 "Check if agenda buffer is of allowed type.
7165 If ERROR is non-nil, throw an error, otherwise just return nil.
7166 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7167 (if (not org-agenda-type)
7168 (error "No Org agenda currently displayed")
7169 (if (memq org-agenda-type types)
7171 (if error
7172 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7173 nil))))
7175 (defun org-agenda-Quit ()
7176 "Exit the agenda and kill buffers loaded by `org-agenda'.
7177 Also restore the window configuration."
7178 (interactive)
7179 (if org-agenda-columns-active
7180 (org-columns-quit)
7181 (let ((buf (current-buffer)))
7182 (if (eq org-agenda-window-setup 'other-frame)
7183 (progn
7184 (org-agenda-reset-markers)
7185 (kill-buffer buf)
7186 (org-columns-remove-overlays)
7187 (setq org-agenda-archives-mode nil)
7188 (delete-frame))
7189 (and (not (eq org-agenda-window-setup 'current-window))
7190 (not (one-window-p))
7191 (delete-window))
7192 (org-agenda-reset-markers)
7193 (kill-buffer buf)
7194 (org-columns-remove-overlays)
7195 (setq org-agenda-archives-mode nil)))
7196 (setq org-agenda-buffer nil)
7197 ;; Maybe restore the pre-agenda window configuration.
7198 (and org-agenda-restore-windows-after-quit
7199 (not (eq org-agenda-window-setup 'other-frame))
7200 org-agenda-pre-window-conf
7201 (set-window-configuration org-agenda-pre-window-conf)
7202 (setq org-agenda-pre-window-conf nil))))
7204 (defun org-agenda-quit ()
7205 "Exit the agenda and restore the window configuration.
7206 When `org-agenda-sticky' is non-nil, only bury the agenda."
7207 (interactive)
7208 (if (and (eq org-indirect-buffer-display 'other-window)
7209 org-last-indirect-buffer)
7210 (let ((org-last-indirect-window
7211 (get-buffer-window org-last-indirect-buffer)))
7212 (if org-last-indirect-window
7213 (delete-window org-last-indirect-window))))
7214 (if org-agenda-columns-active
7215 (org-columns-quit)
7216 (if org-agenda-sticky
7217 (let ((buf (current-buffer)))
7218 (if (eq org-agenda-window-setup 'other-frame)
7219 (progn
7220 (delete-frame))
7221 (and (not (eq org-agenda-window-setup 'current-window))
7222 (not (one-window-p))
7223 (delete-window)))
7224 (with-current-buffer buf
7225 (bury-buffer)
7226 ;; Maybe restore the pre-agenda window configuration.
7227 (and org-agenda-restore-windows-after-quit
7228 (not (eq org-agenda-window-setup 'other-frame))
7229 org-agenda-pre-window-conf
7230 (set-window-configuration org-agenda-pre-window-conf)
7231 (setq org-agenda-pre-window-conf nil))))
7232 (org-agenda-Quit))))
7234 (defun org-agenda-exit ()
7235 "Exit the agenda and restore the window configuration.
7236 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7237 buffers visited directly by the user will not be touched."
7238 (interactive)
7239 (org-release-buffers org-agenda-new-buffers)
7240 (setq org-agenda-new-buffers nil)
7241 (org-agenda-Quit))
7243 (defun org-agenda-kill-all-agenda-buffers ()
7244 "Kill all buffers in `org-agenda-mode'.
7245 This is used when toggling sticky agendas.
7246 You can also explicitly invoke it with `C-c a C-k'."
7247 (interactive)
7248 (let (blist)
7249 (dolist (buf (buffer-list))
7250 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7251 (push buf blist)))
7252 (mapc 'kill-buffer blist)))
7254 (defun org-agenda-execute (arg)
7255 "Execute another agenda command, keeping same window.
7256 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7257 in the agenda."
7258 (interactive "P")
7259 (let ((org-agenda-window-setup 'current-window))
7260 (org-agenda arg)))
7262 (defun org-agenda-redo (&optional all)
7263 "Rebuild possibly ALL agenda view(s) in the current buffer."
7264 (interactive "P")
7265 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7266 (cpa (unless (eq all t) current-prefix-arg))
7267 (org-agenda-doing-sticky-redo org-agenda-sticky)
7268 (org-agenda-sticky nil)
7269 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7270 org-agenda-buffer-name))
7271 (org-agenda-keep-modes t)
7272 (tag-filter org-agenda-tag-filter)
7273 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7274 (top-hl-filter org-agenda-top-headline-filter)
7275 (cat-filter org-agenda-category-filter)
7276 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7277 (re-filter org-agenda-regexp-filter)
7278 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7279 (effort-filter org-agenda-effort-filter)
7280 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7281 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7282 (cols org-agenda-columns-active)
7283 (line (org-current-line))
7284 (window-line (- line (org-current-line (window-start))))
7285 (lprops (get 'org-agenda-redo-command 'org-lprops))
7286 (redo-cmd (get-text-property p 'org-redo-cmd))
7287 (last-args (get-text-property p 'org-last-args))
7288 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7289 (org-agenda-overriding-cmd-arguments
7290 (unless (eq all t)
7291 (cond ((listp last-args)
7292 (cons (or cpa (car last-args)) (cdr last-args)))
7293 ((stringp last-args)
7294 last-args))))
7295 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7296 (put 'org-agenda-tag-filter :preset-filter nil)
7297 (put 'org-agenda-category-filter :preset-filter nil)
7298 (put 'org-agenda-regexp-filter :preset-filter nil)
7299 (put 'org-agenda-effort-filter :preset-filter nil)
7300 (and cols (org-columns-quit))
7301 (message "Rebuilding agenda buffer...")
7302 (if series-redo-cmd
7303 (eval series-redo-cmd)
7304 (org-let lprops redo-cmd))
7305 (setq org-agenda-undo-list nil
7306 org-agenda-pending-undo-list nil
7307 org-agenda-tag-filter tag-filter
7308 org-agenda-category-filter cat-filter
7309 org-agenda-regexp-filter re-filter
7310 org-agenda-effort-filter effort-filter
7311 org-agenda-top-headline-filter top-hl-filter)
7312 (message "Rebuilding agenda buffer...done")
7313 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7314 (put 'org-agenda-category-filter :preset-filter cat-preset)
7315 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7316 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7317 (let ((tag (or tag-filter tag-preset))
7318 (cat (or cat-filter cat-preset))
7319 (effort (or effort-filter effort-preset))
7320 (re (or re-filter re-preset)))
7321 (when tag (org-agenda-filter-apply tag 'tag))
7322 (when cat (org-agenda-filter-apply cat 'category))
7323 (when effort (org-agenda-filter-apply effort 'effort))
7324 (when re (org-agenda-filter-apply re 'regexp)))
7325 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7326 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7327 (org-goto-line line)
7328 (recenter window-line)))
7330 (defvar org-global-tags-completion-table nil)
7331 (defvar org-agenda-filter-form nil)
7332 (defvar org-agenda-filtered-by-category nil)
7334 (defun org-agenda-filter-by-category (strip)
7335 "Keep only those lines in the agenda buffer that have a specific category.
7336 The category is that of the current line."
7337 (interactive "P")
7338 (if (and org-agenda-filtered-by-category
7339 org-agenda-category-filter)
7340 (org-agenda-filter-show-all-cat)
7341 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7342 (cond
7343 ((and cat strip)
7344 (org-agenda-filter-apply
7345 (push (concat "-" cat) org-agenda-category-filter) 'category))
7346 ((and cat)
7347 (org-agenda-filter-apply
7348 (setq org-agenda-category-filter
7349 (list (concat "+" cat))) 'category))
7350 (t (error "No category at point"))))))
7352 (defun org-find-top-headline (&optional pos)
7353 "Find the topmost parent headline and return it."
7354 (save-excursion
7355 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7356 (if pos (goto-char pos))
7357 ;; Skip up to the topmost parent
7358 (while (ignore-errors (outline-up-heading 1) t))
7359 (ignore-errors
7360 (nth 4 (org-heading-components))))))
7362 (defvar org-agenda-filtered-by-top-headline nil)
7363 (defun org-agenda-filter-by-top-headline (strip)
7364 "Keep only those lines that are descendants from the same top headline.
7365 The top headline is that of the current line."
7366 (interactive "P")
7367 (if org-agenda-filtered-by-top-headline
7368 (progn
7369 (setq org-agenda-filtered-by-top-headline nil
7370 org-agenda-top-headline-filter nil)
7371 (org-agenda-filter-show-all-top-filter))
7372 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7373 (if toph (org-agenda-filter-top-headline-apply toph strip)
7374 (error "No top-level headline at point")))))
7376 (defvar org-agenda-regexp-filter nil)
7377 (defun org-agenda-filter-by-regexp (strip)
7378 "Filter agenda entries by a regular expression.
7379 Regexp filters are cumulative.
7380 With no prefix argument, keep entries matching the regexp.
7381 With one prefix argument, filter out entries matching the regexp.
7382 With two prefix arguments, remove the regexp filters."
7383 (interactive "P")
7384 (if (not (equal strip '(16)))
7385 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7386 (read-from-minibuffer
7387 (if (equal strip '(4))
7388 "Filter out entries matching regexp: "
7389 "Narrow to entries matching regexp: ")))))
7390 (push flt org-agenda-regexp-filter)
7391 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7392 (org-agenda-filter-show-all-re)
7393 (message "Regexp filter removed")))
7395 (defvar org-agenda-effort-filter nil)
7396 (defun org-agenda-filter-by-effort (strip)
7397 "Filter agenda entries by effort.
7398 With no prefix argument, keep entries matching the effort condition.
7399 With one prefix argument, filter out entries matching the condition.
7400 With two prefix arguments, remove the effort filters."
7401 (interactive "P")
7402 (cond ((member strip '(nil 4))
7403 (let ((efforts (org-split-string
7404 (or (cdr (assoc (concat org-effort-property "_ALL")
7405 org-global-properties))
7406 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7407 "")))
7408 (eff -1)
7409 effort-prompt op)
7410 (while (not (member op '(?< ?> ?=)))
7411 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7412 (loop for i from 0 to 9 do
7413 (setq effort-prompt
7414 (concat
7415 effort-prompt " ["
7416 (if (= i 9) "0" (int-to-string (1+ i)))
7417 "]" (nth i efforts))))
7418 (message "Effort %s%s" (char-to-string op) effort-prompt)
7419 (while (or (< eff 0) (> eff 9))
7420 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7421 (setq org-agenda-effort-filter
7422 (list (concat (if strip "-" "+")
7423 (char-to-string op) (nth (1- eff) efforts))))
7424 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7425 (t (org-agenda-filter-show-all-effort)
7426 (message "Effort filter removed"))))
7428 (defun org-agenda-filter-remove-all ()
7429 "Remove all filters from the current agenda buffer."
7430 (interactive)
7431 (when org-agenda-tag-filter
7432 (org-agenda-filter-show-all-tag))
7433 (when org-agenda-category-filter
7434 (org-agenda-filter-show-all-cat))
7435 (when org-agenda-regexp-filter
7436 (org-agenda-filter-show-all-re))
7437 (when org-agenda-top-headline-filter
7438 (org-agenda-filter-show-all-top-filter))
7439 (when org-agenda-effort-filter
7440 (org-agenda-filter-show-all-effort))
7441 (org-agenda-finalize))
7443 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7444 "Keep only those lines in the agenda buffer that have a specific tag.
7445 The tag is selected with its fast selection letter, as configured.
7446 With prefix argument STRIP, remove all lines that do have the tag.
7447 A lisp caller can specify CHAR. NARROW means that the new tag should be
7448 used to narrow the search - the interactive user can also press `-' or `+'
7449 to switch to narrowing."
7450 (interactive "P")
7451 (let* ((alist org-tag-alist-for-agenda)
7452 (tag-chars (mapconcat
7453 (lambda (x) (if (and (not (symbolp (car x)))
7454 (cdr x))
7455 (char-to-string (cdr x))
7456 ""))
7457 alist ""))
7458 (inhibit-read-only t)
7459 (current org-agenda-tag-filter)
7460 maybe-refresh a n tag)
7461 (unless char
7462 (message
7463 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow"
7464 (if narrow "Narrow" "Filter") tag-chars
7465 (if org-agenda-auto-exclude-function "[RET], " ""))
7466 (setq char (read-char-exclusive)))
7467 (when (member char '(?+ ?-))
7468 ;; Narrowing down
7469 (cond ((equal char ?-) (setq strip t narrow t))
7470 ((equal char ?+) (setq strip nil narrow t)))
7471 (message
7472 "Narrow by tag [%s ], [TAB], [/]:off" tag-chars)
7473 (setq char (read-char-exclusive)))
7474 (when (equal char ?\t)
7475 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7476 (org-set-local 'org-global-tags-completion-table
7477 (org-global-tags-completion-table)))
7478 (let ((completion-ignore-case t))
7479 (setq tag (org-icompleting-read
7480 "Tag: " org-global-tags-completion-table))))
7481 (cond
7482 ((equal char ?\r)
7483 (org-agenda-filter-show-all-tag)
7484 (when org-agenda-auto-exclude-function
7485 (setq org-agenda-tag-filter nil)
7486 (dolist (tag (org-agenda-get-represented-tags))
7487 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7488 (if modifier
7489 (push modifier org-agenda-tag-filter))))
7490 (if (not (null org-agenda-tag-filter))
7491 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7492 (setq maybe-refresh t))
7493 ((equal char ?/)
7494 (org-agenda-filter-show-all-tag)
7495 (when (get 'org-agenda-tag-filter :preset-filter)
7496 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7497 (setq maybe-refresh t))
7498 ((equal char ?. )
7499 (setq org-agenda-tag-filter
7500 (mapcar (lambda(tag) (concat "+" tag))
7501 (org-get-at-bol 'tags)))
7502 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7503 (setq maybe-refresh t))
7504 ((or (equal char ?\ )
7505 (setq a (rassoc char alist))
7506 (and tag (setq a (cons tag nil))))
7507 (org-agenda-filter-show-all-tag)
7508 (setq tag (car a))
7509 (setq org-agenda-tag-filter
7510 (cons (concat (if strip "-" "+") tag)
7511 (if narrow current nil)))
7512 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7513 (setq maybe-refresh t))
7514 (t (error "Invalid tag selection character %c" char)))
7515 (when (and maybe-refresh
7516 (eq org-agenda-clockreport-mode 'with-filter))
7517 (org-agenda-redo))))
7519 (defun org-agenda-get-represented-tags ()
7520 "Get a list of all tags currently represented in the agenda."
7521 (let (p tags)
7522 (save-excursion
7523 (goto-char (point-min))
7524 (while (setq p (next-single-property-change (point) 'tags))
7525 (goto-char p)
7526 (mapc (lambda (x) (add-to-list 'tags x))
7527 (get-text-property (point) 'tags))))
7528 tags))
7530 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7531 "Refine the current filter. See `org-agenda-filter-by-tag'."
7532 (interactive "P")
7533 (org-agenda-filter-by-tag strip char 'refine))
7535 (defun org-agenda-filter-make-matcher (filter type)
7536 "Create the form that tests a line for agenda filter."
7537 (let (f f1)
7538 (cond
7539 ;; Tag filter
7540 ((eq type 'tag)
7541 (setq filter
7542 (delete-dups
7543 (append (get 'org-agenda-tag-filter :preset-filter)
7544 filter)))
7545 (dolist (x filter)
7546 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7547 (ffunc
7548 (lambda (nf0 nf01 fltr notgroup op)
7549 (dolist (x fltr)
7550 (if (member x '("-" "+"))
7551 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7552 (setq nf01 (list 'member (downcase (substring x 1))
7553 'tags))
7554 (when (equal (string-to-char x) ?-)
7555 (setq nf01 (list 'not nf01))
7556 (when (not notgroup) (setq op 'and))))
7557 (push nf01 nf0))
7558 (if notgroup
7559 (push (cons 'and nf0) f)
7560 (push (cons (or op 'or) nf0) f)))))
7561 (cond ((equal filter '("+"))
7562 (setq f (list (list 'not 'tags))))
7563 ((equal nfilter filter)
7564 (funcall ffunc f1 f filter t nil))
7565 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7566 ;; Category filter
7567 ((eq type 'category)
7568 (setq filter
7569 (delete-dups
7570 (append (get 'org-agenda-category-filter :preset-filter)
7571 filter)))
7572 (dolist (x filter)
7573 (if (equal "-" (substring x 0 1))
7574 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7575 (setq f1 (list 'equal (substring x 1) 'cat)))
7576 (push f1 f)))
7577 ;; Regexp filter
7578 ((eq type 'regexp)
7579 (setq filter
7580 (delete-dups
7581 (append (get 'org-agenda-regexp-filter :preset-filter)
7582 filter)))
7583 (dolist (x filter)
7584 (if (equal "-" (substring x 0 1))
7585 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7586 (setq f1 (list 'string-match (substring x 1) 'txt)))
7587 (push f1 f)))
7588 ;; Effort filter
7589 ((eq type 'effort)
7590 (setq filter
7591 (delete-dups
7592 (append (get 'org-agenda-effort-filter :preset-filter)
7593 filter)))
7594 (dolist (x filter)
7595 (push (org-agenda-filter-effort-form x) f))))
7596 (cons 'and (nreverse f))))
7598 (defun org-agenda-filter-effort-form (e)
7599 "Return the form to compare the effort of the current line with what E says.
7600 E looks like \"+<2:25\"."
7601 (let (op)
7602 (setq e (substring e 1))
7603 (setq op (string-to-char e) e (substring e 1))
7604 (setq op (cond ((equal op ?<) '<=)
7605 ((equal op ?>) '>=)
7606 ((equal op ??) op)
7607 (t '=)))
7608 (list 'org-agenda-compare-effort (list 'quote op)
7609 (org-duration-string-to-minutes e))))
7611 (defun org-agenda-compare-effort (op value)
7612 "Compare the effort of the current line with VALUE, using OP.
7613 If the line does not have an effort defined, return nil."
7614 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7615 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7616 value)))
7618 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7619 "Expand group tags in FILTER for the agenda.
7620 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7621 (if org-group-tags
7622 (let ((case-fold-search t) rtn)
7623 (mapc
7624 (lambda (f)
7625 (let (f0 dir)
7626 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7627 (setq dir (match-string 1 f) f0 (match-string 2 f))
7628 (setq dir (if no-operator "" "+") f0 f))
7629 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7630 (org-tags-expand f0 t t))
7631 rtn))))
7632 filter)
7633 (reverse rtn))
7634 filter))
7636 (defun org-agenda-filter-apply (filter type)
7637 "Set FILTER as the new agenda filter and apply it."
7638 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7639 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7640 (let (tags cat txt)
7641 (setq org-agenda-filter-form
7642 (org-agenda-filter-make-matcher filter type))
7643 (if (and (eq type 'category)
7644 (not (equal (substring (car filter) 0 1) "-")))
7645 ;; Only set `org-agenda-filtered-by-category' to t
7646 ;; when a unique category is used as the filter
7647 (setq org-agenda-filtered-by-category t))
7648 (org-agenda-set-mode-name)
7649 (save-excursion
7650 (goto-char (point-min))
7651 (while (not (eobp))
7652 (if (org-get-at-bol 'org-marker)
7653 (progn
7654 (setq tags ; used in eval
7655 (apply 'append
7656 (mapcar (lambda (f)
7657 (org-agenda-filter-expand-tags (list f) t))
7658 (org-get-at-bol 'tags)))
7659 cat (org-get-at-eol 'org-category 1)
7660 txt (org-get-at-eol 'txt 1))
7661 (if (not (eval org-agenda-filter-form))
7662 (org-agenda-filter-hide-line type))
7663 (beginning-of-line 2))
7664 (beginning-of-line 2))))
7665 (if (get-char-property (point) 'invisible)
7666 (ignore-errors (org-agenda-previous-line)))))
7668 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7669 "Filter by top headline HL."
7670 (org-agenda-set-mode-name)
7671 (save-excursion
7672 (goto-char (point-min))
7673 (while (not (eobp))
7674 (let* ((pos (org-get-at-bol 'org-hd-marker))
7675 (tophl (and pos (org-find-top-headline pos))))
7676 (if (and tophl (funcall (if negative 'identity 'not)
7677 (string= hl tophl)))
7678 (org-agenda-filter-hide-line 'top-headline)))
7679 (beginning-of-line 2)))
7680 (if (get-char-property (point) 'invisible)
7681 (org-agenda-previous-line))
7682 (setq org-agenda-top-headline-filter hl
7683 org-agenda-filtered-by-top-headline t))
7685 (defun org-agenda-filter-hide-line (type)
7686 "Hide lines with TYPE in the agenda buffer."
7687 (let* ((b (max (point-min) (1- (point-at-bol))))
7688 (e (point-at-eol)))
7689 (let ((inhibit-read-only t))
7690 (add-text-properties
7691 b e `(invisible org-filtered org-filter-type ,type)))))
7693 (defun org-agenda-remove-filter (type)
7694 (interactive)
7695 "Remove filter of type TYPE from the agenda buffer."
7696 (save-excursion
7697 (goto-char (point-min))
7698 (let ((inhibit-read-only t) pos)
7699 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7700 (goto-char pos)
7701 (remove-text-properties
7702 (point) (next-single-property-change (point) 'org-filter-type)
7703 `(invisible org-filtered org-filter-type ,type))))
7704 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7705 (setq org-agenda-filter-form nil)
7706 (org-agenda-set-mode-name)
7707 (org-agenda-finalize)))
7709 (defun org-agenda-filter-show-all-tag nil
7710 (org-agenda-remove-filter 'tag))
7711 (defun org-agenda-filter-show-all-re nil
7712 (org-agenda-remove-filter 'regexp))
7713 (defun org-agenda-filter-show-all-effort nil
7714 (org-agenda-remove-filter 'effort))
7715 (defun org-agenda-filter-show-all-cat nil
7716 (org-agenda-remove-filter 'category))
7717 (defun org-agenda-filter-show-all-top-filter nil
7718 (org-agenda-remove-filter 'top-headline))
7720 (defun org-agenda-manipulate-query-add ()
7721 "Manipulate the query by adding a search term with positive selection.
7722 Positive selection means the term must be matched for selection of an entry."
7723 (interactive)
7724 (org-agenda-manipulate-query ?\[))
7725 (defun org-agenda-manipulate-query-subtract ()
7726 "Manipulate the query by adding a search term with negative selection.
7727 Negative selection means term must not be matched for selection of an entry."
7728 (interactive)
7729 (org-agenda-manipulate-query ?\]))
7730 (defun org-agenda-manipulate-query-add-re ()
7731 "Manipulate the query by adding a search regexp with positive selection.
7732 Positive selection means the regexp must match for selection of an entry."
7733 (interactive)
7734 (org-agenda-manipulate-query ?\{))
7735 (defun org-agenda-manipulate-query-subtract-re ()
7736 "Manipulate the query by adding a search regexp with negative selection.
7737 Negative selection means regexp must not match for selection of an entry."
7738 (interactive)
7739 (org-agenda-manipulate-query ?\}))
7740 (defun org-agenda-manipulate-query (char)
7741 (cond
7742 ((memq org-agenda-type '(timeline agenda))
7743 (let ((org-agenda-include-inactive-timestamps t))
7744 (org-agenda-redo))
7745 (message "Display now includes inactive timestamps as well"))
7746 ((eq org-agenda-type 'search)
7747 (org-add-to-string
7748 'org-agenda-query-string
7749 (if org-agenda-last-search-view-search-was-boolean
7750 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7751 (?\{ . " +{}") (?\} . " -{}"))))
7752 " "))
7753 (setq org-agenda-redo-command
7754 (list 'org-search-view
7755 (car (get-text-property (min (1- (point-max)) (point))
7756 'org-last-args))
7757 org-agenda-query-string
7758 (+ (length org-agenda-query-string)
7759 (if (member char '(?\{ ?\})) 0 1))))
7760 (set-register org-agenda-query-register org-agenda-query-string)
7761 (let ((org-agenda-overriding-arguments
7762 (cdr org-agenda-redo-command)))
7763 (org-agenda-redo)))
7764 (t (error "Cannot manipulate query for %s-type agenda buffers"
7765 org-agenda-type))))
7767 (defun org-add-to-string (var string)
7768 (set var (concat (symbol-value var) string)))
7770 (defun org-agenda-goto-date (span)
7771 "Jump to DATE in agenda."
7772 (interactive "P")
7773 (let* ((org-read-date-prefer-future
7774 (eval org-agenda-jump-prefer-future))
7775 (date (org-read-date))
7776 (day (time-to-days (org-time-string-to-time date)))
7777 (org-agenda-sticky-orig org-agenda-sticky)
7778 (org-agenda-buffer-tmp-name (buffer-name))
7779 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7780 (0-arg (or current-prefix-arg (car args)))
7781 (2-arg (nth 2 args))
7782 (with-hour-p (nth 4 org-agenda-redo-command))
7783 (newcmd (list 'org-agenda-list 0-arg date
7784 (org-agenda-span-to-ndays
7785 2-arg (org-time-string-to-absolute date))
7786 with-hour-p))
7787 (newargs (cdr newcmd))
7788 (inhibit-read-only t)
7789 org-agenda-sticky)
7790 (if (not (org-agenda-check-type t 'agenda))
7791 (error "Not available in non-agenda views")
7792 (add-text-properties (point-min) (point-max)
7793 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7794 (org-agenda-redo)
7795 (goto-char (point-min))
7796 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7797 (save-excursion (move-beginning-of-line 2) (eobp))))
7798 (move-beginning-of-line 2))
7799 (setq org-agenda-sticky org-agenda-sticky-orig
7800 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7802 (defun org-agenda-goto-today ()
7803 "Go to today."
7804 (interactive)
7805 (org-agenda-check-type t 'timeline 'agenda)
7806 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7807 (curspan (nth 2 args))
7808 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7809 (cond
7810 (tdpos (goto-char tdpos))
7811 ((eq org-agenda-type 'agenda)
7812 (let* ((sd (org-agenda-compute-starting-span
7813 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7814 (org-agenda-overriding-arguments args))
7815 (setf (nth 1 org-agenda-overriding-arguments) sd)
7816 (org-agenda-redo)
7817 (org-agenda-find-same-or-today-or-agenda)))
7818 (t (error "Cannot find today")))))
7820 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7821 (goto-char
7822 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7823 (text-property-any (point-min) (point-max) 'org-today t)
7824 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7825 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7826 (org-agenda-backward-block))
7827 (point-min))))
7829 (defun org-agenda-backward-block ()
7830 "Move backward by one agenda block."
7831 (interactive)
7832 (org-agenda-forward-block 'backward))
7834 (defun org-agenda-forward-block (&optional backward)
7835 "Move forward by one agenda block.
7836 When optional argument BACKWARD is set, go backward"
7837 (interactive)
7838 (cond ((not (derived-mode-p 'org-agenda-mode))
7839 (user-error
7840 "Cannot execute this command outside of org-agenda-mode buffers"))
7841 ((looking-at (if backward "\\`" "\\'"))
7842 (message "Already at the %s block" (if backward "first" "last")))
7843 (t (let ((pos (prog1 (point)
7844 (ignore-errors (if backward (backward-char 1)
7845 (move-end-of-line 1)))))
7846 (f (if backward
7847 'previous-single-property-change
7848 'next-single-property-change))
7849 moved dest)
7850 (while (and (setq dest (funcall
7851 f (point) 'org-agenda-structural-header))
7852 (not (get-text-property
7853 (point) 'org-agenda-structural-header)))
7854 (setq moved t)
7855 (goto-char dest))
7856 (if moved (move-beginning-of-line 1)
7857 (goto-char (if backward (point-min) (point-max)))
7858 (move-beginning-of-line 1)
7859 (message "No %s block" (if backward "previous" "further")))))))
7861 (defun org-agenda-later (arg)
7862 "Go forward in time by the current span.
7863 With prefix ARG, go forward that many times the current span."
7864 (interactive "p")
7865 (org-agenda-check-type t 'agenda)
7866 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7867 (span (or (nth 2 args) org-agenda-current-span))
7868 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7869 (greg (calendar-gregorian-from-absolute sd))
7870 (cnt (org-get-at-bol 'org-day-cnt))
7871 greg2)
7872 (cond
7873 ((numberp span)
7874 (setq sd (+ (* span arg) sd)))
7875 ((eq span 'day)
7876 (setq sd (+ arg sd)))
7877 ((eq span 'week)
7878 (setq sd (+ (* 7 arg) sd)))
7879 ((eq span 'fortnight)
7880 (setq sd (+ (* 14 arg) sd)))
7881 ((eq span 'month)
7882 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7883 sd (calendar-absolute-from-gregorian greg2))
7884 (setcar greg2 (1+ (car greg2))))
7885 ((eq span 'year)
7886 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7887 sd (calendar-absolute-from-gregorian greg2))
7888 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7890 (setq sd (+ (* span arg) sd))))
7891 (let ((org-agenda-overriding-cmd
7892 ;; `cmd' may have been set by `org-agenda-run-series' which
7893 ;; uses `org-agenda-overriding-cmd' to decide whether
7894 ;; overriding is allowed for `cmd'
7895 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7896 (org-agenda-overriding-arguments
7897 (list (car args) sd span)))
7898 (org-agenda-redo)
7899 (org-agenda-find-same-or-today-or-agenda cnt))))
7901 (defun org-agenda-earlier (arg)
7902 "Go backward in time by the current span.
7903 With prefix ARG, go backward that many times the current span."
7904 (interactive "p")
7905 (org-agenda-later (- arg)))
7907 (defun org-agenda-view-mode-dispatch ()
7908 "Call one of the view mode commands."
7909 (interactive)
7910 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7911 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7912 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7913 (let ((a (read-char-exclusive)))
7914 (case a
7915 (?\ (call-interactively 'org-agenda-reset-view))
7916 (?d (call-interactively 'org-agenda-day-view))
7917 (?w (call-interactively 'org-agenda-week-view))
7918 (?t (call-interactively 'org-agenda-fortnight-view))
7919 (?m (call-interactively 'org-agenda-month-view))
7920 (?y (call-interactively 'org-agenda-year-view))
7921 (?l (call-interactively 'org-agenda-log-mode))
7922 (?L (org-agenda-log-mode '(4)))
7923 (?c (org-agenda-log-mode 'clockcheck))
7924 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7925 (?a (call-interactively 'org-agenda-archives-mode))
7926 (?A (org-agenda-archives-mode 'files))
7927 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7928 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7929 (?G (call-interactively 'org-agenda-toggle-time-grid))
7930 (?D (call-interactively 'org-agenda-toggle-diary))
7931 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7932 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7933 (org-agenda-check-type t 'timeline 'agenda)
7934 (org-agenda-redo))
7935 (message "Display now includes inactive timestamps as well"))
7936 (?q (message "Abort"))
7937 (otherwise (error "Invalid key" )))))
7939 (defun org-agenda-reset-view ()
7940 "Switch to default view for agenda."
7941 (interactive)
7942 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7943 (defun org-agenda-day-view (&optional day-of-month)
7944 "Switch to daily view for agenda.
7945 With argument DAY-OF-MONTH, switch to that day of the month."
7946 (interactive "P")
7947 (org-agenda-change-time-span 'day day-of-month))
7948 (defun org-agenda-week-view (&optional iso-week)
7949 "Switch to daily view for agenda.
7950 With argument ISO-WEEK, switch to the corresponding ISO week.
7951 If ISO-WEEK has more then 2 digits, only the last two encode the
7952 week. Any digits before this encode a year. So 200712 means
7953 week 12 of year 2007. Years in the range 1938-2037 can also be
7954 written as 2-digit years."
7955 (interactive "P")
7956 (org-agenda-change-time-span 'week iso-week))
7957 (defun org-agenda-fortnight-view (&optional iso-week)
7958 "Switch to daily view for agenda.
7959 With argument ISO-WEEK, switch to the corresponding ISO week.
7960 If ISO-WEEK has more then 2 digits, only the last two encode the
7961 week. Any digits before this encode a year. So 200712 means
7962 week 12 of year 2007. Years in the range 1938-2037 can also be
7963 written as 2-digit years."
7964 (interactive "P")
7965 (org-agenda-change-time-span 'fortnight iso-week))
7966 (defun org-agenda-month-view (&optional month)
7967 "Switch to monthly view for agenda.
7968 With argument MONTH, switch to that month."
7969 (interactive "P")
7970 (org-agenda-change-time-span 'month month))
7971 (defun org-agenda-year-view (&optional year)
7972 "Switch to yearly view for agenda.
7973 With argument YEAR, switch to that year.
7974 If MONTH has more then 2 digits, only the last two encode the
7975 month. Any digits before this encode a year. So 200712 means
7976 December year 2007. Years in the range 1938-2037 can also be
7977 written as 2-digit years."
7978 (interactive "P")
7979 (when year
7980 (setq year (org-small-year-to-year year)))
7981 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7982 (org-agenda-change-time-span 'year year)
7983 (error "Abort")))
7985 (defun org-agenda-change-time-span (span &optional n)
7986 "Change the agenda view to SPAN.
7987 SPAN may be `day', `week', `fortnight', `month', `year'."
7988 (org-agenda-check-type t 'agenda)
7989 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7990 (curspan (nth 2 args)))
7991 (if (and (not n) (equal curspan span))
7992 (error "Viewing span is already \"%s\"" span))
7993 (let* ((sd (or (org-get-at-bol 'day)
7994 (nth 1 args)
7995 org-starting-day))
7996 (sd (org-agenda-compute-starting-span sd span n))
7997 (org-agenda-overriding-cmd
7998 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7999 (org-agenda-overriding-arguments
8000 (list (car args) sd span)))
8001 (org-agenda-redo)
8002 (org-agenda-find-same-or-today-or-agenda))
8003 (org-agenda-set-mode-name)
8004 (message "Switched to %s view" span)))
8006 (defun org-agenda-compute-starting-span (sd span &optional n)
8007 "Compute starting date for agenda.
8008 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8009 is a cons cell with the starting date and the number of days,
8010 so that the date SD will be in that range."
8011 (let* ((greg (calendar-gregorian-from-absolute sd))
8012 (dg (nth 1 greg))
8013 (mg (car greg))
8014 (yg (nth 2 greg)))
8015 (cond
8016 ((eq span 'day)
8017 (when n
8018 (setq sd (+ (calendar-absolute-from-gregorian
8019 (list mg 1 yg))
8020 n -1))))
8021 ((or (eq span 'week) (eq span 'fortnight))
8022 (let* ((nt (calendar-day-of-week
8023 (calendar-gregorian-from-absolute sd)))
8024 (d (if org-agenda-start-on-weekday
8025 (- nt org-agenda-start-on-weekday)
8028 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8029 (when n
8030 (require 'cal-iso)
8031 (when (> n 99)
8032 (setq y1 (org-small-year-to-year (/ n 100))
8033 n (mod n 100)))
8034 (setq sd
8035 (calendar-absolute-from-iso
8036 (list n 1
8037 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8038 ((eq span 'month)
8039 (let (y1)
8040 (when (and n (> n 99))
8041 (setq y1 (org-small-year-to-year (/ n 100))
8042 n (mod n 100)))
8043 (setq sd (calendar-absolute-from-gregorian
8044 (list (or n mg) 1 (or y1 yg))))))
8045 ((eq span 'year)
8046 (setq sd (calendar-absolute-from-gregorian
8047 (list 1 1 (or n yg))))))
8048 sd))
8050 (defun org-agenda-next-date-line (&optional arg)
8051 "Jump to the next line indicating a date in agenda buffer."
8052 (interactive "p")
8053 (org-agenda-check-type t 'agenda 'timeline)
8054 (beginning-of-line 1)
8055 ;; This does not work if user makes date format that starts with a blank
8056 (if (looking-at "^\\S-") (forward-char 1))
8057 (if (not (re-search-forward "^\\S-" nil t arg))
8058 (progn
8059 (backward-char 1)
8060 (error "No next date after this line in this buffer")))
8061 (goto-char (match-beginning 0)))
8063 (defun org-agenda-previous-date-line (&optional arg)
8064 "Jump to the previous line indicating a date in agenda buffer."
8065 (interactive "p")
8066 (org-agenda-check-type t 'agenda 'timeline)
8067 (beginning-of-line 1)
8068 (if (not (re-search-backward "^\\S-" nil t arg))
8069 (error "No previous date before this line in this buffer")))
8071 ;; Initialize the highlight
8072 (defvar org-hl (make-overlay 1 1))
8073 (overlay-put org-hl 'face 'highlight)
8075 (defun org-highlight (begin end &optional buffer)
8076 "Highlight a region with overlay."
8077 (move-overlay org-hl begin end (or buffer (current-buffer))))
8079 (defun org-unhighlight ()
8080 "Detach overlay INDEX."
8081 (org-detach-overlay org-hl))
8083 (defun org-unhighlight-once ()
8084 "Remove the highlight from its position, and this function from the hook."
8085 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8086 (org-unhighlight))
8088 (defvar org-agenda-pre-follow-window-conf nil)
8089 (defun org-agenda-follow-mode ()
8090 "Toggle follow mode in an agenda buffer."
8091 (interactive)
8092 (unless org-agenda-follow-mode
8093 (setq org-agenda-pre-follow-window-conf
8094 (current-window-configuration)))
8095 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8096 (unless org-agenda-follow-mode
8097 (set-window-configuration org-agenda-pre-follow-window-conf))
8098 (org-agenda-set-mode-name)
8099 (org-agenda-do-context-action)
8100 (message "Follow mode is %s"
8101 (if org-agenda-follow-mode "on" "off")))
8103 (defun org-agenda-entry-text-mode (&optional arg)
8104 "Toggle entry text mode in an agenda buffer."
8105 (interactive "P")
8106 (if (or org-agenda-tag-filter
8107 org-agenda-category-filter
8108 org-agenda-regexp-filter
8109 org-agenda-top-headline-filter)
8110 (user-error "Can't show entry text in filtered views")
8111 (setq org-agenda-entry-text-mode (or (integerp arg)
8112 (not org-agenda-entry-text-mode)))
8113 (org-agenda-entry-text-hide)
8114 (and org-agenda-entry-text-mode
8115 (let ((org-agenda-entry-text-maxlines
8116 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8117 (org-agenda-entry-text-show)))
8118 (org-agenda-set-mode-name)
8119 (message "Entry text mode is %s%s"
8120 (if org-agenda-entry-text-mode "on" "off")
8121 (if (not org-agenda-entry-text-mode) ""
8122 (format " (maximum number of lines is %d)"
8123 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8125 (defun org-agenda-clockreport-mode (&optional with-filter)
8126 "Toggle clocktable mode in an agenda buffer.
8127 With prefix arg WITH-FILTER, make the clocktable respect the current
8128 agenda filter."
8129 (interactive "P")
8130 (org-agenda-check-type t 'agenda)
8131 (if with-filter
8132 (setq org-agenda-clockreport-mode 'with-filter)
8133 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8134 (org-agenda-set-mode-name)
8135 (org-agenda-redo)
8136 (message "Clocktable mode is %s"
8137 (if org-agenda-clockreport-mode "on" "off")))
8139 (defun org-agenda-log-mode (&optional special)
8140 "Toggle log mode in an agenda buffer.
8141 With argument SPECIAL, show all possible log items, not only the ones
8142 configured in `org-agenda-log-mode-items'.
8143 With a double `C-u' prefix arg, show *only* log items, nothing else."
8144 (interactive "P")
8145 (org-agenda-check-type t 'agenda 'timeline)
8146 (setq org-agenda-show-log
8147 (cond
8148 ((equal special '(16)) 'only)
8149 ((eq special 'clockcheck)
8150 (if (eq org-agenda-show-log 'clockcheck)
8151 nil 'clockcheck))
8152 (special '(closed clock state))
8153 (t (not org-agenda-show-log))))
8154 (org-agenda-set-mode-name)
8155 (org-agenda-redo)
8156 (message "Log mode is %s"
8157 (if org-agenda-show-log "on" "off")))
8159 (defun org-agenda-archives-mode (&optional with-files)
8160 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8161 When called with a prefix argument, include all archive files as well."
8162 (interactive "P")
8163 (setq org-agenda-archives-mode
8164 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8165 (org-agenda-set-mode-name)
8166 (org-agenda-redo)
8167 (message
8168 "%s"
8169 (cond
8170 ((eq org-agenda-archives-mode nil)
8171 "No archives are included")
8172 ((eq org-agenda-archives-mode 'trees)
8173 (format "Trees with :%s: tag are included" org-archive-tag))
8174 ((eq org-agenda-archives-mode t)
8175 (format "Trees with :%s: tag and all active archive files are included"
8176 org-archive-tag)))))
8178 (defun org-agenda-toggle-diary ()
8179 "Toggle diary inclusion in an agenda buffer."
8180 (interactive)
8181 (org-agenda-check-type t 'agenda)
8182 (setq org-agenda-include-diary (not org-agenda-include-diary))
8183 (org-agenda-redo)
8184 (org-agenda-set-mode-name)
8185 (message "Diary inclusion turned %s"
8186 (if org-agenda-include-diary "on" "off")))
8188 (defun org-agenda-toggle-deadlines ()
8189 "Toggle inclusion of entries with a deadline in an agenda buffer."
8190 (interactive)
8191 (org-agenda-check-type t 'agenda)
8192 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8193 (org-agenda-redo)
8194 (org-agenda-set-mode-name)
8195 (message "Deadlines inclusion turned %s"
8196 (if org-agenda-include-deadlines "on" "off")))
8198 (defun org-agenda-toggle-time-grid ()
8199 "Toggle time grid in an agenda buffer."
8200 (interactive)
8201 (org-agenda-check-type t 'agenda)
8202 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8203 (org-agenda-redo)
8204 (org-agenda-set-mode-name)
8205 (message "Time-grid turned %s"
8206 (if org-agenda-use-time-grid "on" "off")))
8208 (defun org-agenda-set-mode-name ()
8209 "Set the mode name to indicate all the small mode settings."
8210 (setq mode-name
8211 (list "Org-Agenda"
8212 (if (get 'org-agenda-files 'org-restrict) " []" "")
8214 '(:eval (org-agenda-span-name org-agenda-current-span))
8215 (if org-agenda-follow-mode " Follow" "")
8216 (if org-agenda-entry-text-mode " ETxt" "")
8217 (if org-agenda-include-diary " Diary" "")
8218 (if org-agenda-include-deadlines " Ddl" "")
8219 (if org-agenda-use-time-grid " Grid" "")
8220 (if (and (boundp 'org-habit-show-habits)
8221 org-habit-show-habits) " Habit" "")
8222 (cond
8223 ((consp org-agenda-show-log) " LogAll")
8224 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8225 (org-agenda-show-log " Log")
8226 (t ""))
8227 (if (or org-agenda-category-filter
8228 (get 'org-agenda-category-filter :preset-filter))
8229 '(:eval (org-propertize
8230 (concat " <"
8231 (mapconcat
8232 'identity
8233 (append
8234 (get 'org-agenda-category-filter :preset-filter)
8235 org-agenda-category-filter)
8237 ">")
8238 'face 'org-agenda-filter-category
8239 'help-echo "Category used in filtering")) "")
8240 (if (or org-agenda-tag-filter
8241 (get 'org-agenda-tag-filter :preset-filter))
8242 '(:eval (org-propertize
8243 (concat " {"
8244 (mapconcat
8245 'identity
8246 (append
8247 (get 'org-agenda-tag-filter :preset-filter)
8248 org-agenda-tag-filter)
8250 "}")
8251 'face 'org-agenda-filter-tags
8252 'help-echo "Tags used in filtering")) "")
8253 (if (or org-agenda-effort-filter
8254 (get 'org-agenda-effort-filter :preset-filter))
8255 '(:eval (org-propertize
8256 (concat " {"
8257 (mapconcat
8258 'identity
8259 (append
8260 (get 'org-agenda-effort-filter :preset-filter)
8261 org-agenda-effort-filter)
8263 "}")
8264 'face 'org-agenda-filter-effort
8265 'help-echo "Effort conditions used in filtering")) "")
8266 (if (or org-agenda-regexp-filter
8267 (get 'org-agenda-regexp-filter :preset-filter))
8268 '(:eval (org-propertize
8269 (concat " ["
8270 (mapconcat
8271 'identity
8272 (append
8273 (get 'org-agenda-regexp-filter :preset-filter)
8274 org-agenda-regexp-filter)
8276 "]")
8277 'face 'org-agenda-filter-regexp
8278 'help-echo "Regexp used in filtering")) "")
8279 (if org-agenda-archives-mode
8280 (if (eq org-agenda-archives-mode t)
8281 " Archives"
8282 (format " :%s:" org-archive-tag))
8284 (if org-agenda-clockreport-mode
8285 (if (eq org-agenda-clockreport-mode 'with-filter)
8286 " Clock{}" " Clock")
8287 "")))
8288 (force-mode-line-update))
8290 (define-obsolete-function-alias
8291 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8293 (defun org-agenda-update-agenda-type ()
8294 "Update the agenda type after each command."
8295 (setq org-agenda-type
8296 (or (get-text-property (point) 'org-agenda-type)
8297 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8299 (defun org-agenda-next-line ()
8300 "Move cursor to the next line, and show if follow mode is active."
8301 (interactive)
8302 (call-interactively 'next-line)
8303 (org-agenda-do-context-action))
8305 (defun org-agenda-previous-line ()
8306 "Move cursor to the previous line, and show if follow-mode is active."
8307 (interactive)
8308 (call-interactively 'previous-line)
8309 (org-agenda-do-context-action))
8311 (defun org-agenda-next-item (n)
8312 "Move cursor to next agenda item."
8313 (interactive "p")
8314 (let ((col (current-column)))
8315 (dotimes (c n)
8316 (when (next-single-property-change (point-at-eol) 'org-marker)
8317 (move-end-of-line 1)
8318 (goto-char (next-single-property-change (point) 'org-marker))))
8319 (org-move-to-column col))
8320 (org-agenda-do-context-action))
8322 (defun org-agenda-previous-item (n)
8323 "Move cursor to next agenda item."
8324 (interactive "p")
8325 (dotimes (c n)
8326 (let ((col (current-column))
8327 (goto (save-excursion
8328 (move-end-of-line 0)
8329 (previous-single-property-change (point) 'org-marker))))
8330 (if goto (goto-char goto))
8331 (org-move-to-column col)))
8332 (org-agenda-do-context-action))
8334 (defun org-agenda-do-context-action ()
8335 "Show outline path and, maybe, follow mode window."
8336 (let ((m (org-get-at-bol 'org-marker)))
8337 (when (and (markerp m) (marker-buffer m))
8338 (and org-agenda-follow-mode
8339 (if org-agenda-follow-indirect
8340 (org-agenda-tree-to-indirect-buffer nil)
8341 (org-agenda-show)))
8342 (and org-agenda-show-outline-path
8343 (org-with-point-at m (org-display-outline-path t))))))
8345 (defun org-agenda-show-tags ()
8346 "Show the tags applicable to the current item."
8347 (interactive)
8348 (let* ((tags (org-get-at-bol 'tags)))
8349 (if tags
8350 (message "Tags are :%s:"
8351 (org-no-properties (mapconcat 'identity tags ":")))
8352 (message "No tags associated with this line"))))
8354 (defun org-agenda-goto (&optional highlight)
8355 "Go to the entry at point in the corresponding Org-mode file."
8356 (interactive)
8357 (let* ((marker (or (org-get-at-bol 'org-marker)
8358 (org-agenda-error)))
8359 (buffer (marker-buffer marker))
8360 (pos (marker-position marker)))
8361 (switch-to-buffer-other-window buffer)
8362 (widen)
8363 (push-mark)
8364 (goto-char pos)
8365 (when (derived-mode-p 'org-mode)
8366 (org-show-context 'agenda)
8367 (save-excursion
8368 (and (outline-next-heading)
8369 (org-flag-heading nil)))) ; show the next heading
8370 (when (outline-invisible-p)
8371 (show-entry)) ; display invisible text
8372 (recenter (/ (window-height) 2))
8373 (org-back-to-heading t)
8374 (if (re-search-forward org-complex-heading-regexp nil t)
8375 (goto-char (match-beginning 4)))
8376 (run-hooks 'org-agenda-after-show-hook)
8377 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8379 (defvar org-agenda-after-show-hook nil
8380 "Normal hook run after an item has been shown from the agenda.
8381 Point is in the buffer where the item originated.")
8383 (defun org-agenda-kill ()
8384 "Kill the entry or subtree belonging to the current agenda entry."
8385 (interactive)
8386 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8387 (let* ((bufname-orig (buffer-name))
8388 (marker (or (org-get-at-bol 'org-marker)
8389 (org-agenda-error)))
8390 (buffer (marker-buffer marker))
8391 (pos (marker-position marker))
8392 (type (org-get-at-bol 'type))
8393 dbeg dend (n 0) conf)
8394 (org-with-remote-undo buffer
8395 (with-current-buffer buffer
8396 (save-excursion
8397 (goto-char pos)
8398 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8399 (setq dbeg (progn (org-back-to-heading t) (point))
8400 dend (org-end-of-subtree t t))
8401 (setq dbeg (point-at-bol)
8402 dend (min (point-max) (1+ (point-at-eol)))))
8403 (goto-char dbeg)
8404 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8405 (setq conf (or (eq t org-agenda-confirm-kill)
8406 (and (numberp org-agenda-confirm-kill)
8407 (> n org-agenda-confirm-kill))))
8408 (and conf
8409 (not (y-or-n-p
8410 (format "Delete entry with %d lines in buffer \"%s\"? "
8411 n (buffer-name buffer))))
8412 (error "Abort"))
8413 (let ((org-agenda-buffer-name bufname-orig))
8414 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8415 (with-current-buffer buffer (delete-region dbeg dend))
8416 (message "Agenda item and source killed"))))
8418 (defvar org-archive-default-command) ; defined in org-archive.el
8419 (defun org-agenda-archive-default ()
8420 "Archive the entry or subtree belonging to the current agenda entry."
8421 (interactive)
8422 (require 'org-archive)
8423 (org-agenda-archive-with org-archive-default-command))
8425 (defun org-agenda-archive-default-with-confirmation ()
8426 "Archive the entry or subtree belonging to the current agenda entry."
8427 (interactive)
8428 (require 'org-archive)
8429 (org-agenda-archive-with org-archive-default-command 'confirm))
8431 (defun org-agenda-archive ()
8432 "Archive the entry or subtree belonging to the current agenda entry."
8433 (interactive)
8434 (org-agenda-archive-with 'org-archive-subtree))
8436 (defun org-agenda-archive-to-archive-sibling ()
8437 "Move the entry to the archive sibling."
8438 (interactive)
8439 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8441 (defun org-agenda-archive-with (cmd &optional confirm)
8442 "Move the entry to the archive sibling."
8443 (interactive)
8444 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8445 (let* ((bufname-orig (buffer-name))
8446 (marker (or (org-get-at-bol 'org-marker)
8447 (org-agenda-error)))
8448 (buffer (marker-buffer marker))
8449 (pos (marker-position marker)))
8450 (org-with-remote-undo buffer
8451 (with-current-buffer buffer
8452 (if (derived-mode-p 'org-mode)
8453 (if (and confirm
8454 (not (y-or-n-p "Archive this subtree or entry? ")))
8455 (error "Abort")
8456 (save-window-excursion
8457 (goto-char pos)
8458 (let ((org-agenda-buffer-name bufname-orig))
8459 (org-remove-subtree-entries-from-agenda))
8460 (org-back-to-heading t)
8461 (funcall cmd)))
8462 (error "Archiving works only in Org-mode files"))))))
8464 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8465 "Remove all lines in the agenda that correspond to a given subtree.
8466 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8467 If this information is not given, the function uses the tree at point."
8468 (let ((buf (or buf (current-buffer))) m p)
8469 (save-excursion
8470 (unless (and beg end)
8471 (org-back-to-heading t)
8472 (setq beg (point))
8473 (org-end-of-subtree t)
8474 (setq end (point)))
8475 (set-buffer (get-buffer org-agenda-buffer-name))
8476 (save-excursion
8477 (goto-char (point-max))
8478 (beginning-of-line 1)
8479 (while (not (bobp))
8480 (when (and (setq m (org-get-at-bol 'org-marker))
8481 (equal buf (marker-buffer m))
8482 (setq p (marker-position m))
8483 (>= p beg)
8484 (< p end))
8485 (let ((inhibit-read-only t))
8486 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8487 (beginning-of-line 0))))))
8489 (defun org-agenda-refile (&optional goto rfloc no-update)
8490 "Refile the item at point.
8492 When GOTO is 0 or '(64), clear the refile cache.
8493 When GOTO is '(16), go to the location of the last refiled item.
8494 RFLOC can be a refile location obtained in a different way.
8495 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8496 (interactive "P")
8497 (cond
8498 ((member goto '(0 (64)))
8499 (org-refile-cache-clear))
8500 ((equal goto '(16))
8501 (org-refile-goto-last-stored))
8503 (let* ((buffer-orig (buffer-name))
8504 (marker (or (org-get-at-bol 'org-hd-marker)
8505 (org-agenda-error)))
8506 (buffer (marker-buffer marker))
8507 (pos (marker-position marker))
8508 (rfloc (or rfloc
8509 (org-refile-get-location
8510 (if goto "Goto" "Refile to") buffer
8511 org-refile-allow-creating-parent-nodes))))
8512 (with-current-buffer buffer
8513 (save-excursion
8514 (save-restriction
8515 (widen)
8516 (goto-char marker)
8517 (let ((org-agenda-buffer-name buffer-orig))
8518 (org-remove-subtree-entries-from-agenda))
8519 (org-refile goto buffer rfloc)))))
8520 (unless no-update (org-agenda-redo)))))
8522 (defun org-agenda-open-link (&optional arg)
8523 "Open the link(s) in the current entry, if any.
8524 This looks for a link in the displayed line in the agenda.
8525 It also looks at the text of the entry itself."
8526 (interactive "P")
8527 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8528 (org-get-at-bol 'org-marker)))
8529 (buffer (and marker (marker-buffer marker)))
8530 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8531 (lkall (and buffer (org-offer-links-in-entry
8532 buffer marker arg prefix)))
8533 (lk0 (car lkall))
8534 (lk (if (stringp lk0) (list lk0) lk0))
8535 (lkend (cdr lkall))
8536 trg)
8537 (cond
8538 ((and buffer lk)
8539 (mapcar (lambda(l)
8540 (with-current-buffer buffer
8541 (setq trg (and (string-match org-bracket-link-regexp l)
8542 (match-string 1 l)))
8543 (if (or (not trg) (string-match org-any-link-re trg))
8544 (save-excursion
8545 (save-restriction
8546 (widen)
8547 (goto-char marker)
8548 (when (search-forward l nil lkend)
8549 (goto-char (match-beginning 0))
8550 (org-open-at-point))))
8551 ;; This is an internal link, widen the buffer
8552 (switch-to-buffer-other-window buffer)
8553 (widen)
8554 (goto-char marker)
8555 (when (search-forward l nil lkend)
8556 (goto-char (match-beginning 0))
8557 (org-open-at-point)))))
8558 lk))
8559 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8560 (save-excursion
8561 (beginning-of-line 1)
8562 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8563 (org-open-link-from-string (match-string 1)))
8564 (t (message "No link to open here")))))
8566 (defun org-agenda-copy-local-variable (var)
8567 "Get a variable from a referenced buffer and install it here."
8568 (let ((m (org-get-at-bol 'org-marker)))
8569 (when (and m (buffer-live-p (marker-buffer m)))
8570 (org-set-local var (with-current-buffer (marker-buffer m)
8571 (symbol-value var))))))
8573 (defun org-agenda-switch-to (&optional delete-other-windows)
8574 "Go to the Org-mode file which contains the item at point."
8575 (interactive)
8576 (if (and org-return-follows-link
8577 (not (org-get-at-bol 'org-marker))
8578 (org-in-regexp org-bracket-link-regexp))
8579 (org-open-link-from-string (match-string 0))
8580 (let* ((marker (or (org-get-at-bol 'org-marker)
8581 (org-agenda-error)))
8582 (buffer (marker-buffer marker))
8583 (pos (marker-position marker)))
8584 (org-pop-to-buffer-same-window buffer)
8585 (and delete-other-windows (delete-other-windows))
8586 (widen)
8587 (goto-char pos)
8588 (org-back-to-heading t)
8589 (when (derived-mode-p 'org-mode)
8590 (org-show-context 'agenda)
8591 (save-excursion
8592 (and (outline-next-heading)
8593 (org-flag-heading nil))) ; show the next heading
8594 (when (outline-invisible-p)
8595 (show-entry)) ; display invisible text
8596 (run-hooks 'org-agenda-after-show-hook)))))
8598 (defun org-agenda-goto-mouse (ev)
8599 "Go to the Org-mode file which contains the item at the mouse click."
8600 (interactive "e")
8601 (mouse-set-point ev)
8602 (org-agenda-goto))
8604 (defun org-agenda-show (&optional full-entry)
8605 "Display the Org-mode file which contains the item at point.
8606 With prefix argument FULL-ENTRY, make the entire entry visible
8607 if it was hidden in the outline."
8608 (interactive "P")
8609 (let ((win (selected-window)))
8610 (if full-entry
8611 (let ((org-show-entry-below t))
8612 (org-agenda-goto t))
8613 (org-agenda-goto t))
8614 (select-window win)))
8616 (defvar org-agenda-show-window nil)
8617 (defun org-agenda-show-and-scroll-up (&optional arg)
8618 "Display the Org-mode file which contains the item at point.
8619 When called repeatedly, scroll the window that is displaying the buffer.
8620 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8621 `show-subtree' to display the item, so that drawers and logbooks stay
8622 folded."
8623 (interactive "P")
8624 (let ((win (selected-window)))
8625 (if (and (window-live-p org-agenda-show-window)
8626 (eq this-command last-command))
8627 (progn
8628 (select-window org-agenda-show-window)
8629 (ignore-errors (scroll-up)))
8630 (org-agenda-goto t)
8631 (if arg (org-show-entry) (show-subtree))
8632 (setq org-agenda-show-window (selected-window)))
8633 (select-window win)))
8635 (defun org-agenda-show-scroll-down ()
8636 "Scroll down the window showing the agenda."
8637 (interactive)
8638 (let ((win (selected-window)))
8639 (when (window-live-p org-agenda-show-window)
8640 (select-window org-agenda-show-window)
8641 (ignore-errors (scroll-down))
8642 (select-window win))))
8644 (defun org-agenda-show-1 (&optional more)
8645 "Display the Org-mode file which contains the item at point.
8646 The prefix arg selects the amount of information to display:
8648 0 hide the subtree
8649 1 just show the entry according to defaults.
8650 2 show the children view
8651 3 show the subtree view
8652 4 show the entire subtree and any LOGBOOK drawers
8653 5 show the entire subtree and any drawers
8654 With prefix argument FULL-ENTRY, make the entire entry visible
8655 if it was hidden in the outline."
8656 (interactive "p")
8657 (let ((win (selected-window)))
8658 (org-agenda-goto t)
8659 (org-recenter-heading 1)
8660 (cond
8661 ((= more 0)
8662 (hide-subtree)
8663 (save-excursion
8664 (org-back-to-heading)
8665 (run-hook-with-args 'org-cycle-hook 'folded))
8666 (message "Remote: FOLDED"))
8667 ((and (org-called-interactively-p 'any) (= more 1))
8668 (message "Remote: show with default settings"))
8669 ((= more 2)
8670 (show-entry)
8671 (show-children)
8672 (save-excursion
8673 (org-back-to-heading)
8674 (run-hook-with-args 'org-cycle-hook 'children))
8675 (message "Remote: CHILDREN"))
8676 ((= more 3)
8677 (show-subtree)
8678 (save-excursion
8679 (org-back-to-heading)
8680 (run-hook-with-args 'org-cycle-hook 'subtree))
8681 (message "Remote: SUBTREE"))
8682 ((= more 4)
8683 (show-subtree)
8684 (save-excursion
8685 (org-back-to-heading)
8686 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8687 (message "Remote: SUBTREE AND LOGBOOK"))
8688 ((> more 4)
8689 (show-subtree)
8690 (message "Remote: SUBTREE AND ALL DRAWERS")))
8691 (select-window win)))
8693 (defun org-recenter-heading (n)
8694 (save-excursion
8695 (org-back-to-heading)
8696 (recenter n)))
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