Allow inserting diary entries last in date tree
[org-mode.git] / lisp / org-agenda.el
blobd97b3a99b80471455bdb1e57994044b5d37995b9
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2015 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-iso-to-absolute "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-pop-to-buffer-same-window "org-compat"
84 (&optional buffer-or-name norecord label))
85 (declare-function org-agenda-columns "org-colview" ())
86 (declare-function org-add-archive-files "org-archive" (files))
87 (declare-function org-capture "org-capture" (&optional goto keys))
89 (defvar calendar-mode-map) ; defined in calendar.el
90 (defvar org-clock-current-task nil) ; defined in org-clock.el
91 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
92 (defvar org-habit-show-habits) ; defined in org-habit.el
93 (defvar org-habit-show-habits-only-for-today)
94 (defvar org-habit-show-all-today)
96 ;; Defined somewhere in this file, but used before definition.
97 (defvar org-agenda-buffer-name "*Org Agenda*")
98 (defvar org-agenda-overriding-header nil)
99 (defvar org-agenda-title-append nil)
100 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
101 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defvar org-agenda-undo-list nil
105 "List of undoable operations in the agenda since last refresh.")
106 (defvar org-agenda-pending-undo-list nil
107 "In a series of undo commands, this is the list of remaining undo items.")
109 (defcustom org-agenda-confirm-kill 1
110 "When set, remote killing from the agenda buffer needs confirmation.
111 When t, a confirmation is always needed. When a number N, confirmation is
112 only needed when the text to be killed contains more than N non-white lines."
113 :group 'org-agenda
114 :type '(choice
115 (const :tag "Never" nil)
116 (const :tag "Always" t)
117 (integer :tag "When more than N lines")))
119 (defcustom org-agenda-compact-blocks nil
120 "Non-nil means make the block agenda more compact.
121 This is done globally by leaving out lines like the agenda span
122 name and week number or the separator lines."
123 :group 'org-agenda
124 :type 'boolean)
126 (defcustom org-agenda-block-separator ?=
127 "The separator between blocks in the agenda.
128 If this is a string, it will be used as the separator, with a newline added.
129 If it is a character, it will be repeated to fill the window width.
130 If nil the separator is disabled. In `org-agenda-custom-commands' this
131 addresses the separator between the current and the previous block."
132 :group 'org-agenda
133 :type '(choice
134 (const :tag "Disabled" nil)
135 (character)
136 (string)))
138 (defgroup org-agenda-export nil
139 "Options concerning exporting agenda views in Org-mode."
140 :tag "Org Agenda Export"
141 :group 'org-agenda)
143 (defcustom org-agenda-with-colors t
144 "Non-nil means use colors in agenda views."
145 :group 'org-agenda-export
146 :type 'boolean)
148 (defcustom org-agenda-exporter-settings nil
149 "Alist of variable/value pairs that should be active during agenda export.
150 This is a good place to set options for ps-print and for htmlize.
151 Note that the way this is implemented, the values will be evaluated
152 before assigned to the variables. So make sure to quote values you do
153 *not* want evaluated, for example
155 (setq org-agenda-exporter-settings
156 '((ps-print-color-p 'black-white)))"
157 :group 'org-agenda-export
158 :type '(repeat
159 (list
160 (variable)
161 (sexp :tag "Value"))))
163 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
164 "Hook run in a temporary buffer before writing the agenda to an export file.
165 A useful function for this hook is `org-agenda-add-entry-text'."
166 :group 'org-agenda-export
167 :type 'hook
168 :options '(org-agenda-add-entry-text))
170 (defcustom org-agenda-add-entry-text-maxlines 0
171 "Maximum number of entry text lines to be added to agenda.
172 This is only relevant when `org-agenda-add-entry-text' is part of
173 `org-agenda-before-write-hook', which is the default.
174 When this is 0, nothing will happen. When it is greater than 0, it
175 specifies the maximum number of lines that will be added for each entry
176 that is listed in the agenda view.
178 Note that this variable is not used during display, only when exporting
179 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
180 and `org-agenda-entry-text-maxlines'."
181 :group 'org-agenda
182 :type 'integer)
184 (defcustom org-agenda-add-entry-text-descriptive-links t
185 "Non-nil means export org-links as descriptive links in agenda added text.
186 This variable applies to the text added to the agenda when
187 `org-agenda-add-entry-text-maxlines' is larger than 0.
188 When this variable nil, the URL will (also) be shown."
189 :group 'org-agenda
190 :type 'boolean)
192 (defcustom org-agenda-export-html-style nil
193 "The style specification for exported HTML Agenda files.
194 If this variable contains a string, it will replace the default <style>
195 section as produced by `htmlize'.
196 Since there are different ways of setting style information, this variable
197 needs to contain the full HTML structure to provide a style, including the
198 surrounding HTML tags. The style specifications should include definitions
199 the fonts used by the agenda, here is an example:
201 <style type=\"text/css\">
202 p { font-weight: normal; color: gray; }
203 .org-agenda-structure {
204 font-size: 110%;
205 color: #003399;
206 font-weight: 600;
208 .org-todo {
209 color: #cc6666;
210 font-weight: bold;
212 .org-agenda-done {
213 color: #339933;
215 .org-done {
216 color: #339933;
218 .title { text-align: center; }
219 .todo, .deadline { color: red; }
220 .done { color: green; }
221 </style>
223 or, if you want to keep the style in a file,
225 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
227 As the value of this option simply gets inserted into the HTML <head> header,
228 you can \"misuse\" it to also add other text to the header."
229 :group 'org-agenda-export
230 :group 'org-export-html
231 :type '(choice
232 (const nil)
233 (string)))
235 (defcustom org-agenda-persistent-filter nil
236 "When set, keep filters from one agenda view to the next."
237 :group 'org-agenda
238 :type 'boolean)
240 (defgroup org-agenda-custom-commands nil
241 "Options concerning agenda views in Org-mode."
242 :tag "Org Agenda Custom Commands"
243 :group 'org-agenda)
245 (defconst org-sorting-choice
246 '(choice
247 (const time-up) (const time-down)
248 (const timestamp-up) (const timestamp-down)
249 (const scheduled-up) (const scheduled-down)
250 (const deadline-up) (const deadline-down)
251 (const ts-up) (const ts-down)
252 (const tsia-up) (const tsia-down)
253 (const category-keep) (const category-up) (const category-down)
254 (const tag-down) (const tag-up)
255 (const priority-up) (const priority-down)
256 (const todo-state-up) (const todo-state-down)
257 (const effort-up) (const effort-down)
258 (const habit-up) (const habit-down)
259 (const alpha-up) (const alpha-down)
260 (const user-defined-up) (const user-defined-down))
261 "Sorting choices.")
263 ;; Keep custom values for `org-agenda-filter-preset' compatible with
264 ;; the new variable `org-agenda-tag-filter-preset'.
265 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
266 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
268 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
269 "List of types searched for when creating the daily/weekly agenda.
270 This variable is a list of symbols that controls the types of
271 items that appear in the daily/weekly agenda. Allowed symbols in this
272 list are are
274 :timestamp List items containing a date stamp or date range matching
275 the selected date. This includes sexp entries in angular
276 brackets.
278 :sexp List entries resulting from plain diary-like sexps.
280 :deadline List deadline due on that date. When the date is today,
281 also list any deadlines past due, or due within
282 `org-deadline-warning-days'. `:deadline' must appear before
283 `:scheduled' if the setting of
284 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
285 any effect.
287 :deadline* Same as above, but only include the deadline if it has an
288 hour specification as [h]h:mm.
290 :scheduled List all items which are scheduled for the given date.
291 The diary for *today* also contains items which were
292 scheduled earlier and are not yet marked DONE.
294 :scheduled* Same as above, but only include the scheduled item if it
295 has an hour specification as [h]h:mm.
297 By default, all four non-starred types are turned on.
299 When :scheduled* or :deadline* are included, :schedule or :deadline
300 will be ignored.
302 Never set this variable globally using `setq', because then it
303 will apply to all future agenda commands. Instead, bind it with
304 `let' to scope it dynamically into the agenda-constructing
305 command. A good way to set it is through options in
306 `org-agenda-custom-commands'. For a more flexible (though
307 somewhat less efficient) way of determining what is included in
308 the daily/weekly agenda, see `org-agenda-skip-function'.")
310 (defconst org-agenda-custom-commands-local-options
311 `(repeat :tag "Local settings for this command. Remember to quote values"
312 (choice :tag "Setting"
313 (list :tag "Heading for this block"
314 (const org-agenda-overriding-header)
315 (string :tag "Headline"))
316 (list :tag "Files to be searched"
317 (const org-agenda-files)
318 (list
319 (const :format "" quote)
320 (repeat (file))))
321 (list :tag "Sorting strategy"
322 (const org-agenda-sorting-strategy)
323 (list
324 (const :format "" quote)
325 (repeat
326 ,org-sorting-choice)))
327 (list :tag "Prefix format"
328 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
329 (string))
330 (list :tag "Number of days in agenda"
331 (const org-agenda-span)
332 (choice (const :tag "Day" day)
333 (const :tag "Week" week)
334 (const :tag "Fortnight" fortnight)
335 (const :tag "Month" month)
336 (const :tag "Year" year)
337 (integer :tag "Custom")))
338 (list :tag "Fixed starting date"
339 (const org-agenda-start-day)
340 (string :value "2007-11-01"))
341 (list :tag "Start on day of week"
342 (const org-agenda-start-on-weekday)
343 (choice :value 1
344 (const :tag "Today" nil)
345 (integer :tag "Weekday No.")))
346 (list :tag "Include data from diary"
347 (const org-agenda-include-diary)
348 (boolean))
349 (list :tag "Deadline Warning days"
350 (const org-deadline-warning-days)
351 (integer :value 1))
352 (list :tag "Category filter preset"
353 (const org-agenda-category-filter-preset)
354 (list
355 (const :format "" quote)
356 (repeat
357 (string :tag "+category or -category"))))
358 (list :tag "Tags filter preset"
359 (const org-agenda-tag-filter-preset)
360 (list
361 (const :format "" quote)
362 (repeat
363 (string :tag "+tag or -tag"))))
364 (list :tag "Effort filter preset"
365 (const org-agenda-effort-filter-preset)
366 (list
367 (const :format "" quote)
368 (repeat
369 (string :tag "+=10 or -=10 or +<10 or ->10"))))
370 (list :tag "Regexp filter preset"
371 (const org-agenda-regexp-filter-preset)
372 (list
373 (const :format "" quote)
374 (repeat
375 (string :tag "+regexp or -regexp"))))
376 (list :tag "Set daily/weekly entry types"
377 (const org-agenda-entry-types)
378 (list
379 (const :format "" quote)
380 (set :greedy t :value ,org-agenda-entry-types
381 (const :deadline)
382 (const :scheduled)
383 (const :deadline*)
384 (const :scheduled*)
385 (const :timestamp)
386 (const :sexp))))
387 (list :tag "Standard skipping condition"
388 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
389 (const org-agenda-skip-function)
390 (list
391 (const :format "" quote)
392 (list
393 (choice
394 :tag "Skipping range"
395 (const :tag "Skip entry" org-agenda-skip-entry-if)
396 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
397 (repeat :inline t :tag "Conditions for skipping"
398 (choice
399 :tag "Condition type"
400 (list :tag "Regexp matches" :inline t
401 (const :format "" 'regexp)
402 (regexp))
403 (list :tag "Regexp does not match" :inline t
404 (const :format "" 'notregexp)
405 (regexp))
406 (list :tag "TODO state is" :inline t
407 (const 'todo)
408 (choice
409 (const :tag "Any not-done state" 'todo)
410 (const :tag "Any done state" 'done)
411 (const :tag "Any state" 'any)
412 (list :tag "Keyword list"
413 (const :format "" quote)
414 (repeat (string :tag "Keyword")))))
415 (list :tag "TODO state is not" :inline t
416 (const 'nottodo)
417 (choice
418 (const :tag "Any not-done state" 'todo)
419 (const :tag "Any done state" 'done)
420 (const :tag "Any state" 'any)
421 (list :tag "Keyword list"
422 (const :format "" quote)
423 (repeat (string :tag "Keyword")))))
424 (const :tag "scheduled" 'scheduled)
425 (const :tag "not scheduled" 'notscheduled)
426 (const :tag "deadline" 'deadline)
427 (const :tag "no deadline" 'notdeadline)
428 (const :tag "timestamp" 'timestamp)
429 (const :tag "no timestamp" 'nottimestamp))))))
430 (list :tag "Non-standard skipping condition"
431 :value (org-agenda-skip-function)
432 (const org-agenda-skip-function)
433 (sexp :tag "Function or form (quoted!)"))
434 (list :tag "Any variable"
435 (variable :tag "Variable")
436 (sexp :tag "Value (sexp)"))))
437 "Selection of examples for agenda command settings.
438 This will be spliced into the custom type of
439 `org-agenda-custom-commands'.")
442 (defcustom org-agenda-custom-commands
443 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
444 "Custom commands for the agenda.
445 These commands will be offered on the splash screen displayed by the
446 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
448 (key desc type match settings files)
450 key The key (one or more characters as a string) to be associated
451 with the command.
452 desc A description of the command, when omitted or nil, a default
453 description is built using MATCH.
454 type The command type, any of the following symbols:
455 agenda The daily/weekly agenda.
456 todo Entries with a specific TODO keyword, in all agenda files.
457 search Entries containing search words entry or headline.
458 tags Tags/Property/TODO match in all agenda files.
459 tags-todo Tags/P/T match in all agenda files, TODO entries only.
460 todo-tree Sparse tree of specific TODO keyword in *current* file.
461 tags-tree Sparse tree with all tags matches in *current* file.
462 occur-tree Occur sparse tree for *current* file.
463 ... A user-defined function.
464 match What to search for:
465 - a single keyword for TODO keyword searches
466 - a tags match expression for tags searches
467 - a word search expression for text searches.
468 - a regular expression for occur searches
469 For all other commands, this should be the empty string.
470 settings A list of option settings, similar to that in a let form, so like
471 this: ((opt1 val1) (opt2 val2) ...). The values will be
472 evaluated at the moment of execution, so quote them when needed.
473 files A list of files file to write the produced agenda buffer to
474 with the command `org-store-agenda-views'.
475 If a file name ends in \".html\", an HTML version of the buffer
476 is written out. If it ends in \".ps\", a postscript version is
477 produced. Otherwise, only the plain text is written to the file.
479 You can also define a set of commands, to create a composite agenda buffer.
480 In this case, an entry looks like this:
482 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
484 where
486 desc A description string to be displayed in the dispatcher menu.
487 cmd An agenda command, similar to the above. However, tree commands
488 are not allowed, but instead you can get agenda and global todo list.
489 So valid commands for a set are:
490 (agenda \"\" settings)
491 (alltodo \"\" settings)
492 (stuck \"\" settings)
493 (todo \"match\" settings files)
494 (search \"match\" settings files)
495 (tags \"match\" settings files)
496 (tags-todo \"match\" settings files)
498 Each command can carry a list of options, and another set of options can be
499 given for the whole set of commands. Individual command options take
500 precedence over the general options.
502 When using several characters as key to a command, the first characters
503 are prefix commands. For the dispatcher to display useful information, you
504 should provide a description for the prefix, like
506 (setq org-agenda-custom-commands
507 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
508 (\"hl\" tags \"+HOME+Lisa\")
509 (\"hp\" tags \"+HOME+Peter\")
510 (\"hk\" tags \"+HOME+Kim\")))"
511 :group 'org-agenda-custom-commands
512 :type `(repeat
513 (choice :value ("x" "Describe command here" tags "" nil)
514 (list :tag "Single command"
515 (string :tag "Access Key(s) ")
516 (option (string :tag "Description"))
517 (choice
518 (const :tag "Agenda" agenda)
519 (const :tag "TODO list" alltodo)
520 (const :tag "Search words" search)
521 (const :tag "Stuck projects" stuck)
522 (const :tag "Tags/Property match (all agenda files)" tags)
523 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
524 (const :tag "TODO keyword search (all agenda files)" todo)
525 (const :tag "Tags sparse tree (current buffer)" tags-tree)
526 (const :tag "TODO keyword tree (current buffer)" todo-tree)
527 (const :tag "Occur tree (current buffer)" occur-tree)
528 (sexp :tag "Other, user-defined function"))
529 (string :tag "Match (only for some commands)")
530 ,org-agenda-custom-commands-local-options
531 (option (repeat :tag "Export" (file :tag "Export to"))))
532 (list :tag "Command series, all agenda files"
533 (string :tag "Access Key(s)")
534 (string :tag "Description ")
535 (repeat :tag "Component"
536 (choice
537 (list :tag "Agenda"
538 (const :format "" agenda)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "TODO list (all keywords)"
542 (const :format "" alltodo)
543 (const :tag "" :format "" "")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Search words"
546 (const :format "" search)
547 (string :tag "Match")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Stuck projects"
550 (const :format "" stuck)
551 (const :tag "" :format "" "")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search"
554 (const :format "" tags)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "Tags search, TODO entries only"
558 (const :format "" tags-todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "TODO keyword search"
562 (const :format "" todo)
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)
565 (list :tag "Other, user-defined function"
566 (symbol :tag "function")
567 (string :tag "Match")
568 ,org-agenda-custom-commands-local-options)))
570 (repeat :tag "Settings for entire command set"
571 (list (variable :tag "Any variable")
572 (sexp :tag "Value")))
573 (option (repeat :tag "Export" (file :tag "Export to"))))
574 (cons :tag "Prefix key documentation"
575 (string :tag "Access Key(s)")
576 (string :tag "Description ")))))
578 (defcustom org-agenda-query-register ?o
579 "The register holding the current query string.
580 The purpose of this is that if you construct a query string interactively,
581 you can then use it to define a custom command."
582 :group 'org-agenda-custom-commands
583 :type 'character)
585 (defcustom org-stuck-projects
586 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
587 "How to identify stuck projects.
588 This is a list of four items:
589 1. A tags/todo/property matcher string that is used to identify a project.
590 See the manual for a description of tag and property searches.
591 The entire tree below a headline matched by this is considered one project.
592 2. A list of TODO keywords identifying non-stuck projects.
593 If the project subtree contains any headline with one of these todo
594 keywords, the project is considered to be not stuck. If you specify
595 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
596 3. A list of tags identifying non-stuck projects.
597 If the project subtree contains any headline with one of these tags,
598 the project is considered to be not stuck. If you specify \"*\" as
599 a tag, any tag will mark the project unstuck. Note that this is about
600 the explicit presence of a tag somewhere in the subtree, inherited
601 tags do not count here. If inherited tags make a project not stuck,
602 use \"-TAG\" in the tags part of the matcher under (1.) above.
603 4. An arbitrary regular expression matching non-stuck projects.
605 If the project turns out to be not stuck, search continues also in the
606 subtree to see if any of the subtasks have project status.
608 See also the variable `org-tags-match-list-sublevels' which applies
609 to projects matched by this search as well.
611 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
612 or `C-c a #' to produce the list."
613 :group 'org-agenda-custom-commands
614 :type '(list
615 (string :tag "Tags/TODO match to identify a project")
616 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
620 (defgroup org-agenda-skip nil
621 "Options concerning skipping parts of agenda files."
622 :tag "Org Agenda Skip"
623 :group 'org-agenda)
625 (defcustom org-agenda-skip-function-global nil
626 "Function to be called at each match during agenda construction.
627 If this function returns nil, the current match should not be skipped.
628 If the function decided to skip an agenda match, is must return the
629 buffer position from which the search should be continued.
630 This may also be a Lisp form, which will be evaluated.
632 This variable will be applied to every agenda match, including
633 tags/property searches and TODO lists. So try to make the test function
634 do its checking as efficiently as possible. To implement a skipping
635 condition just for specific agenda commands, use the variable
636 `org-agenda-skip-function' which can be set in the options section
637 of custom agenda commands."
638 :group 'org-agenda-skip
639 :type 'sexp)
641 (defgroup org-agenda-daily/weekly nil
642 "Options concerning the daily/weekly agenda."
643 :tag "Org Agenda Daily/Weekly"
644 :group 'org-agenda)
645 (defgroup org-agenda-todo-list nil
646 "Options concerning the global todo list agenda view."
647 :tag "Org Agenda Todo List"
648 :group 'org-agenda)
649 (defgroup org-agenda-match-view nil
650 "Options concerning the general tags/property/todo match agenda view."
651 :tag "Org Agenda Match View"
652 :group 'org-agenda)
653 (defgroup org-agenda-search-view nil
654 "Options concerning the search agenda view."
655 :tag "Org Agenda Search View"
656 :group 'org-agenda)
658 (defvar org-agenda-archives-mode nil
659 "Non-nil means the agenda will include archived items.
660 If this is the symbol `trees', trees in the selected agenda scope
661 that are marked with the ARCHIVE tag will be included anyway. When this is
662 t, also all archive files associated with the current selection of agenda
663 files will be included.")
665 (defcustom org-agenda-restriction-lock-highlight-subtree t
666 "Non-nil means highlight the whole subtree when restriction is active.
667 Otherwise only highlight the headline. Highlighting the whole subtree is
668 useful to ensure no edits happen beyond the restricted region."
669 :group 'org-agenda
670 :type 'boolean)
672 (defcustom org-agenda-skip-comment-trees t
673 "Non-nil means skip trees that start with the COMMENT keyword.
674 When nil, these trees are also scanned by agenda commands."
675 :group 'org-agenda-skip
676 :type 'boolean)
678 (defcustom org-agenda-todo-list-sublevels t
679 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
680 When nil, the sublevels of a TODO entry are not checked, resulting in
681 potentially much shorter TODO lists."
682 :group 'org-agenda-skip
683 :group 'org-agenda-todo-list
684 :type 'boolean)
686 (defcustom org-agenda-todo-ignore-with-date nil
687 "Non-nil means don't show entries with a date in the global todo list.
688 You can use this if you prefer to mark mere appointments with a TODO keyword,
689 but don't want them to show up in the TODO list.
690 When this is set, it also covers deadlines and scheduled items, the settings
691 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
692 will be ignored.
693 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
694 :group 'org-agenda-skip
695 :group 'org-agenda-todo-list
696 :type 'boolean)
698 (defcustom org-agenda-todo-ignore-timestamp nil
699 "Non-nil means don't show entries with a timestamp.
700 This applies when creating the global todo list.
701 Valid values are:
703 past Don't show entries for today or in the past.
705 future Don't show entries with a timestamp in the future.
706 The idea behind this is that if it has a future
707 timestamp, you don't want to think about it until the
708 date.
710 all Don't show any entries with a timestamp in the global todo list.
711 The idea behind this is that by setting a timestamp, you
712 have already \"taken care\" of this item.
714 This variable can also have an integer as a value. If positive (N),
715 todos with a timestamp N or more days in the future will be ignored. If
716 negative (-N), todos with a timestamp N or more days in the past will be
717 ignored. If 0, todos with a timestamp either today or in the future will
718 be ignored. For example, a value of -1 will exclude todos with a
719 timestamp in the past (yesterday or earlier), while a value of 7 will
720 exclude todos with a timestamp a week or more in the future.
722 See also `org-agenda-todo-ignore-with-date'.
723 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
724 to make his option also apply to the tags-todo list."
725 :group 'org-agenda-skip
726 :group 'org-agenda-todo-list
727 :version "24.1"
728 :type '(choice
729 (const :tag "Ignore future timestamp todos" future)
730 (const :tag "Ignore past or present timestamp todos" past)
731 (const :tag "Ignore all timestamp todos" all)
732 (const :tag "Show timestamp todos" nil)
733 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
735 (defcustom org-agenda-todo-ignore-scheduled nil
736 "Non-nil means, ignore some scheduled TODO items when making TODO list.
737 This applies when creating the global todo list.
738 Valid values are:
740 past Don't show entries scheduled today or in the past.
742 future Don't show entries scheduled in the future.
743 The idea behind this is that by scheduling it, you don't want to
744 think about it until the scheduled date.
746 all Don't show any scheduled entries in the global todo list.
747 The idea behind this is that by scheduling it, you have already
748 \"taken care\" of this item.
750 t Same as `all', for backward compatibility.
752 This variable can also have an integer as a value. See
753 `org-agenda-todo-ignore-timestamp' for more details.
755 See also `org-agenda-todo-ignore-with-date'.
756 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
757 to make his option also apply to the tags-todo list."
758 :group 'org-agenda-skip
759 :group 'org-agenda-todo-list
760 :type '(choice
761 (const :tag "Ignore future-scheduled todos" future)
762 (const :tag "Ignore past- or present-scheduled todos" past)
763 (const :tag "Ignore all scheduled todos" all)
764 (const :tag "Ignore all scheduled todos (compatibility)" t)
765 (const :tag "Show scheduled todos" nil)
766 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
768 (defcustom org-agenda-todo-ignore-deadlines nil
769 "Non-nil means ignore some deadline TODO items when making TODO list.
770 There are different motivations for using different values, please think
771 carefully when configuring this variable.
773 This applies when creating the global todo list.
774 Valid values are:
776 near Don't show near deadline entries. A deadline is near when it is
777 closer than `org-deadline-warning-days' days. The idea behind this
778 is that such items will appear in the agenda anyway.
780 far Don't show TODO entries where a deadline has been defined, but
781 the deadline is not near. This is useful if you don't want to
782 use the todo list to figure out what to do now.
784 past Don't show entries with a deadline timestamp for today or in the past.
786 future Don't show entries with a deadline timestamp in the future, not even
787 when they become `near' ones. Use it with caution.
789 all Ignore all TODO entries that do have a deadline.
791 t Same as `near', for backward compatibility.
793 This variable can also have an integer as a value. See
794 `org-agenda-todo-ignore-timestamp' for more details.
796 See also `org-agenda-todo-ignore-with-date'.
797 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
798 to make his option also apply to the tags-todo list."
799 :group 'org-agenda-skip
800 :group 'org-agenda-todo-list
801 :type '(choice
802 (const :tag "Ignore near deadlines" near)
803 (const :tag "Ignore near deadlines (compatibility)" t)
804 (const :tag "Ignore far deadlines" far)
805 (const :tag "Ignore all TODOs with a deadlines" all)
806 (const :tag "Show all TODOs, even if they have a deadline" nil)
807 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
809 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
810 "Time unit to use when possibly ignoring an agenda item.
812 See the docstring of various `org-agenda-todo-ignore-*' options.
813 The default is to compare time stamps using days. An item is thus
814 considered to be in the future if it is at least one day after today.
815 Non-nil means to compare time stamps using seconds. An item is then
816 considered future if it has a time value later than current time."
817 :group 'org-agenda-skip
818 :group 'org-agenda-todo-list
819 :version "24.4"
820 :package-version '(Org . "8.0")
821 :type '(choice
822 (const :tag "Compare time with days" nil)
823 (const :tag "Compare time with seconds" t)))
825 (defcustom org-agenda-tags-todo-honor-ignore-options nil
826 "Non-nil means honor todo-list ignores options also in tags-todo search.
827 The variables
828 `org-agenda-todo-ignore-with-date',
829 `org-agenda-todo-ignore-timestamp',
830 `org-agenda-todo-ignore-scheduled',
831 `org-agenda-todo-ignore-deadlines'
832 make the global TODO list skip entries that have time stamps of certain
833 kinds. If this option is set, the same options will also apply for the
834 tags-todo search, which is the general tags/property matcher
835 restricted to unfinished TODO entries only."
836 :group 'org-agenda-skip
837 :group 'org-agenda-todo-list
838 :group 'org-agenda-match-view
839 :type 'boolean)
841 (defcustom org-agenda-skip-scheduled-if-done nil
842 "Non-nil means don't show scheduled items in agenda when they are done.
843 This is relevant for the daily/weekly agenda, not for the TODO list. And
844 it applies only to the actual date of the scheduling. Warnings about
845 an item with a past scheduling dates are always turned off when the item
846 is DONE."
847 :group 'org-agenda-skip
848 :group 'org-agenda-daily/weekly
849 :type 'boolean)
851 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
852 "Non-nil means skip scheduling line if same entry shows because of deadline.
854 In the agenda of today, an entry can show up multiple times
855 because it is both scheduled and has a nearby deadline, and maybe
856 a plain time stamp as well.
858 When this variable is nil, the entry will be shown several times.
860 When set to t, then only the deadline is shown and the fact that
861 the entry is scheduled today or was scheduled previously is not
862 shown.
864 When set to the symbol `not-today', skip scheduled previously,
865 but not scheduled today.
867 When set to the symbol `repeated-after-deadline', skip scheduled
868 items if they are repeated beyond the current deadline."
869 :group 'org-agenda-skip
870 :group 'org-agenda-daily/weekly
871 :type '(choice
872 (const :tag "Never" nil)
873 (const :tag "Always" t)
874 (const :tag "Not when scheduled today" not-today)
875 (const :tag "When repeated past deadline" repeated-after-deadline)))
877 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
878 "Non-nil means skip timestamp line if same entry shows because of deadline.
879 In the agenda of today, an entry can show up multiple times
880 because it has both a plain timestamp and has a nearby deadline.
881 When this variable is t, then only the deadline is shown and the
882 fact that the entry has a timestamp for or including today is not
883 shown. When this variable is nil, the entry will be shown
884 several times."
885 :group 'org-agenda-skip
886 :group 'org-agenda-daily/weekly
887 :version "24.1"
888 :type '(choice
889 (const :tag "Never" nil)
890 (const :tag "Always" t)))
892 (defcustom org-agenda-skip-deadline-if-done nil
893 "Non-nil means don't show deadlines when the corresponding item is done.
894 When nil, the deadline is still shown and should give you a happy feeling.
895 This is relevant for the daily/weekly agenda. And it applied only to the
896 actually date of the deadline. Warnings about approaching and past-due
897 deadlines are always turned off when the item is DONE."
898 :group 'org-agenda-skip
899 :group 'org-agenda-daily/weekly
900 :type 'boolean)
902 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
903 "Non-nil means skip deadline prewarning when entry is also scheduled.
904 This will apply on all days where a prewarning for the deadline would
905 be shown, but not at the day when the entry is actually due. On that day,
906 the deadline will be shown anyway.
907 This variable may be set to nil, t, the symbol `pre-scheduled',
908 or a number which will then give the number of days before the actual
909 deadline when the prewarnings should resume. The symbol `pre-scheduled'
910 eliminates the deadline prewarning only prior to the scheduled date.
911 This can be used in a workflow where the first showing of the deadline will
912 trigger you to schedule it, and then you don't want to be reminded of it
913 because you will take care of it on the day when scheduled."
914 :group 'org-agenda-skip
915 :group 'org-agenda-daily/weekly
916 :version "24.1"
917 :type '(choice
918 (const :tag "Always show prewarning" nil)
919 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
920 (const :tag "Remove prewarning if entry is scheduled" t)
921 (integer :tag "Restart prewarning N days before deadline")))
923 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
924 "Non-nil means skip scheduled delay when entry also has a deadline.
925 This variable may be set to nil, t, the symbol `post-deadline',
926 or a number which will then give the number of days after the actual
927 scheduled date when the delay should expire. The symbol `post-deadline'
928 eliminates the schedule delay when the date is posterior to the deadline."
929 :group 'org-agenda-skip
930 :group 'org-agenda-daily/weekly
931 :version "24.4"
932 :package-version '(Org . "8.0")
933 :type '(choice
934 (const :tag "Always honor delay" nil)
935 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
936 (const :tag "Ignore delay if entry has a deadline" t)
937 (integer :tag "Honor delay up until N days after the scheduled date")))
939 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
940 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
941 When non-nil, after the search for timestamps has matched once in an
942 entry, the rest of the entry will not be searched."
943 :group 'org-agenda-skip
944 :type 'boolean)
946 (defcustom org-agenda-skip-timestamp-if-done nil
947 "Non-nil means don't select item by timestamp or -range if it is DONE."
948 :group 'org-agenda-skip
949 :group 'org-agenda-daily/weekly
950 :type 'boolean)
952 (defcustom org-agenda-dim-blocked-tasks t
953 "Non-nil means dim blocked tasks in the agenda display.
954 This causes some overhead during agenda construction, but if you
955 have turned on `org-enforce-todo-dependencies',
956 `org-enforce-todo-checkbox-dependencies', or any other blocking
957 mechanism, this will create useful feedback in the agenda.
959 Instead of t, this variable can also have the value `invisible'.
960 Then blocked tasks will be invisible and only become visible when
961 they become unblocked. An exemption to this behavior is when a task is
962 blocked because of unchecked checkboxes below it. Since checkboxes do
963 not show up in the agenda views, making this task invisible you remove any
964 trace from agenda views that there is something to do. Therefore, a task
965 that is blocked because of checkboxes will never be made invisible, it
966 will only be dimmed."
967 :group 'org-agenda-daily/weekly
968 :group 'org-agenda-todo-list
969 :version "24.3"
970 :type '(choice
971 (const :tag "Do not dim" nil)
972 (const :tag "Dim to a gray face" t)
973 (const :tag "Make invisible" invisible)))
975 (defcustom org-timeline-show-empty-dates 3
976 "Non-nil means `org-timeline' also shows dates without an entry.
977 When nil, only the days which actually have entries are shown.
978 When t, all days between the first and the last date are shown.
979 When an integer, show also empty dates, but if there is a gap of more than
980 N days, just insert a special line indicating the size of the gap."
981 :group 'org-agenda-skip
982 :type '(choice
983 (const :tag "None" nil)
984 (const :tag "All" t)
985 (integer :tag "at most")))
987 (defgroup org-agenda-startup nil
988 "Options concerning initial settings in the Agenda in Org Mode."
989 :tag "Org Agenda Startup"
990 :group 'org-agenda)
992 (defcustom org-agenda-menu-show-matcher t
993 "Non-nil means show the match string in the agenda dispatcher menu.
994 When nil, the matcher string is not shown, but is put into the help-echo
995 property so than moving the mouse over the command shows it.
996 Setting it to nil is good if matcher strings are very long and/or if
997 you want to use two-columns display (see `org-agenda-menu-two-columns')."
998 :group 'org-agenda
999 :version "24.1"
1000 :type 'boolean)
1002 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1004 (defcustom org-agenda-menu-two-columns nil
1005 "Non-nil means, use two columns to show custom commands in the dispatcher.
1006 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1007 to nil."
1008 :group 'org-agenda
1009 :version "24.1"
1010 :type 'boolean)
1012 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1013 (defcustom org-agenda-finalize-hook nil
1014 "Hook run just before displaying an agenda buffer.
1015 The buffer is still writable when the hook is called.
1017 You can modify some of the buffer substrings but you should be
1018 extra careful not to modify the text properties of the agenda
1019 headlines as the agenda display heavily relies on them."
1020 :group 'org-agenda-startup
1021 :type 'hook)
1023 (defcustom org-agenda-mouse-1-follows-link nil
1024 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1025 A longer mouse click will still set point. Does not work on XEmacs.
1026 Needs to be set before org.el is loaded."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-start-with-follow-mode nil
1031 "The initial value of follow mode in a newly created agenda window."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-follow-indirect nil
1036 "Non-nil means `org-agenda-follow-mode' displays only the
1037 current item's tree, in an indirect buffer."
1038 :group 'org-agenda
1039 :version "24.1"
1040 :type 'boolean)
1042 (defcustom org-agenda-show-outline-path t
1043 "Non-nil means show outline path in echo area after line motion."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-start-with-entry-text-mode nil
1048 "The initial value of entry-text-mode in a newly created agenda window."
1049 :group 'org-agenda-startup
1050 :type 'boolean)
1052 (defcustom org-agenda-entry-text-maxlines 5
1053 "Number of text lines to be added when `E' is pressed in the agenda.
1055 Note that this variable only used during agenda display. Add add entry text
1056 when exporting the agenda, configure the variable
1057 `org-agenda-add-entry-ext-maxlines'."
1058 :group 'org-agenda
1059 :type 'integer)
1061 (defcustom org-agenda-entry-text-exclude-regexps nil
1062 "List of regular expressions to clean up entry text.
1063 The complete matches of all regular expressions in this list will be
1064 removed from entry text before it is shown in the agenda."
1065 :group 'org-agenda
1066 :type '(repeat (regexp)))
1068 (defcustom org-agenda-entry-text-leaders " > "
1069 "Text prepended to the entry text in agenda buffers."
1070 :version "24.4"
1071 :package-version '(Org . "8.0")
1072 :group 'org-agenda
1073 :type 'string)
1075 (defvar org-agenda-entry-text-cleanup-hook nil
1076 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1077 This cleanup is done in a temporary buffer, so the function may inspect and
1078 change the entire buffer.
1079 Some default stuff like drawers and scheduling/deadline dates will already
1080 have been removed when this is called, as will any matches for regular
1081 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1083 (defvar org-agenda-include-inactive-timestamps nil
1084 "Non-nil means include inactive time stamps in agenda and timeline.
1085 Dynamically scoped.")
1087 (defgroup org-agenda-windows nil
1088 "Options concerning the windows used by the Agenda in Org Mode."
1089 :tag "Org Agenda Windows"
1090 :group 'org-agenda)
1092 (defcustom org-agenda-window-setup 'reorganize-frame
1093 "How the agenda buffer should be displayed.
1094 Possible values for this option are:
1096 current-window Show agenda in the current window, keeping all other windows.
1097 other-window Use `switch-to-buffer-other-window' to display agenda.
1098 only-window Show agenda, deleting all other windows.
1099 reorganize-frame Show only two windows on the current frame, the current
1100 window and the agenda.
1101 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1102 Also, when exiting the agenda, kill that frame.
1103 See also the variable `org-agenda-restore-windows-after-quit'."
1104 :group 'org-agenda-windows
1105 :type '(choice
1106 (const current-window)
1107 (const other-frame)
1108 (const other-window)
1109 (const only-window)
1110 (const reorganize-frame)))
1112 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1113 "The min and max height of the agenda window as a fraction of frame height.
1114 The value of the variable is a cons cell with two numbers between 0 and 1.
1115 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1116 :group 'org-agenda-windows
1117 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1119 (defcustom org-agenda-restore-windows-after-quit nil
1120 "Non-nil means restore window configuration upon exiting agenda.
1121 Before the window configuration is changed for displaying the agenda,
1122 the current status is recorded. When the agenda is exited with
1123 `q' or `x' and this option is set, the old state is restored. If
1124 `org-agenda-window-setup' is `other-frame', the value of this
1125 option will be ignored."
1126 :group 'org-agenda-windows
1127 :type 'boolean)
1129 (defcustom org-agenda-ndays nil
1130 "Number of days to include in overview display.
1131 Should be 1 or 7.
1132 Obsolete, see `org-agenda-span'."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const nil)
1135 (integer)))
1137 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1139 (defcustom org-agenda-span 'week
1140 "Number of days to include in overview display.
1141 Can be day, week, month, year, or any number of days.
1142 Custom commands can set this variable in the options section."
1143 :group 'org-agenda-daily/weekly
1144 :type '(choice (const :tag "Day" day)
1145 (const :tag "Week" week)
1146 (const :tag "Fortnight" fortnight)
1147 (const :tag "Month" month)
1148 (const :tag "Year" year)
1149 (integer :tag "Custom")))
1151 (defcustom org-agenda-start-on-weekday 1
1152 "Non-nil means start the overview always on the specified weekday.
1153 0 denotes Sunday, 1 denotes Monday, etc.
1154 When nil, always start on the current day.
1155 Custom commands can set this variable in the options section."
1156 :group 'org-agenda-daily/weekly
1157 :type '(choice (const :tag "Today" nil)
1158 (integer :tag "Weekday No.")))
1160 (defcustom org-agenda-show-all-dates t
1161 "Non-nil means `org-agenda' shows every day in the selected range.
1162 When nil, only the days which actually have entries are shown."
1163 :group 'org-agenda-daily/weekly
1164 :type 'boolean)
1166 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1167 "Format string for displaying dates in the agenda.
1168 Used by the daily/weekly agenda and by the timeline. This should be
1169 a format string understood by `format-time-string', or a function returning
1170 the formatted date as a string. The function must take a single argument,
1171 a calendar-style date list like (month day year)."
1172 :group 'org-agenda-daily/weekly
1173 :type '(choice
1174 (string :tag "Format string")
1175 (function :tag "Function")))
1177 (defun org-agenda-format-date-aligned (date)
1178 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1179 This function makes sure that dates are aligned for easy reading."
1180 (require 'cal-iso)
1181 (let* ((dayname (calendar-day-name date))
1182 (day (cadr date))
1183 (day-of-week (calendar-day-of-week date))
1184 (month (car date))
1185 (monthname (calendar-month-name month))
1186 (year (nth 2 date))
1187 (iso-week (org-days-to-iso-week
1188 (calendar-absolute-from-gregorian date)))
1189 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1190 (1- year))
1191 ((and (= month 12) (<= iso-week 1))
1192 (1+ year))
1193 (t year)))
1194 (weekstring (if (= day-of-week 1)
1195 (format " W%02d" iso-week)
1196 "")))
1197 (format "%-10s %2d %s %4d%s"
1198 dayname day monthname year weekstring)))
1200 (defcustom org-agenda-time-leading-zero nil
1201 "Non-nil means use leading zero for military times in agenda.
1202 For example, 9:30am would become 09:30 rather than 9:30."
1203 :group 'org-agenda-daily/weekly
1204 :version "24.1"
1205 :type 'boolean)
1207 (defcustom org-agenda-timegrid-use-ampm nil
1208 "When set, show AM/PM style timestamps on the timegrid."
1209 :group 'org-agenda
1210 :version "24.1"
1211 :type 'boolean)
1213 (defun org-agenda-time-of-day-to-ampm (time)
1214 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1215 (let* ((hour-number (string-to-number (substring time 0 -3)))
1216 (minute (substring time -2))
1217 (ampm "am"))
1218 (cond
1219 ((equal hour-number 12)
1220 (setq ampm "pm"))
1221 ((> hour-number 12)
1222 (setq ampm "pm")
1223 (setq hour-number (- hour-number 12))))
1224 (concat
1225 (if org-agenda-time-leading-zero
1226 (format "%02d" hour-number)
1227 (format "%02s" (number-to-string hour-number)))
1228 ":" minute ampm)))
1230 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1231 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1232 (if org-agenda-timegrid-use-ampm
1233 (org-agenda-time-of-day-to-ampm time)
1234 time))
1236 (defcustom org-agenda-weekend-days '(6 0)
1237 "Which days are weekend?
1238 These days get the special face `org-agenda-date-weekend' in the agenda
1239 and timeline buffers."
1240 :group 'org-agenda-daily/weekly
1241 :type '(set :greedy t
1242 (const :tag "Monday" 1)
1243 (const :tag "Tuesday" 2)
1244 (const :tag "Wednesday" 3)
1245 (const :tag "Thursday" 4)
1246 (const :tag "Friday" 5)
1247 (const :tag "Saturday" 6)
1248 (const :tag "Sunday" 0)))
1250 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1251 "Non-nil means jump to today when moving a past date forward in time.
1252 When using S-right in the agenda to move a a date forward, and the date
1253 stamp currently points to the past, the first key press will move it
1254 to today. WHen nil, just move one day forward even if the date stays
1255 in the past."
1256 :group 'org-agenda-daily/weekly
1257 :version "24.1"
1258 :type 'boolean)
1260 (defcustom org-agenda-include-diary nil
1261 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :type 'boolean)
1266 (defcustom org-agenda-include-deadlines t
1267 "If non-nil, include entries within their deadline warning period.
1268 Custom commands can set this variable in the options section."
1269 :group 'org-agenda-daily/weekly
1270 :version "24.1"
1271 :type 'boolean)
1273 (defcustom org-agenda-repeating-timestamp-show-all t
1274 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1275 When set to a list of strings, only show occurrences of repeating
1276 stamps for these TODO keywords. When nil, only one occurrence is
1277 shown, either today or the nearest into the future."
1278 :group 'org-agenda-daily/weekly
1279 :type '(choice
1280 (const :tag "Show repeating stamps" t)
1281 (repeat :tag "Show repeating stamps for these TODO keywords"
1282 (string :tag "TODO Keyword"))
1283 (const :tag "Don't show repeating stamps" nil)))
1285 (defcustom org-scheduled-past-days 10000
1286 "Number of days to continue listing scheduled items not marked DONE.
1287 When an item is scheduled on a date, it shows up in the agenda on this
1288 day and will be listed until it is marked done for the number of days
1289 given here."
1290 :group 'org-agenda-daily/weekly
1291 :type 'integer)
1293 (defcustom org-agenda-log-mode-items '(closed clock)
1294 "List of items that should be shown in agenda log mode.
1295 This list may contain the following symbols:
1297 closed Show entries that have been closed on that day.
1298 clock Show entries that have received clocked time on that day.
1299 state Show all logged state changes.
1300 Note that instead of changing this variable, you can also press `C-u l' in
1301 the agenda to display all available LOG items temporarily."
1302 :group 'org-agenda-daily/weekly
1303 :type '(set :greedy t (const closed) (const clock) (const state)))
1305 (defcustom org-agenda-clock-consistency-checks
1306 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1307 :gap-ok-around ("4:00")
1308 :default-face ((:background "DarkRed") (:foreground "white"))
1309 :overlap-face nil :gap-face nil :no-end-time-face nil
1310 :long-face nil :short-face nil)
1311 "This is a property list, with the following keys:
1313 :max-duration Mark clocking chunks that are longer than this time.
1314 This is a time string like \"HH:MM\", or the number
1315 of minutes as an integer.
1317 :min-duration Mark clocking chunks that are shorter that this.
1318 This is a time string like \"HH:MM\", or the number
1319 of minutes as an integer.
1321 :max-gap Mark gaps between clocking chunks that are longer than
1322 this duration. A number of minutes, or a string
1323 like \"HH:MM\".
1325 :gap-ok-around List of times during the day which are usually not working
1326 times. When a gap is detected, but the gap contains any
1327 of these times, the gap is *not* reported. For example,
1328 if this is (\"4:00\" \"13:00\") then gaps that contain
1329 4:00 in the morning (i.e. the night) and 13:00
1330 (i.e. a typical lunch time) do not cause a warning.
1331 You should have at least one time during the night in this
1332 list, or otherwise the first task each morning will trigger
1333 a warning because it follows a long gap.
1335 Furthermore, the following properties can be used to define faces for
1336 issue display.
1338 :default-face the default face, if the specific face is undefined
1339 :overlap-face face for overlapping clocks
1340 :gap-face face for gaps between clocks
1341 :no-end-time-face face for incomplete clocks
1342 :long-face face for clock intervals that are too long
1343 :short-face face for clock intervals that are too short"
1344 :group 'org-agenda-daily/weekly
1345 :group 'org-clock
1346 :version "24.1"
1347 :type 'plist)
1349 (defcustom org-agenda-log-mode-add-notes t
1350 "Non-nil means add first line of notes to log entries in agenda views.
1351 If a log item like a state change or a clock entry is associated with
1352 notes, the first line of these notes will be added to the entry in the
1353 agenda display."
1354 :group 'org-agenda-daily/weekly
1355 :type 'boolean)
1357 (defcustom org-agenda-start-with-log-mode nil
1358 "The initial value of log-mode in a newly created agenda window.
1359 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1360 explanations on the possible values."
1361 :group 'org-agenda-startup
1362 :group 'org-agenda-daily/weekly
1363 :type '(choice (const :tag "Don't show log items" nil)
1364 (const :tag "Show only log items" only)
1365 (const :tag "Show all possible log items" clockcheck)
1366 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1367 (choice (const :tag "Show closed log items" closed)
1368 (const :tag "Show clocked log items" clock)
1369 (const :tag "Show all logged state changes" state)))))
1371 (defcustom org-agenda-start-with-clockreport-mode nil
1372 "The initial value of clockreport-mode in a newly created agenda window."
1373 :group 'org-agenda-startup
1374 :group 'org-agenda-daily/weekly
1375 :type 'boolean)
1377 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1378 "Property list with parameters for the clocktable in clockreport mode.
1379 This is the display mode that shows a clock table in the daily/weekly
1380 agenda, the properties for this dynamic block can be set here.
1381 The usual clocktable parameters are allowed here, but you cannot set
1382 the properties :name, :tstart, :tend, :block, and :scope - these will
1383 be overwritten to make sure the content accurately reflects the
1384 current display in the agenda."
1385 :group 'org-agenda-daily/weekly
1386 :type 'plist)
1388 (defcustom org-agenda-search-view-always-boolean nil
1389 "Non-nil means the search string is interpreted as individual parts.
1391 The search string for search view can either be interpreted as a phrase,
1392 or as a list of snippets that define a boolean search for a number of
1393 strings.
1395 When this is non-nil, the string will be split on whitespace, and each
1396 snippet will be searched individually, and all must match in order to
1397 select an entry. A snippet is then a single string of non-white
1398 characters, or a string in double quotes, or a regexp in {} braces.
1399 If a snippet is preceded by \"-\", the snippet must *not* match.
1400 \"+\" is syntactic sugar for positive selection. Each snippet may
1401 be found as a full word or a partial word, but see the variable
1402 `org-agenda-search-view-force-full-words'.
1404 When this is nil, search will look for the entire search phrase as one,
1405 with each space character matching any amount of whitespace, including
1406 line breaks.
1408 Even when this is nil, you can still switch to Boolean search dynamically
1409 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1410 is a regexp marked with braces like \"{abc}\", this will also switch to
1411 boolean search."
1412 :group 'org-agenda-search-view
1413 :version "24.1"
1414 :type 'boolean)
1416 (org-defvaralias 'org-agenda-search-view-search-words-only
1417 'org-agenda-search-view-always-boolean)
1419 (defcustom org-agenda-search-view-force-full-words nil
1420 "Non-nil means, search words must be matches as complete words.
1421 When nil, they may also match part of a word."
1422 :group 'org-agenda-search-view
1423 :version "24.1"
1424 :type 'boolean)
1426 (defcustom org-agenda-search-view-max-outline-level 0
1427 "Maximum outline level to display in search view.
1428 E.g. when this is set to 1, the search view will only
1429 show headlines of level 1. When set to 0, the default
1430 value, don't limit agenda view by outline level."
1431 :group 'org-agenda-search-view
1432 :version "24.4"
1433 :package-version '(Org . "8.3")
1434 :type 'integer)
1436 (defgroup org-agenda-time-grid nil
1437 "Options concerning the time grid in the Org-mode Agenda."
1438 :tag "Org Agenda Time Grid"
1439 :group 'org-agenda)
1441 (defcustom org-agenda-search-headline-for-time t
1442 "Non-nil means search headline for a time-of-day.
1443 If the headline contains a time-of-day in one format or another, it will
1444 be used to sort the entry into the time sequence of items for a day.
1445 Some people have time stamps in the headline that refer to the creation
1446 time or so, and then this produces an unwanted side effect. If this is
1447 the case for your, use this variable to turn off searching the headline
1448 for a time."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1452 (defcustom org-agenda-use-time-grid t
1453 "Non-nil means show a time grid in the agenda schedule.
1454 A time grid is a set of lines for specific times (like every two hours between
1455 8:00 and 20:00). The items scheduled for a day at specific times are
1456 sorted in between these lines.
1457 For details about when the grid will be shown, and what it will look like, see
1458 the variable `org-agenda-time-grid'."
1459 :group 'org-agenda-time-grid
1460 :type 'boolean)
1462 (defcustom org-agenda-time-grid
1463 '((daily today require-timed)
1464 "----------------"
1465 (800 1000 1200 1400 1600 1800 2000))
1467 "The settings for time grid for agenda display.
1468 This is a list of three items. The first item is again a list. It contains
1469 symbols specifying conditions when the grid should be displayed:
1471 daily if the agenda shows a single day
1472 weekly if the agenda shows an entire week
1473 today show grid on current date, independent of daily/weekly display
1474 require-timed show grid only if at least one item has a time specification
1475 remove-match skip grid times already present in an entry
1477 The second item is a string which will be placed behind the grid time.
1479 The third item is a list of integers, indicating the times that should have
1480 a grid line."
1481 :group 'org-agenda-time-grid
1482 :type
1483 '(list
1484 (set :greedy t :tag "Grid Display Options"
1485 (const :tag "Show grid in single day agenda display" daily)
1486 (const :tag "Show grid in weekly agenda display" weekly)
1487 (const :tag "Always show grid for today" today)
1488 (const :tag "Show grid only if any timed entries are present"
1489 require-timed)
1490 (const :tag "Skip grid times already present in an entry"
1491 remove-match))
1492 (string :tag "Grid String")
1493 (repeat :tag "Grid Times" (integer :tag "Time"))))
1495 (defcustom org-agenda-show-current-time-in-grid t
1496 "Non-nil means show the current time in the time grid."
1497 :group 'org-agenda-time-grid
1498 :version "24.1"
1499 :type 'boolean)
1501 (defcustom org-agenda-current-time-string
1502 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1503 "The string for the current time marker in the agenda."
1504 :group 'org-agenda-time-grid
1505 :version "24.1"
1506 :type 'string)
1508 (defgroup org-agenda-sorting nil
1509 "Options concerning sorting in the Org-mode Agenda."
1510 :tag "Org Agenda Sorting"
1511 :group 'org-agenda)
1513 (defcustom org-agenda-sorting-strategy
1514 '((agenda habit-down time-up priority-down category-keep)
1515 (todo priority-down category-keep)
1516 (tags priority-down category-keep)
1517 (search category-keep))
1518 "Sorting structure for the agenda items of a single day.
1519 This is a list of symbols which will be used in sequence to determine
1520 if an entry should be listed before another entry. The following
1521 symbols are recognized:
1523 time-up Put entries with time-of-day indications first, early first
1524 time-down Put entries with time-of-day indications first, late first
1525 timestamp-up Sort by any timestamp, early first
1526 timestamp-down Sort by any timestamp, late first
1527 scheduled-up Sort by scheduled timestamp, early first
1528 scheduled-down Sort by scheduled timestamp, late first
1529 deadline-up Sort by deadline timestamp, early first
1530 deadline-down Sort by deadline timestamp, late first
1531 ts-up Sort by active timestamp, early first
1532 ts-down Sort by active timestamp, late first
1533 tsia-up Sort by inactive timestamp, early first
1534 tsia-down Sort by inactive timestamp, late first
1535 category-keep Keep the default order of categories, corresponding to the
1536 sequence in `org-agenda-files'.
1537 category-up Sort alphabetically by category, A-Z.
1538 category-down Sort alphabetically by category, Z-A.
1539 tag-up Sort alphabetically by last tag, A-Z.
1540 tag-down Sort alphabetically by last tag, Z-A.
1541 priority-up Sort numerically by priority, high priority last.
1542 priority-down Sort numerically by priority, high priority first.
1543 todo-state-up Sort by todo state, tasks that are done last.
1544 todo-state-down Sort by todo state, tasks that are done first.
1545 effort-up Sort numerically by estimated effort, high effort last.
1546 effort-down Sort numerically by estimated effort, high effort first.
1547 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1548 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1549 habit-up Put entries that are habits first
1550 habit-down Put entries that are habits last
1551 alpha-up Sort headlines alphabetically
1552 alpha-down Sort headlines alphabetically, reversed
1554 The different possibilities will be tried in sequence, and testing stops
1555 if one comparison returns a \"not-equal\". For example, the default
1556 '(time-up category-keep priority-down)
1557 means: Pull out all entries having a specified time of day and sort them,
1558 in order to make a time schedule for the current day the first thing in the
1559 agenda listing for the day. Of the entries without a time indication, keep
1560 the grouped in categories, don't sort the categories, but keep them in
1561 the sequence given in `org-agenda-files'. Within each category sort by
1562 priority.
1564 Leaving out `category-keep' would mean that items will be sorted across
1565 categories by priority.
1567 Instead of a single list, this can also be a set of list for specific
1568 contents, with a context symbol in the car of the list, any of
1569 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1571 Custom commands can bind this variable in the options section."
1572 :group 'org-agenda-sorting
1573 :type `(choice
1574 (repeat :tag "General" ,org-sorting-choice)
1575 (list :tag "Individually"
1576 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1577 (repeat ,org-sorting-choice))
1578 (cons (const :tag "Strategy for TODO lists" todo)
1579 (repeat ,org-sorting-choice))
1580 (cons (const :tag "Strategy for Tags matches" tags)
1581 (repeat ,org-sorting-choice))
1582 (cons (const :tag "Strategy for search matches" search)
1583 (repeat ,org-sorting-choice)))))
1585 (defcustom org-agenda-cmp-user-defined nil
1586 "A function to define the comparison `user-defined'.
1587 This function must receive two arguments, agenda entry a and b.
1588 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1589 the user comparison, return nil.
1590 When this is defined, you can make `user-defined-up' and `user-defined-down'
1591 part of an agenda sorting strategy."
1592 :group 'org-agenda-sorting
1593 :type 'symbol)
1595 (defcustom org-sort-agenda-notime-is-late t
1596 "Non-nil means items without time are considered late.
1597 This is only relevant for sorting. When t, items which have no explicit
1598 time like 15:30 will be considered as 99:01, i.e. later than any items which
1599 do have a time. When nil, the default time is before 0:00. You can use this
1600 option to decide if the schedule for today should come before or after timeless
1601 agenda entries."
1602 :group 'org-agenda-sorting
1603 :type 'boolean)
1605 (defcustom org-sort-agenda-noeffort-is-high t
1606 "Non-nil means items without effort estimate are sorted as high effort.
1607 This also applies when filtering an agenda view with respect to the
1608 < or > effort operator. Then, tasks with no effort defined will be treated
1609 as tasks with high effort.
1610 When nil, such items are sorted as 0 minutes effort."
1611 :group 'org-agenda-sorting
1612 :type 'boolean)
1614 (defgroup org-agenda-line-format nil
1615 "Options concerning the entry prefix in the Org-mode agenda display."
1616 :tag "Org Agenda Line Format"
1617 :group 'org-agenda)
1619 (defcustom org-agenda-prefix-format
1620 '((agenda . " %i %-12:c%?-12t% s")
1621 (timeline . " % s")
1622 (todo . " %i %-12:c")
1623 (tags . " %i %-12:c")
1624 (search . " %i %-12:c"))
1625 "Format specifications for the prefix of items in the agenda views.
1626 An alist with five entries, each for the different agenda types. The
1627 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1628 The values are format strings.
1630 This format works similar to a printf format, with the following meaning:
1632 %c the category of the item, \"Diary\" for entries from the diary,
1633 or as given by the CATEGORY keyword or derived from the file name
1634 %e the effort required by the item
1635 %l the level of the item (insert X space(s) if item is of level X)
1636 %i the icon category of the item, see `org-agenda-category-icon-alist'
1637 %T the last tag of the item (ignore inherited tags, which come first)
1638 %t the HH:MM time-of-day specification if one applies to the entry
1639 %s Scheduling/Deadline information, a short string
1640 %b show breadcrumbs, i.e., the names of the higher levels
1641 %(expression) Eval EXPRESSION and replace the control string
1642 by the result
1644 All specifiers work basically like the standard `%s' of printf, but may
1645 contain two additional characters: a question mark just after the `%'
1646 and a whitespace/punctuation character just before the final letter.
1648 If the first character after `%' is a question mark, the entire field
1649 will only be included if the corresponding value applies to the current
1650 entry. This is useful for fields which should have fixed width when
1651 present, but zero width when absent. For example, \"%?-12t\" will
1652 result in a 12 character time field if a time of the day is specified,
1653 but will completely disappear in entries which do not contain a time.
1655 If there is punctuation or whitespace character just before the
1656 final format letter, this character will be appended to the field
1657 value if the value is not empty. For example, the format
1658 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1659 the category is empty, no additional colon is inserted.
1661 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1662 which means:
1664 - Indent the line with two space characters
1665 - Give the category a 12 chars wide field, padded with whitespace on
1666 the right (because of `-'). Append a colon if there is a category
1667 (because of `:').
1668 - If there is a time-of-day, put it into a 12 chars wide field. If no
1669 time, don't put in an empty field, just skip it (because of '?').
1670 - Finally, put the scheduling information.
1672 See also the variables `org-agenda-remove-times-when-in-prefix' and
1673 `org-agenda-remove-tags'.
1675 Custom commands can set this variable in the options section."
1676 :type '(choice
1677 (string :tag "General format")
1678 (list :greedy t :tag "View dependent"
1679 (cons (const agenda) (string :tag "Format"))
1680 (cons (const timeline) (string :tag "Format"))
1681 (cons (const todo) (string :tag "Format"))
1682 (cons (const tags) (string :tag "Format"))
1683 (cons (const search) (string :tag "Format"))))
1684 :group 'org-agenda-line-format)
1686 (defvar org-prefix-format-compiled nil
1687 "The compiled prefix format and associated variables.
1688 This is a list where first element is a list of variable bindings, and second
1689 element is the compiled format expression. See the variable
1690 `org-agenda-prefix-format'.")
1692 (defcustom org-agenda-todo-keyword-format "%-1s"
1693 "Format for the TODO keyword in agenda lines.
1694 Set this to something like \"%-12s\" if you want all TODO keywords
1695 to occupy a fixed space in the agenda display."
1696 :group 'org-agenda-line-format
1697 :type 'string)
1699 (defcustom org-agenda-diary-sexp-prefix nil
1700 "A regexp that matches part of a diary sexp entry
1701 which should be treated as scheduling/deadline information in
1702 `org-agenda'.
1704 For example, you can use this to extract the `diary-remind-message' from
1705 `diary-remind' entries."
1706 :group 'org-agenda-line-format
1707 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1709 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1710 "Text preceding timerange entries in the agenda view.
1711 This is a list with two strings. The first applies when the range
1712 is entirely on one day. The second applies if the range spans several days.
1713 The strings may have two \"%d\" format specifiers which will be filled
1714 with the sequence number of the days, and the total number of days in the
1715 range, respectively."
1716 :group 'org-agenda-line-format
1717 :type '(list
1718 (string :tag "Deadline today ")
1719 (choice :tag "Deadline relative"
1720 (string :tag "Format string")
1721 (function))))
1723 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1724 "Text preceding scheduled items in the agenda view.
1725 This is a list with two strings. The first applies when the item is
1726 scheduled on the current day. The second applies when it has been scheduled
1727 previously, it may contain a %d indicating that this is the nth time that
1728 this item is scheduled, due to automatic rescheduling of unfinished items
1729 for the following day. So this number is one larger than the number of days
1730 that passed since this item was scheduled first."
1731 :group 'org-agenda-line-format
1732 :version "24.4"
1733 :package-version '(Org . "8.0")
1734 :type '(list
1735 (string :tag "Scheduled today ")
1736 (string :tag "Scheduled previously")))
1738 (defcustom org-agenda-inactive-leader "["
1739 "Text preceding item pulled into the agenda by inactive time stamps.
1740 These entries are added to the agenda when pressing \"[\"."
1741 :group 'org-agenda-line-format
1742 :version "24.1"
1743 :type 'string)
1745 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1746 "Text preceding deadline items in the agenda view.
1747 This is a list with three strings. The first applies when the item has its
1748 deadline on the current day. The second applies when the deadline is in the
1749 future, the third one when it is in the past. The strings may contain %d
1750 to capture the number of days."
1751 :group 'org-agenda-line-format
1752 :version "24.4"
1753 :package-version '(Org . "8.0")
1754 :type '(list
1755 (string :tag "Deadline today ")
1756 (string :tag "Deadline in the future ")
1757 (string :tag "Deadline in the past ")))
1759 (defcustom org-agenda-remove-times-when-in-prefix t
1760 "Non-nil means remove duplicate time specifications in agenda items.
1761 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1762 time-of-day specification in a headline or diary entry is extracted and
1763 placed into the prefix. If this option is non-nil, the original specification
1764 \(a timestamp or -range, or just a plain time(range) specification like
1765 11:30-4pm) will be removed for agenda display. This makes the agenda less
1766 cluttered.
1767 The option can be t or nil. It may also be the symbol `beg', indicating
1768 that the time should only be removed when it is located at the beginning of
1769 the headline/diary entry."
1770 :group 'org-agenda-line-format
1771 :type '(choice
1772 (const :tag "Always" t)
1773 (const :tag "Never" nil)
1774 (const :tag "When at beginning of entry" beg)))
1776 (defcustom org-agenda-remove-timeranges-from-blocks nil
1777 "Non-nil means remove time ranges specifications in agenda
1778 items that span on several days."
1779 :group 'org-agenda-line-format
1780 :version "24.1"
1781 :type 'boolean)
1783 (defcustom org-agenda-default-appointment-duration nil
1784 "Default duration for appointments that only have a starting time.
1785 When nil, no duration is specified in such cases.
1786 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1787 :group 'org-agenda-line-format
1788 :type '(choice
1789 (integer :tag "Minutes")
1790 (const :tag "No default duration")))
1792 (defcustom org-agenda-show-inherited-tags t
1793 "Non-nil means show inherited tags in each agenda line.
1795 When this option is set to 'always, it take precedences over
1796 `org-agenda-use-tag-inheritance' and inherited tags are shown
1797 in every agenda.
1799 When this option is set to t (the default), inherited tags are
1800 shown when they are available, i.e. when the value of
1801 `org-agenda-use-tag-inheritance' has been taken into account.
1803 This can be set to a list of agenda types in which the agenda
1804 must display the inherited tags. Available types are 'todo,
1805 'agenda, 'search and 'timeline.
1807 When set to nil, never show inherited tags in agenda lines."
1808 :group 'org-agenda-line-format
1809 :group 'org-agenda
1810 :version "24.3"
1811 :type '(choice
1812 (const :tag "Show inherited tags when available" t)
1813 (const :tag "Always show inherited tags" always)
1814 (repeat :tag "Show inherited tags only in selected agenda types"
1815 (symbol :tag "Agenda type"))))
1817 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1818 "List of agenda view types where to use tag inheritance.
1820 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1821 controlled by `org-use-tag-inheritance'. In other agenda types,
1822 `org-use-tag-inheritance' is not used for the selection of the
1823 agenda entries. Still, you may want the agenda to be aware of
1824 the inherited tags anyway, e.g. for later tag filtering.
1826 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1828 This variable has no effect if `org-agenda-show-inherited-tags'
1829 is set to 'always. In that case, the agenda is aware of those
1830 tags.
1832 The default value sets tags in every agenda type. Setting this
1833 option to nil will speed up non-tags agenda view a lot."
1834 :group 'org-agenda
1835 :version "24.3"
1836 :type '(choice
1837 (const :tag "Use tag inheritance in all agenda types" t)
1838 (repeat :tag "Use tag inheritance in selected agenda types"
1839 (symbol :tag "Agenda type"))))
1841 (defcustom org-agenda-hide-tags-regexp nil
1842 "Regular expression used to filter away specific tags in agenda views.
1843 This means that these tags will be present, but not be shown in the agenda
1844 line. Secondary filtering will still work on the hidden tags.
1845 Nil means don't hide any tags."
1846 :group 'org-agenda-line-format
1847 :type '(choice
1848 (const :tag "Hide none" nil)
1849 (string :tag "Regexp ")))
1851 (defcustom org-agenda-remove-tags nil
1852 "Non-nil means remove the tags from the headline copy in the agenda.
1853 When this is the symbol `prefix', only remove tags when
1854 `org-agenda-prefix-format' contains a `%T' specifier."
1855 :group 'org-agenda-line-format
1856 :type '(choice
1857 (const :tag "Always" t)
1858 (const :tag "Never" nil)
1859 (const :tag "When prefix format contains %T" prefix)))
1861 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1862 'org-agenda-remove-tags)
1864 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1865 "Shift tags in agenda items to this column.
1866 If this number is positive, it specifies the column. If it is negative,
1867 it means that the tags should be flushright to that column. For example,
1868 -80 works well for a normal 80 character screen."
1869 :group 'org-agenda-line-format
1870 :type 'integer)
1872 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1874 (defcustom org-agenda-fontify-priorities 'cookies
1875 "Non-nil means highlight low and high priorities in agenda.
1876 When t, the highest priority entries are bold, lowest priority italic.
1877 However, settings in `org-priority-faces' will overrule these faces.
1878 When this variable is the symbol `cookies', only fontify the
1879 cookies, not the entire task.
1880 This may also be an association list of priority faces, whose
1881 keys are the character values of `org-highest-priority',
1882 `org-default-priority', and `org-lowest-priority' (the default values
1883 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1884 color as a string, or a list like `(:background \"Red\")'.
1885 If it is a color, the variable `org-faces-easy-properties'
1886 determines if it is a foreground or a background color."
1887 :group 'org-agenda-line-format
1888 :type '(choice
1889 (const :tag "Never" nil)
1890 (const :tag "Defaults" t)
1891 (const :tag "Cookies only" cookies)
1892 (repeat :tag "Specify"
1893 (list (character :tag "Priority" :value ?A)
1894 (choice :tag "Face "
1895 (string :tag "Color")
1896 (sexp :tag "Face"))))))
1898 (defcustom org-agenda-day-face-function nil
1899 "Function called to determine what face should be used to display a day.
1900 The only argument passed to that function is the day. It should
1901 returns a face, or nil if does not want to specify a face and let
1902 the normal rules apply."
1903 :group 'org-agenda-line-format
1904 :version "24.1"
1905 :type '(choice (const nil) (function)))
1907 (defcustom org-agenda-category-icon-alist nil
1908 "Alist of category icon to be displayed in agenda views.
1910 Each entry should have the following format:
1912 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1914 Where CATEGORY-REGEXP is a regexp matching the categories where
1915 the icon should be displayed.
1916 FILE-OR-DATA either a file path or a string containing image data.
1918 The other fields can be omitted safely if not needed:
1919 TYPE indicates the image type.
1920 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1921 image data.
1922 PROPS are additional image attributes to assign to the image,
1923 like, e.g. `:ascent center'.
1925 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1927 If you want to set the display properties yourself, just put a
1928 list as second element:
1930 (CATEGORY-REGEXP (MY PROPERTY LIST))
1932 For example, to display a 16px horizontal space for Emacs
1933 category, you can use:
1935 (\"Emacs\" '(space . (:width (16))))"
1936 :group 'org-agenda-line-format
1937 :version "24.1"
1938 :type '(alist :key-type (string :tag "Regexp matching category")
1939 :value-type (choice (list :tag "Icon"
1940 (string :tag "File or data")
1941 (symbol :tag "Type")
1942 (boolean :tag "Data?")
1943 (repeat :tag "Extra image properties" :inline t symbol))
1944 (list :tag "Display properties" sexp))))
1946 (defgroup org-agenda-column-view nil
1947 "Options concerning column view in the agenda."
1948 :tag "Org Agenda Column View"
1949 :group 'org-agenda)
1951 (defcustom org-agenda-columns-show-summaries t
1952 "Non-nil means show summaries for columns displayed in the agenda view."
1953 :group 'org-agenda-column-view
1954 :type 'boolean)
1956 (defcustom org-agenda-columns-compute-summary-properties t
1957 "Non-nil means recompute all summary properties before column view.
1958 When column view in the agenda is listing properties that have a summary
1959 operator, it can go to all relevant buffers and recompute the summaries
1960 there. This can mean overhead for the agenda column view, but is necessary
1961 to have thing up to date.
1962 As a special case, a CLOCKSUM property also makes sure that the clock
1963 computations are current."
1964 :group 'org-agenda-column-view
1965 :type 'boolean)
1967 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1968 "Non-nil means the duration of an appointment will add to day effort.
1969 The property to which appointment durations will be added is the one given
1970 in the option `org-effort-property'. If an appointment does not have
1971 an end time, `org-agenda-default-appointment-duration' will be used. If that
1972 is not set, an appointment without end time will not contribute to the time
1973 estimate."
1974 :group 'org-agenda-column-view
1975 :type 'boolean)
1977 (defcustom org-agenda-auto-exclude-function nil
1978 "A function called with a tag to decide if it is filtered on '/ RET'.
1979 The sole argument to the function, which is called once for each
1980 possible tag, is a string giving the name of the tag. The
1981 function should return either nil if the tag should be included
1982 as normal, or \"-<TAG>\" to exclude the tag.
1983 Note that for the purpose of tag filtering, only the lower-case version of
1984 all tags will be considered, so that this function will only ever see
1985 the lower-case version of all tags."
1986 :group 'org-agenda
1987 :type '(choice (const nil) (function)))
1989 (defcustom org-agenda-bulk-custom-functions nil
1990 "Alist of characters and custom functions for bulk actions.
1991 For example, this value makes those two functions available:
1993 '((?R set-category)
1994 (?C bulk-cut))
1996 With selected entries in an agenda buffer, `B R' will call
1997 the custom function `set-category' on the selected entries.
1998 Note that functions in this alist don't need to be quoted."
1999 :type 'alist
2000 :version "24.1"
2001 :group 'org-agenda)
2003 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2004 "Execute BODY with point at location given by `org-hd-marker' property.
2005 If STRING is non-nil, the text property will be fetched from position 0
2006 in that string. If STRING is nil, it will be fetched from the beginning
2007 of the current line."
2008 (org-with-gensyms (marker)
2009 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2010 'org-hd-marker ,string)))
2011 (with-current-buffer (marker-buffer ,marker)
2012 (save-excursion
2013 (goto-char ,marker)
2014 ,@body)))))
2015 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2017 (defun org-add-agenda-custom-command (entry)
2018 "Replace or add a command in `org-agenda-custom-commands'.
2019 This is mostly for hacking and trying a new command - once the command
2020 works you probably want to add it to `org-agenda-custom-commands' for good."
2021 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2022 (if ass
2023 (setcdr ass (cdr entry))
2024 (push entry org-agenda-custom-commands))))
2026 ;;; Define the org-agenda-mode
2028 (defvar org-agenda-mode-map (make-sparse-keymap)
2029 "Keymap for `org-agenda-mode'.")
2030 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2032 (defvar org-agenda-menu) ; defined later in this file.
2033 (defvar org-agenda-restrict nil) ; defined later in this file.
2034 (defvar org-agenda-follow-mode nil)
2035 (defvar org-agenda-entry-text-mode nil)
2036 (defvar org-agenda-clockreport-mode nil)
2037 (defvar org-agenda-show-log nil)
2038 (defvar org-agenda-redo-command nil)
2039 (defvar org-agenda-query-string nil)
2040 (defvar org-agenda-mode-hook nil
2041 "Hook run after `org-agenda-mode' is turned on.
2042 The buffer is still writable when this hook is called.")
2043 (defvar org-agenda-type nil)
2044 (defvar org-agenda-force-single-file nil)
2045 (defvar org-agenda-bulk-marked-entries nil
2046 "List of markers that refer to marked entries in the agenda.")
2048 ;;; Multiple agenda buffers support
2050 (defcustom org-agenda-sticky nil
2051 "Non-nil means agenda q key will bury agenda buffers.
2052 Agenda commands will then show existing buffer instead of generating new ones.
2053 When nil, `q' will kill the single agenda buffer."
2054 :group 'org-agenda
2055 :version "24.3"
2056 :type 'boolean)
2059 ;;;###autoload
2060 (defun org-toggle-sticky-agenda (&optional arg)
2061 "Toggle `org-agenda-sticky'."
2062 (interactive "P")
2063 (let ((new-value (if arg
2064 (> (prefix-numeric-value arg) 0)
2065 (not org-agenda-sticky))))
2066 (if (equal new-value org-agenda-sticky)
2067 (and (org-called-interactively-p 'interactive)
2068 (message "Sticky agenda was already %s"
2069 (if org-agenda-sticky "enabled" "disabled")))
2070 (setq org-agenda-sticky new-value)
2071 (org-agenda-kill-all-agenda-buffers)
2072 (and (org-called-interactively-p 'interactive)
2073 (message "Sticky agenda %s"
2074 (if org-agenda-sticky "enabled" "disabled"))))))
2076 (defvar org-agenda-buffer nil
2077 "Agenda buffer currently being generated.")
2079 (defvar org-agenda-last-prefix-arg nil)
2080 (defvar org-agenda-this-buffer-name nil)
2081 (defvar org-agenda-doing-sticky-redo nil)
2082 (defvar org-agenda-this-buffer-is-sticky nil)
2083 (defvar org-agenda-last-indirect-buffer nil
2084 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2086 (defconst org-agenda-local-vars
2087 '(org-agenda-this-buffer-name
2088 org-agenda-undo-list
2089 org-agenda-pending-undo-list
2090 org-agenda-follow-mode
2091 org-agenda-entry-text-mode
2092 org-agenda-clockreport-mode
2093 org-agenda-show-log
2094 org-agenda-redo-command
2095 org-agenda-query-string
2096 org-agenda-type
2097 org-agenda-bulk-marked-entries
2098 org-agenda-undo-has-started-in
2099 org-agenda-info
2100 org-agenda-pre-window-conf
2101 org-agenda-columns-active
2102 org-agenda-tag-filter
2103 org-agenda-category-filter
2104 org-agenda-top-headline-filter
2105 org-agenda-regexp-filter
2106 org-agenda-effort-filter
2107 org-agenda-markers
2108 org-agenda-last-search-view-search-was-boolean
2109 org-agenda-last-indirect-buffer
2110 org-agenda-filtered-by-category
2111 org-agenda-filter-form
2112 org-agenda-cycle-counter
2113 org-agenda-last-prefix-arg)
2114 "Variables that must be local in agenda buffers to allow multiple buffers.")
2116 (defun org-agenda-mode ()
2117 "Mode for time-sorted view on action items in Org-mode files.
2119 The following commands are available:
2121 \\{org-agenda-mode-map}"
2122 (interactive)
2123 (cond (org-agenda-doing-sticky-redo
2124 ;; Refreshing sticky agenda-buffer
2126 ;; Preserve the value of `org-agenda-local-vars' variables,
2127 ;; while letting `kill-all-local-variables' kill the rest
2128 (let ((save (buffer-local-variables)))
2129 (kill-all-local-variables)
2130 (mapc 'make-local-variable org-agenda-local-vars)
2131 (dolist (elem save)
2132 (let ((var (car elem))
2133 (val (cdr elem)))
2134 (when (and val
2135 (member var org-agenda-local-vars))
2136 (set var val)))))
2137 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2138 (org-agenda-sticky
2139 ;; Creating a sticky Agenda buffer for the first time
2140 (kill-all-local-variables)
2141 (mapc 'make-local-variable org-agenda-local-vars)
2142 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2144 ;; Creating a non-sticky agenda buffer
2145 (kill-all-local-variables)
2146 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2147 (setq org-agenda-undo-list nil
2148 org-agenda-pending-undo-list nil
2149 org-agenda-bulk-marked-entries nil)
2150 (setq major-mode 'org-agenda-mode)
2151 ;; Keep global-font-lock-mode from turning on font-lock-mode
2152 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2153 (setq mode-name "Org-Agenda")
2154 (setq indent-tabs-mode nil)
2155 (use-local-map org-agenda-mode-map)
2156 (easy-menu-add org-agenda-menu)
2157 (if org-startup-truncated (setq truncate-lines t))
2158 (org-set-local 'line-move-visual nil)
2159 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2160 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2161 ;; Make sure properties are removed when copying text
2162 (org-add-hook 'filter-buffer-substring-functions
2163 (lambda (fun start end delete)
2164 (substring-no-properties (funcall fun start end delete)))
2165 nil t)
2166 (unless org-agenda-keep-modes
2167 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2168 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2169 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2170 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2171 (add-to-invisibility-spec '(org-filtered))
2172 (add-to-invisibility-spec '(org-link))
2173 (easy-menu-change
2174 '("Agenda") "Agenda Files"
2175 (append
2176 (list
2177 (vector
2178 (if (get 'org-agenda-files 'org-restrict)
2179 "Restricted to single file"
2180 "Edit File List")
2181 '(org-edit-agenda-file-list)
2182 (not (get 'org-agenda-files 'org-restrict)))
2183 "--")
2184 (mapcar 'org-file-menu-entry (org-agenda-files))))
2185 (org-agenda-set-mode-name)
2186 (apply
2187 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2188 (list 'org-agenda-mode-hook)))
2190 (substitute-key-definition 'undo 'org-agenda-undo
2191 org-agenda-mode-map global-map)
2192 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2193 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2194 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2195 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2196 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2197 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2198 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2199 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2200 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2201 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2202 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2203 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2204 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2205 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2206 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2207 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2208 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2209 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2210 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2211 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2212 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2213 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2214 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2215 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2216 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2217 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2218 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2219 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2220 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2221 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2222 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2223 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2224 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2225 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2226 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2227 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2228 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2229 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2230 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2231 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2232 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2233 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2234 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2235 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2236 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2237 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2238 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2239 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2240 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2241 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2243 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2244 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2245 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2246 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2247 (while l (org-defkey org-agenda-mode-map
2248 (int-to-string (pop l)) 'digit-argument)))
2250 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2251 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2252 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2253 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2254 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2255 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2256 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2257 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2258 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2259 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2260 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2261 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2262 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2263 'org-clock-modify-effort-estimate)
2264 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2265 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2266 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2267 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2268 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2269 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2270 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2271 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2272 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2273 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2274 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2275 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2276 (substitute-key-definition 'next-line 'org-agenda-next-line
2277 org-agenda-mode-map global-map)
2278 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2279 org-agenda-mode-map global-map)
2280 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2281 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2282 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2283 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2284 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2285 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2286 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2287 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2288 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2289 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2290 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2291 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2293 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2294 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2295 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2296 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2297 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2298 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2299 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2300 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2301 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2302 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2303 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2304 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2305 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2306 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2307 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2308 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2309 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2311 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2312 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2313 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2314 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2315 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2316 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2317 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2318 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2319 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2320 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2321 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2322 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2323 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2324 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2325 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2326 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2328 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2329 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2331 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2332 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2334 (when org-agenda-mouse-1-follows-link
2335 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2336 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2337 '("Agenda"
2338 ("Agenda Files")
2339 "--"
2340 ("Agenda Dates"
2341 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2342 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2343 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2344 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2345 "--"
2346 ("View"
2347 ["Day View" org-agenda-day-view
2348 :active (org-agenda-check-type nil 'agenda)
2349 :style radio :selected (eq org-agenda-current-span 'day)
2350 :keys "v d (or just d)"]
2351 ["Week View" org-agenda-week-view
2352 :active (org-agenda-check-type nil 'agenda)
2353 :style radio :selected (eq org-agenda-current-span 'week)
2354 :keys "v w"]
2355 ["Fortnight View" org-agenda-fortnight-view
2356 :active (org-agenda-check-type nil 'agenda)
2357 :style radio :selected (eq org-agenda-current-span 'fortnight)
2358 :keys "v f"]
2359 ["Month View" org-agenda-month-view
2360 :active (org-agenda-check-type nil 'agenda)
2361 :style radio :selected (eq org-agenda-current-span 'month)
2362 :keys "v m"]
2363 ["Year View" org-agenda-year-view
2364 :active (org-agenda-check-type nil 'agenda)
2365 :style radio :selected (eq org-agenda-current-span 'year)
2366 :keys "v y"]
2367 "--"
2368 ["Include Diary" org-agenda-toggle-diary
2369 :style toggle :selected org-agenda-include-diary
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Include Deadlines" org-agenda-toggle-deadlines
2372 :style toggle :selected org-agenda-include-deadlines
2373 :active (org-agenda-check-type nil 'agenda)]
2374 ["Use Time Grid" org-agenda-toggle-time-grid
2375 :style toggle :selected org-agenda-use-time-grid
2376 :active (org-agenda-check-type nil 'agenda)]
2377 "--"
2378 ["Show clock report" org-agenda-clockreport-mode
2379 :style toggle :selected org-agenda-clockreport-mode
2380 :active (org-agenda-check-type nil 'agenda)]
2381 ["Show some entry text" org-agenda-entry-text-mode
2382 :style toggle :selected org-agenda-entry-text-mode
2383 :active t]
2384 "--"
2385 ["Show Logbook entries" org-agenda-log-mode
2386 :style toggle :selected org-agenda-show-log
2387 :active (org-agenda-check-type nil 'agenda 'timeline)
2388 :keys "v l (or just l)"]
2389 ["Include archived trees" org-agenda-archives-mode
2390 :style toggle :selected org-agenda-archives-mode :active t
2391 :keys "v a"]
2392 ["Include archive files" (org-agenda-archives-mode t)
2393 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2394 :keys "v A"]
2395 "--"
2396 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2397 ["Write view to file" org-agenda-write t]
2398 ["Rebuild buffer" org-agenda-redo t]
2399 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2400 "--"
2401 ["Show original entry" org-agenda-show t]
2402 ["Go To (other window)" org-agenda-goto t]
2403 ["Go To (this window)" org-agenda-switch-to t]
2404 ["Capture with cursor date" org-agenda-capture t]
2405 ["Follow Mode" org-agenda-follow-mode
2406 :style toggle :selected org-agenda-follow-mode :active t]
2407 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2408 "--"
2409 ("TODO"
2410 ["Cycle TODO" org-agenda-todo t]
2411 ["Next TODO set" org-agenda-todo-nextset t]
2412 ["Previous TODO set" org-agenda-todo-previousset t]
2413 ["Add note" org-agenda-add-note t])
2414 ("Archive/Refile/Delete"
2415 ["Archive default" org-agenda-archive-default t]
2416 ["Archive default" org-agenda-archive-default-with-confirmation t]
2417 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2418 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2419 ["Archive subtree" org-agenda-archive t]
2420 "--"
2421 ["Refile" org-agenda-refile t]
2422 "--"
2423 ["Delete subtree" org-agenda-kill t])
2424 ("Bulk action"
2425 ["Mark entry" org-agenda-bulk-mark t]
2426 ["Mark all" org-agenda-bulk-mark-all t]
2427 ["Unmark entry" org-agenda-bulk-unmark t]
2428 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2429 ["Toggle mark" org-agenda-bulk-toggle t]
2430 ["Toggle all" org-agenda-bulk-toggle-all t]
2431 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2432 ["Act on all marked" org-agenda-bulk-action t]
2433 "--"
2434 ("Tags and Properties"
2435 ["Show all Tags" org-agenda-show-tags t]
2436 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2437 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2438 "--"
2439 ["Column View" org-columns t])
2440 ("Deadline/Schedule"
2441 ["Schedule" org-agenda-schedule t]
2442 ["Set Deadline" org-agenda-deadline t]
2443 "--"
2444 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2445 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2446 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2447 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2448 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2449 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2450 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2451 ("Clock and Effort"
2452 ["Clock in" org-agenda-clock-in t]
2453 ["Clock out" org-agenda-clock-out t]
2454 ["Clock cancel" org-agenda-clock-cancel t]
2455 ["Goto running clock" org-clock-goto t]
2456 "--"
2457 ["Set Effort" org-agenda-set-effort t]
2458 ["Change clocked effort" org-clock-modify-effort-estimate
2459 (org-clock-is-active)])
2460 ("Priority"
2461 ["Set Priority" org-agenda-priority t]
2462 ["Increase Priority" org-agenda-priority-up t]
2463 ["Decrease Priority" org-agenda-priority-down t]
2464 ["Show Priority" org-show-priority t])
2465 ("Calendar/Diary"
2466 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2467 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2468 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2469 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2470 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2471 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2472 "--"
2473 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2474 "--"
2475 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2476 "--"
2477 ("MobileOrg"
2478 ["Push Files and Views" org-mobile-push t]
2479 ["Get Captured and Flagged" org-mobile-pull t]
2480 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2481 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2482 "--"
2483 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2484 "--"
2485 ["Quit" org-agenda-quit t]
2486 ["Exit and Release Buffers" org-agenda-exit t]
2489 ;;; Agenda undo
2491 (defvar org-agenda-allow-remote-undo t
2492 "Non-nil means allow remote undo from the agenda buffer.")
2493 (defvar org-agenda-undo-has-started-in nil
2494 "Buffers that have already seen `undo-start' in the current undo sequence.")
2496 (defun org-agenda-undo ()
2497 "Undo a remote editing step in the agenda.
2498 This undoes changes both in the agenda buffer and in the remote buffer
2499 that have been changed along."
2500 (interactive)
2501 (or org-agenda-allow-remote-undo
2502 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2503 (if (not (eq this-command last-command))
2504 (setq org-agenda-undo-has-started-in nil
2505 org-agenda-pending-undo-list org-agenda-undo-list))
2506 (if (not org-agenda-pending-undo-list)
2507 (user-error "No further undo information"))
2508 (let* ((entry (pop org-agenda-pending-undo-list))
2509 buf line cmd rembuf)
2510 (setq cmd (pop entry) line (pop entry))
2511 (setq rembuf (nth 2 entry))
2512 (org-with-remote-undo rembuf
2513 (while (bufferp (setq buf (pop entry)))
2514 (if (pop entry)
2515 (with-current-buffer buf
2516 (let ((last-undo-buffer buf)
2517 (inhibit-read-only t))
2518 (unless (memq buf org-agenda-undo-has-started-in)
2519 (push buf org-agenda-undo-has-started-in)
2520 (make-local-variable 'pending-undo-list)
2521 (undo-start))
2522 (while (and pending-undo-list
2523 (listp pending-undo-list)
2524 (not (car pending-undo-list)))
2525 (pop pending-undo-list))
2526 (undo-more 1))))))
2527 (org-goto-line line)
2528 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2530 (defun org-verify-change-for-undo (l1 l2)
2531 "Verify that a real change occurred between the undo lists L1 and L2."
2532 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2533 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2534 (not (eq l1 l2)))
2536 ;;; Agenda dispatch
2538 (defvar org-agenda-restrict-begin (make-marker))
2539 (defvar org-agenda-restrict-end (make-marker))
2540 (defvar org-agenda-last-dispatch-buffer nil)
2541 (defvar org-agenda-overriding-restriction nil)
2543 (defcustom org-agenda-custom-commands-contexts nil
2544 "Alist of custom agenda keys and contextual rules.
2546 For example, if you have a custom agenda command \"p\" and you
2547 want this command to be accessible only from plain text files,
2548 use this:
2550 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2552 Here are the available contexts definitions:
2554 in-file: command displayed only in matching files
2555 in-mode: command displayed only in matching modes
2556 not-in-file: command not displayed in matching files
2557 not-in-mode: command not displayed in matching modes
2558 in-buffer: command displayed only in matching buffers
2559 not-in-buffer: command not displayed in matching buffers
2560 [function]: a custom function taking no argument
2562 If you define several checks, the agenda command will be
2563 accessible if there is at least one valid check.
2565 You can also bind a key to another agenda custom command
2566 depending on contextual rules.
2568 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2570 Here it means: in .txt files, use \"p\" as the key for the
2571 agenda command otherwise associated with \"q\". (The command
2572 originally associated with \"q\" is not displayed to avoid
2573 duplicates.)"
2574 :version "24.3"
2575 :group 'org-agenda-custom-commands
2576 :type '(repeat (list :tag "Rule"
2577 (string :tag " Agenda key")
2578 (string :tag "Replace by command")
2579 (repeat :tag "Available when"
2580 (choice
2581 (cons :tag "Condition"
2582 (choice
2583 (const :tag "In file" in-file)
2584 (const :tag "Not in file" not-in-file)
2585 (const :tag "In buffer" in-buffer)
2586 (const :tag "Not in buffer" not-in-buffer)
2587 (const :tag "In mode" in-mode)
2588 (const :tag "Not in mode" not-in-mode))
2589 (regexp))
2590 (function :tag "Custom function"))))))
2592 (defcustom org-agenda-max-entries nil
2593 "Maximum number of entries to display in an agenda.
2594 This can be nil (no limit) or an integer or an alist of agenda
2595 types with an associated number of entries to display in this
2596 type."
2597 :version "24.4"
2598 :package-version '(Org . "8.0")
2599 :group 'org-agenda-custom-commands
2600 :type '(choice (symbol :tag "No limit" nil)
2601 (integer :tag "Max number of entries")
2602 (repeat
2603 (cons (choice :tag "Agenda type"
2604 (const agenda)
2605 (const todo)
2606 (const tags)
2607 (const search)
2608 (const timeline))
2609 (integer :tag "Max number of entries")))))
2611 (defcustom org-agenda-max-todos nil
2612 "Maximum number of TODOs to display in an agenda.
2613 This can be nil (no limit) or an integer or an alist of agenda
2614 types with an associated number of entries to display in this
2615 type."
2616 :version "24.4"
2617 :package-version '(Org . "8.0")
2618 :group 'org-agenda-custom-commands
2619 :type '(choice (symbol :tag "No limit" nil)
2620 (integer :tag "Max number of TODOs")
2621 (repeat
2622 (cons (choice :tag "Agenda type"
2623 (const agenda)
2624 (const todo)
2625 (const tags)
2626 (const search)
2627 (const timeline))
2628 (integer :tag "Max number of TODOs")))))
2630 (defcustom org-agenda-max-tags nil
2631 "Maximum number of tagged entries to display in an agenda.
2632 This can be nil (no limit) or an integer or an alist of agenda
2633 types with an associated number of entries to display in this
2634 type."
2635 :version "24.4"
2636 :package-version '(Org . "8.0")
2637 :group 'org-agenda-custom-commands
2638 :type '(choice (symbol :tag "No limit" nil)
2639 (integer :tag "Max number of tagged entries")
2640 (repeat
2641 (cons (choice :tag "Agenda type"
2642 (const agenda)
2643 (const todo)
2644 (const tags)
2645 (const search)
2646 (const timeline))
2647 (integer :tag "Max number of tagged entries")))))
2649 (defcustom org-agenda-max-effort nil
2650 "Maximum cumulated effort duration for the agenda.
2651 This can be nil (no limit) or a number of minutes (as an integer)
2652 or an alist of agenda types with an associated number of minutes
2653 to limit entries to in this type."
2654 :version "24.4"
2655 :package-version '(Org . "8.0")
2656 :group 'org-agenda-custom-commands
2657 :type '(choice (symbol :tag "No limit" nil)
2658 (integer :tag "Max number of minutes")
2659 (repeat
2660 (cons (choice :tag "Agenda type"
2661 (const agenda)
2662 (const todo)
2663 (const tags)
2664 (const search)
2665 (const timeline))
2666 (integer :tag "Max number of minutes")))))
2668 (defvar org-keys nil)
2669 (defvar org-match nil)
2670 ;;;###autoload
2671 (defun org-agenda (&optional arg org-keys restriction)
2672 "Dispatch agenda commands to collect entries to the agenda buffer.
2673 Prompts for a command to execute. Any prefix arg will be passed
2674 on to the selected command. The default selections are:
2676 a Call `org-agenda-list' to display the agenda for current day or week.
2677 t Call `org-todo-list' to display the global todo list.
2678 T Call `org-todo-list' to display the global todo list, select only
2679 entries with a specific TODO keyword (the user gets a prompt).
2680 m Call `org-tags-view' to display headlines with tags matching
2681 a condition (the user is prompted for the condition).
2682 M Like `m', but select only TODO entries, no ordinary headlines.
2683 L Create a timeline for the current buffer.
2684 e Export views to associated files.
2685 s Search entries for keywords.
2686 S Search entries for keywords, only with TODO keywords.
2687 / Multi occur across all agenda files and also files listed
2688 in `org-agenda-text-search-extra-files'.
2689 < Restrict agenda commands to buffer, subtree, or region.
2690 Press several times to get the desired effect.
2691 > Remove a previous restriction.
2692 # List \"stuck\" projects.
2693 ! Configure what \"stuck\" means.
2694 C Configure custom agenda commands.
2696 More commands can be added by configuring the variable
2697 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2698 searches can be pre-defined in this way.
2700 If the current buffer is in Org-mode and visiting a file, you can also
2701 first press `<' once to indicate that the agenda should be temporarily
2702 \(until the next use of \\[org-agenda]) restricted to the current file.
2703 Pressing `<' twice means to restrict to the current subtree or region
2704 \(if active)."
2705 (interactive "P")
2706 (catch 'exit
2707 (let* ((prefix-descriptions nil)
2708 (org-agenda-buffer-name org-agenda-buffer-name)
2709 (org-agenda-window-setup (if (equal (buffer-name)
2710 org-agenda-buffer-name)
2711 'current-window
2712 org-agenda-window-setup))
2713 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2714 (org-agenda-custom-commands
2715 ;; normalize different versions
2716 (delq nil
2717 (mapcar
2718 (lambda (x)
2719 (cond ((stringp (cdr x))
2720 (push x prefix-descriptions)
2721 nil)
2722 ((stringp (nth 1 x)) x)
2723 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2724 (t (cons (car x) (cons "" (cdr x))))))
2725 org-agenda-custom-commands)))
2726 (org-agenda-custom-commands
2727 (org-contextualize-keys
2728 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2729 (buf (current-buffer))
2730 (bfn (buffer-file-name (buffer-base-buffer)))
2731 entry key type org-match lprops ans)
2732 ;; Turn off restriction unless there is an overriding one,
2733 (unless org-agenda-overriding-restriction
2734 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2735 ;; There is a request to keep the file list in place
2736 (put 'org-agenda-files 'org-restrict nil))
2737 (setq org-agenda-restrict nil)
2738 (move-marker org-agenda-restrict-begin nil)
2739 (move-marker org-agenda-restrict-end nil))
2740 ;; Delete old local properties
2741 (put 'org-agenda-redo-command 'org-lprops nil)
2742 ;; Delete previously set last-arguments
2743 (put 'org-agenda-redo-command 'last-args nil)
2744 ;; Remember where this call originated
2745 (setq org-agenda-last-dispatch-buffer (current-buffer))
2746 (unless org-keys
2747 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2748 org-keys (car ans)
2749 restriction (cdr ans)))
2750 ;; If we have sticky agenda buffers, set a name for the buffer,
2751 ;; depending on the invoking keys. The user may still set this
2752 ;; as a command option, which will overwrite what we do here.
2753 (if org-agenda-sticky
2754 (setq org-agenda-buffer-name
2755 (format "*Org Agenda(%s)*" org-keys)))
2756 ;; Establish the restriction, if any
2757 (when (and (not org-agenda-overriding-restriction) restriction)
2758 (put 'org-agenda-files 'org-restrict (list bfn))
2759 (cond
2760 ((eq restriction 'region)
2761 (setq org-agenda-restrict (current-buffer))
2762 (move-marker org-agenda-restrict-begin (region-beginning))
2763 (move-marker org-agenda-restrict-end (region-end)))
2764 ((eq restriction 'subtree)
2765 (save-excursion
2766 (setq org-agenda-restrict (current-buffer))
2767 (org-back-to-heading t)
2768 (move-marker org-agenda-restrict-begin (point))
2769 (move-marker org-agenda-restrict-end
2770 (progn (org-end-of-subtree t)))))))
2772 ;; For example the todo list should not need it (but does...)
2773 (cond
2774 ((setq entry (assoc org-keys org-agenda-custom-commands))
2775 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2776 (progn
2777 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2778 lprops (nth 4 entry))
2779 (if org-agenda-sticky
2780 (setq org-agenda-buffer-name
2781 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2782 (format "*Org Agenda(%s)*" org-keys))))
2783 (put 'org-agenda-redo-command 'org-lprops lprops)
2784 (cond
2785 ((eq type 'agenda)
2786 (org-let lprops '(org-agenda-list current-prefix-arg)))
2787 ((eq type 'agenda*)
2788 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2789 ((eq type 'alltodo)
2790 (org-let lprops '(org-todo-list current-prefix-arg)))
2791 ((eq type 'search)
2792 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2793 ((eq type 'stuck)
2794 (org-let lprops '(org-agenda-list-stuck-projects
2795 current-prefix-arg)))
2796 ((eq type 'tags)
2797 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2798 ((eq type 'tags-todo)
2799 (org-let lprops '(org-tags-view '(4) org-match)))
2800 ((eq type 'todo)
2801 (org-let lprops '(org-todo-list org-match)))
2802 ((eq type 'tags-tree)
2803 (org-check-for-org-mode)
2804 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2805 ((eq type 'todo-tree)
2806 (org-check-for-org-mode)
2807 (org-let lprops
2808 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2809 (regexp-quote org-match) "\\>"))))
2810 ((eq type 'occur-tree)
2811 (org-check-for-org-mode)
2812 (org-let lprops '(org-occur org-match)))
2813 ((functionp type)
2814 (org-let lprops '(funcall type org-match)))
2815 ((fboundp type)
2816 (org-let lprops '(funcall type org-match)))
2817 (t (user-error "Invalid custom agenda command type %s" type))))
2818 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2819 ((equal org-keys "C")
2820 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2821 (customize-variable 'org-agenda-custom-commands))
2822 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2823 ((equal org-keys "s") (call-interactively 'org-search-view))
2824 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2825 ((equal org-keys "t") (call-interactively 'org-todo-list))
2826 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2827 ((equal org-keys "m") (call-interactively 'org-tags-view))
2828 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2829 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2830 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2831 (org-add-hook
2832 'post-command-hook
2833 (lambda ()
2834 (unless (current-message)
2835 (let* ((m (org-agenda-get-any-marker))
2836 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2837 (when note
2838 (message (concat
2839 "FLAGGING-NOTE ([?] for more info): "
2840 (org-add-props
2841 (replace-regexp-in-string
2842 "\\\\n" "//"
2843 (copy-sequence note))
2844 nil 'face 'org-warning)))))))
2845 t t))
2846 ((equal org-keys "L")
2847 (unless (derived-mode-p 'org-mode)
2848 (user-error "This is not an Org-mode file"))
2849 (unless restriction
2850 (put 'org-agenda-files 'org-restrict (list bfn))
2851 (org-call-with-arg 'org-timeline arg)))
2852 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2853 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2854 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2855 (t (user-error "Invalid agenda key"))))))
2857 (defvar org-agenda-multi)
2859 (defun org-agenda-append-agenda ()
2860 "Append another agenda view to the current one.
2861 This function allows interactive building of block agendas.
2862 Agenda views are separated by `org-agenda-block-separator'."
2863 (interactive)
2864 (unless (derived-mode-p 'org-agenda-mode)
2865 (user-error "Can only append from within agenda buffer"))
2866 (let ((org-agenda-multi t))
2867 (org-agenda)
2868 (widen)
2869 (org-agenda-finalize)
2870 (setq buffer-read-only t)
2871 (org-agenda-fit-window-to-buffer)))
2873 (defun org-agenda-normalize-custom-commands (cmds)
2874 "Normalize custom commands CMDS."
2875 (delq nil
2876 (mapcar
2877 (lambda (x)
2878 (cond ((stringp (cdr x)) nil)
2879 ((stringp (nth 1 x)) x)
2880 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2881 (t (cons (car x) (cons "" (cdr x))))))
2882 cmds)))
2884 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2885 "The user interface for selecting an agenda command."
2886 (catch 'exit
2887 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2888 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2889 (region-p (org-region-active-p))
2890 (custom org-agenda-custom-commands)
2891 (selstring "")
2892 restriction second-time
2893 c entry key type match prefixes rmheader header-end custom1 desc
2894 line lines left right n n1)
2895 (save-window-excursion
2896 (delete-other-windows)
2897 (org-switch-to-buffer-other-window " *Agenda Commands*")
2898 (erase-buffer)
2899 (insert (eval-when-compile
2900 (let ((header
2901 "Press key for an agenda command: < Buffer, subtree/region restriction
2902 -------------------------------- > Remove restriction
2903 a Agenda for current week or day e Export agenda views
2904 t List of all TODO entries T Entries with special TODO kwd
2905 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2906 s Search for keywords S Like s, but only TODO entries
2907 L Timeline for current buffer # List stuck projects (!=configure)
2908 / Multi-occur C Configure custom agenda commands
2909 ? Find :FLAGGED: entries * Toggle sticky agenda views
2911 (start 0))
2912 (while (string-match
2913 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2914 header start)
2915 (setq start (match-end 0))
2916 (add-text-properties (match-beginning 2) (match-end 2)
2917 '(face bold) header))
2918 header)))
2919 (setq header-end (point-marker))
2920 (while t
2921 (setq custom1 custom)
2922 (when (eq rmheader t)
2923 (org-goto-line 1)
2924 (re-search-forward ":" nil t)
2925 (delete-region (match-end 0) (point-at-eol))
2926 (forward-char 1)
2927 (looking-at "-+")
2928 (delete-region (match-end 0) (point-at-eol))
2929 (move-marker header-end (match-end 0)))
2930 (goto-char header-end)
2931 (delete-region (point) (point-max))
2933 ;; Produce all the lines that describe custom commands and prefixes
2934 (setq lines nil)
2935 (while (setq entry (pop custom1))
2936 (setq key (car entry) desc (nth 1 entry)
2937 type (nth 2 entry)
2938 match (nth 3 entry))
2939 (if (> (length key) 1)
2940 (add-to-list 'prefixes (string-to-char key))
2941 (setq line
2942 (format
2943 "%-4s%-14s"
2944 (org-add-props (copy-sequence key)
2945 '(face bold))
2946 (cond
2947 ((string-match "\\S-" desc) desc)
2948 ((eq type 'agenda) "Agenda for current week or day")
2949 ((eq type 'agenda*) "Appointments for current week or day")
2950 ((eq type 'alltodo) "List of all TODO entries")
2951 ((eq type 'search) "Word search")
2952 ((eq type 'stuck) "List of stuck projects")
2953 ((eq type 'todo) "TODO keyword")
2954 ((eq type 'tags) "Tags query")
2955 ((eq type 'tags-todo) "Tags (TODO)")
2956 ((eq type 'tags-tree) "Tags tree")
2957 ((eq type 'todo-tree) "TODO kwd tree")
2958 ((eq type 'occur-tree) "Occur tree")
2959 ((functionp type) (if (symbolp type)
2960 (symbol-name type)
2961 "Lambda expression"))
2962 (t "???"))))
2963 (if org-agenda-menu-show-matcher
2964 (setq line
2965 (concat line ": "
2966 (cond
2967 ((stringp match)
2968 (setq match (copy-sequence match))
2969 (org-add-props match nil 'face 'org-warning))
2970 ((listp type)
2971 (format "set of %d commands" (length type))))))
2972 (if (org-string-nw-p match)
2973 (add-text-properties
2974 0 (length line) (list 'help-echo
2975 (concat "Matcher: " match)) line)))
2976 (push line lines)))
2977 (setq lines (nreverse lines))
2978 (when prefixes
2979 (mapc (lambda (x)
2980 (push
2981 (format "%s %s"
2982 (org-add-props (char-to-string x)
2983 nil 'face 'bold)
2984 (or (cdr (assoc (concat selstring
2985 (char-to-string x))
2986 prefix-descriptions))
2987 "Prefix key"))
2988 lines))
2989 prefixes))
2991 ;; Check if we should display in two columns
2992 (if org-agenda-menu-two-columns
2993 (progn
2994 (setq n (length lines)
2995 n1 (+ (/ n 2) (mod n 2))
2996 right (nthcdr n1 lines)
2997 left (copy-sequence lines))
2998 (setcdr (nthcdr (1- n1) left) nil))
2999 (setq left lines right nil))
3000 (while left
3001 (insert "\n" (pop left))
3002 (when right
3003 (if (< (current-column) 40)
3004 (move-to-column 40 t)
3005 (insert " "))
3006 (insert (pop right))))
3008 ;; Make the window the right size
3009 (goto-char (point-min))
3010 (if second-time
3011 (if (not (pos-visible-in-window-p (point-max)))
3012 (org-fit-window-to-buffer))
3013 (setq second-time t)
3014 (org-fit-window-to-buffer))
3016 ;; Ask for selection
3017 (message "Press key for agenda command%s:"
3018 (if (or restrict-ok org-agenda-overriding-restriction)
3019 (if org-agenda-overriding-restriction
3020 " (restriction lock active)"
3021 (if restriction
3022 (format " (restricted to %s)" restriction)
3023 " (unrestricted)"))
3024 ""))
3025 (setq c (read-char-exclusive))
3026 (message "")
3027 (cond
3028 ((assoc (char-to-string c) custom)
3029 (setq selstring (concat selstring (char-to-string c)))
3030 (throw 'exit (cons selstring restriction)))
3031 ((memq c prefixes)
3032 (setq selstring (concat selstring (char-to-string c))
3033 prefixes nil
3034 rmheader (or rmheader t)
3035 custom (delq nil (mapcar
3036 (lambda (x)
3037 (if (or (= (length (car x)) 1)
3038 (/= (string-to-char (car x)) c))
3040 (cons (substring (car x) 1) (cdr x))))
3041 custom))))
3042 ((eq c ?*)
3043 (call-interactively 'org-toggle-sticky-agenda)
3044 (sit-for 2))
3045 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3046 (message "Restriction is only possible in Org-mode buffers")
3047 (ding) (sit-for 1))
3048 ((eq c ?1)
3049 (org-agenda-remove-restriction-lock 'noupdate)
3050 (setq restriction 'buffer))
3051 ((eq c ?0)
3052 (org-agenda-remove-restriction-lock 'noupdate)
3053 (setq restriction (if region-p 'region 'subtree)))
3054 ((eq c ?<)
3055 (org-agenda-remove-restriction-lock 'noupdate)
3056 (setq restriction
3057 (cond
3058 ((eq restriction 'buffer)
3059 (if region-p 'region 'subtree))
3060 ((memq restriction '(subtree region))
3061 nil)
3062 (t 'buffer))))
3063 ((eq c ?>)
3064 (org-agenda-remove-restriction-lock 'noupdate)
3065 (setq restriction nil))
3066 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3067 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3068 ((and (> (length selstring) 0) (eq c ?\d))
3069 (delete-window)
3070 (org-agenda-get-restriction-and-command prefix-descriptions))
3072 ((equal c ?q) (error "Abort"))
3073 (t (user-error "Invalid key %c" c))))))))
3075 (defun org-agenda-fit-window-to-buffer ()
3076 "Fit the window to the buffer size."
3077 (and (memq org-agenda-window-setup '(reorganize-frame))
3078 (fboundp 'fit-window-to-buffer)
3079 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3080 (= (car org-agenda-window-frame-fractions) 1.0))
3081 (delete-other-windows)
3082 (org-fit-window-to-buffer
3084 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3085 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3087 (defvar org-cmd nil)
3088 (defvar org-agenda-overriding-cmd nil)
3089 (defvar org-agenda-overriding-arguments nil)
3090 (defvar org-agenda-overriding-cmd-arguments nil)
3091 (defun org-agenda-run-series (name series)
3092 "Run agenda NAME as a SERIES of agenda commands."
3093 (org-let (nth 1 series) '(org-agenda-prepare name))
3094 ;; We need to reset agenda markers here, because when constructing a
3095 ;; block agenda, the individual blocks do not do that.
3096 (org-agenda-reset-markers)
3097 (let* ((org-agenda-multi t)
3098 (redo (list 'org-agenda-run-series name (list 'quote series)))
3099 (cmds (car series))
3100 (gprops (nth 1 series))
3101 match ;; The byte compiler incorrectly complains about this. Keep it!
3102 org-cmd type lprops)
3103 (while (setq org-cmd (pop cmds))
3104 (setq type (car org-cmd)
3105 match (eval (nth 1 org-cmd))
3106 lprops (nth 2 org-cmd))
3107 (let ((org-agenda-overriding-arguments
3108 (if (eq org-agenda-overriding-cmd org-cmd)
3109 (or org-agenda-overriding-arguments
3110 org-agenda-overriding-cmd-arguments))))
3111 (cond
3112 ((eq type 'agenda)
3113 (org-let2 gprops lprops
3114 '(call-interactively 'org-agenda-list)))
3115 ((eq type 'agenda*)
3116 (org-let2 gprops lprops
3117 '(funcall 'org-agenda-list nil nil t)))
3118 ((eq type 'alltodo)
3119 (org-let2 gprops lprops
3120 '(call-interactively 'org-todo-list)))
3121 ((eq type 'search)
3122 (org-let2 gprops lprops
3123 '(org-search-view current-prefix-arg match nil)))
3124 ((eq type 'stuck)
3125 (org-let2 gprops lprops
3126 '(call-interactively 'org-agenda-list-stuck-projects)))
3127 ((eq type 'tags)
3128 (org-let2 gprops lprops
3129 '(org-tags-view current-prefix-arg match)))
3130 ((eq type 'tags-todo)
3131 (org-let2 gprops lprops
3132 '(org-tags-view '(4) match)))
3133 ((eq type 'todo)
3134 (org-let2 gprops lprops
3135 '(org-todo-list match)))
3136 ((fboundp type)
3137 (org-let2 gprops lprops
3138 '(funcall type match)))
3139 (t (error "Invalid type in command series")))))
3140 (widen)
3141 (let ((inhibit-read-only t))
3142 (add-text-properties (point-min) (point-max)
3143 `(org-series t org-series-redo-cmd ,redo)))
3144 (setq org-agenda-redo-command redo)
3145 (goto-char (point-min)))
3146 (org-agenda-fit-window-to-buffer)
3147 (org-let (nth 1 series) '(org-agenda-finalize)))
3149 ;;;###autoload
3150 (defmacro org-batch-agenda (cmd-key &rest parameters)
3151 "Run an agenda command in batch mode and send the result to STDOUT.
3152 If CMD-KEY is a string of length 1, it is used as a key in
3153 `org-agenda-custom-commands' and triggers this command. If it is a
3154 longer string it is used as a tags/todo match string.
3155 Parameters are alternating variable names and values that will be bound
3156 before running the agenda command."
3157 (org-eval-in-environment (org-make-parameter-alist parameters)
3158 (let (org-agenda-sticky)
3159 (if (> (length cmd-key) 2)
3160 (org-tags-view nil cmd-key)
3161 (org-agenda nil cmd-key))))
3162 (set-buffer org-agenda-buffer-name)
3163 (princ (buffer-string)))
3165 (defvar org-agenda-info nil)
3167 ;;;###autoload
3168 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3169 "Run an agenda command in batch mode and send the result to STDOUT.
3170 If CMD-KEY is a string of length 1, it is used as a key in
3171 `org-agenda-custom-commands' and triggers this command. If it is a
3172 longer string it is used as a tags/todo match string.
3173 Parameters are alternating variable names and values that will be bound
3174 before running the agenda command.
3176 The output gives a line for each selected agenda item. Each
3177 item is a list of comma-separated values, like this:
3179 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3181 category The category of the item
3182 head The headline, without TODO kwd, TAGS and PRIORITY
3183 type The type of the agenda entry, can be
3184 todo selected in TODO match
3185 tagsmatch selected in tags match
3186 diary imported from diary
3187 deadline a deadline on given date
3188 scheduled scheduled on given date
3189 timestamp entry has timestamp on given date
3190 closed entry was closed on given date
3191 upcoming-deadline warning about deadline
3192 past-scheduled forwarded scheduled item
3193 block entry has date block including g. date
3194 todo The todo keyword, if any
3195 tags All tags including inherited ones, separated by colons
3196 date The relevant date, like 2007-2-14
3197 time The time, like 15:00-16:50
3198 extra Sting with extra planning info
3199 priority-l The priority letter if any was given
3200 priority-n The computed numerical priority
3201 agenda-day The day in the agenda where this is listed"
3202 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3203 (org-make-parameter-alist parameters))
3204 (if (> (length cmd-key) 2)
3205 (org-tags-view nil cmd-key)
3206 (org-agenda nil cmd-key)))
3207 (set-buffer org-agenda-buffer-name)
3208 (let* ((lines (org-split-string (buffer-string) "\n"))
3209 line)
3210 (while (setq line (pop lines))
3211 (catch 'next
3212 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3213 (setq org-agenda-info
3214 (org-fix-agenda-info (text-properties-at 0 line)))
3215 (princ
3216 (mapconcat 'org-agenda-export-csv-mapper
3217 '(org-category txt type todo tags date time extra
3218 priority-letter priority agenda-day)
3219 ","))
3220 (princ "\n")))))
3222 (defun org-fix-agenda-info (props)
3223 "Make sure all properties on an agenda item have a canonical form.
3224 This ensures the export commands can easily use it."
3225 (let (tmp re)
3226 (when (setq tmp (plist-get props 'tags))
3227 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3228 (when (setq tmp (plist-get props 'date))
3229 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3230 (let ((calendar-date-display-form '(year "-" month "-" day)))
3231 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3233 (setq tmp (calendar-date-string tmp)))
3234 (setq props (plist-put props 'date tmp)))
3235 (when (setq tmp (plist-get props 'day))
3236 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3237 (let ((calendar-date-display-form '(year "-" month "-" day)))
3238 (setq tmp (calendar-date-string tmp)))
3239 (setq props (plist-put props 'day tmp))
3240 (setq props (plist-put props 'agenda-day tmp)))
3241 (when (setq tmp (plist-get props 'txt))
3242 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3243 (plist-put props 'priority-letter (match-string 1 tmp))
3244 (setq tmp (replace-match "" t t tmp)))
3245 (when (and (setq re (plist-get props 'org-todo-regexp))
3246 (setq re (concat "\\`\\.*" re " ?"))
3247 (string-match re tmp))
3248 (plist-put props 'todo (match-string 1 tmp))
3249 (setq tmp (replace-match "" t t tmp)))
3250 (plist-put props 'txt tmp)))
3251 props)
3253 (defun org-agenda-export-csv-mapper (prop)
3254 (let ((res (plist-get org-agenda-info prop)))
3255 (setq res
3256 (cond
3257 ((not res) "")
3258 ((stringp res) res)
3259 (t (prin1-to-string res))))
3260 (while (string-match "," res)
3261 (setq res (replace-match ";" t t res)))
3262 (org-trim res)))
3264 ;;;###autoload
3265 (defun org-store-agenda-views (&rest parameters)
3266 "Store agenda views."
3267 (interactive)
3268 (eval (list 'org-batch-store-agenda-views)))
3270 ;;;###autoload
3271 (defmacro org-batch-store-agenda-views (&rest parameters)
3272 "Run all custom agenda commands that have a file argument."
3273 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3274 (pop-up-frames nil)
3275 (dir default-directory)
3276 (pars (org-make-parameter-alist parameters))
3277 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3278 (save-window-excursion
3279 (while cmds
3280 (setq cmd (pop cmds)
3281 thiscmdkey (car cmd)
3282 thiscmdcmd (cdr cmd)
3283 match (nth 2 thiscmdcmd)
3284 bufname (if org-agenda-sticky
3285 (or (and (stringp match)
3286 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3287 (format "*Org Agenda(%s)*" thiscmdkey))
3288 org-agenda-buffer-name)
3289 cmd-or-set (nth 2 cmd)
3290 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3291 files (nth (if (listp cmd-or-set) 4 5) cmd))
3292 (if (stringp files) (setq files (list files)))
3293 (when files
3294 (org-eval-in-environment (append org-agenda-exporter-settings
3295 opts pars)
3296 (org-agenda nil thiscmdkey))
3297 (set-buffer bufname)
3298 (while files
3299 (org-eval-in-environment (append org-agenda-exporter-settings
3300 opts pars)
3301 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3302 (and (get-buffer bufname)
3303 (kill-buffer bufname)))))))
3305 (defvar org-agenda-current-span nil
3306 "The current span used in the agenda view.") ; local variable in the agenda buffer
3307 (defun org-agenda-mark-header-line (pos)
3308 "Mark the line at POS as an agenda structure header."
3309 (save-excursion
3310 (goto-char pos)
3311 (put-text-property (point-at-bol) (point-at-eol)
3312 'org-agenda-structural-header t)
3313 (when org-agenda-title-append
3314 (put-text-property (point-at-bol) (point-at-eol)
3315 'org-agenda-title-append org-agenda-title-append))))
3317 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3318 (defvar org-agenda-write-buffer-name "Agenda View")
3319 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3320 "Write the current buffer (an agenda view) as a file.
3322 Depending on the extension of the file name, plain text (.txt),
3323 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3324 If the extension is .ics, translate visible agenda into iCalendar
3325 format. If the extension is .org, collect all subtrees
3326 corresponding to the agenda entries and add them in an .org file.
3328 With prefix argument OPEN, open the new file immediately. If
3329 NOSETTINGS is given, do not scope the settings of
3330 `org-agenda-exporter-settings' into the export commands. This is
3331 used when the settings have already been scoped and we do not
3332 wish to overrule other, higher priority settings. If
3333 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3334 the agenda to write."
3335 (interactive "FWrite agenda to file: \nP")
3336 (if (or (not (file-writable-p file))
3337 (and (file-exists-p file)
3338 (if (org-called-interactively-p 'any)
3339 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3340 (user-error "Cannot write agenda to file %s" file))
3341 (org-let (if nosettings nil org-agenda-exporter-settings)
3342 '(save-excursion
3343 (save-window-excursion
3344 (let ((bs (copy-sequence (buffer-string))) beg content)
3345 (with-temp-buffer
3346 (rename-buffer org-agenda-write-buffer-name t)
3347 (set-buffer-modified-p nil)
3348 (insert bs)
3349 (org-agenda-remove-marked-text 'org-filtered)
3350 (run-hooks 'org-agenda-before-write-hook)
3351 (cond
3352 ((org-bound-and-true-p org-mobile-creating-agendas)
3353 (org-mobile-write-agenda-for-mobile file))
3354 ((string-match "\\.org\\'" file)
3355 (let (content p m message-log-max)
3356 (goto-char (point-min))
3357 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3358 (goto-char p)
3359 (setq m (get-text-property (point) 'org-hd-marker))
3360 (when m
3361 (push (save-excursion
3362 (set-buffer (marker-buffer m))
3363 (goto-char m)
3364 (org-copy-subtree 1 nil t t)
3365 org-subtree-clip)
3366 content)))
3367 (find-file file)
3368 (erase-buffer)
3369 (dolist (s content) (org-paste-subtree 1 s))
3370 (write-file file)
3371 (kill-buffer (current-buffer))
3372 (message "Org file written to %s" file)))
3373 ((string-match "\\.html?\\'" file)
3374 (require 'htmlize)
3375 (set-buffer (htmlize-buffer (current-buffer)))
3376 (when org-agenda-export-html-style
3377 ;; replace <style> section with org-agenda-export-html-style
3378 (goto-char (point-min))
3379 (kill-region (- (search-forward "<style") 6)
3380 (search-forward "</style>"))
3381 (insert org-agenda-export-html-style))
3382 (write-file file)
3383 (kill-buffer (current-buffer))
3384 (message "HTML written to %s" file))
3385 ((string-match "\\.ps\\'" file)
3386 (require 'ps-print)
3387 (ps-print-buffer-with-faces file)
3388 (message "Postscript written to %s" file))
3389 ((string-match "\\.pdf\\'" file)
3390 (require 'ps-print)
3391 (ps-print-buffer-with-faces
3392 (concat (file-name-sans-extension file) ".ps"))
3393 (call-process "ps2pdf" nil nil nil
3394 (expand-file-name
3395 (concat (file-name-sans-extension file) ".ps"))
3396 (expand-file-name file))
3397 (delete-file (concat (file-name-sans-extension file) ".ps"))
3398 (message "PDF written to %s" file))
3399 ((string-match "\\.ics\\'" file)
3400 (require 'ox-icalendar)
3401 (org-icalendar-export-current-agenda (expand-file-name file)))
3403 (let ((bs (buffer-string)))
3404 (find-file file)
3405 (erase-buffer)
3406 (insert bs)
3407 (save-buffer 0)
3408 (kill-buffer (current-buffer))
3409 (message "Plain text written to %s" file))))))))
3410 (set-buffer (or agenda-bufname
3411 (and (org-called-interactively-p 'any) (buffer-name))
3412 org-agenda-buffer-name)))
3413 (when open (org-open-file file)))
3415 (defun org-agenda-remove-marked-text (property &optional value)
3416 "Delete all text marked with VALUE of PROPERTY.
3417 VALUE defaults to t."
3418 (let (beg)
3419 (setq value (or value t))
3420 (while (setq beg (text-property-any (point-min) (point-max)
3421 property value))
3422 (delete-region
3423 beg (or (next-single-property-change beg property)
3424 (point-max))))))
3426 (defun org-agenda-add-entry-text ()
3427 "Add entry text to agenda lines.
3428 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3429 entry text following headings shown in the agenda.
3430 Drawers will be excluded, also the line with scheduling/deadline info."
3431 (when (and (> org-agenda-add-entry-text-maxlines 0)
3432 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3433 (let (m txt)
3434 (goto-char (point-min))
3435 (while (not (eobp))
3436 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3437 (beginning-of-line 2)
3438 (setq txt (org-agenda-get-some-entry-text
3439 m org-agenda-add-entry-text-maxlines " > "))
3440 (end-of-line 1)
3441 (if (string-match "\\S-" txt)
3442 (insert "\n" txt)
3443 (or (eobp) (forward-char 1))))))))
3445 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3446 &rest keep)
3447 "Extract entry text from MARKER, at most N-LINES lines.
3448 This will ignore drawers etc, just get the text.
3449 If INDENT is given, prefix every line with this string. If KEEP is
3450 given, it is a list of symbols, defining stuff that should not be
3451 removed from the entry content. Currently only `planning' is allowed here."
3452 (let (txt drawer-re kwd-time-re ind)
3453 (save-excursion
3454 (with-current-buffer (marker-buffer marker)
3455 (if (not (derived-mode-p 'org-mode))
3456 (setq txt "")
3457 (save-excursion
3458 (save-restriction
3459 (widen)
3460 (goto-char marker)
3461 (end-of-line 1)
3462 (setq txt (buffer-substring
3463 (min (1+ (point)) (point-max))
3464 (progn (outline-next-heading) (point)))
3465 drawer-re org-drawer-regexp
3466 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3467 ".*\n?"))
3468 (with-temp-buffer
3469 (insert txt)
3470 (when org-agenda-add-entry-text-descriptive-links
3471 (goto-char (point-min))
3472 (while (org-activate-bracket-links (point-max))
3473 (add-text-properties (match-beginning 0) (match-end 0)
3474 '(face org-link))))
3475 (goto-char (point-min))
3476 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3477 (set-text-properties (match-beginning 0) (match-end 0)
3478 nil))
3479 (goto-char (point-min))
3480 (while (re-search-forward drawer-re nil t)
3481 (delete-region
3482 (match-beginning 0)
3483 (progn (re-search-forward
3484 "^[ \t]*:END:.*\n?" nil 'move)
3485 (point))))
3486 (unless (member 'planning keep)
3487 (goto-char (point-min))
3488 (while (re-search-forward kwd-time-re nil t)
3489 (replace-match "")))
3490 (goto-char (point-min))
3491 (when org-agenda-entry-text-exclude-regexps
3492 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3493 (while (setq re (pop re-list))
3494 (goto-char (point-min))
3495 (while (re-search-forward re nil t)
3496 (replace-match "")))))
3497 (goto-char (point-max))
3498 (skip-chars-backward " \t\n")
3499 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3501 ;; find and remove min common indentation
3502 (goto-char (point-min))
3503 (untabify (point-min) (point-max))
3504 (setq ind (org-get-indentation))
3505 (while (not (eobp))
3506 (unless (looking-at "[ \t]*$")
3507 (setq ind (min ind (org-get-indentation))))
3508 (beginning-of-line 2))
3509 (goto-char (point-min))
3510 (while (not (eobp))
3511 (unless (looking-at "[ \t]*$")
3512 (move-to-column ind)
3513 (delete-region (point-at-bol) (point)))
3514 (beginning-of-line 2))
3516 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3518 (goto-char (point-min))
3519 (when indent
3520 (while (and (not (eobp)) (re-search-forward "^" nil t))
3521 (replace-match indent t t)))
3522 (goto-char (point-min))
3523 (while (looking-at "[ \t]*\n") (replace-match ""))
3524 (goto-char (point-max))
3525 (when (> (org-current-line)
3526 n-lines)
3527 (org-goto-line (1+ n-lines))
3528 (backward-char 1))
3529 (setq txt (buffer-substring (point-min) (point)))))))))
3530 txt))
3532 (defun org-check-for-org-mode ()
3533 "Make sure current buffer is in org-mode. Error if not."
3534 (or (derived-mode-p 'org-mode)
3535 (error "Cannot execute org-mode agenda command on buffer in %s"
3536 major-mode)))
3538 ;;; Agenda prepare and finalize
3540 (defvar org-agenda-multi nil) ; dynamically scoped
3541 (defvar org-agenda-pre-window-conf nil)
3542 (defvar org-agenda-columns-active nil)
3543 (defvar org-agenda-name nil)
3544 (defvar org-agenda-tag-filter nil)
3545 (defvar org-agenda-category-filter nil)
3546 (defvar org-agenda-regexp-filter nil)
3547 (defvar org-agenda-effort-filter nil)
3548 (defvar org-agenda-top-headline-filter nil)
3549 (defvar org-agenda-tag-filter-preset nil
3550 "A preset of the tags filter used for secondary agenda filtering.
3551 This must be a list of strings, each string must be a single tag preceded
3552 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-category-filter-preset nil
3560 "A preset of the category filter used for secondary agenda filtering.
3561 This must be a list of strings, each string must be a single category
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-regexp-filter-preset nil
3570 "A preset of the regexp filter 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 (defvar org-agenda-effort-filter-preset nil
3580 "A preset of the effort condition used for secondary agenda filtering.
3581 This must be a list of strings, each string must be a single regexp
3582 preceded by \"+\" or \"-\".
3583 This variable should not be set directly, but agenda custom commands can
3584 bind it in the options section. The preset filter is a global property of
3585 the entire agenda view. In a block agenda, it will not work reliably to
3586 define a filter for one of the individual blocks. You need to set it in
3587 the global options and expect it to be applied to the entire view.")
3589 (defun org-agenda-use-sticky-p ()
3590 "Return non-nil if an agenda buffer named
3591 `org-agenda-buffer-name' exists and should be shown instead of
3592 generating a new one."
3593 (and
3594 ;; turned off by user
3595 org-agenda-sticky
3596 ;; For multi-agenda buffer already exists
3597 (not org-agenda-multi)
3598 ;; buffer found
3599 (get-buffer org-agenda-buffer-name)
3600 ;; C-u parameter is same as last call
3601 (with-current-buffer (get-buffer org-agenda-buffer-name)
3602 (and
3603 (equal current-prefix-arg
3604 org-agenda-last-prefix-arg)
3605 ;; In case user turned stickiness on, while having existing
3606 ;; Agenda buffer active, don't reuse that buffer, because it
3607 ;; does not have org variables local
3608 org-agenda-this-buffer-is-sticky))))
3610 (defun org-agenda-prepare-window (abuf filter-alist)
3611 "Setup agenda buffer in the window.
3612 ABUF is the buffer for the agenda window.
3613 FILTER-ALIST is an alist of filters we need to apply when
3614 `org-agenda-persistent-filter' is non-nil."
3615 (let* ((awin (get-buffer-window abuf)) wconf)
3616 (cond
3617 ((equal (current-buffer) abuf) nil)
3618 (awin (select-window awin))
3619 ((not (setq wconf (current-window-configuration))))
3620 ((equal org-agenda-window-setup 'current-window)
3621 (org-pop-to-buffer-same-window abuf))
3622 ((equal org-agenda-window-setup 'other-window)
3623 (org-switch-to-buffer-other-window abuf))
3624 ((equal org-agenda-window-setup 'other-frame)
3625 (switch-to-buffer-other-frame abuf))
3626 ((eq org-agenda-window-setup 'only-window)
3627 (delete-other-windows)
3628 (org-pop-to-buffer-same-window abuf))
3629 ((equal org-agenda-window-setup 'reorganize-frame)
3630 (delete-other-windows)
3631 (org-switch-to-buffer-other-window abuf)))
3632 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3633 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3634 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3635 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3636 ;; Additional test in case agenda is invoked from within agenda
3637 ;; buffer via elisp link.
3638 (unless (equal (current-buffer) abuf)
3639 (org-pop-to-buffer-same-window abuf))
3640 (setq org-agenda-pre-window-conf
3641 (or wconf org-agenda-pre-window-conf))))
3643 (defun org-agenda-prepare (&optional name)
3644 (let ((filter-alist (if org-agenda-persistent-filter
3645 (with-current-buffer
3646 (get-buffer-create org-agenda-buffer-name)
3647 (list `(tag . ,org-agenda-tag-filter)
3648 `(re . ,org-agenda-regexp-filter)
3649 `(effort . ,org-agenda-effort-filter)
3650 `(cat . ,org-agenda-category-filter))))))
3651 (if (org-agenda-use-sticky-p)
3652 (progn
3653 (put 'org-agenda-tag-filter :preset-filter nil)
3654 (put 'org-agenda-category-filter :preset-filter nil)
3655 (put 'org-agenda-regexp-filter :preset-filter nil)
3656 ;; Popup existing buffer
3657 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3658 filter-alist)
3659 (message "Sticky Agenda buffer, use `r' to refresh")
3660 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3661 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3662 (setq org-todo-keywords-for-agenda nil)
3663 (put 'org-agenda-tag-filter :preset-filter
3664 org-agenda-tag-filter-preset)
3665 (put 'org-agenda-category-filter :preset-filter
3666 org-agenda-category-filter-preset)
3667 (put 'org-agenda-regexp-filter :preset-filter
3668 org-agenda-regexp-filter-preset)
3669 (put 'org-agenda-effort-filter :preset-filter
3670 org-agenda-effort-filter-preset)
3671 (if org-agenda-multi
3672 (progn
3673 (setq buffer-read-only nil)
3674 (goto-char (point-max))
3675 (unless (or (bobp) org-agenda-compact-blocks
3676 (not org-agenda-block-separator))
3677 (insert "\n"
3678 (if (stringp org-agenda-block-separator)
3679 org-agenda-block-separator
3680 (make-string (window-width) org-agenda-block-separator))
3681 "\n"))
3682 (narrow-to-region (point) (point-max)))
3683 (setq org-done-keywords-for-agenda nil)
3684 ;; Setting any org variables that are in org-agenda-local-vars
3685 ;; list need to be done after the prepare call
3686 (org-agenda-prepare-window
3687 (get-buffer-create org-agenda-buffer-name) filter-alist)
3688 (setq buffer-read-only nil)
3689 (org-agenda-reset-markers)
3690 (let ((inhibit-read-only t)) (erase-buffer))
3691 (org-agenda-mode)
3692 (setq org-agenda-buffer (current-buffer))
3693 (setq org-agenda-contributing-files nil)
3694 (setq org-agenda-columns-active nil)
3695 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3696 (setq org-todo-keywords-for-agenda
3697 (org-uniquify org-todo-keywords-for-agenda))
3698 (setq org-done-keywords-for-agenda
3699 (org-uniquify org-done-keywords-for-agenda))
3700 (setq org-agenda-last-prefix-arg current-prefix-arg)
3701 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3702 (and name (not org-agenda-name)
3703 (org-set-local 'org-agenda-name name)))
3704 (setq buffer-read-only nil))))
3706 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3707 (defun org-agenda-finalize ()
3708 "Finishing touch for the agenda buffer, called just before displaying it."
3709 (unless org-agenda-multi
3710 (save-excursion
3711 (let ((inhibit-read-only t))
3712 (goto-char (point-min))
3713 (save-excursion
3714 (while (org-activate-bracket-links (point-max))
3715 (add-text-properties (match-beginning 0) (match-end 0)
3716 '(face org-link))))
3717 (save-excursion
3718 (while (org-activate-plain-links (point-max))
3719 (add-text-properties (match-beginning 0) (match-end 0)
3720 '(face org-link))))
3721 (unless (eq org-agenda-remove-tags t)
3722 (org-agenda-align-tags))
3723 (unless org-agenda-with-colors
3724 (remove-text-properties (point-min) (point-max) '(face nil)))
3725 (if (and (boundp 'org-agenda-overriding-columns-format)
3726 org-agenda-overriding-columns-format)
3727 (org-set-local 'org-agenda-overriding-columns-format
3728 org-agenda-overriding-columns-format))
3729 (if (and (boundp 'org-agenda-view-columns-initially)
3730 org-agenda-view-columns-initially)
3731 (org-agenda-columns))
3732 (when org-agenda-fontify-priorities
3733 (org-agenda-fontify-priorities))
3734 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3735 (org-agenda-dim-blocked-tasks))
3736 (org-agenda-mark-clocking-task)
3737 (when org-agenda-entry-text-mode
3738 (org-agenda-entry-text-hide)
3739 (org-agenda-entry-text-show))
3740 (if (and (functionp 'org-habit-insert-consistency-graphs)
3741 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3742 (org-habit-insert-consistency-graphs))
3743 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3744 (unless (or (eq org-agenda-show-inherited-tags 'always)
3745 (and (listp org-agenda-show-inherited-tags)
3746 (memq org-agenda-type org-agenda-show-inherited-tags))
3747 (and (eq org-agenda-show-inherited-tags t)
3748 (or (eq org-agenda-use-tag-inheritance t)
3749 (and (listp org-agenda-use-tag-inheritance)
3750 (not (memq org-agenda-type
3751 org-agenda-use-tag-inheritance))))))
3752 (let (mrk)
3753 (save-excursion
3754 (goto-char (point-min))
3755 (while (equal (forward-line) 0)
3756 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3757 (put-text-property (point-at-bol) (point-at-eol)
3758 'tags (org-with-point-at mrk
3759 (delete-dups
3760 (mapcar 'downcase (org-get-tags-at))))))))))
3761 (run-hooks 'org-agenda-finalize-hook)
3762 (when org-agenda-top-headline-filter
3763 (org-agenda-filter-top-headline-apply
3764 org-agenda-top-headline-filter))
3765 (when org-agenda-tag-filter
3766 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3767 (when (get 'org-agenda-tag-filter :preset-filter)
3768 (org-agenda-filter-apply
3769 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3770 (when org-agenda-category-filter
3771 (org-agenda-filter-apply org-agenda-category-filter 'category))
3772 (when (get 'org-agenda-category-filter :preset-filter)
3773 (org-agenda-filter-apply
3774 (get 'org-agenda-category-filter :preset-filter) 'category))
3775 (when org-agenda-regexp-filter
3776 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3777 (when (get 'org-agenda-regexp-filter :preset-filter)
3778 (org-agenda-filter-apply
3779 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3780 (when org-agenda-effort-filter
3781 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3782 (when (get 'org-agenda-effort-filter :preset-filter)
3783 (org-agenda-filter-apply
3784 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3785 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3787 (defun org-agenda-mark-clocking-task ()
3788 "Mark the current clock entry in the agenda if it is present."
3789 ;; We need to widen when `org-agenda-finalize' is called from
3790 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3791 (when org-clock-current-task
3792 (save-restriction
3793 (widen)
3794 (org-agenda-unmark-clocking-task)
3795 (when (marker-buffer org-clock-hd-marker)
3796 (save-excursion
3797 (goto-char (point-min))
3798 (let (s ov)
3799 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3800 (goto-char s)
3801 (when (equal (org-get-at-bol 'org-hd-marker)
3802 org-clock-hd-marker)
3803 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3804 (overlay-put ov 'type 'org-agenda-clocking)
3805 (overlay-put ov 'face 'org-agenda-clocking)
3806 (overlay-put ov 'help-echo
3807 "The clock is running in this item")))))))))
3809 (defun org-agenda-unmark-clocking-task ()
3810 "Unmark the current clocking task."
3811 (mapc (lambda (o)
3812 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3813 (delete-overlay o)))
3814 (overlays-in (point-min) (point-max))))
3816 (defun org-agenda-fontify-priorities ()
3817 "Make highest priority lines bold, and lowest italic."
3818 (interactive)
3819 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3820 (delete-overlay o)))
3821 (overlays-in (point-min) (point-max)))
3822 (save-excursion
3823 (let (b e p ov h l)
3824 (goto-char (point-min))
3825 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3826 (setq h (or (get-char-property (point) 'org-highest-priority)
3827 org-highest-priority)
3828 l (or (get-char-property (point) 'org-lowest-priority)
3829 org-lowest-priority)
3830 p (string-to-char (match-string 1))
3831 b (match-beginning 0)
3832 e (if (eq org-agenda-fontify-priorities 'cookies)
3833 (match-end 0)
3834 (point-at-eol))
3835 ov (make-overlay b e))
3836 (overlay-put
3837 ov 'face
3838 (let ((special-face
3839 (cond ((org-face-from-face-or-color
3840 'priority nil
3841 (cdr (assoc p org-priority-faces))))
3842 ((and (listp org-agenda-fontify-priorities)
3843 (org-face-from-face-or-color
3844 'priority nil
3845 (cdr (assoc p org-agenda-fontify-priorities)))))
3846 ((equal p l) 'italic)
3847 ((equal p h) 'bold))))
3848 (if special-face (list special-face 'org-priority) 'org-priority)))
3849 (overlay-put ov 'org-type 'org-priority)))))
3851 (defvar org-depend-tag-blocked)
3853 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3854 "Dim currently blocked TODO's in the agenda display.
3855 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3856 dimming them."
3857 (interactive "P")
3858 (when (org-called-interactively-p 'interactive)
3859 (message "Dim or hide blocked tasks..."))
3860 (dolist (o (overlays-in (point-min) (point-max)))
3861 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3862 (delete-overlay o)))
3863 (save-excursion
3864 (let ((inhibit-read-only t)
3865 (org-depend-tag-blocked nil)
3866 org-blocked-by-checkboxes)
3867 (goto-char (point-min))
3868 (while (let ((pos (text-property-not-all
3869 (point) (point-max) 'todo-state nil)))
3870 (when pos (goto-char pos)))
3871 (setq org-blocked-by-checkboxes nil)
3872 (let ((marker (org-get-at-bol 'org-hd-marker)))
3873 (when (and (markerp marker)
3874 (with-current-buffer (marker-buffer marker)
3875 (save-excursion (goto-char marker)
3876 (org-entry-blocked-p))))
3877 ;; Entries blocked by checkboxes cannot be made invisible.
3878 ;; See `org-agenda-dim-blocked-tasks' for details.
3879 (let* ((really-invisible
3880 (and (not org-blocked-by-checkboxes)
3881 (or invisible (eq org-agenda-dim-blocked-tasks
3882 'invisible))))
3883 (ov (make-overlay (if really-invisible (line-end-position 0)
3884 (line-beginning-position))
3885 (line-end-position))))
3886 (if really-invisible (overlay-put ov 'invisible t)
3887 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3888 (overlay-put ov 'org-type 'org-blocked-todo))))
3889 (forward-line))))
3890 (when (org-called-interactively-p 'interactive)
3891 (message "Dim or hide blocked tasks...done")))
3893 (defvar org-agenda-skip-function nil
3894 "Function to be called at each match during agenda construction.
3895 If this function returns nil, the current match should not be skipped.
3896 Otherwise, the function must return a position from where the search
3897 should be continued.
3898 This may also be a Lisp form, it will be evaluated.
3899 Never set this variable using `setq' or so, because then it will apply
3900 to all future agenda commands. If you do want a global skipping condition,
3901 use the option `org-agenda-skip-function-global' instead.
3902 The correct usage for `org-agenda-skip-function' is to bind it with
3903 `let' to scope it dynamically into the agenda-constructing command.
3904 A good way to set it is through options in `org-agenda-custom-commands'.")
3906 (defun org-agenda-skip ()
3907 "Throw to `:skip' in places that should be skipped.
3908 Also moves point to the end of the skipped region, so that search can
3909 continue from there."
3910 (let ((p (point-at-bol)) to)
3911 (when (or
3912 (save-excursion (goto-char p) (looking-at comment-start-skip))
3913 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3914 (get-text-property p :org-archived)
3915 (org-end-of-subtree t))
3916 (and org-agenda-skip-comment-trees
3917 (get-text-property p :org-comment)
3918 (org-end-of-subtree t))
3919 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3920 (org-agenda-skip-eval org-agenda-skip-function)))
3921 (goto-char to))
3922 (org-in-src-block-p t))
3923 (throw :skip t))))
3925 (defun org-agenda-skip-eval (form)
3926 "If FORM is a function or a list, call (or eval) it and return the result.
3927 `save-excursion' and `save-match-data' are wrapped around the call, so point
3928 and match data are returned to the previous state no matter what these
3929 functions do."
3930 (let (fp)
3931 (and form
3932 (or (setq fp (functionp form))
3933 (consp form))
3934 (save-excursion
3935 (save-match-data
3936 (if fp
3937 (funcall form)
3938 (eval form)))))))
3940 (defvar org-agenda-markers nil
3941 "List of all currently active markers created by `org-agenda'.")
3942 (defvar org-agenda-last-marker-time (org-float-time)
3943 "Creation time of the last agenda marker.")
3945 (defun org-agenda-new-marker (&optional pos)
3946 "Return a new agenda marker.
3947 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3948 these markers and resets them when they are no longer in use."
3949 (let ((m (copy-marker (or pos (point)) t)))
3950 (setq org-agenda-last-marker-time (org-float-time))
3951 (if org-agenda-buffer
3952 (with-current-buffer org-agenda-buffer
3953 (push m org-agenda-markers))
3954 (push m org-agenda-markers))
3957 (defun org-agenda-reset-markers ()
3958 "Reset markers created by `org-agenda'."
3959 (while org-agenda-markers
3960 (move-marker (pop org-agenda-markers) nil)))
3962 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3963 "Save relative positions of markers in region.
3964 This check for agenda markers in all agenda buffers currently active."
3965 (dolist (buf (buffer-list))
3966 (with-current-buffer buf
3967 (when (eq major-mode 'org-agenda-mode)
3968 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3969 org-agenda-markers)))))
3971 ;;; Entry text mode
3973 (defun org-agenda-entry-text-show-here ()
3974 "Add some text from the entry as context to the current line."
3975 (let (m txt o)
3976 (setq m (org-get-at-bol 'org-hd-marker))
3977 (unless (marker-buffer m)
3978 (error "No marker points to an entry here"))
3979 (setq txt (concat "\n" (org-no-properties
3980 (org-agenda-get-some-entry-text
3981 m org-agenda-entry-text-maxlines
3982 org-agenda-entry-text-leaders))))
3983 (when (string-match "\\S-" txt)
3984 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3985 (overlay-put o 'evaporate t)
3986 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3987 (overlay-put o 'after-string txt))))
3989 (defun org-agenda-entry-text-show ()
3990 "Add entry context for all agenda lines."
3991 (interactive)
3992 (save-excursion
3993 (goto-char (point-max))
3994 (beginning-of-line 1)
3995 (while (not (bobp))
3996 (when (org-get-at-bol 'org-hd-marker)
3997 (org-agenda-entry-text-show-here))
3998 (beginning-of-line 0))))
4000 (defun org-agenda-entry-text-hide ()
4001 "Remove any shown entry context."
4002 (delq nil
4003 (mapcar (lambda (o)
4004 (if (eq (overlay-get o 'org-overlay-type)
4005 'agenda-entry-content)
4006 (progn (delete-overlay o) t)))
4007 (overlays-in (point-min) (point-max)))))
4009 (defun org-agenda-get-day-face (date)
4010 "Return the face DATE should be displayed with."
4011 (or (and (functionp org-agenda-day-face-function)
4012 (funcall org-agenda-day-face-function date))
4013 (cond ((org-agenda-todayp date)
4014 'org-agenda-date-today)
4015 ((member (calendar-day-of-week date) org-agenda-weekend-days)
4016 'org-agenda-date-weekend)
4017 (t 'org-agenda-date))))
4019 ;;; Agenda timeline
4021 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4022 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4024 (defun org-timeline (&optional dotodo)
4025 "Show a time-sorted view of the entries in the current org file.
4026 Only entries with a time stamp of today or later will be listed. With
4027 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4028 under the current date.
4029 If the buffer contains an active region, only check the region for
4030 dates."
4031 (interactive "P")
4032 (let* ((dopast t)
4033 (org-agenda-show-log-scoped org-agenda-show-log)
4034 (org-agenda-show-log org-agenda-show-log-scoped)
4035 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4036 (current-buffer))))
4037 (date (calendar-current-date))
4038 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4039 (end (if (org-region-active-p) (region-end) (point-max)))
4040 (day-numbers (org-get-all-dates
4041 beg end 'no-ranges
4042 t org-agenda-show-log-scoped ; always include today
4043 org-timeline-show-empty-dates))
4044 (org-deadline-warning-days 0)
4045 (org-agenda-only-exact-dates t)
4046 (today (org-today))
4047 (past t)
4048 args
4049 s e rtn d emptyp)
4050 (setq org-agenda-redo-command
4051 (list 'let
4052 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4053 (list 'org-switch-to-buffer-other-window (current-buffer))
4054 (list 'org-timeline (list 'quote dotodo))))
4055 (put 'org-agenda-redo-command 'org-lprops nil)
4056 (if (not dopast)
4057 ;; Remove past dates from the list of dates.
4058 (setq day-numbers (delq nil (mapcar (lambda(x)
4059 (if (>= x today) x nil))
4060 day-numbers))))
4061 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4062 (org-compile-prefix-format 'timeline)
4063 (org-set-sorting-strategy 'timeline)
4064 (if org-agenda-show-log-scoped (push :closed args))
4065 (push :timestamp args)
4066 (push :deadline args)
4067 (push :scheduled args)
4068 (push :sexp args)
4069 (if dotodo (push :todo args))
4070 (insert "Timeline of file " entry "\n")
4071 (add-text-properties (point-min) (point)
4072 (list 'face 'org-agenda-structure))
4073 (org-agenda-mark-header-line (point-min))
4074 (while (setq d (pop day-numbers))
4075 (if (and (listp d) (eq (car d) :omitted))
4076 (progn
4077 (setq s (point))
4078 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4079 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4080 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4081 (if (and (>= d today)
4082 dopast
4083 past)
4084 (progn
4085 (setq past nil)
4086 (insert (make-string 79 ?-) "\n")))
4087 (setq date (calendar-gregorian-from-absolute d))
4088 (setq s (point))
4089 (setq rtn (and (not emptyp)
4090 (apply 'org-agenda-get-day-entries entry
4091 date args)))
4092 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4093 (progn
4094 (insert
4095 (if (stringp org-agenda-format-date)
4096 (format-time-string org-agenda-format-date
4097 (org-time-from-absolute date))
4098 (funcall org-agenda-format-date date))
4099 "\n")
4100 (put-text-property s (1- (point)) 'face
4101 (org-agenda-get-day-face date))
4102 (put-text-property s (1- (point)) 'org-date-line t)
4103 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4104 (if (equal d today)
4105 (put-text-property s (1- (point)) 'org-today t))
4106 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4107 (put-text-property s (1- (point)) 'day d)))))
4108 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4109 (point-min)))
4110 (add-text-properties
4111 (point-min) (point-max)
4112 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4113 (org-agenda-finalize)
4114 (setq buffer-read-only t)))
4116 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4117 "Return a list of all relevant day numbers from BEG to END buffer positions.
4118 If NO-RANGES is non-nil, include only the start and end dates of a range,
4119 not every single day in the range. If FORCE-TODAY is non-nil, make
4120 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4121 inactive time stamps (those in square brackets) are included.
4122 When EMPTY is non-nil, also include days without any entries."
4123 (let ((re (concat
4124 (if pre-re pre-re "")
4125 (if inactive org-ts-regexp-both org-ts-regexp)))
4126 dates dates1 date day day1 day2 ts1 ts2 pos)
4127 (if force-today
4128 (setq dates (list (org-today))))
4129 (save-excursion
4130 (goto-char beg)
4131 (while (re-search-forward re end t)
4132 (setq day (time-to-days (org-time-string-to-time
4133 (substring (match-string 1) 0 10)
4134 (current-buffer) (match-beginning 0))))
4135 (or (memq day dates) (push day dates)))
4136 (unless no-ranges
4137 (goto-char beg)
4138 (while (re-search-forward org-tr-regexp end t)
4139 (setq pos (match-beginning 0))
4140 (setq ts1 (substring (match-string 1) 0 10)
4141 ts2 (substring (match-string 2) 0 10)
4142 day1 (time-to-days (org-time-string-to-time
4143 ts1 (current-buffer) pos))
4144 day2 (time-to-days (org-time-string-to-time
4145 ts2 (current-buffer) pos)))
4146 (while (< (setq day1 (1+ day1)) day2)
4147 (or (memq day1 dates) (push day1 dates)))))
4148 (setq dates (sort dates '<))
4149 (when empty
4150 (while (setq day (pop dates))
4151 (setq day2 (car dates))
4152 (push day dates1)
4153 (when (and day2 empty)
4154 (if (or (eq empty t)
4155 (and (numberp empty) (<= (- day2 day) empty)))
4156 (while (< (setq day (1+ day)) day2)
4157 (push (list day) dates1))
4158 (push (cons :omitted (- day2 day)) dates1))))
4159 (setq dates (nreverse dates1)))
4160 dates)))
4162 ;;; Agenda Daily/Weekly
4164 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4165 "Start day for the agenda view.
4166 Custom commands can set this variable in the options section.
4167 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4168 input allowed when reading a date through the Org calendar.
4169 See the docstring of `org-read-date' for details.")
4170 (defvar org-starting-day nil) ; local variable in the agenda buffer
4171 (defvar org-arg-loc nil) ; local variable
4173 (defvar org-agenda-buffer-tmp-name nil)
4174 ;;;###autoload
4175 (defun org-agenda-list (&optional arg start-day span with-hour)
4176 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4177 The view will be for the current day or week, but from the overview buffer
4178 you will be able to go to other days/weeks.
4180 With a numeric prefix argument in an interactive call, the agenda will
4181 span ARG days. Lisp programs should instead specify SPAN to change
4182 the number of days. SPAN defaults to `org-agenda-span'.
4184 START-DAY defaults to TODAY, or to the most recent match for the weekday
4185 given in `org-agenda-start-on-weekday'.
4187 When WITH-HOUR is non-nil, only include scheduled and deadline
4188 items if they have an hour specification like [h]h:mm."
4189 (interactive "P")
4190 (if org-agenda-overriding-arguments
4191 (setq arg (car org-agenda-overriding-arguments)
4192 start-day (nth 1 org-agenda-overriding-arguments)
4193 span (nth 2 org-agenda-overriding-arguments)))
4194 (if (and (integerp arg) (> arg 0))
4195 (setq span arg arg nil))
4196 (catch 'exit
4197 (setq org-agenda-buffer-name
4198 (or org-agenda-buffer-tmp-name
4199 (if org-agenda-sticky
4200 (cond ((and org-keys (stringp org-match))
4201 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4202 (org-keys
4203 (format "*Org Agenda(%s)*" org-keys))
4204 (t "*Org Agenda(a)*")))
4205 org-agenda-buffer-name))
4206 (org-agenda-prepare "Day/Week")
4207 (setq start-day (or start-day org-agenda-start-day))
4208 (if (stringp start-day)
4209 ;; Convert to an absolute day number
4210 (setq start-day (time-to-days (org-read-date nil t start-day))))
4211 (org-compile-prefix-format 'agenda)
4212 (org-set-sorting-strategy 'agenda)
4213 (let* ((span (org-agenda-ndays-to-span
4214 (or span org-agenda-ndays org-agenda-span)))
4215 (today (org-today))
4216 (sd (or start-day today))
4217 (ndays (org-agenda-span-to-ndays span sd))
4218 (org-agenda-start-on-weekday
4219 (if (or (eq ndays 7) (eq ndays 14))
4220 org-agenda-start-on-weekday))
4221 (thefiles (org-agenda-files nil 'ifmode))
4222 (files thefiles)
4223 (start (if (or (null org-agenda-start-on-weekday)
4224 (< ndays 7))
4226 (let* ((nt (calendar-day-of-week
4227 (calendar-gregorian-from-absolute sd)))
4228 (n1 org-agenda-start-on-weekday)
4229 (d (- nt n1)))
4230 (- sd (+ (if (< d 0) 7 0) d)))))
4231 (day-numbers (list start))
4232 (day-cnt 0)
4233 (inhibit-redisplay (not debug-on-error))
4234 (org-agenda-show-log-scoped org-agenda-show-log)
4235 s e rtn rtnall file date d start-pos end-pos todayp
4236 clocktable-start clocktable-end filter)
4237 (setq org-agenda-redo-command
4238 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4239 (dotimes (n (1- ndays))
4240 (push (1+ (car day-numbers)) day-numbers))
4241 (setq day-numbers (nreverse day-numbers))
4242 (setq clocktable-start (car day-numbers)
4243 clocktable-end (1+ (or (org-last day-numbers) 0)))
4244 (org-set-local 'org-starting-day (car day-numbers))
4245 (org-set-local 'org-arg-loc arg)
4246 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4247 (unless org-agenda-compact-blocks
4248 (let* ((d1 (car day-numbers))
4249 (d2 (org-last day-numbers))
4250 (w1 (org-days-to-iso-week d1))
4251 (w2 (org-days-to-iso-week d2)))
4252 (setq s (point))
4253 (if org-agenda-overriding-header
4254 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4255 nil 'face 'org-agenda-structure) "\n")
4256 (insert (org-agenda-span-name span)
4257 "-agenda"
4258 (if (< (- d2 d1) 350)
4259 (if (= w1 w2)
4260 (format " (W%02d)" w1)
4261 (format " (W%02d-W%02d)" w1 w2))
4263 ":\n")))
4264 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4265 'org-date-line t))
4266 (org-agenda-mark-header-line s))
4267 (while (setq d (pop day-numbers))
4268 (setq date (calendar-gregorian-from-absolute d)
4269 s (point))
4270 (if (or (setq todayp (= d today))
4271 (and (not start-pos) (= d sd)))
4272 (setq start-pos (point))
4273 (if (and start-pos (not end-pos))
4274 (setq end-pos (point))))
4275 (setq files thefiles
4276 rtnall nil)
4277 (while (setq file (pop files))
4278 (catch 'nextfile
4279 (org-check-agenda-file file)
4280 (let ((org-agenda-entry-types org-agenda-entry-types))
4281 ;; Starred types override non-starred equivalents
4282 (when (member :deadline* org-agenda-entry-types)
4283 (setq org-agenda-entry-types
4284 (delq :deadline org-agenda-entry-types)))
4285 (when (member :scheduled* org-agenda-entry-types)
4286 (setq org-agenda-entry-types
4287 (delq :scheduled org-agenda-entry-types)))
4288 ;; Honor with-hour
4289 (when with-hour
4290 (when (member :deadline org-agenda-entry-types)
4291 (setq org-agenda-entry-types
4292 (delq :deadline org-agenda-entry-types))
4293 (push :deadline* org-agenda-entry-types))
4294 (when (member :scheduled org-agenda-entry-types)
4295 (setq org-agenda-entry-types
4296 (delq :scheduled org-agenda-entry-types))
4297 (push :scheduled* org-agenda-entry-types)))
4298 (unless org-agenda-include-deadlines
4299 (setq org-agenda-entry-types
4300 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4301 (cond
4302 ((memq org-agenda-show-log-scoped '(only clockcheck))
4303 (setq rtn (org-agenda-get-day-entries
4304 file date :closed)))
4305 (org-agenda-show-log-scoped
4306 (setq rtn (apply 'org-agenda-get-day-entries
4307 file date
4308 (append '(:closed) org-agenda-entry-types))))
4310 (setq rtn (apply 'org-agenda-get-day-entries
4311 file date
4312 org-agenda-entry-types)))))
4313 (setq rtnall (append rtnall rtn)))) ;; all entries
4314 (if org-agenda-include-diary
4315 (let ((org-agenda-search-headline-for-time t))
4316 (require 'diary-lib)
4317 (setq rtn (org-get-entries-from-diary date))
4318 (setq rtnall (append rtnall rtn))))
4319 (if (or rtnall org-agenda-show-all-dates)
4320 (progn
4321 (setq day-cnt (1+ day-cnt))
4322 (insert
4323 (if (stringp org-agenda-format-date)
4324 (format-time-string org-agenda-format-date
4325 (org-time-from-absolute date))
4326 (funcall org-agenda-format-date date))
4327 "\n")
4328 (put-text-property s (1- (point)) 'face
4329 (org-agenda-get-day-face date))
4330 (put-text-property s (1- (point)) 'org-date-line t)
4331 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4332 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4333 (when todayp
4334 (put-text-property s (1- (point)) 'org-today t))
4335 (setq rtnall
4336 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4337 (if rtnall (insert ;; all entries
4338 (org-agenda-finalize-entries rtnall 'agenda)
4339 "\n"))
4340 (put-text-property s (1- (point)) 'day d)
4341 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4342 (when (and org-agenda-clockreport-mode clocktable-start)
4343 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4344 ;; the above line is to ensure the restricted range!
4345 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4346 tbl)
4347 (setq p (org-plist-delete p :block))
4348 (setq p (plist-put p :tstart clocktable-start))
4349 (setq p (plist-put p :tend clocktable-end))
4350 (setq p (plist-put p :scope 'agenda))
4351 (setq tbl (apply 'org-clock-get-clocktable p))
4352 (insert tbl)))
4353 (goto-char (point-min))
4354 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4355 (unless (and (pos-visible-in-window-p (point-min))
4356 (pos-visible-in-window-p (point-max)))
4357 (goto-char (1- (point-max)))
4358 (recenter -1)
4359 (if (not (pos-visible-in-window-p (or start-pos 1)))
4360 (progn
4361 (goto-char (or start-pos 1))
4362 (recenter 1))))
4363 (goto-char (or start-pos 1))
4364 (add-text-properties (point-min) (point-max)
4365 `(org-agenda-type agenda
4366 org-last-args (,arg ,start-day ,span)
4367 org-redo-cmd ,org-agenda-redo-command
4368 org-series-cmd ,org-cmd))
4369 (if (eq org-agenda-show-log-scoped 'clockcheck)
4370 (org-agenda-show-clocking-issues))
4371 (org-agenda-finalize)
4372 (setq buffer-read-only t)
4373 (message ""))))
4375 (defun org-agenda-ndays-to-span (n)
4376 "Return a span symbol for a span of N days, or N if none matches."
4377 (cond ((symbolp n) n)
4378 ((= n 1) 'day)
4379 ((= n 7) 'week)
4380 ((= n 14) 'fortnight)
4381 (t n)))
4383 (defun org-agenda-span-to-ndays (span &optional start-day)
4384 "Return ndays from SPAN, possibly starting at START-DAY.
4385 START-DAY is an absolute time value."
4386 (cond ((numberp span) span)
4387 ((eq span 'day) 1)
4388 ((eq span 'week) 7)
4389 ((eq span 'fortnight) 14)
4390 ((eq span 'month)
4391 (let ((date (calendar-gregorian-from-absolute start-day)))
4392 (calendar-last-day-of-month (car date) (caddr date))))
4393 ((eq span 'year)
4394 (let ((date (calendar-gregorian-from-absolute start-day)))
4395 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4397 (defun org-agenda-span-name (span)
4398 "Return a SPAN name."
4399 (if (null span)
4401 (if (symbolp span)
4402 (capitalize (symbol-name span))
4403 (format "%d days" span))))
4405 ;;; Agenda word search
4407 (defvar org-agenda-search-history nil)
4409 (defvar org-search-syntax-table nil
4410 "Special syntax table for org-mode search.
4411 In this table, we have single quotes not as word constituents, to
4412 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4414 (defvar org-mode-syntax-table) ; From org.el
4415 (defun org-search-syntax-table ()
4416 (unless org-search-syntax-table
4417 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4418 (modify-syntax-entry ?' "." org-search-syntax-table)
4419 (modify-syntax-entry ?` "." org-search-syntax-table))
4420 org-search-syntax-table)
4422 (defvar org-agenda-last-search-view-search-was-boolean nil)
4424 ;;;###autoload
4425 (defun org-search-view (&optional todo-only string edit-at)
4426 "Show all entries that contain a phrase or words or regular expressions.
4428 With optional prefix argument TODO-ONLY, only consider entries that are
4429 TODO entries. The argument STRING can be used to pass a default search
4430 string into this function. If EDIT-AT is non-nil, it means that the
4431 user should get a chance to edit this string, with cursor at position
4432 EDIT-AT.
4434 The search string can be viewed either as a phrase that should be found as
4435 is, or it can be broken into a number of snippets, each of which must match
4436 in a Boolean way to select an entry. The default depends on the variable
4437 `org-agenda-search-view-always-boolean'.
4438 Even if this is turned off (the default) you can always switch to
4439 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4441 The default is a direct search of the whole phrase, where each space in
4442 the search string can expand to an arbitrary amount of whitespace,
4443 including newlines.
4445 If using a Boolean search, the search string is split on whitespace and
4446 each snippet is searched separately, with logical AND to select an entry.
4447 Words prefixed with a minus must *not* occur in the entry. Words without
4448 a prefix or prefixed with a plus must occur in the entry. Matching is
4449 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4450 match whole words, not parts of a word) if
4451 `org-agenda-search-view-force-full-words' is set (default is nil).
4453 Boolean search snippets enclosed by curly braces are interpreted as
4454 regular expressions that must or (when preceded with \"-\") must not
4455 match in the entry. Snippets enclosed into double quotes will be taken
4456 as a whole, to include whitespace.
4458 - If the search string starts with an asterisk, search only in headlines.
4459 - If (possibly after the leading star) the search string starts with an
4460 exclamation mark, this also means to look at TODO entries only, an effect
4461 that can also be achieved with a prefix argument.
4462 - If (possibly after star and exclamation mark) the search string starts
4463 with a colon, this will mean that the (non-regexp) snippets of the
4464 Boolean search must match as full words.
4466 This command searches the agenda files, and in addition the files listed
4467 in `org-agenda-text-search-extra-files'."
4468 (interactive "P")
4469 (if org-agenda-overriding-arguments
4470 (setq todo-only (car org-agenda-overriding-arguments)
4471 string (nth 1 org-agenda-overriding-arguments)
4472 edit-at (nth 2 org-agenda-overriding-arguments)))
4473 (let* ((props (list 'face nil
4474 'done-face 'org-agenda-done
4475 'org-not-done-regexp org-not-done-regexp
4476 'org-todo-regexp org-todo-regexp
4477 'org-complex-heading-regexp org-complex-heading-regexp
4478 'mouse-face 'highlight
4479 'help-echo (format "mouse-2 or RET jump to location")))
4480 (full-words org-agenda-search-view-force-full-words)
4481 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4482 regexp rtn rtnall files file pos inherited-tags
4483 marker category level tags c neg re boolean
4484 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4485 (unless (and (not edit-at)
4486 (stringp string)
4487 (string-match "\\S-" string))
4488 (setq string (read-string
4489 (if org-agenda-search-view-always-boolean
4490 "[+-]Word/{Regexp} ...: "
4491 "Phrase or [+-]Word/{Regexp} ...: ")
4492 (cond
4493 ((integerp edit-at) (cons string edit-at))
4494 (edit-at string))
4495 'org-agenda-search-history)))
4496 (catch 'exit
4497 (if org-agenda-sticky
4498 (setq org-agenda-buffer-name
4499 (if (stringp string)
4500 (format "*Org Agenda(%s:%s)*"
4501 (or org-keys (or (and todo-only "S") "s")) string)
4502 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4503 (org-agenda-prepare "SEARCH")
4504 (org-compile-prefix-format 'search)
4505 (org-set-sorting-strategy 'search)
4506 (setq org-agenda-redo-command
4507 (list 'org-search-view (if todo-only t nil)
4508 (list 'if 'current-prefix-arg nil string)))
4509 (setq org-agenda-query-string string)
4510 (if (equal (string-to-char string) ?*)
4511 (setq hdl-only t
4512 words (substring string 1))
4513 (setq words string))
4514 (when (equal (string-to-char words) ?!)
4515 (setq todo-only t
4516 words (substring words 1)))
4517 (when (equal (string-to-char words) ?:)
4518 (setq full-words t
4519 words (substring words 1)))
4520 (if (or org-agenda-search-view-always-boolean
4521 (member (string-to-char words) '(?- ?+ ?\{)))
4522 (setq boolean t))
4523 (setq words (org-split-string words))
4524 (let (www w)
4525 (while (setq w (pop words))
4526 (while (and (string-match "\\\\\\'" w) words)
4527 (setq w (concat (substring w 0 -1) " " (pop words))))
4528 (push w www))
4529 (setq words (nreverse www) www nil)
4530 (while (setq w (pop words))
4531 (when (and (string-match "\\`[-+]?{" w)
4532 (not (string-match "}\\'" w)))
4533 (while (and words (not (string-match "}\\'" (car words))))
4534 (setq w (concat w " " (pop words))))
4535 (setq w (concat w " " (pop words))))
4536 (push w www))
4537 (setq words (nreverse www)))
4538 (setq org-agenda-last-search-view-search-was-boolean boolean)
4539 (when boolean
4540 (let (wds w)
4541 (while (setq w (pop words))
4542 (if (or (equal (substring w 0 1) "\"")
4543 (and (> (length w) 1)
4544 (member (substring w 0 1) '("+" "-"))
4545 (equal (substring w 1 2) "\"")))
4546 (while (and words (not (equal (substring w -1) "\"")))
4547 (setq w (concat w " " (pop words)))))
4548 (and (string-match "\\`\\([-+]?\\)\"" w)
4549 (setq w (replace-match "\\1" nil nil w)))
4550 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4551 (push w wds))
4552 (setq words (nreverse wds))))
4553 (if boolean
4554 (mapc (lambda (w)
4555 (setq c (string-to-char w))
4556 (if (equal c ?-)
4557 (setq neg t w (substring w 1))
4558 (if (equal c ?+)
4559 (setq neg nil w (substring w 1))
4560 (setq neg nil)))
4561 (if (string-match "\\`{.*}\\'" w)
4562 (setq re (substring w 1 -1))
4563 (if full-words
4564 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4565 (setq re (regexp-quote (downcase w)))))
4566 (if neg (push re regexps-) (push re regexps+)))
4567 words)
4568 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4569 regexps+))
4570 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4571 (if (not regexps+)
4572 (setq regexp org-outline-regexp-bol)
4573 (setq regexp (pop regexps+))
4574 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4575 regexp))))
4576 (setq files (org-agenda-files nil 'ifmode))
4577 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4578 (pop org-agenda-text-search-extra-files)
4579 (setq files (org-add-archive-files files)))
4580 (setq files (append files org-agenda-text-search-extra-files)
4581 rtnall nil)
4582 (while (setq file (pop files))
4583 (setq ee nil)
4584 (catch 'nextfile
4585 (org-check-agenda-file file)
4586 (setq buffer (if (file-exists-p file)
4587 (org-get-agenda-file-buffer file)
4588 (error "No such file %s" file)))
4589 (if (not buffer)
4590 ;; If file does not exist, make sure an error message is sent
4591 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4592 file))))
4593 (with-current-buffer buffer
4594 (with-syntax-table (org-search-syntax-table)
4595 (unless (derived-mode-p 'org-mode)
4596 (error "Agenda file %s is not in `org-mode'" file))
4597 (let ((case-fold-search t))
4598 (save-excursion
4599 (save-restriction
4600 (if (eq buffer org-agenda-restrict)
4601 (narrow-to-region org-agenda-restrict-begin
4602 org-agenda-restrict-end)
4603 (widen))
4604 (goto-char (point-min))
4605 (unless (or (org-at-heading-p)
4606 (outline-next-heading))
4607 (throw 'nextfile t))
4608 (goto-char (max (point-min) (1- (point))))
4609 (while (re-search-forward regexp nil t)
4610 (org-back-to-heading t)
4611 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4612 (> (org-reduced-level (org-outline-level))
4613 org-agenda-search-view-max-outline-level)
4614 (forward-line -1)
4615 (outline-back-to-heading t)))
4616 (skip-chars-forward "* ")
4617 (setq beg (point-at-bol)
4618 beg1 (point)
4619 end (progn
4620 (outline-next-heading)
4621 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4622 (> (org-reduced-level (org-outline-level))
4623 org-agenda-search-view-max-outline-level)
4624 (forward-line 1)
4625 (outline-next-heading)))
4626 (point)))
4628 (catch :skip
4629 (goto-char beg)
4630 (org-agenda-skip)
4631 (setq str (buffer-substring-no-properties
4632 (point-at-bol)
4633 (if hdl-only (point-at-eol) end)))
4634 (mapc (lambda (wr) (when (string-match wr str)
4635 (goto-char (1- end))
4636 (throw :skip t)))
4637 regexps-)
4638 (mapc (lambda (wr) (unless (string-match wr str)
4639 (goto-char (1- end))
4640 (throw :skip t)))
4641 (if todo-only
4642 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4643 regexps+)
4644 regexps+))
4645 (goto-char beg)
4646 (setq marker (org-agenda-new-marker (point))
4647 category (org-get-category)
4648 level (make-string (org-reduced-level (org-outline-level)) ? )
4649 inherited-tags
4650 (or (eq org-agenda-show-inherited-tags 'always)
4651 (and (listp org-agenda-show-inherited-tags)
4652 (memq 'todo org-agenda-show-inherited-tags))
4653 (and (eq org-agenda-show-inherited-tags t)
4654 (or (eq org-agenda-use-tag-inheritance t)
4655 (memq 'todo org-agenda-use-tag-inheritance))))
4656 tags (org-get-tags-at nil (not inherited-tags))
4657 txt (org-agenda-format-item
4659 (buffer-substring-no-properties
4660 beg1 (point-at-eol))
4661 level category tags t))
4662 (org-add-props txt props
4663 'org-marker marker 'org-hd-marker marker
4664 'org-todo-regexp org-todo-regexp
4665 'level level
4666 'org-complex-heading-regexp org-complex-heading-regexp
4667 'priority 1000
4668 'type "search")
4669 (push txt ee)
4670 (goto-char (1- end))))))))))
4671 (setq rtn (nreverse ee))
4672 (setq rtnall (append rtnall rtn)))
4673 (if org-agenda-overriding-header
4674 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4675 nil 'face 'org-agenda-structure) "\n")
4676 (insert "Search words: ")
4677 (add-text-properties (point-min) (1- (point))
4678 (list 'face 'org-agenda-structure))
4679 (setq pos (point))
4680 (insert string "\n")
4681 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4682 (setq pos (point))
4683 (unless org-agenda-multi
4684 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4685 (add-text-properties pos (1- (point))
4686 (list 'face 'org-agenda-structure))))
4687 (org-agenda-mark-header-line (point-min))
4688 (when rtnall
4689 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4690 (goto-char (point-min))
4691 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4692 (add-text-properties (point-min) (point-max)
4693 `(org-agenda-type search
4694 org-last-args (,todo-only ,string ,edit-at)
4695 org-redo-cmd ,org-agenda-redo-command
4696 org-series-cmd ,org-cmd))
4697 (org-agenda-finalize)
4698 (setq buffer-read-only t))))
4700 ;;; Agenda TODO list
4702 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4703 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4704 (concat
4705 (if (or (equal keywords "ALL") (not keywords))
4706 (propertize "ALL" 'face 'warning)
4707 (mapconcat
4708 (lambda (kw)
4709 (propertize kw 'face (org-get-todo-face kw)))
4710 (org-split-string keywords "|")
4711 "|"))
4712 "\n"))
4714 (defvar org-select-this-todo-keyword nil)
4715 (defvar org-last-arg nil)
4717 ;;;###autoload
4718 (defun org-todo-list (&optional arg)
4719 "Show all (not done) TODO entries from all agenda file in a single list.
4720 The prefix arg can be used to select a specific TODO keyword and limit
4721 the list to these. When using \\[universal-argument], you will be prompted
4722 for a keyword. A numeric prefix directly selects the Nth keyword in
4723 `org-todo-keywords-1'."
4724 (interactive "P")
4725 (if org-agenda-overriding-arguments
4726 (setq arg org-agenda-overriding-arguments))
4727 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4728 (let* ((today (org-today))
4729 (date (calendar-gregorian-from-absolute today))
4730 (kwds org-todo-keywords-for-agenda)
4731 (completion-ignore-case t)
4732 (org-select-this-todo-keyword
4733 (if (stringp arg) arg
4734 (and arg (integerp arg) (> arg 0)
4735 (nth (1- arg) kwds))))
4736 rtn rtnall files file pos)
4737 (when (equal arg '(4))
4738 (setq org-select-this-todo-keyword
4739 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4740 (mapcar 'list kwds) nil nil)))
4741 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4742 (catch 'exit
4743 (if org-agenda-sticky
4744 (setq org-agenda-buffer-name
4745 (if (stringp org-select-this-todo-keyword)
4746 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4747 org-select-this-todo-keyword)
4748 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4749 (org-agenda-prepare "TODO")
4750 (org-compile-prefix-format 'todo)
4751 (org-set-sorting-strategy 'todo)
4752 (setq org-agenda-redo-command
4753 `(org-todo-list (or (and (numberp current-prefix-arg)
4754 current-prefix-arg)
4755 ,org-select-this-todo-keyword
4756 current-prefix-arg ,arg)))
4757 (setq files (org-agenda-files nil 'ifmode)
4758 rtnall nil)
4759 (while (setq file (pop files))
4760 (catch 'nextfile
4761 (org-check-agenda-file file)
4762 (setq rtn (org-agenda-get-day-entries file date :todo))
4763 (setq rtnall (append rtnall rtn))))
4764 (if org-agenda-overriding-header
4765 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4766 nil 'face 'org-agenda-structure) "\n")
4767 (insert "Global list of TODO items of type: ")
4768 (add-text-properties (point-min) (1- (point))
4769 (list 'face 'org-agenda-structure
4770 'short-heading
4771 (concat "ToDo: "
4772 (or org-select-this-todo-keyword "ALL"))))
4773 (org-agenda-mark-header-line (point-min))
4774 (insert (org-agenda-propertize-selected-todo-keywords
4775 org-select-this-todo-keyword))
4776 (setq pos (point))
4777 (unless org-agenda-multi
4778 (insert "Available with `N r': (0)[ALL]")
4779 (let ((n 0) s)
4780 (mapc (lambda (x)
4781 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4782 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4783 (insert "\n "))
4784 (insert " " s))
4785 kwds))
4786 (insert "\n"))
4787 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4788 (org-agenda-mark-header-line (point-min))
4789 (when rtnall
4790 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4791 (goto-char (point-min))
4792 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4793 (add-text-properties (point-min) (point-max)
4794 `(org-agenda-type todo
4795 org-last-args ,arg
4796 org-redo-cmd ,org-agenda-redo-command
4797 org-series-cmd ,org-cmd))
4798 (org-agenda-finalize)
4799 (setq buffer-read-only t))))
4801 ;;; Agenda tags match
4803 ;;;###autoload
4804 (defun org-tags-view (&optional todo-only match)
4805 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4806 The prefix arg TODO-ONLY limits the search to TODO entries."
4807 (interactive "P")
4808 (if org-agenda-overriding-arguments
4809 (setq todo-only (car org-agenda-overriding-arguments)
4810 match (nth 1 org-agenda-overriding-arguments)))
4811 (let* ((org-tags-match-list-sublevels
4812 org-tags-match-list-sublevels)
4813 (completion-ignore-case t)
4814 rtn rtnall files file pos matcher
4815 buffer)
4816 (when (and (stringp match) (not (string-match "\\S-" match)))
4817 (setq match nil))
4818 (catch 'exit
4819 (if org-agenda-sticky
4820 (setq org-agenda-buffer-name
4821 (if (stringp match)
4822 (format "*Org Agenda(%s:%s)*"
4823 (or org-keys (or (and todo-only "M") "m")) match)
4824 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4825 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4826 ;; expanding tags within `org-make-tags-matcher'
4827 (org-agenda-prepare (concat "TAGS " match))
4828 (setq matcher (org-make-tags-matcher match)
4829 match (car matcher) matcher (cdr matcher))
4830 (org-compile-prefix-format 'tags)
4831 (org-set-sorting-strategy 'tags)
4832 (setq org-agenda-query-string match)
4833 (setq org-agenda-redo-command
4834 (list 'org-tags-view `(quote ,todo-only)
4835 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4836 (setq files (org-agenda-files nil 'ifmode)
4837 rtnall nil)
4838 (while (setq file (pop files))
4839 (catch 'nextfile
4840 (org-check-agenda-file file)
4841 (setq buffer (if (file-exists-p file)
4842 (org-get-agenda-file-buffer file)
4843 (error "No such file %s" file)))
4844 (if (not buffer)
4845 ;; If file does not exist, error message to agenda
4846 (setq rtn (list
4847 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4848 rtnall (append rtnall rtn))
4849 (with-current-buffer buffer
4850 (unless (derived-mode-p 'org-mode)
4851 (error "Agenda file %s is not in `org-mode'" file))
4852 (save-excursion
4853 (save-restriction
4854 (if (eq buffer org-agenda-restrict)
4855 (narrow-to-region org-agenda-restrict-begin
4856 org-agenda-restrict-end)
4857 (widen))
4858 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4859 (setq rtnall (append rtnall rtn))))))))
4860 (if org-agenda-overriding-header
4861 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4862 nil 'face 'org-agenda-structure) "\n")
4863 (insert "Headlines with TAGS match: ")
4864 (add-text-properties (point-min) (1- (point))
4865 (list 'face 'org-agenda-structure
4866 'short-heading
4867 (concat "Match: " match)))
4868 (setq pos (point))
4869 (insert match "\n")
4870 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4871 (setq pos (point))
4872 (unless org-agenda-multi
4873 (insert "Press `C-u r' to search again with new search string\n"))
4874 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4875 (org-agenda-mark-header-line (point-min))
4876 (when rtnall
4877 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4878 (goto-char (point-min))
4879 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4880 (add-text-properties (point-min) (point-max)
4881 `(org-agenda-type tags
4882 org-last-args (,todo-only ,match)
4883 org-redo-cmd ,org-agenda-redo-command
4884 org-series-cmd ,org-cmd))
4885 (org-agenda-finalize)
4886 (setq buffer-read-only t))))
4888 ;;; Agenda Finding stuck projects
4890 (defvar org-agenda-skip-regexp nil
4891 "Regular expression used in skipping subtrees for the agenda.
4892 This is basically a temporary global variable that can be set and then
4893 used by user-defined selections using `org-agenda-skip-function'.")
4895 (defvar org-agenda-overriding-header nil
4896 "When set during agenda, todo and tags searches it replaces the header.
4897 This variable should not be set directly, but custom commands can bind it
4898 in the options section.")
4900 (defun org-agenda-skip-entry-when-regexp-matches ()
4901 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4902 If yes, it returns the end position of this entry, causing agenda commands
4903 to skip the entry but continuing the search in the subtree. This is a
4904 function that can be put into `org-agenda-skip-function' for the duration
4905 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-subtree-when-regexp-matches ()
4913 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4914 If yes, it returns the end position of this tree, causing agenda commands
4915 to skip this subtree. This is a function that can be put into
4916 `org-agenda-skip-function' for the duration of a command."
4917 (let ((end (save-excursion (org-end-of-subtree t)))
4918 skip)
4919 (save-excursion
4920 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4921 (and skip end)))
4923 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4924 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4925 If yes, it returns the end position of the current entry (NOT the tree),
4926 causing agenda commands to skip the entry but continuing the search in
4927 the subtree. This is a function that can be put into
4928 `org-agenda-skip-function' for the duration of a command. An important
4929 use of this function is for the stuck project list."
4930 (let ((end (save-excursion (org-end-of-subtree t)))
4931 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4932 skip)
4933 (save-excursion
4934 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4935 (and skip entry-end)))
4937 (defun org-agenda-skip-entry-if (&rest conditions)
4938 "Skip entry if any of CONDITIONS is true.
4939 See `org-agenda-skip-if' for details."
4940 (org-agenda-skip-if nil conditions))
4942 (defun org-agenda-skip-subtree-if (&rest conditions)
4943 "Skip subtree if any of CONDITIONS is true.
4944 See `org-agenda-skip-if' for details."
4945 (org-agenda-skip-if t conditions))
4947 (defun org-agenda-skip-if (subtree conditions)
4948 "Checks current entity for CONDITIONS.
4949 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4950 the entry (i.e. the text before the next heading) is checked.
4952 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4953 from different tests. Valid conditions are:
4955 scheduled Check if there is a scheduled cookie
4956 notscheduled Check if there is no scheduled cookie
4957 deadline Check if there is a deadline
4958 notdeadline Check if there is no deadline
4959 timestamp Check if there is a timestamp (also deadline or scheduled)
4960 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4961 regexp Check if regexp matches
4962 notregexp Check if regexp does not match.
4963 todo Check if TODO keyword matches
4964 nottodo Check if TODO keyword does not match
4966 The regexp is taken from the conditions list, it must come right after
4967 the `regexp' or `notregexp' element.
4969 `todo' and `nottodo' accept as an argument a list of todo
4970 keywords, which may include \"*\" to match any todo keyword.
4972 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4974 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4976 Instead of a list, a keyword class may be given. For example:
4978 (org-agenda-skip-entry-if 'nottodo 'done)
4980 would skip entries that haven't been marked with any of \"DONE\"
4981 keywords. Possible classes are: `todo', `done', `any'.
4983 If any of these conditions is met, this function returns the end point of
4984 the entity, causing the search to continue from there. This is a function
4985 that can be put into `org-agenda-skip-function' for the duration of a command."
4986 (let (beg end m)
4987 (org-back-to-heading t)
4988 (setq beg (point)
4989 end (if subtree
4990 (progn (org-end-of-subtree t) (point))
4991 (progn (outline-next-heading) (1- (point)))))
4992 (goto-char beg)
4993 (and
4995 (and (memq 'scheduled conditions)
4996 (re-search-forward org-scheduled-time-regexp end t))
4997 (and (memq 'notscheduled conditions)
4998 (not (re-search-forward org-scheduled-time-regexp end t)))
4999 (and (memq 'deadline conditions)
5000 (re-search-forward org-deadline-time-regexp end t))
5001 (and (memq 'notdeadline conditions)
5002 (not (re-search-forward org-deadline-time-regexp end t)))
5003 (and (memq 'timestamp conditions)
5004 (re-search-forward org-ts-regexp end t))
5005 (and (memq 'nottimestamp conditions)
5006 (not (re-search-forward org-ts-regexp end t)))
5007 (and (setq m (memq 'regexp conditions))
5008 (stringp (nth 1 m))
5009 (re-search-forward (nth 1 m) end t))
5010 (and (setq m (memq 'notregexp conditions))
5011 (stringp (nth 1 m))
5012 (not (re-search-forward (nth 1 m) end t)))
5013 (and (or
5014 (setq m (memq 'nottodo conditions))
5015 (setq m (memq 'todo-unblocked conditions))
5016 (setq m (memq 'nottodo-unblocked conditions))
5017 (setq m (memq 'todo conditions)))
5018 (org-agenda-skip-if-todo m end)))
5019 end)))
5021 (defun org-agenda-skip-if-todo (args end)
5022 "Helper function for `org-agenda-skip-if', do not use it directly.
5023 ARGS is a list with first element either `todo', `nottodo',
5024 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5025 a list of TODO keywords, or a state symbol `todo' or `done' or
5026 `any'."
5027 (let ((kw (car args))
5028 (arg (cadr args))
5029 todo-wds todo-re)
5030 (setq todo-wds
5031 (org-uniquify
5032 (cond
5033 ((listp arg) ;; list of keywords
5034 (if (member "*" arg)
5035 (mapcar 'substring-no-properties org-todo-keywords-1)
5036 arg))
5037 ((symbolp arg) ;; keyword class name
5038 (cond
5039 ((eq arg 'todo)
5040 (org-delete-all org-done-keywords
5041 (mapcar 'substring-no-properties
5042 org-todo-keywords-1)))
5043 ((eq arg 'done) org-done-keywords)
5044 ((eq arg 'any)
5045 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5046 (setq todo-re
5047 (concat "^\\*+[ \t]+\\<\\("
5048 (mapconcat 'identity todo-wds "\\|")
5049 "\\)\\>"))
5050 (cond
5051 ((eq kw 'todo) (re-search-forward todo-re end t))
5052 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5053 ((eq kw 'todo-unblocked)
5054 (catch 'unblocked
5055 (while (re-search-forward todo-re end t)
5056 (or (org-entry-blocked-p) (throw 'unblocked t)))
5057 nil))
5058 ((eq kw 'nottodo-unblocked)
5059 (catch 'unblocked
5060 (while (re-search-forward todo-re end t)
5061 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5065 ;;;###autoload
5066 (defun org-agenda-list-stuck-projects (&rest ignore)
5067 "Create agenda view for projects that are stuck.
5068 Stuck projects are project that have no next actions. For the definitions
5069 of what a project is and how to check if it stuck, customize the variable
5070 `org-stuck-projects'."
5071 (interactive)
5072 (let* ((org-agenda-skip-function
5073 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5074 ;; We could have used org-agenda-skip-if here.
5075 (org-agenda-overriding-header
5076 (or org-agenda-overriding-header "List of stuck projects: "))
5077 (matcher (nth 0 org-stuck-projects))
5078 (todo (nth 1 org-stuck-projects))
5079 (todo-wds (if (member "*" todo)
5080 (progn
5081 (org-agenda-prepare-buffers (org-agenda-files
5082 nil 'ifmode))
5083 (org-delete-all
5084 org-done-keywords-for-agenda
5085 (copy-sequence org-todo-keywords-for-agenda)))
5086 todo))
5087 (todo-re (concat "^\\*+[ \t]+\\("
5088 (mapconcat 'identity todo-wds "\\|")
5089 "\\)\\>"))
5090 (tags (nth 2 org-stuck-projects))
5091 (tags-re (if (member "*" tags)
5092 (concat org-outline-regexp-bol
5093 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5094 (if tags
5095 (concat org-outline-regexp-bol
5096 ".*:\\("
5097 (mapconcat 'identity tags "\\|")
5098 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5099 (gen-re (nth 3 org-stuck-projects))
5100 (re-list
5101 (delq nil
5102 (list
5103 (if todo todo-re)
5104 (if tags tags-re)
5105 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5106 gen-re)))))
5107 (setq org-agenda-skip-regexp
5108 (if re-list
5109 (mapconcat 'identity re-list "\\|")
5110 (error "No information how to identify unstuck projects")))
5111 (org-tags-view nil matcher)
5112 (setq org-agenda-buffer-name (buffer-name))
5113 (with-current-buffer org-agenda-buffer-name
5114 (setq org-agenda-redo-command
5115 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5117 ;;; Diary integration
5119 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5120 (defvar diary-list-entries-hook)
5121 (defvar diary-time-regexp)
5122 (defun org-get-entries-from-diary (date)
5123 "Get the (Emacs Calendar) diary entries for DATE."
5124 (require 'diary-lib)
5125 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5126 (diary-display-function 'diary-fancy-display)
5127 (pop-up-frames nil)
5128 (diary-list-entries-hook
5129 (cons 'org-diary-default-entry diary-list-entries-hook))
5130 (diary-file-name-prefix nil) ; turn this feature off
5131 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5132 entries
5133 (org-disable-agenda-to-diary t))
5134 (save-excursion
5135 (save-window-excursion
5136 (funcall (if (fboundp 'diary-list-entries)
5137 'diary-list-entries 'list-diary-entries)
5138 date 1)))
5139 (if (not (get-buffer diary-fancy-buffer))
5140 (setq entries nil)
5141 (with-current-buffer diary-fancy-buffer
5142 (setq buffer-read-only nil)
5143 (if (zerop (buffer-size))
5144 ;; No entries
5145 (setq entries nil)
5146 ;; Omit the date and other unnecessary stuff
5147 (org-agenda-cleanup-fancy-diary)
5148 ;; Add prefix to each line and extend the text properties
5149 (if (zerop (buffer-size))
5150 (setq entries nil)
5151 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5152 (setq entries
5153 (with-temp-buffer
5154 (insert entries) (goto-char (point-min))
5155 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5156 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5157 (replace-match (concat "; " (match-string 1)))))
5158 (buffer-string)))))
5159 (set-buffer-modified-p nil)
5160 (kill-buffer diary-fancy-buffer)))
5161 (when entries
5162 (setq entries (org-split-string entries "\n"))
5163 (setq entries
5164 (mapcar
5165 (lambda (x)
5166 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5167 ;; Extend the text properties to the beginning of the line
5168 (org-add-props x (text-properties-at (1- (length x)) x)
5169 'type "diary" 'date date 'face 'org-agenda-diary))
5170 entries)))))
5172 (defvar org-agenda-cleanup-fancy-diary-hook nil
5173 "Hook run when the fancy diary buffer is cleaned up.")
5175 (defun org-agenda-cleanup-fancy-diary ()
5176 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5177 This gets rid of the date, the underline under the date, and
5178 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5179 date. It also removes lines that contain only whitespace."
5180 (goto-char (point-min))
5181 (if (looking-at ".*?:[ \t]*")
5182 (progn
5183 (replace-match "")
5184 (re-search-forward "\n=+$" nil t)
5185 (replace-match "")
5186 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5187 (re-search-forward "\n=+$" nil t)
5188 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5189 (goto-char (point-min))
5190 (while (re-search-forward "^ +\n" nil t)
5191 (replace-match ""))
5192 (goto-char (point-min))
5193 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5194 (replace-match ""))
5195 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5197 ;; Make sure entries from the diary have the right text properties.
5198 (eval-after-load "diary-lib"
5199 '(if (boundp 'diary-modify-entry-list-string-function)
5200 ;; We can rely on the hook, nothing to do
5202 ;; Hook not available, must use advice to make this work
5203 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5204 "Make the position visible."
5205 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5206 (stringp string)
5207 buffer-file-name)
5208 (setq string (org-modify-diary-entry-string string))))))
5210 (defun org-modify-diary-entry-string (string)
5211 "Add text properties to string, allowing org-mode to act on it."
5212 (org-add-props string nil
5213 'mouse-face 'highlight
5214 'help-echo (if buffer-file-name
5215 (format "mouse-2 or RET jump to diary file %s"
5216 (abbreviate-file-name buffer-file-name))
5218 'org-agenda-diary-link t
5219 'org-marker (org-agenda-new-marker (point-at-bol))))
5221 (defun org-diary-default-entry ()
5222 "Add a dummy entry to the diary.
5223 Needed to avoid empty dates which mess up holiday display."
5224 ;; Catch the error if dealing with the new add-to-diary-alist
5225 (when org-disable-agenda-to-diary
5226 (condition-case nil
5227 (org-add-to-diary-list original-date "Org-mode dummy" "")
5228 (error
5229 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5231 (defun org-add-to-diary-list (&rest args)
5232 (if (fboundp 'diary-add-to-list)
5233 (apply 'diary-add-to-list args)
5234 (apply 'add-to-diary-list args)))
5236 (defvar org-diary-last-run-time nil)
5238 ;;;###autoload
5239 (defun org-diary (&rest args)
5240 "Return diary information from org files.
5241 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5242 It accesses org files and extracts information from those files to be
5243 listed in the diary. The function accepts arguments specifying what
5244 items should be listed. For a list of arguments allowed here, see the
5245 variable `org-agenda-entry-types'.
5247 The call in the diary file should look like this:
5249 &%%(org-diary) ~/path/to/some/orgfile.org
5251 Use a separate line for each org file to check. Or, if you omit the file name,
5252 all files listed in `org-agenda-files' will be checked automatically:
5254 &%%(org-diary)
5256 If you don't give any arguments (as in the example above), the default value
5257 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5258 So the example above may also be written as
5260 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5262 The function expects the lisp variables `entry' and `date' to be provided
5263 by the caller, because this is how the calendar works. Don't use this
5264 function from a program - use `org-agenda-get-day-entries' instead."
5265 (when (> (- (org-float-time)
5266 org-agenda-last-marker-time)
5268 ;; I am not sure if this works with sticky agendas, because the marker
5269 ;; list is then no longer a global variable.
5270 (org-agenda-reset-markers))
5271 (org-compile-prefix-format 'agenda)
5272 (org-set-sorting-strategy 'agenda)
5273 (setq args (or args org-agenda-entry-types))
5274 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5275 (list entry)
5276 (org-agenda-files t)))
5277 (time (org-float-time))
5278 file rtn results)
5279 (when (or (not org-diary-last-run-time)
5280 (> (- time
5281 org-diary-last-run-time)
5283 (org-agenda-prepare-buffers files))
5284 (setq org-diary-last-run-time time)
5285 ;; If this is called during org-agenda, don't return any entries to
5286 ;; the calendar. Org Agenda will list these entries itself.
5287 (if org-disable-agenda-to-diary (setq files nil))
5288 (while (setq file (pop files))
5289 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5290 (setq results (append results rtn)))
5291 (when results
5292 (setq results
5293 (mapcar (lambda (i) (replace-regexp-in-string
5294 org-bracket-link-regexp "\\3" i)) results))
5295 (concat (org-agenda-finalize-entries results) "\n"))))
5297 ;;; Agenda entry finders
5299 (defun org-agenda-get-day-entries (file date &rest args)
5300 "Does the work for `org-diary' and `org-agenda'.
5301 FILE is the path to a file to be checked for entries. DATE is date like
5302 the one returned by `calendar-current-date'. ARGS are symbols indicating
5303 which kind of entries should be extracted. For details about these, see
5304 the documentation of `org-diary'."
5305 (setq args (or args org-agenda-entry-types))
5306 (let* ((org-startup-folded nil)
5307 (org-startup-align-all-tables nil)
5308 (buffer (if (file-exists-p file)
5309 (org-get-agenda-file-buffer file)
5310 (error "No such file %s" file)))
5311 arg results rtn deadline-results)
5312 (if (not buffer)
5313 ;; If file does not exist, make sure an error message ends up in diary
5314 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5315 (with-current-buffer buffer
5316 (unless (derived-mode-p 'org-mode)
5317 (error "Agenda file %s is not in `org-mode'" file))
5318 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5319 (let ((case-fold-search nil))
5320 (save-excursion
5321 (save-restriction
5322 (if (eq buffer org-agenda-restrict)
5323 (narrow-to-region org-agenda-restrict-begin
5324 org-agenda-restrict-end)
5325 (widen))
5326 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5327 (while (setq arg (pop args))
5328 (cond
5329 ((and (eq arg :todo)
5330 (equal date (calendar-gregorian-from-absolute
5331 (org-today))))
5332 (setq rtn (org-agenda-get-todos))
5333 (setq results (append results rtn)))
5334 ((eq arg :timestamp)
5335 (setq rtn (org-agenda-get-blocks))
5336 (setq results (append results rtn))
5337 (setq rtn (org-agenda-get-timestamps deadline-results))
5338 (setq results (append results rtn)))
5339 ((eq arg :sexp)
5340 (setq rtn (org-agenda-get-sexps))
5341 (setq results (append results rtn)))
5342 ((eq arg :scheduled)
5343 (setq rtn (org-agenda-get-scheduled deadline-results))
5344 (setq results (append results rtn)))
5345 ((eq arg :scheduled*)
5346 (setq rtn (org-agenda-get-scheduled deadline-results t))
5347 (setq results (append results rtn)))
5348 ((eq arg :closed)
5349 (setq rtn (org-agenda-get-progress))
5350 (setq results (append results rtn)))
5351 ((eq arg :deadline)
5352 (setq rtn (org-agenda-get-deadlines))
5353 (setq deadline-results (copy-sequence rtn))
5354 (setq results (append results rtn)))
5355 ((eq arg :deadline*)
5356 (setq rtn (org-agenda-get-deadlines t))
5357 (setq deadline-results (copy-sequence rtn))
5358 (setq results (append results rtn))))))))
5359 results))))
5361 (defsubst org-em (x y list)
5362 "Is X or Y a member of LIST?"
5363 (or (memq x list) (memq y list)))
5365 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5366 (defvar org-agenda-sorting-strategy-selected nil)
5368 (defun org-agenda-entry-get-agenda-timestamp (pom)
5369 "Retrieve timestamp information for sorting agenda views.
5370 Given a point or marker POM, returns a cons cell of the timestamp
5371 and the timestamp type relevant for the sorting strategy in
5372 `org-agenda-sorting-strategy-selected'."
5373 (let (ts ts-date-type)
5374 (save-match-data
5375 (cond ((org-em 'scheduled-up 'scheduled-down
5376 org-agenda-sorting-strategy-selected)
5377 (setq ts (org-entry-get pom "SCHEDULED")
5378 ts-date-type " scheduled"))
5379 ((org-em 'deadline-up 'deadline-down
5380 org-agenda-sorting-strategy-selected)
5381 (setq ts (org-entry-get pom "DEADLINE")
5382 ts-date-type " deadline"))
5383 ((org-em 'ts-up 'ts-down
5384 org-agenda-sorting-strategy-selected)
5385 (setq ts (org-entry-get pom "TIMESTAMP")
5386 ts-date-type " timestamp"))
5387 ((org-em 'tsia-up 'tsia-down
5388 org-agenda-sorting-strategy-selected)
5389 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5390 ts-date-type " timestamp_ia"))
5391 ((org-em 'timestamp-up 'timestamp-down
5392 org-agenda-sorting-strategy-selected)
5393 (setq ts (or (org-entry-get pom "SCHEDULED")
5394 (org-entry-get pom "DEADLINE")
5395 (org-entry-get pom "TIMESTAMP")
5396 (org-entry-get pom "TIMESTAMP_IA"))
5397 ts-date-type ""))
5398 (t (setq ts-date-type "")))
5399 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5400 ts-date-type))))
5402 (defun org-agenda-get-todos ()
5403 "Return the TODO information for agenda display."
5404 (let* ((props (list 'face nil
5405 'done-face 'org-agenda-done
5406 'org-not-done-regexp org-not-done-regexp
5407 'org-todo-regexp org-todo-regexp
5408 'org-complex-heading-regexp org-complex-heading-regexp
5409 'mouse-face 'highlight
5410 'help-echo
5411 (format "mouse-2 or RET jump to org file %s"
5412 (abbreviate-file-name buffer-file-name))))
5413 (regexp (format org-heading-keyword-regexp-format
5414 (cond
5415 ((and org-select-this-todo-keyword
5416 (equal org-select-this-todo-keyword "*"))
5417 org-todo-regexp)
5418 (org-select-this-todo-keyword
5419 (concat "\\("
5420 (mapconcat 'identity
5421 (org-split-string
5422 org-select-this-todo-keyword
5423 "|")
5424 "\\|") "\\)"))
5425 (t org-not-done-regexp))))
5426 marker priority category level tags todo-state
5427 ts-date ts-date-type ts-date-pair
5428 ee txt beg end inherited-tags todo-state-end-pos)
5429 (goto-char (point-min))
5430 (while (re-search-forward regexp nil t)
5431 (catch :skip
5432 (save-match-data
5433 (beginning-of-line)
5434 (org-agenda-skip)
5435 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5436 (unless (and (setq todo-state (org-get-todo-state))
5437 (setq todo-state-end-pos (match-end 2)))
5438 (goto-char end)
5439 (throw :skip nil))
5440 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5441 (goto-char (1+ beg))
5442 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5443 (throw :skip nil)))
5444 (goto-char (match-beginning 2))
5445 (setq marker (org-agenda-new-marker (match-beginning 0))
5446 category (org-get-category)
5447 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5448 ts-date (car ts-date-pair)
5449 ts-date-type (cdr ts-date-pair)
5450 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5451 inherited-tags
5452 (or (eq org-agenda-show-inherited-tags 'always)
5453 (and (listp org-agenda-show-inherited-tags)
5454 (memq 'todo org-agenda-show-inherited-tags))
5455 (and (eq org-agenda-show-inherited-tags t)
5456 (or (eq org-agenda-use-tag-inheritance t)
5457 (memq 'todo org-agenda-use-tag-inheritance))))
5458 tags (org-get-tags-at nil (not inherited-tags))
5459 level (make-string (org-reduced-level (org-outline-level)) ? )
5460 txt (org-agenda-format-item "" txt level category tags t)
5461 priority (1+ (org-get-priority txt)))
5462 (org-add-props txt props
5463 'org-marker marker 'org-hd-marker marker
5464 'priority priority
5465 'level level
5466 'ts-date ts-date
5467 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5468 (push txt ee)
5469 (if org-agenda-todo-list-sublevels
5470 (goto-char todo-state-end-pos)
5471 (org-end-of-subtree 'invisible))))
5472 (nreverse ee)))
5474 (defun org-agenda-todo-custom-ignore-p (time n)
5475 "Check whether timestamp is farther away than n number of days.
5476 This function is invoked if `org-agenda-todo-ignore-deadlines',
5477 `org-agenda-todo-ignore-scheduled' or
5478 `org-agenda-todo-ignore-timestamp' is set to an integer."
5479 (let ((days (org-time-stamp-to-now
5480 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5481 (if (>= n 0)
5482 (>= days n)
5483 (<= days n))))
5485 ;;;###autoload
5486 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5487 (&optional end)
5488 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5489 (when (or org-agenda-todo-ignore-with-date
5490 org-agenda-todo-ignore-scheduled
5491 org-agenda-todo-ignore-deadlines
5492 org-agenda-todo-ignore-timestamp)
5493 (setq end (or end (save-excursion (outline-next-heading) (point))))
5494 (save-excursion
5495 (or (and org-agenda-todo-ignore-with-date
5496 (re-search-forward org-ts-regexp end t))
5497 (and org-agenda-todo-ignore-scheduled
5498 (re-search-forward org-scheduled-time-regexp end t)
5499 (cond
5500 ((eq org-agenda-todo-ignore-scheduled 'future)
5501 (> (org-time-stamp-to-now
5502 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5503 ((eq org-agenda-todo-ignore-scheduled 'past)
5504 (<= (org-time-stamp-to-now
5505 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5506 ((numberp org-agenda-todo-ignore-scheduled)
5507 (org-agenda-todo-custom-ignore-p
5508 (match-string 1) org-agenda-todo-ignore-scheduled))
5509 (t)))
5510 (and org-agenda-todo-ignore-deadlines
5511 (re-search-forward org-deadline-time-regexp end t)
5512 (cond
5513 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5514 ((eq org-agenda-todo-ignore-deadlines 'far)
5515 (not (org-deadline-close (match-string 1))))
5516 ((eq org-agenda-todo-ignore-deadlines 'future)
5517 (> (org-time-stamp-to-now
5518 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5519 ((eq org-agenda-todo-ignore-deadlines 'past)
5520 (<= (org-time-stamp-to-now
5521 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5522 ((numberp org-agenda-todo-ignore-deadlines)
5523 (org-agenda-todo-custom-ignore-p
5524 (match-string 1) org-agenda-todo-ignore-deadlines))
5525 (t (org-deadline-close (match-string 1)))))
5526 (and org-agenda-todo-ignore-timestamp
5527 (let ((buffer (current-buffer))
5528 (regexp
5529 (concat
5530 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5531 (start (point)))
5532 ;; Copy current buffer into a temporary one
5533 (with-temp-buffer
5534 (insert-buffer-substring buffer start end)
5535 (goto-char (point-min))
5536 ;; Delete SCHEDULED and DEADLINE items
5537 (while (re-search-forward regexp end t)
5538 (delete-region (match-beginning 0) (match-end 0)))
5539 (goto-char (point-min))
5540 ;; No search for timestamp left
5541 (when (re-search-forward org-ts-regexp nil t)
5542 (cond
5543 ((eq org-agenda-todo-ignore-timestamp 'future)
5544 (> (org-time-stamp-to-now
5545 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5546 ((eq org-agenda-todo-ignore-timestamp 'past)
5547 (<= (org-time-stamp-to-now
5548 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5549 ((numberp org-agenda-todo-ignore-timestamp)
5550 (org-agenda-todo-custom-ignore-p
5551 (match-string 1) org-agenda-todo-ignore-timestamp))
5552 (t))))))))))
5554 (defun org-agenda-get-timestamps (&optional deadline-results)
5555 "Return the date stamp information for agenda display."
5556 (let* ((props (list 'face 'org-agenda-calendar-event
5557 'org-not-done-regexp org-not-done-regexp
5558 'org-todo-regexp org-todo-regexp
5559 'org-complex-heading-regexp org-complex-heading-regexp
5560 'mouse-face 'highlight
5561 'help-echo
5562 (format "mouse-2 or RET jump to org file %s"
5563 (abbreviate-file-name buffer-file-name))))
5564 (d1 (calendar-absolute-from-gregorian date))
5566 (deadline-position-alist
5567 (mapcar (lambda (a) (and (setq mm (get-text-property
5568 0 'org-hd-marker a))
5569 (cons (marker-position mm) a)))
5570 deadline-results))
5571 (remove-re org-ts-regexp)
5572 (regexp
5573 (concat
5574 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5575 (regexp-quote
5576 (substring
5577 (format-time-string
5578 (car org-time-stamp-formats)
5579 (apply 'encode-time ; DATE bound by calendar
5580 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5581 1 11))
5582 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5583 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5584 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5585 donep tmp priority category level ee txt timestr tags
5586 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5587 inherited-tags ts-date)
5588 (goto-char (point-min))
5589 (while (setq end-of-match (re-search-forward regexp nil t))
5590 (setq b0 (match-beginning 0)
5591 b3 (match-beginning 3) e3 (match-end 3)
5592 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5593 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5594 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5595 (member todo-state
5596 org-agenda-repeating-timestamp-show-all)))
5597 (catch :skip
5598 (and (org-at-date-range-p) (throw :skip nil))
5599 (org-agenda-skip)
5600 (if (and (match-end 1)
5601 (not (= d1 (org-time-string-to-absolute
5602 (match-string 1) d1 nil show-all
5603 (current-buffer) b0))))
5604 (throw :skip nil))
5605 (if (and e3
5606 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5607 (throw :skip nil))
5608 (setq tmp (buffer-substring (max (point-min)
5609 (- b0 org-ds-keyword-length))
5611 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5612 inactivep (= (char-after b0) ?\[)
5613 deadlinep (string-match org-deadline-regexp tmp)
5614 scheduledp (string-match org-scheduled-regexp tmp)
5615 closedp (and org-agenda-include-inactive-timestamps
5616 (string-match org-closed-string tmp))
5617 clockp (and org-agenda-include-inactive-timestamps
5618 (or (string-match org-clock-string tmp)
5619 (string-match "]-+\\'" tmp)))
5620 warntime (get-text-property (point) 'org-appt-warntime)
5621 donep (member todo-state org-done-keywords))
5622 (if (or scheduledp deadlinep closedp clockp
5623 (and donep org-agenda-skip-timestamp-if-done))
5624 (throw :skip t))
5625 (if (string-match ">" timestr)
5626 ;; substring should only run to end of time stamp
5627 (setq timestr (substring timestr 0 (match-end 0))))
5628 (setq marker (org-agenda-new-marker b0)
5629 category (org-get-category b0))
5630 (save-excursion
5631 (if (not (re-search-backward org-outline-regexp-bol nil t))
5632 (throw :skip nil)
5633 (goto-char (match-beginning 0))
5634 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5635 (assoc (point) deadline-position-alist))
5636 (throw :skip nil))
5637 (setq hdmarker (org-agenda-new-marker)
5638 inherited-tags
5639 (or (eq org-agenda-show-inherited-tags 'always)
5640 (and (listp org-agenda-show-inherited-tags)
5641 (memq 'agenda org-agenda-show-inherited-tags))
5642 (and (eq org-agenda-show-inherited-tags t)
5643 (or (eq org-agenda-use-tag-inheritance t)
5644 (memq 'agenda org-agenda-use-tag-inheritance))))
5645 tags (org-get-tags-at nil (not inherited-tags))
5646 level (make-string (org-reduced-level (org-outline-level)) ? ))
5647 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5648 (setq head (or (match-string 1) ""))
5649 (setq txt (org-agenda-format-item
5650 (if inactivep org-agenda-inactive-leader nil)
5651 head level category tags timestr
5652 remove-re habitp)))
5653 (setq priority (org-get-priority txt))
5654 (org-add-props txt props 'priority priority
5655 'org-marker marker 'org-hd-marker hdmarker
5656 'date date
5657 'level level
5658 'ts-date
5659 (ignore-errors (org-time-string-to-absolute timestr))
5660 'todo-state todo-state
5661 'warntime warntime
5662 'type "timestamp")
5663 (push txt ee))
5664 (if org-agenda-skip-additional-timestamps-same-entry
5665 (outline-next-heading)
5666 (goto-char end-of-match))))
5667 (nreverse ee)))
5669 (defun org-agenda-get-sexps ()
5670 "Return the sexp information for agenda display."
5671 (require 'diary-lib)
5672 (let* ((props (list 'face 'org-agenda-calendar-sexp
5673 'mouse-face 'highlight
5674 'help-echo
5675 (format "mouse-2 or RET jump to org file %s"
5676 (abbreviate-file-name buffer-file-name))))
5677 (regexp "^&?%%(")
5678 marker category extra level ee txt tags entry
5679 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5680 (goto-char (point-min))
5681 (while (re-search-forward regexp nil t)
5682 (catch :skip
5683 (org-agenda-skip)
5684 (setq beg (match-beginning 0))
5685 (goto-char (1- (match-end 0)))
5686 (setq b (point))
5687 (forward-sexp 1)
5688 (setq sexp (buffer-substring b (point)))
5689 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5690 (org-trim (match-string 1))
5691 ""))
5692 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5693 (when result
5694 (setq marker (org-agenda-new-marker beg)
5695 level (make-string (org-reduced-level (org-outline-level)) ? )
5696 category (org-get-category beg)
5697 inherited-tags
5698 (or (eq org-agenda-show-inherited-tags 'always)
5699 (and (listp org-agenda-show-inherited-tags)
5700 (memq 'agenda org-agenda-show-inherited-tags))
5701 (and (eq org-agenda-show-inherited-tags t)
5702 (or (eq org-agenda-use-tag-inheritance t)
5703 (memq 'agenda org-agenda-use-tag-inheritance))))
5704 tags (org-get-tags-at nil (not inherited-tags))
5705 todo-state (org-get-todo-state)
5706 warntime (get-text-property (point) 'org-appt-warntime)
5707 extra nil)
5709 (dolist (r (if (stringp result)
5710 (list result)
5711 result)) ;; we expect a list here
5712 (when (and org-agenda-diary-sexp-prefix
5713 (string-match org-agenda-diary-sexp-prefix r))
5714 (setq extra (match-string 0 r)
5715 r (replace-match "" nil nil r)))
5716 (if (string-match "\\S-" r)
5717 (setq txt r)
5718 (setq txt "SEXP entry returned empty string"))
5719 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5720 (org-add-props txt props 'org-marker marker
5721 'date date 'todo-state todo-state
5722 'level level 'type "sexp" 'warntime warntime)
5723 (push txt ee)))))
5724 (nreverse ee)))
5726 ;; Calendar sanity: define some functions that are independent of
5727 ;; `calendar-date-style'.
5728 ;; Normally I would like to use ISO format when calling the diary functions,
5729 ;; but to make sure we still have Emacs 22 compatibility we bind
5730 ;; also `european-calendar-style' and use european format
5731 (defun org-anniversary (year month day &optional mark)
5732 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5733 (org-no-warnings
5734 (let ((calendar-date-style 'european) (european-calendar-style t))
5735 (diary-anniversary day month year mark))))
5736 (defun org-cyclic (N year month day &optional mark)
5737 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5738 (org-no-warnings
5739 (let ((calendar-date-style 'european) (european-calendar-style t))
5740 (diary-cyclic N day month year mark))))
5741 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5742 "Like `diary-block', but with fixed (ISO) order of arguments."
5743 (org-no-warnings
5744 (let ((calendar-date-style 'european) (european-calendar-style t))
5745 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5746 (defun org-date (year month day &optional mark)
5747 "Like `diary-date', but with fixed (ISO) order of arguments."
5748 (org-no-warnings
5749 (let ((calendar-date-style 'european) (european-calendar-style t))
5750 (diary-date day month year mark))))
5752 ;; Define the `org-class' function
5753 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5754 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5755 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5756 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5757 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5758 `holidays', then any date that is known by the Emacs calendar to be a
5759 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5760 then those holidays will be skipped."
5761 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5762 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5763 (d (calendar-absolute-from-gregorian date))
5764 (h (when skip-weeks (calendar-check-holidays date))))
5765 (and
5766 (<= date1 d)
5767 (<= d date2)
5768 (= (calendar-day-of-week date) dayname)
5769 (or (not skip-weeks)
5770 (progn
5771 (require 'cal-iso)
5772 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5773 (not (or (and h (memq 'holidays skip-weeks))
5774 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5775 entry)))
5777 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5778 "Like `org-class', but honor `calendar-date-style'.
5779 The order of the first 2 times 3 arguments depends on the variable
5780 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5781 So for American calendars, give this as MONTH DAY YEAR, for European as
5782 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5783 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5784 is any number of ISO weeks in the block period for which the item should
5785 be skipped.
5787 This function is here only for backward compatibility and it is deprecated,
5788 please use `org-class' instead."
5789 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5790 (date2 (org-order-calendar-date-args m2 d2 y2)))
5791 (org-class
5792 (nth 2 date1) (car date1) (nth 1 date1)
5793 (nth 2 date2) (car date2) (nth 1 date2)
5794 dayname skip-weeks)))
5795 (make-obsolete 'org-diary-class 'org-class "")
5797 (defalias 'org-get-closed 'org-agenda-get-progress)
5798 (defun org-agenda-get-progress ()
5799 "Return the logged TODO entries for agenda display."
5800 (let* ((props (list 'mouse-face 'highlight
5801 'org-not-done-regexp org-not-done-regexp
5802 'org-todo-regexp org-todo-regexp
5803 'org-complex-heading-regexp org-complex-heading-regexp
5804 'help-echo
5805 (format "mouse-2 or RET jump to org file %s"
5806 (abbreviate-file-name buffer-file-name))))
5807 (items (if (consp org-agenda-show-log-scoped)
5808 org-agenda-show-log-scoped
5809 (if (eq org-agenda-show-log-scoped 'clockcheck)
5810 '(clock)
5811 org-agenda-log-mode-items)))
5812 (parts
5813 (delq nil
5814 (list
5815 (if (memq 'closed items) (concat "\\<" org-closed-string))
5816 (if (memq 'clock items) (concat "\\<" org-clock-string))
5817 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5818 (parts-re (if parts (mapconcat 'identity parts "\\|")
5819 (error "`org-agenda-log-mode-items' is empty")))
5820 (regexp (concat
5821 "\\(" parts-re "\\)"
5822 " *\\["
5823 (regexp-quote
5824 (substring
5825 (format-time-string
5826 (car org-time-stamp-formats)
5827 (apply 'encode-time ; DATE bound by calendar
5828 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5829 1 11))))
5830 (org-agenda-search-headline-for-time nil)
5831 marker hdmarker priority category level tags closedp
5832 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5833 (goto-char (point-min))
5834 (while (re-search-forward regexp nil t)
5835 (catch :skip
5836 (org-agenda-skip)
5837 (setq marker (org-agenda-new-marker (match-beginning 0))
5838 closedp (equal (match-string 1) org-closed-string)
5839 statep (equal (string-to-char (match-string 1)) ?-)
5840 clockp (not (or closedp statep))
5841 state (and statep (match-string 2))
5842 category (org-get-category (match-beginning 0))
5843 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5844 (when (string-match "\\]" timestr)
5845 ;; substring should only run to end of time stamp
5846 (setq rest (substring timestr (match-end 0))
5847 timestr (substring timestr 0 (match-end 0)))
5848 (if (and (not closedp) (not statep)
5849 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5850 rest))
5851 (progn (setq timestr (concat (substring timestr 0 -1)
5852 "-" (match-string 1 rest) "]"))
5853 (setq clocked (match-string 2 rest)))
5854 (setq clocked "-")))
5855 (save-excursion
5856 (setq extra
5857 (cond
5858 ((not org-agenda-log-mode-add-notes) nil)
5859 (statep
5860 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5861 (match-string 1)))
5862 (clockp
5863 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5864 (match-string 1)))))
5865 (if (not (re-search-backward org-outline-regexp-bol nil t))
5866 (throw :skip nil)
5867 (goto-char (match-beginning 0))
5868 (setq hdmarker (org-agenda-new-marker)
5869 inherited-tags
5870 (or (eq org-agenda-show-inherited-tags 'always)
5871 (and (listp org-agenda-show-inherited-tags)
5872 (memq 'todo org-agenda-show-inherited-tags))
5873 (and (eq org-agenda-show-inherited-tags t)
5874 (or (eq org-agenda-use-tag-inheritance t)
5875 (memq 'todo org-agenda-use-tag-inheritance))))
5876 tags (org-get-tags-at nil (not inherited-tags))
5877 level (make-string (org-reduced-level (org-outline-level)) ? ))
5878 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5879 (setq txt (match-string 1))
5880 (when extra
5881 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5882 (setq txt (concat (substring txt 0 (match-beginning 1))
5883 " - " extra " " (match-string 2 txt)))
5884 (setq txt (concat txt " - " extra))))
5885 (setq txt (org-agenda-format-item
5886 (cond
5887 (closedp "Closed: ")
5888 (statep (concat "State: (" state ")"))
5889 (t (concat "Clocked: (" clocked ")")))
5890 txt level category tags timestr)))
5891 (setq priority 100000)
5892 (org-add-props txt props
5893 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5894 'priority priority 'level level
5895 'type "closed" 'date date
5896 'undone-face 'org-warning 'done-face 'org-agenda-done)
5897 (push txt ee))
5898 (goto-char (point-at-eol))))
5899 (nreverse ee)))
5901 (defun org-agenda-show-clocking-issues ()
5902 "Add overlays, showing issues with clocking.
5903 See also the user option `org-agenda-clock-consistency-checks'."
5904 (interactive)
5905 (let* ((org-time-clocksum-use-effort-durations nil)
5906 (pl org-agenda-clock-consistency-checks)
5907 (re (concat "^[ \t]*"
5908 org-clock-string
5909 "[ \t]+"
5910 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5911 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5912 (tlstart 0.)
5913 (tlend 0.)
5914 (maxtime (org-hh:mm-string-to-minutes
5915 (or (plist-get pl :max-duration) "24:00")))
5916 (mintime (org-hh:mm-string-to-minutes
5917 (or (plist-get pl :min-duration) 0)))
5918 (maxgap (org-hh:mm-string-to-minutes
5919 ;; default 30:00 means never complain
5920 (or (plist-get pl :max-gap) "30:00")))
5921 (gapok (mapcar 'org-hh:mm-string-to-minutes
5922 (plist-get pl :gap-ok-around)))
5923 (def-face (or (plist-get pl :default-face)
5924 '((:background "DarkRed") (:foreground "white"))))
5925 issue face m te ts dt ov)
5926 (goto-char (point-min))
5927 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5928 (setq issue nil face def-face)
5929 (catch 'next
5930 (setq m (org-get-at-bol 'org-marker)
5931 te nil ts nil)
5932 (unless (and m (markerp m))
5933 (setq issue "No valid clock line") (throw 'next t))
5934 (org-with-point-at m
5935 (save-excursion
5936 (goto-char (point-at-bol))
5937 (unless (looking-at re)
5938 (error "No valid Clock line")
5939 (throw 'next t))
5940 (unless (match-end 3)
5941 (setq issue "No end time"
5942 face (or (plist-get pl :no-end-time-face) face))
5943 (throw 'next t))
5944 (setq ts (match-string 1)
5945 te (match-string 3)
5946 ts (org-float-time
5947 (apply 'encode-time (org-parse-time-string ts)))
5948 te (org-float-time
5949 (apply 'encode-time (org-parse-time-string te)))
5950 dt (- te ts))))
5951 (cond
5952 ((> dt (* 60 maxtime))
5953 ;; a very long clocking chunk
5954 (setq issue (format "Clocking interval is very long: %s"
5955 (org-minutes-to-clocksum-string
5956 (floor (/ (float dt) 60.))))
5957 face (or (plist-get pl :long-face) face)))
5958 ((< dt (* 60 mintime))
5959 ;; a very short clocking chunk
5960 (setq issue (format "Clocking interval is very short: %s"
5961 (org-minutes-to-clocksum-string
5962 (floor (/ (float dt) 60.))))
5963 face (or (plist-get pl :short-face) face)))
5964 ((and (> tlend 0) (< ts tlend))
5965 ;; Two clock entries are overlapping
5966 (setq issue (format "Clocking overlap: %d minutes"
5967 (/ (- tlend ts) 60))
5968 face (or (plist-get pl :overlap-face) face)))
5969 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5970 ;; There is a gap, lets see if we need to report it
5971 (unless (org-agenda-check-clock-gap tlend ts gapok)
5972 (setq issue (format "Clocking gap: %d minutes"
5973 (/ (- ts tlend) 60))
5974 face (or (plist-get pl :gap-face) face))))
5975 (t nil)))
5976 (setq tlend (or te tlend) tlstart (or ts tlstart))
5977 (when issue
5978 ;; OK, there was some issue, add an overlay to show the issue
5979 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5980 (overlay-put ov 'before-string
5981 (concat
5982 (org-add-props
5983 (format "%-43s" (concat " " issue))
5985 'face face)
5986 "\n"))
5987 (overlay-put ov 'evaporate t)))))
5989 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5990 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5991 (catch 'exit
5992 (unless ok-list
5993 ;; there are no OK times for gaps...
5994 (throw 'exit nil))
5995 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5996 ;; This is more than 24 hours, so it is OK.
5997 ;; because we have at least one OK time, that must be in the
5998 ;; 24 hour interval.
5999 (throw 'exit t))
6000 ;; We have a shorter gap.
6001 ;; Now we have to get the minute of the day when these times are
6002 (let* ((t1dec (decode-time (seconds-to-time t1)))
6003 (t2dec (decode-time (seconds-to-time t2)))
6004 ;; compute the minute on the day
6005 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6006 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6007 (when (< min2 min1)
6008 ;; if min2 is smaller than min1, this means it is on the next day.
6009 ;; Wrap it to after midnight.
6010 (setq min2 (+ min2 1440)))
6011 ;; Now check if any of the OK times is in the gap
6012 (mapc (lambda (x)
6013 ;; Wrap the time to after midnight if necessary
6014 (if (< x min1) (setq x (+ x 1440)))
6015 ;; Check if in interval
6016 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6017 ok-list)
6018 ;; Nope, this gap is not OK
6019 nil)))
6021 (defun org-agenda-get-deadlines (&optional with-hour)
6022 "Return the deadline information for agenda display.
6023 When WITH-HOUR is non-nil, only return deadlines with an hour
6024 specification like [h]h:mm."
6025 (let* ((props (list 'mouse-face 'highlight
6026 'org-not-done-regexp org-not-done-regexp
6027 'org-todo-regexp org-todo-regexp
6028 'org-complex-heading-regexp org-complex-heading-regexp
6029 'help-echo
6030 (format "mouse-2 or RET jump to org file %s"
6031 (abbreviate-file-name buffer-file-name))))
6032 (regexp (if with-hour
6033 org-deadline-time-hour-regexp
6034 org-deadline-time-regexp))
6035 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6036 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6037 (dl0 (car org-agenda-deadline-leaders))
6038 (dl1 (nth 1 org-agenda-deadline-leaders))
6039 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6040 d2 diff dfrac wdays pos pos1 category level
6041 tags suppress-prewarning ee txt head face s todo-state
6042 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6043 (goto-char (point-min))
6044 (while (re-search-forward regexp nil t)
6045 (catch :skip
6046 (org-agenda-skip)
6047 (setq s (match-string 1)
6048 txt nil
6049 pos (1- (match-beginning 1))
6050 todo-state (save-match-data (org-get-todo-state))
6051 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6052 (member todo-state
6053 org-agenda-repeating-timestamp-show-all))
6054 d2 (org-time-string-to-absolute
6055 s d1 'past show-all (current-buffer) pos)
6056 diff (- d2 d1))
6057 (setq suppress-prewarning
6058 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6059 (let ((item (buffer-substring (point-at-bol)
6060 (point-at-eol))))
6061 (save-match-data
6062 (and (string-match
6063 org-scheduled-time-regexp item)
6064 (match-string 1 item)))))))
6065 (cond
6066 ((not ds) nil)
6067 ;; The current item has a scheduled date (in ds), so
6068 ;; evaluate its prewarning lead time.
6069 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6070 ;; Use global prewarning-restart lead time.
6071 org-agenda-skip-deadline-prewarning-if-scheduled)
6072 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6073 'pre-scheduled)
6074 ;; Set prewarning to no earlier than scheduled.
6075 (min (- d2 (org-time-string-to-absolute
6076 ds d1 'past show-all (current-buffer) pos))
6077 org-deadline-warning-days))
6078 ;; Set prewarning to deadline.
6079 (t 0))))
6080 (setq wdays (if suppress-prewarning
6081 (let ((org-deadline-warning-days suppress-prewarning))
6082 (org-get-wdays s))
6083 (org-get-wdays s))
6084 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6085 upcomingp (and todayp (> diff 0)))
6086 ;; When to show a deadline in the calendar:
6087 ;; If the expiration is within wdays warning time.
6088 ;; Past-due deadlines are only shown on the current date
6089 (if (and (or (and (<= diff wdays)
6090 (and todayp (not org-agenda-only-exact-dates)))
6091 (= diff 0)))
6092 (save-excursion
6093 ;; (setq todo-state (org-get-todo-state))
6094 (setq donep (member todo-state org-done-keywords))
6095 (if (and donep
6096 (or org-agenda-skip-deadline-if-done
6097 (not (= diff 0))))
6098 (setq txt nil)
6099 (setq category (org-get-category)
6100 warntime (get-text-property (point) 'org-appt-warntime))
6101 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6102 (throw :skip nil)
6103 (goto-char (match-end 0))
6104 (setq pos1 (match-beginning 0))
6105 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6106 (setq inherited-tags
6107 (or (eq org-agenda-show-inherited-tags 'always)
6108 (and (listp org-agenda-show-inherited-tags)
6109 (memq 'agenda org-agenda-show-inherited-tags))
6110 (and (eq org-agenda-show-inherited-tags t)
6111 (or (eq org-agenda-use-tag-inheritance t)
6112 (memq 'agenda org-agenda-use-tag-inheritance))))
6113 tags (org-get-tags-at pos1 (not inherited-tags)))
6114 (setq head (buffer-substring
6115 (point)
6116 (progn (skip-chars-forward "^\r\n")
6117 (point))))
6118 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6119 (setq timestr
6120 (concat (substring s (match-beginning 1)) " "))
6121 (setq timestr 'time))
6122 (setq txt (org-agenda-format-item
6123 (cond ((= diff 0) dl0)
6124 ((> diff 0)
6125 (if (functionp dl1)
6126 (funcall dl1 diff date)
6127 (format dl1 diff)))
6129 (if (functionp dl2)
6130 (funcall dl2 diff date)
6131 (format dl2 (if (string= dl2 dl1)
6132 diff (abs diff))))))
6133 head level category tags
6134 (if (not (= diff 0)) nil timestr)))))
6135 (when txt
6136 (setq face (org-agenda-deadline-face dfrac))
6137 (org-add-props txt props
6138 'org-marker (org-agenda-new-marker pos)
6139 'warntime warntime
6140 'level level
6141 'ts-date d2
6142 'org-hd-marker (org-agenda-new-marker pos1)
6143 'priority (+ (- diff)
6144 (org-get-priority txt))
6145 'todo-state todo-state
6146 'type (if upcomingp "upcoming-deadline" "deadline")
6147 'date (if upcomingp date d2)
6148 'face (if donep 'org-agenda-done face)
6149 'undone-face face 'done-face 'org-agenda-done)
6150 (push txt ee))))))
6151 (nreverse ee)))
6153 (defun org-agenda-deadline-face (fraction)
6154 "Return the face to displaying a deadline item.
6155 FRACTION is what fraction of the head-warning time has passed."
6156 (let ((faces org-agenda-deadline-faces) f)
6157 (catch 'exit
6158 (while (setq f (pop faces))
6159 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6161 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6162 "Return the scheduled information for agenda display.
6163 When WITH-HOUR is non-nil, only return scheduled items with
6164 an hour specification like [h]h:mm."
6165 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6166 'org-todo-regexp org-todo-regexp
6167 'org-complex-heading-regexp org-complex-heading-regexp
6168 'done-face 'org-agenda-done
6169 'mouse-face 'highlight
6170 'help-echo
6171 (format "mouse-2 or RET jump to org file %s"
6172 (abbreviate-file-name buffer-file-name))))
6173 (regexp (if with-hour
6174 org-scheduled-time-hour-regexp
6175 org-scheduled-time-regexp))
6176 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6177 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6179 (deadline-position-alist
6180 (mapcar (lambda (a) (and (setq mm (get-text-property
6181 0 'org-hd-marker a))
6182 (cons (marker-position mm) a)))
6183 deadline-results))
6184 d2 diff pos pos1 category level tags donep
6185 ee txt head pastschedp todo-state face timestr s habitp show-all
6186 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6187 ddays)
6188 (goto-char (point-min))
6189 (while (re-search-forward regexp nil t)
6190 (catch :skip
6191 (org-agenda-skip)
6192 (setq s (match-string 1)
6193 txt nil
6194 pos (1- (match-beginning 1))
6195 todo-state (save-match-data (org-get-todo-state))
6196 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6197 (member todo-state
6198 org-agenda-repeating-timestamp-show-all))
6199 d2 (org-time-string-to-absolute
6200 s d1 'past show-all (current-buffer) pos)
6201 diff (- d2 d1)
6202 warntime (get-text-property (point) 'org-appt-warntime))
6203 (setq pastschedp (and todayp (< diff 0)))
6204 (setq did-habit-check-p nil)
6205 (setq suppress-delay
6206 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6207 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6208 (save-match-data
6209 (and (string-match
6210 org-deadline-time-regexp item)
6211 (match-string 1 item)))))))
6212 (cond
6213 ((not ds) nil)
6214 ;; The current item has a deadline date (in ds), so
6215 ;; evaluate its delay time.
6216 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6217 ;; Use global delay time.
6218 (- org-agenda-skip-scheduled-delay-if-deadline))
6219 ((eq org-agenda-skip-scheduled-delay-if-deadline
6220 'post-deadline)
6221 ;; Set delay to no later than deadline.
6222 (min (- d2 (org-time-string-to-absolute
6223 ds d1 'past show-all (current-buffer) pos))
6224 org-scheduled-delay-days))
6225 (t 0))))
6226 (setq ddays (if suppress-delay
6227 (let ((org-scheduled-delay-days suppress-delay))
6228 (org-get-wdays s t t))
6229 (org-get-wdays s t)))
6230 ;; Use a delay of 0 when there is a repeater and the delay is
6231 ;; of the form --3d
6232 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6233 (< (org-time-string-to-absolute s)
6234 (org-time-string-to-absolute
6235 s d2 'past nil (current-buffer) pos)))
6236 (setq ddays 0))
6237 ;; When to show a scheduled item in the calendar:
6238 ;; If it is on or past the date.
6239 (when (or (and (> ddays 0) (= diff (- ddays)))
6240 (and (zerop ddays) (= diff 0))
6241 (and (< (+ diff ddays) 0)
6242 (< (abs diff) org-scheduled-past-days)
6243 (and todayp (not org-agenda-only-exact-dates)))
6244 ;; org-is-habit-p uses org-entry-get, which is expansive
6245 ;; so we go extra mile to only call it once
6246 (and todayp
6247 (boundp 'org-habit-show-all-today)
6248 org-habit-show-all-today
6249 (setq did-habit-check-p t)
6250 (setq habitp (and (functionp 'org-is-habit-p)
6251 (org-is-habit-p)))))
6252 (save-excursion
6253 (setq donep (member todo-state org-done-keywords))
6254 (if (and donep
6255 (or org-agenda-skip-scheduled-if-done
6256 (not (= diff 0))
6257 (and (functionp 'org-is-habit-p)
6258 (org-is-habit-p))))
6259 (setq txt nil)
6260 (setq habitp (if did-habit-check-p habitp
6261 (and (functionp 'org-is-habit-p)
6262 (org-is-habit-p))))
6263 (setq category (org-get-category))
6264 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6265 'repeated-after-deadline)
6266 (org-get-deadline-time (point))
6267 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6268 (throw :skip nil))
6269 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6270 (throw :skip nil)
6271 (goto-char (match-end 0))
6272 (setq pos1 (match-beginning 0))
6273 (if habitp
6274 (if (or (not org-habit-show-habits)
6275 (and (not todayp)
6276 (boundp 'org-habit-show-habits-only-for-today)
6277 org-habit-show-habits-only-for-today))
6278 (throw :skip nil))
6279 (if (and
6280 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6281 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6282 pastschedp))
6283 (setq mm (assoc pos1 deadline-position-alist)))
6284 (throw :skip nil)))
6285 (setq inherited-tags
6286 (or (eq org-agenda-show-inherited-tags 'always)
6287 (and (listp org-agenda-show-inherited-tags)
6288 (memq 'agenda org-agenda-show-inherited-tags))
6289 (and (eq org-agenda-show-inherited-tags t)
6290 (or (eq org-agenda-use-tag-inheritance t)
6291 (memq 'agenda org-agenda-use-tag-inheritance))))
6293 tags (org-get-tags-at nil (not inherited-tags)))
6294 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6295 (setq head (buffer-substring
6296 (point)
6297 (progn (skip-chars-forward "^\r\n") (point))))
6298 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6299 (setq timestr
6300 (concat (substring s (match-beginning 1)) " "))
6301 (setq timestr 'time))
6302 (setq txt (org-agenda-format-item
6303 (if (= diff 0)
6304 (car org-agenda-scheduled-leaders)
6305 (format (nth 1 org-agenda-scheduled-leaders)
6306 (- 1 diff)))
6307 head level category tags
6308 (if (not (= diff 0)) nil timestr)
6309 nil habitp))))
6310 (when txt
6311 (setq face
6312 (cond
6313 ((and (not habitp) pastschedp)
6314 'org-scheduled-previously)
6315 (todayp 'org-scheduled-today)
6316 (t 'org-scheduled))
6317 habitp (and habitp (org-habit-parse-todo)))
6318 (org-add-props txt props
6319 'undone-face face
6320 'face (if donep 'org-agenda-done face)
6321 'org-marker (org-agenda-new-marker pos)
6322 'org-hd-marker (org-agenda-new-marker pos1)
6323 'type (if pastschedp "past-scheduled" "scheduled")
6324 'date (if pastschedp d2 date)
6325 'ts-date d2
6326 'warntime warntime
6327 'level level
6328 'priority (if habitp
6329 (org-habit-get-priority habitp)
6330 (+ 94 (- 5 diff) (org-get-priority txt)))
6331 'org-habit-p habitp
6332 'todo-state todo-state)
6333 (push txt ee))))))
6334 (nreverse ee)))
6336 (defun org-agenda-get-blocks ()
6337 "Return the date-range information for agenda display."
6338 (let* ((props (list 'face nil
6339 'org-not-done-regexp org-not-done-regexp
6340 'org-todo-regexp org-todo-regexp
6341 'org-complex-heading-regexp org-complex-heading-regexp
6342 'mouse-face 'highlight
6343 'help-echo
6344 (format "mouse-2 or RET jump to org file %s"
6345 (abbreviate-file-name buffer-file-name))))
6346 (regexp org-tr-regexp)
6347 (d0 (calendar-absolute-from-gregorian date))
6348 marker hdmarker ee txt d1 d2 s1 s2 category
6349 level todo-state tags pos head donep inherited-tags)
6350 (goto-char (point-min))
6351 (while (re-search-forward regexp nil t)
6352 (catch :skip
6353 (org-agenda-skip)
6354 (setq pos (point))
6355 (let ((start-time (match-string 1))
6356 (end-time (match-string 2)))
6357 (setq s1 (match-string 1)
6358 s2 (match-string 2)
6359 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6360 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6361 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6362 ;; Only allow days between the limits, because the normal
6363 ;; date stamps will catch the limits.
6364 (save-excursion
6365 (setq todo-state (org-get-todo-state))
6366 (setq donep (member todo-state org-done-keywords))
6367 (if (and donep org-agenda-skip-timestamp-if-done)
6368 (throw :skip t))
6369 (setq marker (org-agenda-new-marker (point))
6370 category (org-get-category))
6371 (if (not (re-search-backward org-outline-regexp-bol nil t))
6372 (throw :skip nil)
6373 (goto-char (match-beginning 0))
6374 (setq hdmarker (org-agenda-new-marker (point))
6375 inherited-tags
6376 (or (eq org-agenda-show-inherited-tags 'always)
6377 (and (listp org-agenda-show-inherited-tags)
6378 (memq 'agenda org-agenda-show-inherited-tags))
6379 (and (eq org-agenda-show-inherited-tags t)
6380 (or (eq org-agenda-use-tag-inheritance t)
6381 (memq 'agenda org-agenda-use-tag-inheritance))))
6383 tags (org-get-tags-at nil (not inherited-tags)))
6384 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6385 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6386 (setq head (match-string 1))
6387 (let ((remove-re
6388 (if org-agenda-remove-timeranges-from-blocks
6389 (concat
6390 "<" (regexp-quote s1) ".*?>"
6391 "--"
6392 "<" (regexp-quote s2) ".*?>")
6393 nil)))
6394 (setq txt (org-agenda-format-item
6395 (format
6396 (nth (if (= d1 d2) 0 1)
6397 org-agenda-timerange-leaders)
6398 (1+ (- d0 d1)) (1+ (- d2 d1)))
6399 head level category tags
6400 (cond ((and (= d1 d0) (= d2 d0))
6401 (concat "<" start-time ">--<" end-time ">"))
6402 ((= d1 d0)
6403 (concat "<" start-time ">"))
6404 ((= d2 d0)
6405 (concat "<" end-time ">")))
6406 remove-re))))
6407 (org-add-props txt props
6408 'org-marker marker 'org-hd-marker hdmarker
6409 'type "block" 'date date
6410 'level level
6411 'todo-state todo-state
6412 'priority (org-get-priority txt))
6413 (push txt ee))))
6414 (goto-char pos)))
6415 ;; Sort the entries by expiration date.
6416 (nreverse ee)))
6418 ;;; Agenda presentation and sorting
6420 (defvar org-prefix-has-time nil
6421 "A flag, set by `org-compile-prefix-format'.
6422 The flag is set if the currently compiled format contains a `%t'.")
6423 (defvar org-prefix-has-tag nil
6424 "A flag, set by `org-compile-prefix-format'.
6425 The flag is set if the currently compiled format contains a `%T'.")
6426 (defvar org-prefix-has-effort nil
6427 "A flag, set by `org-compile-prefix-format'.
6428 The flag is set if the currently compiled format contains a `%e'.")
6429 (defvar org-prefix-has-breadcrumbs nil
6430 "A flag, set by `org-compile-prefix-format'.
6431 The flag is set if the currently compiled format contains a `%b'.")
6432 (defvar org-prefix-category-length nil
6433 "Used by `org-compile-prefix-format' to remember the category field width.")
6434 (defvar org-prefix-category-max-length nil
6435 "Used by `org-compile-prefix-format' to remember the category field width.")
6437 (defun org-agenda-get-category-icon (category)
6438 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6439 (dolist (entry org-agenda-category-icon-alist)
6440 (when (org-string-match-p (car entry) category)
6441 (if (listp (cadr entry))
6442 (return (cadr entry))
6443 (return (apply 'create-image (cdr entry)))))))
6445 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6446 remove-re habitp)
6447 "Format TXT to be inserted into the agenda buffer.
6448 In particular, add the prefix and corresponding text properties.
6450 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6451 LEVEL may be a string to replace the `%l' specifier.
6452 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6453 category taken from local variable or file name. It will replace the `%c'
6454 specifier in the format.
6455 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6456 TXT for sorting of this entry, and for the `%t' specifier in the format.
6457 When DOTIME is a string, this string is searched for a time before TXT is.
6458 TAGS can be the tags of the headline.
6459 Any match of REMOVE-RE will be removed from TXT."
6460 ;; We keep the org-prefix-* variable values along with a compiled
6461 ;; formatter, so that multiple agendas existing at the same time do
6462 ;; not step on each other toes.
6464 ;; It was inconvenient to make these variables buffer local in
6465 ;; Agenda buffers, because this function expects to be called with
6466 ;; the buffer where item comes from being current, and not agenda
6467 ;; buffer
6468 (let* ((bindings (car org-prefix-format-compiled))
6469 (formatter (cadr org-prefix-format-compiled)))
6470 (loop for (var value) in bindings
6471 do (set var value))
6472 (save-match-data
6473 ;; Diary entries sometimes have extra whitespace at the beginning
6474 (setq txt (org-trim txt))
6476 ;; Fix the tags part in txt
6477 (setq txt (org-agenda-fix-displayed-tags
6478 txt tags
6479 org-agenda-show-inherited-tags
6480 org-agenda-hide-tags-regexp))
6482 (let* ((category (or category
6483 (if buffer-file-name
6484 (file-name-sans-extension
6485 (file-name-nondirectory buffer-file-name))
6486 "")))
6487 (category-icon (org-agenda-get-category-icon category))
6488 (category-icon (if category-icon
6489 (propertize " " 'display category-icon)
6490 ""))
6491 (effort (and (not (string= txt ""))
6492 (get-text-property 1 'effort txt)))
6493 ;; time, tag, effort are needed for the eval of the prefix format
6494 (tag (if tags (nth (1- (length tags)) tags) ""))
6495 time
6496 (ts (if dotime (concat
6497 (if (stringp dotime) dotime "")
6498 (and org-agenda-search-headline-for-time txt))))
6499 (time-of-day (and dotime (org-get-time-of-day ts)))
6500 stamp plain s0 s1 s2 rtn srp l
6501 duration breadcrumbs)
6502 (and (derived-mode-p 'org-mode) buffer-file-name
6503 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6504 (when (and dotime time-of-day)
6505 ;; Extract starting and ending time and move them to prefix
6506 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6507 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6508 (setq s0 (match-string 0 ts)
6509 srp (and stamp (match-end 3))
6510 s1 (match-string (if plain 1 2) ts)
6511 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6513 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6514 ;; them, we might want to remove them there to avoid duplication.
6515 ;; The user can turn this off with a variable.
6516 (if (and org-prefix-has-time
6517 org-agenda-remove-times-when-in-prefix (or stamp plain)
6518 (string-match (concat (regexp-quote s0) " *") txt)
6519 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6520 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6521 (= (match-beginning 0) 0)
6523 (setq txt (replace-match "" nil nil txt))))
6524 ;; Normalize the time(s) to 24 hour
6525 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6526 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6528 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6529 (let (org-time-clocksum-use-effort-durations)
6530 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6531 (setq s2
6532 (org-minutes-to-clocksum-string
6533 (+ (org-hh:mm-string-to-minutes s1)
6534 org-agenda-default-appointment-duration)))))
6536 ;; Compute the duration
6537 (when s2
6538 (setq duration (- (org-hh:mm-string-to-minutes s2)
6539 (org-hh:mm-string-to-minutes s1)))))
6541 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6542 txt)
6543 ;; Tags are in the string
6544 (if (or (eq org-agenda-remove-tags t)
6545 (and org-agenda-remove-tags
6546 org-prefix-has-tag))
6547 (setq txt (replace-match "" t t txt))
6548 (setq txt (replace-match
6549 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6550 (match-string 2 txt))
6551 t t txt))))
6553 (when remove-re
6554 (while (string-match remove-re txt)
6555 (setq txt (replace-match "" t t txt))))
6557 ;; Set org-heading property on `txt' to mark the start of the
6558 ;; heading.
6559 (add-text-properties 0 (length txt) '(org-heading t) txt)
6561 ;; Prepare the variables needed in the eval of the compiled format
6562 (if org-prefix-has-breadcrumbs
6563 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6564 (let ((s (org-display-outline-path nil nil "->" t)))
6565 (if (eq "" s) "" (concat s "->"))))))
6566 (setq time (cond (s2 (concat
6567 (org-agenda-time-of-day-to-ampm-maybe s1)
6568 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6569 (if org-agenda-timegrid-use-ampm " ")))
6570 (s1 (concat
6571 (org-agenda-time-of-day-to-ampm-maybe s1)
6572 (if org-agenda-timegrid-use-ampm
6573 "........ "
6574 "......")))
6575 (t ""))
6576 extra (or (and (not habitp) extra) "")
6577 category (if (symbolp category) (symbol-name category) category)
6578 level (or level ""))
6579 (if (string-match org-bracket-link-regexp category)
6580 (progn
6581 (setq l (if (match-end 3)
6582 (- (match-end 3) (match-beginning 3))
6583 (- (match-end 1) (match-beginning 1))))
6584 (when (< l (or org-prefix-category-length 0))
6585 (setq category (copy-sequence category))
6586 (org-add-props category nil
6587 'extra-space (make-string
6588 (- org-prefix-category-length l 1) ?\ ))))
6589 (if (and org-prefix-category-max-length
6590 (>= (length category) org-prefix-category-max-length))
6591 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6592 ;; Evaluate the compiled format
6593 (setq rtn (concat (eval formatter) txt))
6595 ;; And finally add the text properties
6596 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6597 (org-add-props rtn nil
6598 'org-category category
6599 'tags (mapcar 'org-downcase-keep-props tags)
6600 'org-highest-priority org-highest-priority
6601 'org-lowest-priority org-lowest-priority
6602 'time-of-day time-of-day
6603 'duration duration
6604 'breadcrumbs breadcrumbs
6605 'txt txt
6606 'level level
6607 'time time
6608 'extra extra
6609 'format org-prefix-format-compiled
6610 'dotime dotime)))))
6612 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6613 "Remove tags string from TXT, and add a modified list of tags.
6614 The modified list may contain inherited tags, and tags matched by
6615 `org-agenda-hide-tags-regexp' will be removed."
6616 (when (or add-inherited hide-re)
6617 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6618 (setq txt (substring txt 0 (match-beginning 0))))
6619 (setq tags
6620 (delq nil
6621 (mapcar (lambda (tg)
6622 (if (or (and hide-re (string-match hide-re tg))
6623 (and (not add-inherited)
6624 (get-text-property 0 'inherited tg)))
6626 tg))
6627 tags)))
6628 (when tags
6629 (let ((have-i (get-text-property 0 'inherited (car tags)))
6631 (setq txt (concat txt " :"
6632 (mapconcat
6633 (lambda (x)
6634 (setq i (get-text-property 0 'inherited x))
6635 (if (and have-i (not i))
6636 (progn
6637 (setq have-i nil)
6638 (concat ":" x))
6640 tags ":")
6641 (if have-i "::" ":"))))))
6642 txt)
6644 (defun org-downcase-keep-props (s)
6645 (let ((props (text-properties-at 0 s)))
6646 (setq s (downcase s))
6647 (add-text-properties 0 (length s) props s)
6650 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6652 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6653 "Add a time-grid for agenda items which need it.
6655 LIST is the list of agenda items formatted by `org-agenda-list'.
6656 NDAYS is the span of the current agenda view.
6657 TODAYP is t when the current agenda view is on today."
6658 (catch 'exit
6659 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6660 ((and todayp (member 'today (car org-agenda-time-grid))))
6661 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6662 ((member 'weekly (car org-agenda-time-grid)))
6663 (t (throw 'exit list)))
6664 (let* ((have (delq nil (mapcar
6665 (lambda (x) (get-text-property 1 'time-of-day x))
6666 list)))
6667 (string (nth 1 org-agenda-time-grid))
6668 (gridtimes (nth 2 org-agenda-time-grid))
6669 (req (car org-agenda-time-grid))
6670 (remove (member 'remove-match req))
6671 new time)
6672 (if (and (member 'require-timed req) (not have))
6673 ;; don't show empty grid
6674 (throw 'exit list))
6675 (while (setq time (pop gridtimes))
6676 (unless (and remove (member time have))
6677 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6678 (push (org-agenda-format-item
6679 nil string nil "" nil
6680 (concat (substring time 0 -2) ":" (substring time -2)))
6681 new)
6682 (put-text-property
6683 2 (length (car new)) 'face 'org-time-grid (car new))))
6684 (when (and todayp org-agenda-show-current-time-in-grid)
6685 (push (org-agenda-format-item
6686 nil org-agenda-current-time-string nil "" nil
6687 (format-time-string "%H:%M "))
6688 new)
6689 (put-text-property
6690 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6692 (if (member 'time-up org-agenda-sorting-strategy-selected)
6693 (append new list)
6694 (append list new)))))
6696 (defun org-compile-prefix-format (key)
6697 "Compile the prefix format into a Lisp form that can be evaluated.
6698 The resulting form and associated variable bindings is returned
6699 and stored in the variable `org-prefix-format-compiled'."
6700 (setq org-prefix-has-time nil
6701 org-prefix-has-tag nil
6702 org-prefix-category-length nil
6703 org-prefix-has-effort nil
6704 org-prefix-has-breadcrumbs nil)
6705 (let ((s (cond
6706 ((stringp org-agenda-prefix-format)
6707 org-agenda-prefix-format)
6708 ((assq key org-agenda-prefix-format)
6709 (cdr (assq key org-agenda-prefix-format)))
6710 (t " %-12:c%?-12t% s")))
6711 (start 0)
6712 varform vars var e c f opt)
6713 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6714 s start)
6715 (setq var (or (cdr (assoc (match-string 4 s)
6716 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6717 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6718 'eval)
6719 c (or (match-string 3 s) "")
6720 opt (match-beginning 1)
6721 start (1+ (match-beginning 0)))
6722 (if (equal var 'time) (setq org-prefix-has-time t))
6723 (if (equal var 'tag) (setq org-prefix-has-tag t))
6724 (if (equal var 'effort) (setq org-prefix-has-effort t))
6725 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6726 (setq f (concat "%" (match-string 2 s) "s"))
6727 (when (equal var 'category)
6728 (setq org-prefix-category-length
6729 (floor (abs (string-to-number (match-string 2 s)))))
6730 (setq org-prefix-category-max-length
6731 (let ((x (match-string 2 s)))
6732 (save-match-data
6733 (if (string-match "\\.[0-9]+" x)
6734 (string-to-number (substring (match-string 0 x) 1)))))))
6735 (if (eq var 'eval)
6736 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6737 (if opt
6738 (setq varform
6739 `(if (or (equal "" ,var) (equal nil ,var))
6741 (format ,f (concat ,var ,c))))
6742 (setq varform
6743 `(format ,f (if (or (equal ,var "")
6744 (equal ,var nil)) ""
6745 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6746 (setq s (replace-match "%s" t nil s))
6747 (push varform vars))
6748 (setq vars (nreverse vars))
6749 (with-current-buffer (or org-agenda-buffer (current-buffer))
6750 (setq org-prefix-format-compiled
6751 (list
6752 `((org-prefix-has-time ,org-prefix-has-time)
6753 (org-prefix-has-tag ,org-prefix-has-tag)
6754 (org-prefix-category-length ,org-prefix-category-length)
6755 (org-prefix-has-effort ,org-prefix-has-effort)
6756 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6757 `(format ,s ,@vars))))))
6759 (defun org-set-sorting-strategy (key)
6760 (if (symbolp (car org-agenda-sorting-strategy))
6761 ;; the old format
6762 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6763 (setq org-agenda-sorting-strategy-selected
6764 (or (cdr (assq key org-agenda-sorting-strategy))
6765 (cdr (assq 'agenda org-agenda-sorting-strategy))
6766 '(time-up category-keep priority-down)))))
6768 (defun org-get-time-of-day (s &optional string mod24)
6769 "Check string S for a time of day.
6770 If found, return it as a military time number between 0 and 2400.
6771 If not found, return nil.
6772 The optional STRING argument forces conversion into a 5 character wide string
6773 HH:MM."
6774 (save-match-data
6775 (when
6776 (and
6777 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6778 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6779 (not (eq (get-text-property 1 'face s) 'org-link)))
6780 (let* ((h (string-to-number (match-string 1 s)))
6781 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6782 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6783 (am-p (equal ampm "am"))
6784 (h1 (cond ((not ampm) h)
6785 ((= h 12) (if am-p 0 12))
6786 (t (+ h (if am-p 0 12)))))
6787 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6788 (mod h1 24) h1))
6789 (t0 (+ (* 100 h2) m))
6790 (t1 (concat (if (>= h1 24) "+" " ")
6791 (if (and org-agenda-time-leading-zero
6792 (< t0 1000)) "0" "")
6793 (if (< t0 100) "0" "")
6794 (if (< t0 10) "0" "")
6795 (int-to-string t0))))
6796 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6798 (defvar org-agenda-before-sorting-filter-function nil
6799 "Function to be applied to agenda items prior to sorting.
6800 Prior to sorting also means just before they are inserted into the agenda.
6802 To aid sorting, you may revisit the original entries and add more text
6803 properties which will later be used by the sorting functions.
6805 The function should take a string argument, an agenda line.
6806 It has access to the text properties in that line, which contain among
6807 other things, the property `org-hd-marker' that points to the entry
6808 where the line comes from. Note that not all lines going into the agenda
6809 have this property, only most.
6811 The function should return the modified string. It is probably best
6812 to ONLY change text properties.
6814 You can also use this function as a filter, by returning nil for lines
6815 you don't want to have in the agenda at all. For this application, you
6816 could bind the variable in the options section of a custom command.")
6818 (defun org-agenda-finalize-entries (list &optional type)
6819 "Sort, limit and concatenate the LIST of agenda items.
6820 The optional argument TYPE tells the agenda type."
6821 (let ((max-effort (cond ((listp org-agenda-max-effort)
6822 (cdr (assoc type org-agenda-max-effort)))
6823 (t org-agenda-max-effort)))
6824 (max-todo (cond ((listp org-agenda-max-todos)
6825 (cdr (assoc type org-agenda-max-todos)))
6826 (t org-agenda-max-todos)))
6827 (max-tags (cond ((listp org-agenda-max-tags)
6828 (cdr (assoc type org-agenda-max-tags)))
6829 (t org-agenda-max-tags)))
6830 (max-entries (cond ((listp org-agenda-max-entries)
6831 (cdr (assoc type org-agenda-max-entries)))
6832 (t org-agenda-max-entries))))
6833 (when org-agenda-before-sorting-filter-function
6834 (setq list
6835 (delq nil
6836 (mapcar
6837 org-agenda-before-sorting-filter-function list))))
6838 (setq list (mapcar 'org-agenda-highlight-todo list)
6839 list (mapcar 'identity (sort list 'org-entries-lessp)))
6840 (when max-effort
6841 (setq list (org-agenda-limit-entries
6842 list 'effort-minutes max-effort
6843 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6844 32767 -1))))))
6845 (when max-todo
6846 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6847 (when max-tags
6848 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6849 (when max-entries
6850 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6851 (mapconcat 'identity list "\n")))
6853 (defun org-agenda-limit-entries (list prop limit &optional fn)
6854 "Limit the number of agenda entries."
6855 (let ((include (and limit (< limit 0))))
6856 (if limit
6857 (let ((fun (or fn (lambda (p) (if p 1))))
6858 (lim 0))
6859 (delq nil
6860 (mapcar
6861 (lambda (e)
6862 (let ((pval (funcall
6863 fun (get-text-property (1- (length e))
6864 prop e))))
6865 (if pval (setq lim (+ lim pval)))
6866 (cond ((and pval (<= lim (abs limit))) e)
6867 ((and include (not pval)) e))))
6868 list)))
6869 list)))
6871 (defun org-agenda-limit-interactively (remove)
6872 "In agenda, interactively limit entries to various maximums."
6873 (interactive "P")
6874 (if remove
6875 (progn (setq org-agenda-max-entries nil
6876 org-agenda-max-todos nil
6877 org-agenda-max-tags nil
6878 org-agenda-max-effort nil)
6879 (org-agenda-redo))
6880 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6881 (msg (cond ((= max ?E) "How many minutes? ")
6882 ((= max ?e) "How many entries? ")
6883 ((= max ?t) "How many TODO entries? ")
6884 ((= max ?T) "How many tagged entries? ")
6885 (t (user-error "Wrong input"))))
6886 (num (string-to-number (read-from-minibuffer msg))))
6887 (cond ((equal max ?e)
6888 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6889 ((equal max ?t)
6890 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6891 ((equal max ?T)
6892 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6893 ((equal max ?E)
6894 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6895 (org-agenda-fit-window-to-buffer))
6897 (defun org-agenda-highlight-todo (x)
6898 (let ((org-done-keywords org-done-keywords-for-agenda)
6899 (case-fold-search nil)
6901 (if (eq x 'line)
6902 (save-excursion
6903 (beginning-of-line 1)
6904 (setq re (org-get-at-bol 'org-todo-regexp))
6905 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6906 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6907 (add-text-properties (match-beginning 0) (match-end 1)
6908 (list 'face (org-get-todo-face 1)))
6909 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6910 (delete-region (match-beginning 1) (1- (match-end 0)))
6911 (goto-char (match-beginning 1))
6912 (insert (format org-agenda-todo-keyword-format s)))))
6913 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6914 (setq re (get-text-property 0 'org-todo-regexp x))
6915 (when (and re
6916 ;; Test `pl' because if there's no heading content,
6917 ;; there's no point matching to highlight. Note
6918 ;; that if we didn't test `pl' first, and there
6919 ;; happened to be no keyword from `org-todo-regexp'
6920 ;; on this heading line, then the `equal' comparison
6921 ;; afterwards would spuriously succeed in the case
6922 ;; where `pl' is nil -- causing an args-out-of-range
6923 ;; error when we try to add text properties to text
6924 ;; that isn't there.
6926 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6927 x pl) pl))
6928 (add-text-properties
6929 (or (match-end 1) (match-end 0)) (match-end 0)
6930 (list 'face (org-get-todo-face (match-string 2 x)))
6932 (when (match-end 1)
6933 (setq x (concat (substring x 0 (match-end 1))
6934 (format org-agenda-todo-keyword-format
6935 (match-string 2 x))
6936 (org-add-props " " (text-properties-at 0 x))
6937 (substring x (match-end 3)))))))
6938 x)))
6940 (defsubst org-cmp-values (a b property)
6941 "Compare the numeric value of text PROPERTY for string A and B."
6942 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6943 (pb (or (get-text-property (1- (length b)) property b) 0)))
6944 (cond ((> pa pb) +1)
6945 ((< pa pb) -1))))
6947 (defsubst org-cmp-effort (a b)
6948 "Compare the effort values of string A and B."
6949 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6950 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6951 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6952 (cond ((> ea eb) +1)
6953 ((< ea eb) -1))))
6955 (defsubst org-cmp-category (a b)
6956 "Compare the string values of categories of strings A and B."
6957 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6958 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6959 (cond ((string-lessp ca cb) -1)
6960 ((string-lessp cb ca) +1))))
6962 (defsubst org-cmp-todo-state (a b)
6963 "Compare the todo states of strings A and B."
6964 (let* ((ma (or (get-text-property 1 'org-marker a)
6965 (get-text-property 1 'org-hd-marker a)))
6966 (mb (or (get-text-property 1 'org-marker b)
6967 (get-text-property 1 'org-hd-marker b)))
6968 (fa (and ma (marker-buffer ma)))
6969 (fb (and mb (marker-buffer mb)))
6970 (todo-kwds
6971 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6972 (and fb (with-current-buffer fb org-todo-keywords-1))))
6973 (ta (or (get-text-property 1 'todo-state a) ""))
6974 (tb (or (get-text-property 1 'todo-state b) ""))
6975 (la (- (length (member ta todo-kwds))))
6976 (lb (- (length (member tb todo-kwds))))
6977 (donepa (member ta org-done-keywords-for-agenda))
6978 (donepb (member tb org-done-keywords-for-agenda)))
6979 (cond ((and donepa (not donepb)) -1)
6980 ((and (not donepa) donepb) +1)
6981 ((< la lb) -1)
6982 ((< lb la) +1))))
6984 (defsubst org-cmp-alpha (a b)
6985 "Compare the headlines, alphabetically."
6986 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6987 (plb (text-property-any 0 (length b) 'org-heading t b))
6988 (ta (and pla (substring a pla)))
6989 (tb (and plb (substring b plb))))
6990 (when pla
6991 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6992 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6993 (setq ta (substring ta (match-end 0))))
6994 (setq ta (downcase ta)))
6995 (when plb
6996 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6997 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6998 (setq tb (substring tb (match-end 0))))
6999 (setq tb (downcase tb)))
7000 (cond ((not ta) +1)
7001 ((not tb) -1)
7002 ((string-lessp ta tb) -1)
7003 ((string-lessp tb ta) +1))))
7005 (defsubst org-cmp-tag (a b)
7006 "Compare the string values of the first tags of A and B."
7007 (let ((ta (car (last (get-text-property 1 'tags a))))
7008 (tb (car (last (get-text-property 1 'tags b)))))
7009 (cond ((not ta) +1)
7010 ((not tb) -1)
7011 ((string-lessp ta tb) -1)
7012 ((string-lessp tb ta) +1))))
7014 (defsubst org-cmp-time (a b)
7015 "Compare the time-of-day values of strings A and B."
7016 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7017 (ta (or (get-text-property 1 'time-of-day a) def))
7018 (tb (or (get-text-property 1 'time-of-day b) def)))
7019 (cond ((< ta tb) -1)
7020 ((< tb ta) +1))))
7022 (defsubst org-cmp-ts (a b type)
7023 "Compare the timestamps values of entries A and B.
7024 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7025 \"timestamp_ia\", compare within each of these type. When TYPE
7026 is the empty string, compare all timestamps without respect of
7027 their type."
7028 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7029 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7030 (get-text-property 1 'ts-date a))
7031 def))
7032 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7033 (get-text-property 1 'ts-date b))
7034 def)))
7035 (cond ((< ta tb) -1)
7036 ((< tb ta) +1))))
7038 (defsubst org-cmp-habit-p (a b)
7039 "Compare the todo states of strings A and B."
7040 (let ((ha (get-text-property 1 'org-habit-p a))
7041 (hb (get-text-property 1 'org-habit-p b)))
7042 (cond ((and ha (not hb)) -1)
7043 ((and (not ha) hb) +1))))
7045 (defun org-entries-lessp (a b)
7046 "Predicate for sorting agenda entries."
7047 ;; The following variables will be used when the form is evaluated.
7048 ;; So even though the compiler complains, keep them.
7049 (let* ((ss org-agenda-sorting-strategy-selected)
7050 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7051 (org-cmp-ts a b "")))
7052 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7053 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7054 (org-cmp-ts a b "scheduled")))
7055 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7056 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7057 (org-cmp-ts a b "deadline")))
7058 (deadline-down (if deadline-up (- deadline-up) nil))
7059 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7060 (org-cmp-ts a b "timestamp_ia")))
7061 (tsia-down (if tsia-up (- tsia-up) nil))
7062 (ts-up (and (org-em 'ts-up 'ts-down ss)
7063 (org-cmp-ts a b "timestamp")))
7064 (ts-down (if ts-up (- ts-up) nil))
7065 (time-up (and (org-em 'time-up 'time-down ss)
7066 (org-cmp-time a b)))
7067 (time-down (if time-up (- time-up) nil))
7068 (stats-up (and (org-em 'stats-up 'stats-down ss)
7069 (org-cmp-values a b 'org-stats)))
7070 (stats-down (if stats-up (- stats-up) nil))
7071 (priority-up (and (org-em 'priority-up 'priority-down ss)
7072 (org-cmp-values a b 'priority)))
7073 (priority-down (if priority-up (- priority-up) nil))
7074 (effort-up (and (org-em 'effort-up 'effort-down ss)
7075 (org-cmp-effort a b)))
7076 (effort-down (if effort-up (- effort-up) nil))
7077 (category-up (and (or (org-em 'category-up 'category-down ss)
7078 (memq 'category-keep ss))
7079 (org-cmp-category a b)))
7080 (category-down (if category-up (- category-up) nil))
7081 (category-keep (if category-up +1 nil))
7082 (tag-up (and (org-em 'tag-up 'tag-down ss)
7083 (org-cmp-tag a b)))
7084 (tag-down (if tag-up (- tag-up) nil))
7085 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7086 (org-cmp-todo-state a b)))
7087 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7088 (habit-up (and (org-em 'habit-up 'habit-down ss)
7089 (org-cmp-habit-p a b)))
7090 (habit-down (if habit-up (- habit-up) nil))
7091 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7092 (org-cmp-alpha a b)))
7093 (alpha-down (if alpha-up (- alpha-up) nil))
7094 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7095 user-defined-up user-defined-down)
7096 (if (and need-user-cmp org-agenda-cmp-user-defined
7097 (functionp org-agenda-cmp-user-defined))
7098 (setq user-defined-up
7099 (funcall org-agenda-cmp-user-defined a b)
7100 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7101 (cdr (assoc
7102 (eval (cons 'or org-agenda-sorting-strategy-selected))
7103 '((-1 . t) (1 . nil) (nil . nil))))))
7105 ;;; Agenda restriction lock
7107 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7108 "Overlay to mark the headline to which agenda commands are restricted.")
7109 (overlay-put org-agenda-restriction-lock-overlay
7110 'face 'org-agenda-restriction-lock)
7111 (overlay-put org-agenda-restriction-lock-overlay
7112 'help-echo "Agendas are currently limited to this subtree.")
7113 (org-detach-overlay org-agenda-restriction-lock-overlay)
7115 ;;;###autoload
7116 (defun org-agenda-set-restriction-lock (&optional type)
7117 "Set restriction lock for agenda, to current subtree or file.
7118 Restriction will be the file if TYPE is `file', or if type is the
7119 universal prefix '(4), or if the cursor is before the first headline
7120 in the file. Otherwise, restriction will be to the current subtree."
7121 (interactive "P")
7122 (org-agenda-remove-restriction-lock 'noupdate)
7123 (and (equal type '(4)) (setq type 'file))
7124 (setq type (cond
7125 (type type)
7126 ((org-at-heading-p) 'subtree)
7127 ((condition-case nil (org-back-to-heading t) (error nil))
7128 'subtree)
7129 (t 'file)))
7130 (if (eq type 'subtree)
7131 (progn
7132 (setq org-agenda-restrict (current-buffer))
7133 (setq org-agenda-overriding-restriction 'subtree)
7134 (put 'org-agenda-files 'org-restrict
7135 (list (buffer-file-name (buffer-base-buffer))))
7136 (org-back-to-heading t)
7137 (move-overlay org-agenda-restriction-lock-overlay
7138 (point)
7139 (if org-agenda-restriction-lock-highlight-subtree
7140 (save-excursion (org-end-of-subtree t t) (point))
7141 (point-at-eol)))
7142 (move-marker org-agenda-restrict-begin (point))
7143 (move-marker org-agenda-restrict-end
7144 (save-excursion (org-end-of-subtree t t)))
7145 (message "Locking agenda restriction to subtree"))
7146 (put 'org-agenda-files 'org-restrict
7147 (list (buffer-file-name (buffer-base-buffer))))
7148 (setq org-agenda-restrict nil)
7149 (setq org-agenda-overriding-restriction 'file)
7150 (move-marker org-agenda-restrict-begin nil)
7151 (move-marker org-agenda-restrict-end nil)
7152 (message "Locking agenda restriction to file"))
7153 (setq current-prefix-arg nil)
7154 (org-agenda-maybe-redo))
7156 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7157 "Remove the agenda restriction lock."
7158 (interactive "P")
7159 (org-detach-overlay org-agenda-restriction-lock-overlay)
7160 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7161 (setq org-agenda-overriding-restriction nil)
7162 (setq org-agenda-restrict nil)
7163 (put 'org-agenda-files 'org-restrict nil)
7164 (move-marker org-agenda-restrict-begin nil)
7165 (move-marker org-agenda-restrict-end nil)
7166 (setq current-prefix-arg nil)
7167 (message "Agenda restriction lock removed")
7168 (or noupdate (org-agenda-maybe-redo)))
7170 (defun org-agenda-maybe-redo ()
7171 "If there is any window showing the agenda view, update it."
7172 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7173 org-agenda-buffer-name)
7175 (w0 (selected-window)))
7176 (when w
7177 (select-window w)
7178 (org-agenda-redo)
7179 (select-window w0)
7180 (if org-agenda-overriding-restriction
7181 (message "Agenda view shifted to new %s restriction"
7182 org-agenda-overriding-restriction)
7183 (message "Agenda restriction lock removed")))))
7185 ;;; Agenda commands
7187 (defun org-agenda-check-type (error &rest types)
7188 "Check if agenda buffer is of allowed type.
7189 If ERROR is non-nil, throw an error, otherwise just return nil.
7190 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7191 (if (not org-agenda-type)
7192 (error "No Org agenda currently displayed")
7193 (if (memq org-agenda-type types)
7195 (if error
7196 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7197 nil))))
7199 (defun org-agenda-Quit ()
7200 "Exit the agenda, killing the agenda buffer.
7201 Like `org-agenda-quit', but kill the buffer even when
7202 `org-agenda-sticky' is non-nil."
7203 (interactive)
7204 (org-agenda--quit))
7206 (defun org-agenda-quit ()
7207 "Exit the agenda.
7209 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7210 instead of killing it.
7212 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7213 the pre-agenda window configuration.
7215 When column view is active, exit column view instead of the
7216 agenda."
7217 (interactive)
7218 (org-agenda--quit org-agenda-sticky))
7220 (defun org-agenda--quit (&optional bury)
7221 (if org-agenda-columns-active
7222 (org-columns-quit)
7223 (let ((buf (current-buffer))
7224 (wconf org-agenda-pre-window-conf)
7225 (org-agenda-last-indirect-window
7226 (and (eq org-indirect-buffer-display 'other-window)
7227 org-agenda-last-indirect-buffer
7228 (get-buffer-window org-agenda-last-indirect-buffer))))
7229 (cond
7230 ((eq org-agenda-window-setup 'other-frame)
7231 (delete-frame))
7232 ((and org-agenda-restore-windows-after-quit
7233 wconf)
7234 ;; Maybe restore the pre-agenda window configuration. Reset
7235 ;; `org-agenda-pre-window-conf' before running
7236 ;; `set-window-configuration', which loses the current buffer.
7237 (setq org-agenda-pre-window-conf nil)
7238 (set-window-configuration wconf))
7240 (when org-agenda-last-indirect-window
7241 (delete-window org-agenda-last-indirect-window))
7242 (and (not (eq org-agenda-window-setup 'current-window))
7243 (not (one-window-p))
7244 (delete-window))))
7245 (if bury
7246 (bury-buffer buf)
7247 (kill-buffer buf)
7248 (setq org-agenda-archives-mode nil
7249 org-agenda-buffer nil)))))
7251 (defun org-agenda-exit ()
7252 "Exit the agenda, killing Org buffers loaded by the agenda.
7253 Like `org-agenda-Quit', but kill any buffers that were created by
7254 the agenda. Org buffers visited directly by the user will not be
7255 touched. Also, exit the agenda even if it is in column view."
7256 (interactive)
7257 (when org-agenda-columns-active
7258 (org-columns-quit))
7259 (org-release-buffers org-agenda-new-buffers)
7260 (setq org-agenda-new-buffers nil)
7261 (org-agenda-Quit))
7263 (defun org-agenda-kill-all-agenda-buffers ()
7264 "Kill all buffers in `org-agenda-mode'.
7265 This is used when toggling sticky agendas.
7266 You can also explicitly invoke it with `C-c a C-k'."
7267 (interactive)
7268 (let (blist)
7269 (dolist (buf (buffer-list))
7270 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7271 (push buf blist)))
7272 (mapc 'kill-buffer blist)))
7274 (defun org-agenda-execute (arg)
7275 "Execute another agenda command, keeping same window.
7276 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7277 in the agenda."
7278 (interactive "P")
7279 (let ((org-agenda-window-setup 'current-window))
7280 (org-agenda arg)))
7282 (defun org-agenda-redo (&optional all)
7283 "Rebuild possibly ALL agenda view(s) in the current buffer."
7284 (interactive "P")
7285 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7286 (cpa (unless (eq all t) current-prefix-arg))
7287 (org-agenda-doing-sticky-redo org-agenda-sticky)
7288 (org-agenda-sticky nil)
7289 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7290 org-agenda-buffer-name))
7291 (org-agenda-keep-modes t)
7292 (tag-filter org-agenda-tag-filter)
7293 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7294 (top-hl-filter org-agenda-top-headline-filter)
7295 (cat-filter org-agenda-category-filter)
7296 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7297 (re-filter org-agenda-regexp-filter)
7298 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7299 (effort-filter org-agenda-effort-filter)
7300 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7301 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7302 (cols org-agenda-columns-active)
7303 (line (org-current-line))
7304 (window-line (- line (org-current-line (window-start))))
7305 (lprops (get 'org-agenda-redo-command 'org-lprops))
7306 (redo-cmd (get-text-property p 'org-redo-cmd))
7307 (last-args (get-text-property p 'org-last-args))
7308 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7309 (org-agenda-overriding-cmd-arguments
7310 (unless (eq all t)
7311 (cond ((listp last-args)
7312 (cons (or cpa (car last-args)) (cdr last-args)))
7313 ((stringp last-args)
7314 last-args))))
7315 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7316 (put 'org-agenda-tag-filter :preset-filter nil)
7317 (put 'org-agenda-category-filter :preset-filter nil)
7318 (put 'org-agenda-regexp-filter :preset-filter nil)
7319 (put 'org-agenda-effort-filter :preset-filter nil)
7320 (and cols (org-columns-quit))
7321 (message "Rebuilding agenda buffer...")
7322 (if series-redo-cmd
7323 (eval series-redo-cmd)
7324 (org-let lprops redo-cmd))
7325 (setq org-agenda-undo-list nil
7326 org-agenda-pending-undo-list nil
7327 org-agenda-tag-filter tag-filter
7328 org-agenda-category-filter cat-filter
7329 org-agenda-regexp-filter re-filter
7330 org-agenda-effort-filter effort-filter
7331 org-agenda-top-headline-filter top-hl-filter)
7332 (message "Rebuilding agenda buffer...done")
7333 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7334 (put 'org-agenda-category-filter :preset-filter cat-preset)
7335 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7336 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7337 (let ((tag (or tag-filter tag-preset))
7338 (cat (or cat-filter cat-preset))
7339 (effort (or effort-filter effort-preset))
7340 (re (or re-filter re-preset)))
7341 (when tag (org-agenda-filter-apply tag 'tag t))
7342 (when cat (org-agenda-filter-apply cat 'category))
7343 (when effort (org-agenda-filter-apply effort 'effort))
7344 (when re (org-agenda-filter-apply re 'regexp)))
7345 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7346 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7347 (org-goto-line line)
7348 (recenter window-line)))
7350 (defvar org-global-tags-completion-table nil)
7351 (defvar org-agenda-filter-form nil)
7352 (defvar org-agenda-filtered-by-category nil)
7354 (defun org-agenda-filter-by-category (strip)
7355 "Keep only those lines in the agenda buffer that have a specific category.
7356 The category is that of the current line."
7357 (interactive "P")
7358 (if (and org-agenda-filtered-by-category
7359 org-agenda-category-filter)
7360 (org-agenda-filter-show-all-cat)
7361 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7362 (cond
7363 ((and cat strip)
7364 (org-agenda-filter-apply
7365 (push (concat "-" cat) org-agenda-category-filter) 'category))
7366 ((and cat)
7367 (org-agenda-filter-apply
7368 (setq org-agenda-category-filter
7369 (list (concat "+" cat))) 'category))
7370 (t (error "No category at point"))))))
7372 (defun org-find-top-headline (&optional pos)
7373 "Find the topmost parent headline and return it."
7374 (save-excursion
7375 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7376 (if pos (goto-char pos))
7377 ;; Skip up to the topmost parent
7378 (while (ignore-errors (outline-up-heading 1) t))
7379 (ignore-errors
7380 (nth 4 (org-heading-components))))))
7382 (defvar org-agenda-filtered-by-top-headline nil)
7383 (defun org-agenda-filter-by-top-headline (strip)
7384 "Keep only those lines that are descendants from the same top headline.
7385 The top headline is that of the current line."
7386 (interactive "P")
7387 (if org-agenda-filtered-by-top-headline
7388 (progn
7389 (setq org-agenda-filtered-by-top-headline nil
7390 org-agenda-top-headline-filter nil)
7391 (org-agenda-filter-show-all-top-filter))
7392 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7393 (if toph (org-agenda-filter-top-headline-apply toph strip)
7394 (error "No top-level headline at point")))))
7396 (defvar org-agenda-regexp-filter nil)
7397 (defun org-agenda-filter-by-regexp (strip)
7398 "Filter agenda entries by a regular expression.
7399 Regexp filters are cumulative.
7400 With no prefix argument, keep entries matching the regexp.
7401 With one prefix argument, filter out entries matching the regexp.
7402 With two prefix arguments, remove the regexp filters."
7403 (interactive "P")
7404 (if (not (equal strip '(16)))
7405 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7406 (read-from-minibuffer
7407 (if (equal strip '(4))
7408 "Filter out entries matching regexp: "
7409 "Narrow to entries matching regexp: ")))))
7410 (push flt org-agenda-regexp-filter)
7411 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7412 (org-agenda-filter-show-all-re)
7413 (message "Regexp filter removed")))
7415 (defvar org-agenda-effort-filter nil)
7416 (defun org-agenda-filter-by-effort (strip)
7417 "Filter agenda entries by effort.
7418 With no prefix argument, keep entries matching the effort condition.
7419 With one prefix argument, filter out entries matching the condition.
7420 With two prefix arguments, remove the effort filters."
7421 (interactive "P")
7422 (cond ((member strip '(nil 4))
7423 (let ((efforts (org-split-string
7424 (or (cdr (assoc (concat org-effort-property "_ALL")
7425 org-global-properties))
7426 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7427 "")))
7428 (eff -1)
7429 effort-prompt op)
7430 (while (not (member op '(?< ?> ?=)))
7431 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7432 (loop for i from 0 to 9 do
7433 (setq effort-prompt
7434 (concat
7435 effort-prompt " ["
7436 (if (= i 9) "0" (int-to-string (1+ i)))
7437 "]" (nth i efforts))))
7438 (message "Effort %s%s" (char-to-string op) effort-prompt)
7439 (while (or (< eff 0) (> eff 9))
7440 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7441 (setq org-agenda-effort-filter
7442 (list (concat (if strip "-" "+")
7443 (char-to-string op) (nth (1- eff) efforts))))
7444 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7445 (t (org-agenda-filter-show-all-effort)
7446 (message "Effort filter removed"))))
7448 (defun org-agenda-filter-remove-all ()
7449 "Remove all filters from the current agenda buffer."
7450 (interactive)
7451 (when org-agenda-tag-filter
7452 (org-agenda-filter-show-all-tag))
7453 (when org-agenda-category-filter
7454 (org-agenda-filter-show-all-cat))
7455 (when org-agenda-regexp-filter
7456 (org-agenda-filter-show-all-re))
7457 (when org-agenda-top-headline-filter
7458 (org-agenda-filter-show-all-top-filter))
7459 (when org-agenda-effort-filter
7460 (org-agenda-filter-show-all-effort))
7461 (org-agenda-finalize))
7463 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7464 "Keep only those lines in the agenda buffer that have a specific tag.
7465 The tag is selected with its fast selection letter, as
7466 configured. With a single \\[universal-argument] prefix ARG,
7467 exclude the agenda search. With a double \\[universal-argument]
7468 prefix ARG, filter the literal tag. I.e. don't filter on all its
7469 group members.
7471 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7472 used to exclude the search - the interactive user can also press `-' or `+'
7473 to switch between filtering and excluding."
7474 (interactive "P")
7475 (let* ((alist org-tag-alist-for-agenda)
7476 (tag-chars (mapconcat
7477 (lambda (x) (if (and (not (symbolp (car x)))
7478 (cdr x))
7479 (char-to-string (cdr x))
7480 ""))
7481 org-tag-alist-for-agenda ""))
7482 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7483 (string-to-list tag-chars)))
7484 (exclude (or exclude (equal arg '(4))))
7485 (expand (not (equal arg '(16))))
7486 (inhibit-read-only t)
7487 (current org-agenda-tag-filter)
7488 a n tag)
7489 (unless char
7490 (while (not (memq char valid-char-list))
7491 (message
7492 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7493 (if exclude "Exclude" "Filter") tag-chars
7494 (if org-agenda-auto-exclude-function "[RET], " "")
7495 (if expand "" ", no grouptag expand"))
7496 (setq char (read-char-exclusive))
7497 ;; Excluding or filtering down
7498 (cond ((eq char ?-) (setq exclude t))
7499 ((eq char ?+) (setq exclude nil)))))
7500 (when (eq char ?\t)
7501 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7502 (org-set-local 'org-global-tags-completion-table
7503 (org-global-tags-completion-table)))
7504 (let ((completion-ignore-case t))
7505 (setq tag (org-icompleting-read
7506 "Tag: " org-global-tags-completion-table))))
7507 (cond
7508 ((eq char ?\r)
7509 (org-agenda-filter-show-all-tag)
7510 (when org-agenda-auto-exclude-function
7511 (setq org-agenda-tag-filter nil)
7512 (dolist (tag (org-agenda-get-represented-tags))
7513 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7514 (if modifier
7515 (push modifier org-agenda-tag-filter))))
7516 (if (not (null org-agenda-tag-filter))
7517 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7518 ((eq char ?/)
7519 (org-agenda-filter-show-all-tag)
7520 (when (get 'org-agenda-tag-filter :preset-filter)
7521 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7522 ((eq char ?.)
7523 (setq org-agenda-tag-filter
7524 (mapcar (lambda(tag) (concat "+" tag))
7525 (org-get-at-bol 'tags)))
7526 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7527 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7528 ((or (eq char ?\s)
7529 (setq a (rassoc char alist))
7530 (and tag (setq a (cons tag nil))))
7531 (org-agenda-filter-show-all-tag)
7532 (setq tag (car a))
7533 (setq org-agenda-tag-filter
7534 (cons (concat (if exclude "-" "+") tag)
7535 current))
7536 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7537 (t (error "Invalid tag selection character %c" char)))))
7539 (defun org-agenda-get-represented-tags ()
7540 "Get a list of all tags currently represented in the agenda."
7541 (let (p tags)
7542 (save-excursion
7543 (goto-char (point-min))
7544 (while (setq p (next-single-property-change (point) 'tags))
7545 (goto-char p)
7546 (mapc (lambda (x) (add-to-list 'tags x))
7547 (get-text-property (point) 'tags))))
7548 tags))
7550 (defun org-agenda-filter-by-tag-refine (arg &optional char)
7551 "Refine the current filter. See `org-agenda-filter-by-tag'."
7552 (interactive "P")
7553 (org-agenda-filter-by-tag arg char 'refine))
7555 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7556 "Create the form that tests a line for agenda filter. Optional
7557 argument EXPAND can be used for the TYPE tag and will expand the
7558 tags in the FILTER if any of the tags in FILTER are grouptags."
7559 (let (f f1)
7560 (cond
7561 ;; Tag filter
7562 ((eq type 'tag)
7563 (setq filter
7564 (delete-dups
7565 (append (get 'org-agenda-tag-filter :preset-filter)
7566 filter)))
7567 (dolist (x filter)
7568 (let ((op (string-to-char x)))
7569 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7570 (setq x (list x)))
7571 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7572 (push f1 f))))
7573 ;; Category filter
7574 ((eq type 'category)
7575 (setq filter
7576 (delete-dups
7577 (append (get 'org-agenda-category-filter :preset-filter)
7578 filter)))
7579 (dolist (x filter)
7580 (if (equal "-" (substring x 0 1))
7581 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7582 (setq f1 (list 'equal (substring x 1) 'cat)))
7583 (push f1 f)))
7584 ;; Regexp filter
7585 ((eq type 'regexp)
7586 (setq filter
7587 (delete-dups
7588 (append (get 'org-agenda-regexp-filter :preset-filter)
7589 filter)))
7590 (dolist (x filter)
7591 (if (equal "-" (substring x 0 1))
7592 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7593 (setq f1 (list 'string-match (substring x 1) 'txt)))
7594 (push f1 f)))
7595 ;; Effort filter
7596 ((eq type 'effort)
7597 (setq filter
7598 (delete-dups
7599 (append (get 'org-agenda-effort-filter :preset-filter)
7600 filter)))
7601 (dolist (x filter)
7602 (push (org-agenda-filter-effort-form x) f))))
7603 (cons 'and (nreverse f))))
7605 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7606 "Create the form that tests a line for agenda filter for
7607 tag-expressions. Return a match-expression given TAGS. OP is an
7608 operator of type CHAR that allows the function to set the right
7609 switches in the returned form."
7610 (let (f f1) ;f = return expression. f1 = working-area
7611 (dolist (x tags)
7612 (let* ((tag (substring x 1))
7613 (isregexp (and (equal "{" (substring tag 0 1))
7614 (equal "}" (substring tag -1))))
7615 regexp)
7616 (cond
7617 (isregexp
7618 (setq regexp (substring tag 1 -1))
7619 (setq f1 (list 'org-match-any-p regexp 'tags)))
7621 (setq f1 (list 'member (downcase tag) 'tags))))
7622 (when (eq op ?-)
7623 (setq f1 (list 'not f1))))
7624 (push f1 f))
7625 ;; Any of the expressions can match if op = +
7626 ;; all must match if the operator is -.
7627 (if (eq op ?-)
7628 (cons 'and f)
7629 (cons 'or f))))
7631 (defun org-agenda-filter-effort-form (e)
7632 "Return the form to compare the effort of the current line with what E says.
7633 E looks like \"+<2:25\"."
7634 (let (op)
7635 (setq e (substring e 1))
7636 (setq op (string-to-char e) e (substring e 1))
7637 (setq op (cond ((equal op ?<) '<=)
7638 ((equal op ?>) '>=)
7639 ((equal op ??) op)
7640 (t '=)))
7641 (list 'org-agenda-compare-effort (list 'quote op)
7642 (org-duration-string-to-minutes e))))
7644 (defun org-agenda-compare-effort (op value)
7645 "Compare the effort of the current line with VALUE, using OP.
7646 If the line does not have an effort defined, return nil."
7647 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7648 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7649 value)))
7651 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7652 "Expand group tags in FILTER for the agenda.
7653 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7654 (if org-group-tags
7655 (let ((case-fold-search t) rtn)
7656 (mapc
7657 (lambda (f)
7658 (let (f0 dir)
7659 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7660 (setq dir (match-string 1 f) f0 (match-string 2 f))
7661 (setq dir (if no-operator "" "+") f0 f))
7662 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7663 (org-tags-expand f0 t t))
7664 rtn))))
7665 filter)
7666 (reverse rtn))
7667 filter))
7669 (defun org-agenda-filter-apply (filter type &optional expand)
7670 "Set FILTER as the new agenda filter and apply it. Optional
7671 argument EXPAND can be used for the TYPE tag and will expand the
7672 tags in the FILTER if any of the tags in FILTER are grouptags."
7673 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7674 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7675 (let (tags cat txt)
7676 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7677 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7678 ;; category is used as the filter:
7679 (setq org-agenda-filtered-by-category
7680 (and (eq type 'category)
7681 (not (equal (substring (car filter) 0 1) "-"))))
7682 (org-agenda-set-mode-name)
7683 (save-excursion
7684 (goto-char (point-min))
7685 (while (not (eobp))
7686 (if (org-get-at-bol 'org-marker)
7687 (progn
7688 (setq tags (org-get-at-bol 'tags)
7689 cat (org-get-at-eol 'org-category 1)
7690 txt (org-get-at-eol 'txt 1))
7691 (if (not (eval org-agenda-filter-form))
7692 (org-agenda-filter-hide-line type))
7693 (beginning-of-line 2))
7694 (beginning-of-line 2))))
7695 (if (get-char-property (point) 'invisible)
7696 (ignore-errors (org-agenda-previous-line)))))
7698 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7699 "Filter by top headline HL."
7700 (org-agenda-set-mode-name)
7701 (save-excursion
7702 (goto-char (point-min))
7703 (while (not (eobp))
7704 (let* ((pos (org-get-at-bol 'org-hd-marker))
7705 (tophl (and pos (org-find-top-headline pos))))
7706 (if (and tophl (funcall (if negative 'identity 'not)
7707 (string= hl tophl)))
7708 (org-agenda-filter-hide-line 'top-headline)))
7709 (beginning-of-line 2)))
7710 (if (get-char-property (point) 'invisible)
7711 (org-agenda-previous-line))
7712 (setq org-agenda-top-headline-filter hl
7713 org-agenda-filtered-by-top-headline t))
7715 (defun org-agenda-filter-hide-line (type)
7716 "Hide lines with TYPE in the agenda buffer."
7717 (let* ((b (max (point-min) (1- (point-at-bol))))
7718 (e (point-at-eol)))
7719 (let ((inhibit-read-only t))
7720 (add-text-properties
7721 b e `(invisible org-filtered org-filter-type ,type)))))
7723 (defun org-agenda-remove-filter (type)
7724 (interactive)
7725 "Remove filter of type TYPE from the agenda buffer."
7726 (save-excursion
7727 (goto-char (point-min))
7728 (let ((inhibit-read-only t) pos)
7729 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7730 (goto-char pos)
7731 (remove-text-properties
7732 (point) (next-single-property-change (point) 'org-filter-type)
7733 `(invisible org-filtered org-filter-type ,type))))
7734 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7735 (setq org-agenda-filter-form nil)
7736 (org-agenda-set-mode-name)
7737 (org-agenda-finalize)))
7739 (defun org-agenda-filter-show-all-tag nil
7740 (org-agenda-remove-filter 'tag))
7741 (defun org-agenda-filter-show-all-re nil
7742 (org-agenda-remove-filter 'regexp))
7743 (defun org-agenda-filter-show-all-effort nil
7744 (org-agenda-remove-filter 'effort))
7745 (defun org-agenda-filter-show-all-cat nil
7746 (org-agenda-remove-filter 'category))
7747 (defun org-agenda-filter-show-all-top-filter nil
7748 (org-agenda-remove-filter 'top-headline))
7750 (defun org-agenda-manipulate-query-add ()
7751 "Manipulate the query by adding a search term with positive selection.
7752 Positive selection means the term must be matched for selection of an entry."
7753 (interactive)
7754 (org-agenda-manipulate-query ?\[))
7755 (defun org-agenda-manipulate-query-subtract ()
7756 "Manipulate the query by adding a search term with negative selection.
7757 Negative selection means term must not be matched for selection of an entry."
7758 (interactive)
7759 (org-agenda-manipulate-query ?\]))
7760 (defun org-agenda-manipulate-query-add-re ()
7761 "Manipulate the query by adding a search regexp with positive selection.
7762 Positive selection means the regexp must match for selection of an entry."
7763 (interactive)
7764 (org-agenda-manipulate-query ?\{))
7765 (defun org-agenda-manipulate-query-subtract-re ()
7766 "Manipulate the query by adding a search regexp with negative selection.
7767 Negative selection means regexp must not match for selection of an entry."
7768 (interactive)
7769 (org-agenda-manipulate-query ?\}))
7770 (defun org-agenda-manipulate-query (char)
7771 (cond
7772 ((memq org-agenda-type '(timeline agenda))
7773 (let ((org-agenda-include-inactive-timestamps t))
7774 (org-agenda-redo))
7775 (message "Display now includes inactive timestamps as well"))
7776 ((eq org-agenda-type 'search)
7777 (org-add-to-string
7778 'org-agenda-query-string
7779 (if org-agenda-last-search-view-search-was-boolean
7780 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7781 (?\{ . " +{}") (?\} . " -{}"))))
7782 " "))
7783 (setq org-agenda-redo-command
7784 (list 'org-search-view
7785 (car (get-text-property (min (1- (point-max)) (point))
7786 'org-last-args))
7787 org-agenda-query-string
7788 (+ (length org-agenda-query-string)
7789 (if (member char '(?\{ ?\})) 0 1))))
7790 (set-register org-agenda-query-register org-agenda-query-string)
7791 (let ((org-agenda-overriding-arguments
7792 (cdr org-agenda-redo-command)))
7793 (org-agenda-redo)))
7794 (t (error "Cannot manipulate query for %s-type agenda buffers"
7795 org-agenda-type))))
7797 (defun org-add-to-string (var string)
7798 (set var (concat (symbol-value var) string)))
7800 (defun org-agenda-goto-date (span)
7801 "Jump to DATE in agenda."
7802 (interactive "P")
7803 (let* ((org-read-date-prefer-future
7804 (eval org-agenda-jump-prefer-future))
7805 (date (org-read-date))
7806 (day (time-to-days (org-time-string-to-time date)))
7807 (org-agenda-sticky-orig org-agenda-sticky)
7808 (org-agenda-buffer-tmp-name (buffer-name))
7809 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7810 (0-arg (or current-prefix-arg (car args)))
7811 (2-arg (nth 2 args))
7812 (with-hour-p (nth 4 org-agenda-redo-command))
7813 (newcmd (list 'org-agenda-list 0-arg date
7814 (org-agenda-span-to-ndays
7815 2-arg (org-time-string-to-absolute date))
7816 with-hour-p))
7817 (newargs (cdr newcmd))
7818 (inhibit-read-only t)
7819 org-agenda-sticky)
7820 (if (not (org-agenda-check-type t 'agenda))
7821 (error "Not available in non-agenda views")
7822 (add-text-properties (point-min) (point-max)
7823 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7824 (org-agenda-redo)
7825 (goto-char (point-min))
7826 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7827 (save-excursion (move-beginning-of-line 2) (eobp))))
7828 (move-beginning-of-line 2))
7829 (setq org-agenda-sticky org-agenda-sticky-orig
7830 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7832 (defun org-agenda-goto-today ()
7833 "Go to today."
7834 (interactive)
7835 (org-agenda-check-type t 'timeline 'agenda)
7836 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7837 (curspan (nth 2 args))
7838 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7839 (cond
7840 (tdpos (goto-char tdpos))
7841 ((eq org-agenda-type 'agenda)
7842 (let* ((sd (org-agenda-compute-starting-span
7843 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7844 (org-agenda-overriding-arguments args))
7845 (setf (nth 1 org-agenda-overriding-arguments) sd)
7846 (org-agenda-redo)
7847 (org-agenda-find-same-or-today-or-agenda)))
7848 (t (error "Cannot find today")))))
7850 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7851 (goto-char
7852 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7853 (text-property-any (point-min) (point-max) 'org-today t)
7854 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7855 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7856 (org-agenda-backward-block))
7857 (point-min))))
7859 (defun org-agenda-backward-block ()
7860 "Move backward by one agenda block."
7861 (interactive)
7862 (org-agenda-forward-block 'backward))
7864 (defun org-agenda-forward-block (&optional backward)
7865 "Move forward by one agenda block.
7866 When optional argument BACKWARD is set, go backward"
7867 (interactive)
7868 (cond ((not (derived-mode-p 'org-agenda-mode))
7869 (user-error
7870 "Cannot execute this command outside of org-agenda-mode buffers"))
7871 ((looking-at (if backward "\\`" "\\'"))
7872 (message "Already at the %s block" (if backward "first" "last")))
7873 (t (let ((pos (prog1 (point)
7874 (ignore-errors (if backward (backward-char 1)
7875 (move-end-of-line 1)))))
7876 (f (if backward
7877 'previous-single-property-change
7878 'next-single-property-change))
7879 moved dest)
7880 (while (and (setq dest (funcall
7881 f (point) 'org-agenda-structural-header))
7882 (not (get-text-property
7883 (point) 'org-agenda-structural-header)))
7884 (setq moved t)
7885 (goto-char dest))
7886 (if moved (move-beginning-of-line 1)
7887 (goto-char (if backward (point-min) (point-max)))
7888 (move-beginning-of-line 1)
7889 (message "No %s block" (if backward "previous" "further")))))))
7891 (defun org-agenda-later (arg)
7892 "Go forward in time by the current span.
7893 With prefix ARG, go forward that many times the current span."
7894 (interactive "p")
7895 (org-agenda-check-type t 'agenda)
7896 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7897 (span (or (nth 2 args) org-agenda-current-span))
7898 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7899 (greg (calendar-gregorian-from-absolute sd))
7900 (cnt (org-get-at-bol 'org-day-cnt))
7901 greg2)
7902 (cond
7903 ((numberp span)
7904 (setq sd (+ (* span arg) sd)))
7905 ((eq span 'day)
7906 (setq sd (+ arg sd)))
7907 ((eq span 'week)
7908 (setq sd (+ (* 7 arg) sd)))
7909 ((eq span 'fortnight)
7910 (setq sd (+ (* 14 arg) sd)))
7911 ((eq span 'month)
7912 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7913 sd (calendar-absolute-from-gregorian greg2))
7914 (setcar greg2 (1+ (car greg2))))
7915 ((eq span 'year)
7916 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7917 sd (calendar-absolute-from-gregorian greg2))
7918 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7920 (setq sd (+ (* span arg) sd))))
7921 (let ((org-agenda-overriding-cmd
7922 ;; `cmd' may have been set by `org-agenda-run-series' which
7923 ;; uses `org-agenda-overriding-cmd' to decide whether
7924 ;; overriding is allowed for `cmd'
7925 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7926 (org-agenda-overriding-arguments
7927 (list (car args) sd span)))
7928 (org-agenda-redo)
7929 (org-agenda-find-same-or-today-or-agenda cnt))))
7931 (defun org-agenda-earlier (arg)
7932 "Go backward in time by the current span.
7933 With prefix ARG, go backward that many times the current span."
7934 (interactive "p")
7935 (org-agenda-later (- arg)))
7937 (defun org-agenda-view-mode-dispatch ()
7938 "Call one of the view mode commands."
7939 (interactive)
7940 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7941 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7942 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7943 (let ((a (read-char-exclusive)))
7944 (case a
7945 (?\ (call-interactively 'org-agenda-reset-view))
7946 (?d (call-interactively 'org-agenda-day-view))
7947 (?w (call-interactively 'org-agenda-week-view))
7948 (?t (call-interactively 'org-agenda-fortnight-view))
7949 (?m (call-interactively 'org-agenda-month-view))
7950 (?y (call-interactively 'org-agenda-year-view))
7951 (?l (call-interactively 'org-agenda-log-mode))
7952 (?L (org-agenda-log-mode '(4)))
7953 (?c (org-agenda-log-mode 'clockcheck))
7954 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7955 (?a (call-interactively 'org-agenda-archives-mode))
7956 (?A (org-agenda-archives-mode 'files))
7957 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7958 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7959 (?G (call-interactively 'org-agenda-toggle-time-grid))
7960 (?D (call-interactively 'org-agenda-toggle-diary))
7961 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7962 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7963 (org-agenda-check-type t 'timeline 'agenda)
7964 (org-agenda-redo))
7965 (message "Display now includes inactive timestamps as well"))
7966 (?q (message "Abort"))
7967 (otherwise (error "Invalid key" )))))
7969 (defun org-agenda-reset-view ()
7970 "Switch to default view for agenda."
7971 (interactive)
7972 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7973 (defun org-agenda-day-view (&optional day-of-month)
7974 "Switch to daily view for agenda.
7975 With argument DAY-OF-MONTH, switch to that day of the month."
7976 (interactive "P")
7977 (org-agenda-change-time-span 'day day-of-month))
7978 (defun org-agenda-week-view (&optional iso-week)
7979 "Switch to daily view for agenda.
7980 With argument ISO-WEEK, switch to the corresponding ISO week.
7981 If ISO-WEEK has more then 2 digits, only the last two encode the
7982 week. Any digits before this encode a year. So 200712 means
7983 week 12 of year 2007. Years in the range 1938-2037 can also be
7984 written as 2-digit years."
7985 (interactive "P")
7986 (org-agenda-change-time-span 'week iso-week))
7987 (defun org-agenda-fortnight-view (&optional iso-week)
7988 "Switch to daily view for agenda.
7989 With argument ISO-WEEK, switch to the corresponding ISO week.
7990 If ISO-WEEK has more then 2 digits, only the last two encode the
7991 week. Any digits before this encode a year. So 200712 means
7992 week 12 of year 2007. Years in the range 1938-2037 can also be
7993 written as 2-digit years."
7994 (interactive "P")
7995 (org-agenda-change-time-span 'fortnight iso-week))
7996 (defun org-agenda-month-view (&optional month)
7997 "Switch to monthly view for agenda.
7998 With argument MONTH, switch to that month."
7999 (interactive "P")
8000 (org-agenda-change-time-span 'month month))
8001 (defun org-agenda-year-view (&optional year)
8002 "Switch to yearly view for agenda.
8003 With argument YEAR, switch to that year.
8004 If MONTH has more then 2 digits, only the last two encode the
8005 month. Any digits before this encode a year. So 200712 means
8006 December year 2007. Years in the range 1938-2037 can also be
8007 written as 2-digit years."
8008 (interactive "P")
8009 (when year
8010 (setq year (org-small-year-to-year year)))
8011 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8012 (org-agenda-change-time-span 'year year)
8013 (error "Abort")))
8015 (defun org-agenda-change-time-span (span &optional n)
8016 "Change the agenda view to SPAN.
8017 SPAN may be `day', `week', `fortnight', `month', `year'."
8018 (org-agenda-check-type t 'agenda)
8019 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8020 (curspan (nth 2 args)))
8021 (if (and (not n) (equal curspan span))
8022 (error "Viewing span is already \"%s\"" span))
8023 (let* ((sd (or (org-get-at-bol 'day)
8024 (nth 1 args)
8025 org-starting-day))
8026 (sd (org-agenda-compute-starting-span sd span n))
8027 (org-agenda-overriding-cmd
8028 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8029 (org-agenda-overriding-arguments
8030 (list (car args) sd span)))
8031 (org-agenda-redo)
8032 (org-agenda-find-same-or-today-or-agenda))
8033 (org-agenda-set-mode-name)
8034 (message "Switched to %s view" span)))
8036 (defun org-agenda-compute-starting-span (sd span &optional n)
8037 "Compute starting date for agenda.
8038 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8039 is a cons cell with the starting date and the number of days,
8040 so that the date SD will be in that range."
8041 (let* ((greg (calendar-gregorian-from-absolute sd))
8042 (dg (nth 1 greg))
8043 (mg (car greg))
8044 (yg (nth 2 greg)))
8045 (cond
8046 ((eq span 'day)
8047 (when n
8048 (setq sd (+ (calendar-absolute-from-gregorian
8049 (list mg 1 yg))
8050 n -1))))
8051 ((or (eq span 'week) (eq span 'fortnight))
8052 (let* ((nt (calendar-day-of-week
8053 (calendar-gregorian-from-absolute sd)))
8054 (d (if org-agenda-start-on-weekday
8055 (- nt org-agenda-start-on-weekday)
8058 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8059 (when n
8060 (require 'cal-iso)
8061 (when (> n 99)
8062 (setq y1 (org-small-year-to-year (/ n 100))
8063 n (mod n 100)))
8064 (setq sd
8065 (calendar-iso-to-absolute
8066 (list n 1
8067 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8068 ((eq span 'month)
8069 (let (y1)
8070 (when (and n (> n 99))
8071 (setq y1 (org-small-year-to-year (/ n 100))
8072 n (mod n 100)))
8073 (setq sd (calendar-absolute-from-gregorian
8074 (list (or n mg) 1 (or y1 yg))))))
8075 ((eq span 'year)
8076 (setq sd (calendar-absolute-from-gregorian
8077 (list 1 1 (or n yg))))))
8078 sd))
8080 (defun org-agenda-next-date-line (&optional arg)
8081 "Jump to the next line indicating a date in agenda buffer."
8082 (interactive "p")
8083 (org-agenda-check-type t 'agenda 'timeline)
8084 (beginning-of-line 1)
8085 ;; This does not work if user makes date format that starts with a blank
8086 (if (looking-at "^\\S-") (forward-char 1))
8087 (if (not (re-search-forward "^\\S-" nil t arg))
8088 (progn
8089 (backward-char 1)
8090 (error "No next date after this line in this buffer")))
8091 (goto-char (match-beginning 0)))
8093 (defun org-agenda-previous-date-line (&optional arg)
8094 "Jump to the previous line indicating a date in agenda buffer."
8095 (interactive "p")
8096 (org-agenda-check-type t 'agenda 'timeline)
8097 (beginning-of-line 1)
8098 (if (not (re-search-backward "^\\S-" nil t arg))
8099 (error "No previous date before this line in this buffer")))
8101 ;; Initialize the highlight
8102 (defvar org-hl (make-overlay 1 1))
8103 (overlay-put org-hl 'face 'highlight)
8105 (defun org-highlight (begin end &optional buffer)
8106 "Highlight a region with overlay."
8107 (move-overlay org-hl begin end (or buffer (current-buffer))))
8109 (defun org-unhighlight ()
8110 "Detach overlay INDEX."
8111 (org-detach-overlay org-hl))
8113 (defun org-unhighlight-once ()
8114 "Remove the highlight from its position, and this function from the hook."
8115 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8116 (org-unhighlight))
8118 (defvar org-agenda-pre-follow-window-conf nil)
8119 (defun org-agenda-follow-mode ()
8120 "Toggle follow mode in an agenda buffer."
8121 (interactive)
8122 (unless org-agenda-follow-mode
8123 (setq org-agenda-pre-follow-window-conf
8124 (current-window-configuration)))
8125 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8126 (unless org-agenda-follow-mode
8127 (set-window-configuration org-agenda-pre-follow-window-conf))
8128 (org-agenda-set-mode-name)
8129 (org-agenda-do-context-action)
8130 (message "Follow mode is %s"
8131 (if org-agenda-follow-mode "on" "off")))
8133 (defun org-agenda-entry-text-mode (&optional arg)
8134 "Toggle entry text mode in an agenda buffer."
8135 (interactive "P")
8136 (if (or org-agenda-tag-filter
8137 org-agenda-category-filter
8138 org-agenda-regexp-filter
8139 org-agenda-top-headline-filter)
8140 (user-error "Can't show entry text in filtered views")
8141 (setq org-agenda-entry-text-mode (or (integerp arg)
8142 (not org-agenda-entry-text-mode)))
8143 (org-agenda-entry-text-hide)
8144 (and org-agenda-entry-text-mode
8145 (let ((org-agenda-entry-text-maxlines
8146 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8147 (org-agenda-entry-text-show)))
8148 (org-agenda-set-mode-name)
8149 (message "Entry text mode is %s%s"
8150 (if org-agenda-entry-text-mode "on" "off")
8151 (if (not org-agenda-entry-text-mode) ""
8152 (format " (maximum number of lines is %d)"
8153 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8155 (defun org-agenda-clockreport-mode ()
8156 "Toggle clocktable mode in an agenda buffer."
8157 (interactive)
8158 (org-agenda-check-type t 'agenda)
8159 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8160 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8161 (org-agenda-set-mode-name)
8162 (org-agenda-redo)
8163 (message "Clocktable mode is %s"
8164 (if org-agenda-clockreport-mode "on" "off")))
8166 (defun org-agenda-log-mode (&optional special)
8167 "Toggle log mode in an agenda buffer.
8168 With argument SPECIAL, show all possible log items, not only the ones
8169 configured in `org-agenda-log-mode-items'.
8170 With a double `C-u' prefix arg, show *only* log items, nothing else."
8171 (interactive "P")
8172 (org-agenda-check-type t 'agenda 'timeline)
8173 (setq org-agenda-show-log
8174 (cond
8175 ((equal special '(16)) 'only)
8176 ((eq special 'clockcheck)
8177 (if (eq org-agenda-show-log 'clockcheck)
8178 nil 'clockcheck))
8179 (special '(closed clock state))
8180 (t (not org-agenda-show-log))))
8181 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8182 (org-agenda-set-mode-name)
8183 (org-agenda-redo)
8184 (message "Log mode is %s"
8185 (if org-agenda-show-log "on" "off")))
8187 (defun org-agenda-archives-mode (&optional with-files)
8188 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8189 When called with a prefix argument, include all archive files as well."
8190 (interactive "P")
8191 (setq org-agenda-archives-mode
8192 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8193 (org-agenda-set-mode-name)
8194 (org-agenda-redo)
8195 (message
8196 "%s"
8197 (cond
8198 ((eq org-agenda-archives-mode nil)
8199 "No archives are included")
8200 ((eq org-agenda-archives-mode 'trees)
8201 (format "Trees with :%s: tag are included" org-archive-tag))
8202 ((eq org-agenda-archives-mode t)
8203 (format "Trees with :%s: tag and all active archive files are included"
8204 org-archive-tag)))))
8206 (defun org-agenda-toggle-diary ()
8207 "Toggle diary inclusion in an agenda buffer."
8208 (interactive)
8209 (org-agenda-check-type t 'agenda)
8210 (setq org-agenda-include-diary (not org-agenda-include-diary))
8211 (org-agenda-redo)
8212 (org-agenda-set-mode-name)
8213 (message "Diary inclusion turned %s"
8214 (if org-agenda-include-diary "on" "off")))
8216 (defun org-agenda-toggle-deadlines ()
8217 "Toggle inclusion of entries with a deadline in an agenda buffer."
8218 (interactive)
8219 (org-agenda-check-type t 'agenda)
8220 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8221 (org-agenda-redo)
8222 (org-agenda-set-mode-name)
8223 (message "Deadlines inclusion turned %s"
8224 (if org-agenda-include-deadlines "on" "off")))
8226 (defun org-agenda-toggle-time-grid ()
8227 "Toggle time grid in an agenda buffer."
8228 (interactive)
8229 (org-agenda-check-type t 'agenda)
8230 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8231 (org-agenda-redo)
8232 (org-agenda-set-mode-name)
8233 (message "Time-grid turned %s"
8234 (if org-agenda-use-time-grid "on" "off")))
8236 (defun org-agenda-set-mode-name ()
8237 "Set the mode name to indicate all the small mode settings."
8238 (setq mode-name
8239 (list "Org-Agenda"
8240 (if (get 'org-agenda-files 'org-restrict) " []" "")
8242 '(:eval (org-agenda-span-name org-agenda-current-span))
8243 (if org-agenda-follow-mode " Follow" "")
8244 (if org-agenda-entry-text-mode " ETxt" "")
8245 (if org-agenda-include-diary " Diary" "")
8246 (if org-agenda-include-deadlines " Ddl" "")
8247 (if org-agenda-use-time-grid " Grid" "")
8248 (if (and (boundp 'org-habit-show-habits)
8249 org-habit-show-habits) " Habit" "")
8250 (cond
8251 ((consp org-agenda-show-log) " LogAll")
8252 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8253 (org-agenda-show-log " Log")
8254 (t ""))
8255 (if (or org-agenda-category-filter
8256 (get 'org-agenda-category-filter :preset-filter))
8257 '(:eval (org-propertize
8258 (concat " <"
8259 (mapconcat
8260 'identity
8261 (append
8262 (get 'org-agenda-category-filter :preset-filter)
8263 org-agenda-category-filter)
8265 ">")
8266 'face 'org-agenda-filter-category
8267 'help-echo "Category used in filtering")) "")
8268 (if (or org-agenda-tag-filter
8269 (get 'org-agenda-tag-filter :preset-filter))
8270 '(:eval (org-propertize
8271 (concat " {"
8272 (mapconcat
8273 'identity
8274 (append
8275 (get 'org-agenda-tag-filter :preset-filter)
8276 org-agenda-tag-filter)
8278 "}")
8279 'face 'org-agenda-filter-tags
8280 'help-echo "Tags used in filtering")) "")
8281 (if (or org-agenda-effort-filter
8282 (get 'org-agenda-effort-filter :preset-filter))
8283 '(:eval (org-propertize
8284 (concat " {"
8285 (mapconcat
8286 'identity
8287 (append
8288 (get 'org-agenda-effort-filter :preset-filter)
8289 org-agenda-effort-filter)
8291 "}")
8292 'face 'org-agenda-filter-effort
8293 'help-echo "Effort conditions used in filtering")) "")
8294 (if (or org-agenda-regexp-filter
8295 (get 'org-agenda-regexp-filter :preset-filter))
8296 '(:eval (org-propertize
8297 (concat " ["
8298 (mapconcat
8299 'identity
8300 (append
8301 (get 'org-agenda-regexp-filter :preset-filter)
8302 org-agenda-regexp-filter)
8304 "]")
8305 'face 'org-agenda-filter-regexp
8306 'help-echo "Regexp used in filtering")) "")
8307 (if org-agenda-archives-mode
8308 (if (eq org-agenda-archives-mode t)
8309 " Archives"
8310 (format " :%s:" org-archive-tag))
8312 (if org-agenda-clockreport-mode " Clock" "")))
8313 (force-mode-line-update))
8315 (define-obsolete-function-alias
8316 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8318 (defun org-agenda-update-agenda-type ()
8319 "Update the agenda type after each command."
8320 (setq org-agenda-type
8321 (or (get-text-property (point) 'org-agenda-type)
8322 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8324 (defun org-agenda-next-line ()
8325 "Move cursor to the next line, and show if follow mode is active."
8326 (interactive)
8327 (call-interactively 'next-line)
8328 (org-agenda-do-context-action))
8330 (defun org-agenda-previous-line ()
8331 "Move cursor to the previous line, and show if follow-mode is active."
8332 (interactive)
8333 (call-interactively 'previous-line)
8334 (org-agenda-do-context-action))
8336 (defun org-agenda-next-item (n)
8337 "Move cursor to next agenda item."
8338 (interactive "p")
8339 (let ((col (current-column)))
8340 (dotimes (c n)
8341 (when (next-single-property-change (point-at-eol) 'org-marker)
8342 (move-end-of-line 1)
8343 (goto-char (next-single-property-change (point) 'org-marker))))
8344 (org-move-to-column col))
8345 (org-agenda-do-context-action))
8347 (defun org-agenda-previous-item (n)
8348 "Move cursor to next agenda item."
8349 (interactive "p")
8350 (dotimes (c n)
8351 (let ((col (current-column))
8352 (goto (save-excursion
8353 (move-end-of-line 0)
8354 (previous-single-property-change (point) 'org-marker))))
8355 (if goto (goto-char goto))
8356 (org-move-to-column col)))
8357 (org-agenda-do-context-action))
8359 (defun org-agenda-do-context-action ()
8360 "Show outline path and, maybe, follow mode window."
8361 (let ((m (org-get-at-bol 'org-marker)))
8362 (when (and (markerp m) (marker-buffer m))
8363 (and org-agenda-follow-mode
8364 (if org-agenda-follow-indirect
8365 (org-agenda-tree-to-indirect-buffer nil)
8366 (org-agenda-show)))
8367 (and org-agenda-show-outline-path
8368 (org-with-point-at m (org-display-outline-path t))))))
8370 (defun org-agenda-show-tags ()
8371 "Show the tags applicable to the current item."
8372 (interactive)
8373 (let* ((tags (org-get-at-bol 'tags)))
8374 (if tags
8375 (message "Tags are :%s:"
8376 (org-no-properties (mapconcat 'identity tags ":")))
8377 (message "No tags associated with this line"))))
8379 (defun org-agenda-goto (&optional highlight)
8380 "Go to the entry at point in the corresponding Org-mode file."
8381 (interactive)
8382 (let* ((marker (or (org-get-at-bol 'org-marker)
8383 (org-agenda-error)))
8384 (buffer (marker-buffer marker))
8385 (pos (marker-position marker)))
8386 (switch-to-buffer-other-window buffer)
8387 (widen)
8388 (push-mark)
8389 (goto-char pos)
8390 (when (derived-mode-p 'org-mode)
8391 (org-show-context 'agenda)
8392 (save-excursion
8393 (and (outline-next-heading)
8394 (org-flag-heading nil)))) ; show the next heading
8395 (when (outline-invisible-p)
8396 (show-entry)) ; display invisible text
8397 (recenter (/ (window-height) 2))
8398 (org-back-to-heading t)
8399 (if (re-search-forward org-complex-heading-regexp nil t)
8400 (goto-char (match-beginning 4)))
8401 (run-hooks 'org-agenda-after-show-hook)
8402 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8404 (defvar org-agenda-after-show-hook nil
8405 "Normal hook run after an item has been shown from the agenda.
8406 Point is in the buffer where the item originated.")
8408 (defun org-agenda-kill ()
8409 "Kill the entry or subtree belonging to the current agenda entry."
8410 (interactive)
8411 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8412 (let* ((bufname-orig (buffer-name))
8413 (marker (or (org-get-at-bol 'org-marker)
8414 (org-agenda-error)))
8415 (buffer (marker-buffer marker))
8416 (pos (marker-position marker))
8417 (type (org-get-at-bol 'type))
8418 dbeg dend (n 0) conf)
8419 (org-with-remote-undo buffer
8420 (with-current-buffer buffer
8421 (save-excursion
8422 (goto-char pos)
8423 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8424 (setq dbeg (progn (org-back-to-heading t) (point))
8425 dend (org-end-of-subtree t t))
8426 (setq dbeg (point-at-bol)
8427 dend (min (point-max) (1+ (point-at-eol)))))
8428 (goto-char dbeg)
8429 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8430 (setq conf (or (eq t org-agenda-confirm-kill)
8431 (and (numberp org-agenda-confirm-kill)
8432 (> n org-agenda-confirm-kill))))
8433 (and conf
8434 (not (y-or-n-p
8435 (format "Delete entry with %d lines in buffer \"%s\"? "
8436 n (buffer-name buffer))))
8437 (error "Abort"))
8438 (let ((org-agenda-buffer-name bufname-orig))
8439 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8440 (with-current-buffer buffer (delete-region dbeg dend))
8441 (message "Agenda item and source killed"))))
8443 (defvar org-archive-default-command) ; defined in org-archive.el
8444 (defun org-agenda-archive-default ()
8445 "Archive the entry or subtree belonging to the current agenda entry."
8446 (interactive)
8447 (require 'org-archive)
8448 (org-agenda-archive-with org-archive-default-command))
8450 (defun org-agenda-archive-default-with-confirmation ()
8451 "Archive the entry or subtree belonging to the current agenda entry."
8452 (interactive)
8453 (require 'org-archive)
8454 (org-agenda-archive-with org-archive-default-command 'confirm))
8456 (defun org-agenda-archive ()
8457 "Archive the entry or subtree belonging to the current agenda entry."
8458 (interactive)
8459 (org-agenda-archive-with 'org-archive-subtree))
8461 (defun org-agenda-archive-to-archive-sibling ()
8462 "Move the entry to the archive sibling."
8463 (interactive)
8464 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8466 (defun org-agenda-archive-with (cmd &optional confirm)
8467 "Move the entry to the archive sibling."
8468 (interactive)
8469 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8470 (let* ((bufname-orig (buffer-name))
8471 (marker (or (org-get-at-bol 'org-marker)
8472 (org-agenda-error)))
8473 (buffer (marker-buffer marker))
8474 (pos (marker-position marker)))
8475 (org-with-remote-undo buffer
8476 (with-current-buffer buffer
8477 (if (derived-mode-p 'org-mode)
8478 (if (and confirm
8479 (not (y-or-n-p "Archive this subtree or entry? ")))
8480 (error "Abort")
8481 (save-window-excursion
8482 (goto-char pos)
8483 (let ((org-agenda-buffer-name bufname-orig))
8484 (org-remove-subtree-entries-from-agenda))
8485 (org-back-to-heading t)
8486 (funcall cmd)))
8487 (error "Archiving works only in Org-mode files"))))))
8489 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8490 "Remove all lines in the agenda that correspond to a given subtree.
8491 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8492 If this information is not given, the function uses the tree at point."
8493 (let ((buf (or buf (current-buffer))) m p)
8494 (save-excursion
8495 (unless (and beg end)
8496 (org-back-to-heading t)
8497 (setq beg (point))
8498 (org-end-of-subtree t)
8499 (setq end (point)))
8500 (set-buffer (get-buffer org-agenda-buffer-name))
8501 (save-excursion
8502 (goto-char (point-max))
8503 (beginning-of-line 1)
8504 (while (not (bobp))
8505 (when (and (setq m (org-get-at-bol 'org-marker))
8506 (equal buf (marker-buffer m))
8507 (setq p (marker-position m))
8508 (>= p beg)
8509 (< p end))
8510 (let ((inhibit-read-only t))
8511 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8512 (beginning-of-line 0))))))
8514 (defun org-agenda-refile (&optional goto rfloc no-update)
8515 "Refile the item at point.
8517 When GOTO is 0 or '(64) or \\[universal-argument] \\[universal-argument] \\[universal-argument], clear the refile cache.
8518 When GOTO is '(16) or \\[universal-argument] \\[universal-argument], go to the location of the last refiled item.
8519 RFLOC can be a refile location obtained in a different way.
8520 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8521 (interactive "P")
8522 (cond
8523 ((member goto '(0 (64)))
8524 (org-refile-cache-clear))
8525 ((equal goto '(16))
8526 (org-refile-goto-last-stored))
8528 (let* ((buffer-orig (buffer-name))
8529 (marker (or (org-get-at-bol 'org-hd-marker)
8530 (org-agenda-error)))
8531 (buffer (marker-buffer marker))
8532 (pos (marker-position marker))
8533 (rfloc (or rfloc
8534 (org-refile-get-location
8535 (if goto "Goto" "Refile to") buffer
8536 org-refile-allow-creating-parent-nodes))))
8537 (with-current-buffer buffer
8538 (save-excursion
8539 (save-restriction
8540 (widen)
8541 (goto-char marker)
8542 (let ((org-agenda-buffer-name buffer-orig))
8543 (org-remove-subtree-entries-from-agenda))
8544 (org-refile goto buffer rfloc)))))
8545 (unless no-update (org-agenda-redo)))))
8547 (defun org-agenda-open-link (&optional arg)
8548 "Open the link(s) in the current entry, if any.
8549 This looks for a link in the displayed line in the agenda.
8550 It also looks at the text of the entry itself."
8551 (interactive "P")
8552 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8553 (org-get-at-bol 'org-marker)))
8554 (buffer (and marker (marker-buffer marker)))
8555 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8556 (lkall (and buffer (org-offer-links-in-entry
8557 buffer marker arg prefix)))
8558 (lk0 (car lkall))
8559 (lk (if (stringp lk0) (list lk0) lk0))
8560 (lkend (cdr lkall))
8561 trg)
8562 (cond
8563 ((and buffer lk)
8564 (mapcar (lambda(l)
8565 (with-current-buffer buffer
8566 (setq trg (and (string-match org-bracket-link-regexp l)
8567 (match-string 1 l)))
8568 (if (or (not trg) (string-match org-any-link-re trg))
8569 (save-excursion
8570 (save-restriction
8571 (widen)
8572 (goto-char marker)
8573 (when (search-forward l nil lkend)
8574 (goto-char (match-beginning 0))
8575 (org-open-at-point))))
8576 ;; This is an internal link, widen the buffer
8577 (switch-to-buffer-other-window buffer)
8578 (widen)
8579 (goto-char marker)
8580 (when (search-forward l nil lkend)
8581 (goto-char (match-beginning 0))
8582 (org-open-at-point)))))
8583 lk))
8584 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8585 (save-excursion
8586 (beginning-of-line 1)
8587 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8588 (org-open-link-from-string (match-string 1)))
8589 (t (message "No link to open here")))))
8591 (defun org-agenda-copy-local-variable (var)
8592 "Get a variable from a referenced buffer and install it here."
8593 (let ((m (org-get-at-bol 'org-marker)))
8594 (when (and m (buffer-live-p (marker-buffer m)))
8595 (org-set-local var (with-current-buffer (marker-buffer m)
8596 (symbol-value var))))))
8598 (defun org-agenda-switch-to (&optional delete-other-windows)
8599 "Go to the Org mode file which contains the item at point.
8600 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8601 displayed Org file fills the frame."
8602 (interactive)
8603 (if (and org-return-follows-link
8604 (not (org-get-at-bol 'org-marker))
8605 (org-in-regexp org-bracket-link-regexp))
8606 (org-open-link-from-string (match-string 0))
8607 (let* ((marker (or (org-get-at-bol 'org-marker)
8608 (org-agenda-error)))
8609 (buffer (marker-buffer marker))
8610 (pos (marker-position marker)))
8611 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8612 (org-pop-to-buffer-same-window buffer)
8613 (when delete-other-windows (delete-other-windows))
8614 (widen)
8615 (goto-char pos)
8616 (when (derived-mode-p 'org-mode)
8617 (org-show-context 'agenda)
8618 (run-hooks 'org-agenda-after-show-hook)))))
8620 (defun org-agenda-goto-mouse (ev)
8621 "Go to the Org-mode file which contains the item at the mouse click."
8622 (interactive "e")
8623 (mouse-set-point ev)
8624 (org-agenda-goto))
8626 (defun org-agenda-show (&optional full-entry)
8627 "Display the Org-mode file which contains the item at point.
8628 With prefix argument FULL-ENTRY, make the entire entry visible
8629 if it was hidden in the outline."
8630 (interactive "P")
8631 (let ((win (selected-window)))
8632 (org-agenda-goto t)
8633 (when full-entry (org-show-entry))
8634 (select-window win)))
8636 (defvar org-agenda-show-window nil)
8637 (defun org-agenda-show-and-scroll-up (&optional arg)
8638 "Display the Org-mode file which contains the item at point.
8639 When called repeatedly, scroll the window that is displaying the buffer.
8640 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8641 `show-subtree' to display the item, so that drawers and logbooks stay
8642 folded."
8643 (interactive "P")
8644 (let ((win (selected-window)))
8645 (if (and (window-live-p org-agenda-show-window)
8646 (eq this-command last-command))
8647 (progn
8648 (select-window org-agenda-show-window)
8649 (ignore-errors (scroll-up)))
8650 (org-agenda-goto t)
8651 (if arg (org-show-entry) (show-subtree))
8652 (setq org-agenda-show-window (selected-window)))
8653 (select-window win)))
8655 (defun org-agenda-show-scroll-down ()
8656 "Scroll down the window showing the agenda."
8657 (interactive)
8658 (let ((win (selected-window)))
8659 (when (window-live-p org-agenda-show-window)
8660 (select-window org-agenda-show-window)
8661 (ignore-errors (scroll-down))
8662 (select-window win))))
8664 (defun org-agenda-show-1 (&optional more)
8665 "Display the Org-mode file which contains the item at point.
8666 The prefix arg selects the amount of information to display:
8668 0 hide the subtree
8669 1 just show the entry according to defaults.
8670 2 show the children view
8671 3 show the subtree view
8672 4 show the entire subtree and any LOGBOOK drawers
8673 5 show the entire subtree and any drawers
8674 With prefix argument FULL-ENTRY, make the entire entry visible
8675 if it was hidden in the outline."
8676 (interactive "p")
8677 (let ((win (selected-window)))
8678 (org-agenda-goto t)
8679 (org-back-to-heading)
8680 (set-window-start (selected-window) (point-at-bol))
8681 (cond
8682 ((= more 0)
8683 (hide-subtree)
8684 (save-excursion
8685 (org-back-to-heading)
8686 (run-hook-with-args 'org-cycle-hook 'folded))
8687 (message "Remote: FOLDED"))
8688 ((and (org-called-interactively-p 'any) (= more 1))
8689 (message "Remote: show with default settings"))
8690 ((= more 2)
8691 (show-entry)
8692 (org-show-children)
8693 (save-excursion
8694 (org-back-to-heading)
8695 (run-hook-with-args 'org-cycle-hook 'children))
8696 (message "Remote: CHILDREN"))
8697 ((= more 3)
8698 (show-subtree)
8699 (save-excursion
8700 (org-back-to-heading)
8701 (run-hook-with-args 'org-cycle-hook 'subtree))
8702 (message "Remote: SUBTREE"))
8703 ((= more 4)
8704 (show-subtree)
8705 (save-excursion
8706 (org-back-to-heading)
8707 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8708 (message "Remote: SUBTREE AND LOGBOOK"))
8709 ((> more 4)
8710 (show-subtree)
8711 (message "Remote: SUBTREE AND ALL DRAWERS")))
8712 (select-window win)))
8714 (defvar org-agenda-cycle-counter nil)
8715 (defun org-agenda-cycle-show (&optional n)
8716 "Show the current entry in another window, with default settings.
8718 Default settings are taken from `org-show-context-detail'. When
8719 use repeatedly in immediate succession, the remote entry will
8720 cycle through visibility
8722 children -> subtree -> folded
8724 When called with a numeric prefix arg, that arg will be passed through to
8725 `org-agenda-show-1'. For the interpretation of that argument, see the
8726 docstring of `org-agenda-show-1'."
8727 (interactive "P")
8728 (if (integerp n)
8729 (setq org-agenda-cycle-counter n)
8730 (if (not (eq last-command this-command))
8731 (setq org-agenda-cycle-counter 1)
8732 (if (equal org-agenda-cycle-counter 0)
8733 (setq org-agenda-cycle-counter 2)
8734 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8735 (if (> org-agenda-cycle-counter 3)
8736 (setq org-agenda-cycle-counter 0)))))
8737 (org-agenda-show-1 org-agenda-cycle-counter))
8739 (defun org-agenda-recenter (arg)
8740 "Display the Org-mode file which contains the item at point and recenter."
8741 (interactive "P")
8742 (let ((win (selected-window)))
8743 (org-agenda-goto t)
8744 (recenter arg)
8745 (select-window win)))
8747 (defun org-agenda-show-mouse (ev)
8748 "Display the Org-mode file which contains the item at the mouse click."
8749 (interactive "e")
8750 (mouse-set-point ev)
8751 (org-agenda-show))
8753 (defun org-agenda-check-no-diary ()
8754 "Check if the entry is a diary link and abort if yes."
8755 (if (org-get-at-bol 'org-agenda-diary-link)
8756 (org-agenda-error)))
8758 (defun org-agenda-error ()
8759 "Throw an error when a command is not allowed in the agenda."
8760 (user-error "Command not allowed in this line"))
8762 (defun org-agenda-tree-to-indirect-buffer (arg)
8763 "Show the subtree corresponding to the current entry in an indirect buffer.
8764 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8766 With a numerical prefix ARG, go up to this level and then take that tree.
8767 With a negative numeric ARG, go up by this number of levels.
8768 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8769 use the dedicated frame)."
8770 (interactive "P")
8771 (if current-prefix-arg
8772 (org-agenda-do-tree-to-indirect-buffer arg)
8773 (let ((agenda-buffer (buffer-name))
8774 (agenda-window (selected-window))
8775 (indirect-window
8776 (and org-last-indirect-buffer
8777 (get-buffer-window org-last-indirect-buffer))))
8778 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8779 (unless (or (eq org-indirect-buffer-display 'new-frame)
8780 (eq org-indirect-buffer-display 'dedicated-frame))
8781 (unwind-protect
8782 (unless (and indirect-window (window-live-p indirect-window))
8783 (setq indirect-window (split-window agenda-window)))
8784 (and indirect-window (select-window indirect-window))
8785 (switch-to-buffer org-last-indirect-buffer :norecord)
8786 (fit-window-to-buffer indirect-window)))
8787 (select-window (get-buffer-window agenda-buffer))
8788 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8790 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8791 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8792 (org-agenda-check-no-diary)
8793 (let* ((marker (or (org-get-at-bol 'org-marker)
8794 (org-agenda-error)))
8795 (buffer (marker-buffer marker))
8796 (pos (marker-position marker)))
8797 (with-current-buffer buffer
8798 (save-excursion
8799 (goto-char pos)
8800 (funcall 'org-tree-to-indirect-buffer arg)))))
8802 (defvar org-last-heading-marker (make-marker)
8803 "Marker pointing to the headline that last changed its TODO state
8804 by a remote command from the agenda.")
8806 (defun org-agenda-todo-nextset ()
8807 "Switch TODO entry to next sequence."
8808 (interactive)
8809 (org-agenda-todo 'nextset))
8811 (defun org-agenda-todo-previousset ()
8812 "Switch TODO entry to previous sequence."
8813 (interactive)
8814 (org-agenda-todo 'previousset))
8816 (defun org-agenda-todo (&optional arg)
8817 "Cycle TODO state of line at point, also in Org-mode file.
8818 This changes the line at point, all other lines in the agenda referring to
8819 the same tree node, and the headline of the tree node in the Org-mode file."
8820 (interactive "P")
8821 (org-agenda-check-no-diary)
8822 (let* ((col (current-column))
8823 (marker (or (org-get-at-bol 'org-marker)
8824 (org-agenda-error)))
8825 (buffer (marker-buffer marker))
8826 (pos (marker-position marker))
8827 (hdmarker (org-get-at-bol 'org-hd-marker))
8828 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8829 (inhibit-read-only t)
8830 org-agenda-headline-snapshot-before-repeat newhead just-one)
8831 (org-with-remote-undo buffer
8832 (with-current-buffer buffer
8833 (widen)
8834 (goto-char pos)
8835 (org-show-context 'agenda)
8836 (save-excursion
8837 (and (outline-next-heading)
8838 (org-flag-heading nil))) ; show the next heading
8839 (let ((current-prefix-arg arg))
8840 (call-interactively 'org-todo))
8841 (and (bolp) (forward-char 1))
8842 (setq newhead (org-get-heading))
8843 (when (and (org-bound-and-true-p
8844 org-agenda-headline-snapshot-before-repeat)
8845 (not (equal org-agenda-headline-snapshot-before-repeat
8846 newhead))
8847 todayp)
8848 (setq newhead org-agenda-headline-snapshot-before-repeat
8849 just-one t))
8850 (save-excursion
8851 (org-back-to-heading)
8852 (move-marker org-last-heading-marker (point))))
8853 (beginning-of-line 1)
8854 (save-window-excursion
8855 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8856 (when (org-bound-and-true-p org-clock-out-when-done)
8857 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8858 newhead)
8859 (org-agenda-unmark-clocking-task))
8860 (org-move-to-column col)
8861 (org-agenda-mark-clocking-task))))
8863 (defun org-agenda-add-note (&optional arg)
8864 "Add a time-stamped note to the entry at point."
8865 (interactive "P")
8866 (org-agenda-check-no-diary)
8867 (let* ((marker (or (org-get-at-bol 'org-marker)
8868 (org-agenda-error)))
8869 (buffer (marker-buffer marker))
8870 (pos (marker-position marker))
8871 (hdmarker (org-get-at-bol 'org-hd-marker))
8872 (inhibit-read-only t))
8873 (with-current-buffer buffer
8874 (widen)
8875 (goto-char pos)
8876 (org-show-context 'agenda)
8877 (save-excursion
8878 (and (outline-next-heading)
8879 (org-flag-heading nil))) ; show the next heading
8880 (org-add-note))))
8882 (defun org-agenda-change-all-lines (newhead hdmarker
8883 &optional fixface just-this)
8884 "Change all lines in the agenda buffer which match HDMARKER.
8885 The new content of the line will be NEWHEAD (as modified by
8886 `org-agenda-format-item'). HDMARKER is checked with
8887 `equal' against all `org-hd-marker' text properties in the file.
8888 If FIXFACE is non-nil, the face of each item is modified according to
8889 the new TODO state.
8890 If JUST-THIS is non-nil, change just the current line, not all.
8891 If FORCE-TAGS is non nil, the car of it returns the new tags."
8892 (let* ((inhibit-read-only t)
8893 (line (org-current-line))
8894 (org-agenda-buffer (current-buffer))
8895 (thetags (with-current-buffer (marker-buffer hdmarker)
8896 (save-excursion (save-restriction (widen)
8897 (goto-char hdmarker)
8898 (org-get-tags-at)))))
8899 props m pl undone-face done-face finish new dotime level cat tags)
8900 (save-excursion
8901 (goto-char (point-max))
8902 (beginning-of-line 1)
8903 (while (not finish)
8904 (setq finish (bobp))
8905 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8906 (or (not just-this) (= (org-current-line) line))
8907 (equal m hdmarker))
8908 (setq props (text-properties-at (point))
8909 dotime (org-get-at-bol 'dotime)
8910 cat (org-get-at-eol 'org-category 1)
8911 level (org-get-at-bol 'level)
8912 tags thetags
8914 (let ((org-prefix-format-compiled
8915 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8916 org-prefix-format-compiled))
8917 (extra (org-get-at-bol 'extra)))
8918 (with-current-buffer (marker-buffer hdmarker)
8919 (save-excursion
8920 (save-restriction
8921 (widen)
8922 (org-agenda-format-item extra newhead level cat tags dotime)))))
8923 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8924 undone-face (org-get-at-bol 'undone-face)
8925 done-face (org-get-at-bol 'done-face))
8926 (beginning-of-line 1)
8927 (cond
8928 ((equal new "")
8929 (and (looking-at ".*\n?") (replace-match "")))
8930 ((looking-at ".*")
8931 (replace-match new t t)
8932 (beginning-of-line 1)
8933 (add-text-properties (point-at-bol) (point-at-eol) props)
8934 (when fixface
8935 (add-text-properties
8936 (point-at-bol) (point-at-eol)
8937 (list 'face
8938 (if org-last-todo-state-is-todo
8939 undone-face done-face))))
8940 (org-agenda-highlight-todo 'line)
8941 (beginning-of-line 1))
8942 (t (error "Line update did not work")))
8943 (save-restriction
8944 (narrow-to-region (point-at-bol) (point-at-eol))
8945 (org-agenda-finalize)))
8946 (beginning-of-line 0)))))
8948 (defun org-agenda-align-tags (&optional line)
8949 "Align all tags in agenda items to `org-agenda-tags-column'."
8950 (let ((inhibit-read-only t) l c)
8951 (save-excursion
8952 (goto-char (if line (point-at-bol) (point-min)))
8953 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8954 (if line (point-at-eol) nil) t)
8955 (add-text-properties
8956 (match-beginning 2) (match-end 2)
8957 (list 'face (delq nil (let ((prop (get-text-property
8958 (match-beginning 2) 'face)))
8959 (or (listp prop) (setq prop (list prop)))
8960 (if (memq 'org-tag prop)
8961 prop
8962 (cons 'org-tag prop))))))
8963 (setq l (- (match-end 2) (match-beginning 2))
8964 c (if (< org-agenda-tags-column 0)
8965 (- (abs org-agenda-tags-column) l)
8966 org-agenda-tags-column))
8967 (delete-region (match-beginning 1) (match-end 1))
8968 (goto-char (match-beginning 1))
8969 (insert (org-add-props
8970 (make-string (max 1 (- c (current-column))) ?\ )
8971 (plist-put (copy-sequence (text-properties-at (point)))
8972 'face nil))))
8973 (goto-char (point-min))
8974 (org-font-lock-add-tag-faces (point-max)))))
8976 (defun org-agenda-priority-up ()
8977 "Increase the priority of line at point, also in Org-mode file."
8978 (interactive)
8979 (org-agenda-priority 'up))
8981 (defun org-agenda-priority-down ()
8982 "Decrease the priority of line at point, also in Org-mode file."
8983 (interactive)
8984 (org-agenda-priority 'down))
8986 (defun org-agenda-priority (&optional force-direction)
8987 "Set the priority of line at point, also in Org-mode file.
8988 This changes the line at point, all other lines in the agenda referring to
8989 the same tree node, and the headline of the tree node in the Org-mode file.
8990 Called with a universal prefix arg, show the priority instead of setting it."
8991 (interactive "P")
8992 (if (equal force-direction '(4))
8993 (org-show-priority)
8994 (unless org-enable-priority-commands
8995 (error "Priority commands are disabled"))
8996 (org-agenda-check-no-diary)
8997 (let* ((col (current-column))
8998 (marker (or (org-get-at-bol 'org-marker)
8999 (org-agenda-error)))
9000 (hdmarker (org-get-at-bol 'org-hd-marker))
9001 (buffer (marker-buffer hdmarker))
9002 (pos (marker-position hdmarker))
9003 (inhibit-read-only t)
9004 newhead)
9005 (org-with-remote-undo buffer
9006 (with-current-buffer buffer
9007 (widen)
9008 (goto-char pos)
9009 (org-show-context 'agenda)
9010 (save-excursion
9011 (and (outline-next-heading)
9012 (org-flag-heading nil))) ; show the next heading
9013 (funcall 'org-priority force-direction)
9014 (end-of-line 1)
9015 (setq newhead (org-get-heading)))
9016 (org-agenda-change-all-lines newhead hdmarker)
9017 (org-move-to-column col)))))
9019 ;; FIXME: should fix the tags property of the agenda line.
9020 (defun org-agenda-set-tags (&optional tag onoff)
9021 "Set tags for the current headline."
9022 (interactive)
9023 (org-agenda-check-no-diary)
9024 (if (and (org-region-active-p) (org-called-interactively-p 'any))
9025 (call-interactively 'org-change-tag-in-region)
9026 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9027 (org-agenda-error)))
9028 (buffer (marker-buffer hdmarker))
9029 (pos (marker-position hdmarker))
9030 (inhibit-read-only t)
9031 newhead)
9032 (org-with-remote-undo buffer
9033 (with-current-buffer buffer
9034 (widen)
9035 (goto-char pos)
9036 (save-excursion
9037 (org-show-context 'agenda))
9038 (save-excursion
9039 (and (outline-next-heading)
9040 (org-flag-heading nil))) ; show the next heading
9041 (goto-char pos)
9042 (if tag
9043 (org-toggle-tag tag onoff)
9044 (call-interactively 'org-set-tags))
9045 (end-of-line 1)
9046 (setq newhead (org-get-heading)))
9047 (org-agenda-change-all-lines newhead hdmarker)
9048 (beginning-of-line 1)))))
9050 (defun org-agenda-set-property ()
9051 "Set a property for the current headline."
9052 (interactive)
9053 (org-agenda-check-no-diary)
9054 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9055 (org-agenda-error)))
9056 (buffer (marker-buffer hdmarker))
9057 (pos (marker-position hdmarker))
9058 (inhibit-read-only t)
9059 newhead)
9060 (org-with-remote-undo buffer
9061 (with-current-buffer buffer
9062 (widen)
9063 (goto-char pos)
9064 (save-excursion
9065 (org-show-context 'agenda))
9066 (save-excursion
9067 (and (outline-next-heading)
9068 (org-flag-heading nil))) ; show the next heading
9069 (goto-char pos)
9070 (call-interactively 'org-set-property)))))
9072 (defun org-agenda-set-effort ()
9073 "Set the effort property for the current headline."
9074 (interactive)
9075 (org-agenda-check-no-diary)
9076 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9077 (org-agenda-error)))
9078 (buffer (marker-buffer hdmarker))
9079 (pos (marker-position hdmarker))
9080 (inhibit-read-only t)
9081 newhead)
9082 (org-with-remote-undo buffer
9083 (with-current-buffer buffer
9084 (widen)
9085 (goto-char pos)
9086 (save-excursion
9087 (org-show-context 'agenda))
9088 (save-excursion
9089 (and (outline-next-heading)
9090 (org-flag-heading nil))) ; show the next heading
9091 (goto-char pos)
9092 (call-interactively 'org-set-effort)
9093 (end-of-line 1)
9094 (setq newhead (org-get-heading)))
9095 (org-agenda-change-all-lines newhead hdmarker))))
9097 (defun org-agenda-toggle-archive-tag ()
9098 "Toggle the archive tag for the current entry."
9099 (interactive)
9100 (org-agenda-check-no-diary)
9101 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9102 (org-agenda-error)))
9103 (buffer (marker-buffer hdmarker))
9104 (pos (marker-position hdmarker))
9105 (inhibit-read-only t)
9106 newhead)
9107 (org-with-remote-undo buffer
9108 (with-current-buffer buffer
9109 (widen)
9110 (goto-char pos)
9111 (org-show-context 'agenda)
9112 (save-excursion
9113 (and (outline-next-heading)
9114 (org-flag-heading nil))) ; show the next heading
9115 (call-interactively 'org-toggle-archive-tag)
9116 (end-of-line 1)
9117 (setq newhead (org-get-heading)))
9118 (org-agenda-change-all-lines newhead hdmarker)
9119 (beginning-of-line 1))))
9121 (defun org-agenda-do-date-later (arg)
9122 (interactive "P")
9123 (cond
9124 ((or (equal arg '(16))
9125 (memq last-command
9126 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9127 (setq this-command 'org-agenda-date-later-minutes)
9128 (org-agenda-date-later-minutes 1))
9129 ((or (equal arg '(4))
9130 (memq last-command
9131 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9132 (setq this-command 'org-agenda-date-later-hours)
9133 (org-agenda-date-later-hours 1))
9135 (org-agenda-date-later (prefix-numeric-value arg)))))
9137 (defun org-agenda-do-date-earlier (arg)
9138 (interactive "P")
9139 (cond
9140 ((or (equal arg '(16))
9141 (memq last-command
9142 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9143 (setq this-command 'org-agenda-date-earlier-minutes)
9144 (org-agenda-date-earlier-minutes 1))
9145 ((or (equal arg '(4))
9146 (memq last-command
9147 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9148 (setq this-command 'org-agenda-date-earlier-hours)
9149 (org-agenda-date-earlier-hours 1))
9151 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9153 (defun org-agenda-date-later (arg &optional what)
9154 "Change the date of this item to ARG day(s) later."
9155 (interactive "p")
9156 (org-agenda-check-type t 'agenda 'timeline)
9157 (org-agenda-check-no-diary)
9158 (let* ((marker (or (org-get-at-bol 'org-marker)
9159 (org-agenda-error)))
9160 (buffer (marker-buffer marker))
9161 (pos (marker-position marker))
9162 cdate today)
9163 (org-with-remote-undo buffer
9164 (with-current-buffer buffer
9165 (widen)
9166 (goto-char pos)
9167 (if (not (org-at-timestamp-p))
9168 (error "Cannot find time stamp"))
9169 (when (and org-agenda-move-date-from-past-immediately-to-today
9170 (equal arg 1)
9171 (or (not what) (eq what 'day))
9172 (not (save-match-data (org-at-date-range-p))))
9173 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9174 cdate (calendar-absolute-from-gregorian
9175 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9176 today (org-today))
9177 (if (> today cdate)
9178 ;; immediately shift to today
9179 (setq arg (- today cdate))))
9180 (org-timestamp-change arg (or what 'day))
9181 (when (and (org-at-date-range-p)
9182 (re-search-backward org-tr-regexp-both (point-at-bol)))
9183 (let ((end org-last-changed-timestamp))
9184 (org-timestamp-change arg (or what 'day))
9185 (setq org-last-changed-timestamp
9186 (concat org-last-changed-timestamp "--" end)))))
9187 (org-agenda-show-new-time marker org-last-changed-timestamp))
9188 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9190 (defun org-agenda-date-earlier (arg &optional what)
9191 "Change the date of this item to ARG day(s) earlier."
9192 (interactive "p")
9193 (org-agenda-date-later (- arg) what))
9195 (defun org-agenda-date-later-minutes (arg)
9196 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9197 (interactive "p")
9198 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9199 (org-agenda-date-later arg 'minute))
9201 (defun org-agenda-date-earlier-minutes (arg)
9202 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9203 (interactive "p")
9204 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9205 (org-agenda-date-earlier arg 'minute))
9207 (defun org-agenda-date-later-hours (arg)
9208 "Change the time of this item, in hour steps."
9209 (interactive "p")
9210 (org-agenda-date-later arg 'hour))
9212 (defun org-agenda-date-earlier-hours (arg)
9213 "Change the time of this item, in hour steps."
9214 (interactive "p")
9215 (org-agenda-date-earlier arg 'hour))
9217 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9218 "Show new date stamp via text properties."
9219 ;; We use text properties to make this undoable
9220 (let ((inhibit-read-only t))
9221 (setq stamp (concat prefix " => " stamp " "))
9222 (save-excursion
9223 (goto-char (point-max))
9224 (while (not (bobp))
9225 (when (equal marker (org-get-at-bol 'org-marker))
9226 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9227 (org-move-to-column (- (window-width) (length stamp)) t)
9228 (if (featurep 'xemacs)
9229 ;; Use `duplicable' property to trigger undo recording
9230 (let ((ex (make-extent nil nil))
9231 (gl (make-glyph stamp)))
9232 (set-glyph-face gl 'secondary-selection)
9233 (set-extent-properties
9234 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9235 (insert-extent ex (1- (point)) (point-at-eol)))
9236 (add-text-properties
9237 (1- (point)) (point-at-eol)
9238 (list 'display (org-add-props stamp nil
9239 'face '(secondary-selection default)))))
9240 (beginning-of-line 1))
9241 (beginning-of-line 0)))))
9243 (defun org-agenda-date-prompt (arg)
9244 "Change the date of this item. Date is prompted for, with default today.
9245 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9246 be used to request time specification in the time stamp."
9247 (interactive "P")
9248 (org-agenda-check-type t 'agenda 'timeline)
9249 (org-agenda-check-no-diary)
9250 (let* ((marker (or (org-get-at-bol 'org-marker)
9251 (org-agenda-error)))
9252 (buffer (marker-buffer marker))
9253 (pos (marker-position marker)))
9254 (org-with-remote-undo buffer
9255 (with-current-buffer buffer
9256 (widen)
9257 (goto-char pos)
9258 (if (not (org-at-timestamp-p t))
9259 (error "Cannot find time stamp"))
9260 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9261 (org-agenda-show-new-time marker org-last-changed-timestamp))
9262 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9264 (defun org-agenda-schedule (arg &optional time)
9265 "Schedule the item at point.
9266 ARG is passed through to `org-schedule'."
9267 (interactive "P")
9268 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9269 (org-agenda-check-no-diary)
9270 (let* ((marker (or (org-get-at-bol 'org-marker)
9271 (org-agenda-error)))
9272 (type (marker-insertion-type marker))
9273 (buffer (marker-buffer marker))
9274 (pos (marker-position marker))
9276 (set-marker-insertion-type marker t)
9277 (org-with-remote-undo buffer
9278 (with-current-buffer buffer
9279 (widen)
9280 (goto-char pos)
9281 (setq ts (org-schedule arg time)))
9282 (org-agenda-show-new-time marker ts " S"))
9283 (message "%s" ts)))
9285 (defun org-agenda-deadline (arg &optional time)
9286 "Schedule the item at point.
9287 ARG is passed through to `org-deadline'."
9288 (interactive "P")
9289 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9290 (org-agenda-check-no-diary)
9291 (let* ((marker (or (org-get-at-bol 'org-marker)
9292 (org-agenda-error)))
9293 (buffer (marker-buffer marker))
9294 (pos (marker-position marker))
9296 (org-with-remote-undo buffer
9297 (with-current-buffer buffer
9298 (widen)
9299 (goto-char pos)
9300 (setq ts (org-deadline arg time)))
9301 (org-agenda-show-new-time marker ts " D"))
9302 (message "%s" ts)))
9304 (defun org-agenda-clock-in (&optional arg)
9305 "Start the clock on the currently selected item."
9306 (interactive "P")
9307 (org-agenda-check-no-diary)
9308 (if (equal arg '(4))
9309 (org-clock-in arg)
9310 (let* ((marker (or (org-get-at-bol 'org-marker)
9311 (org-agenda-error)))
9312 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9313 (pos (marker-position marker))
9314 (col (current-column))
9315 newhead)
9316 (org-with-remote-undo (marker-buffer marker)
9317 (with-current-buffer (marker-buffer marker)
9318 (widen)
9319 (goto-char pos)
9320 (org-show-context 'agenda)
9321 (org-show-entry)
9322 (org-cycle-hide-drawers 'children)
9323 (org-clock-in arg)
9324 (setq newhead (org-get-heading)))
9325 (org-agenda-change-all-lines newhead hdmarker))
9326 (org-move-to-column col))))
9328 (defun org-agenda-clock-out ()
9329 "Stop the currently running clock."
9330 (interactive)
9331 (unless (marker-buffer org-clock-marker)
9332 (error "No running clock"))
9333 (let ((marker (make-marker)) (col (current-column)) newhead)
9334 (org-with-remote-undo (marker-buffer org-clock-marker)
9335 (with-current-buffer (marker-buffer org-clock-marker)
9336 (save-excursion
9337 (save-restriction
9338 (widen)
9339 (goto-char org-clock-marker)
9340 (org-back-to-heading t)
9341 (move-marker marker (point))
9342 (org-clock-out)
9343 (setq newhead (org-get-heading))))))
9344 (org-agenda-change-all-lines newhead marker)
9345 (move-marker marker nil)
9346 (org-move-to-column col)
9347 (org-agenda-unmark-clocking-task)))
9349 (defun org-agenda-clock-cancel (&optional arg)
9350 "Cancel the currently running clock."
9351 (interactive "P")
9352 (unless (marker-buffer org-clock-marker)
9353 (user-error "No running clock"))
9354 (org-with-remote-undo (marker-buffer org-clock-marker)
9355 (org-clock-cancel)))
9357 (defun org-agenda-clock-goto ()
9358 "Jump to the currently clocked in task within the agenda.
9359 If the currently clocked in task is not listed in the agenda
9360 buffer, display it in another window."
9361 (interactive)
9362 (let (pos)
9363 (mapc (lambda (o)
9364 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9365 (setq pos (overlay-start o))))
9366 (overlays-in (point-min) (point-max)))
9367 (cond (pos (goto-char pos))
9368 ;; If the currently clocked entry is not in the agenda
9369 ;; buffer, we visit it in another window:
9370 (org-clock-current-task
9371 (org-switch-to-buffer-other-window (org-clock-goto)))
9372 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9374 (defun org-agenda-diary-entry-in-org-file ()
9375 "Make a diary entry in the file `org-agenda-diary-file'."
9376 (let (d1 d2 char (text "") dp1 dp2)
9377 (if (equal (buffer-name) "*Calendar*")
9378 (setq d1 (calendar-cursor-to-date t)
9379 d2 (car calendar-mark-ring))
9380 (setq dp1 (get-text-property (point-at-bol) 'day))
9381 (unless dp1 (user-error "No date defined in current line"))
9382 (setq d1 (calendar-gregorian-from-absolute dp1)
9383 d2 (and (ignore-errors (mark))
9384 (save-excursion
9385 (goto-char (mark))
9386 (setq dp2 (get-text-property (point-at-bol) 'day)))
9387 (calendar-gregorian-from-absolute dp2))))
9388 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9389 (setq char (read-char-exclusive))
9390 (cond
9391 ((equal char ?d)
9392 (setq text (read-string "Day entry: "))
9393 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9394 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9395 ((equal char ?a)
9396 (setq d1 (list (car d1) (nth 1 d1)
9397 (read-number (format "Reference year [%d]: " (nth 2 d1))
9398 (nth 2 d1))))
9399 (setq text (read-string "Anniversary (use %d to show years): "))
9400 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9401 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9402 ((equal char ?b)
9403 (setq text (read-string "Block entry: "))
9404 (unless (and d1 d2 (not (equal d1 d2)))
9405 (user-error "No block of days selected"))
9406 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9407 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9408 ((equal char ?j)
9409 (org-switch-to-buffer-other-window
9410 (find-file-noselect org-agenda-diary-file))
9411 (require 'org-datetree)
9412 (org-datetree-find-date-create d1)
9413 (org-reveal t))
9414 (t (user-error "Invalid selection character `%c'" char)))))
9416 (defcustom org-agenda-insert-diary-strategy 'date-tree
9417 "Where in `org-agenda-diary-file' should new entries be added?
9418 Valid values:
9420 date-tree in the date tree, as first child of the date
9421 date-tree-last in the date tree, as last child of the date
9422 top-level as top-level entries at the end of the file."
9423 :group 'org-agenda
9424 :type '(choice
9425 (const :tag "first in a date tree" date-tree)
9426 (const :tag "last in a date tree" date-tree-last)
9427 (const :tag "as top level at end of file" top-level)))
9429 (defcustom org-agenda-insert-diary-extract-time nil
9430 "Non-nil means extract any time specification from the diary entry."
9431 :group 'org-agenda
9432 :version "24.1"
9433 :type 'boolean)
9435 (defcustom org-agenda-bulk-mark-char ">"
9436 "A single-character string to be used as the bulk mark."
9437 :group 'org-agenda
9438 :version "24.1"
9439 :type 'string)
9441 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9442 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9443 If TEXT is not empty, it will become the headline of the new entry, and
9444 the resulting entry will not be shown. When TEXT is empty, switch to
9445 `org-agenda-diary-file' and let the user finish the entry there."
9446 (let ((cw (current-window-configuration)))
9447 (org-switch-to-buffer-other-window
9448 (find-file-noselect org-agenda-diary-file))
9449 (widen)
9450 (goto-char (point-min))
9451 (cond
9452 ((eq type 'anniversary)
9453 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9454 (progn
9455 (or (org-at-heading-p t)
9456 (progn
9457 (outline-next-heading)
9458 (insert "* Anniversaries\n\n")
9459 (beginning-of-line -1)))))
9460 (outline-next-heading)
9461 (org-back-over-empty-lines)
9462 (backward-char 1)
9463 (insert "\n")
9464 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9465 (nth 2 d1) (car d1) (nth 1 d1) text)))
9466 ((eq type 'day)
9467 (let ((org-prefix-has-time t)
9468 (org-agenda-time-leading-zero t)
9469 fmt time time2)
9470 (if org-agenda-insert-diary-extract-time
9471 ;; Use org-agenda-format-item to parse text for a time-range and
9472 ;; remove it. FIXME: This is a hack, we should refactor
9473 ;; that function to make time extraction available separately
9474 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9475 time (get-text-property 0 'time fmt)
9476 time2 (if (> (length time) 0)
9477 ;; split-string removes trailing ...... if
9478 ;; no end time given. First space
9479 ;; separates time from date.
9480 (concat " " (car (split-string time "\\.")))
9481 nil)
9482 text (get-text-property 0 'txt fmt)))
9483 (if (eq org-agenda-insert-diary-strategy 'top-level)
9484 (org-agenda-insert-diary-as-top-level text)
9485 (require 'org-datetree)
9486 (org-datetree-find-date-create d1)
9487 (org-agenda-insert-diary-make-new-entry text))
9488 (org-insert-time-stamp (org-time-from-absolute
9489 (calendar-absolute-from-gregorian d1))
9490 nil nil nil nil time2))
9491 (end-of-line 0))
9492 ((eq type 'block)
9493 (if (> (calendar-absolute-from-gregorian d1)
9494 (calendar-absolute-from-gregorian d2))
9495 (setq d1 (prog1 d2 (setq d2 d1))))
9496 (if (eq org-agenda-insert-diary-strategy 'top-level)
9497 (org-agenda-insert-diary-as-top-level text)
9498 (require 'org-datetree)
9499 (org-datetree-find-date-create d1)
9500 (org-agenda-insert-diary-make-new-entry text))
9501 (org-insert-time-stamp (org-time-from-absolute
9502 (calendar-absolute-from-gregorian d1)))
9503 (insert "--")
9504 (org-insert-time-stamp (org-time-from-absolute
9505 (calendar-absolute-from-gregorian d2)))
9506 (end-of-line 0)))
9507 (if (string-match "\\S-" text)
9508 (progn
9509 (set-window-configuration cw)
9510 (message "%s entry added to %s"
9511 (capitalize (symbol-name type))
9512 (abbreviate-file-name org-agenda-diary-file)))
9513 (org-reveal t)
9514 (message "Please finish entry here"))))
9516 (defun org-agenda-insert-diary-as-top-level (text)
9517 "Make new entry as a top-level entry at the end of the file.
9518 Add TEXT as headline, and position the cursor in the second line so that
9519 a timestamp can be added there."
9520 (widen)
9521 (goto-char (point-max))
9522 (unless (bolp) (insert "\n"))
9523 (org-insert-heading nil t t)
9524 (insert text)
9525 (org-end-of-meta-data)
9526 (unless (bolp) (insert "\n"))
9527 (when org-adapt-indentation (org-indent-to-column 2)))
9529 (defun org-agenda-insert-diary-make-new-entry (text)
9530 "Make a new entry with TEXT as a child of the current subtree.
9531 Position the point in the heading's first body line so that
9532 a timestamp can be added there."
9533 (cond
9534 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9535 (end-of-line)
9536 (org-insert-heading '(4) t)
9537 (org-do-demote))
9539 (outline-next-heading)
9540 (org-back-over-empty-lines)
9541 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9542 (org-insert-heading nil t)
9543 (org-do-demote)))
9544 (let ((col (current-column)))
9545 (insert text)
9546 (org-end-of-meta-data)
9547 ;; Ensure point is left on a blank line, at proper indentation.
9548 (unless (bolp) (insert "\n"))
9549 (unless (org-looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9550 (when org-adapt-indentation (org-indent-to-column col)))
9551 (org-show-set-visibility 'lineage))
9553 (defun org-agenda-diary-entry ()
9554 "Make a diary entry, like the `i' command from the calendar.
9555 All the standard commands work: block, weekly etc.
9556 When `org-agenda-diary-file' points to a file,
9557 `org-agenda-diary-entry-in-org-file' is called instead to create
9558 entries in that Org-mode file."
9559 (interactive)
9560 (if (not (eq org-agenda-diary-file 'diary-file))
9561 (org-agenda-diary-entry-in-org-file)
9562 (require 'diary-lib)
9563 (let* ((char (progn
9564 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9565 (read-char-exclusive)))
9566 (cmd (cdr (assoc char
9567 '((?d . diary-insert-entry)
9568 (?w . diary-insert-weekly-entry)
9569 (?m . diary-insert-monthly-entry)
9570 (?y . diary-insert-yearly-entry)
9571 (?a . diary-insert-anniversary-entry)
9572 (?b . diary-insert-block-entry)
9573 (?c . diary-insert-cyclic-entry)))))
9574 (oldf (symbol-function 'calendar-cursor-to-date))
9575 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9576 (point (point))
9577 (mark (or (mark t) (point))))
9578 (unless cmd
9579 (user-error "No command associated with <%c>" char))
9580 (unless (and (get-text-property point 'day)
9581 (or (not (equal ?b char))
9582 (get-text-property mark 'day)))
9583 (user-error "Don't know which date to use for diary entry"))
9584 ;; We implement this by hacking the `calendar-cursor-to-date' function
9585 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9586 (let ((calendar-mark-ring
9587 (list (calendar-gregorian-from-absolute
9588 (or (get-text-property mark 'day)
9589 (get-text-property point 'day))))))
9590 (unwind-protect
9591 (progn
9592 (fset 'calendar-cursor-to-date
9593 (lambda (&optional error dummy)
9594 (calendar-gregorian-from-absolute
9595 (get-text-property point 'day))))
9596 (call-interactively cmd))
9597 (fset 'calendar-cursor-to-date oldf))))))
9599 (defun org-agenda-execute-calendar-command (cmd)
9600 "Execute a calendar command from the agenda with date from cursor."
9601 (org-agenda-check-type t 'agenda 'timeline)
9602 (require 'diary-lib)
9603 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9604 (user-error "Don't know which date to use for the calendar command"))
9605 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9606 (point (point))
9607 (date (calendar-gregorian-from-absolute
9608 (get-text-property point 'day)))
9609 ;; the following 2 vars are needed in the calendar
9610 (displayed-month (car date))
9611 (displayed-year (nth 2 date)))
9612 (unwind-protect
9613 (progn
9614 (fset 'calendar-cursor-to-date
9615 (lambda (&optional error dummy)
9616 (calendar-gregorian-from-absolute
9617 (get-text-property point 'day))))
9618 (call-interactively cmd))
9619 (fset 'calendar-cursor-to-date oldf))))
9621 (defun org-agenda-phases-of-moon ()
9622 "Display the phases of the moon for the 3 months around the cursor date."
9623 (interactive)
9624 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9626 (defun org-agenda-holidays ()
9627 "Display the holidays for the 3 months around the cursor date."
9628 (interactive)
9629 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9631 (defvar calendar-longitude) ; defined in calendar.el
9632 (defvar calendar-latitude) ; defined in calendar.el
9633 (defvar calendar-location-name) ; defined in calendar.el
9635 (defun org-agenda-sunrise-sunset (arg)
9636 "Display sunrise and sunset for the cursor date.
9637 Latitude and longitude can be specified with the variables
9638 `calendar-latitude' and `calendar-longitude'. When called with prefix
9639 argument, latitude and longitude will be prompted for."
9640 (interactive "P")
9641 (require 'solar)
9642 (let ((calendar-longitude (if arg nil calendar-longitude))
9643 (calendar-latitude (if arg nil calendar-latitude))
9644 (calendar-location-name
9645 (if arg "the given coordinates" calendar-location-name)))
9646 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9648 (defun org-agenda-goto-calendar ()
9649 "Open the Emacs calendar with the date at the cursor."
9650 (interactive)
9651 (org-agenda-check-type t 'agenda 'timeline)
9652 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9653 (user-error "Don't know which date to open in calendar")))
9654 (date (calendar-gregorian-from-absolute day))
9655 (calendar-move-hook nil)
9656 (calendar-view-holidays-initially-flag nil)
9657 (calendar-view-diary-initially-flag nil))
9658 (calendar)
9659 (calendar-goto-date date)))
9661 ;;;###autoload
9662 (defun org-calendar-goto-agenda ()
9663 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9664 This is a command that has to be installed in `calendar-mode-map'."
9665 (interactive)
9666 ;; Temporarily disable sticky agenda since user clearly wants to
9667 ;; refresh view anyway.
9668 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9669 (org-agenda-sticky nil))
9670 (org-agenda-list nil (calendar-absolute-from-gregorian
9671 (calendar-cursor-to-date))
9672 nil)))
9674 (defun org-agenda-convert-date ()
9675 (interactive)
9676 (org-agenda-check-type t 'agenda 'timeline)
9677 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9678 date s)
9679 (unless day
9680 (user-error "Don't know which date to convert"))
9681 (setq date (calendar-gregorian-from-absolute day))
9682 (setq s (concat
9683 "Gregorian: " (calendar-date-string date) "\n"
9684 "ISO: " (calendar-iso-date-string date) "\n"
9685 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9686 "Julian: " (calendar-julian-date-string date) "\n"
9687 "Astron. JD: " (calendar-astro-date-string date)
9688 " (Julian date number at noon UTC)\n"
9689 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9690 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9691 "French: " (calendar-french-date-string date) "\n"
9692 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9693 "Mayan: " (calendar-mayan-date-string date) "\n"
9694 "Coptic: " (calendar-coptic-date-string date) "\n"
9695 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9696 "Persian: " (calendar-persian-date-string date) "\n"
9697 "Chinese: " (calendar-chinese-date-string date) "\n"))
9698 (with-output-to-temp-buffer "*Dates*"
9699 (princ s))
9700 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9702 ;;; Bulk commands
9704 (defun org-agenda-bulk-marked-p ()
9705 (eq (get-char-property (point-at-bol) 'type)
9706 'org-marked-entry-overlay))
9708 (defun org-agenda-bulk-mark (&optional arg)
9709 "Mark the entry at point for future bulk action."
9710 (interactive "p")
9711 (dotimes (i (or arg 1))
9712 (unless (org-get-at-bol 'org-agenda-diary-link)
9713 (let* ((m (org-get-at-bol 'org-hd-marker))
9715 (unless (org-agenda-bulk-marked-p)
9716 (unless m (user-error "Nothing to mark at point"))
9717 (push m org-agenda-bulk-marked-entries)
9718 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9719 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9720 (org-get-todo-face "TODO")
9721 'evaporate)
9722 (overlay-put ov 'type 'org-marked-entry-overlay))
9723 (end-of-line 1)
9724 (or (ignore-errors
9725 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9726 (beginning-of-line 2))
9727 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9728 (beginning-of-line 2))
9729 (message "%d entries marked for bulk action"
9730 (length org-agenda-bulk-marked-entries))))))
9732 (defun org-agenda-bulk-mark-all ()
9733 "Mark all entries for future agenda bulk action."
9734 (interactive)
9735 (org-agenda-bulk-mark-regexp "."))
9737 (defun org-agenda-bulk-mark-regexp (regexp)
9738 "Mark entries matching REGEXP for future agenda bulk action."
9739 (interactive "sMark entries matching regexp: ")
9740 (let ((entries-marked 0) txt-at-point)
9741 (save-excursion
9742 (goto-char (point-min))
9743 (goto-char (next-single-property-change (point) 'org-hd-marker))
9744 (while (and (re-search-forward regexp nil t)
9745 (setq txt-at-point (get-text-property (point) 'txt)))
9746 (when (string-match regexp txt-at-point)
9747 (setq entries-marked (1+ entries-marked))
9748 (call-interactively 'org-agenda-bulk-mark))))
9749 (if (not entries-marked)
9750 (message "No entry matching this regexp."))))
9752 (defun org-agenda-bulk-unmark (&optional arg)
9753 "Unmark the entry at point for future bulk action."
9754 (interactive "P")
9755 (if arg
9756 (org-agenda-bulk-unmark-all)
9757 (cond ((org-agenda-bulk-marked-p)
9758 (org-agenda-bulk-remove-overlays
9759 (point-at-bol) (+ 2 (point-at-bol)))
9760 (setq org-agenda-bulk-marked-entries
9761 (delete (org-get-at-bol 'org-hd-marker)
9762 org-agenda-bulk-marked-entries))
9763 (end-of-line 1)
9764 (or (ignore-errors
9765 (goto-char (next-single-property-change (point) 'txt)))
9766 (beginning-of-line 2))
9767 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9768 (beginning-of-line 2))
9769 (message "%d entries left marked for bulk action"
9770 (length org-agenda-bulk-marked-entries)))
9771 (t (message "No entry to unmark here")))))
9773 (defun org-agenda-bulk-toggle-all ()
9774 "Toggle all marks for bulk action."
9775 (interactive)
9776 (save-excursion
9777 (goto-char (point-min))
9778 (while (ignore-errors
9779 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9780 (org-agenda-bulk-toggle))))
9782 (defun org-agenda-bulk-toggle ()
9783 "Toggle the mark at point for bulk action."
9784 (interactive)
9785 (if (org-agenda-bulk-marked-p)
9786 (org-agenda-bulk-unmark)
9787 (org-agenda-bulk-mark)))
9789 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9790 "Remove the mark overlays between BEG and END in the agenda buffer.
9791 BEG and END default to the buffer limits.
9793 This only removes the overlays, it does not remove the markers
9794 from the list in `org-agenda-bulk-marked-entries'."
9795 (interactive)
9796 (mapc (lambda (ov)
9797 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9798 (delete-overlay ov)))
9799 (overlays-in (or beg (point-min)) (or end (point-max)))))
9801 (defun org-agenda-bulk-unmark-all ()
9802 "Remove all marks in the agenda buffer.
9803 This will remove the markers and the overlays."
9804 (interactive)
9805 (if (null org-agenda-bulk-marked-entries)
9806 (message "No entry to unmark")
9807 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9808 (setq org-agenda-bulk-marked-entries nil)
9809 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9811 (defcustom org-agenda-persistent-marks nil
9812 "Non-nil means marked items will stay marked after a bulk action.
9813 You can toggle this interactively by typing `p' when prompted for a
9814 bulk action."
9815 :group 'org-agenda
9816 :version "24.1"
9817 :type 'boolean)
9819 (defun org-agenda-bulk-action (&optional arg)
9820 "Execute an remote-editing action on all marked entries.
9821 The prefix arg is passed through to the command if possible."
9822 (interactive "P")
9823 ;; Make sure we have markers, and only valid ones
9824 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9825 (mapc
9826 (lambda (m)
9827 (unless (and (markerp m)
9828 (marker-buffer m)
9829 (buffer-live-p (marker-buffer m))
9830 (marker-position m))
9831 (user-error "Marker %s for bulk command is invalid" m)))
9832 org-agenda-bulk-marked-entries)
9834 ;; Prompt for the bulk command
9835 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9836 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9837 "[S]catter [f]unction "
9838 (when org-agenda-bulk-custom-functions
9839 (concat " Custom: ["
9840 (mapconcat (lambda(f) (char-to-string (car f)))
9841 org-agenda-bulk-custom-functions "")
9842 "]"))))
9843 (catch 'exit
9844 (let* ((action (read-char-exclusive))
9845 (org-log-refile (if org-log-refile 'time nil))
9846 (entries (reverse org-agenda-bulk-marked-entries))
9847 (org-overriding-default-time
9848 (if (get-text-property (point) 'org-agenda-date-header)
9849 (org-get-cursor-date)))
9850 redo-at-end
9851 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9852 (cond
9853 ((equal action ?p)
9854 (let ((org-agenda-persistent-marks
9855 (not org-agenda-persistent-marks)))
9856 (org-agenda-bulk-action)
9857 (throw 'exit nil)))
9859 ((equal action ?$)
9860 (setq cmd '(org-agenda-archive)))
9862 ((equal action ?A)
9863 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9865 ((member action '(?r ?w))
9866 (setq rfloc (org-refile-get-location
9867 "Refile to"
9868 (marker-buffer (car entries))
9869 org-refile-allow-creating-parent-nodes))
9870 (if (nth 3 rfloc)
9871 (setcar (nthcdr 3 rfloc)
9872 (move-marker (make-marker) (nth 3 rfloc)
9873 (or (get-file-buffer (nth 1 rfloc))
9874 (find-buffer-visiting (nth 1 rfloc))
9875 (error "This should not happen")))))
9877 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9878 redo-at-end t))
9880 ((equal action ?t)
9881 (setq state (org-icompleting-read
9882 "Todo state: "
9883 (with-current-buffer (marker-buffer (car entries))
9884 (mapcar 'list org-todo-keywords-1))))
9885 (setq cmd `(let ((org-inhibit-blocking t)
9886 (org-inhibit-logging 'note))
9887 (org-agenda-todo ,state))))
9889 ((memq action '(?- ?+))
9890 (setq tag (org-icompleting-read
9891 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9892 (with-current-buffer (marker-buffer (car entries))
9893 (delq nil
9894 (mapcar (lambda (x)
9895 (if (stringp (car x)) x)) org-tag-alist)))))
9896 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9898 ((memq action '(?s ?d))
9899 (let* ((time
9900 (unless arg
9901 (org-read-date
9902 nil nil nil
9903 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9904 org-overriding-default-time)))
9905 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9906 (setq cmd `(eval '(,c1 arg ,time)))))
9908 ((equal action ?S)
9909 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9910 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9911 (let ((days (read-number
9912 (format "Scatter tasks across how many %sdays: "
9913 (if arg "week" "")) 7)))
9914 (setq cmd
9915 `(let ((distance (1+ (random ,days))))
9916 (if arg
9917 (let ((dist distance)
9918 (day-of-week
9919 (calendar-day-of-week
9920 (calendar-gregorian-from-absolute (org-today)))))
9921 (dotimes (i (1+ dist))
9922 (while (member day-of-week org-agenda-weekend-days)
9923 (incf distance)
9924 (incf day-of-week)
9925 (if (= day-of-week 7)
9926 (setq day-of-week 0)))
9927 (incf day-of-week)
9928 (if (= day-of-week 7)
9929 (setq day-of-week 0)))))
9930 ;; silently fail when try to replan a sexp entry
9931 (condition-case nil
9932 (let* ((date (calendar-gregorian-from-absolute
9933 (+ (org-today) distance)))
9934 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9935 (nth 2 date))))
9936 (org-agenda-schedule nil time))
9937 (error nil)))))))
9939 ((assoc action org-agenda-bulk-custom-functions)
9940 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9941 redo-at-end t))
9943 ((equal action ?f)
9944 (setq cmd (list (intern
9945 (org-icompleting-read "Function: "
9946 obarray 'fboundp t nil nil)))))
9948 (t (user-error "Invalid bulk action")))
9950 ;; Sort the markers, to make sure that parents are handled before children
9951 (setq entries (sort entries
9952 (lambda (a b)
9953 (cond
9954 ((equal (marker-buffer a) (marker-buffer b))
9955 (< (marker-position a) (marker-position b)))
9957 (string< (buffer-name (marker-buffer a))
9958 (buffer-name (marker-buffer b))))))))
9960 ;; Now loop over all markers and apply cmd
9961 (while (setq e (pop entries))
9962 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9963 (if (not pos)
9964 (progn (message "Skipping removed entry at %s" e)
9965 (setq cntskip (1+ cntskip)))
9966 (goto-char pos)
9967 (let (org-loop-over-headlines-in-active-region)
9968 (eval cmd))
9969 ;; `post-command-hook' is not run yet. We make sure any
9970 ;; pending log note is processed.
9971 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
9972 (memq 'org-add-log-note post-command-hook))
9973 (org-add-log-note))
9974 (setq cnt (1+ cnt))))
9975 (when redo-at-end (org-agenda-redo))
9976 (unless org-agenda-persistent-marks
9977 (org-agenda-bulk-unmark-all))
9978 (message "Acted on %d entries%s%s"
9980 (if (= cntskip 0)
9982 (format ", skipped %d (disappeared before their turn)"
9983 cntskip))
9984 (if (not org-agenda-persistent-marks)
9985 "" " (kept marked)"))))))
9987 (defun org-agenda-capture (&optional with-time)
9988 "Call `org-capture' with the date at point.
9989 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9990 current HH:MM time."
9991 (interactive "P")
9992 (if (not (eq major-mode 'org-agenda-mode))
9993 (user-error "You cannot do this outside of agenda buffers")
9994 (let ((org-overriding-default-time
9995 (org-get-cursor-date (equal with-time 1))))
9996 (call-interactively 'org-capture))))
9998 ;;; Dragging agenda lines forward/backward
10000 (defun org-agenda-reapply-filters ()
10001 "Re-apply all agenda filters."
10002 (mapcar
10003 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10004 `((,org-agenda-tag-filter tag)
10005 (,org-agenda-category-filter category)
10006 (,org-agenda-regexp-filter regexp)
10007 (,org-agenda-effort-filter effort)
10008 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10009 (,(get 'org-agenda-category-filter :preset-filter) category)
10010 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10011 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10013 (defun org-agenda-drag-line-forward (arg &optional backward)
10014 "Drag an agenda line forward by ARG lines.
10015 When the optional argument `backward' is non-nil, move backward."
10016 (interactive "p")
10017 (let ((inhibit-read-only t) lst line)
10018 (if (or (not (get-text-property (point) 'txt))
10019 (save-excursion
10020 (dotimes (n arg)
10021 (move-beginning-of-line (if backward 0 2))
10022 (push (not (get-text-property (point) 'txt)) lst))
10023 (delq nil lst)))
10024 (message "Cannot move line forward")
10025 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10026 (move-beginning-of-line 1)
10027 (setq line (buffer-substring (point) end))
10028 (delete-region (point) end)
10029 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10030 (insert line)
10031 (org-agenda-reapply-filters)
10032 (org-agenda-mark-clocking-task)
10033 (move-beginning-of-line 0)))))
10035 (defun org-agenda-drag-line-backward (arg)
10036 "Drag an agenda line backward by ARG lines."
10037 (interactive "p")
10038 (org-agenda-drag-line-forward arg t))
10040 ;;; Flagging notes
10042 (defun org-agenda-show-the-flagging-note ()
10043 "Display the flagging note in the other window.
10044 When called a second time in direct sequence, offer to remove the FLAGGING
10045 tag and (if present) the flagging note."
10046 (interactive)
10047 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10048 (win (selected-window))
10049 note heading newhead)
10050 (unless hdmarker
10051 (user-error "No linked entry at point"))
10052 (if (and (eq this-command last-command)
10053 (y-or-n-p "Unflag and remove any flagging note? "))
10054 (progn
10055 (org-agenda-remove-flag hdmarker)
10056 (let ((win (get-buffer-window "*Flagging Note*")))
10057 (and win (delete-window win)))
10058 (message "Entry unflagged"))
10059 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10060 (unless note
10061 (user-error "No flagging note"))
10062 (org-kill-new note)
10063 (org-switch-to-buffer-other-window "*Flagging Note*")
10064 (erase-buffer)
10065 (insert note)
10066 (goto-char (point-min))
10067 (while (re-search-forward "\\\\n" nil t)
10068 (replace-match "\n" t t))
10069 (goto-char (point-min))
10070 (select-window win)
10071 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10073 (defun org-agenda-remove-flag (marker)
10074 "Remove the FLAGGED tag and any flagging note in the entry."
10075 (let (newhead)
10076 (org-with-point-at marker
10077 (org-toggle-tag "FLAGGED" 'off)
10078 (org-entry-delete nil "THEFLAGGINGNOTE")
10079 (setq newhead (org-get-heading)))
10080 (org-agenda-change-all-lines newhead marker)
10081 (message "Entry unflagged")))
10083 (defun org-agenda-get-any-marker (&optional pos)
10084 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10085 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10087 ;;; Appointment reminders
10089 (defvar appt-time-msg-list) ; defined in appt.el
10091 ;;;###autoload
10092 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10093 "Activate appointments found in `org-agenda-files'.
10094 With a \\[universal-argument] prefix, refresh the list of
10095 appointments.
10097 If FILTER is t, interactively prompt the user for a regular
10098 expression, and filter out entries that don't match it.
10100 If FILTER is a string, use this string as a regular expression
10101 for filtering entries out.
10103 If FILTER is a function, filter out entries against which
10104 calling the function returns nil. This function takes one
10105 argument: an entry from `org-agenda-get-day-entries'.
10107 FILTER can also be an alist with the car of each cell being
10108 either 'headline or 'category. For example:
10110 '((headline \"IMPORTANT\")
10111 (category \"Work\"))
10113 will only add headlines containing IMPORTANT or headlines
10114 belonging to the \"Work\" category.
10116 ARGS are symbols indicating what kind of entries to consider.
10117 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10118 \(i.e., deadlines and scheduled items with a hh:mm specification)
10119 and :timestamp entries. See the docstring of `org-diary' for
10120 details and examples.
10122 If an entry has a APPT_WARNTIME property, its value will be used
10123 to override `appt-message-warning-time'."
10124 (interactive "P")
10125 (if refresh (setq appt-time-msg-list nil))
10126 (if (eq filter t)
10127 (setq filter (read-from-minibuffer "Regexp filter: ")))
10128 (let* ((cnt 0) ; count added events
10129 (scope (or args '(:deadline* :scheduled* :timestamp)))
10130 (org-agenda-new-buffers nil)
10131 (org-deadline-warning-days 0)
10132 ;; Do not use `org-today' here because appt only takes
10133 ;; time and without date as argument, so it may pass wrong
10134 ;; information otherwise
10135 (today (org-date-to-gregorian
10136 (time-to-days (current-time))))
10137 (org-agenda-restrict nil)
10138 (files (org-agenda-files 'unrestricted)) entries file
10139 (org-agenda-buffer nil))
10140 ;; Get all entries which may contain an appt
10141 (org-agenda-prepare-buffers files)
10142 (while (setq file (pop files))
10143 (setq entries
10144 (delq nil
10145 (append entries
10146 (apply 'org-agenda-get-day-entries
10147 file today scope)))))
10148 ;; Map thru entries and find if we should filter them out
10149 (mapc
10150 (lambda(x)
10151 (let* ((evt (org-trim
10152 (replace-regexp-in-string
10153 org-bracket-link-regexp "\\3"
10154 (or (get-text-property 1 'txt x) ""))))
10155 (cat (get-text-property (1- (length x)) 'org-category x))
10156 (tod (get-text-property 1 'time-of-day x))
10157 (ok (or (null filter)
10158 (and (stringp filter) (string-match filter evt))
10159 (and (functionp filter) (funcall filter x))
10160 (and (listp filter)
10161 (let ((cat-filter (cadr (assoc 'category filter)))
10162 (evt-filter (cadr (assoc 'headline filter))))
10163 (or (and (stringp cat-filter)
10164 (string-match cat-filter cat))
10165 (and (stringp evt-filter)
10166 (string-match evt-filter evt)))))))
10167 (wrn (get-text-property 1 'warntime x)))
10168 ;; FIXME: Shall we remove text-properties for the appt text?
10169 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10170 (when (and ok tod)
10171 (setq tod (concat "00" (number-to-string tod))
10172 tod (when (string-match
10173 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10174 (concat (match-string 1 tod) ":"
10175 (match-string 2 tod))))
10176 (if (version< emacs-version "23.3")
10177 (appt-add tod evt)
10178 (appt-add tod evt wrn))
10179 (setq cnt (1+ cnt))))) entries)
10180 (org-release-buffers org-agenda-new-buffers)
10181 (if (eq cnt 0)
10182 (message "No event to add")
10183 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10185 (defun org-agenda-todayp (date)
10186 "Does DATE mean today, when considering `org-extend-today-until'?"
10187 (let ((today (org-today))
10188 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10189 date)))
10190 (eq date today)))
10192 (defun org-agenda-todo-yesterday (&optional arg)
10193 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10194 (interactive "P")
10195 (let* ((org-use-effective-time t)
10196 (hour (third (decode-time
10197 (org-current-time))))
10198 (org-extend-today-until (1+ hour)))
10199 (org-agenda-todo arg)))
10201 (provide 'org-agenda)
10203 ;;; org-agenda.el ends here