Fix 3072cb2
[org-mode.git] / lisp / org-agenda.el
blobe925ba1163086f38d71ccc422a63ee8c508fe63d
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 TODOs" ((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 \\<org-agenda-mode-map>\
1296 This list may contain the following symbols:
1298 closed Show entries that have been closed on that day.
1299 clock Show entries that have received clocked time on that day.
1300 state Show all logged state changes.
1301 Note that instead of changing this variable, you can also press \
1302 `\\[universal-argument] \\[org-agenda-log-mode]' in
1303 the agenda to display all available LOG items temporarily."
1304 :group 'org-agenda-daily/weekly
1305 :type '(set :greedy t (const closed) (const clock) (const state)))
1307 (defcustom org-agenda-clock-consistency-checks
1308 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1309 :gap-ok-around ("4:00")
1310 :default-face ((:background "DarkRed") (:foreground "white"))
1311 :overlap-face nil :gap-face nil :no-end-time-face nil
1312 :long-face nil :short-face nil)
1313 "This is a property list, with the following keys:
1315 :max-duration Mark clocking chunks that are longer than this time.
1316 This is a time string like \"HH:MM\", or the number
1317 of minutes as an integer.
1319 :min-duration Mark clocking chunks that are shorter that this.
1320 This is a time string like \"HH:MM\", or the number
1321 of minutes as an integer.
1323 :max-gap Mark gaps between clocking chunks that are longer than
1324 this duration. A number of minutes, or a string
1325 like \"HH:MM\".
1327 :gap-ok-around List of times during the day which are usually not working
1328 times. When a gap is detected, but the gap contains any
1329 of these times, the gap is *not* reported. For example,
1330 if this is (\"4:00\" \"13:00\") then gaps that contain
1331 4:00 in the morning (i.e. the night) and 13:00
1332 (i.e. a typical lunch time) do not cause a warning.
1333 You should have at least one time during the night in this
1334 list, or otherwise the first task each morning will trigger
1335 a warning because it follows a long gap.
1337 Furthermore, the following properties can be used to define faces for
1338 issue display.
1340 :default-face the default face, if the specific face is undefined
1341 :overlap-face face for overlapping clocks
1342 :gap-face face for gaps between clocks
1343 :no-end-time-face face for incomplete clocks
1344 :long-face face for clock intervals that are too long
1345 :short-face face for clock intervals that are too short"
1346 :group 'org-agenda-daily/weekly
1347 :group 'org-clock
1348 :version "24.1"
1349 :type 'plist)
1351 (defcustom org-agenda-log-mode-add-notes t
1352 "Non-nil means add first line of notes to log entries in agenda views.
1353 If a log item like a state change or a clock entry is associated with
1354 notes, the first line of these notes will be added to the entry in the
1355 agenda display."
1356 :group 'org-agenda-daily/weekly
1357 :type 'boolean)
1359 (defcustom org-agenda-start-with-log-mode nil
1360 "The initial value of log-mode in a newly created agenda window.
1361 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1362 explanations on the possible values."
1363 :group 'org-agenda-startup
1364 :group 'org-agenda-daily/weekly
1365 :type '(choice (const :tag "Don't show log items" nil)
1366 (const :tag "Show only log items" only)
1367 (const :tag "Show all possible log items" clockcheck)
1368 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1369 (choice (const :tag "Show closed log items" closed)
1370 (const :tag "Show clocked log items" clock)
1371 (const :tag "Show all logged state changes" state)))))
1373 (defcustom org-agenda-start-with-clockreport-mode nil
1374 "The initial value of clockreport-mode in a newly created agenda window."
1375 :group 'org-agenda-startup
1376 :group 'org-agenda-daily/weekly
1377 :type 'boolean)
1379 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1380 "Property list with parameters for the clocktable in clockreport mode.
1381 This is the display mode that shows a clock table in the daily/weekly
1382 agenda, the properties for this dynamic block can be set here.
1383 The usual clocktable parameters are allowed here, but you cannot set
1384 the properties :name, :tstart, :tend, :block, and :scope - these will
1385 be overwritten to make sure the content accurately reflects the
1386 current display in the agenda."
1387 :group 'org-agenda-daily/weekly
1388 :type 'plist)
1390 (defcustom org-agenda-search-view-always-boolean nil
1391 "Non-nil means the search string is interpreted as individual parts.
1393 The search string for search view can either be interpreted as a phrase,
1394 or as a list of snippets that define a boolean search for a number of
1395 strings.
1397 When this is non-nil, the string will be split on whitespace, and each
1398 snippet will be searched individually, and all must match in order to
1399 select an entry. A snippet is then a single string of non-white
1400 characters, or a string in double quotes, or a regexp in {} braces.
1401 If a snippet is preceded by \"-\", the snippet must *not* match.
1402 \"+\" is syntactic sugar for positive selection. Each snippet may
1403 be found as a full word or a partial word, but see the variable
1404 `org-agenda-search-view-force-full-words'.
1406 When this is nil, search will look for the entire search phrase as one,
1407 with each space character matching any amount of whitespace, including
1408 line breaks.
1410 Even when this is nil, you can still switch to Boolean search dynamically
1411 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1412 is a regexp marked with braces like \"{abc}\", this will also switch to
1413 boolean search."
1414 :group 'org-agenda-search-view
1415 :version "24.1"
1416 :type 'boolean)
1418 (org-defvaralias 'org-agenda-search-view-search-words-only
1419 'org-agenda-search-view-always-boolean)
1421 (defcustom org-agenda-search-view-force-full-words nil
1422 "Non-nil means, search words must be matches as complete words.
1423 When nil, they may also match part of a word."
1424 :group 'org-agenda-search-view
1425 :version "24.1"
1426 :type 'boolean)
1428 (defcustom org-agenda-search-view-max-outline-level 0
1429 "Maximum outline level to display in search view.
1430 E.g. when this is set to 1, the search view will only
1431 show headlines of level 1. When set to 0, the default
1432 value, don't limit agenda view by outline level."
1433 :group 'org-agenda-search-view
1434 :version "24.4"
1435 :package-version '(Org . "8.3")
1436 :type 'integer)
1438 (defgroup org-agenda-time-grid nil
1439 "Options concerning the time grid in the Org-mode Agenda."
1440 :tag "Org Agenda Time Grid"
1441 :group 'org-agenda)
1443 (defcustom org-agenda-search-headline-for-time t
1444 "Non-nil means search headline for a time-of-day.
1445 If the headline contains a time-of-day in one format or another, it will
1446 be used to sort the entry into the time sequence of items for a day.
1447 Some people have time stamps in the headline that refer to the creation
1448 time or so, and then this produces an unwanted side effect. If this is
1449 the case for your, use this variable to turn off searching the headline
1450 for a time."
1451 :group 'org-agenda-time-grid
1452 :type 'boolean)
1454 (defcustom org-agenda-use-time-grid t
1455 "Non-nil means show a time grid in the agenda schedule.
1456 A time grid is a set of lines for specific times (like every two hours between
1457 8:00 and 20:00). The items scheduled for a day at specific times are
1458 sorted in between these lines.
1459 For details about when the grid will be shown, and what it will look like, see
1460 the variable `org-agenda-time-grid'."
1461 :group 'org-agenda-time-grid
1462 :type 'boolean)
1464 (defcustom org-agenda-time-grid
1465 '((daily today require-timed)
1466 "----------------"
1467 (800 1000 1200 1400 1600 1800 2000))
1469 "The settings for time grid for agenda display.
1470 This is a list of three items. The first item is again a list. It contains
1471 symbols specifying conditions when the grid should be displayed:
1473 daily if the agenda shows a single day
1474 weekly if the agenda shows an entire week
1475 today show grid on current date, independent of daily/weekly display
1476 require-timed show grid only if at least one item has a time specification
1477 remove-match skip grid times already present in an entry
1479 The second item is a string which will be placed behind the grid time.
1481 The third item is a list of integers, indicating the times that should have
1482 a grid line."
1483 :group 'org-agenda-time-grid
1484 :type
1485 '(list
1486 (set :greedy t :tag "Grid Display Options"
1487 (const :tag "Show grid in single day agenda display" daily)
1488 (const :tag "Show grid in weekly agenda display" weekly)
1489 (const :tag "Always show grid for today" today)
1490 (const :tag "Show grid only if any timed entries are present"
1491 require-timed)
1492 (const :tag "Skip grid times already present in an entry"
1493 remove-match))
1494 (string :tag "Grid String")
1495 (repeat :tag "Grid Times" (integer :tag "Time"))))
1497 (defcustom org-agenda-show-current-time-in-grid t
1498 "Non-nil means show the current time in the time grid."
1499 :group 'org-agenda-time-grid
1500 :version "24.1"
1501 :type 'boolean)
1503 (defcustom org-agenda-current-time-string
1504 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1505 "The string for the current time marker in the agenda."
1506 :group 'org-agenda-time-grid
1507 :version "24.1"
1508 :type 'string)
1510 (defgroup org-agenda-sorting nil
1511 "Options concerning sorting in the Org-mode Agenda."
1512 :tag "Org Agenda Sorting"
1513 :group 'org-agenda)
1515 (defcustom org-agenda-sorting-strategy
1516 '((agenda habit-down time-up priority-down category-keep)
1517 (todo priority-down category-keep)
1518 (tags priority-down category-keep)
1519 (search category-keep))
1520 "Sorting structure for the agenda items of a single day.
1521 This is a list of symbols which will be used in sequence to determine
1522 if an entry should be listed before another entry. The following
1523 symbols are recognized:
1525 time-up Put entries with time-of-day indications first, early first
1526 time-down Put entries with time-of-day indications first, late first
1527 timestamp-up Sort by any timestamp, early first
1528 timestamp-down Sort by any timestamp, late first
1529 scheduled-up Sort by scheduled timestamp, early first
1530 scheduled-down Sort by scheduled timestamp, late first
1531 deadline-up Sort by deadline timestamp, early first
1532 deadline-down Sort by deadline timestamp, late first
1533 ts-up Sort by active timestamp, early first
1534 ts-down Sort by active timestamp, late first
1535 tsia-up Sort by inactive timestamp, early first
1536 tsia-down Sort by inactive timestamp, late first
1537 category-keep Keep the default order of categories, corresponding to the
1538 sequence in `org-agenda-files'.
1539 category-up Sort alphabetically by category, A-Z.
1540 category-down Sort alphabetically by category, Z-A.
1541 tag-up Sort alphabetically by last tag, A-Z.
1542 tag-down Sort alphabetically by last tag, Z-A.
1543 priority-up Sort numerically by priority, high priority last.
1544 priority-down Sort numerically by priority, high priority first.
1545 todo-state-up Sort by todo state, tasks that are done last.
1546 todo-state-down Sort by todo state, tasks that are done first.
1547 effort-up Sort numerically by estimated effort, high effort last.
1548 effort-down Sort numerically by estimated effort, high effort first.
1549 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1550 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1551 habit-up Put entries that are habits first
1552 habit-down Put entries that are habits last
1553 alpha-up Sort headlines alphabetically
1554 alpha-down Sort headlines alphabetically, reversed
1556 The different possibilities will be tried in sequence, and testing stops
1557 if one comparison returns a \"not-equal\". For example, the default
1558 '(time-up category-keep priority-down)
1559 means: Pull out all entries having a specified time of day and sort them,
1560 in order to make a time schedule for the current day the first thing in the
1561 agenda listing for the day. Of the entries without a time indication, keep
1562 the grouped in categories, don't sort the categories, but keep them in
1563 the sequence given in `org-agenda-files'. Within each category sort by
1564 priority.
1566 Leaving out `category-keep' would mean that items will be sorted across
1567 categories by priority.
1569 Instead of a single list, this can also be a set of list for specific
1570 contents, with a context symbol in the car of the list, any of
1571 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1573 Custom commands can bind this variable in the options section."
1574 :group 'org-agenda-sorting
1575 :type `(choice
1576 (repeat :tag "General" ,org-sorting-choice)
1577 (list :tag "Individually"
1578 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1579 (repeat ,org-sorting-choice))
1580 (cons (const :tag "Strategy for TODO lists" todo)
1581 (repeat ,org-sorting-choice))
1582 (cons (const :tag "Strategy for Tags matches" tags)
1583 (repeat ,org-sorting-choice))
1584 (cons (const :tag "Strategy for search matches" search)
1585 (repeat ,org-sorting-choice)))))
1587 (defcustom org-agenda-cmp-user-defined nil
1588 "A function to define the comparison `user-defined'.
1589 This function must receive two arguments, agenda entry a and b.
1590 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1591 the user comparison, return nil.
1592 When this is defined, you can make `user-defined-up' and `user-defined-down'
1593 part of an agenda sorting strategy."
1594 :group 'org-agenda-sorting
1595 :type 'symbol)
1597 (defcustom org-sort-agenda-notime-is-late t
1598 "Non-nil means items without time are considered late.
1599 This is only relevant for sorting. When t, items which have no explicit
1600 time like 15:30 will be considered as 99:01, i.e. later than any items which
1601 do have a time. When nil, the default time is before 0:00. You can use this
1602 option to decide if the schedule for today should come before or after timeless
1603 agenda entries."
1604 :group 'org-agenda-sorting
1605 :type 'boolean)
1607 (defcustom org-sort-agenda-noeffort-is-high t
1608 "Non-nil means items without effort estimate are sorted as high effort.
1609 This also applies when filtering an agenda view with respect to the
1610 < or > effort operator. Then, tasks with no effort defined will be treated
1611 as tasks with high effort.
1612 When nil, such items are sorted as 0 minutes effort."
1613 :group 'org-agenda-sorting
1614 :type 'boolean)
1616 (defgroup org-agenda-line-format nil
1617 "Options concerning the entry prefix in the Org-mode agenda display."
1618 :tag "Org Agenda Line Format"
1619 :group 'org-agenda)
1621 (defcustom org-agenda-prefix-format
1622 '((agenda . " %i %-12:c%?-12t% s")
1623 (timeline . " % s")
1624 (todo . " %i %-12:c")
1625 (tags . " %i %-12:c")
1626 (search . " %i %-12:c"))
1627 "Format specifications for the prefix of items in the agenda views.
1628 An alist with five entries, each for the different agenda types. The
1629 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1630 The values are format strings.
1632 This format works similar to a printf format, with the following meaning:
1634 %c the category of the item, \"Diary\" for entries from the diary,
1635 or as given by the CATEGORY keyword or derived from the file name
1636 %e the effort required by the item
1637 %l the level of the item (insert X space(s) if item is of level X)
1638 %i the icon category of the item, see `org-agenda-category-icon-alist'
1639 %T the last tag of the item (ignore inherited tags, which come first)
1640 %t the HH:MM time-of-day specification if one applies to the entry
1641 %s Scheduling/Deadline information, a short string
1642 %b show breadcrumbs, i.e., the names of the higher levels
1643 %(expression) Eval EXPRESSION and replace the control string
1644 by the result
1646 All specifiers work basically like the standard `%s' of printf, but may
1647 contain two additional characters: a question mark just after the `%'
1648 and a whitespace/punctuation character just before the final letter.
1650 If the first character after `%' is a question mark, the entire field
1651 will only be included if the corresponding value applies to the current
1652 entry. This is useful for fields which should have fixed width when
1653 present, but zero width when absent. For example, \"%?-12t\" will
1654 result in a 12 character time field if a time of the day is specified,
1655 but will completely disappear in entries which do not contain a time.
1657 If there is punctuation or whitespace character just before the
1658 final format letter, this character will be appended to the field
1659 value if the value is not empty. For example, the format
1660 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1661 the category is empty, no additional colon is inserted.
1663 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1664 which means:
1666 - Indent the line with two space characters
1667 - Give the category a 12 chars wide field, padded with whitespace on
1668 the right (because of `-'). Append a colon if there is a category
1669 (because of `:').
1670 - If there is a time-of-day, put it into a 12 chars wide field. If no
1671 time, don't put in an empty field, just skip it (because of '?').
1672 - Finally, put the scheduling information.
1674 See also the variables `org-agenda-remove-times-when-in-prefix' and
1675 `org-agenda-remove-tags'.
1677 Custom commands can set this variable in the options section."
1678 :type '(choice
1679 (string :tag "General format")
1680 (list :greedy t :tag "View dependent"
1681 (cons (const agenda) (string :tag "Format"))
1682 (cons (const timeline) (string :tag "Format"))
1683 (cons (const todo) (string :tag "Format"))
1684 (cons (const tags) (string :tag "Format"))
1685 (cons (const search) (string :tag "Format"))))
1686 :group 'org-agenda-line-format)
1688 (defvar org-prefix-format-compiled nil
1689 "The compiled prefix format and associated variables.
1690 This is a list where first element is a list of variable bindings, and second
1691 element is the compiled format expression. See the variable
1692 `org-agenda-prefix-format'.")
1694 (defcustom org-agenda-todo-keyword-format "%-1s"
1695 "Format for the TODO keyword in agenda lines.
1696 Set this to something like \"%-12s\" if you want all TODO keywords
1697 to occupy a fixed space in the agenda display."
1698 :group 'org-agenda-line-format
1699 :type 'string)
1701 (defcustom org-agenda-diary-sexp-prefix nil
1702 "A regexp that matches part of a diary sexp entry
1703 which should be treated as scheduling/deadline information in
1704 `org-agenda'.
1706 For example, you can use this to extract the `diary-remind-message' from
1707 `diary-remind' entries."
1708 :group 'org-agenda-line-format
1709 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1711 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1712 "Text preceding timerange entries in the agenda view.
1713 This is a list with two strings. The first applies when the range
1714 is entirely on one day. The second applies if the range spans several days.
1715 The strings may have two \"%d\" format specifiers which will be filled
1716 with the sequence number of the days, and the total number of days in the
1717 range, respectively."
1718 :group 'org-agenda-line-format
1719 :type '(list
1720 (string :tag "Deadline today ")
1721 (choice :tag "Deadline relative"
1722 (string :tag "Format string")
1723 (function))))
1725 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1726 "Text preceding scheduled items in the agenda view.
1727 This is a list with two strings. The first applies when the item is
1728 scheduled on the current day. The second applies when it has been scheduled
1729 previously, it may contain a %d indicating that this is the nth time that
1730 this item is scheduled, due to automatic rescheduling of unfinished items
1731 for the following day. So this number is one larger than the number of days
1732 that passed since this item was scheduled first."
1733 :group 'org-agenda-line-format
1734 :version "24.4"
1735 :package-version '(Org . "8.0")
1736 :type '(list
1737 (string :tag "Scheduled today ")
1738 (string :tag "Scheduled previously")))
1740 (defcustom org-agenda-inactive-leader "["
1741 "Text preceding item pulled into the agenda by inactive time stamps.
1742 These entries are added to the agenda when pressing \"[\"."
1743 :group 'org-agenda-line-format
1744 :version "24.1"
1745 :type 'string)
1747 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1748 "Text preceding deadline items in the agenda view.
1749 This is a list with three strings. The first applies when the item has its
1750 deadline on the current day. The second applies when the deadline is in the
1751 future, the third one when it is in the past. The strings may contain %d
1752 to capture the number of days."
1753 :group 'org-agenda-line-format
1754 :version "24.4"
1755 :package-version '(Org . "8.0")
1756 :type '(list
1757 (string :tag "Deadline today ")
1758 (string :tag "Deadline in the future ")
1759 (string :tag "Deadline in the past ")))
1761 (defcustom org-agenda-remove-times-when-in-prefix t
1762 "Non-nil means remove duplicate time specifications in agenda items.
1763 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1764 time-of-day specification in a headline or diary entry is extracted and
1765 placed into the prefix. If this option is non-nil, the original specification
1766 \(a timestamp or -range, or just a plain time(range) specification like
1767 11:30-4pm) will be removed for agenda display. This makes the agenda less
1768 cluttered.
1769 The option can be t or nil. It may also be the symbol `beg', indicating
1770 that the time should only be removed when it is located at the beginning of
1771 the headline/diary entry."
1772 :group 'org-agenda-line-format
1773 :type '(choice
1774 (const :tag "Always" t)
1775 (const :tag "Never" nil)
1776 (const :tag "When at beginning of entry" beg)))
1778 (defcustom org-agenda-remove-timeranges-from-blocks nil
1779 "Non-nil means remove time ranges specifications in agenda
1780 items that span on several days."
1781 :group 'org-agenda-line-format
1782 :version "24.1"
1783 :type 'boolean)
1785 (defcustom org-agenda-default-appointment-duration nil
1786 "Default duration for appointments that only have a starting time.
1787 When nil, no duration is specified in such cases.
1788 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1789 :group 'org-agenda-line-format
1790 :type '(choice
1791 (integer :tag "Minutes")
1792 (const :tag "No default duration")))
1794 (defcustom org-agenda-show-inherited-tags t
1795 "Non-nil means show inherited tags in each agenda line.
1797 When this option is set to `always', it take precedences over
1798 `org-agenda-use-tag-inheritance' and inherited tags are shown
1799 in every agenda.
1801 When this option is set to t (the default), inherited tags are
1802 shown when they are available, i.e. when the value of
1803 `org-agenda-use-tag-inheritance' has been taken into account.
1805 This can be set to a list of agenda types in which the agenda
1806 must display the inherited tags. Available types are `todo',
1807 `agenda', `search' and `timeline'.
1809 When set to nil, never show inherited tags in agenda lines."
1810 :group 'org-agenda-line-format
1811 :group 'org-agenda
1812 :version "24.3"
1813 :type '(choice
1814 (const :tag "Show inherited tags when available" t)
1815 (const :tag "Always show inherited tags" always)
1816 (repeat :tag "Show inherited tags only in selected agenda types"
1817 (symbol :tag "Agenda type"))))
1819 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1820 "List of agenda view types where to use tag inheritance.
1822 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1823 controlled by `org-use-tag-inheritance'. In other agenda types,
1824 `org-use-tag-inheritance' is not used for the selection of the
1825 agenda entries. Still, you may want the agenda to be aware of
1826 the inherited tags anyway, e.g. for later tag filtering.
1828 Allowed value are `todo', `search', `timeline' and `agenda'.
1830 This variable has no effect if `org-agenda-show-inherited-tags'
1831 is set to `always'. In that case, the agenda is aware of those
1832 tags.
1834 The default value sets tags in every agenda type. Setting this
1835 option to nil will speed up non-tags agenda view a lot."
1836 :group 'org-agenda
1837 :version "24.3"
1838 :type '(choice
1839 (const :tag "Use tag inheritance in all agenda types" t)
1840 (repeat :tag "Use tag inheritance in selected agenda types"
1841 (symbol :tag "Agenda type"))))
1843 (defcustom org-agenda-hide-tags-regexp nil
1844 "Regular expression used to filter away specific tags in agenda views.
1845 This means that these tags will be present, but not be shown in the agenda
1846 line. Secondary filtering will still work on the hidden tags.
1847 Nil means don't hide any tags."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Hide none" nil)
1851 (string :tag "Regexp ")))
1853 (defcustom org-agenda-remove-tags nil
1854 "Non-nil means remove the tags from the headline copy in the agenda.
1855 When this is the symbol `prefix', only remove tags when
1856 `org-agenda-prefix-format' contains a `%T' specifier."
1857 :group 'org-agenda-line-format
1858 :type '(choice
1859 (const :tag "Always" t)
1860 (const :tag "Never" nil)
1861 (const :tag "When prefix format contains %T" prefix)))
1863 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1864 'org-agenda-remove-tags)
1866 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1867 "Shift tags in agenda items to this column.
1868 If this number is positive, it specifies the column. If it is negative,
1869 it means that the tags should be flushright to that column. For example,
1870 -80 works well for a normal 80 character screen."
1871 :group 'org-agenda-line-format
1872 :type 'integer)
1874 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1876 (defcustom org-agenda-fontify-priorities 'cookies
1877 "Non-nil means highlight low and high priorities in agenda.
1878 When t, the highest priority entries are bold, lowest priority italic.
1879 However, settings in `org-priority-faces' will overrule these faces.
1880 When this variable is the symbol `cookies', only fontify the
1881 cookies, not the entire task.
1882 This may also be an association list of priority faces, whose
1883 keys are the character values of `org-highest-priority',
1884 `org-default-priority', and `org-lowest-priority' (the default values
1885 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1886 color as a string, or a list like `(:background \"Red\")'.
1887 If it is a color, the variable `org-faces-easy-properties'
1888 determines if it is a foreground or a background color."
1889 :group 'org-agenda-line-format
1890 :type '(choice
1891 (const :tag "Never" nil)
1892 (const :tag "Defaults" t)
1893 (const :tag "Cookies only" cookies)
1894 (repeat :tag "Specify"
1895 (list (character :tag "Priority" :value ?A)
1896 (choice :tag "Face "
1897 (string :tag "Color")
1898 (sexp :tag "Face"))))))
1900 (defcustom org-agenda-day-face-function nil
1901 "Function called to determine what face should be used to display a day.
1902 The only argument passed to that function is the day. It should
1903 returns a face, or nil if does not want to specify a face and let
1904 the normal rules apply."
1905 :group 'org-agenda-line-format
1906 :version "24.1"
1907 :type '(choice (const nil) (function)))
1909 (defcustom org-agenda-category-icon-alist nil
1910 "Alist of category icon to be displayed in agenda views.
1912 Each entry should have the following format:
1914 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1916 Where CATEGORY-REGEXP is a regexp matching the categories where
1917 the icon should be displayed.
1918 FILE-OR-DATA either a file path or a string containing image data.
1920 The other fields can be omitted safely if not needed:
1921 TYPE indicates the image type.
1922 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1923 image data.
1924 PROPS are additional image attributes to assign to the image,
1925 like, e.g. `:ascent center'.
1927 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1929 If you want to set the display properties yourself, just put a
1930 list as second element:
1932 (CATEGORY-REGEXP (MY PROPERTY LIST))
1934 For example, to display a 16px horizontal space for Emacs
1935 category, you can use:
1937 (\"Emacs\" \\='(space . (:width (16))))"
1938 :group 'org-agenda-line-format
1939 :version "24.1"
1940 :type '(alist :key-type (string :tag "Regexp matching category")
1941 :value-type (choice (list :tag "Icon"
1942 (string :tag "File or data")
1943 (symbol :tag "Type")
1944 (boolean :tag "Data?")
1945 (repeat :tag "Extra image properties" :inline t symbol))
1946 (list :tag "Display properties" sexp))))
1948 (defgroup org-agenda-column-view nil
1949 "Options concerning column view in the agenda."
1950 :tag "Org Agenda Column View"
1951 :group 'org-agenda)
1953 (defcustom org-agenda-columns-show-summaries t
1954 "Non-nil means show summaries for columns displayed in the agenda view."
1955 :group 'org-agenda-column-view
1956 :type 'boolean)
1958 (defcustom org-agenda-columns-compute-summary-properties t
1959 "Non-nil means recompute all summary properties before column view.
1960 When column view in the agenda is listing properties that have a summary
1961 operator, it can go to all relevant buffers and recompute the summaries
1962 there. This can mean overhead for the agenda column view, but is necessary
1963 to have thing up to date.
1964 As a special case, a CLOCKSUM property also makes sure that the clock
1965 computations are current."
1966 :group 'org-agenda-column-view
1967 :type 'boolean)
1969 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1970 "Non-nil means the duration of an appointment will add to day effort.
1971 The property to which appointment durations will be added is the one given
1972 in the option `org-effort-property'. If an appointment does not have
1973 an end time, `org-agenda-default-appointment-duration' will be used. If that
1974 is not set, an appointment without end time will not contribute to the time
1975 estimate."
1976 :group 'org-agenda-column-view
1977 :type 'boolean)
1979 (defcustom org-agenda-auto-exclude-function nil
1980 "A function called with a tag to decide if it is filtered on \
1981 \\<org-agenda-mode-map>`\\[org-agenda-filter-by-tag] RET'.
1982 The sole argument to the function, which is called once for each
1983 possible tag, is a string giving the name of the tag. The
1984 function should return either nil if the tag should be included
1985 as normal, or \"-<TAG>\" to exclude the tag.
1986 Note that for the purpose of tag filtering, only the lower-case version of
1987 all tags will be considered, so that this function will only ever see
1988 the lower-case version of all tags."
1989 :group 'org-agenda
1990 :type '(choice (const nil) (function)))
1992 (defcustom org-agenda-bulk-custom-functions nil
1993 "Alist of characters and custom functions for bulk actions.
1994 For example, this value makes those two functions available:
1996 \\='((?R set-category)
1997 (?C bulk-cut))
1999 With selected entries in an agenda buffer, `B R' will call
2000 the custom function `set-category' on the selected entries.
2001 Note that functions in this alist don't need to be quoted."
2002 :type 'alist
2003 :version "24.1"
2004 :group 'org-agenda)
2006 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2007 "Execute BODY with point at location given by `org-hd-marker' property.
2008 If STRING is non-nil, the text property will be fetched from position 0
2009 in that string. If STRING is nil, it will be fetched from the beginning
2010 of the current line."
2011 (org-with-gensyms (marker)
2012 `(let ((,marker (get-text-property (if ,string 0 (point-at-bol))
2013 'org-hd-marker ,string)))
2014 (with-current-buffer (marker-buffer ,marker)
2015 (save-excursion
2016 (goto-char ,marker)
2017 ,@body)))))
2018 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2020 (defun org-add-agenda-custom-command (entry)
2021 "Replace or add a command in `org-agenda-custom-commands'.
2022 This is mostly for hacking and trying a new command - once the command
2023 works you probably want to add it to `org-agenda-custom-commands' for good."
2024 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2025 (if ass
2026 (setcdr ass (cdr entry))
2027 (push entry org-agenda-custom-commands))))
2029 ;;; Define the org-agenda-mode
2031 (defvar org-agenda-mode-map (make-sparse-keymap)
2032 "Keymap for `org-agenda-mode'.")
2033 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2035 (defvar org-agenda-menu) ; defined later in this file.
2036 (defvar org-agenda-restrict nil) ; defined later in this file.
2037 (defvar org-agenda-follow-mode nil)
2038 (defvar org-agenda-entry-text-mode nil)
2039 (defvar org-agenda-clockreport-mode nil)
2040 (defvar org-agenda-show-log nil)
2041 (defvar org-agenda-redo-command nil)
2042 (defvar org-agenda-query-string nil)
2043 (defvar org-agenda-mode-hook nil
2044 "Hook run after `org-agenda-mode' is turned on.
2045 The buffer is still writable when this hook is called.")
2046 (defvar org-agenda-type nil)
2047 (defvar org-agenda-force-single-file nil)
2048 (defvar org-agenda-bulk-marked-entries nil
2049 "List of markers that refer to marked entries in the agenda.")
2051 ;;; Multiple agenda buffers support
2053 (defcustom org-agenda-sticky nil
2054 "Non-nil means agenda q key will bury agenda buffers.
2055 Agenda commands will then show existing buffer instead of generating new ones.
2056 When nil, `q' will kill the single agenda buffer."
2057 :group 'org-agenda
2058 :version "24.3"
2059 :type 'boolean)
2062 ;;;###autoload
2063 (defun org-toggle-sticky-agenda (&optional arg)
2064 "Toggle `org-agenda-sticky'."
2065 (interactive "P")
2066 (let ((new-value (if arg
2067 (> (prefix-numeric-value arg) 0)
2068 (not org-agenda-sticky))))
2069 (if (equal new-value org-agenda-sticky)
2070 (and (org-called-interactively-p 'interactive)
2071 (message "Sticky agenda was already %s"
2072 (if org-agenda-sticky "enabled" "disabled")))
2073 (setq org-agenda-sticky new-value)
2074 (org-agenda-kill-all-agenda-buffers)
2075 (and (org-called-interactively-p 'interactive)
2076 (message "Sticky agenda %s"
2077 (if org-agenda-sticky "enabled" "disabled"))))))
2079 (defvar org-agenda-buffer nil
2080 "Agenda buffer currently being generated.")
2082 (defvar org-agenda-last-prefix-arg nil)
2083 (defvar org-agenda-this-buffer-name nil)
2084 (defvar org-agenda-doing-sticky-redo nil)
2085 (defvar org-agenda-this-buffer-is-sticky nil)
2086 (defvar org-agenda-last-indirect-buffer nil
2087 "Last buffer loaded by `org-agenda-tree-to-indirect-buffer'.")
2089 (defconst org-agenda-local-vars
2090 '(org-agenda-this-buffer-name
2091 org-agenda-undo-list
2092 org-agenda-pending-undo-list
2093 org-agenda-follow-mode
2094 org-agenda-entry-text-mode
2095 org-agenda-clockreport-mode
2096 org-agenda-show-log
2097 org-agenda-redo-command
2098 org-agenda-query-string
2099 org-agenda-type
2100 org-agenda-bulk-marked-entries
2101 org-agenda-undo-has-started-in
2102 org-agenda-info
2103 org-agenda-pre-window-conf
2104 org-agenda-columns-active
2105 org-agenda-tag-filter
2106 org-agenda-category-filter
2107 org-agenda-top-headline-filter
2108 org-agenda-regexp-filter
2109 org-agenda-effort-filter
2110 org-agenda-markers
2111 org-agenda-last-search-view-search-was-boolean
2112 org-agenda-last-indirect-buffer
2113 org-agenda-filtered-by-category
2114 org-agenda-filter-form
2115 org-agenda-cycle-counter
2116 org-agenda-last-prefix-arg)
2117 "Variables that must be local in agenda buffers to allow multiple buffers.")
2119 (defun org-agenda-mode ()
2120 "Mode for time-sorted view on action items in Org-mode files.
2122 The following commands are available:
2124 \\{org-agenda-mode-map}"
2125 (interactive)
2126 (cond (org-agenda-doing-sticky-redo
2127 ;; Refreshing sticky agenda-buffer
2129 ;; Preserve the value of `org-agenda-local-vars' variables,
2130 ;; while letting `kill-all-local-variables' kill the rest
2131 (let ((save (buffer-local-variables)))
2132 (kill-all-local-variables)
2133 (mapc 'make-local-variable org-agenda-local-vars)
2134 (dolist (elem save)
2135 (let ((var (car elem))
2136 (val (cdr elem)))
2137 (when (and val
2138 (member var org-agenda-local-vars))
2139 (set var val)))))
2140 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2141 (org-agenda-sticky
2142 ;; Creating a sticky Agenda buffer for the first time
2143 (kill-all-local-variables)
2144 (mapc 'make-local-variable org-agenda-local-vars)
2145 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2147 ;; Creating a non-sticky agenda buffer
2148 (kill-all-local-variables)
2149 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2150 (setq org-agenda-undo-list nil
2151 org-agenda-pending-undo-list nil
2152 org-agenda-bulk-marked-entries nil)
2153 (setq major-mode 'org-agenda-mode)
2154 ;; Keep global-font-lock-mode from turning on font-lock-mode
2155 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2156 (setq mode-name "Org-Agenda")
2157 (setq indent-tabs-mode nil)
2158 (use-local-map org-agenda-mode-map)
2159 (easy-menu-add org-agenda-menu)
2160 (if org-startup-truncated (setq truncate-lines t))
2161 (org-set-local 'line-move-visual nil)
2162 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2163 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2164 ;; Make sure properties are removed when copying text
2165 (org-add-hook 'filter-buffer-substring-functions
2166 (lambda (fun start end delete)
2167 (substring-no-properties (funcall fun start end delete)))
2168 nil t)
2169 (unless org-agenda-keep-modes
2170 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2171 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2172 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2173 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2174 (add-to-invisibility-spec '(org-filtered))
2175 (add-to-invisibility-spec '(org-link))
2176 (easy-menu-change
2177 '("Agenda") "Agenda Files"
2178 (append
2179 (list
2180 (vector
2181 (if (get 'org-agenda-files 'org-restrict)
2182 "Restricted to single file"
2183 "Edit File List")
2184 '(org-edit-agenda-file-list)
2185 (not (get 'org-agenda-files 'org-restrict)))
2186 "--")
2187 (mapcar 'org-file-menu-entry (org-agenda-files))))
2188 (org-agenda-set-mode-name)
2189 (apply
2190 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2191 (list 'org-agenda-mode-hook)))
2193 (substitute-key-definition 'undo 'org-agenda-undo
2194 org-agenda-mode-map global-map)
2195 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2196 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2197 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2198 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2199 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2200 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2201 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2202 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2203 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2204 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2205 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2206 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2207 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2208 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2209 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2210 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2211 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2212 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2213 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2214 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2215 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2216 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2217 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2218 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2219 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2220 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2221 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2222 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2223 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2224 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2225 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2226 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2227 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2228 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2229 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2230 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2231 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2232 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2233 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2234 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2235 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2236 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2237 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2238 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2239 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2240 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2241 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2242 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2243 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2244 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2246 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2247 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2248 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2249 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2250 (while l (org-defkey org-agenda-mode-map
2251 (int-to-string (pop l)) 'digit-argument)))
2253 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2254 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2255 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2256 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2257 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2258 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2259 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2260 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2261 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2262 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2263 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2264 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2265 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2266 'org-clock-modify-effort-estimate)
2267 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2268 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2269 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2270 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2271 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2272 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2273 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2274 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2275 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2276 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2277 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2278 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2279 (substitute-key-definition 'next-line 'org-agenda-next-line
2280 org-agenda-mode-map global-map)
2281 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2282 org-agenda-mode-map global-map)
2283 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2284 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2285 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2286 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2287 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2288 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2289 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2290 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2291 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2292 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2293 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2294 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2295 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2296 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2298 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2299 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2300 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2301 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2302 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2303 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2304 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2305 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2306 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2307 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2308 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2309 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2310 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2311 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2312 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2314 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2315 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2316 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2317 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2318 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2319 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2320 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2321 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2322 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2323 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2324 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2325 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2326 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2327 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2328 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2329 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2331 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2332 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2334 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2335 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2337 (when org-agenda-mouse-1-follows-link
2338 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2339 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2340 '("Agenda"
2341 ("Agenda Files")
2342 "--"
2343 ("Agenda Dates"
2344 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2345 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2346 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2347 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2348 "--"
2349 ("View"
2350 ["Day View" org-agenda-day-view
2351 :active (org-agenda-check-type nil 'agenda)
2352 :style radio :selected (eq org-agenda-current-span 'day)
2353 :keys "v d (or just d)"]
2354 ["Week View" org-agenda-week-view
2355 :active (org-agenda-check-type nil 'agenda)
2356 :style radio :selected (eq org-agenda-current-span 'week)
2357 :keys "v w"]
2358 ["Fortnight View" org-agenda-fortnight-view
2359 :active (org-agenda-check-type nil 'agenda)
2360 :style radio :selected (eq org-agenda-current-span 'fortnight)
2361 :keys "v f"]
2362 ["Month View" org-agenda-month-view
2363 :active (org-agenda-check-type nil 'agenda)
2364 :style radio :selected (eq org-agenda-current-span 'month)
2365 :keys "v m"]
2366 ["Year View" org-agenda-year-view
2367 :active (org-agenda-check-type nil 'agenda)
2368 :style radio :selected (eq org-agenda-current-span 'year)
2369 :keys "v y"]
2370 "--"
2371 ["Include Diary" org-agenda-toggle-diary
2372 :style toggle :selected org-agenda-include-diary
2373 :active (org-agenda-check-type nil 'agenda)]
2374 ["Include Deadlines" org-agenda-toggle-deadlines
2375 :style toggle :selected org-agenda-include-deadlines
2376 :active (org-agenda-check-type nil 'agenda)]
2377 ["Use Time Grid" org-agenda-toggle-time-grid
2378 :style toggle :selected org-agenda-use-time-grid
2379 :active (org-agenda-check-type nil 'agenda)]
2380 "--"
2381 ["Show clock report" org-agenda-clockreport-mode
2382 :style toggle :selected org-agenda-clockreport-mode
2383 :active (org-agenda-check-type nil 'agenda)]
2384 ["Show some entry text" org-agenda-entry-text-mode
2385 :style toggle :selected org-agenda-entry-text-mode
2386 :active t]
2387 "--"
2388 ["Show Logbook entries" org-agenda-log-mode
2389 :style toggle :selected org-agenda-show-log
2390 :active (org-agenda-check-type nil 'agenda 'timeline)
2391 :keys "v l (or just l)"]
2392 ["Include archived trees" org-agenda-archives-mode
2393 :style toggle :selected org-agenda-archives-mode :active t
2394 :keys "v a"]
2395 ["Include archive files" (org-agenda-archives-mode t)
2396 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2397 :keys "v A"]
2398 "--"
2399 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2400 ["Write view to file" org-agenda-write t]
2401 ["Rebuild buffer" org-agenda-redo t]
2402 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2403 "--"
2404 ["Show original entry" org-agenda-show t]
2405 ["Go To (other window)" org-agenda-goto t]
2406 ["Go To (this window)" org-agenda-switch-to t]
2407 ["Capture with cursor date" org-agenda-capture t]
2408 ["Follow Mode" org-agenda-follow-mode
2409 :style toggle :selected org-agenda-follow-mode :active t]
2410 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2411 "--"
2412 ("TODO"
2413 ["Cycle TODO" org-agenda-todo t]
2414 ["Next TODO set" org-agenda-todo-nextset t]
2415 ["Previous TODO set" org-agenda-todo-previousset t]
2416 ["Add note" org-agenda-add-note t])
2417 ("Archive/Refile/Delete"
2418 ["Archive default" org-agenda-archive-default t]
2419 ["Archive default" org-agenda-archive-default-with-confirmation t]
2420 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2421 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2422 ["Archive subtree" org-agenda-archive t]
2423 "--"
2424 ["Refile" org-agenda-refile t]
2425 "--"
2426 ["Delete subtree" org-agenda-kill t])
2427 ("Bulk action"
2428 ["Mark entry" org-agenda-bulk-mark t]
2429 ["Mark all" org-agenda-bulk-mark-all t]
2430 ["Unmark entry" org-agenda-bulk-unmark t]
2431 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2432 ["Toggle mark" org-agenda-bulk-toggle t]
2433 ["Toggle all" org-agenda-bulk-toggle-all t]
2434 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2435 ["Act on all marked" org-agenda-bulk-action t]
2436 "--"
2437 ("Tags and Properties"
2438 ["Show all Tags" org-agenda-show-tags t]
2439 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2440 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2441 "--"
2442 ["Column View" org-columns t])
2443 ("Deadline/Schedule"
2444 ["Schedule" org-agenda-schedule t]
2445 ["Set Deadline" org-agenda-deadline t]
2446 "--"
2447 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2448 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2449 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2450 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2451 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2452 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2453 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2454 ("Clock and Effort"
2455 ["Clock in" org-agenda-clock-in t]
2456 ["Clock out" org-agenda-clock-out t]
2457 ["Clock cancel" org-agenda-clock-cancel t]
2458 ["Goto running clock" org-clock-goto t]
2459 "--"
2460 ["Set Effort" org-agenda-set-effort t]
2461 ["Change clocked effort" org-clock-modify-effort-estimate
2462 (org-clock-is-active)])
2463 ("Priority"
2464 ["Set Priority" org-agenda-priority t]
2465 ["Increase Priority" org-agenda-priority-up t]
2466 ["Decrease Priority" org-agenda-priority-down t]
2467 ["Show Priority" org-show-priority t])
2468 ("Calendar/Diary"
2469 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2470 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2471 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2472 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2473 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2474 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2475 "--"
2476 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2477 "--"
2478 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2479 "--"
2480 ("MobileOrg"
2481 ["Push Files and Views" org-mobile-push t]
2482 ["Get Captured and Flagged" org-mobile-pull t]
2483 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2484 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2485 "--"
2486 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2487 "--"
2488 ["Quit" org-agenda-quit t]
2489 ["Exit and Release Buffers" org-agenda-exit t]
2492 ;;; Agenda undo
2494 (defvar org-agenda-allow-remote-undo t
2495 "Non-nil means allow remote undo from the agenda buffer.")
2496 (defvar org-agenda-undo-has-started-in nil
2497 "Buffers that have already seen `undo-start' in the current undo sequence.")
2499 (defun org-agenda-undo ()
2500 "Undo a remote editing step in the agenda.
2501 This undoes changes both in the agenda buffer and in the remote buffer
2502 that have been changed along."
2503 (interactive)
2504 (or org-agenda-allow-remote-undo
2505 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2506 (if (not (eq this-command last-command))
2507 (setq org-agenda-undo-has-started-in nil
2508 org-agenda-pending-undo-list org-agenda-undo-list))
2509 (if (not org-agenda-pending-undo-list)
2510 (user-error "No further undo information"))
2511 (let* ((entry (pop org-agenda-pending-undo-list))
2512 buf line cmd rembuf)
2513 (setq cmd (pop entry) line (pop entry))
2514 (setq rembuf (nth 2 entry))
2515 (org-with-remote-undo rembuf
2516 (while (bufferp (setq buf (pop entry)))
2517 (if (pop entry)
2518 (with-current-buffer buf
2519 (let ((last-undo-buffer buf)
2520 (inhibit-read-only t))
2521 (unless (memq buf org-agenda-undo-has-started-in)
2522 (push buf org-agenda-undo-has-started-in)
2523 (make-local-variable 'pending-undo-list)
2524 (undo-start))
2525 (while (and pending-undo-list
2526 (listp pending-undo-list)
2527 (not (car pending-undo-list)))
2528 (pop pending-undo-list))
2529 (undo-more 1))))))
2530 (org-goto-line line)
2531 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2533 (defun org-verify-change-for-undo (l1 l2)
2534 "Verify that a real change occurred between the undo lists L1 and L2."
2535 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2536 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2537 (not (eq l1 l2)))
2539 ;;; Agenda dispatch
2541 (defvar org-agenda-restrict-begin (make-marker))
2542 (defvar org-agenda-restrict-end (make-marker))
2543 (defvar org-agenda-last-dispatch-buffer nil)
2544 (defvar org-agenda-overriding-restriction nil)
2546 (defcustom org-agenda-custom-commands-contexts nil
2547 "Alist of custom agenda keys and contextual rules.
2549 For example, if you have a custom agenda command \"p\" and you
2550 want this command to be accessible only from plain text files,
2551 use this:
2553 \\='((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2555 Here are the available contexts definitions:
2557 in-file: command displayed only in matching files
2558 in-mode: command displayed only in matching modes
2559 not-in-file: command not displayed in matching files
2560 not-in-mode: command not displayed in matching modes
2561 in-buffer: command displayed only in matching buffers
2562 not-in-buffer: command not displayed in matching buffers
2563 [function]: a custom function taking no argument
2565 If you define several checks, the agenda command will be
2566 accessible if there is at least one valid check.
2568 You can also bind a key to another agenda custom command
2569 depending on contextual rules.
2571 \\='((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2573 Here it means: in .txt files, use \"p\" as the key for the
2574 agenda command otherwise associated with \"q\". (The command
2575 originally associated with \"q\" is not displayed to avoid
2576 duplicates.)"
2577 :version "24.3"
2578 :group 'org-agenda-custom-commands
2579 :type '(repeat (list :tag "Rule"
2580 (string :tag " Agenda key")
2581 (string :tag "Replace by command")
2582 (repeat :tag "Available when"
2583 (choice
2584 (cons :tag "Condition"
2585 (choice
2586 (const :tag "In file" in-file)
2587 (const :tag "Not in file" not-in-file)
2588 (const :tag "In buffer" in-buffer)
2589 (const :tag "Not in buffer" not-in-buffer)
2590 (const :tag "In mode" in-mode)
2591 (const :tag "Not in mode" not-in-mode))
2592 (regexp))
2593 (function :tag "Custom function"))))))
2595 (defcustom org-agenda-max-entries nil
2596 "Maximum number of entries to display in an agenda.
2597 This can be nil (no limit) or an integer or an alist of agenda
2598 types with an associated number of entries to display in this
2599 type."
2600 :version "24.4"
2601 :package-version '(Org . "8.0")
2602 :group 'org-agenda-custom-commands
2603 :type '(choice (symbol :tag "No limit" nil)
2604 (integer :tag "Max number of entries")
2605 (repeat
2606 (cons (choice :tag "Agenda type"
2607 (const agenda)
2608 (const todo)
2609 (const tags)
2610 (const search)
2611 (const timeline))
2612 (integer :tag "Max number of entries")))))
2614 (defcustom org-agenda-max-todos nil
2615 "Maximum number of TODOs to display in an agenda.
2616 This can be nil (no limit) or an integer or an alist of agenda
2617 types with an associated number of entries to display in this
2618 type."
2619 :version "24.4"
2620 :package-version '(Org . "8.0")
2621 :group 'org-agenda-custom-commands
2622 :type '(choice (symbol :tag "No limit" nil)
2623 (integer :tag "Max number of TODOs")
2624 (repeat
2625 (cons (choice :tag "Agenda type"
2626 (const agenda)
2627 (const todo)
2628 (const tags)
2629 (const search)
2630 (const timeline))
2631 (integer :tag "Max number of TODOs")))))
2633 (defcustom org-agenda-max-tags nil
2634 "Maximum number of tagged entries to display in an agenda.
2635 This can be nil (no limit) or an integer or an alist of agenda
2636 types with an associated number of entries to display in this
2637 type."
2638 :version "24.4"
2639 :package-version '(Org . "8.0")
2640 :group 'org-agenda-custom-commands
2641 :type '(choice (symbol :tag "No limit" nil)
2642 (integer :tag "Max number of tagged entries")
2643 (repeat
2644 (cons (choice :tag "Agenda type"
2645 (const agenda)
2646 (const todo)
2647 (const tags)
2648 (const search)
2649 (const timeline))
2650 (integer :tag "Max number of tagged entries")))))
2652 (defcustom org-agenda-max-effort nil
2653 "Maximum cumulated effort duration for the agenda.
2654 This can be nil (no limit) or a number of minutes (as an integer)
2655 or an alist of agenda types with an associated number of minutes
2656 to limit entries to in this type."
2657 :version "24.4"
2658 :package-version '(Org . "8.0")
2659 :group 'org-agenda-custom-commands
2660 :type '(choice (symbol :tag "No limit" nil)
2661 (integer :tag "Max number of minutes")
2662 (repeat
2663 (cons (choice :tag "Agenda type"
2664 (const agenda)
2665 (const todo)
2666 (const tags)
2667 (const search)
2668 (const timeline))
2669 (integer :tag "Max number of minutes")))))
2671 (defvar org-keys nil)
2672 (defvar org-match nil)
2673 ;;;###autoload
2674 (defun org-agenda (&optional arg org-keys restriction)
2675 "Dispatch agenda commands to collect entries to the agenda buffer.
2676 Prompts for a command to execute. Any prefix arg will be passed
2677 on to the selected command. The default selections are:
2679 a Call `org-agenda-list' to display the agenda for current day or week.
2680 t Call `org-todo-list' to display the global todo list.
2681 T Call `org-todo-list' to display the global todo list, select only
2682 entries with a specific TODO keyword (the user gets a prompt).
2683 m Call `org-tags-view' to display headlines with tags matching
2684 a condition (the user is prompted for the condition).
2685 M Like `m', but select only TODO entries, no ordinary headlines.
2686 L Create a timeline for the current buffer.
2687 e Export views to associated files.
2688 s Search entries for keywords.
2689 S Search entries for keywords, only with TODO keywords.
2690 / Multi occur across all agenda files and also files listed
2691 in `org-agenda-text-search-extra-files'.
2692 < Restrict agenda commands to buffer, subtree, or region.
2693 Press several times to get the desired effect.
2694 > Remove a previous restriction.
2695 # List \"stuck\" projects.
2696 ! Configure what \"stuck\" means.
2697 C Configure custom agenda commands.
2699 More commands can be added by configuring the variable
2700 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2701 searches can be pre-defined in this way.
2703 If the current buffer is in Org-mode and visiting a file, you can also
2704 first press `<' once to indicate that the agenda should be temporarily
2705 \(until the next use of \\[org-agenda]) restricted to the current file.
2706 Pressing `<' twice means to restrict to the current subtree or region
2707 \(if active)."
2708 (interactive "P")
2709 (catch 'exit
2710 (let* ((prefix-descriptions nil)
2711 (org-agenda-buffer-name org-agenda-buffer-name)
2712 (org-agenda-window-setup (if (equal (buffer-name)
2713 org-agenda-buffer-name)
2714 'current-window
2715 org-agenda-window-setup))
2716 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2717 (org-agenda-custom-commands
2718 ;; normalize different versions
2719 (delq nil
2720 (mapcar
2721 (lambda (x)
2722 (cond ((stringp (cdr x))
2723 (push x prefix-descriptions)
2724 nil)
2725 ((stringp (nth 1 x)) x)
2726 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2727 (t (cons (car x) (cons "" (cdr x))))))
2728 org-agenda-custom-commands)))
2729 (org-agenda-custom-commands
2730 (org-contextualize-keys
2731 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2732 (buf (current-buffer))
2733 (bfn (buffer-file-name (buffer-base-buffer)))
2734 entry key type org-match lprops ans)
2735 ;; Turn off restriction unless there is an overriding one,
2736 (unless org-agenda-overriding-restriction
2737 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2738 ;; There is a request to keep the file list in place
2739 (put 'org-agenda-files 'org-restrict nil))
2740 (setq org-agenda-restrict nil)
2741 (move-marker org-agenda-restrict-begin nil)
2742 (move-marker org-agenda-restrict-end nil))
2743 ;; Delete old local properties
2744 (put 'org-agenda-redo-command 'org-lprops nil)
2745 ;; Delete previously set last-arguments
2746 (put 'org-agenda-redo-command 'last-args nil)
2747 ;; Remember where this call originated
2748 (setq org-agenda-last-dispatch-buffer (current-buffer))
2749 (unless org-keys
2750 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2751 org-keys (car ans)
2752 restriction (cdr ans)))
2753 ;; If we have sticky agenda buffers, set a name for the buffer,
2754 ;; depending on the invoking keys. The user may still set this
2755 ;; as a command option, which will overwrite what we do here.
2756 (if org-agenda-sticky
2757 (setq org-agenda-buffer-name
2758 (format "*Org Agenda(%s)*" org-keys)))
2759 ;; Establish the restriction, if any
2760 (when (and (not org-agenda-overriding-restriction) restriction)
2761 (put 'org-agenda-files 'org-restrict (list bfn))
2762 (cond
2763 ((eq restriction 'region)
2764 (setq org-agenda-restrict (current-buffer))
2765 (move-marker org-agenda-restrict-begin (region-beginning))
2766 (move-marker org-agenda-restrict-end (region-end)))
2767 ((eq restriction 'subtree)
2768 (save-excursion
2769 (setq org-agenda-restrict (current-buffer))
2770 (org-back-to-heading t)
2771 (move-marker org-agenda-restrict-begin (point))
2772 (move-marker org-agenda-restrict-end
2773 (progn (org-end-of-subtree t)))))))
2775 ;; For example the todo list should not need it (but does...)
2776 (cond
2777 ((setq entry (assoc org-keys org-agenda-custom-commands))
2778 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2779 (progn
2780 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2781 lprops (nth 4 entry))
2782 (if org-agenda-sticky
2783 (setq org-agenda-buffer-name
2784 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2785 (format "*Org Agenda(%s)*" org-keys))))
2786 (put 'org-agenda-redo-command 'org-lprops lprops)
2787 (cond
2788 ((eq type 'agenda)
2789 (org-let lprops '(org-agenda-list current-prefix-arg)))
2790 ((eq type 'agenda*)
2791 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2792 ((eq type 'alltodo)
2793 (org-let lprops '(org-todo-list current-prefix-arg)))
2794 ((eq type 'search)
2795 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2796 ((eq type 'stuck)
2797 (org-let lprops '(org-agenda-list-stuck-projects
2798 current-prefix-arg)))
2799 ((eq type 'tags)
2800 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2801 ((eq type 'tags-todo)
2802 (org-let lprops '(org-tags-view '(4) org-match)))
2803 ((eq type 'todo)
2804 (org-let lprops '(org-todo-list org-match)))
2805 ((eq type 'tags-tree)
2806 (org-check-for-org-mode)
2807 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2808 ((eq type 'todo-tree)
2809 (org-check-for-org-mode)
2810 (org-let lprops
2811 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2812 (regexp-quote org-match) "\\>"))))
2813 ((eq type 'occur-tree)
2814 (org-check-for-org-mode)
2815 (org-let lprops '(org-occur org-match)))
2816 ((functionp type)
2817 (org-let lprops '(funcall type org-match)))
2818 ((fboundp type)
2819 (org-let lprops '(funcall type org-match)))
2820 (t (user-error "Invalid custom agenda command type %s" type))))
2821 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2822 ((equal org-keys "C")
2823 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2824 (customize-variable 'org-agenda-custom-commands))
2825 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2826 ((equal org-keys "s") (call-interactively 'org-search-view))
2827 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2828 ((equal org-keys "t") (call-interactively 'org-todo-list))
2829 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2830 ((equal org-keys "m") (call-interactively 'org-tags-view))
2831 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2832 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2833 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2834 (org-add-hook
2835 'post-command-hook
2836 (lambda ()
2837 (unless (current-message)
2838 (let* ((m (org-agenda-get-any-marker))
2839 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2840 (when note
2841 (message (concat
2842 "FLAGGING-NOTE ([?] for more info): "
2843 (org-add-props
2844 (replace-regexp-in-string
2845 "\\\\n" "//"
2846 (copy-sequence note))
2847 nil 'face 'org-warning)))))))
2848 t t))
2849 ((equal org-keys "L")
2850 (unless (derived-mode-p 'org-mode)
2851 (user-error "This is not an Org-mode file"))
2852 (unless restriction
2853 (put 'org-agenda-files 'org-restrict (list bfn))
2854 (org-call-with-arg 'org-timeline arg)))
2855 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2856 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2857 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2858 (t (user-error "Invalid agenda key"))))))
2860 (defvar org-agenda-multi)
2862 (defun org-agenda-append-agenda ()
2863 "Append another agenda view to the current one.
2864 This function allows interactive building of block agendas.
2865 Agenda views are separated by `org-agenda-block-separator'."
2866 (interactive)
2867 (unless (derived-mode-p 'org-agenda-mode)
2868 (user-error "Can only append from within agenda buffer"))
2869 (let ((org-agenda-multi t))
2870 (org-agenda)
2871 (widen)
2872 (org-agenda-finalize)
2873 (setq buffer-read-only t)
2874 (org-agenda-fit-window-to-buffer)))
2876 (defun org-agenda-normalize-custom-commands (cmds)
2877 "Normalize custom commands CMDS."
2878 (delq nil
2879 (mapcar
2880 (lambda (x)
2881 (cond ((stringp (cdr x)) nil)
2882 ((stringp (nth 1 x)) x)
2883 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2884 (t (cons (car x) (cons "" (cdr x))))))
2885 cmds)))
2887 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2888 "The user interface for selecting an agenda command."
2889 (catch 'exit
2890 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2891 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2892 (region-p (org-region-active-p))
2893 (custom org-agenda-custom-commands)
2894 (selstring "")
2895 restriction second-time
2896 c entry key type match prefixes rmheader header-end custom1 desc
2897 line lines left right n n1)
2898 (save-window-excursion
2899 (delete-other-windows)
2900 (org-switch-to-buffer-other-window " *Agenda Commands*")
2901 (erase-buffer)
2902 (insert (eval-when-compile
2903 (let ((header
2904 "Press key for an agenda command: < Buffer, subtree/region restriction
2905 -------------------------------- > Remove restriction
2906 a Agenda for current week or day e Export agenda views
2907 t List of all TODO entries T Entries with special TODO kwd
2908 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2909 s Search for keywords S Like s, but only TODO entries
2910 L Timeline for current buffer # List stuck projects (!=configure)
2911 / Multi-occur C Configure custom agenda commands
2912 ? Find :FLAGGED: entries * Toggle sticky agenda views
2914 (start 0))
2915 (while (string-match
2916 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2917 header start)
2918 (setq start (match-end 0))
2919 (add-text-properties (match-beginning 2) (match-end 2)
2920 '(face bold) header))
2921 header)))
2922 (setq header-end (point-marker))
2923 (while t
2924 (setq custom1 custom)
2925 (when (eq rmheader t)
2926 (org-goto-line 1)
2927 (re-search-forward ":" nil t)
2928 (delete-region (match-end 0) (point-at-eol))
2929 (forward-char 1)
2930 (looking-at "-+")
2931 (delete-region (match-end 0) (point-at-eol))
2932 (move-marker header-end (match-end 0)))
2933 (goto-char header-end)
2934 (delete-region (point) (point-max))
2936 ;; Produce all the lines that describe custom commands and prefixes
2937 (setq lines nil)
2938 (while (setq entry (pop custom1))
2939 (setq key (car entry) desc (nth 1 entry)
2940 type (nth 2 entry)
2941 match (nth 3 entry))
2942 (if (> (length key) 1)
2943 (add-to-list 'prefixes (string-to-char key))
2944 (setq line
2945 (format
2946 "%-4s%-14s"
2947 (org-add-props (copy-sequence key)
2948 '(face bold))
2949 (cond
2950 ((string-match "\\S-" desc) desc)
2951 ((eq type 'agenda) "Agenda for current week or day")
2952 ((eq type 'agenda*) "Appointments for current week or day")
2953 ((eq type 'alltodo) "List of all TODO entries")
2954 ((eq type 'search) "Word search")
2955 ((eq type 'stuck) "List of stuck projects")
2956 ((eq type 'todo) "TODO keyword")
2957 ((eq type 'tags) "Tags query")
2958 ((eq type 'tags-todo) "Tags (TODO)")
2959 ((eq type 'tags-tree) "Tags tree")
2960 ((eq type 'todo-tree) "TODO kwd tree")
2961 ((eq type 'occur-tree) "Occur tree")
2962 ((functionp type) (if (symbolp type)
2963 (symbol-name type)
2964 "Lambda expression"))
2965 (t "???"))))
2966 (if org-agenda-menu-show-matcher
2967 (setq line
2968 (concat line ": "
2969 (cond
2970 ((stringp match)
2971 (setq match (copy-sequence match))
2972 (org-add-props match nil 'face 'org-warning))
2973 ((listp type)
2974 (format "set of %d commands" (length type))))))
2975 (if (org-string-nw-p match)
2976 (add-text-properties
2977 0 (length line) (list 'help-echo
2978 (concat "Matcher: " match)) line)))
2979 (push line lines)))
2980 (setq lines (nreverse lines))
2981 (when prefixes
2982 (mapc (lambda (x)
2983 (push
2984 (format "%s %s"
2985 (org-add-props (char-to-string x)
2986 nil 'face 'bold)
2987 (or (cdr (assoc (concat selstring
2988 (char-to-string x))
2989 prefix-descriptions))
2990 "Prefix key"))
2991 lines))
2992 prefixes))
2994 ;; Check if we should display in two columns
2995 (if org-agenda-menu-two-columns
2996 (progn
2997 (setq n (length lines)
2998 n1 (+ (/ n 2) (mod n 2))
2999 right (nthcdr n1 lines)
3000 left (copy-sequence lines))
3001 (setcdr (nthcdr (1- n1) left) nil))
3002 (setq left lines right nil))
3003 (while left
3004 (insert "\n" (pop left))
3005 (when right
3006 (if (< (current-column) 40)
3007 (move-to-column 40 t)
3008 (insert " "))
3009 (insert (pop right))))
3011 ;; Make the window the right size
3012 (goto-char (point-min))
3013 (if second-time
3014 (if (not (pos-visible-in-window-p (point-max)))
3015 (org-fit-window-to-buffer))
3016 (setq second-time t)
3017 (org-fit-window-to-buffer))
3019 ;; Ask for selection
3020 (message "Press key for agenda command%s:"
3021 (if (or restrict-ok org-agenda-overriding-restriction)
3022 (if org-agenda-overriding-restriction
3023 " (restriction lock active)"
3024 (if restriction
3025 (format " (restricted to %s)" restriction)
3026 " (unrestricted)"))
3027 ""))
3028 (setq c (read-char-exclusive))
3029 (message "")
3030 (cond
3031 ((assoc (char-to-string c) custom)
3032 (setq selstring (concat selstring (char-to-string c)))
3033 (throw 'exit (cons selstring restriction)))
3034 ((memq c prefixes)
3035 (setq selstring (concat selstring (char-to-string c))
3036 prefixes nil
3037 rmheader (or rmheader t)
3038 custom (delq nil (mapcar
3039 (lambda (x)
3040 (if (or (= (length (car x)) 1)
3041 (/= (string-to-char (car x)) c))
3043 (cons (substring (car x) 1) (cdr x))))
3044 custom))))
3045 ((eq c ?*)
3046 (call-interactively 'org-toggle-sticky-agenda)
3047 (sit-for 2))
3048 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3049 (message "Restriction is only possible in Org-mode buffers")
3050 (ding) (sit-for 1))
3051 ((eq c ?1)
3052 (org-agenda-remove-restriction-lock 'noupdate)
3053 (setq restriction 'buffer))
3054 ((eq c ?0)
3055 (org-agenda-remove-restriction-lock 'noupdate)
3056 (setq restriction (if region-p 'region 'subtree)))
3057 ((eq c ?<)
3058 (org-agenda-remove-restriction-lock 'noupdate)
3059 (setq restriction
3060 (cond
3061 ((eq restriction 'buffer)
3062 (if region-p 'region 'subtree))
3063 ((memq restriction '(subtree region))
3064 nil)
3065 (t 'buffer))))
3066 ((eq c ?>)
3067 (org-agenda-remove-restriction-lock 'noupdate)
3068 (setq restriction nil))
3069 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3070 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3071 ((and (> (length selstring) 0) (eq c ?\d))
3072 (delete-window)
3073 (org-agenda-get-restriction-and-command prefix-descriptions))
3075 ((equal c ?q) (error "Abort"))
3076 (t (user-error "Invalid key %c" c))))))))
3078 (defun org-agenda-fit-window-to-buffer ()
3079 "Fit the window to the buffer size."
3080 (and (memq org-agenda-window-setup '(reorganize-frame))
3081 (fboundp 'fit-window-to-buffer)
3082 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3083 (= (car org-agenda-window-frame-fractions) 1.0))
3084 (delete-other-windows)
3085 (org-fit-window-to-buffer
3087 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3088 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3090 (defvar org-cmd nil)
3091 (defvar org-agenda-overriding-cmd nil)
3092 (defvar org-agenda-overriding-arguments nil)
3093 (defvar org-agenda-overriding-cmd-arguments nil)
3094 (defun org-agenda-run-series (name series)
3095 "Run agenda NAME as a SERIES of agenda commands."
3096 (org-let (nth 1 series) '(org-agenda-prepare name))
3097 ;; We need to reset agenda markers here, because when constructing a
3098 ;; block agenda, the individual blocks do not do that.
3099 (org-agenda-reset-markers)
3100 (let* ((org-agenda-multi t)
3101 (redo (list 'org-agenda-run-series name (list 'quote series)))
3102 (cmds (car series))
3103 (gprops (nth 1 series))
3104 match ;; The byte compiler incorrectly complains about this. Keep it!
3105 org-cmd type lprops)
3106 (while (setq org-cmd (pop cmds))
3107 (setq type (car org-cmd)
3108 match (eval (nth 1 org-cmd))
3109 lprops (nth 2 org-cmd))
3110 (let ((org-agenda-overriding-arguments
3111 (if (eq org-agenda-overriding-cmd org-cmd)
3112 (or org-agenda-overriding-arguments
3113 org-agenda-overriding-cmd-arguments))))
3114 (cond
3115 ((eq type 'agenda)
3116 (org-let2 gprops lprops
3117 '(call-interactively 'org-agenda-list)))
3118 ((eq type 'agenda*)
3119 (org-let2 gprops lprops
3120 '(funcall 'org-agenda-list nil nil t)))
3121 ((eq type 'alltodo)
3122 (org-let2 gprops lprops
3123 '(call-interactively 'org-todo-list)))
3124 ((eq type 'search)
3125 (org-let2 gprops lprops
3126 '(org-search-view current-prefix-arg match nil)))
3127 ((eq type 'stuck)
3128 (org-let2 gprops lprops
3129 '(call-interactively 'org-agenda-list-stuck-projects)))
3130 ((eq type 'tags)
3131 (org-let2 gprops lprops
3132 '(org-tags-view current-prefix-arg match)))
3133 ((eq type 'tags-todo)
3134 (org-let2 gprops lprops
3135 '(org-tags-view '(4) match)))
3136 ((eq type 'todo)
3137 (org-let2 gprops lprops
3138 '(org-todo-list match)))
3139 ((fboundp type)
3140 (org-let2 gprops lprops
3141 '(funcall type match)))
3142 (t (error "Invalid type in command series")))))
3143 (widen)
3144 (let ((inhibit-read-only t))
3145 (add-text-properties (point-min) (point-max)
3146 `(org-series t org-series-redo-cmd ,redo)))
3147 (setq org-agenda-redo-command redo)
3148 (goto-char (point-min)))
3149 (org-agenda-fit-window-to-buffer)
3150 (org-let (nth 1 series) '(org-agenda-finalize)))
3152 ;;;###autoload
3153 (defmacro org-batch-agenda (cmd-key &rest parameters)
3154 "Run an agenda command in batch mode and send the result to STDOUT.
3155 If CMD-KEY is a string of length 1, it is used as a key in
3156 `org-agenda-custom-commands' and triggers this command. If it is a
3157 longer string it is used as a tags/todo match string.
3158 Parameters are alternating variable names and values that will be bound
3159 before running the agenda command."
3160 (org-eval-in-environment (org-make-parameter-alist parameters)
3161 (let (org-agenda-sticky)
3162 (if (> (length cmd-key) 2)
3163 (org-tags-view nil cmd-key)
3164 (org-agenda nil cmd-key))))
3165 (set-buffer org-agenda-buffer-name)
3166 (princ (buffer-string)))
3168 (defvar org-agenda-info nil)
3170 ;;;###autoload
3171 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3172 "Run an agenda command in batch mode and send the result to STDOUT.
3173 If CMD-KEY is a string of length 1, it is used as a key in
3174 `org-agenda-custom-commands' and triggers this command. If it is a
3175 longer string it is used as a tags/todo match string.
3176 Parameters are alternating variable names and values that will be bound
3177 before running the agenda command.
3179 The output gives a line for each selected agenda item. Each
3180 item is a list of comma-separated values, like this:
3182 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3184 category The category of the item
3185 head The headline, without TODO kwd, TAGS and PRIORITY
3186 type The type of the agenda entry, can be
3187 todo selected in TODO match
3188 tagsmatch selected in tags match
3189 diary imported from diary
3190 deadline a deadline on given date
3191 scheduled scheduled on given date
3192 timestamp entry has timestamp on given date
3193 closed entry was closed on given date
3194 upcoming-deadline warning about deadline
3195 past-scheduled forwarded scheduled item
3196 block entry has date block including g. date
3197 todo The todo keyword, if any
3198 tags All tags including inherited ones, separated by colons
3199 date The relevant date, like 2007-2-14
3200 time The time, like 15:00-16:50
3201 extra Sting with extra planning info
3202 priority-l The priority letter if any was given
3203 priority-n The computed numerical priority
3204 agenda-day The day in the agenda where this is listed"
3205 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3206 (org-make-parameter-alist parameters))
3207 (if (> (length cmd-key) 2)
3208 (org-tags-view nil cmd-key)
3209 (org-agenda nil cmd-key)))
3210 (set-buffer org-agenda-buffer-name)
3211 (let* ((lines (org-split-string (buffer-string) "\n"))
3212 line)
3213 (while (setq line (pop lines))
3214 (catch 'next
3215 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3216 (setq org-agenda-info
3217 (org-fix-agenda-info (text-properties-at 0 line)))
3218 (princ
3219 (mapconcat 'org-agenda-export-csv-mapper
3220 '(org-category txt type todo tags date time extra
3221 priority-letter priority agenda-day)
3222 ","))
3223 (princ "\n")))))
3225 (defun org-fix-agenda-info (props)
3226 "Make sure all properties on an agenda item have a canonical form.
3227 This ensures the export commands can easily use it."
3228 (let (tmp re)
3229 (when (setq tmp (plist-get props 'tags))
3230 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3231 (when (setq tmp (plist-get props 'date))
3232 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3233 (let ((calendar-date-display-form '(year "-" month "-" day)))
3234 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3236 (setq tmp (calendar-date-string tmp)))
3237 (setq props (plist-put props 'date tmp)))
3238 (when (setq tmp (plist-get props 'day))
3239 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3240 (let ((calendar-date-display-form '(year "-" month "-" day)))
3241 (setq tmp (calendar-date-string tmp)))
3242 (setq props (plist-put props 'day tmp))
3243 (setq props (plist-put props 'agenda-day tmp)))
3244 (when (setq tmp (plist-get props 'txt))
3245 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3246 (plist-put props 'priority-letter (match-string 1 tmp))
3247 (setq tmp (replace-match "" t t tmp)))
3248 (when (and (setq re (plist-get props 'org-todo-regexp))
3249 (setq re (concat "\\`\\.*" re " ?"))
3250 (string-match re tmp))
3251 (plist-put props 'todo (match-string 1 tmp))
3252 (setq tmp (replace-match "" t t tmp)))
3253 (plist-put props 'txt tmp)))
3254 props)
3256 (defun org-agenda-export-csv-mapper (prop)
3257 (let ((res (plist-get org-agenda-info prop)))
3258 (setq res
3259 (cond
3260 ((not res) "")
3261 ((stringp res) res)
3262 (t (prin1-to-string res))))
3263 (while (string-match "," res)
3264 (setq res (replace-match ";" t t res)))
3265 (org-trim res)))
3267 ;;;###autoload
3268 (defun org-store-agenda-views (&rest parameters)
3269 "Store agenda views."
3270 (interactive)
3271 (eval (list 'org-batch-store-agenda-views)))
3273 ;;;###autoload
3274 (defmacro org-batch-store-agenda-views (&rest parameters)
3275 "Run all custom agenda commands that have a file argument."
3276 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3277 (pop-up-frames nil)
3278 (dir default-directory)
3279 (pars (org-make-parameter-alist parameters))
3280 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3281 (save-window-excursion
3282 (while cmds
3283 (setq cmd (pop cmds)
3284 thiscmdkey (car cmd)
3285 thiscmdcmd (cdr cmd)
3286 match (nth 2 thiscmdcmd)
3287 bufname (if org-agenda-sticky
3288 (or (and (stringp match)
3289 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3290 (format "*Org Agenda(%s)*" thiscmdkey))
3291 org-agenda-buffer-name)
3292 cmd-or-set (nth 2 cmd)
3293 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3294 files (nth (if (listp cmd-or-set) 4 5) cmd))
3295 (if (stringp files) (setq files (list files)))
3296 (when files
3297 (org-eval-in-environment (append org-agenda-exporter-settings
3298 opts pars)
3299 (org-agenda nil thiscmdkey))
3300 (set-buffer bufname)
3301 (while files
3302 (org-eval-in-environment (append org-agenda-exporter-settings
3303 opts pars)
3304 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3305 (and (get-buffer bufname)
3306 (kill-buffer bufname)))))))
3308 (defvar org-agenda-current-span nil
3309 "The current span used in the agenda view.") ; local variable in the agenda buffer
3310 (defun org-agenda-mark-header-line (pos)
3311 "Mark the line at POS as an agenda structure header."
3312 (save-excursion
3313 (goto-char pos)
3314 (put-text-property (point-at-bol) (point-at-eol)
3315 'org-agenda-structural-header t)
3316 (when org-agenda-title-append
3317 (put-text-property (point-at-bol) (point-at-eol)
3318 'org-agenda-title-append org-agenda-title-append))))
3320 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3321 (defvar org-agenda-write-buffer-name "Agenda View")
3322 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3323 "Write the current buffer (an agenda view) as a file.
3325 Depending on the extension of the file name, plain text (.txt),
3326 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3327 If the extension is .ics, translate visible agenda into iCalendar
3328 format. If the extension is .org, collect all subtrees
3329 corresponding to the agenda entries and add them in an .org file.
3331 With prefix argument OPEN, open the new file immediately. If
3332 NOSETTINGS is given, do not scope the settings of
3333 `org-agenda-exporter-settings' into the export commands. This is
3334 used when the settings have already been scoped and we do not
3335 wish to overrule other, higher priority settings. If
3336 AGENDA-BUFFER-NAME is provided, use this as the buffer name for
3337 the agenda to write."
3338 (interactive "FWrite agenda to file: \nP")
3339 (if (or (not (file-writable-p file))
3340 (and (file-exists-p file)
3341 (if (org-called-interactively-p 'any)
3342 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3343 (user-error "Cannot write agenda to file %s" file))
3344 (org-let (if nosettings nil org-agenda-exporter-settings)
3345 '(save-excursion
3346 (save-window-excursion
3347 (let ((bs (copy-sequence (buffer-string))) beg content)
3348 (with-temp-buffer
3349 (rename-buffer org-agenda-write-buffer-name t)
3350 (set-buffer-modified-p nil)
3351 (insert bs)
3352 (org-agenda-remove-marked-text 'org-filtered)
3353 (run-hooks 'org-agenda-before-write-hook)
3354 (cond
3355 ((org-bound-and-true-p org-mobile-creating-agendas)
3356 (org-mobile-write-agenda-for-mobile file))
3357 ((string-match "\\.org\\'" file)
3358 (let (content p m message-log-max)
3359 (goto-char (point-min))
3360 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3361 (goto-char p)
3362 (setq m (get-text-property (point) 'org-hd-marker))
3363 (when m
3364 (push (save-excursion
3365 (set-buffer (marker-buffer m))
3366 (goto-char m)
3367 (org-copy-subtree 1 nil t t)
3368 org-subtree-clip)
3369 content)))
3370 (find-file file)
3371 (erase-buffer)
3372 (dolist (s content) (org-paste-subtree 1 s))
3373 (write-file file)
3374 (kill-buffer (current-buffer))
3375 (message "Org file written to %s" file)))
3376 ((string-match "\\.html?\\'" file)
3377 (require 'htmlize)
3378 (set-buffer (htmlize-buffer (current-buffer)))
3379 (when org-agenda-export-html-style
3380 ;; replace <style> section with org-agenda-export-html-style
3381 (goto-char (point-min))
3382 (kill-region (- (search-forward "<style") 6)
3383 (search-forward "</style>"))
3384 (insert org-agenda-export-html-style))
3385 (write-file file)
3386 (kill-buffer (current-buffer))
3387 (message "HTML written to %s" file))
3388 ((string-match "\\.ps\\'" file)
3389 (require 'ps-print)
3390 (ps-print-buffer-with-faces file)
3391 (message "Postscript written to %s" file))
3392 ((string-match "\\.pdf\\'" file)
3393 (require 'ps-print)
3394 (ps-print-buffer-with-faces
3395 (concat (file-name-sans-extension file) ".ps"))
3396 (call-process "ps2pdf" nil nil nil
3397 (expand-file-name
3398 (concat (file-name-sans-extension file) ".ps"))
3399 (expand-file-name file))
3400 (delete-file (concat (file-name-sans-extension file) ".ps"))
3401 (message "PDF written to %s" file))
3402 ((string-match "\\.ics\\'" file)
3403 (require 'ox-icalendar)
3404 (org-icalendar-export-current-agenda (expand-file-name file)))
3406 (let ((bs (buffer-string)))
3407 (find-file file)
3408 (erase-buffer)
3409 (insert bs)
3410 (save-buffer 0)
3411 (kill-buffer (current-buffer))
3412 (message "Plain text written to %s" file))))))))
3413 (set-buffer (or agenda-bufname
3414 (and (org-called-interactively-p 'any) (buffer-name))
3415 org-agenda-buffer-name)))
3416 (when open (org-open-file file)))
3418 (defun org-agenda-remove-marked-text (property &optional value)
3419 "Delete all text marked with VALUE of PROPERTY.
3420 VALUE defaults to t."
3421 (let (beg)
3422 (setq value (or value t))
3423 (while (setq beg (text-property-any (point-min) (point-max)
3424 property value))
3425 (delete-region
3426 beg (or (next-single-property-change beg property)
3427 (point-max))))))
3429 (defun org-agenda-add-entry-text ()
3430 "Add entry text to agenda lines.
3431 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3432 entry text following headings shown in the agenda.
3433 Drawers will be excluded, also the line with scheduling/deadline info."
3434 (when (and (> org-agenda-add-entry-text-maxlines 0)
3435 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3436 (let (m txt)
3437 (goto-char (point-min))
3438 (while (not (eobp))
3439 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3440 (beginning-of-line 2)
3441 (setq txt (org-agenda-get-some-entry-text
3442 m org-agenda-add-entry-text-maxlines " > "))
3443 (end-of-line 1)
3444 (if (string-match "\\S-" txt)
3445 (insert "\n" txt)
3446 (or (eobp) (forward-char 1))))))))
3448 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3449 &rest keep)
3450 "Extract entry text from MARKER, at most N-LINES lines.
3451 This will ignore drawers etc, just get the text.
3452 If INDENT is given, prefix every line with this string. If KEEP is
3453 given, it is a list of symbols, defining stuff that should not be
3454 removed from the entry content. Currently only `planning' is allowed here."
3455 (let (txt drawer-re kwd-time-re ind)
3456 (save-excursion
3457 (with-current-buffer (marker-buffer marker)
3458 (if (not (derived-mode-p 'org-mode))
3459 (setq txt "")
3460 (save-excursion
3461 (save-restriction
3462 (widen)
3463 (goto-char marker)
3464 (end-of-line 1)
3465 (setq txt (buffer-substring
3466 (min (1+ (point)) (point-max))
3467 (progn (outline-next-heading) (point)))
3468 drawer-re org-drawer-regexp
3469 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3470 ".*\n?"))
3471 (with-temp-buffer
3472 (insert txt)
3473 (when org-agenda-add-entry-text-descriptive-links
3474 (goto-char (point-min))
3475 (while (org-activate-bracket-links (point-max))
3476 (add-text-properties (match-beginning 0) (match-end 0)
3477 '(face org-link))))
3478 (goto-char (point-min))
3479 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3480 (set-text-properties (match-beginning 0) (match-end 0)
3481 nil))
3482 (goto-char (point-min))
3483 (while (re-search-forward drawer-re nil t)
3484 (delete-region
3485 (match-beginning 0)
3486 (progn (re-search-forward
3487 "^[ \t]*:END:.*\n?" nil 'move)
3488 (point))))
3489 (unless (member 'planning keep)
3490 (goto-char (point-min))
3491 (while (re-search-forward kwd-time-re nil t)
3492 (replace-match "")))
3493 (goto-char (point-min))
3494 (when org-agenda-entry-text-exclude-regexps
3495 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3496 (while (setq re (pop re-list))
3497 (goto-char (point-min))
3498 (while (re-search-forward re nil t)
3499 (replace-match "")))))
3500 (goto-char (point-max))
3501 (skip-chars-backward " \t\n")
3502 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3504 ;; find and remove min common indentation
3505 (goto-char (point-min))
3506 (untabify (point-min) (point-max))
3507 (setq ind (org-get-indentation))
3508 (while (not (eobp))
3509 (unless (looking-at "[ \t]*$")
3510 (setq ind (min ind (org-get-indentation))))
3511 (beginning-of-line 2))
3512 (goto-char (point-min))
3513 (while (not (eobp))
3514 (unless (looking-at "[ \t]*$")
3515 (move-to-column ind)
3516 (delete-region (point-at-bol) (point)))
3517 (beginning-of-line 2))
3519 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3521 (goto-char (point-min))
3522 (when indent
3523 (while (and (not (eobp)) (re-search-forward "^" nil t))
3524 (replace-match indent t t)))
3525 (goto-char (point-min))
3526 (while (looking-at "[ \t]*\n") (replace-match ""))
3527 (goto-char (point-max))
3528 (when (> (org-current-line)
3529 n-lines)
3530 (org-goto-line (1+ n-lines))
3531 (backward-char 1))
3532 (setq txt (buffer-substring (point-min) (point)))))))))
3533 txt))
3535 (defun org-check-for-org-mode ()
3536 "Make sure current buffer is in org-mode. Error if not."
3537 (or (derived-mode-p 'org-mode)
3538 (error "Cannot execute org-mode agenda command on buffer in %s"
3539 major-mode)))
3541 ;;; Agenda prepare and finalize
3543 (defvar org-agenda-multi nil) ; dynamically scoped
3544 (defvar org-agenda-pre-window-conf nil)
3545 (defvar org-agenda-columns-active nil)
3546 (defvar org-agenda-name nil)
3547 (defvar org-agenda-tag-filter nil)
3548 (defvar org-agenda-category-filter nil)
3549 (defvar org-agenda-regexp-filter nil)
3550 (defvar org-agenda-effort-filter nil)
3551 (defvar org-agenda-top-headline-filter nil)
3552 (defvar org-agenda-tag-filter-preset nil
3553 "A preset of the tags filter used for secondary agenda filtering.
3554 This must be a list of strings, each string must be a single tag preceded
3555 by \"+\" or \"-\".
3556 This variable should not be set directly, but agenda custom commands can
3557 bind it in the options section. The preset filter is a global property of
3558 the entire agenda view. In a block agenda, it will not work reliably to
3559 define a filter for one of the individual blocks. You need to set it in
3560 the global options and expect it to be applied to the entire view.")
3562 (defvar org-agenda-category-filter-preset nil
3563 "A preset of the category filter used for secondary agenda filtering.
3564 This must be a list of strings, each string must be a single category
3565 preceded by \"+\" or \"-\".
3566 This variable should not be set directly, but agenda custom commands can
3567 bind it in the options section. The preset filter is a global property of
3568 the entire agenda view. In a block agenda, it will not work reliably to
3569 define a filter for one of the individual blocks. You need to set it in
3570 the global options and expect it to be applied to the entire view.")
3572 (defvar org-agenda-regexp-filter-preset nil
3573 "A preset of the regexp filter used for secondary agenda filtering.
3574 This must be a list of strings, each string must be a single regexp
3575 preceded by \"+\" or \"-\".
3576 This variable should not be set directly, but agenda custom commands can
3577 bind it in the options section. The preset filter is a global property of
3578 the entire agenda view. In a block agenda, it will not work reliably to
3579 define a filter for one of the individual blocks. You need to set it in
3580 the global options and expect it to be applied to the entire view.")
3582 (defvar org-agenda-effort-filter-preset nil
3583 "A preset of the effort condition used for secondary agenda filtering.
3584 This must be a list of strings, each string must be a single regexp
3585 preceded by \"+\" or \"-\".
3586 This variable should not be set directly, but agenda custom commands can
3587 bind it in the options section. The preset filter is a global property of
3588 the entire agenda view. In a block agenda, it will not work reliably to
3589 define a filter for one of the individual blocks. You need to set it in
3590 the global options and expect it to be applied to the entire view.")
3592 (defun org-agenda-use-sticky-p ()
3593 "Return non-nil if an agenda buffer named
3594 `org-agenda-buffer-name' exists and should be shown instead of
3595 generating a new one."
3596 (and
3597 ;; turned off by user
3598 org-agenda-sticky
3599 ;; For multi-agenda buffer already exists
3600 (not org-agenda-multi)
3601 ;; buffer found
3602 (get-buffer org-agenda-buffer-name)
3603 ;; C-u parameter is same as last call
3604 (with-current-buffer (get-buffer org-agenda-buffer-name)
3605 (and
3606 (equal current-prefix-arg
3607 org-agenda-last-prefix-arg)
3608 ;; In case user turned stickiness on, while having existing
3609 ;; Agenda buffer active, don't reuse that buffer, because it
3610 ;; does not have org variables local
3611 org-agenda-this-buffer-is-sticky))))
3613 (defun org-agenda-prepare-window (abuf filter-alist)
3614 "Setup agenda buffer in the window.
3615 ABUF is the buffer for the agenda window.
3616 FILTER-ALIST is an alist of filters we need to apply when
3617 `org-agenda-persistent-filter' is non-nil."
3618 (let* ((awin (get-buffer-window abuf)) wconf)
3619 (cond
3620 ((equal (current-buffer) abuf) nil)
3621 (awin (select-window awin))
3622 ((not (setq wconf (current-window-configuration))))
3623 ((equal org-agenda-window-setup 'current-window)
3624 (org-pop-to-buffer-same-window abuf))
3625 ((equal org-agenda-window-setup 'other-window)
3626 (org-switch-to-buffer-other-window abuf))
3627 ((equal org-agenda-window-setup 'other-frame)
3628 (switch-to-buffer-other-frame abuf))
3629 ((eq org-agenda-window-setup 'only-window)
3630 (delete-other-windows)
3631 (org-pop-to-buffer-same-window abuf))
3632 ((equal org-agenda-window-setup 'reorganize-frame)
3633 (delete-other-windows)
3634 (org-switch-to-buffer-other-window abuf)))
3635 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3636 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3637 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3638 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3639 ;; Additional test in case agenda is invoked from within agenda
3640 ;; buffer via elisp link.
3641 (unless (equal (current-buffer) abuf)
3642 (org-pop-to-buffer-same-window abuf))
3643 (setq org-agenda-pre-window-conf
3644 (or wconf org-agenda-pre-window-conf))))
3646 (defun org-agenda-prepare (&optional name)
3647 (let ((filter-alist (if org-agenda-persistent-filter
3648 (with-current-buffer
3649 (get-buffer-create org-agenda-buffer-name)
3650 (list `(tag . ,org-agenda-tag-filter)
3651 `(re . ,org-agenda-regexp-filter)
3652 `(effort . ,org-agenda-effort-filter)
3653 `(cat . ,org-agenda-category-filter))))))
3654 (if (org-agenda-use-sticky-p)
3655 (progn
3656 (put 'org-agenda-tag-filter :preset-filter nil)
3657 (put 'org-agenda-category-filter :preset-filter nil)
3658 (put 'org-agenda-regexp-filter :preset-filter nil)
3659 ;; Popup existing buffer
3660 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3661 filter-alist)
3662 (message "Sticky Agenda buffer, use `r' to refresh")
3663 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3664 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3665 (setq org-todo-keywords-for-agenda nil)
3666 (put 'org-agenda-tag-filter :preset-filter
3667 org-agenda-tag-filter-preset)
3668 (put 'org-agenda-category-filter :preset-filter
3669 org-agenda-category-filter-preset)
3670 (put 'org-agenda-regexp-filter :preset-filter
3671 org-agenda-regexp-filter-preset)
3672 (put 'org-agenda-effort-filter :preset-filter
3673 org-agenda-effort-filter-preset)
3674 (if org-agenda-multi
3675 (progn
3676 (setq buffer-read-only nil)
3677 (goto-char (point-max))
3678 (unless (or (bobp) org-agenda-compact-blocks
3679 (not org-agenda-block-separator))
3680 (insert "\n"
3681 (if (stringp org-agenda-block-separator)
3682 org-agenda-block-separator
3683 (make-string (window-width) org-agenda-block-separator))
3684 "\n"))
3685 (narrow-to-region (point) (point-max)))
3686 (setq org-done-keywords-for-agenda nil)
3687 ;; Setting any org variables that are in org-agenda-local-vars
3688 ;; list need to be done after the prepare call
3689 (org-agenda-prepare-window
3690 (get-buffer-create org-agenda-buffer-name) filter-alist)
3691 (setq buffer-read-only nil)
3692 (org-agenda-reset-markers)
3693 (let ((inhibit-read-only t)) (erase-buffer))
3694 (org-agenda-mode)
3695 (setq org-agenda-buffer (current-buffer))
3696 (setq org-agenda-contributing-files nil)
3697 (setq org-agenda-columns-active nil)
3698 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3699 (setq org-todo-keywords-for-agenda
3700 (org-uniquify org-todo-keywords-for-agenda))
3701 (setq org-done-keywords-for-agenda
3702 (org-uniquify org-done-keywords-for-agenda))
3703 (setq org-agenda-last-prefix-arg current-prefix-arg)
3704 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3705 (and name (not org-agenda-name)
3706 (org-set-local 'org-agenda-name name)))
3707 (setq buffer-read-only nil))))
3709 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3710 (defun org-agenda-finalize ()
3711 "Finishing touch for the agenda buffer, called just before displaying it."
3712 (unless org-agenda-multi
3713 (save-excursion
3714 (let ((inhibit-read-only t))
3715 (goto-char (point-min))
3716 (save-excursion
3717 (while (org-activate-bracket-links (point-max))
3718 (add-text-properties (match-beginning 0) (match-end 0)
3719 '(face org-link))))
3720 (save-excursion
3721 (while (org-activate-plain-links (point-max))
3722 (add-text-properties (match-beginning 0) (match-end 0)
3723 '(face org-link))))
3724 (unless (eq org-agenda-remove-tags t)
3725 (org-agenda-align-tags))
3726 (unless org-agenda-with-colors
3727 (remove-text-properties (point-min) (point-max) '(face nil)))
3728 (if (and (boundp 'org-agenda-overriding-columns-format)
3729 org-agenda-overriding-columns-format)
3730 (org-set-local 'org-agenda-overriding-columns-format
3731 org-agenda-overriding-columns-format))
3732 (if (and (boundp 'org-agenda-view-columns-initially)
3733 org-agenda-view-columns-initially)
3734 (org-agenda-columns))
3735 (when org-agenda-fontify-priorities
3736 (org-agenda-fontify-priorities))
3737 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3738 (org-agenda-dim-blocked-tasks))
3739 (org-agenda-mark-clocking-task)
3740 (when org-agenda-entry-text-mode
3741 (org-agenda-entry-text-hide)
3742 (org-agenda-entry-text-show))
3743 (if (and (functionp 'org-habit-insert-consistency-graphs)
3744 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3745 (org-habit-insert-consistency-graphs))
3746 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3747 (unless (or (eq org-agenda-show-inherited-tags 'always)
3748 (and (listp org-agenda-show-inherited-tags)
3749 (memq org-agenda-type org-agenda-show-inherited-tags))
3750 (and (eq org-agenda-show-inherited-tags t)
3751 (or (eq org-agenda-use-tag-inheritance t)
3752 (and (listp org-agenda-use-tag-inheritance)
3753 (not (memq org-agenda-type
3754 org-agenda-use-tag-inheritance))))))
3755 (let (mrk)
3756 (save-excursion
3757 (goto-char (point-min))
3758 (while (equal (forward-line) 0)
3759 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3760 (put-text-property (point-at-bol) (point-at-eol)
3761 'tags (org-with-point-at mrk
3762 (delete-dups
3763 (mapcar 'downcase (org-get-tags-at))))))))))
3764 (run-hooks 'org-agenda-finalize-hook)
3765 (when org-agenda-top-headline-filter
3766 (org-agenda-filter-top-headline-apply
3767 org-agenda-top-headline-filter))
3768 (when org-agenda-tag-filter
3769 (org-agenda-filter-apply org-agenda-tag-filter 'tag t))
3770 (when (get 'org-agenda-tag-filter :preset-filter)
3771 (org-agenda-filter-apply
3772 (get 'org-agenda-tag-filter :preset-filter) 'tag t))
3773 (when org-agenda-category-filter
3774 (org-agenda-filter-apply org-agenda-category-filter 'category))
3775 (when (get 'org-agenda-category-filter :preset-filter)
3776 (org-agenda-filter-apply
3777 (get 'org-agenda-category-filter :preset-filter) 'category))
3778 (when org-agenda-regexp-filter
3779 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3780 (when (get 'org-agenda-regexp-filter :preset-filter)
3781 (org-agenda-filter-apply
3782 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3783 (when org-agenda-effort-filter
3784 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3785 (when (get 'org-agenda-effort-filter :preset-filter)
3786 (org-agenda-filter-apply
3787 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3788 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3790 (defun org-agenda-mark-clocking-task ()
3791 "Mark the current clock entry in the agenda if it is present."
3792 ;; We need to widen when `org-agenda-finalize' is called from
3793 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3794 (when org-clock-current-task
3795 (save-restriction
3796 (widen)
3797 (org-agenda-unmark-clocking-task)
3798 (when (marker-buffer org-clock-hd-marker)
3799 (save-excursion
3800 (goto-char (point-min))
3801 (let (s ov)
3802 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3803 (goto-char s)
3804 (when (equal (org-get-at-bol 'org-hd-marker)
3805 org-clock-hd-marker)
3806 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3807 (overlay-put ov 'type 'org-agenda-clocking)
3808 (overlay-put ov 'face 'org-agenda-clocking)
3809 (overlay-put ov 'help-echo
3810 "The clock is running in this item")))))))))
3812 (defun org-agenda-unmark-clocking-task ()
3813 "Unmark the current clocking task."
3814 (mapc (lambda (o)
3815 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3816 (delete-overlay o)))
3817 (overlays-in (point-min) (point-max))))
3819 (defun org-agenda-fontify-priorities ()
3820 "Make highest priority lines bold, and lowest italic."
3821 (interactive)
3822 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3823 (delete-overlay o)))
3824 (overlays-in (point-min) (point-max)))
3825 (save-excursion
3826 (let (b e p ov h l)
3827 (goto-char (point-min))
3828 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3829 (setq h (or (get-char-property (point) 'org-highest-priority)
3830 org-highest-priority)
3831 l (or (get-char-property (point) 'org-lowest-priority)
3832 org-lowest-priority)
3833 p (string-to-char (match-string 1))
3834 b (match-beginning 0)
3835 e (if (eq org-agenda-fontify-priorities 'cookies)
3836 (match-end 0)
3837 (point-at-eol))
3838 ov (make-overlay b e))
3839 (overlay-put
3840 ov 'face
3841 (let ((special-face
3842 (cond ((org-face-from-face-or-color
3843 'priority nil
3844 (cdr (assoc p org-priority-faces))))
3845 ((and (listp org-agenda-fontify-priorities)
3846 (org-face-from-face-or-color
3847 'priority nil
3848 (cdr (assoc p org-agenda-fontify-priorities)))))
3849 ((equal p l) 'italic)
3850 ((equal p h) 'bold))))
3851 (if special-face (list special-face 'org-priority) 'org-priority)))
3852 (overlay-put ov 'org-type 'org-priority)))))
3854 (defvar org-depend-tag-blocked)
3856 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3857 "Dim currently blocked TODOs in the agenda display.
3858 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3859 dimming them."
3860 (interactive "P")
3861 (when (org-called-interactively-p 'interactive)
3862 (message "Dim or hide blocked tasks..."))
3863 (dolist (o (overlays-in (point-min) (point-max)))
3864 (when (eq (overlay-get o 'org-type) 'org-blocked-todo)
3865 (delete-overlay o)))
3866 (save-excursion
3867 (let ((inhibit-read-only t)
3868 (org-depend-tag-blocked nil)
3869 org-blocked-by-checkboxes)
3870 (goto-char (point-min))
3871 (while (let ((pos (text-property-not-all
3872 (point) (point-max) 'todo-state nil)))
3873 (when pos (goto-char pos)))
3874 (setq org-blocked-by-checkboxes nil)
3875 (let ((marker (org-get-at-bol 'org-hd-marker)))
3876 (when (and (markerp marker)
3877 (with-current-buffer (marker-buffer marker)
3878 (save-excursion (goto-char marker)
3879 (org-entry-blocked-p))))
3880 ;; Entries blocked by checkboxes cannot be made invisible.
3881 ;; See `org-agenda-dim-blocked-tasks' for details.
3882 (let* ((really-invisible
3883 (and (not org-blocked-by-checkboxes)
3884 (or invisible (eq org-agenda-dim-blocked-tasks
3885 'invisible))))
3886 (ov (make-overlay (if really-invisible (line-end-position 0)
3887 (line-beginning-position))
3888 (line-end-position))))
3889 (if really-invisible (overlay-put ov 'invisible t)
3890 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3891 (overlay-put ov 'org-type 'org-blocked-todo))))
3892 (forward-line))))
3893 (when (org-called-interactively-p 'interactive)
3894 (message "Dim or hide blocked tasks...done")))
3896 (defvar org-agenda-skip-function nil
3897 "Function to be called at each match during agenda construction.
3898 If this function returns nil, the current match should not be skipped.
3899 Otherwise, the function must return a position from where the search
3900 should be continued.
3901 This may also be a Lisp form, it will be evaluated.
3902 Never set this variable using `setq' or so, because then it will apply
3903 to all future agenda commands. If you do want a global skipping condition,
3904 use the option `org-agenda-skip-function-global' instead.
3905 The correct usage for `org-agenda-skip-function' is to bind it with
3906 `let' to scope it dynamically into the agenda-constructing command.
3907 A good way to set it is through options in `org-agenda-custom-commands'.")
3909 (defun org-agenda-skip ()
3910 "Throw to `:skip' in places that should be skipped.
3911 Also moves point to the end of the skipped region, so that search can
3912 continue from there."
3913 (let ((p (point-at-bol)) to)
3914 (when (or
3915 (save-excursion (goto-char p) (looking-at comment-start-skip))
3916 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3917 (get-text-property p :org-archived)
3918 (org-end-of-subtree t))
3919 (and org-agenda-skip-comment-trees
3920 (get-text-property p :org-comment)
3921 (org-end-of-subtree t))
3922 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3923 (org-agenda-skip-eval org-agenda-skip-function)))
3924 (goto-char to))
3925 (org-in-src-block-p t))
3926 (throw :skip t))))
3928 (defun org-agenda-skip-eval (form)
3929 "If FORM is a function or a list, call (or eval) it and return the result.
3930 `save-excursion' and `save-match-data' are wrapped around the call, so point
3931 and match data are returned to the previous state no matter what these
3932 functions do."
3933 (let (fp)
3934 (and form
3935 (or (setq fp (functionp form))
3936 (consp form))
3937 (save-excursion
3938 (save-match-data
3939 (if fp
3940 (funcall form)
3941 (eval form)))))))
3943 (defvar org-agenda-markers nil
3944 "List of all currently active markers created by `org-agenda'.")
3945 (defvar org-agenda-last-marker-time (org-float-time)
3946 "Creation time of the last agenda marker.")
3948 (defun org-agenda-new-marker (&optional pos)
3949 "Return a new agenda marker.
3950 Maker is at point, or at POS if non-nil. Org mode keeps a list of
3951 these markers and resets them when they are no longer in use."
3952 (let ((m (copy-marker (or pos (point)) t)))
3953 (setq org-agenda-last-marker-time (org-float-time))
3954 (if org-agenda-buffer
3955 (with-current-buffer org-agenda-buffer
3956 (push m org-agenda-markers))
3957 (push m org-agenda-markers))
3960 (defun org-agenda-reset-markers ()
3961 "Reset markers created by `org-agenda'."
3962 (while org-agenda-markers
3963 (move-marker (pop org-agenda-markers) nil)))
3965 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3966 "Save relative positions of markers in region.
3967 This check for agenda markers in all agenda buffers currently active."
3968 (dolist (buf (buffer-list))
3969 (with-current-buffer buf
3970 (when (eq major-mode 'org-agenda-mode)
3971 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3972 org-agenda-markers)))))
3974 ;;; Entry text mode
3976 (defun org-agenda-entry-text-show-here ()
3977 "Add some text from the entry as context to the current line."
3978 (let (m txt o)
3979 (setq m (org-get-at-bol 'org-hd-marker))
3980 (unless (marker-buffer m)
3981 (error "No marker points to an entry here"))
3982 (setq txt (concat "\n" (org-no-properties
3983 (org-agenda-get-some-entry-text
3984 m org-agenda-entry-text-maxlines
3985 org-agenda-entry-text-leaders))))
3986 (when (string-match "\\S-" txt)
3987 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3988 (overlay-put o 'evaporate t)
3989 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3990 (overlay-put o 'after-string txt))))
3992 (defun org-agenda-entry-text-show ()
3993 "Add entry context for all agenda lines."
3994 (interactive)
3995 (save-excursion
3996 (goto-char (point-max))
3997 (beginning-of-line 1)
3998 (while (not (bobp))
3999 (when (org-get-at-bol 'org-hd-marker)
4000 (org-agenda-entry-text-show-here))
4001 (beginning-of-line 0))))
4003 (defun org-agenda-entry-text-hide ()
4004 "Remove any shown entry context."
4005 (delq nil
4006 (mapcar (lambda (o)
4007 (if (eq (overlay-get o 'org-overlay-type)
4008 'agenda-entry-content)
4009 (progn (delete-overlay o) t)))
4010 (overlays-in (point-min) (point-max)))))
4012 (defun org-agenda-get-day-face (date)
4013 "Return the face DATE should be displayed with."
4014 (or (and (functionp org-agenda-day-face-function)
4015 (funcall org-agenda-day-face-function date))
4016 (cond ((org-agenda-todayp date)
4017 'org-agenda-date-today)
4018 ((member (calendar-day-of-week date) org-agenda-weekend-days)
4019 'org-agenda-date-weekend)
4020 (t 'org-agenda-date))))
4022 ;;; Agenda timeline
4024 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4025 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4027 (defun org-timeline (&optional dotodo)
4028 "Show a time-sorted view of the entries in the current org file.
4029 Only entries with a time stamp of today or later will be listed. With
4030 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4031 under the current date.
4032 If the buffer contains an active region, only check the region for
4033 dates."
4034 (interactive "P")
4035 (let* ((dopast t)
4036 (org-agenda-show-log-scoped org-agenda-show-log)
4037 (org-agenda-show-log org-agenda-show-log-scoped)
4038 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4039 (current-buffer))))
4040 (date (calendar-current-date))
4041 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4042 (end (if (org-region-active-p) (region-end) (point-max)))
4043 (day-numbers (org-get-all-dates
4044 beg end 'no-ranges
4045 t org-agenda-show-log-scoped ; always include today
4046 org-timeline-show-empty-dates))
4047 (org-deadline-warning-days 0)
4048 (org-agenda-only-exact-dates t)
4049 (today (org-today))
4050 (past t)
4051 args
4052 s e rtn d emptyp)
4053 (setq org-agenda-redo-command
4054 (list 'let
4055 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4056 (list 'org-switch-to-buffer-other-window (current-buffer))
4057 (list 'org-timeline (list 'quote dotodo))))
4058 (put 'org-agenda-redo-command 'org-lprops nil)
4059 (if (not dopast)
4060 ;; Remove past dates from the list of dates.
4061 (setq day-numbers (delq nil (mapcar (lambda(x)
4062 (if (>= x today) x nil))
4063 day-numbers))))
4064 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4065 (org-compile-prefix-format 'timeline)
4066 (org-set-sorting-strategy 'timeline)
4067 (if org-agenda-show-log-scoped (push :closed args))
4068 (push :timestamp args)
4069 (push :deadline args)
4070 (push :scheduled args)
4071 (push :sexp args)
4072 (if dotodo (push :todo args))
4073 (insert "Timeline of file " entry "\n")
4074 (add-text-properties (point-min) (point)
4075 (list 'face 'org-agenda-structure))
4076 (org-agenda-mark-header-line (point-min))
4077 (while (setq d (pop day-numbers))
4078 (if (and (listp d) (eq (car d) :omitted))
4079 (progn
4080 (setq s (point))
4081 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4082 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4083 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4084 (if (and (>= d today)
4085 dopast
4086 past)
4087 (progn
4088 (setq past nil)
4089 (insert (make-string 79 ?-) "\n")))
4090 (setq date (calendar-gregorian-from-absolute d))
4091 (setq s (point))
4092 (setq rtn (and (not emptyp)
4093 (apply 'org-agenda-get-day-entries entry
4094 date args)))
4095 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4096 (progn
4097 (insert
4098 (if (stringp org-agenda-format-date)
4099 (format-time-string org-agenda-format-date
4100 (org-time-from-absolute date))
4101 (funcall org-agenda-format-date date))
4102 "\n")
4103 (put-text-property s (1- (point)) 'face
4104 (org-agenda-get-day-face date))
4105 (put-text-property s (1- (point)) 'org-date-line t)
4106 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4107 (if (equal d today)
4108 (put-text-property s (1- (point)) 'org-today t))
4109 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4110 (put-text-property s (1- (point)) 'day d)))))
4111 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4112 (point-min)))
4113 (add-text-properties
4114 (point-min) (point-max)
4115 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4116 (org-agenda-finalize)
4117 (setq buffer-read-only t)))
4119 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4120 "Return a list of all relevant day numbers from BEG to END buffer positions.
4121 If NO-RANGES is non-nil, include only the start and end dates of a range,
4122 not every single day in the range. If FORCE-TODAY is non-nil, make
4123 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4124 inactive time stamps (those in square brackets) are included.
4125 When EMPTY is non-nil, also include days without any entries."
4126 (let ((re (concat
4127 (if pre-re pre-re "")
4128 (if inactive org-ts-regexp-both org-ts-regexp)))
4129 dates dates1 date day day1 day2 ts1 ts2 pos)
4130 (if force-today
4131 (setq dates (list (org-today))))
4132 (save-excursion
4133 (goto-char beg)
4134 (while (re-search-forward re end t)
4135 (setq day (time-to-days (org-time-string-to-time
4136 (substring (match-string 1) 0 10)
4137 (current-buffer) (match-beginning 0))))
4138 (or (memq day dates) (push day dates)))
4139 (unless no-ranges
4140 (goto-char beg)
4141 (while (re-search-forward org-tr-regexp end t)
4142 (setq pos (match-beginning 0))
4143 (setq ts1 (substring (match-string 1) 0 10)
4144 ts2 (substring (match-string 2) 0 10)
4145 day1 (time-to-days (org-time-string-to-time
4146 ts1 (current-buffer) pos))
4147 day2 (time-to-days (org-time-string-to-time
4148 ts2 (current-buffer) pos)))
4149 (while (< (setq day1 (1+ day1)) day2)
4150 (or (memq day1 dates) (push day1 dates)))))
4151 (setq dates (sort dates '<))
4152 (when empty
4153 (while (setq day (pop dates))
4154 (setq day2 (car dates))
4155 (push day dates1)
4156 (when (and day2 empty)
4157 (if (or (eq empty t)
4158 (and (numberp empty) (<= (- day2 day) empty)))
4159 (while (< (setq day (1+ day)) day2)
4160 (push (list day) dates1))
4161 (push (cons :omitted (- day2 day)) dates1))))
4162 (setq dates (nreverse dates1)))
4163 dates)))
4165 ;;; Agenda Daily/Weekly
4167 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4168 "Start day for the agenda view.
4169 Custom commands can set this variable in the options section.
4170 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4171 input allowed when reading a date through the Org calendar.
4172 See the docstring of `org-read-date' for details.")
4173 (defvar org-starting-day nil) ; local variable in the agenda buffer
4174 (defvar org-arg-loc nil) ; local variable
4176 (defvar org-agenda-buffer-tmp-name nil)
4177 ;;;###autoload
4178 (defun org-agenda-list (&optional arg start-day span with-hour)
4179 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4180 The view will be for the current day or week, but from the overview buffer
4181 you will be able to go to other days/weeks.
4183 With a numeric prefix argument in an interactive call, the agenda will
4184 span ARG days. Lisp programs should instead specify SPAN to change
4185 the number of days. SPAN defaults to `org-agenda-span'.
4187 START-DAY defaults to TODAY, or to the most recent match for the weekday
4188 given in `org-agenda-start-on-weekday'.
4190 When WITH-HOUR is non-nil, only include scheduled and deadline
4191 items if they have an hour specification like [h]h:mm."
4192 (interactive "P")
4193 (if org-agenda-overriding-arguments
4194 (setq arg (car org-agenda-overriding-arguments)
4195 start-day (nth 1 org-agenda-overriding-arguments)
4196 span (nth 2 org-agenda-overriding-arguments)))
4197 (if (and (integerp arg) (> arg 0))
4198 (setq span arg arg nil))
4199 (catch 'exit
4200 (setq org-agenda-buffer-name
4201 (or org-agenda-buffer-tmp-name
4202 (if org-agenda-sticky
4203 (cond ((and org-keys (stringp org-match))
4204 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4205 (org-keys
4206 (format "*Org Agenda(%s)*" org-keys))
4207 (t "*Org Agenda(a)*")))
4208 org-agenda-buffer-name))
4209 (org-agenda-prepare "Day/Week")
4210 (setq start-day (or start-day org-agenda-start-day))
4211 (if (stringp start-day)
4212 ;; Convert to an absolute day number
4213 (setq start-day (time-to-days (org-read-date nil t start-day))))
4214 (org-compile-prefix-format 'agenda)
4215 (org-set-sorting-strategy 'agenda)
4216 (let* ((span (org-agenda-ndays-to-span
4217 (or span org-agenda-ndays org-agenda-span)))
4218 (today (org-today))
4219 (sd (or start-day today))
4220 (ndays (org-agenda-span-to-ndays span sd))
4221 (org-agenda-start-on-weekday
4222 (if (or (eq ndays 7) (eq ndays 14))
4223 org-agenda-start-on-weekday))
4224 (thefiles (org-agenda-files nil 'ifmode))
4225 (files thefiles)
4226 (start (if (or (null org-agenda-start-on-weekday)
4227 (< ndays 7))
4229 (let* ((nt (calendar-day-of-week
4230 (calendar-gregorian-from-absolute sd)))
4231 (n1 org-agenda-start-on-weekday)
4232 (d (- nt n1)))
4233 (- sd (+ (if (< d 0) 7 0) d)))))
4234 (day-numbers (list start))
4235 (day-cnt 0)
4236 (inhibit-redisplay (not debug-on-error))
4237 (org-agenda-show-log-scoped org-agenda-show-log)
4238 s e rtn rtnall file date d start-pos end-pos todayp
4239 clocktable-start clocktable-end filter)
4240 (setq org-agenda-redo-command
4241 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4242 (dotimes (n (1- ndays))
4243 (push (1+ (car day-numbers)) day-numbers))
4244 (setq day-numbers (nreverse day-numbers))
4245 (setq clocktable-start (car day-numbers)
4246 clocktable-end (1+ (or (org-last day-numbers) 0)))
4247 (org-set-local 'org-starting-day (car day-numbers))
4248 (org-set-local 'org-arg-loc arg)
4249 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4250 (unless org-agenda-compact-blocks
4251 (let* ((d1 (car day-numbers))
4252 (d2 (org-last day-numbers))
4253 (w1 (org-days-to-iso-week d1))
4254 (w2 (org-days-to-iso-week d2)))
4255 (setq s (point))
4256 (if org-agenda-overriding-header
4257 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4258 nil 'face 'org-agenda-structure) "\n")
4259 (insert (org-agenda-span-name span)
4260 "-agenda"
4261 (if (< (- d2 d1) 350)
4262 (if (= w1 w2)
4263 (format " (W%02d)" w1)
4264 (format " (W%02d-W%02d)" w1 w2))
4266 ":\n")))
4267 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4268 'org-date-line t))
4269 (org-agenda-mark-header-line s))
4270 (while (setq d (pop day-numbers))
4271 (setq date (calendar-gregorian-from-absolute d)
4272 s (point))
4273 (if (or (setq todayp (= d today))
4274 (and (not start-pos) (= d sd)))
4275 (setq start-pos (point))
4276 (if (and start-pos (not end-pos))
4277 (setq end-pos (point))))
4278 (setq files thefiles
4279 rtnall nil)
4280 (while (setq file (pop files))
4281 (catch 'nextfile
4282 (org-check-agenda-file file)
4283 (let ((org-agenda-entry-types org-agenda-entry-types))
4284 ;; Starred types override non-starred equivalents
4285 (when (member :deadline* org-agenda-entry-types)
4286 (setq org-agenda-entry-types
4287 (delq :deadline org-agenda-entry-types)))
4288 (when (member :scheduled* org-agenda-entry-types)
4289 (setq org-agenda-entry-types
4290 (delq :scheduled org-agenda-entry-types)))
4291 ;; Honor with-hour
4292 (when with-hour
4293 (when (member :deadline org-agenda-entry-types)
4294 (setq org-agenda-entry-types
4295 (delq :deadline org-agenda-entry-types))
4296 (push :deadline* org-agenda-entry-types))
4297 (when (member :scheduled org-agenda-entry-types)
4298 (setq org-agenda-entry-types
4299 (delq :scheduled org-agenda-entry-types))
4300 (push :scheduled* org-agenda-entry-types)))
4301 (unless org-agenda-include-deadlines
4302 (setq org-agenda-entry-types
4303 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4304 (cond
4305 ((memq org-agenda-show-log-scoped '(only clockcheck))
4306 (setq rtn (org-agenda-get-day-entries
4307 file date :closed)))
4308 (org-agenda-show-log-scoped
4309 (setq rtn (apply 'org-agenda-get-day-entries
4310 file date
4311 (append '(:closed) org-agenda-entry-types))))
4313 (setq rtn (apply 'org-agenda-get-day-entries
4314 file date
4315 org-agenda-entry-types)))))
4316 (setq rtnall (append rtnall rtn)))) ;; all entries
4317 (if org-agenda-include-diary
4318 (let ((org-agenda-search-headline-for-time t))
4319 (require 'diary-lib)
4320 (setq rtn (org-get-entries-from-diary date))
4321 (setq rtnall (append rtnall rtn))))
4322 (if (or rtnall org-agenda-show-all-dates)
4323 (progn
4324 (setq day-cnt (1+ day-cnt))
4325 (insert
4326 (if (stringp org-agenda-format-date)
4327 (format-time-string org-agenda-format-date
4328 (org-time-from-absolute date))
4329 (funcall org-agenda-format-date date))
4330 "\n")
4331 (put-text-property s (1- (point)) 'face
4332 (org-agenda-get-day-face date))
4333 (put-text-property s (1- (point)) 'org-date-line t)
4334 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4335 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4336 (when todayp
4337 (put-text-property s (1- (point)) 'org-today t))
4338 (setq rtnall
4339 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4340 (if rtnall (insert ;; all entries
4341 (org-agenda-finalize-entries rtnall 'agenda)
4342 "\n"))
4343 (put-text-property s (1- (point)) 'day d)
4344 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4345 (when (and org-agenda-clockreport-mode clocktable-start)
4346 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4347 ;; the above line is to ensure the restricted range!
4348 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4349 tbl)
4350 (setq p (org-plist-delete p :block))
4351 (setq p (plist-put p :tstart clocktable-start))
4352 (setq p (plist-put p :tend clocktable-end))
4353 (setq p (plist-put p :scope 'agenda))
4354 (setq tbl (apply 'org-clock-get-clocktable p))
4355 (insert tbl)))
4356 (goto-char (point-min))
4357 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4358 (unless (and (pos-visible-in-window-p (point-min))
4359 (pos-visible-in-window-p (point-max)))
4360 (goto-char (1- (point-max)))
4361 (recenter -1)
4362 (if (not (pos-visible-in-window-p (or start-pos 1)))
4363 (progn
4364 (goto-char (or start-pos 1))
4365 (recenter 1))))
4366 (goto-char (or start-pos 1))
4367 (add-text-properties (point-min) (point-max)
4368 `(org-agenda-type agenda
4369 org-last-args (,arg ,start-day ,span)
4370 org-redo-cmd ,org-agenda-redo-command
4371 org-series-cmd ,org-cmd))
4372 (if (eq org-agenda-show-log-scoped 'clockcheck)
4373 (org-agenda-show-clocking-issues))
4374 (org-agenda-finalize)
4375 (setq buffer-read-only t)
4376 (message ""))))
4378 (defun org-agenda-ndays-to-span (n)
4379 "Return a span symbol for a span of N days, or N if none matches."
4380 (cond ((symbolp n) n)
4381 ((= n 1) 'day)
4382 ((= n 7) 'week)
4383 ((= n 14) 'fortnight)
4384 (t n)))
4386 (defun org-agenda-span-to-ndays (span &optional start-day)
4387 "Return ndays from SPAN, possibly starting at START-DAY.
4388 START-DAY is an absolute time value."
4389 (cond ((numberp span) span)
4390 ((eq span 'day) 1)
4391 ((eq span 'week) 7)
4392 ((eq span 'fortnight) 14)
4393 ((eq span 'month)
4394 (let ((date (calendar-gregorian-from-absolute start-day)))
4395 (calendar-last-day-of-month (car date) (caddr date))))
4396 ((eq span 'year)
4397 (let ((date (calendar-gregorian-from-absolute start-day)))
4398 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4400 (defun org-agenda-span-name (span)
4401 "Return a SPAN name."
4402 (if (null span)
4404 (if (symbolp span)
4405 (capitalize (symbol-name span))
4406 (format "%d days" span))))
4408 ;;; Agenda word search
4410 (defvar org-agenda-search-history nil)
4412 (defvar org-search-syntax-table nil
4413 "Special syntax table for org-mode search.
4414 In this table, we have single quotes not as word constituents, to
4415 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4417 (defvar org-mode-syntax-table) ; From org.el
4418 (defun org-search-syntax-table ()
4419 (unless org-search-syntax-table
4420 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4421 (modify-syntax-entry ?' "." org-search-syntax-table)
4422 (modify-syntax-entry ?` "." org-search-syntax-table))
4423 org-search-syntax-table)
4425 (defvar org-agenda-last-search-view-search-was-boolean nil)
4427 ;;;###autoload
4428 (defun org-search-view (&optional todo-only string edit-at)
4429 "Show all entries that contain a phrase or words or regular expressions.
4431 With optional prefix argument TODO-ONLY, only consider entries that are
4432 TODO entries. The argument STRING can be used to pass a default search
4433 string into this function. If EDIT-AT is non-nil, it means that the
4434 user should get a chance to edit this string, with cursor at position
4435 EDIT-AT.
4437 The search string can be viewed either as a phrase that should be found as
4438 is, or it can be broken into a number of snippets, each of which must match
4439 in a Boolean way to select an entry. The default depends on the variable
4440 `org-agenda-search-view-always-boolean'.
4441 Even if this is turned off (the default) you can always switch to
4442 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4444 The default is a direct search of the whole phrase, where each space in
4445 the search string can expand to an arbitrary amount of whitespace,
4446 including newlines.
4448 If using a Boolean search, the search string is split on whitespace and
4449 each snippet is searched separately, with logical AND to select an entry.
4450 Words prefixed with a minus must *not* occur in the entry. Words without
4451 a prefix or prefixed with a plus must occur in the entry. Matching is
4452 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4453 match whole words, not parts of a word) if
4454 `org-agenda-search-view-force-full-words' is set (default is nil).
4456 Boolean search snippets enclosed by curly braces are interpreted as
4457 regular expressions that must or (when preceded with \"-\") must not
4458 match in the entry. Snippets enclosed into double quotes will be taken
4459 as a whole, to include whitespace.
4461 - If the search string starts with an asterisk, search only in headlines.
4462 - If (possibly after the leading star) the search string starts with an
4463 exclamation mark, this also means to look at TODO entries only, an effect
4464 that can also be achieved with a prefix argument.
4465 - If (possibly after star and exclamation mark) the search string starts
4466 with a colon, this will mean that the (non-regexp) snippets of the
4467 Boolean search must match as full words.
4469 This command searches the agenda files, and in addition the files listed
4470 in `org-agenda-text-search-extra-files'."
4471 (interactive "P")
4472 (if org-agenda-overriding-arguments
4473 (setq todo-only (car org-agenda-overriding-arguments)
4474 string (nth 1 org-agenda-overriding-arguments)
4475 edit-at (nth 2 org-agenda-overriding-arguments)))
4476 (let* ((props (list 'face nil
4477 'done-face 'org-agenda-done
4478 'org-not-done-regexp org-not-done-regexp
4479 'org-todo-regexp org-todo-regexp
4480 'org-complex-heading-regexp org-complex-heading-regexp
4481 'mouse-face 'highlight
4482 'help-echo (format "mouse-2 or RET jump to location")))
4483 (full-words org-agenda-search-view-force-full-words)
4484 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4485 regexp rtn rtnall files file pos inherited-tags
4486 marker category level tags c neg re boolean
4487 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4488 (unless (and (not edit-at)
4489 (stringp string)
4490 (string-match "\\S-" string))
4491 (setq string (read-string
4492 (if org-agenda-search-view-always-boolean
4493 "[+-]Word/{Regexp} ...: "
4494 "Phrase or [+-]Word/{Regexp} ...: ")
4495 (cond
4496 ((integerp edit-at) (cons string edit-at))
4497 (edit-at string))
4498 'org-agenda-search-history)))
4499 (catch 'exit
4500 (if org-agenda-sticky
4501 (setq org-agenda-buffer-name
4502 (if (stringp string)
4503 (format "*Org Agenda(%s:%s)*"
4504 (or org-keys (or (and todo-only "S") "s")) string)
4505 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4506 (org-agenda-prepare "SEARCH")
4507 (org-compile-prefix-format 'search)
4508 (org-set-sorting-strategy 'search)
4509 (setq org-agenda-redo-command
4510 (list 'org-search-view (if todo-only t nil)
4511 (list 'if 'current-prefix-arg nil string)))
4512 (setq org-agenda-query-string string)
4513 (if (equal (string-to-char string) ?*)
4514 (setq hdl-only t
4515 words (substring string 1))
4516 (setq words string))
4517 (when (equal (string-to-char words) ?!)
4518 (setq todo-only t
4519 words (substring words 1)))
4520 (when (equal (string-to-char words) ?:)
4521 (setq full-words t
4522 words (substring words 1)))
4523 (if (or org-agenda-search-view-always-boolean
4524 (member (string-to-char words) '(?- ?+ ?\{)))
4525 (setq boolean t))
4526 (setq words (org-split-string words))
4527 (let (www w)
4528 (while (setq w (pop words))
4529 (while (and (string-match "\\\\\\'" w) words)
4530 (setq w (concat (substring w 0 -1) " " (pop words))))
4531 (push w www))
4532 (setq words (nreverse www) www nil)
4533 (while (setq w (pop words))
4534 (when (and (string-match "\\`[-+]?{" w)
4535 (not (string-match "}\\'" w)))
4536 (while (and words (not (string-match "}\\'" (car words))))
4537 (setq w (concat w " " (pop words))))
4538 (setq w (concat w " " (pop words))))
4539 (push w www))
4540 (setq words (nreverse www)))
4541 (setq org-agenda-last-search-view-search-was-boolean boolean)
4542 (when boolean
4543 (let (wds w)
4544 (while (setq w (pop words))
4545 (if (or (equal (substring w 0 1) "\"")
4546 (and (> (length w) 1)
4547 (member (substring w 0 1) '("+" "-"))
4548 (equal (substring w 1 2) "\"")))
4549 (while (and words (not (equal (substring w -1) "\"")))
4550 (setq w (concat w " " (pop words)))))
4551 (and (string-match "\\`\\([-+]?\\)\"" w)
4552 (setq w (replace-match "\\1" nil nil w)))
4553 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4554 (push w wds))
4555 (setq words (nreverse wds))))
4556 (if boolean
4557 (mapc (lambda (w)
4558 (setq c (string-to-char w))
4559 (if (equal c ?-)
4560 (setq neg t w (substring w 1))
4561 (if (equal c ?+)
4562 (setq neg nil w (substring w 1))
4563 (setq neg nil)))
4564 (if (string-match "\\`{.*}\\'" w)
4565 (setq re (substring w 1 -1))
4566 (if full-words
4567 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4568 (setq re (regexp-quote (downcase w)))))
4569 (if neg (push re regexps-) (push re regexps+)))
4570 words)
4571 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4572 regexps+))
4573 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4574 (if (not regexps+)
4575 (setq regexp org-outline-regexp-bol)
4576 (setq regexp (pop regexps+))
4577 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4578 regexp))))
4579 (setq files (org-agenda-files nil 'ifmode))
4580 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4581 (pop org-agenda-text-search-extra-files)
4582 (setq files (org-add-archive-files files)))
4583 (setq files (append files org-agenda-text-search-extra-files)
4584 rtnall nil)
4585 (while (setq file (pop files))
4586 (setq ee nil)
4587 (catch 'nextfile
4588 (org-check-agenda-file file)
4589 (setq buffer (if (file-exists-p file)
4590 (org-get-agenda-file-buffer file)
4591 (error "No such file %s" file)))
4592 (if (not buffer)
4593 ;; If file does not exist, make sure an error message is sent
4594 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4595 file))))
4596 (with-current-buffer buffer
4597 (with-syntax-table (org-search-syntax-table)
4598 (unless (derived-mode-p 'org-mode)
4599 (error "Agenda file %s is not in `org-mode'" file))
4600 (let ((case-fold-search t))
4601 (save-excursion
4602 (save-restriction
4603 (if (eq buffer org-agenda-restrict)
4604 (narrow-to-region org-agenda-restrict-begin
4605 org-agenda-restrict-end)
4606 (widen))
4607 (goto-char (point-min))
4608 (unless (or (org-at-heading-p)
4609 (outline-next-heading))
4610 (throw 'nextfile t))
4611 (goto-char (max (point-min) (1- (point))))
4612 (while (re-search-forward regexp nil t)
4613 (org-back-to-heading t)
4614 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4615 (> (org-reduced-level (org-outline-level))
4616 org-agenda-search-view-max-outline-level)
4617 (forward-line -1)
4618 (outline-back-to-heading t)))
4619 (skip-chars-forward "* ")
4620 (setq beg (point-at-bol)
4621 beg1 (point)
4622 end (progn
4623 (outline-next-heading)
4624 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4625 (> (org-reduced-level (org-outline-level))
4626 org-agenda-search-view-max-outline-level)
4627 (forward-line 1)
4628 (outline-next-heading)))
4629 (point)))
4631 (catch :skip
4632 (goto-char beg)
4633 (org-agenda-skip)
4634 (setq str (buffer-substring-no-properties
4635 (point-at-bol)
4636 (if hdl-only (point-at-eol) end)))
4637 (mapc (lambda (wr) (when (string-match wr str)
4638 (goto-char (1- end))
4639 (throw :skip t)))
4640 regexps-)
4641 (mapc (lambda (wr) (unless (string-match wr str)
4642 (goto-char (1- end))
4643 (throw :skip t)))
4644 (if todo-only
4645 (cons (concat "^\\*+[ \t]+"
4646 org-not-done-regexp)
4647 regexps+)
4648 regexps+))
4649 (goto-char beg)
4650 (setq marker (org-agenda-new-marker (point))
4651 category (org-get-category)
4652 level (make-string (org-reduced-level (org-outline-level)) ? )
4653 inherited-tags
4654 (or (eq org-agenda-show-inherited-tags 'always)
4655 (and (listp org-agenda-show-inherited-tags)
4656 (memq 'todo org-agenda-show-inherited-tags))
4657 (and (eq org-agenda-show-inherited-tags t)
4658 (or (eq org-agenda-use-tag-inheritance t)
4659 (memq 'todo org-agenda-use-tag-inheritance))))
4660 tags (org-get-tags-at nil (not inherited-tags))
4661 txt (org-agenda-format-item
4663 (buffer-substring-no-properties
4664 beg1 (point-at-eol))
4665 level category tags t))
4666 (org-add-props txt props
4667 'org-marker marker 'org-hd-marker marker
4668 'org-todo-regexp org-todo-regexp
4669 'level level
4670 'org-complex-heading-regexp org-complex-heading-regexp
4671 'priority 1000
4672 'type "search")
4673 (push txt ee)
4674 (goto-char (1- end))))))))))
4675 (setq rtn (nreverse ee))
4676 (setq rtnall (append rtnall rtn)))
4677 (if org-agenda-overriding-header
4678 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4679 nil 'face 'org-agenda-structure) "\n")
4680 (insert "Search words: ")
4681 (add-text-properties (point-min) (1- (point))
4682 (list 'face 'org-agenda-structure))
4683 (setq pos (point))
4684 (insert string "\n")
4685 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4686 (setq pos (point))
4687 (unless org-agenda-multi
4688 (insert (substitute-command-keys "\
4689 Press `\\[org-agenda-manipulate-query-add]', \
4690 `\\[org-agenda-manipulate-query-subtract]' to add/sub word, \
4691 `\\[org-agenda-manipulate-query-add-re]', \
4692 `\\[org-agenda-manipulate-query-subtract-re]' to add/sub regexp, \
4693 `\\[universal-argument] \\[org-agenda-redo]' to edit\n"))
4694 (add-text-properties pos (1- (point))
4695 (list 'face 'org-agenda-structure))))
4696 (org-agenda-mark-header-line (point-min))
4697 (when rtnall
4698 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4699 (goto-char (point-min))
4700 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4701 (add-text-properties (point-min) (point-max)
4702 `(org-agenda-type search
4703 org-last-args (,todo-only ,string ,edit-at)
4704 org-redo-cmd ,org-agenda-redo-command
4705 org-series-cmd ,org-cmd))
4706 (org-agenda-finalize)
4707 (setq buffer-read-only t))))
4709 ;;; Agenda TODO list
4711 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4712 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4713 (concat
4714 (if (or (equal keywords "ALL") (not keywords))
4715 (propertize "ALL" 'face 'warning)
4716 (mapconcat
4717 (lambda (kw)
4718 (propertize kw 'face (org-get-todo-face kw)))
4719 (org-split-string keywords "|")
4720 "|"))
4721 "\n"))
4723 (defvar org-select-this-todo-keyword nil)
4724 (defvar org-last-arg nil)
4726 ;;;###autoload
4727 (defun org-todo-list (&optional arg)
4728 "Show all (not done) TODO entries from all agenda file in a single list.
4729 The prefix arg can be used to select a specific TODO keyword and limit
4730 the list to these. When using \\[universal-argument], you will be prompted
4731 for a keyword. A numeric prefix directly selects the Nth keyword in
4732 `org-todo-keywords-1'."
4733 (interactive "P")
4734 (if org-agenda-overriding-arguments
4735 (setq arg org-agenda-overriding-arguments))
4736 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4737 (let* ((today (org-today))
4738 (date (calendar-gregorian-from-absolute today))
4739 (kwds org-todo-keywords-for-agenda)
4740 (completion-ignore-case t)
4741 (org-select-this-todo-keyword
4742 (if (stringp arg) arg
4743 (and arg (integerp arg) (> arg 0)
4744 (nth (1- arg) kwds))))
4745 rtn rtnall files file pos)
4746 (when (equal arg '(4))
4747 (setq org-select-this-todo-keyword
4748 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4749 (mapcar 'list kwds) nil nil)))
4750 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4751 (catch 'exit
4752 (if org-agenda-sticky
4753 (setq org-agenda-buffer-name
4754 (if (stringp org-select-this-todo-keyword)
4755 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4756 org-select-this-todo-keyword)
4757 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4758 (org-agenda-prepare "TODO")
4759 (org-compile-prefix-format 'todo)
4760 (org-set-sorting-strategy 'todo)
4761 (setq org-agenda-redo-command
4762 `(org-todo-list (or (and (numberp current-prefix-arg)
4763 current-prefix-arg)
4764 ,org-select-this-todo-keyword
4765 current-prefix-arg ,arg)))
4766 (setq files (org-agenda-files nil 'ifmode)
4767 rtnall nil)
4768 (while (setq file (pop files))
4769 (catch 'nextfile
4770 (org-check-agenda-file file)
4771 (setq rtn (org-agenda-get-day-entries file date :todo))
4772 (setq rtnall (append rtnall rtn))))
4773 (if org-agenda-overriding-header
4774 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4775 nil 'face 'org-agenda-structure) "\n")
4776 (insert "Global list of TODO items of type: ")
4777 (add-text-properties (point-min) (1- (point))
4778 (list 'face 'org-agenda-structure
4779 'short-heading
4780 (concat "ToDo: "
4781 (or org-select-this-todo-keyword "ALL"))))
4782 (org-agenda-mark-header-line (point-min))
4783 (insert (org-agenda-propertize-selected-todo-keywords
4784 org-select-this-todo-keyword))
4785 (setq pos (point))
4786 (unless org-agenda-multi
4787 (insert (substitute-command-keys "Available with \
4788 `N \\[org-agenda-redo]': (0)[ALL]"))
4789 (let ((n 0) s)
4790 (mapc (lambda (x)
4791 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4792 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4793 (insert "\n "))
4794 (insert " " s))
4795 kwds))
4796 (insert "\n"))
4797 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4798 (org-agenda-mark-header-line (point-min))
4799 (when rtnall
4800 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4801 (goto-char (point-min))
4802 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4803 (add-text-properties (point-min) (point-max)
4804 `(org-agenda-type todo
4805 org-last-args ,arg
4806 org-redo-cmd ,org-agenda-redo-command
4807 org-series-cmd ,org-cmd))
4808 (org-agenda-finalize)
4809 (setq buffer-read-only t))))
4811 ;;; Agenda tags match
4813 ;;;###autoload
4814 (defun org-tags-view (&optional todo-only match)
4815 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4816 The prefix arg TODO-ONLY limits the search to TODO entries."
4817 (interactive "P")
4818 (if org-agenda-overriding-arguments
4819 (setq todo-only (car org-agenda-overriding-arguments)
4820 match (nth 1 org-agenda-overriding-arguments)))
4821 (let* ((org-tags-match-list-sublevels
4822 org-tags-match-list-sublevels)
4823 (completion-ignore-case t)
4824 rtn rtnall files file pos matcher
4825 buffer)
4826 (when (and (stringp match) (not (string-match "\\S-" match)))
4827 (setq match nil))
4828 (catch 'exit
4829 (if org-agenda-sticky
4830 (setq org-agenda-buffer-name
4831 (if (stringp match)
4832 (format "*Org Agenda(%s:%s)*"
4833 (or org-keys (or (and todo-only "M") "m")) match)
4834 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4835 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4836 ;; expanding tags within `org-make-tags-matcher'
4837 (org-agenda-prepare (concat "TAGS " match))
4838 (setq matcher (org-make-tags-matcher match)
4839 match (car matcher) matcher (cdr matcher))
4840 (org-compile-prefix-format 'tags)
4841 (org-set-sorting-strategy 'tags)
4842 (setq org-agenda-query-string match)
4843 (setq org-agenda-redo-command
4844 (list 'org-tags-view `(quote ,todo-only)
4845 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4846 (setq files (org-agenda-files nil 'ifmode)
4847 rtnall nil)
4848 (while (setq file (pop files))
4849 (catch 'nextfile
4850 (org-check-agenda-file file)
4851 (setq buffer (if (file-exists-p file)
4852 (org-get-agenda-file-buffer file)
4853 (error "No such file %s" file)))
4854 (if (not buffer)
4855 ;; If file does not exist, error message to agenda
4856 (setq rtn (list
4857 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4858 rtnall (append rtnall rtn))
4859 (with-current-buffer buffer
4860 (unless (derived-mode-p 'org-mode)
4861 (error "Agenda file %s is not in `org-mode'" file))
4862 (save-excursion
4863 (save-restriction
4864 (if (eq buffer org-agenda-restrict)
4865 (narrow-to-region org-agenda-restrict-begin
4866 org-agenda-restrict-end)
4867 (widen))
4868 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4869 (setq rtnall (append rtnall rtn))))))))
4870 (if org-agenda-overriding-header
4871 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4872 nil 'face 'org-agenda-structure) "\n")
4873 (insert "Headlines with TAGS match: ")
4874 (add-text-properties (point-min) (1- (point))
4875 (list 'face 'org-agenda-structure
4876 'short-heading
4877 (concat "Match: " match)))
4878 (setq pos (point))
4879 (insert match "\n")
4880 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4881 (setq pos (point))
4882 (unless org-agenda-multi
4883 (insert (substitute-command-keys
4884 "Press `\\[universal-argument] \\[org-agenda-redo]' \
4885 to search again with new search string\n")))
4886 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4887 (org-agenda-mark-header-line (point-min))
4888 (when rtnall
4889 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4890 (goto-char (point-min))
4891 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4892 (add-text-properties (point-min) (point-max)
4893 `(org-agenda-type tags
4894 org-last-args (,todo-only ,match)
4895 org-redo-cmd ,org-agenda-redo-command
4896 org-series-cmd ,org-cmd))
4897 (org-agenda-finalize)
4898 (setq buffer-read-only t))))
4900 ;;; Agenda Finding stuck projects
4902 (defvar org-agenda-skip-regexp nil
4903 "Regular expression used in skipping subtrees for the agenda.
4904 This is basically a temporary global variable that can be set and then
4905 used by user-defined selections using `org-agenda-skip-function'.")
4907 (defvar org-agenda-overriding-header nil
4908 "When set during agenda, todo and tags searches it replaces the header.
4909 This variable should not be set directly, but custom commands can bind it
4910 in the options section.")
4912 (defun org-agenda-skip-entry-when-regexp-matches ()
4913 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4914 If yes, it returns the end position of this entry, causing agenda commands
4915 to skip the entry but continuing the search in the subtree. This is a
4916 function that can be put into `org-agenda-skip-function' for the duration
4917 of a command."
4918 (let ((end (save-excursion (org-end-of-subtree t)))
4919 skip)
4920 (save-excursion
4921 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4922 (and skip end)))
4924 (defun org-agenda-skip-subtree-when-regexp-matches ()
4925 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4926 If yes, it returns the end position of this tree, causing agenda commands
4927 to skip this subtree. This is a function that can be put into
4928 `org-agenda-skip-function' for the duration of a command."
4929 (let ((end (save-excursion (org-end-of-subtree t)))
4930 skip)
4931 (save-excursion
4932 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4933 (and skip end)))
4935 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4936 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4937 If yes, it returns the end position of the current entry (NOT the tree),
4938 causing agenda commands to skip the entry but continuing the search in
4939 the subtree. This is a function that can be put into
4940 `org-agenda-skip-function' for the duration of a command. An important
4941 use of this function is for the stuck project list."
4942 (let ((end (save-excursion (org-end-of-subtree t)))
4943 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4944 skip)
4945 (save-excursion
4946 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4947 (and skip entry-end)))
4949 (defun org-agenda-skip-entry-if (&rest conditions)
4950 "Skip entry if any of CONDITIONS is true.
4951 See `org-agenda-skip-if' for details."
4952 (org-agenda-skip-if nil conditions))
4954 (defun org-agenda-skip-subtree-if (&rest conditions)
4955 "Skip subtree if any of CONDITIONS is true.
4956 See `org-agenda-skip-if' for details."
4957 (org-agenda-skip-if t conditions))
4959 (defun org-agenda-skip-if (subtree conditions)
4960 "Checks current entity for CONDITIONS.
4961 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4962 the entry (i.e. the text before the next heading) is checked.
4964 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4965 from different tests. Valid conditions are:
4967 scheduled Check if there is a scheduled cookie
4968 notscheduled Check if there is no scheduled cookie
4969 deadline Check if there is a deadline
4970 notdeadline Check if there is no deadline
4971 timestamp Check if there is a timestamp (also deadline or scheduled)
4972 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4973 regexp Check if regexp matches
4974 notregexp Check if regexp does not match.
4975 todo Check if TODO keyword matches
4976 nottodo Check if TODO keyword does not match
4978 The regexp is taken from the conditions list, it must come right after
4979 the `regexp' or `notregexp' element.
4981 `todo' and `nottodo' accept as an argument a list of todo
4982 keywords, which may include \"*\" to match any todo keyword.
4984 (org-agenda-skip-entry-if \\='todo \\='(\"TODO\" \"WAITING\"))
4986 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4988 Instead of a list, a keyword class may be given. For example:
4990 (org-agenda-skip-entry-if \\='nottodo \\='done)
4992 would skip entries that haven't been marked with any of \"DONE\"
4993 keywords. Possible classes are: `todo', `done', `any'.
4995 If any of these conditions is met, this function returns the end point of
4996 the entity, causing the search to continue from there. This is a function
4997 that can be put into `org-agenda-skip-function' for the duration of a command."
4998 (let (beg end m)
4999 (org-back-to-heading t)
5000 (setq beg (point)
5001 end (if subtree
5002 (progn (org-end-of-subtree t) (point))
5003 (progn (outline-next-heading) (1- (point)))))
5004 (goto-char beg)
5005 (and
5007 (and (memq 'scheduled conditions)
5008 (re-search-forward org-scheduled-time-regexp end t))
5009 (and (memq 'notscheduled conditions)
5010 (not (re-search-forward org-scheduled-time-regexp end t)))
5011 (and (memq 'deadline conditions)
5012 (re-search-forward org-deadline-time-regexp end t))
5013 (and (memq 'notdeadline conditions)
5014 (not (re-search-forward org-deadline-time-regexp end t)))
5015 (and (memq 'timestamp conditions)
5016 (re-search-forward org-ts-regexp end t))
5017 (and (memq 'nottimestamp conditions)
5018 (not (re-search-forward org-ts-regexp end t)))
5019 (and (setq m (memq 'regexp conditions))
5020 (stringp (nth 1 m))
5021 (re-search-forward (nth 1 m) end t))
5022 (and (setq m (memq 'notregexp conditions))
5023 (stringp (nth 1 m))
5024 (not (re-search-forward (nth 1 m) end t)))
5025 (and (or
5026 (setq m (memq 'nottodo conditions))
5027 (setq m (memq 'todo-unblocked conditions))
5028 (setq m (memq 'nottodo-unblocked conditions))
5029 (setq m (memq 'todo conditions)))
5030 (org-agenda-skip-if-todo m end)))
5031 end)))
5033 (defun org-agenda-skip-if-todo (args end)
5034 "Helper function for `org-agenda-skip-if', do not use it directly.
5035 ARGS is a list with first element either `todo', `nottodo',
5036 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5037 a list of TODO keywords, or a state symbol `todo' or `done' or
5038 `any'."
5039 (let ((kw (car args))
5040 (arg (cadr args))
5041 todo-wds todo-re)
5042 (setq todo-wds
5043 (org-uniquify
5044 (cond
5045 ((listp arg) ;; list of keywords
5046 (if (member "*" arg)
5047 (mapcar 'substring-no-properties org-todo-keywords-1)
5048 arg))
5049 ((symbolp arg) ;; keyword class name
5050 (cond
5051 ((eq arg 'todo)
5052 (org-delete-all org-done-keywords
5053 (mapcar 'substring-no-properties
5054 org-todo-keywords-1)))
5055 ((eq arg 'done) org-done-keywords)
5056 ((eq arg 'any)
5057 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5058 (setq todo-re
5059 (concat "^\\*+[ \t]+\\<\\("
5060 (mapconcat 'identity todo-wds "\\|")
5061 "\\)\\>"))
5062 (cond
5063 ((eq kw 'todo) (re-search-forward todo-re end t))
5064 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5065 ((eq kw 'todo-unblocked)
5066 (catch 'unblocked
5067 (while (re-search-forward todo-re end t)
5068 (or (org-entry-blocked-p) (throw 'unblocked t)))
5069 nil))
5070 ((eq kw 'nottodo-unblocked)
5071 (catch 'unblocked
5072 (while (re-search-forward todo-re end t)
5073 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5077 ;;;###autoload
5078 (defun org-agenda-list-stuck-projects (&rest ignore)
5079 "Create agenda view for projects that are stuck.
5080 Stuck projects are project that have no next actions. For the definitions
5081 of what a project is and how to check if it stuck, customize the variable
5082 `org-stuck-projects'."
5083 (interactive)
5084 (let* ((org-agenda-skip-function
5085 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5086 ;; We could have used org-agenda-skip-if here.
5087 (org-agenda-overriding-header
5088 (or org-agenda-overriding-header "List of stuck projects: "))
5089 (matcher (nth 0 org-stuck-projects))
5090 (todo (nth 1 org-stuck-projects))
5091 (todo-wds (if (member "*" todo)
5092 (progn
5093 (org-agenda-prepare-buffers (org-agenda-files
5094 nil 'ifmode))
5095 (org-delete-all
5096 org-done-keywords-for-agenda
5097 (copy-sequence org-todo-keywords-for-agenda)))
5098 todo))
5099 (todo-re (concat "^\\*+[ \t]+\\("
5100 (mapconcat 'identity todo-wds "\\|")
5101 "\\)\\>"))
5102 (tags (nth 2 org-stuck-projects))
5103 (tags-re (if (member "*" tags)
5104 (concat org-outline-regexp-bol
5105 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5106 (if tags
5107 (concat org-outline-regexp-bol
5108 ".*:\\("
5109 (mapconcat 'identity tags "\\|")
5110 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5111 (gen-re (nth 3 org-stuck-projects))
5112 (re-list
5113 (delq nil
5114 (list
5115 (if todo todo-re)
5116 (if tags tags-re)
5117 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5118 gen-re)))))
5119 (setq org-agenda-skip-regexp
5120 (if re-list
5121 (mapconcat 'identity re-list "\\|")
5122 (error "No information how to identify unstuck projects")))
5123 (org-tags-view nil matcher)
5124 (setq org-agenda-buffer-name (buffer-name))
5125 (with-current-buffer org-agenda-buffer-name
5126 (setq org-agenda-redo-command
5127 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5129 ;;; Diary integration
5131 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5132 (defvar diary-list-entries-hook)
5133 (defvar diary-time-regexp)
5134 (defun org-get-entries-from-diary (date)
5135 "Get the (Emacs Calendar) diary entries for DATE."
5136 (require 'diary-lib)
5137 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5138 (diary-display-function 'diary-fancy-display)
5139 (pop-up-frames nil)
5140 (diary-list-entries-hook
5141 (cons 'org-diary-default-entry diary-list-entries-hook))
5142 (diary-file-name-prefix nil) ; turn this feature off
5143 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5144 entries
5145 (org-disable-agenda-to-diary t))
5146 (save-excursion
5147 (save-window-excursion
5148 (funcall (if (fboundp 'diary-list-entries)
5149 'diary-list-entries 'list-diary-entries)
5150 date 1)))
5151 (if (not (get-buffer diary-fancy-buffer))
5152 (setq entries nil)
5153 (with-current-buffer diary-fancy-buffer
5154 (setq buffer-read-only nil)
5155 (if (zerop (buffer-size))
5156 ;; No entries
5157 (setq entries nil)
5158 ;; Omit the date and other unnecessary stuff
5159 (org-agenda-cleanup-fancy-diary)
5160 ;; Add prefix to each line and extend the text properties
5161 (if (zerop (buffer-size))
5162 (setq entries nil)
5163 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5164 (setq entries
5165 (with-temp-buffer
5166 (insert entries) (goto-char (point-min))
5167 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5168 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5169 (replace-match (concat "; " (match-string 1)))))
5170 (buffer-string)))))
5171 (set-buffer-modified-p nil)
5172 (kill-buffer diary-fancy-buffer)))
5173 (when entries
5174 (setq entries (org-split-string entries "\n"))
5175 (setq entries
5176 (mapcar
5177 (lambda (x)
5178 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5179 ;; Extend the text properties to the beginning of the line
5180 (org-add-props x (text-properties-at (1- (length x)) x)
5181 'type "diary" 'date date 'face 'org-agenda-diary))
5182 entries)))))
5184 (defvar org-agenda-cleanup-fancy-diary-hook nil
5185 "Hook run when the fancy diary buffer is cleaned up.")
5187 (defun org-agenda-cleanup-fancy-diary ()
5188 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5189 This gets rid of the date, the underline under the date, and
5190 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5191 date. It also removes lines that contain only whitespace."
5192 (goto-char (point-min))
5193 (if (looking-at ".*?:[ \t]*")
5194 (progn
5195 (replace-match "")
5196 (re-search-forward "\n=+$" nil t)
5197 (replace-match "")
5198 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5199 (re-search-forward "\n=+$" nil t)
5200 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5201 (goto-char (point-min))
5202 (while (re-search-forward "^ +\n" nil t)
5203 (replace-match ""))
5204 (goto-char (point-min))
5205 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5206 (replace-match ""))
5207 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5209 ;; Make sure entries from the diary have the right text properties.
5210 (eval-after-load "diary-lib"
5211 '(if (boundp 'diary-modify-entry-list-string-function)
5212 ;; We can rely on the hook, nothing to do
5214 ;; Hook not available, must use advice to make this work
5215 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5216 "Make the position visible."
5217 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5218 (stringp string)
5219 buffer-file-name)
5220 (setq string (org-modify-diary-entry-string string))))))
5222 (defun org-modify-diary-entry-string (string)
5223 "Add text properties to string, allowing org-mode to act on it."
5224 (org-add-props string nil
5225 'mouse-face 'highlight
5226 'help-echo (if buffer-file-name
5227 (format "mouse-2 or RET jump to diary file %s"
5228 (abbreviate-file-name buffer-file-name))
5230 'org-agenda-diary-link t
5231 'org-marker (org-agenda-new-marker (point-at-bol))))
5233 (defun org-diary-default-entry ()
5234 "Add a dummy entry to the diary.
5235 Needed to avoid empty dates which mess up holiday display."
5236 ;; Catch the error if dealing with the new add-to-diary-alist
5237 (when org-disable-agenda-to-diary
5238 (condition-case nil
5239 (org-add-to-diary-list original-date "Org-mode dummy" "")
5240 (error
5241 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5243 (defun org-add-to-diary-list (&rest args)
5244 (if (fboundp 'diary-add-to-list)
5245 (apply 'diary-add-to-list args)
5246 (apply 'add-to-diary-list args)))
5248 (defvar org-diary-last-run-time nil)
5250 ;;;###autoload
5251 (defun org-diary (&rest args)
5252 "Return diary information from org files.
5253 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5254 It accesses org files and extracts information from those files to be
5255 listed in the diary. The function accepts arguments specifying what
5256 items should be listed. For a list of arguments allowed here, see the
5257 variable `org-agenda-entry-types'.
5259 The call in the diary file should look like this:
5261 &%%(org-diary) ~/path/to/some/orgfile.org
5263 Use a separate line for each org file to check. Or, if you omit the file name,
5264 all files listed in `org-agenda-files' will be checked automatically:
5266 &%%(org-diary)
5268 If you don't give any arguments (as in the example above), the default value
5269 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5270 So the example above may also be written as
5272 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5274 The function expects the lisp variables `entry' and `date' to be provided
5275 by the caller, because this is how the calendar works. Don't use this
5276 function from a program - use `org-agenda-get-day-entries' instead."
5277 (when (> (- (org-float-time)
5278 org-agenda-last-marker-time)
5280 ;; I am not sure if this works with sticky agendas, because the marker
5281 ;; list is then no longer a global variable.
5282 (org-agenda-reset-markers))
5283 (org-compile-prefix-format 'agenda)
5284 (org-set-sorting-strategy 'agenda)
5285 (setq args (or args org-agenda-entry-types))
5286 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5287 (list entry)
5288 (org-agenda-files t)))
5289 (time (org-float-time))
5290 file rtn results)
5291 (when (or (not org-diary-last-run-time)
5292 (> (- time
5293 org-diary-last-run-time)
5295 (org-agenda-prepare-buffers files))
5296 (setq org-diary-last-run-time time)
5297 ;; If this is called during org-agenda, don't return any entries to
5298 ;; the calendar. Org Agenda will list these entries itself.
5299 (if org-disable-agenda-to-diary (setq files nil))
5300 (while (setq file (pop files))
5301 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5302 (setq results (append results rtn)))
5303 (when results
5304 (setq results
5305 (mapcar (lambda (i) (replace-regexp-in-string
5306 org-bracket-link-regexp "\\3" i)) results))
5307 (concat (org-agenda-finalize-entries results) "\n"))))
5309 ;;; Agenda entry finders
5311 (defun org-agenda-get-day-entries (file date &rest args)
5312 "Does the work for `org-diary' and `org-agenda'.
5313 FILE is the path to a file to be checked for entries. DATE is date like
5314 the one returned by `calendar-current-date'. ARGS are symbols indicating
5315 which kind of entries should be extracted. For details about these, see
5316 the documentation of `org-diary'."
5317 (setq args (or args org-agenda-entry-types))
5318 (let* ((org-startup-folded nil)
5319 (org-startup-align-all-tables nil)
5320 (buffer (if (file-exists-p file)
5321 (org-get-agenda-file-buffer file)
5322 (error "No such file %s" file)))
5323 arg results rtn deadline-results)
5324 (if (not buffer)
5325 ;; If file does not exist, make sure an error message ends up in diary
5326 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5327 (with-current-buffer buffer
5328 (unless (derived-mode-p 'org-mode)
5329 (error "Agenda file %s is not in `org-mode'" file))
5330 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5331 (let ((case-fold-search nil))
5332 (save-excursion
5333 (save-restriction
5334 (if (eq buffer org-agenda-restrict)
5335 (narrow-to-region org-agenda-restrict-begin
5336 org-agenda-restrict-end)
5337 (widen))
5338 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5339 (while (setq arg (pop args))
5340 (cond
5341 ((and (eq arg :todo)
5342 (equal date (calendar-gregorian-from-absolute
5343 (org-today))))
5344 (setq rtn (org-agenda-get-todos))
5345 (setq results (append results rtn)))
5346 ((eq arg :timestamp)
5347 (setq rtn (org-agenda-get-blocks))
5348 (setq results (append results rtn))
5349 (setq rtn (org-agenda-get-timestamps deadline-results))
5350 (setq results (append results rtn)))
5351 ((eq arg :sexp)
5352 (setq rtn (org-agenda-get-sexps))
5353 (setq results (append results rtn)))
5354 ((eq arg :scheduled)
5355 (setq rtn (org-agenda-get-scheduled deadline-results))
5356 (setq results (append results rtn)))
5357 ((eq arg :scheduled*)
5358 (setq rtn (org-agenda-get-scheduled deadline-results t))
5359 (setq results (append results rtn)))
5360 ((eq arg :closed)
5361 (setq rtn (org-agenda-get-progress))
5362 (setq results (append results rtn)))
5363 ((eq arg :deadline)
5364 (setq rtn (org-agenda-get-deadlines))
5365 (setq deadline-results (copy-sequence rtn))
5366 (setq results (append results rtn)))
5367 ((eq arg :deadline*)
5368 (setq rtn (org-agenda-get-deadlines t))
5369 (setq deadline-results (copy-sequence rtn))
5370 (setq results (append results rtn))))))))
5371 results))))
5373 (defsubst org-em (x y list)
5374 "Is X or Y a member of LIST?"
5375 (or (memq x list) (memq y list)))
5377 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5378 (defvar org-agenda-sorting-strategy-selected nil)
5380 (defun org-agenda-entry-get-agenda-timestamp (pom)
5381 "Retrieve timestamp information for sorting agenda views.
5382 Given a point or marker POM, returns a cons cell of the timestamp
5383 and the timestamp type relevant for the sorting strategy in
5384 `org-agenda-sorting-strategy-selected'."
5385 (let (ts ts-date-type)
5386 (save-match-data
5387 (cond ((org-em 'scheduled-up 'scheduled-down
5388 org-agenda-sorting-strategy-selected)
5389 (setq ts (org-entry-get pom "SCHEDULED")
5390 ts-date-type " scheduled"))
5391 ((org-em 'deadline-up 'deadline-down
5392 org-agenda-sorting-strategy-selected)
5393 (setq ts (org-entry-get pom "DEADLINE")
5394 ts-date-type " deadline"))
5395 ((org-em 'ts-up 'ts-down
5396 org-agenda-sorting-strategy-selected)
5397 (setq ts (org-entry-get pom "TIMESTAMP")
5398 ts-date-type " timestamp"))
5399 ((org-em 'tsia-up 'tsia-down
5400 org-agenda-sorting-strategy-selected)
5401 (setq ts (org-entry-get pom "TIMESTAMP_IA")
5402 ts-date-type " timestamp_ia"))
5403 ((org-em 'timestamp-up 'timestamp-down
5404 org-agenda-sorting-strategy-selected)
5405 (setq ts (or (org-entry-get pom "SCHEDULED")
5406 (org-entry-get pom "DEADLINE")
5407 (org-entry-get pom "TIMESTAMP")
5408 (org-entry-get pom "TIMESTAMP_IA"))
5409 ts-date-type ""))
5410 (t (setq ts-date-type "")))
5411 (cons (when ts (ignore-errors (org-time-string-to-absolute ts)))
5412 ts-date-type))))
5414 (defun org-agenda-get-todos ()
5415 "Return the TODO information for agenda display."
5416 (let* ((props (list 'face nil
5417 'done-face 'org-agenda-done
5418 'org-not-done-regexp org-not-done-regexp
5419 'org-todo-regexp org-todo-regexp
5420 'org-complex-heading-regexp org-complex-heading-regexp
5421 'mouse-face 'highlight
5422 'help-echo
5423 (format "mouse-2 or RET jump to org file %s"
5424 (abbreviate-file-name buffer-file-name))))
5425 (regexp (format org-heading-keyword-regexp-format
5426 (cond
5427 ((and org-select-this-todo-keyword
5428 (equal org-select-this-todo-keyword "*"))
5429 org-todo-regexp)
5430 (org-select-this-todo-keyword
5431 (concat "\\("
5432 (mapconcat 'identity
5433 (org-split-string
5434 org-select-this-todo-keyword
5435 "|")
5436 "\\|") "\\)"))
5437 (t org-not-done-regexp))))
5438 marker priority category level tags todo-state
5439 ts-date ts-date-type ts-date-pair
5440 ee txt beg end inherited-tags todo-state-end-pos)
5441 (goto-char (point-min))
5442 (while (re-search-forward regexp nil t)
5443 (catch :skip
5444 (save-match-data
5445 (beginning-of-line)
5446 (org-agenda-skip)
5447 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5448 (unless (and (setq todo-state (org-get-todo-state))
5449 (setq todo-state-end-pos (match-end 2)))
5450 (goto-char end)
5451 (throw :skip nil))
5452 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5453 (goto-char (1+ beg))
5454 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5455 (throw :skip nil)))
5456 (goto-char (match-beginning 2))
5457 (setq marker (org-agenda-new-marker (match-beginning 0))
5458 category (org-get-category)
5459 ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
5460 ts-date (car ts-date-pair)
5461 ts-date-type (cdr ts-date-pair)
5462 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5463 inherited-tags
5464 (or (eq org-agenda-show-inherited-tags 'always)
5465 (and (listp org-agenda-show-inherited-tags)
5466 (memq 'todo org-agenda-show-inherited-tags))
5467 (and (eq org-agenda-show-inherited-tags t)
5468 (or (eq org-agenda-use-tag-inheritance t)
5469 (memq 'todo org-agenda-use-tag-inheritance))))
5470 tags (org-get-tags-at nil (not inherited-tags))
5471 level (make-string (org-reduced-level (org-outline-level)) ? )
5472 txt (org-agenda-format-item "" txt level category tags t)
5473 priority (1+ (org-get-priority txt)))
5474 (org-add-props txt props
5475 'org-marker marker 'org-hd-marker marker
5476 'priority priority
5477 'level level
5478 'ts-date ts-date
5479 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5480 (push txt ee)
5481 (if org-agenda-todo-list-sublevels
5482 (goto-char todo-state-end-pos)
5483 (org-end-of-subtree 'invisible))))
5484 (nreverse ee)))
5486 (defun org-agenda-todo-custom-ignore-p (time n)
5487 "Check whether timestamp is farther away than n number of days.
5488 This function is invoked if `org-agenda-todo-ignore-deadlines',
5489 `org-agenda-todo-ignore-scheduled' or
5490 `org-agenda-todo-ignore-timestamp' is set to an integer."
5491 (let ((days (org-time-stamp-to-now
5492 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5493 (if (>= n 0)
5494 (>= days n)
5495 (<= days n))))
5497 ;;;###autoload
5498 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5499 (&optional end)
5500 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5501 (when (or org-agenda-todo-ignore-with-date
5502 org-agenda-todo-ignore-scheduled
5503 org-agenda-todo-ignore-deadlines
5504 org-agenda-todo-ignore-timestamp)
5505 (setq end (or end (save-excursion (outline-next-heading) (point))))
5506 (save-excursion
5507 (or (and org-agenda-todo-ignore-with-date
5508 (re-search-forward org-ts-regexp end t))
5509 (and org-agenda-todo-ignore-scheduled
5510 (re-search-forward org-scheduled-time-regexp end t)
5511 (cond
5512 ((eq org-agenda-todo-ignore-scheduled 'future)
5513 (> (org-time-stamp-to-now
5514 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5515 ((eq org-agenda-todo-ignore-scheduled 'past)
5516 (<= (org-time-stamp-to-now
5517 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5518 ((numberp org-agenda-todo-ignore-scheduled)
5519 (org-agenda-todo-custom-ignore-p
5520 (match-string 1) org-agenda-todo-ignore-scheduled))
5521 (t)))
5522 (and org-agenda-todo-ignore-deadlines
5523 (re-search-forward org-deadline-time-regexp end t)
5524 (cond
5525 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5526 ((eq org-agenda-todo-ignore-deadlines 'far)
5527 (not (org-deadline-close (match-string 1))))
5528 ((eq org-agenda-todo-ignore-deadlines 'future)
5529 (> (org-time-stamp-to-now
5530 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5531 ((eq org-agenda-todo-ignore-deadlines 'past)
5532 (<= (org-time-stamp-to-now
5533 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5534 ((numberp org-agenda-todo-ignore-deadlines)
5535 (org-agenda-todo-custom-ignore-p
5536 (match-string 1) org-agenda-todo-ignore-deadlines))
5537 (t (org-deadline-close (match-string 1)))))
5538 (and org-agenda-todo-ignore-timestamp
5539 (let ((buffer (current-buffer))
5540 (regexp
5541 (concat
5542 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5543 (start (point)))
5544 ;; Copy current buffer into a temporary one
5545 (with-temp-buffer
5546 (insert-buffer-substring buffer start end)
5547 (goto-char (point-min))
5548 ;; Delete SCHEDULED and DEADLINE items
5549 (while (re-search-forward regexp end t)
5550 (delete-region (match-beginning 0) (match-end 0)))
5551 (goto-char (point-min))
5552 ;; No search for timestamp left
5553 (when (re-search-forward org-ts-regexp nil t)
5554 (cond
5555 ((eq org-agenda-todo-ignore-timestamp 'future)
5556 (> (org-time-stamp-to-now
5557 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5558 ((eq org-agenda-todo-ignore-timestamp 'past)
5559 (<= (org-time-stamp-to-now
5560 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5561 ((numberp org-agenda-todo-ignore-timestamp)
5562 (org-agenda-todo-custom-ignore-p
5563 (match-string 1) org-agenda-todo-ignore-timestamp))
5564 (t))))))))))
5566 (defun org-agenda-get-timestamps (&optional deadline-results)
5567 "Return the date stamp information for agenda display."
5568 (let* ((props (list 'face 'org-agenda-calendar-event
5569 'org-not-done-regexp org-not-done-regexp
5570 'org-todo-regexp org-todo-regexp
5571 'org-complex-heading-regexp org-complex-heading-regexp
5572 'mouse-face 'highlight
5573 'help-echo
5574 (format "mouse-2 or RET jump to org file %s"
5575 (abbreviate-file-name buffer-file-name))))
5576 (d1 (calendar-absolute-from-gregorian date))
5578 (deadline-position-alist
5579 (mapcar (lambda (a) (and (setq mm (get-text-property
5580 0 'org-hd-marker a))
5581 (cons (marker-position mm) a)))
5582 deadline-results))
5583 (remove-re org-ts-regexp)
5584 (regexp
5585 (concat
5586 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5587 (regexp-quote
5588 (substring
5589 (format-time-string
5590 (car org-time-stamp-formats)
5591 (apply 'encode-time ; DATE bound by calendar
5592 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5593 1 11))
5594 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5595 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5596 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5597 donep tmp priority category level ee txt timestr tags
5598 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5599 inherited-tags ts-date)
5600 (goto-char (point-min))
5601 (while (setq end-of-match (re-search-forward regexp nil t))
5602 (setq b0 (match-beginning 0)
5603 b3 (match-beginning 3) e3 (match-end 3)
5604 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5605 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5606 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5607 (member todo-state
5608 org-agenda-repeating-timestamp-show-all)))
5609 (catch :skip
5610 (and (org-at-date-range-p) (throw :skip nil))
5611 (org-agenda-skip)
5612 (if (and (match-end 1)
5613 (not (= d1 (org-time-string-to-absolute
5614 (match-string 1) d1 nil show-all
5615 (current-buffer) b0))))
5616 (throw :skip nil))
5617 (if (and e3
5618 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5619 (throw :skip nil))
5620 (setq tmp (buffer-substring (max (point-min)
5621 (- b0 org-ds-keyword-length))
5623 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5624 inactivep (= (char-after b0) ?\[)
5625 deadlinep (string-match org-deadline-regexp tmp)
5626 scheduledp (string-match org-scheduled-regexp tmp)
5627 closedp (and org-agenda-include-inactive-timestamps
5628 (string-match org-closed-string tmp))
5629 clockp (and org-agenda-include-inactive-timestamps
5630 (or (string-match org-clock-string tmp)
5631 (string-match "]-+\\'" tmp)))
5632 warntime (get-text-property (point) 'org-appt-warntime)
5633 donep (member todo-state org-done-keywords))
5634 (if (or scheduledp deadlinep closedp clockp
5635 (and donep org-agenda-skip-timestamp-if-done))
5636 (throw :skip t))
5637 (if (string-match ">" timestr)
5638 ;; substring should only run to end of time stamp
5639 (setq timestr (substring timestr 0 (match-end 0))))
5640 (setq marker (org-agenda-new-marker b0)
5641 category (org-get-category b0))
5642 (save-excursion
5643 (if (not (re-search-backward org-outline-regexp-bol nil t))
5644 (throw :skip nil)
5645 (goto-char (match-beginning 0))
5646 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5647 (assoc (point) deadline-position-alist))
5648 (throw :skip nil))
5649 (setq hdmarker (org-agenda-new-marker)
5650 inherited-tags
5651 (or (eq org-agenda-show-inherited-tags 'always)
5652 (and (listp org-agenda-show-inherited-tags)
5653 (memq 'agenda org-agenda-show-inherited-tags))
5654 (and (eq org-agenda-show-inherited-tags t)
5655 (or (eq org-agenda-use-tag-inheritance t)
5656 (memq 'agenda org-agenda-use-tag-inheritance))))
5657 tags (org-get-tags-at nil (not inherited-tags))
5658 level (make-string (org-reduced-level (org-outline-level)) ? ))
5659 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5660 (setq head (or (match-string 1) ""))
5661 (setq txt (org-agenda-format-item
5662 (if inactivep org-agenda-inactive-leader nil)
5663 head level category tags timestr
5664 remove-re habitp)))
5665 (setq priority (org-get-priority txt))
5666 (org-add-props txt props 'priority priority
5667 'org-marker marker 'org-hd-marker hdmarker
5668 'date date
5669 'level level
5670 'ts-date
5671 (ignore-errors (org-time-string-to-absolute timestr))
5672 'todo-state todo-state
5673 'warntime warntime
5674 'type "timestamp")
5675 (push txt ee))
5676 (if org-agenda-skip-additional-timestamps-same-entry
5677 (outline-next-heading)
5678 (goto-char end-of-match))))
5679 (nreverse ee)))
5681 (defun org-agenda-get-sexps ()
5682 "Return the sexp information for agenda display."
5683 (require 'diary-lib)
5684 (let* ((props (list 'face 'org-agenda-calendar-sexp
5685 'mouse-face 'highlight
5686 'help-echo
5687 (format "mouse-2 or RET jump to org file %s"
5688 (abbreviate-file-name buffer-file-name))))
5689 (regexp "^&?%%(")
5690 marker category extra level ee txt tags entry
5691 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5692 (goto-char (point-min))
5693 (while (re-search-forward regexp nil t)
5694 (catch :skip
5695 (org-agenda-skip)
5696 (setq beg (match-beginning 0))
5697 (goto-char (1- (match-end 0)))
5698 (setq b (point))
5699 (forward-sexp 1)
5700 (setq sexp (buffer-substring b (point)))
5701 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5702 (org-trim (match-string 1))
5703 ""))
5704 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5705 (when result
5706 (setq marker (org-agenda-new-marker beg)
5707 level (make-string (org-reduced-level (org-outline-level)) ? )
5708 category (org-get-category beg)
5709 inherited-tags
5710 (or (eq org-agenda-show-inherited-tags 'always)
5711 (and (listp org-agenda-show-inherited-tags)
5712 (memq 'agenda org-agenda-show-inherited-tags))
5713 (and (eq org-agenda-show-inherited-tags t)
5714 (or (eq org-agenda-use-tag-inheritance t)
5715 (memq 'agenda org-agenda-use-tag-inheritance))))
5716 tags (org-get-tags-at nil (not inherited-tags))
5717 todo-state (org-get-todo-state)
5718 warntime (get-text-property (point) 'org-appt-warntime)
5719 extra nil)
5721 (dolist (r (if (stringp result)
5722 (list result)
5723 result)) ;; we expect a list here
5724 (when (and org-agenda-diary-sexp-prefix
5725 (string-match org-agenda-diary-sexp-prefix r))
5726 (setq extra (match-string 0 r)
5727 r (replace-match "" nil nil r)))
5728 (if (string-match "\\S-" r)
5729 (setq txt r)
5730 (setq txt "SEXP entry returned empty string"))
5731 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5732 (org-add-props txt props 'org-marker marker
5733 'date date 'todo-state todo-state
5734 'level level 'type "sexp" 'warntime warntime)
5735 (push txt ee)))))
5736 (nreverse ee)))
5738 ;; Calendar sanity: define some functions that are independent of
5739 ;; `calendar-date-style'.
5740 ;; Normally I would like to use ISO format when calling the diary functions,
5741 ;; but to make sure we still have Emacs 22 compatibility we bind
5742 ;; also `european-calendar-style' and use european format
5743 (defun org-anniversary (year month day &optional mark)
5744 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5745 (org-no-warnings
5746 (let ((calendar-date-style 'european) (european-calendar-style t))
5747 (diary-anniversary day month year mark))))
5748 (defun org-cyclic (N year month day &optional mark)
5749 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5750 (org-no-warnings
5751 (let ((calendar-date-style 'european) (european-calendar-style t))
5752 (diary-cyclic N day month year mark))))
5753 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5754 "Like `diary-block', but with fixed (ISO) order of arguments."
5755 (org-no-warnings
5756 (let ((calendar-date-style 'european) (european-calendar-style t))
5757 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5758 (defun org-date (year month day &optional mark)
5759 "Like `diary-date', but with fixed (ISO) order of arguments."
5760 (org-no-warnings
5761 (let ((calendar-date-style 'european) (european-calendar-style t))
5762 (diary-date day month year mark))))
5764 ;; Define the `org-class' function
5765 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5766 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5767 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5768 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5769 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5770 `holidays', then any date that is known by the Emacs calendar to be a
5771 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5772 then those holidays will be skipped."
5773 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5774 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5775 (d (calendar-absolute-from-gregorian date))
5776 (h (when skip-weeks (calendar-check-holidays date))))
5777 (and
5778 (<= date1 d)
5779 (<= d date2)
5780 (= (calendar-day-of-week date) dayname)
5781 (or (not skip-weeks)
5782 (progn
5783 (require 'cal-iso)
5784 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5785 (not (or (and h (memq 'holidays skip-weeks))
5786 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5787 entry)))
5789 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5790 "Like `org-class', but honor `calendar-date-style'.
5791 The order of the first 2 times 3 arguments depends on the variable
5792 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5793 So for American calendars, give this as MONTH DAY YEAR, for European as
5794 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5795 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5796 is any number of ISO weeks in the block period for which the item should
5797 be skipped.
5799 This function is here only for backward compatibility and it is deprecated,
5800 please use `org-class' instead."
5801 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5802 (date2 (org-order-calendar-date-args m2 d2 y2)))
5803 (org-class
5804 (nth 2 date1) (car date1) (nth 1 date1)
5805 (nth 2 date2) (car date2) (nth 1 date2)
5806 dayname skip-weeks)))
5807 (make-obsolete 'org-diary-class 'org-class "")
5809 (defalias 'org-get-closed 'org-agenda-get-progress)
5810 (defun org-agenda-get-progress ()
5811 "Return the logged TODO entries for agenda display."
5812 (let* ((props (list 'mouse-face 'highlight
5813 'org-not-done-regexp org-not-done-regexp
5814 'org-todo-regexp org-todo-regexp
5815 'org-complex-heading-regexp org-complex-heading-regexp
5816 'help-echo
5817 (format "mouse-2 or RET jump to org file %s"
5818 (abbreviate-file-name buffer-file-name))))
5819 (items (if (consp org-agenda-show-log-scoped)
5820 org-agenda-show-log-scoped
5821 (if (eq org-agenda-show-log-scoped 'clockcheck)
5822 '(clock)
5823 org-agenda-log-mode-items)))
5824 (parts
5825 (delq nil
5826 (list
5827 (if (memq 'closed items) (concat "\\<" org-closed-string))
5828 (if (memq 'clock items) (concat "\\<" org-clock-string))
5829 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5830 (parts-re (if parts (mapconcat 'identity parts "\\|")
5831 (error "`org-agenda-log-mode-items' is empty")))
5832 (regexp (concat
5833 "\\(" parts-re "\\)"
5834 " *\\["
5835 (regexp-quote
5836 (substring
5837 (format-time-string
5838 (car org-time-stamp-formats)
5839 (apply 'encode-time ; DATE bound by calendar
5840 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5841 1 11))))
5842 (org-agenda-search-headline-for-time nil)
5843 marker hdmarker priority category level tags closedp
5844 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5845 (goto-char (point-min))
5846 (while (re-search-forward regexp nil t)
5847 (catch :skip
5848 (org-agenda-skip)
5849 (setq marker (org-agenda-new-marker (match-beginning 0))
5850 closedp (equal (match-string 1) org-closed-string)
5851 statep (equal (string-to-char (match-string 1)) ?-)
5852 clockp (not (or closedp statep))
5853 state (and statep (match-string 2))
5854 category (org-get-category (match-beginning 0))
5855 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5856 (when (string-match "\\]" timestr)
5857 ;; substring should only run to end of time stamp
5858 (setq rest (substring timestr (match-end 0))
5859 timestr (substring timestr 0 (match-end 0)))
5860 (if (and (not closedp) (not statep)
5861 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5862 rest))
5863 (progn (setq timestr (concat (substring timestr 0 -1)
5864 "-" (match-string 1 rest) "]"))
5865 (setq clocked (match-string 2 rest)))
5866 (setq clocked "-")))
5867 (save-excursion
5868 (setq extra
5869 (cond
5870 ((not org-agenda-log-mode-add-notes) nil)
5871 (statep
5872 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5873 (match-string 1)))
5874 (clockp
5875 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5876 (match-string 1)))))
5877 (if (not (re-search-backward org-outline-regexp-bol nil t))
5878 (throw :skip nil)
5879 (goto-char (match-beginning 0))
5880 (setq hdmarker (org-agenda-new-marker)
5881 inherited-tags
5882 (or (eq org-agenda-show-inherited-tags 'always)
5883 (and (listp org-agenda-show-inherited-tags)
5884 (memq 'todo org-agenda-show-inherited-tags))
5885 (and (eq org-agenda-show-inherited-tags t)
5886 (or (eq org-agenda-use-tag-inheritance t)
5887 (memq 'todo org-agenda-use-tag-inheritance))))
5888 tags (org-get-tags-at nil (not inherited-tags))
5889 level (make-string (org-reduced-level (org-outline-level)) ? ))
5890 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5891 (setq txt (match-string 1))
5892 (when extra
5893 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5894 (setq txt (concat (substring txt 0 (match-beginning 1))
5895 " - " extra " " (match-string 2 txt)))
5896 (setq txt (concat txt " - " extra))))
5897 (setq txt (org-agenda-format-item
5898 (cond
5899 (closedp "Closed: ")
5900 (statep (concat "State: (" state ")"))
5901 (t (concat "Clocked: (" clocked ")")))
5902 txt level category tags timestr)))
5903 (setq priority 100000)
5904 (org-add-props txt props
5905 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5906 'priority priority 'level level
5907 'type "closed" 'date date
5908 'undone-face 'org-warning 'done-face 'org-agenda-done)
5909 (push txt ee))
5910 (goto-char (point-at-eol))))
5911 (nreverse ee)))
5913 (defun org-agenda-show-clocking-issues ()
5914 "Add overlays, showing issues with clocking.
5915 See also the user option `org-agenda-clock-consistency-checks'."
5916 (interactive)
5917 (let* ((org-time-clocksum-use-effort-durations nil)
5918 (pl org-agenda-clock-consistency-checks)
5919 (re (concat "^[ \t]*"
5920 org-clock-string
5921 "[ \t]+"
5922 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5923 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5924 (tlstart 0.)
5925 (tlend 0.)
5926 (maxtime (org-hh:mm-string-to-minutes
5927 (or (plist-get pl :max-duration) "24:00")))
5928 (mintime (org-hh:mm-string-to-minutes
5929 (or (plist-get pl :min-duration) 0)))
5930 (maxgap (org-hh:mm-string-to-minutes
5931 ;; default 30:00 means never complain
5932 (or (plist-get pl :max-gap) "30:00")))
5933 (gapok (mapcar 'org-hh:mm-string-to-minutes
5934 (plist-get pl :gap-ok-around)))
5935 (def-face (or (plist-get pl :default-face)
5936 '((:background "DarkRed") (:foreground "white"))))
5937 issue face m te ts dt ov)
5938 (goto-char (point-min))
5939 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5940 (setq issue nil face def-face)
5941 (catch 'next
5942 (setq m (org-get-at-bol 'org-marker)
5943 te nil ts nil)
5944 (unless (and m (markerp m))
5945 (setq issue "No valid clock line") (throw 'next t))
5946 (org-with-point-at m
5947 (save-excursion
5948 (goto-char (point-at-bol))
5949 (unless (looking-at re)
5950 (error "No valid Clock line")
5951 (throw 'next t))
5952 (unless (match-end 3)
5953 (setq issue "No end time"
5954 face (or (plist-get pl :no-end-time-face) face))
5955 (throw 'next t))
5956 (setq ts (match-string 1)
5957 te (match-string 3)
5958 ts (org-float-time
5959 (apply 'encode-time (org-parse-time-string ts)))
5960 te (org-float-time
5961 (apply 'encode-time (org-parse-time-string te)))
5962 dt (- te ts))))
5963 (cond
5964 ((> dt (* 60 maxtime))
5965 ;; a very long clocking chunk
5966 (setq issue (format "Clocking interval is very long: %s"
5967 (org-minutes-to-clocksum-string
5968 (floor (/ (float dt) 60.))))
5969 face (or (plist-get pl :long-face) face)))
5970 ((< dt (* 60 mintime))
5971 ;; a very short clocking chunk
5972 (setq issue (format "Clocking interval is very short: %s"
5973 (org-minutes-to-clocksum-string
5974 (floor (/ (float dt) 60.))))
5975 face (or (plist-get pl :short-face) face)))
5976 ((and (> tlend 0) (< ts tlend))
5977 ;; Two clock entries are overlapping
5978 (setq issue (format "Clocking overlap: %d minutes"
5979 (/ (- tlend ts) 60))
5980 face (or (plist-get pl :overlap-face) face)))
5981 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5982 ;; There is a gap, lets see if we need to report it
5983 (unless (org-agenda-check-clock-gap tlend ts gapok)
5984 (setq issue (format "Clocking gap: %d minutes"
5985 (/ (- ts tlend) 60))
5986 face (or (plist-get pl :gap-face) face))))
5987 (t nil)))
5988 (setq tlend (or te tlend) tlstart (or ts tlstart))
5989 (when issue
5990 ;; OK, there was some issue, add an overlay to show the issue
5991 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5992 (overlay-put ov 'before-string
5993 (concat
5994 (org-add-props
5995 (format "%-43s" (concat " " issue))
5997 'face face)
5998 "\n"))
5999 (overlay-put ov 'evaporate t)))))
6001 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
6002 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
6003 (catch 'exit
6004 (unless ok-list
6005 ;; there are no OK times for gaps...
6006 (throw 'exit nil))
6007 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
6008 ;; This is more than 24 hours, so it is OK.
6009 ;; because we have at least one OK time, that must be in the
6010 ;; 24 hour interval.
6011 (throw 'exit t))
6012 ;; We have a shorter gap.
6013 ;; Now we have to get the minute of the day when these times are
6014 (let* ((t1dec (decode-time (seconds-to-time t1)))
6015 (t2dec (decode-time (seconds-to-time t2)))
6016 ;; compute the minute on the day
6017 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
6018 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
6019 (when (< min2 min1)
6020 ;; if min2 is smaller than min1, this means it is on the next day.
6021 ;; Wrap it to after midnight.
6022 (setq min2 (+ min2 1440)))
6023 ;; Now check if any of the OK times is in the gap
6024 (mapc (lambda (x)
6025 ;; Wrap the time to after midnight if necessary
6026 (if (< x min1) (setq x (+ x 1440)))
6027 ;; Check if in interval
6028 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
6029 ok-list)
6030 ;; Nope, this gap is not OK
6031 nil)))
6033 (defun org-agenda-get-deadlines (&optional with-hour)
6034 "Return the deadline information for agenda display.
6035 When WITH-HOUR is non-nil, only return deadlines with an hour
6036 specification like [h]h:mm."
6037 (let* ((props (list 'mouse-face 'highlight
6038 'org-not-done-regexp org-not-done-regexp
6039 'org-todo-regexp org-todo-regexp
6040 'org-complex-heading-regexp org-complex-heading-regexp
6041 'help-echo
6042 (format "mouse-2 or RET jump to org file %s"
6043 (abbreviate-file-name buffer-file-name))))
6044 (regexp (if with-hour
6045 org-deadline-time-hour-regexp
6046 org-deadline-time-regexp))
6047 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6048 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6049 (dl0 (car org-agenda-deadline-leaders))
6050 (dl1 (nth 1 org-agenda-deadline-leaders))
6051 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6052 d2 diff dfrac wdays pos pos1 category level
6053 tags suppress-prewarning ee txt head face s todo-state
6054 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6055 (goto-char (point-min))
6056 (while (re-search-forward regexp nil t)
6057 (catch :skip
6058 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6059 (org-agenda-skip)
6060 (setq s (match-string 1)
6061 txt nil
6062 pos (1- (match-beginning 1))
6063 todo-state (save-match-data (org-get-todo-state))
6064 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6065 (member todo-state
6066 org-agenda-repeating-timestamp-show-all))
6067 d2 (org-time-string-to-absolute
6068 s d1 'past show-all (current-buffer) pos)
6069 diff (- d2 d1))
6070 (setq suppress-prewarning
6071 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6072 (let ((item (buffer-substring (point-at-bol)
6073 (point-at-eol))))
6074 (save-match-data
6075 (and (string-match
6076 org-scheduled-time-regexp item)
6077 (match-string 1 item)))))))
6078 (cond
6079 ((not ds) nil)
6080 ;; The current item has a scheduled date (in ds), so
6081 ;; evaluate its prewarning lead time.
6082 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6083 ;; Use global prewarning-restart lead time.
6084 org-agenda-skip-deadline-prewarning-if-scheduled)
6085 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6086 'pre-scheduled)
6087 ;; Set prewarning to no earlier than scheduled.
6088 (min (- d2 (org-time-string-to-absolute
6089 ds d1 'past show-all (current-buffer) pos))
6090 org-deadline-warning-days))
6091 ;; Set prewarning to deadline.
6092 (t 0))))
6093 (setq wdays (if suppress-prewarning
6094 (let ((org-deadline-warning-days suppress-prewarning))
6095 (org-get-wdays s))
6096 (org-get-wdays s))
6097 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6098 upcomingp (and todayp (> diff 0)))
6099 ;; When to show a deadline in the calendar:
6100 ;; If the expiration is within wdays warning time.
6101 ;; Past-due deadlines are only shown on the current date
6102 (if (and (or (and (<= diff wdays)
6103 (and todayp (not org-agenda-only-exact-dates)))
6104 (= diff 0)))
6105 (save-excursion
6106 ;; (setq todo-state (org-get-todo-state))
6107 (setq donep (member todo-state org-done-keywords))
6108 (if (and donep
6109 (or org-agenda-skip-deadline-if-done
6110 (not (= diff 0))))
6111 (setq txt nil)
6112 (setq category (org-get-category)
6113 warntime (get-text-property (point) 'org-appt-warntime))
6114 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6115 (throw :skip nil)
6116 (goto-char (match-end 0))
6117 (setq pos1 (match-beginning 0))
6118 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6119 (setq inherited-tags
6120 (or (eq org-agenda-show-inherited-tags 'always)
6121 (and (listp org-agenda-show-inherited-tags)
6122 (memq 'agenda org-agenda-show-inherited-tags))
6123 (and (eq org-agenda-show-inherited-tags t)
6124 (or (eq org-agenda-use-tag-inheritance t)
6125 (memq 'agenda org-agenda-use-tag-inheritance))))
6126 tags (org-get-tags-at pos1 (not inherited-tags)))
6127 (setq head (buffer-substring
6128 (point)
6129 (progn (skip-chars-forward "^\r\n")
6130 (point))))
6131 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6132 (setq timestr
6133 (concat (substring s (match-beginning 1)) " "))
6134 (setq timestr 'time))
6135 (setq txt
6136 (org-agenda-format-item
6137 ;; For past deadlines, make sure to report
6138 ;; time difference since date S, not since
6139 ;; closest repeater.
6140 (let ((diff (if (< (org-today) d1) diff
6141 (- (org-time-string-to-absolute s) d1))))
6142 (cond ((= diff 0) dl0)
6143 ((> diff 0)
6144 (if (functionp dl1)
6145 (funcall dl1 diff date)
6146 (format dl1 diff)))
6148 (if (functionp dl2)
6149 (funcall dl2 diff date)
6150 (format dl2 (if (string= dl2 dl1)
6151 diff (abs diff)))))))
6152 head level category tags
6153 (and (= diff 0) timestr)))))
6154 (when txt
6155 (setq face (org-agenda-deadline-face dfrac))
6156 (org-add-props txt props
6157 'org-marker (org-agenda-new-marker pos)
6158 'warntime warntime
6159 'level level
6160 'ts-date d2
6161 'org-hd-marker (org-agenda-new-marker pos1)
6162 'priority (+ (- diff)
6163 (org-get-priority txt))
6164 'todo-state todo-state
6165 'type (if upcomingp "upcoming-deadline" "deadline")
6166 'date (if upcomingp date d2)
6167 'face (if donep 'org-agenda-done face)
6168 'undone-face face 'done-face 'org-agenda-done)
6169 (push txt ee))))))
6170 (nreverse ee)))
6172 (defun org-agenda-deadline-face (fraction)
6173 "Return the face to displaying a deadline item.
6174 FRACTION is what fraction of the head-warning time has passed."
6175 (let ((faces org-agenda-deadline-faces) f)
6176 (catch 'exit
6177 (while (setq f (pop faces))
6178 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6180 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6181 "Return the scheduled information for agenda display.
6182 When WITH-HOUR is non-nil, only return scheduled items with
6183 an hour specification like [h]h:mm."
6184 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6185 'org-todo-regexp org-todo-regexp
6186 'org-complex-heading-regexp org-complex-heading-regexp
6187 'done-face 'org-agenda-done
6188 'mouse-face 'highlight
6189 'help-echo
6190 (format "mouse-2 or RET jump to org file %s"
6191 (abbreviate-file-name buffer-file-name))))
6192 (regexp (if with-hour
6193 org-scheduled-time-hour-regexp
6194 org-scheduled-time-regexp))
6195 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6196 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6198 (deadline-position-alist
6199 (mapcar (lambda (a) (and (setq mm (get-text-property
6200 0 'org-hd-marker a))
6201 (cons (marker-position mm) a)))
6202 deadline-results))
6203 d2 diff pos pos1 category level tags donep
6204 ee txt head pastschedp todo-state face timestr s habitp show-all
6205 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6206 ddays)
6207 (goto-char (point-min))
6208 (while (re-search-forward regexp nil t)
6209 (catch :skip
6210 (unless (save-match-data (org-at-planning-p)) (throw :skip nil))
6211 (org-agenda-skip)
6212 (setq s (match-string 1)
6213 txt nil
6214 pos (1- (match-beginning 1))
6215 todo-state (save-match-data (org-get-todo-state))
6216 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6217 (member todo-state
6218 org-agenda-repeating-timestamp-show-all))
6219 d2 (org-time-string-to-absolute
6220 s d1 'past show-all (current-buffer) pos)
6221 diff (- d2 d1)
6222 warntime (get-text-property (point) 'org-appt-warntime))
6223 (setq pastschedp (and todayp (< diff 0)))
6224 (setq did-habit-check-p nil)
6225 (setq suppress-delay
6226 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6227 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6228 (save-match-data
6229 (and (string-match
6230 org-deadline-time-regexp item)
6231 (match-string 1 item)))))))
6232 (cond
6233 ((not ds) nil)
6234 ;; The current item has a deadline date (in ds), so
6235 ;; evaluate its delay time.
6236 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6237 ;; Use global delay time.
6238 (- org-agenda-skip-scheduled-delay-if-deadline))
6239 ((eq org-agenda-skip-scheduled-delay-if-deadline
6240 'post-deadline)
6241 ;; Set delay to no later than deadline.
6242 (min (- d2 (org-time-string-to-absolute
6243 ds d1 'past show-all (current-buffer) pos))
6244 org-scheduled-delay-days))
6245 (t 0))))
6246 (setq ddays (if suppress-delay
6247 (let ((org-scheduled-delay-days suppress-delay))
6248 (org-get-wdays s t t))
6249 (org-get-wdays s t)))
6250 ;; Use a delay of 0 when there is a repeater and the delay is
6251 ;; of the form --3d
6252 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6253 (< (org-time-string-to-absolute s)
6254 (org-time-string-to-absolute
6255 s d2 'past nil (current-buffer) pos)))
6256 (setq ddays 0))
6257 ;; When to show a scheduled item in the calendar:
6258 ;; If it is on or past the date.
6259 (when (or (and (> ddays 0) (= diff (- ddays)))
6260 (and (zerop ddays) (= diff 0))
6261 (and (< (+ diff ddays) 0)
6262 (< (abs diff) org-scheduled-past-days)
6263 (and todayp (not org-agenda-only-exact-dates)))
6264 ;; org-is-habit-p uses org-entry-get, which is expansive
6265 ;; so we go extra mile to only call it once
6266 (and todayp
6267 (boundp 'org-habit-show-all-today)
6268 org-habit-show-all-today
6269 (setq did-habit-check-p t)
6270 (setq habitp (and (functionp 'org-is-habit-p)
6271 (org-is-habit-p)))))
6272 (save-excursion
6273 (setq donep (member todo-state org-done-keywords))
6274 (if (and donep
6275 (or org-agenda-skip-scheduled-if-done
6276 (not (= diff 0))
6277 (and (functionp 'org-is-habit-p)
6278 (org-is-habit-p))))
6279 (setq txt nil)
6280 (setq habitp (if did-habit-check-p habitp
6281 (and (functionp 'org-is-habit-p)
6282 (org-is-habit-p))))
6283 (setq category (org-get-category))
6284 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6285 'repeated-after-deadline)
6286 (org-get-deadline-time (point))
6287 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6288 (throw :skip nil))
6289 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6290 (throw :skip nil)
6291 (goto-char (match-end 0))
6292 (setq pos1 (match-beginning 0))
6293 (if habitp
6294 (if (or (not org-habit-show-habits)
6295 (and (not todayp)
6296 (boundp 'org-habit-show-habits-only-for-today)
6297 org-habit-show-habits-only-for-today))
6298 (throw :skip nil))
6299 (if (and
6300 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6301 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6302 pastschedp))
6303 (setq mm (assoc pos1 deadline-position-alist)))
6304 (throw :skip nil)))
6305 (setq inherited-tags
6306 (or (eq org-agenda-show-inherited-tags 'always)
6307 (and (listp org-agenda-show-inherited-tags)
6308 (memq 'agenda org-agenda-show-inherited-tags))
6309 (and (eq org-agenda-show-inherited-tags t)
6310 (or (eq org-agenda-use-tag-inheritance t)
6311 (memq 'agenda org-agenda-use-tag-inheritance))))
6313 tags (org-get-tags-at nil (not inherited-tags)))
6314 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6315 (setq head (buffer-substring
6316 (point)
6317 (progn (skip-chars-forward "^\r\n") (point))))
6318 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6319 (setq timestr
6320 (concat (substring s (match-beginning 1)) " "))
6321 (setq timestr 'time))
6322 (setq txt (org-agenda-format-item
6323 ;; For past scheduled dates, make sure to
6324 ;; report time difference since date S, not
6325 ;; since closest repeater.
6326 (let ((diff
6327 (if (< (org-today) d1) diff
6328 (- (org-time-string-to-absolute s) d1))))
6329 (if (= diff 0) (car org-agenda-scheduled-leaders)
6330 (format (nth 1 org-agenda-scheduled-leaders)
6331 (- 1 diff))))
6332 head level category tags
6333 (and (= diff 0) timestr)
6334 nil habitp))))
6335 (when txt
6336 (setq face
6337 (cond
6338 ((and (not habitp) pastschedp)
6339 'org-scheduled-previously)
6340 (todayp 'org-scheduled-today)
6341 (t 'org-scheduled))
6342 habitp (and habitp (org-habit-parse-todo)))
6343 (org-add-props txt props
6344 'undone-face face
6345 'face (if donep 'org-agenda-done face)
6346 'org-marker (org-agenda-new-marker pos)
6347 'org-hd-marker (org-agenda-new-marker pos1)
6348 'type (if pastschedp "past-scheduled" "scheduled")
6349 'date (if pastschedp d2 date)
6350 'ts-date d2
6351 'warntime warntime
6352 'level level
6353 'priority (if habitp
6354 (org-habit-get-priority habitp)
6355 (+ 94 (- 5 diff) (org-get-priority txt)))
6356 'org-habit-p habitp
6357 'todo-state todo-state)
6358 (push txt ee))))))
6359 (nreverse ee)))
6361 (defun org-agenda-get-blocks ()
6362 "Return the date-range information for agenda display."
6363 (let* ((props (list 'face nil
6364 'org-not-done-regexp org-not-done-regexp
6365 'org-todo-regexp org-todo-regexp
6366 'org-complex-heading-regexp org-complex-heading-regexp
6367 'mouse-face 'highlight
6368 'help-echo
6369 (format "mouse-2 or RET jump to org file %s"
6370 (abbreviate-file-name buffer-file-name))))
6371 (regexp org-tr-regexp)
6372 (d0 (calendar-absolute-from-gregorian date))
6373 marker hdmarker ee txt d1 d2 s1 s2 category
6374 level todo-state tags pos head donep inherited-tags)
6375 (goto-char (point-min))
6376 (while (re-search-forward regexp nil t)
6377 (catch :skip
6378 (org-agenda-skip)
6379 (setq pos (point))
6380 (let ((start-time (match-string 1))
6381 (end-time (match-string 2)))
6382 (setq s1 (match-string 1)
6383 s2 (match-string 2)
6384 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6385 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6386 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6387 ;; Only allow days between the limits, because the normal
6388 ;; date stamps will catch the limits.
6389 (save-excursion
6390 (setq todo-state (org-get-todo-state))
6391 (setq donep (member todo-state org-done-keywords))
6392 (if (and donep org-agenda-skip-timestamp-if-done)
6393 (throw :skip t))
6394 (setq marker (org-agenda-new-marker (point))
6395 category (org-get-category))
6396 (if (not (re-search-backward org-outline-regexp-bol nil t))
6397 (throw :skip nil)
6398 (goto-char (match-beginning 0))
6399 (setq hdmarker (org-agenda-new-marker (point))
6400 inherited-tags
6401 (or (eq org-agenda-show-inherited-tags 'always)
6402 (and (listp org-agenda-show-inherited-tags)
6403 (memq 'agenda org-agenda-show-inherited-tags))
6404 (and (eq org-agenda-show-inherited-tags t)
6405 (or (eq org-agenda-use-tag-inheritance t)
6406 (memq 'agenda org-agenda-use-tag-inheritance))))
6408 tags (org-get-tags-at nil (not inherited-tags)))
6409 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6410 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6411 (setq head (match-string 1))
6412 (let ((remove-re
6413 (if org-agenda-remove-timeranges-from-blocks
6414 (concat
6415 "<" (regexp-quote s1) ".*?>"
6416 "--"
6417 "<" (regexp-quote s2) ".*?>")
6418 nil)))
6419 (setq txt (org-agenda-format-item
6420 (format
6421 (nth (if (= d1 d2) 0 1)
6422 org-agenda-timerange-leaders)
6423 (1+ (- d0 d1)) (1+ (- d2 d1)))
6424 head level category tags
6425 (cond ((and (= d1 d0) (= d2 d0))
6426 (concat "<" start-time ">--<" end-time ">"))
6427 ((= d1 d0)
6428 (concat "<" start-time ">"))
6429 ((= d2 d0)
6430 (concat "<" end-time ">")))
6431 remove-re))))
6432 (org-add-props txt props
6433 'org-marker marker 'org-hd-marker hdmarker
6434 'type "block" 'date date
6435 'level level
6436 'todo-state todo-state
6437 'priority (org-get-priority txt))
6438 (push txt ee))))
6439 (goto-char pos)))
6440 ;; Sort the entries by expiration date.
6441 (nreverse ee)))
6443 ;;; Agenda presentation and sorting
6445 (defvar org-prefix-has-time nil
6446 "A flag, set by `org-compile-prefix-format'.
6447 The flag is set if the currently compiled format contains a `%t'.")
6448 (defvar org-prefix-has-tag nil
6449 "A flag, set by `org-compile-prefix-format'.
6450 The flag is set if the currently compiled format contains a `%T'.")
6451 (defvar org-prefix-has-effort nil
6452 "A flag, set by `org-compile-prefix-format'.
6453 The flag is set if the currently compiled format contains a `%e'.")
6454 (defvar org-prefix-has-breadcrumbs nil
6455 "A flag, set by `org-compile-prefix-format'.
6456 The flag is set if the currently compiled format contains a `%b'.")
6457 (defvar org-prefix-category-length nil
6458 "Used by `org-compile-prefix-format' to remember the category field width.")
6459 (defvar org-prefix-category-max-length nil
6460 "Used by `org-compile-prefix-format' to remember the category field width.")
6462 (defun org-agenda-get-category-icon (category)
6463 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6464 (dolist (entry org-agenda-category-icon-alist)
6465 (when (org-string-match-p (car entry) category)
6466 (if (listp (cadr entry))
6467 (return (cadr entry))
6468 (return (apply 'create-image (cdr entry)))))))
6470 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6471 remove-re habitp)
6472 "Format TXT to be inserted into the agenda buffer.
6473 In particular, add the prefix and corresponding text properties.
6475 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6476 LEVEL may be a string to replace the `%l' specifier.
6477 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6478 category taken from local variable or file name. It will replace the `%c'
6479 specifier in the format.
6480 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6481 TXT for sorting of this entry, and for the `%t' specifier in the format.
6482 When DOTIME is a string, this string is searched for a time before TXT is.
6483 TAGS can be the tags of the headline.
6484 Any match of REMOVE-RE will be removed from TXT."
6485 ;; We keep the org-prefix-* variable values along with a compiled
6486 ;; formatter, so that multiple agendas existing at the same time do
6487 ;; not step on each other toes.
6489 ;; It was inconvenient to make these variables buffer local in
6490 ;; Agenda buffers, because this function expects to be called with
6491 ;; the buffer where item comes from being current, and not agenda
6492 ;; buffer
6493 (let* ((bindings (car org-prefix-format-compiled))
6494 (formatter (cadr org-prefix-format-compiled)))
6495 (loop for (var value) in bindings
6496 do (set var value))
6497 (save-match-data
6498 ;; Diary entries sometimes have extra whitespace at the beginning
6499 (setq txt (org-trim txt))
6501 ;; Fix the tags part in txt
6502 (setq txt (org-agenda-fix-displayed-tags
6503 txt tags
6504 org-agenda-show-inherited-tags
6505 org-agenda-hide-tags-regexp))
6507 (let* ((category (or category
6508 (if buffer-file-name
6509 (file-name-sans-extension
6510 (file-name-nondirectory buffer-file-name))
6511 "")))
6512 (category-icon (org-agenda-get-category-icon category))
6513 (category-icon (if category-icon
6514 (propertize " " 'display category-icon)
6515 ""))
6516 (effort (and (not (string= txt ""))
6517 (get-text-property 1 'effort txt)))
6518 ;; time, tag, effort are needed for the eval of the prefix format
6519 (tag (if tags (nth (1- (length tags)) tags) ""))
6520 time
6521 (ts (if dotime (concat
6522 (if (stringp dotime) dotime "")
6523 (and org-agenda-search-headline-for-time txt))))
6524 (time-of-day (and dotime (org-get-time-of-day ts)))
6525 stamp plain s0 s1 s2 rtn srp l
6526 duration breadcrumbs)
6527 (and (derived-mode-p 'org-mode) buffer-file-name
6528 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6529 (when (and dotime time-of-day)
6530 ;; Extract starting and ending time and move them to prefix
6531 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6532 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6533 (setq s0 (match-string 0 ts)
6534 srp (and stamp (match-end 3))
6535 s1 (match-string (if plain 1 2) ts)
6536 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6538 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6539 ;; them, we might want to remove them there to avoid duplication.
6540 ;; The user can turn this off with a variable.
6541 (if (and org-prefix-has-time
6542 org-agenda-remove-times-when-in-prefix (or stamp plain)
6543 (string-match (concat (regexp-quote s0) " *") txt)
6544 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6545 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6546 (= (match-beginning 0) 0)
6548 (setq txt (replace-match "" nil nil txt))))
6549 ;; Normalize the time(s) to 24 hour
6550 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6551 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6553 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6554 (let (org-time-clocksum-use-effort-durations)
6555 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6556 (setq s2
6557 (org-minutes-to-clocksum-string
6558 (+ (org-hh:mm-string-to-minutes s1)
6559 org-agenda-default-appointment-duration)))))
6561 ;; Compute the duration
6562 (when s2
6563 (setq duration (- (org-hh:mm-string-to-minutes s2)
6564 (org-hh:mm-string-to-minutes s1)))))
6566 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6567 txt)
6568 ;; Tags are in the string
6569 (if (or (eq org-agenda-remove-tags t)
6570 (and org-agenda-remove-tags
6571 org-prefix-has-tag))
6572 (setq txt (replace-match "" t t txt))
6573 (setq txt (replace-match
6574 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6575 (match-string 2 txt))
6576 t t txt))))
6578 (when remove-re
6579 (while (string-match remove-re txt)
6580 (setq txt (replace-match "" t t txt))))
6582 ;; Set org-heading property on `txt' to mark the start of the
6583 ;; heading.
6584 (add-text-properties 0 (length txt) '(org-heading t) txt)
6586 ;; Prepare the variables needed in the eval of the compiled format
6587 (if org-prefix-has-breadcrumbs
6588 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6589 (let ((s (org-display-outline-path nil nil "->" t)))
6590 (if (eq "" s) "" (concat s "->"))))))
6591 (setq time (cond (s2 (concat
6592 (org-agenda-time-of-day-to-ampm-maybe s1)
6593 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6594 (if org-agenda-timegrid-use-ampm " ")))
6595 (s1 (concat
6596 (org-agenda-time-of-day-to-ampm-maybe s1)
6597 (if org-agenda-timegrid-use-ampm
6598 "........ "
6599 "......")))
6600 (t ""))
6601 extra (or (and (not habitp) extra) "")
6602 category (if (symbolp category) (symbol-name category) category)
6603 level (or level ""))
6604 (if (string-match org-bracket-link-regexp category)
6605 (progn
6606 (setq l (if (match-end 3)
6607 (- (match-end 3) (match-beginning 3))
6608 (- (match-end 1) (match-beginning 1))))
6609 (when (< l (or org-prefix-category-length 0))
6610 (setq category (copy-sequence category))
6611 (org-add-props category nil
6612 'extra-space (make-string
6613 (- org-prefix-category-length l 1) ?\ ))))
6614 (if (and org-prefix-category-max-length
6615 (>= (length category) org-prefix-category-max-length))
6616 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6617 ;; Evaluate the compiled format
6618 (setq rtn (concat (eval formatter) txt))
6620 ;; And finally add the text properties
6621 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6622 (org-add-props rtn nil
6623 'org-category category
6624 'tags (mapcar 'org-downcase-keep-props tags)
6625 'org-highest-priority org-highest-priority
6626 'org-lowest-priority org-lowest-priority
6627 'time-of-day time-of-day
6628 'duration duration
6629 'breadcrumbs breadcrumbs
6630 'txt txt
6631 'level level
6632 'time time
6633 'extra extra
6634 'format org-prefix-format-compiled
6635 'dotime dotime)))))
6637 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6638 "Remove tags string from TXT, and add a modified list of tags.
6639 The modified list may contain inherited tags, and tags matched by
6640 `org-agenda-hide-tags-regexp' will be removed."
6641 (when (or add-inherited hide-re)
6642 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6643 (setq txt (substring txt 0 (match-beginning 0))))
6644 (setq tags
6645 (delq nil
6646 (mapcar (lambda (tg)
6647 (if (or (and hide-re (string-match hide-re tg))
6648 (and (not add-inherited)
6649 (get-text-property 0 'inherited tg)))
6651 tg))
6652 tags)))
6653 (when tags
6654 (let ((have-i (get-text-property 0 'inherited (car tags)))
6656 (setq txt (concat txt " :"
6657 (mapconcat
6658 (lambda (x)
6659 (setq i (get-text-property 0 'inherited x))
6660 (if (and have-i (not i))
6661 (progn
6662 (setq have-i nil)
6663 (concat ":" x))
6665 tags ":")
6666 (if have-i "::" ":"))))))
6667 txt)
6669 (defun org-downcase-keep-props (s)
6670 (let ((props (text-properties-at 0 s)))
6671 (setq s (downcase s))
6672 (add-text-properties 0 (length s) props s)
6675 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6677 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6678 "Add a time-grid for agenda items which need it.
6680 LIST is the list of agenda items formatted by `org-agenda-list'.
6681 NDAYS is the span of the current agenda view.
6682 TODAYP is t when the current agenda view is on today."
6683 (catch 'exit
6684 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6685 ((and todayp (member 'today (car org-agenda-time-grid))))
6686 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6687 ((member 'weekly (car org-agenda-time-grid)))
6688 (t (throw 'exit list)))
6689 (let* ((have (delq nil (mapcar
6690 (lambda (x) (get-text-property 1 'time-of-day x))
6691 list)))
6692 (string (nth 1 org-agenda-time-grid))
6693 (gridtimes (nth 2 org-agenda-time-grid))
6694 (req (car org-agenda-time-grid))
6695 (remove (member 'remove-match req))
6696 new time)
6697 (if (and (member 'require-timed req) (not have))
6698 ;; don't show empty grid
6699 (throw 'exit list))
6700 (while (setq time (pop gridtimes))
6701 (unless (and remove (member time have))
6702 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6703 (push (org-agenda-format-item
6704 nil string nil "" nil
6705 (concat (substring time 0 -2) ":" (substring time -2)))
6706 new)
6707 (put-text-property
6708 2 (length (car new)) 'face 'org-time-grid (car new))))
6709 (when (and todayp org-agenda-show-current-time-in-grid)
6710 (push (org-agenda-format-item
6711 nil org-agenda-current-time-string nil "" nil
6712 (format-time-string "%H:%M "))
6713 new)
6714 (put-text-property
6715 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6717 (if (member 'time-up org-agenda-sorting-strategy-selected)
6718 (append new list)
6719 (append list new)))))
6721 (defun org-compile-prefix-format (key)
6722 "Compile the prefix format into a Lisp form that can be evaluated.
6723 The resulting form and associated variable bindings is returned
6724 and stored in the variable `org-prefix-format-compiled'."
6725 (setq org-prefix-has-time nil
6726 org-prefix-has-tag nil
6727 org-prefix-category-length nil
6728 org-prefix-has-effort nil
6729 org-prefix-has-breadcrumbs nil)
6730 (let ((s (cond
6731 ((stringp org-agenda-prefix-format)
6732 org-agenda-prefix-format)
6733 ((assq key org-agenda-prefix-format)
6734 (cdr (assq key org-agenda-prefix-format)))
6735 (t " %-12:c%?-12t% s")))
6736 (start 0)
6737 varform vars var e c f opt)
6738 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6739 s start)
6740 (setq var (or (cdr (assoc (match-string 4 s)
6741 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6742 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6743 'eval)
6744 c (or (match-string 3 s) "")
6745 opt (match-beginning 1)
6746 start (1+ (match-beginning 0)))
6747 (if (equal var 'time) (setq org-prefix-has-time t))
6748 (if (equal var 'tag) (setq org-prefix-has-tag t))
6749 (if (equal var 'effort) (setq org-prefix-has-effort t))
6750 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6751 (setq f (concat "%" (match-string 2 s) "s"))
6752 (when (equal var 'category)
6753 (setq org-prefix-category-length
6754 (floor (abs (string-to-number (match-string 2 s)))))
6755 (setq org-prefix-category-max-length
6756 (let ((x (match-string 2 s)))
6757 (save-match-data
6758 (if (string-match "\\.[0-9]+" x)
6759 (string-to-number (substring (match-string 0 x) 1)))))))
6760 (if (eq var 'eval)
6761 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6762 (if opt
6763 (setq varform
6764 `(if (or (equal "" ,var) (equal nil ,var))
6766 (format ,f (concat ,var ,c))))
6767 (setq varform
6768 `(format ,f (if (or (equal ,var "")
6769 (equal ,var nil)) ""
6770 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6771 (setq s (replace-match "%s" t nil s))
6772 (push varform vars))
6773 (setq vars (nreverse vars))
6774 (with-current-buffer (or org-agenda-buffer (current-buffer))
6775 (setq org-prefix-format-compiled
6776 (list
6777 `((org-prefix-has-time ,org-prefix-has-time)
6778 (org-prefix-has-tag ,org-prefix-has-tag)
6779 (org-prefix-category-length ,org-prefix-category-length)
6780 (org-prefix-has-effort ,org-prefix-has-effort)
6781 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6782 `(format ,s ,@vars))))))
6784 (defun org-set-sorting-strategy (key)
6785 (if (symbolp (car org-agenda-sorting-strategy))
6786 ;; the old format
6787 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6788 (setq org-agenda-sorting-strategy-selected
6789 (or (cdr (assq key org-agenda-sorting-strategy))
6790 (cdr (assq 'agenda org-agenda-sorting-strategy))
6791 '(time-up category-keep priority-down)))))
6793 (defun org-get-time-of-day (s &optional string mod24)
6794 "Check string S for a time of day.
6795 If found, return it as a military time number between 0 and 2400.
6796 If not found, return nil.
6797 The optional STRING argument forces conversion into a 5 character wide string
6798 HH:MM."
6799 (save-match-data
6800 (when
6801 (and
6802 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6803 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6804 (not (eq (get-text-property 1 'face s) 'org-link)))
6805 (let* ((h (string-to-number (match-string 1 s)))
6806 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6807 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6808 (am-p (equal ampm "am"))
6809 (h1 (cond ((not ampm) h)
6810 ((= h 12) (if am-p 0 12))
6811 (t (+ h (if am-p 0 12)))))
6812 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6813 (mod h1 24) h1))
6814 (t0 (+ (* 100 h2) m))
6815 (t1 (concat (if (>= h1 24) "+" " ")
6816 (if (and org-agenda-time-leading-zero
6817 (< t0 1000)) "0" "")
6818 (if (< t0 100) "0" "")
6819 (if (< t0 10) "0" "")
6820 (int-to-string t0))))
6821 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6823 (defvar org-agenda-before-sorting-filter-function nil
6824 "Function to be applied to agenda items prior to sorting.
6825 Prior to sorting also means just before they are inserted into the agenda.
6827 To aid sorting, you may revisit the original entries and add more text
6828 properties which will later be used by the sorting functions.
6830 The function should take a string argument, an agenda line.
6831 It has access to the text properties in that line, which contain among
6832 other things, the property `org-hd-marker' that points to the entry
6833 where the line comes from. Note that not all lines going into the agenda
6834 have this property, only most.
6836 The function should return the modified string. It is probably best
6837 to ONLY change text properties.
6839 You can also use this function as a filter, by returning nil for lines
6840 you don't want to have in the agenda at all. For this application, you
6841 could bind the variable in the options section of a custom command.")
6843 (defun org-agenda-finalize-entries (list &optional type)
6844 "Sort, limit and concatenate the LIST of agenda items.
6845 The optional argument TYPE tells the agenda type."
6846 (let ((max-effort (cond ((listp org-agenda-max-effort)
6847 (cdr (assoc type org-agenda-max-effort)))
6848 (t org-agenda-max-effort)))
6849 (max-todo (cond ((listp org-agenda-max-todos)
6850 (cdr (assoc type org-agenda-max-todos)))
6851 (t org-agenda-max-todos)))
6852 (max-tags (cond ((listp org-agenda-max-tags)
6853 (cdr (assoc type org-agenda-max-tags)))
6854 (t org-agenda-max-tags)))
6855 (max-entries (cond ((listp org-agenda-max-entries)
6856 (cdr (assoc type org-agenda-max-entries)))
6857 (t org-agenda-max-entries))))
6858 (when org-agenda-before-sorting-filter-function
6859 (setq list
6860 (delq nil
6861 (mapcar
6862 org-agenda-before-sorting-filter-function list))))
6863 (setq list (mapcar 'org-agenda-highlight-todo list)
6864 list (mapcar 'identity (sort list 'org-entries-lessp)))
6865 (when max-effort
6866 (setq list (org-agenda-limit-entries
6867 list 'effort-minutes max-effort
6868 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6869 32767 -1))))))
6870 (when max-todo
6871 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6872 (when max-tags
6873 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6874 (when max-entries
6875 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6876 (mapconcat 'identity list "\n")))
6878 (defun org-agenda-limit-entries (list prop limit &optional fn)
6879 "Limit the number of agenda entries."
6880 (let ((include (and limit (< limit 0))))
6881 (if limit
6882 (let ((fun (or fn (lambda (p) (if p 1))))
6883 (lim 0))
6884 (delq nil
6885 (mapcar
6886 (lambda (e)
6887 (let ((pval (funcall
6888 fun (get-text-property (1- (length e))
6889 prop e))))
6890 (if pval (setq lim (+ lim pval)))
6891 (cond ((and pval (<= lim (abs limit))) e)
6892 ((and include (not pval)) e))))
6893 list)))
6894 list)))
6896 (defun org-agenda-limit-interactively (remove)
6897 "In agenda, interactively limit entries to various maximums."
6898 (interactive "P")
6899 (if remove
6900 (progn (setq org-agenda-max-entries nil
6901 org-agenda-max-todos nil
6902 org-agenda-max-tags nil
6903 org-agenda-max-effort nil)
6904 (org-agenda-redo))
6905 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6906 (msg (cond ((= max ?E) "How many minutes? ")
6907 ((= max ?e) "How many entries? ")
6908 ((= max ?t) "How many TODO entries? ")
6909 ((= max ?T) "How many tagged entries? ")
6910 (t (user-error "Wrong input"))))
6911 (num (string-to-number (read-from-minibuffer msg))))
6912 (cond ((equal max ?e)
6913 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6914 ((equal max ?t)
6915 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6916 ((equal max ?T)
6917 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6918 ((equal max ?E)
6919 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6920 (org-agenda-fit-window-to-buffer))
6922 (defun org-agenda-highlight-todo (x)
6923 (let ((org-done-keywords org-done-keywords-for-agenda)
6924 (case-fold-search nil)
6926 (if (eq x 'line)
6927 (save-excursion
6928 (beginning-of-line 1)
6929 (setq re (org-get-at-bol 'org-todo-regexp))
6930 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6931 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6932 (add-text-properties (match-beginning 0) (match-end 1)
6933 (list 'face (org-get-todo-face 1)))
6934 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6935 (delete-region (match-beginning 1) (1- (match-end 0)))
6936 (goto-char (match-beginning 1))
6937 (insert (format org-agenda-todo-keyword-format s)))))
6938 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6939 (setq re (get-text-property 0 'org-todo-regexp x))
6940 (when (and re
6941 ;; Test `pl' because if there's no heading content,
6942 ;; there's no point matching to highlight. Note
6943 ;; that if we didn't test `pl' first, and there
6944 ;; happened to be no keyword from `org-todo-regexp'
6945 ;; on this heading line, then the `equal' comparison
6946 ;; afterwards would spuriously succeed in the case
6947 ;; where `pl' is nil -- causing an args-out-of-range
6948 ;; error when we try to add text properties to text
6949 ;; that isn't there.
6951 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6952 x pl) pl))
6953 (add-text-properties
6954 (or (match-end 1) (match-end 0)) (match-end 0)
6955 (list 'face (org-get-todo-face (match-string 2 x)))
6957 (when (match-end 1)
6958 (setq x (concat (substring x 0 (match-end 1))
6959 (format org-agenda-todo-keyword-format
6960 (match-string 2 x))
6961 (org-add-props " " (text-properties-at 0 x))
6962 (substring x (match-end 3)))))))
6963 x)))
6965 (defsubst org-cmp-values (a b property)
6966 "Compare the numeric value of text PROPERTY for string A and B."
6967 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6968 (pb (or (get-text-property (1- (length b)) property b) 0)))
6969 (cond ((> pa pb) +1)
6970 ((< pa pb) -1))))
6972 (defsubst org-cmp-effort (a b)
6973 "Compare the effort values of string A and B."
6974 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6975 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6976 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6977 (cond ((> ea eb) +1)
6978 ((< ea eb) -1))))
6980 (defsubst org-cmp-category (a b)
6981 "Compare the string values of categories of strings A and B."
6982 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6983 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6984 (cond ((string-lessp ca cb) -1)
6985 ((string-lessp cb ca) +1))))
6987 (defsubst org-cmp-todo-state (a b)
6988 "Compare the todo states of strings A and B."
6989 (let* ((ma (or (get-text-property 1 'org-marker a)
6990 (get-text-property 1 'org-hd-marker a)))
6991 (mb (or (get-text-property 1 'org-marker b)
6992 (get-text-property 1 'org-hd-marker b)))
6993 (fa (and ma (marker-buffer ma)))
6994 (fb (and mb (marker-buffer mb)))
6995 (todo-kwds
6996 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6997 (and fb (with-current-buffer fb org-todo-keywords-1))))
6998 (ta (or (get-text-property 1 'todo-state a) ""))
6999 (tb (or (get-text-property 1 'todo-state b) ""))
7000 (la (- (length (member ta todo-kwds))))
7001 (lb (- (length (member tb todo-kwds))))
7002 (donepa (member ta org-done-keywords-for-agenda))
7003 (donepb (member tb org-done-keywords-for-agenda)))
7004 (cond ((and donepa (not donepb)) -1)
7005 ((and (not donepa) donepb) +1)
7006 ((< la lb) -1)
7007 ((< lb la) +1))))
7009 (defsubst org-cmp-alpha (a b)
7010 "Compare the headlines, alphabetically."
7011 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
7012 (plb (text-property-any 0 (length b) 'org-heading t b))
7013 (ta (and pla (substring a pla)))
7014 (tb (and plb (substring b plb))))
7015 (when pla
7016 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
7017 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
7018 (setq ta (substring ta (match-end 0))))
7019 (setq ta (downcase ta)))
7020 (when plb
7021 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
7022 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
7023 (setq tb (substring tb (match-end 0))))
7024 (setq tb (downcase tb)))
7025 (cond ((not ta) +1)
7026 ((not tb) -1)
7027 ((string-lessp ta tb) -1)
7028 ((string-lessp tb ta) +1))))
7030 (defsubst org-cmp-tag (a b)
7031 "Compare the string values of the first tags of A and B."
7032 (let ((ta (car (last (get-text-property 1 'tags a))))
7033 (tb (car (last (get-text-property 1 'tags b)))))
7034 (cond ((not ta) +1)
7035 ((not tb) -1)
7036 ((string-lessp ta tb) -1)
7037 ((string-lessp tb ta) +1))))
7039 (defsubst org-cmp-time (a b)
7040 "Compare the time-of-day values of strings A and B."
7041 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
7042 (ta (or (get-text-property 1 'time-of-day a) def))
7043 (tb (or (get-text-property 1 'time-of-day b) def)))
7044 (cond ((< ta tb) -1)
7045 ((< tb ta) +1))))
7047 (defsubst org-cmp-ts (a b type)
7048 "Compare the timestamps values of entries A and B.
7049 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7050 \"timestamp_ia\", compare within each of these type. When TYPE
7051 is the empty string, compare all timestamps without respect of
7052 their type."
7053 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7054 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7055 (get-text-property 1 'ts-date a))
7056 def))
7057 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7058 (get-text-property 1 'ts-date b))
7059 def)))
7060 (cond ((< ta tb) -1)
7061 ((< tb ta) +1))))
7063 (defsubst org-cmp-habit-p (a b)
7064 "Compare the todo states of strings A and B."
7065 (let ((ha (get-text-property 1 'org-habit-p a))
7066 (hb (get-text-property 1 'org-habit-p b)))
7067 (cond ((and ha (not hb)) -1)
7068 ((and (not ha) hb) +1))))
7070 (defun org-entries-lessp (a b)
7071 "Predicate for sorting agenda entries."
7072 ;; The following variables will be used when the form is evaluated.
7073 ;; So even though the compiler complains, keep them.
7074 (let* ((ss org-agenda-sorting-strategy-selected)
7075 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7076 (org-cmp-ts a b "")))
7077 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7078 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7079 (org-cmp-ts a b "scheduled")))
7080 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7081 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7082 (org-cmp-ts a b "deadline")))
7083 (deadline-down (if deadline-up (- deadline-up) nil))
7084 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7085 (org-cmp-ts a b "timestamp_ia")))
7086 (tsia-down (if tsia-up (- tsia-up) nil))
7087 (ts-up (and (org-em 'ts-up 'ts-down ss)
7088 (org-cmp-ts a b "timestamp")))
7089 (ts-down (if ts-up (- ts-up) nil))
7090 (time-up (and (org-em 'time-up 'time-down ss)
7091 (org-cmp-time a b)))
7092 (time-down (if time-up (- time-up) nil))
7093 (stats-up (and (org-em 'stats-up 'stats-down ss)
7094 (org-cmp-values a b 'org-stats)))
7095 (stats-down (if stats-up (- stats-up) nil))
7096 (priority-up (and (org-em 'priority-up 'priority-down ss)
7097 (org-cmp-values a b 'priority)))
7098 (priority-down (if priority-up (- priority-up) nil))
7099 (effort-up (and (org-em 'effort-up 'effort-down ss)
7100 (org-cmp-effort a b)))
7101 (effort-down (if effort-up (- effort-up) nil))
7102 (category-up (and (or (org-em 'category-up 'category-down ss)
7103 (memq 'category-keep ss))
7104 (org-cmp-category a b)))
7105 (category-down (if category-up (- category-up) nil))
7106 (category-keep (if category-up +1 nil))
7107 (tag-up (and (org-em 'tag-up 'tag-down ss)
7108 (org-cmp-tag a b)))
7109 (tag-down (if tag-up (- tag-up) nil))
7110 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7111 (org-cmp-todo-state a b)))
7112 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7113 (habit-up (and (org-em 'habit-up 'habit-down ss)
7114 (org-cmp-habit-p a b)))
7115 (habit-down (if habit-up (- habit-up) nil))
7116 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7117 (org-cmp-alpha a b)))
7118 (alpha-down (if alpha-up (- alpha-up) nil))
7119 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7120 user-defined-up user-defined-down)
7121 (if (and need-user-cmp org-agenda-cmp-user-defined
7122 (functionp org-agenda-cmp-user-defined))
7123 (setq user-defined-up
7124 (funcall org-agenda-cmp-user-defined a b)
7125 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7126 (cdr (assoc
7127 (eval (cons 'or org-agenda-sorting-strategy-selected))
7128 '((-1 . t) (1 . nil) (nil . nil))))))
7130 ;;; Agenda restriction lock
7132 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7133 "Overlay to mark the headline to which agenda commands are restricted.")
7134 (overlay-put org-agenda-restriction-lock-overlay
7135 'face 'org-agenda-restriction-lock)
7136 (overlay-put org-agenda-restriction-lock-overlay
7137 'help-echo "Agendas are currently limited to this subtree.")
7138 (org-detach-overlay org-agenda-restriction-lock-overlay)
7140 ;;;###autoload
7141 (defun org-agenda-set-restriction-lock (&optional type)
7142 "Set restriction lock for agenda, to current subtree or file.
7143 Restriction will be the file if TYPE is `file', or if type is the
7144 universal prefix \\='(4), or if the cursor is before the first headline
7145 in the file. Otherwise, restriction will be to the current subtree."
7146 (interactive "P")
7147 (org-agenda-remove-restriction-lock 'noupdate)
7148 (and (equal type '(4)) (setq type 'file))
7149 (setq type (cond
7150 (type type)
7151 ((org-at-heading-p) 'subtree)
7152 ((condition-case nil (org-back-to-heading t) (error nil))
7153 'subtree)
7154 (t 'file)))
7155 (if (eq type 'subtree)
7156 (progn
7157 (setq org-agenda-restrict (current-buffer))
7158 (setq org-agenda-overriding-restriction 'subtree)
7159 (put 'org-agenda-files 'org-restrict
7160 (list (buffer-file-name (buffer-base-buffer))))
7161 (org-back-to-heading t)
7162 (move-overlay org-agenda-restriction-lock-overlay
7163 (point)
7164 (if org-agenda-restriction-lock-highlight-subtree
7165 (save-excursion (org-end-of-subtree t t) (point))
7166 (point-at-eol)))
7167 (move-marker org-agenda-restrict-begin (point))
7168 (move-marker org-agenda-restrict-end
7169 (save-excursion (org-end-of-subtree t t)))
7170 (message "Locking agenda restriction to subtree"))
7171 (put 'org-agenda-files 'org-restrict
7172 (list (buffer-file-name (buffer-base-buffer))))
7173 (setq org-agenda-restrict nil)
7174 (setq org-agenda-overriding-restriction 'file)
7175 (move-marker org-agenda-restrict-begin nil)
7176 (move-marker org-agenda-restrict-end nil)
7177 (message "Locking agenda restriction to file"))
7178 (setq current-prefix-arg nil)
7179 (org-agenda-maybe-redo))
7181 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7182 "Remove the agenda restriction lock."
7183 (interactive "P")
7184 (org-detach-overlay org-agenda-restriction-lock-overlay)
7185 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7186 (setq org-agenda-overriding-restriction nil)
7187 (setq org-agenda-restrict nil)
7188 (put 'org-agenda-files 'org-restrict nil)
7189 (move-marker org-agenda-restrict-begin nil)
7190 (move-marker org-agenda-restrict-end nil)
7191 (setq current-prefix-arg nil)
7192 (message "Agenda restriction lock removed")
7193 (or noupdate (org-agenda-maybe-redo)))
7195 (defun org-agenda-maybe-redo ()
7196 "If there is any window showing the agenda view, update it."
7197 (let ((w (get-buffer-window (or org-agenda-this-buffer-name
7198 org-agenda-buffer-name)
7200 (w0 (selected-window)))
7201 (when w
7202 (select-window w)
7203 (org-agenda-redo)
7204 (select-window w0)
7205 (if org-agenda-overriding-restriction
7206 (message "Agenda view shifted to new %s restriction"
7207 org-agenda-overriding-restriction)
7208 (message "Agenda restriction lock removed")))))
7210 ;;; Agenda commands
7212 (defun org-agenda-check-type (error &rest types)
7213 "Check if agenda buffer is of allowed type.
7214 If ERROR is non-nil, throw an error, otherwise just return nil.
7215 Allowed types are `agenda' `timeline' `todo' `tags' `search'."
7216 (if (not org-agenda-type)
7217 (error "No Org agenda currently displayed")
7218 (if (memq org-agenda-type types)
7220 (if error
7221 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7222 nil))))
7224 (defun org-agenda-Quit ()
7225 "Exit the agenda, killing the agenda buffer.
7226 Like `org-agenda-quit', but kill the buffer even when
7227 `org-agenda-sticky' is non-nil."
7228 (interactive)
7229 (org-agenda--quit))
7231 (defun org-agenda-quit ()
7232 "Exit the agenda.
7234 When `org-agenda-sticky' is non-nil, bury the agenda buffer
7235 instead of killing it.
7237 When `org-agenda-restore-windows-after-quit' is non-nil, restore
7238 the pre-agenda window configuration.
7240 When column view is active, exit column view instead of the
7241 agenda."
7242 (interactive)
7243 (org-agenda--quit org-agenda-sticky))
7245 (defun org-agenda--quit (&optional bury)
7246 (if org-agenda-columns-active
7247 (org-columns-quit)
7248 (let ((buf (current-buffer))
7249 (wconf org-agenda-pre-window-conf)
7250 (org-agenda-last-indirect-window
7251 (and (eq org-indirect-buffer-display 'other-window)
7252 org-agenda-last-indirect-buffer
7253 (get-buffer-window org-agenda-last-indirect-buffer))))
7254 (cond
7255 ((eq org-agenda-window-setup 'other-frame)
7256 (delete-frame))
7257 ((and org-agenda-restore-windows-after-quit
7258 wconf)
7259 ;; Maybe restore the pre-agenda window configuration. Reset
7260 ;; `org-agenda-pre-window-conf' before running
7261 ;; `set-window-configuration', which loses the current buffer.
7262 (setq org-agenda-pre-window-conf nil)
7263 (set-window-configuration wconf))
7265 (when org-agenda-last-indirect-window
7266 (delete-window org-agenda-last-indirect-window))
7267 (and (not (eq org-agenda-window-setup 'current-window))
7268 (not (one-window-p))
7269 (delete-window))))
7270 (if bury
7271 (bury-buffer buf)
7272 (kill-buffer buf)
7273 (setq org-agenda-archives-mode nil
7274 org-agenda-buffer nil)))))
7276 (defun org-agenda-exit ()
7277 "Exit the agenda, killing Org buffers loaded by the agenda.
7278 Like `org-agenda-Quit', but kill any buffers that were created by
7279 the agenda. Org buffers visited directly by the user will not be
7280 touched. Also, exit the agenda even if it is in column view."
7281 (interactive)
7282 (when org-agenda-columns-active
7283 (org-columns-quit))
7284 (org-release-buffers org-agenda-new-buffers)
7285 (setq org-agenda-new-buffers nil)
7286 (org-agenda-Quit))
7288 (defun org-agenda-kill-all-agenda-buffers ()
7289 "Kill all buffers in `org-agenda-mode'.
7290 This is used when toggling sticky agendas."
7291 (interactive)
7292 (let (blist)
7293 (dolist (buf (buffer-list))
7294 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7295 (push buf blist)))
7296 (mapc 'kill-buffer blist)))
7298 (defun org-agenda-execute (arg)
7299 "Execute another agenda command, keeping same window.
7300 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7301 in the agenda."
7302 (interactive "P")
7303 (let ((org-agenda-window-setup 'current-window))
7304 (org-agenda arg)))
7306 (defun org-agenda-redo (&optional all)
7307 "Rebuild possibly ALL agenda view(s) in the current buffer."
7308 (interactive "P")
7309 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7310 (cpa (unless (eq all t) current-prefix-arg))
7311 (org-agenda-doing-sticky-redo org-agenda-sticky)
7312 (org-agenda-sticky nil)
7313 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7314 org-agenda-buffer-name))
7315 (org-agenda-keep-modes t)
7316 (tag-filter org-agenda-tag-filter)
7317 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7318 (top-hl-filter org-agenda-top-headline-filter)
7319 (cat-filter org-agenda-category-filter)
7320 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7321 (re-filter org-agenda-regexp-filter)
7322 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7323 (effort-filter org-agenda-effort-filter)
7324 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7325 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7326 (cols org-agenda-columns-active)
7327 (line (org-current-line))
7328 (window-line (- line (org-current-line (window-start))))
7329 (lprops (get 'org-agenda-redo-command 'org-lprops))
7330 (redo-cmd (get-text-property p 'org-redo-cmd))
7331 (last-args (get-text-property p 'org-last-args))
7332 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7333 (org-agenda-overriding-cmd-arguments
7334 (unless (eq all t)
7335 (cond ((listp last-args)
7336 (cons (or cpa (car last-args)) (cdr last-args)))
7337 ((stringp last-args)
7338 last-args))))
7339 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7340 (put 'org-agenda-tag-filter :preset-filter nil)
7341 (put 'org-agenda-category-filter :preset-filter nil)
7342 (put 'org-agenda-regexp-filter :preset-filter nil)
7343 (put 'org-agenda-effort-filter :preset-filter nil)
7344 (and cols (org-columns-quit))
7345 (message "Rebuilding agenda buffer...")
7346 (if series-redo-cmd
7347 (eval series-redo-cmd)
7348 (org-let lprops redo-cmd))
7349 (setq org-agenda-undo-list nil
7350 org-agenda-pending-undo-list nil
7351 org-agenda-tag-filter tag-filter
7352 org-agenda-category-filter cat-filter
7353 org-agenda-regexp-filter re-filter
7354 org-agenda-effort-filter effort-filter
7355 org-agenda-top-headline-filter top-hl-filter)
7356 (message "Rebuilding agenda buffer...done")
7357 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7358 (put 'org-agenda-category-filter :preset-filter cat-preset)
7359 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7360 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7361 (let ((tag (or tag-filter tag-preset))
7362 (cat (or cat-filter cat-preset))
7363 (effort (or effort-filter effort-preset))
7364 (re (or re-filter re-preset)))
7365 (when tag (org-agenda-filter-apply tag 'tag t))
7366 (when cat (org-agenda-filter-apply cat 'category))
7367 (when effort (org-agenda-filter-apply effort 'effort))
7368 (when re (org-agenda-filter-apply re 'regexp)))
7369 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7370 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7371 (org-goto-line line)
7372 (recenter window-line)))
7374 (defvar org-global-tags-completion-table nil)
7375 (defvar org-agenda-filter-form nil)
7376 (defvar org-agenda-filtered-by-category nil)
7378 (defun org-agenda-filter-by-category (strip)
7379 "Filter lines in the agenda buffer that have a specific category.
7380 The category is that of the current line.
7381 Without prefix argument, keep only the lines of that category.
7382 With a prefix argument, exclude the lines of that category.
7384 (interactive "P")
7385 (if (and org-agenda-filtered-by-category
7386 org-agenda-category-filter)
7387 (org-agenda-filter-show-all-cat)
7388 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7389 (cond
7390 ((and cat strip)
7391 (org-agenda-filter-apply
7392 (push (concat "-" cat) org-agenda-category-filter) 'category))
7393 (cat
7394 (org-agenda-filter-apply
7395 (setq org-agenda-category-filter
7396 (list (concat "+" cat))) 'category))
7397 (t (error "No category at point"))))))
7399 (defun org-find-top-headline (&optional pos)
7400 "Find the topmost parent headline and return it."
7401 (save-excursion
7402 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7403 (if pos (goto-char pos))
7404 ;; Skip up to the topmost parent
7405 (while (ignore-errors (outline-up-heading 1) t))
7406 (ignore-errors
7407 (nth 4 (org-heading-components))))))
7409 (defvar org-agenda-filtered-by-top-headline nil)
7410 (defun org-agenda-filter-by-top-headline (strip)
7411 "Keep only those lines that are descendants from the same top headline.
7412 The top headline is that of the current line."
7413 (interactive "P")
7414 (if org-agenda-filtered-by-top-headline
7415 (progn
7416 (setq org-agenda-filtered-by-top-headline nil
7417 org-agenda-top-headline-filter nil)
7418 (org-agenda-filter-show-all-top-filter))
7419 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7420 (if toph (org-agenda-filter-top-headline-apply toph strip)
7421 (error "No top-level headline at point")))))
7423 (defvar org-agenda-regexp-filter nil)
7424 (defun org-agenda-filter-by-regexp (strip)
7425 "Filter agenda entries by a regular expression.
7426 Regexp filters are cumulative.
7427 With no prefix argument, keep entries matching the regexp.
7428 With one prefix argument, filter out entries matching the regexp.
7429 With two prefix arguments, remove the regexp filters."
7430 (interactive "P")
7431 (if (not (equal strip '(16)))
7432 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7433 (read-from-minibuffer
7434 (if (equal strip '(4))
7435 "Filter out entries matching regexp: "
7436 "Narrow to entries matching regexp: ")))))
7437 (push flt org-agenda-regexp-filter)
7438 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7439 (org-agenda-filter-show-all-re)
7440 (message "Regexp filter removed")))
7442 (defvar org-agenda-effort-filter nil)
7443 (defun org-agenda-filter-by-effort (strip)
7444 "Filter agenda entries by effort.
7445 With no prefix argument, keep entries matching the effort condition.
7446 With one prefix argument, filter out entries matching the condition.
7447 With two prefix arguments, remove the effort filters."
7448 (interactive "P")
7449 (cond ((member strip '(nil 4))
7450 (let ((efforts (org-split-string
7451 (or (cdr (assoc (concat org-effort-property "_ALL")
7452 org-global-properties))
7453 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7454 "")))
7455 (eff -1)
7456 effort-prompt op)
7457 (while (not (member op '(?< ?> ?=)))
7458 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7459 (loop for i from 0 to 9 do
7460 (setq effort-prompt
7461 (concat
7462 effort-prompt " ["
7463 (if (= i 9) "0" (int-to-string (1+ i)))
7464 "]" (nth i efforts))))
7465 (message "Effort %s%s" (char-to-string op) effort-prompt)
7466 (while (or (< eff 0) (> eff 9))
7467 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7468 (setq org-agenda-effort-filter
7469 (list (concat (if strip "-" "+")
7470 (char-to-string op) (nth (1- eff) efforts))))
7471 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7472 (t (org-agenda-filter-show-all-effort)
7473 (message "Effort filter removed"))))
7475 (defun org-agenda-filter-remove-all ()
7476 "Remove all filters from the current agenda buffer."
7477 (interactive)
7478 (when org-agenda-tag-filter
7479 (org-agenda-filter-show-all-tag))
7480 (when org-agenda-category-filter
7481 (org-agenda-filter-show-all-cat))
7482 (when org-agenda-regexp-filter
7483 (org-agenda-filter-show-all-re))
7484 (when org-agenda-top-headline-filter
7485 (org-agenda-filter-show-all-top-filter))
7486 (when org-agenda-effort-filter
7487 (org-agenda-filter-show-all-effort))
7488 (org-agenda-finalize))
7490 (defun org-agenda-filter-by-tag (arg &optional char exclude)
7491 "Keep only those lines in the agenda buffer that have a specific tag.
7492 The tag is selected with its fast selection letter, as
7493 configured. With a single \\[universal-argument] prefix ARG,
7494 exclude the agenda search. With a double \\[universal-argument]
7495 prefix ARG, filter the literal tag. I.e. don't filter on all its
7496 group members.
7498 A lisp caller can specify CHAR. EXCLUDE means that the new tag should be
7499 used to exclude the search - the interactive user can also press `-' or `+'
7500 to switch between filtering and excluding."
7501 (interactive "P")
7502 (let* ((alist org-tag-alist-for-agenda)
7503 (tag-chars (mapconcat
7504 (lambda (x) (if (and (not (symbolp (car x)))
7505 (cdr x))
7506 (char-to-string (cdr x))
7507 ""))
7508 org-tag-alist-for-agenda ""))
7509 (valid-char-list (append '(?\t ?\r ?/ ?. ?\s ?q)
7510 (string-to-list tag-chars)))
7511 (exclude (or exclude (equal arg '(4))))
7512 (expand (not (equal arg '(16))))
7513 (inhibit-read-only t)
7514 (current org-agenda-tag-filter)
7515 a n tag)
7516 (unless char
7517 (while (not (memq char valid-char-list))
7518 (message
7519 "%s by tag [%s ], [TAB], %s[/]:off, [+/-]:filter/exclude%s, [q]:quit"
7520 (if exclude "Exclude" "Filter") tag-chars
7521 (if org-agenda-auto-exclude-function "[RET], " "")
7522 (if expand "" ", no grouptag expand"))
7523 (setq char (read-char-exclusive))
7524 ;; Excluding or filtering down
7525 (cond ((eq char ?-) (setq exclude t))
7526 ((eq char ?+) (setq exclude nil)))))
7527 (when (eq char ?\t)
7528 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7529 (org-set-local 'org-global-tags-completion-table
7530 (org-global-tags-completion-table)))
7531 (let ((completion-ignore-case t))
7532 (setq tag (org-icompleting-read
7533 "Tag: " org-global-tags-completion-table))))
7534 (cond
7535 ((eq char ?\r)
7536 (org-agenda-filter-show-all-tag)
7537 (when org-agenda-auto-exclude-function
7538 (setq org-agenda-tag-filter nil)
7539 (dolist (tag (org-agenda-get-represented-tags))
7540 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7541 (if modifier
7542 (push modifier org-agenda-tag-filter))))
7543 (if (not (null org-agenda-tag-filter))
7544 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))))
7545 ((eq char ?/)
7546 (org-agenda-filter-show-all-tag)
7547 (when (get 'org-agenda-tag-filter :preset-filter)
7548 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand)))
7549 ((eq char ?.)
7550 (setq org-agenda-tag-filter
7551 (mapcar (lambda(tag) (concat "+" tag))
7552 (org-get-at-bol 'tags)))
7553 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7554 ((eq char ?q)) ;If q, abort (even if there is a q-key for a tag...)
7555 ((or (eq char ?\s)
7556 (setq a (rassoc char alist))
7557 (and tag (setq a (cons tag nil))))
7558 (org-agenda-filter-show-all-tag)
7559 (setq tag (car a))
7560 (setq org-agenda-tag-filter
7561 (cons (concat (if exclude "-" "+") tag)
7562 current))
7563 (org-agenda-filter-apply org-agenda-tag-filter 'tag expand))
7564 (t (error "Invalid tag selection character %c" char)))))
7566 (defun org-agenda-get-represented-tags ()
7567 "Get a list of all tags currently represented in the agenda."
7568 (let (p tags)
7569 (save-excursion
7570 (goto-char (point-min))
7571 (while (setq p (next-single-property-change (point) 'tags))
7572 (goto-char p)
7573 (mapc (lambda (x) (add-to-list 'tags x))
7574 (get-text-property (point) 'tags))))
7575 tags))
7577 (defun org-agenda-filter-by-tag-refine (arg &optional char)
7578 "Refine the current filter. See `org-agenda-filter-by-tag'."
7579 (interactive "P")
7580 (org-agenda-filter-by-tag arg char 'refine))
7582 (defun org-agenda-filter-make-matcher (filter type &optional expand)
7583 "Create the form that tests a line for agenda filter. Optional
7584 argument EXPAND can be used for the TYPE tag and will expand the
7585 tags in the FILTER if any of the tags in FILTER are grouptags."
7586 (let (f f1)
7587 (cond
7588 ;; Tag filter
7589 ((eq type 'tag)
7590 (setq filter
7591 (delete-dups
7592 (append (get 'org-agenda-tag-filter :preset-filter)
7593 filter)))
7594 (dolist (x filter)
7595 (let ((op (string-to-char x)))
7596 (if expand (setq x (org-agenda-filter-expand-tags (list x) t))
7597 (setq x (list x)))
7598 (setq f1 (org-agenda-filter-make-matcher-tag-exp x op))
7599 (push f1 f))))
7600 ;; Category filter
7601 ((eq type 'category)
7602 (setq filter
7603 (delete-dups
7604 (append (get 'org-agenda-category-filter :preset-filter)
7605 filter)))
7606 (dolist (x filter)
7607 (if (equal "-" (substring x 0 1))
7608 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7609 (setq f1 (list 'equal (substring x 1) 'cat)))
7610 (push f1 f)))
7611 ;; Regexp filter
7612 ((eq type 'regexp)
7613 (setq filter
7614 (delete-dups
7615 (append (get 'org-agenda-regexp-filter :preset-filter)
7616 filter)))
7617 (dolist (x filter)
7618 (if (equal "-" (substring x 0 1))
7619 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7620 (setq f1 (list 'string-match (substring x 1) 'txt)))
7621 (push f1 f)))
7622 ;; Effort filter
7623 ((eq type 'effort)
7624 (setq filter
7625 (delete-dups
7626 (append (get 'org-agenda-effort-filter :preset-filter)
7627 filter)))
7628 (dolist (x filter)
7629 (push (org-agenda-filter-effort-form x) f))))
7630 (cons 'and (nreverse f))))
7632 (defun org-agenda-filter-make-matcher-tag-exp (tags op)
7633 "Create the form that tests a line for agenda filter for
7634 tag-expressions. Return a match-expression given TAGS. OP is an
7635 operator of type CHAR that allows the function to set the right
7636 switches in the returned form."
7637 (let (f f1) ;f = return expression. f1 = working-area
7638 (dolist (x tags)
7639 (let* ((tag (substring x 1))
7640 (isregexp (and (equal "{" (substring tag 0 1))
7641 (equal "}" (substring tag -1))))
7642 regexp)
7643 (cond
7644 (isregexp
7645 (setq regexp (substring tag 1 -1))
7646 (setq f1 (list 'org-match-any-p regexp 'tags)))
7648 (setq f1 (list 'member (downcase tag) 'tags))))
7649 (when (eq op ?-)
7650 (setq f1 (list 'not f1))))
7651 (push f1 f))
7652 ;; Any of the expressions can match if op = +
7653 ;; all must match if the operator is -.
7654 (if (eq op ?-)
7655 (cons 'and f)
7656 (cons 'or f))))
7658 (defun org-agenda-filter-effort-form (e)
7659 "Return the form to compare the effort of the current line with what E says.
7660 E looks like \"+<2:25\"."
7661 (let (op)
7662 (setq e (substring e 1))
7663 (setq op (string-to-char e) e (substring e 1))
7664 (setq op (cond ((equal op ?<) '<=)
7665 ((equal op ?>) '>=)
7666 ((equal op ??) op)
7667 (t '=)))
7668 (list 'org-agenda-compare-effort (list 'quote op)
7669 (org-duration-string-to-minutes e))))
7671 (defun org-agenda-compare-effort (op value)
7672 "Compare the effort of the current line with VALUE, using OP.
7673 If the line does not have an effort defined, return nil."
7674 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7675 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7676 value)))
7678 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7679 "Expand group tags in FILTER for the agenda.
7680 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7681 (if org-group-tags
7682 (let ((case-fold-search t) rtn)
7683 (mapc
7684 (lambda (f)
7685 (let (f0 dir)
7686 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7687 (setq dir (match-string 1 f) f0 (match-string 2 f))
7688 (setq dir (if no-operator "" "+") f0 f))
7689 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7690 (org-tags-expand f0 t t))
7691 rtn))))
7692 filter)
7693 (reverse rtn))
7694 filter))
7696 (defun org-agenda-filter-apply (filter type &optional expand)
7697 "Set FILTER as the new agenda filter and apply it. Optional
7698 argument EXPAND can be used for the TYPE tag and will expand the
7699 tags in the FILTER if any of the tags in FILTER are grouptags."
7700 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7701 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7702 (let (tags cat txt)
7703 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type expand))
7704 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7705 ;; category is used as the filter:
7706 (setq org-agenda-filtered-by-category
7707 (and (eq type 'category)
7708 (not (equal (substring (car filter) 0 1) "-"))))
7709 (org-agenda-set-mode-name)
7710 (save-excursion
7711 (goto-char (point-min))
7712 (while (not (eobp))
7713 (if (org-get-at-bol 'org-marker)
7714 (progn
7715 (setq tags (org-get-at-bol 'tags)
7716 cat (org-get-at-eol 'org-category 1)
7717 txt (org-get-at-eol 'txt 1))
7718 (if (not (eval org-agenda-filter-form))
7719 (org-agenda-filter-hide-line type))
7720 (beginning-of-line 2))
7721 (beginning-of-line 2))))
7722 (if (get-char-property (point) 'invisible)
7723 (ignore-errors (org-agenda-previous-line)))))
7725 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7726 "Filter by top headline HL."
7727 (org-agenda-set-mode-name)
7728 (save-excursion
7729 (goto-char (point-min))
7730 (while (not (eobp))
7731 (let* ((pos (org-get-at-bol 'org-hd-marker))
7732 (tophl (and pos (org-find-top-headline pos))))
7733 (if (and tophl (funcall (if negative 'identity 'not)
7734 (string= hl tophl)))
7735 (org-agenda-filter-hide-line 'top-headline)))
7736 (beginning-of-line 2)))
7737 (if (get-char-property (point) 'invisible)
7738 (org-agenda-previous-line))
7739 (setq org-agenda-top-headline-filter hl
7740 org-agenda-filtered-by-top-headline t))
7742 (defun org-agenda-filter-hide-line (type)
7743 "Hide lines with TYPE in the agenda buffer."
7744 (let* ((b (max (point-min) (1- (point-at-bol))))
7745 (e (point-at-eol)))
7746 (let ((inhibit-read-only t))
7747 (add-text-properties
7748 b e `(invisible org-filtered org-filter-type ,type)))))
7750 (defun org-agenda-remove-filter (type)
7751 (interactive)
7752 "Remove filter of type TYPE from the agenda buffer."
7753 (save-excursion
7754 (goto-char (point-min))
7755 (let ((inhibit-read-only t) pos)
7756 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7757 (goto-char pos)
7758 (remove-text-properties
7759 (point) (next-single-property-change (point) 'org-filter-type)
7760 `(invisible org-filtered org-filter-type ,type))))
7761 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7762 (setq org-agenda-filter-form nil)
7763 (org-agenda-set-mode-name)
7764 (org-agenda-finalize)))
7766 (defun org-agenda-filter-show-all-tag nil
7767 (org-agenda-remove-filter 'tag))
7768 (defun org-agenda-filter-show-all-re nil
7769 (org-agenda-remove-filter 'regexp))
7770 (defun org-agenda-filter-show-all-effort nil
7771 (org-agenda-remove-filter 'effort))
7772 (defun org-agenda-filter-show-all-cat nil
7773 (org-agenda-remove-filter 'category))
7774 (defun org-agenda-filter-show-all-top-filter nil
7775 (org-agenda-remove-filter 'top-headline))
7777 (defun org-agenda-manipulate-query-add ()
7778 "Manipulate the query by adding a search term with positive selection.
7779 Positive selection means the term must be matched for selection of an entry."
7780 (interactive)
7781 (org-agenda-manipulate-query ?\[))
7782 (defun org-agenda-manipulate-query-subtract ()
7783 "Manipulate the query by adding a search term with negative selection.
7784 Negative selection means term must not be matched for selection of an entry."
7785 (interactive)
7786 (org-agenda-manipulate-query ?\]))
7787 (defun org-agenda-manipulate-query-add-re ()
7788 "Manipulate the query by adding a search regexp with positive selection.
7789 Positive selection means the regexp must match for selection of an entry."
7790 (interactive)
7791 (org-agenda-manipulate-query ?\{))
7792 (defun org-agenda-manipulate-query-subtract-re ()
7793 "Manipulate the query by adding a search regexp with negative selection.
7794 Negative selection means regexp must not match for selection of an entry."
7795 (interactive)
7796 (org-agenda-manipulate-query ?\}))
7797 (defun org-agenda-manipulate-query (char)
7798 (cond
7799 ((memq org-agenda-type '(timeline agenda))
7800 (let ((org-agenda-include-inactive-timestamps t))
7801 (org-agenda-redo))
7802 (message "Display now includes inactive timestamps as well"))
7803 ((eq org-agenda-type 'search)
7804 (org-add-to-string
7805 'org-agenda-query-string
7806 (if org-agenda-last-search-view-search-was-boolean
7807 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7808 (?\{ . " +{}") (?\} . " -{}"))))
7809 " "))
7810 (setq org-agenda-redo-command
7811 (list 'org-search-view
7812 (car (get-text-property (min (1- (point-max)) (point))
7813 'org-last-args))
7814 org-agenda-query-string
7815 (+ (length org-agenda-query-string)
7816 (if (member char '(?\{ ?\})) 0 1))))
7817 (set-register org-agenda-query-register org-agenda-query-string)
7818 (let ((org-agenda-overriding-arguments
7819 (cdr org-agenda-redo-command)))
7820 (org-agenda-redo)))
7821 (t (error "Cannot manipulate query for %s-type agenda buffers"
7822 org-agenda-type))))
7824 (defun org-add-to-string (var string)
7825 (set var (concat (symbol-value var) string)))
7827 (defun org-agenda-goto-date (span)
7828 "Jump to DATE in agenda."
7829 (interactive "P")
7830 (let* ((org-read-date-prefer-future
7831 (eval org-agenda-jump-prefer-future))
7832 (date (org-read-date))
7833 (day (time-to-days (org-time-string-to-time date)))
7834 (org-agenda-sticky-orig org-agenda-sticky)
7835 (org-agenda-buffer-tmp-name (buffer-name))
7836 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7837 (0-arg (or current-prefix-arg (car args)))
7838 (2-arg (nth 2 args))
7839 (with-hour-p (nth 4 org-agenda-redo-command))
7840 (newcmd (list 'org-agenda-list 0-arg date
7841 (org-agenda-span-to-ndays
7842 2-arg (org-time-string-to-absolute date))
7843 with-hour-p))
7844 (newargs (cdr newcmd))
7845 (inhibit-read-only t)
7846 org-agenda-sticky)
7847 (if (not (org-agenda-check-type t 'agenda))
7848 (error "Not available in non-agenda views")
7849 (add-text-properties (point-min) (point-max)
7850 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7851 (org-agenda-redo)
7852 (goto-char (point-min))
7853 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7854 (save-excursion (move-beginning-of-line 2) (eobp))))
7855 (move-beginning-of-line 2))
7856 (setq org-agenda-sticky org-agenda-sticky-orig
7857 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7859 (defun org-agenda-goto-today ()
7860 "Go to today."
7861 (interactive)
7862 (org-agenda-check-type t 'timeline 'agenda)
7863 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7864 (curspan (nth 2 args))
7865 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7866 (cond
7867 (tdpos (goto-char tdpos))
7868 ((eq org-agenda-type 'agenda)
7869 (let* ((sd (org-agenda-compute-starting-span
7870 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7871 (org-agenda-overriding-arguments args))
7872 (setf (nth 1 org-agenda-overriding-arguments) sd)
7873 (org-agenda-redo)
7874 (org-agenda-find-same-or-today-or-agenda)))
7875 (t (error "Cannot find today")))))
7877 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7878 (goto-char
7879 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7880 (text-property-any (point-min) (point-max) 'org-today t)
7881 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7882 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7883 (org-agenda-backward-block))
7884 (point-min))))
7886 (defun org-agenda-backward-block ()
7887 "Move backward by one agenda block."
7888 (interactive)
7889 (org-agenda-forward-block 'backward))
7891 (defun org-agenda-forward-block (&optional backward)
7892 "Move forward by one agenda block.
7893 When optional argument BACKWARD is set, go backward"
7894 (interactive)
7895 (cond ((not (derived-mode-p 'org-agenda-mode))
7896 (user-error
7897 "Cannot execute this command outside of org-agenda-mode buffers"))
7898 ((looking-at (if backward "\\`" "\\'"))
7899 (message "Already at the %s block" (if backward "first" "last")))
7900 (t (let ((pos (prog1 (point)
7901 (ignore-errors (if backward (backward-char 1)
7902 (move-end-of-line 1)))))
7903 (f (if backward
7904 'previous-single-property-change
7905 'next-single-property-change))
7906 moved dest)
7907 (while (and (setq dest (funcall
7908 f (point) 'org-agenda-structural-header))
7909 (not (get-text-property
7910 (point) 'org-agenda-structural-header)))
7911 (setq moved t)
7912 (goto-char dest))
7913 (if moved (move-beginning-of-line 1)
7914 (goto-char (if backward (point-min) (point-max)))
7915 (move-beginning-of-line 1)
7916 (message "No %s block" (if backward "previous" "further")))))))
7918 (defun org-agenda-later (arg)
7919 "Go forward in time by the current span.
7920 With prefix ARG, go forward that many times the current span."
7921 (interactive "p")
7922 (org-agenda-check-type t 'agenda)
7923 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7924 (span (or (nth 2 args) org-agenda-current-span))
7925 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7926 (greg (calendar-gregorian-from-absolute sd))
7927 (cnt (org-get-at-bol 'org-day-cnt))
7928 greg2)
7929 (cond
7930 ((numberp span)
7931 (setq sd (+ (* span arg) sd)))
7932 ((eq span 'day)
7933 (setq sd (+ arg sd)))
7934 ((eq span 'week)
7935 (setq sd (+ (* 7 arg) sd)))
7936 ((eq span 'fortnight)
7937 (setq sd (+ (* 14 arg) sd)))
7938 ((eq span 'month)
7939 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7940 sd (calendar-absolute-from-gregorian greg2))
7941 (setcar greg2 (1+ (car greg2))))
7942 ((eq span 'year)
7943 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7944 sd (calendar-absolute-from-gregorian greg2))
7945 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7947 (setq sd (+ (* span arg) sd))))
7948 (let ((org-agenda-overriding-cmd
7949 ;; `cmd' may have been set by `org-agenda-run-series' which
7950 ;; uses `org-agenda-overriding-cmd' to decide whether
7951 ;; overriding is allowed for `cmd'
7952 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7953 (org-agenda-overriding-arguments
7954 (list (car args) sd span)))
7955 (org-agenda-redo)
7956 (org-agenda-find-same-or-today-or-agenda cnt))))
7958 (defun org-agenda-earlier (arg)
7959 "Go backward in time by the current span.
7960 With prefix ARG, go backward that many times the current span."
7961 (interactive "p")
7962 (org-agenda-later (- arg)))
7964 (defun org-agenda-view-mode-dispatch ()
7965 "Call one of the view mode commands."
7966 (interactive)
7967 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7968 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7969 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7970 (let ((a (read-char-exclusive)))
7971 (case a
7972 (?\ (call-interactively 'org-agenda-reset-view))
7973 (?d (call-interactively 'org-agenda-day-view))
7974 (?w (call-interactively 'org-agenda-week-view))
7975 (?t (call-interactively 'org-agenda-fortnight-view))
7976 (?m (call-interactively 'org-agenda-month-view))
7977 (?y (call-interactively 'org-agenda-year-view))
7978 (?l (call-interactively 'org-agenda-log-mode))
7979 (?L (org-agenda-log-mode '(4)))
7980 (?c (org-agenda-log-mode 'clockcheck))
7981 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7982 (?a (call-interactively 'org-agenda-archives-mode))
7983 (?A (org-agenda-archives-mode 'files))
7984 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7985 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7986 (?G (call-interactively 'org-agenda-toggle-time-grid))
7987 (?D (call-interactively 'org-agenda-toggle-diary))
7988 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7989 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7990 (org-agenda-check-type t 'timeline 'agenda)
7991 (org-agenda-redo))
7992 (message "Display now includes inactive timestamps as well"))
7993 (?q (message "Abort"))
7994 (otherwise (error "Invalid key" )))))
7996 (defun org-agenda-reset-view ()
7997 "Switch to default view for agenda."
7998 (interactive)
7999 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
8000 (defun org-agenda-day-view (&optional day-of-month)
8001 "Switch to daily view for agenda.
8002 With argument DAY-OF-MONTH, switch to that day of the month."
8003 (interactive "P")
8004 (org-agenda-change-time-span 'day day-of-month))
8005 (defun org-agenda-week-view (&optional iso-week)
8006 "Switch to daily view for agenda.
8007 With argument ISO-WEEK, switch to the corresponding ISO week.
8008 If ISO-WEEK has more then 2 digits, only the last two encode the
8009 week. Any digits before this encode a year. So 200712 means
8010 week 12 of year 2007. Years in the range 1938-2037 can also be
8011 written as 2-digit years."
8012 (interactive "P")
8013 (org-agenda-change-time-span 'week iso-week))
8014 (defun org-agenda-fortnight-view (&optional iso-week)
8015 "Switch to daily view for agenda.
8016 With argument ISO-WEEK, switch to the corresponding ISO week.
8017 If ISO-WEEK has more then 2 digits, only the last two encode the
8018 week. Any digits before this encode a year. So 200712 means
8019 week 12 of year 2007. Years in the range 1938-2037 can also be
8020 written as 2-digit years."
8021 (interactive "P")
8022 (org-agenda-change-time-span 'fortnight iso-week))
8023 (defun org-agenda-month-view (&optional month)
8024 "Switch to monthly view for agenda.
8025 With argument MONTH, switch to that month."
8026 (interactive "P")
8027 (org-agenda-change-time-span 'month month))
8028 (defun org-agenda-year-view (&optional year)
8029 "Switch to yearly view for agenda.
8030 With argument YEAR, switch to that year.
8031 If MONTH has more then 2 digits, only the last two encode the
8032 month. Any digits before this encode a year. So 200712 means
8033 December year 2007. Years in the range 1938-2037 can also be
8034 written as 2-digit years."
8035 (interactive "P")
8036 (when year
8037 (setq year (org-small-year-to-year year)))
8038 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
8039 (org-agenda-change-time-span 'year year)
8040 (error "Abort")))
8042 (defun org-agenda-change-time-span (span &optional n)
8043 "Change the agenda view to SPAN.
8044 SPAN may be `day', `week', `fortnight', `month', `year'."
8045 (org-agenda-check-type t 'agenda)
8046 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
8047 (curspan (nth 2 args)))
8048 (if (and (not n) (equal curspan span))
8049 (error "Viewing span is already \"%s\"" span))
8050 (let* ((sd (or (org-get-at-bol 'day)
8051 (nth 1 args)
8052 org-starting-day))
8053 (sd (org-agenda-compute-starting-span sd span n))
8054 (org-agenda-overriding-cmd
8055 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
8056 (org-agenda-overriding-arguments
8057 (list (car args) sd span)))
8058 (org-agenda-redo)
8059 (org-agenda-find-same-or-today-or-agenda))
8060 (org-agenda-set-mode-name)
8061 (message "Switched to %s view" span)))
8063 (defun org-agenda-compute-starting-span (sd span &optional n)
8064 "Compute starting date for agenda.
8065 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
8066 is a cons cell with the starting date and the number of days,
8067 so that the date SD will be in that range."
8068 (let* ((greg (calendar-gregorian-from-absolute sd))
8069 (dg (nth 1 greg))
8070 (mg (car greg))
8071 (yg (nth 2 greg)))
8072 (cond
8073 ((eq span 'day)
8074 (when n
8075 (setq sd (+ (calendar-absolute-from-gregorian
8076 (list mg 1 yg))
8077 n -1))))
8078 ((or (eq span 'week) (eq span 'fortnight))
8079 (let* ((nt (calendar-day-of-week
8080 (calendar-gregorian-from-absolute sd)))
8081 (d (if org-agenda-start-on-weekday
8082 (- nt org-agenda-start-on-weekday)
8085 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8086 (when n
8087 (require 'cal-iso)
8088 (when (> n 99)
8089 (setq y1 (org-small-year-to-year (/ n 100))
8090 n (mod n 100)))
8091 (setq sd
8092 (calendar-iso-to-absolute
8093 (list n 1
8094 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8095 ((eq span 'month)
8096 (let (y1)
8097 (when (and n (> n 99))
8098 (setq y1 (org-small-year-to-year (/ n 100))
8099 n (mod n 100)))
8100 (setq sd (calendar-absolute-from-gregorian
8101 (list (or n mg) 1 (or y1 yg))))))
8102 ((eq span 'year)
8103 (setq sd (calendar-absolute-from-gregorian
8104 (list 1 1 (or n yg))))))
8105 sd))
8107 (defun org-agenda-next-date-line (&optional arg)
8108 "Jump to the next line indicating a date in agenda buffer."
8109 (interactive "p")
8110 (org-agenda-check-type t 'agenda 'timeline)
8111 (beginning-of-line 1)
8112 ;; This does not work if user makes date format that starts with a blank
8113 (if (looking-at "^\\S-") (forward-char 1))
8114 (if (not (re-search-forward "^\\S-" nil t arg))
8115 (progn
8116 (backward-char 1)
8117 (error "No next date after this line in this buffer")))
8118 (goto-char (match-beginning 0)))
8120 (defun org-agenda-previous-date-line (&optional arg)
8121 "Jump to the previous line indicating a date in agenda buffer."
8122 (interactive "p")
8123 (org-agenda-check-type t 'agenda 'timeline)
8124 (beginning-of-line 1)
8125 (if (not (re-search-backward "^\\S-" nil t arg))
8126 (error "No previous date before this line in this buffer")))
8128 ;; Initialize the highlight
8129 (defvar org-hl (make-overlay 1 1))
8130 (overlay-put org-hl 'face 'highlight)
8132 (defun org-highlight (begin end &optional buffer)
8133 "Highlight a region with overlay."
8134 (move-overlay org-hl begin end (or buffer (current-buffer))))
8136 (defun org-unhighlight ()
8137 "Detach overlay INDEX."
8138 (org-detach-overlay org-hl))
8140 (defun org-unhighlight-once ()
8141 "Remove the highlight from its position, and this function from the hook."
8142 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8143 (org-unhighlight))
8145 (defvar org-agenda-pre-follow-window-conf nil)
8146 (defun org-agenda-follow-mode ()
8147 "Toggle follow mode in an agenda buffer."
8148 (interactive)
8149 (unless org-agenda-follow-mode
8150 (setq org-agenda-pre-follow-window-conf
8151 (current-window-configuration)))
8152 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8153 (unless org-agenda-follow-mode
8154 (set-window-configuration org-agenda-pre-follow-window-conf))
8155 (org-agenda-set-mode-name)
8156 (org-agenda-do-context-action)
8157 (message "Follow mode is %s"
8158 (if org-agenda-follow-mode "on" "off")))
8160 (defun org-agenda-entry-text-mode (&optional arg)
8161 "Toggle entry text mode in an agenda buffer."
8162 (interactive "P")
8163 (if (or org-agenda-tag-filter
8164 org-agenda-category-filter
8165 org-agenda-regexp-filter
8166 org-agenda-top-headline-filter)
8167 (user-error "Can't show entry text in filtered views")
8168 (setq org-agenda-entry-text-mode (or (integerp arg)
8169 (not org-agenda-entry-text-mode)))
8170 (org-agenda-entry-text-hide)
8171 (and org-agenda-entry-text-mode
8172 (let ((org-agenda-entry-text-maxlines
8173 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8174 (org-agenda-entry-text-show)))
8175 (org-agenda-set-mode-name)
8176 (message "Entry text mode is %s%s"
8177 (if org-agenda-entry-text-mode "on" "off")
8178 (if (not org-agenda-entry-text-mode) ""
8179 (format " (maximum number of lines is %d)"
8180 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8182 (defun org-agenda-clockreport-mode ()
8183 "Toggle clocktable mode in an agenda buffer."
8184 (interactive)
8185 (org-agenda-check-type t 'agenda)
8186 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8187 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8188 (org-agenda-set-mode-name)
8189 (org-agenda-redo)
8190 (message "Clocktable mode is %s"
8191 (if org-agenda-clockreport-mode "on" "off")))
8193 (defun org-agenda-log-mode (&optional special)
8194 "Toggle log mode in an agenda buffer.
8195 With argument SPECIAL, show all possible log items, not only the ones
8196 configured in `org-agenda-log-mode-items'.
8197 With a double \\[universal-argument] prefix arg, show *only* \
8198 log items, nothing else."
8199 (interactive "P")
8200 (org-agenda-check-type t 'agenda 'timeline)
8201 (setq org-agenda-show-log
8202 (cond
8203 ((equal special '(16)) 'only)
8204 ((eq special 'clockcheck)
8205 (if (eq org-agenda-show-log 'clockcheck)
8206 nil 'clockcheck))
8207 (special '(closed clock state))
8208 (t (not org-agenda-show-log))))
8209 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8210 (org-agenda-set-mode-name)
8211 (org-agenda-redo)
8212 (message "Log mode is %s"
8213 (if org-agenda-show-log "on" "off")))
8215 (defun org-agenda-archives-mode (&optional with-files)
8216 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8217 When called with a prefix argument, include all archive files as well."
8218 (interactive "P")
8219 (setq org-agenda-archives-mode
8220 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8221 (org-agenda-set-mode-name)
8222 (org-agenda-redo)
8223 (message
8224 "%s"
8225 (cond
8226 ((eq org-agenda-archives-mode nil)
8227 "No archives are included")
8228 ((eq org-agenda-archives-mode 'trees)
8229 (format "Trees with :%s: tag are included" org-archive-tag))
8230 ((eq org-agenda-archives-mode t)
8231 (format "Trees with :%s: tag and all active archive files are included"
8232 org-archive-tag)))))
8234 (defun org-agenda-toggle-diary ()
8235 "Toggle diary inclusion in an agenda buffer."
8236 (interactive)
8237 (org-agenda-check-type t 'agenda)
8238 (setq org-agenda-include-diary (not org-agenda-include-diary))
8239 (org-agenda-redo)
8240 (org-agenda-set-mode-name)
8241 (message "Diary inclusion turned %s"
8242 (if org-agenda-include-diary "on" "off")))
8244 (defun org-agenda-toggle-deadlines ()
8245 "Toggle inclusion of entries with a deadline in an agenda buffer."
8246 (interactive)
8247 (org-agenda-check-type t 'agenda)
8248 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8249 (org-agenda-redo)
8250 (org-agenda-set-mode-name)
8251 (message "Deadlines inclusion turned %s"
8252 (if org-agenda-include-deadlines "on" "off")))
8254 (defun org-agenda-toggle-time-grid ()
8255 "Toggle time grid in an agenda buffer."
8256 (interactive)
8257 (org-agenda-check-type t 'agenda)
8258 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8259 (org-agenda-redo)
8260 (org-agenda-set-mode-name)
8261 (message "Time-grid turned %s"
8262 (if org-agenda-use-time-grid "on" "off")))
8264 (defun org-agenda-set-mode-name ()
8265 "Set the mode name to indicate all the small mode settings."
8266 (setq mode-name
8267 (list "Org-Agenda"
8268 (if (get 'org-agenda-files 'org-restrict) " []" "")
8270 '(:eval (org-agenda-span-name org-agenda-current-span))
8271 (if org-agenda-follow-mode " Follow" "")
8272 (if org-agenda-entry-text-mode " ETxt" "")
8273 (if org-agenda-include-diary " Diary" "")
8274 (if org-agenda-include-deadlines " Ddl" "")
8275 (if org-agenda-use-time-grid " Grid" "")
8276 (if (and (boundp 'org-habit-show-habits)
8277 org-habit-show-habits) " Habit" "")
8278 (cond
8279 ((consp org-agenda-show-log) " LogAll")
8280 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8281 (org-agenda-show-log " Log")
8282 (t ""))
8283 (if (or org-agenda-category-filter
8284 (get 'org-agenda-category-filter :preset-filter))
8285 '(:eval (org-propertize
8286 (concat " <"
8287 (mapconcat
8288 'identity
8289 (append
8290 (get 'org-agenda-category-filter :preset-filter)
8291 org-agenda-category-filter)
8293 ">")
8294 'face 'org-agenda-filter-category
8295 'help-echo "Category used in filtering")) "")
8296 (if (or org-agenda-tag-filter
8297 (get 'org-agenda-tag-filter :preset-filter))
8298 '(:eval (org-propertize
8299 (concat " {"
8300 (mapconcat
8301 'identity
8302 (append
8303 (get 'org-agenda-tag-filter :preset-filter)
8304 org-agenda-tag-filter)
8306 "}")
8307 'face 'org-agenda-filter-tags
8308 'help-echo "Tags used in filtering")) "")
8309 (if (or org-agenda-effort-filter
8310 (get 'org-agenda-effort-filter :preset-filter))
8311 '(:eval (org-propertize
8312 (concat " {"
8313 (mapconcat
8314 'identity
8315 (append
8316 (get 'org-agenda-effort-filter :preset-filter)
8317 org-agenda-effort-filter)
8319 "}")
8320 'face 'org-agenda-filter-effort
8321 'help-echo "Effort conditions used in filtering")) "")
8322 (if (or org-agenda-regexp-filter
8323 (get 'org-agenda-regexp-filter :preset-filter))
8324 '(:eval (org-propertize
8325 (concat " ["
8326 (mapconcat
8327 'identity
8328 (append
8329 (get 'org-agenda-regexp-filter :preset-filter)
8330 org-agenda-regexp-filter)
8332 "]")
8333 'face 'org-agenda-filter-regexp
8334 'help-echo "Regexp used in filtering")) "")
8335 (if org-agenda-archives-mode
8336 (if (eq org-agenda-archives-mode t)
8337 " Archives"
8338 (format " :%s:" org-archive-tag))
8340 (if org-agenda-clockreport-mode " Clock" "")))
8341 (force-mode-line-update))
8343 (define-obsolete-function-alias
8344 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8346 (defun org-agenda-update-agenda-type ()
8347 "Update the agenda type after each command."
8348 (setq org-agenda-type
8349 (or (get-text-property (point) 'org-agenda-type)
8350 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8352 (defun org-agenda-next-line ()
8353 "Move cursor to the next line, and show if follow mode is active."
8354 (interactive)
8355 (call-interactively 'next-line)
8356 (org-agenda-do-context-action))
8358 (defun org-agenda-previous-line ()
8359 "Move cursor to the previous line, and show if follow-mode is active."
8360 (interactive)
8361 (call-interactively 'previous-line)
8362 (org-agenda-do-context-action))
8364 (defun org-agenda-next-item (n)
8365 "Move cursor to next agenda item."
8366 (interactive "p")
8367 (let ((col (current-column)))
8368 (dotimes (c n)
8369 (when (next-single-property-change (point-at-eol) 'org-marker)
8370 (move-end-of-line 1)
8371 (goto-char (next-single-property-change (point) 'org-marker))))
8372 (org-move-to-column col))
8373 (org-agenda-do-context-action))
8375 (defun org-agenda-previous-item (n)
8376 "Move cursor to next agenda item."
8377 (interactive "p")
8378 (dotimes (c n)
8379 (let ((col (current-column))
8380 (goto (save-excursion
8381 (move-end-of-line 0)
8382 (previous-single-property-change (point) 'org-marker))))
8383 (if goto (goto-char goto))
8384 (org-move-to-column col)))
8385 (org-agenda-do-context-action))
8387 (defun org-agenda-do-context-action ()
8388 "Show outline path and, maybe, follow mode window."
8389 (let ((m (org-get-at-bol 'org-marker)))
8390 (when (and (markerp m) (marker-buffer m))
8391 (and org-agenda-follow-mode
8392 (if org-agenda-follow-indirect
8393 (org-agenda-tree-to-indirect-buffer nil)
8394 (org-agenda-show)))
8395 (and org-agenda-show-outline-path
8396 (org-with-point-at m (org-display-outline-path t))))))
8398 (defun org-agenda-show-tags ()
8399 "Show the tags applicable to the current item."
8400 (interactive)
8401 (let* ((tags (org-get-at-bol 'tags)))
8402 (if tags
8403 (message "Tags are :%s:"
8404 (org-no-properties (mapconcat 'identity tags ":")))
8405 (message "No tags associated with this line"))))
8407 (defun org-agenda-goto (&optional highlight)
8408 "Go to the entry at point in the corresponding Org-mode file."
8409 (interactive)
8410 (let* ((marker (or (org-get-at-bol 'org-marker)
8411 (org-agenda-error)))
8412 (buffer (marker-buffer marker))
8413 (pos (marker-position marker)))
8414 (switch-to-buffer-other-window buffer)
8415 (widen)
8416 (push-mark)
8417 (goto-char pos)
8418 (when (derived-mode-p 'org-mode)
8419 (org-show-context 'agenda)
8420 (save-excursion
8421 (and (outline-next-heading)
8422 (org-flag-heading nil))) ; show the next heading
8423 (when (outline-invisible-p)
8424 (outline-show-entry)) ; display invisible text
8425 (recenter (/ (window-height) 2))
8426 (org-back-to-heading t)
8427 (if (re-search-forward org-complex-heading-regexp nil t)
8428 (goto-char (match-beginning 4))))
8429 (run-hooks 'org-agenda-after-show-hook)
8430 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8432 (defvar org-agenda-after-show-hook nil
8433 "Normal hook run after an item has been shown from the agenda.
8434 Point is in the buffer where the item originated.")
8436 (defun org-agenda-kill ()
8437 "Kill the entry or subtree belonging to the current agenda entry."
8438 (interactive)
8439 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8440 (let* ((bufname-orig (buffer-name))
8441 (marker (or (org-get-at-bol 'org-marker)
8442 (org-agenda-error)))
8443 (buffer (marker-buffer marker))
8444 (pos (marker-position marker))
8445 (type (org-get-at-bol 'type))
8446 dbeg dend (n 0) conf)
8447 (org-with-remote-undo buffer
8448 (with-current-buffer buffer
8449 (save-excursion
8450 (goto-char pos)
8451 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8452 (setq dbeg (progn (org-back-to-heading t) (point))
8453 dend (org-end-of-subtree t t))
8454 (setq dbeg (point-at-bol)
8455 dend (min (point-max) (1+ (point-at-eol)))))
8456 (goto-char dbeg)
8457 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8458 (setq conf (or (eq t org-agenda-confirm-kill)
8459 (and (numberp org-agenda-confirm-kill)
8460 (> n org-agenda-confirm-kill))))
8461 (and conf
8462 (not (y-or-n-p
8463 (format "Delete entry with %d lines in buffer \"%s\"? "
8464 n (buffer-name buffer))))
8465 (error "Abort"))
8466 (let ((org-agenda-buffer-name bufname-orig))
8467 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8468 (with-current-buffer buffer (delete-region dbeg dend))
8469 (message "Agenda item and source killed"))))
8471 (defvar org-archive-default-command) ; defined in org-archive.el
8472 (defun org-agenda-archive-default ()
8473 "Archive the entry or subtree belonging to the current agenda entry."
8474 (interactive)
8475 (require 'org-archive)
8476 (org-agenda-archive-with org-archive-default-command))
8478 (defun org-agenda-archive-default-with-confirmation ()
8479 "Archive the entry or subtree belonging to the current agenda entry."
8480 (interactive)
8481 (require 'org-archive)
8482 (org-agenda-archive-with org-archive-default-command 'confirm))
8484 (defun org-agenda-archive ()
8485 "Archive the entry or subtree belonging to the current agenda entry."
8486 (interactive)
8487 (org-agenda-archive-with 'org-archive-subtree))
8489 (defun org-agenda-archive-to-archive-sibling ()
8490 "Move the entry to the archive sibling."
8491 (interactive)
8492 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8494 (defun org-agenda-archive-with (cmd &optional confirm)
8495 "Move the entry to the archive sibling."
8496 (interactive)
8497 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8498 (let* ((bufname-orig (buffer-name))
8499 (marker (or (org-get-at-bol 'org-marker)
8500 (org-agenda-error)))
8501 (buffer (marker-buffer marker))
8502 (pos (marker-position marker)))
8503 (org-with-remote-undo buffer
8504 (with-current-buffer buffer
8505 (if (derived-mode-p 'org-mode)
8506 (if (and confirm
8507 (not (y-or-n-p "Archive this subtree or entry? ")))
8508 (error "Abort")
8509 (save-window-excursion
8510 (goto-char pos)
8511 (let ((org-agenda-buffer-name bufname-orig))
8512 (org-remove-subtree-entries-from-agenda))
8513 (org-back-to-heading t)
8514 (funcall cmd)))
8515 (error "Archiving works only in Org-mode files"))))))
8517 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8518 "Remove all lines in the agenda that correspond to a given subtree.
8519 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8520 If this information is not given, the function uses the tree at point."
8521 (let ((buf (or buf (current-buffer))) m p)
8522 (save-excursion
8523 (unless (and beg end)
8524 (org-back-to-heading t)
8525 (setq beg (point))
8526 (org-end-of-subtree t)
8527 (setq end (point)))
8528 (set-buffer (get-buffer org-agenda-buffer-name))
8529 (save-excursion
8530 (goto-char (point-max))
8531 (beginning-of-line 1)
8532 (while (not (bobp))
8533 (when (and (setq m (org-get-at-bol 'org-marker))
8534 (equal buf (marker-buffer m))
8535 (setq p (marker-position m))
8536 (>= p beg)
8537 (< p end))
8538 (let ((inhibit-read-only t))
8539 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8540 (beginning-of-line 0))))))
8542 (defun org-agenda-refile (&optional goto rfloc no-update)
8543 "Refile the item at point.
8545 When GOTO is 0 or \\='(64) or a triple \\[universal-argument] prefix argument,
8546 clear the refile cache.
8547 When GOTO is \\='(16) or a double \\[universal-argument] prefix argument,
8548 go to the location of the last refiled item.
8549 RFLOC can be a refile location obtained in a different way.
8550 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8551 (interactive "P")
8552 (cond
8553 ((member goto '(0 (64)))
8554 (org-refile-cache-clear))
8555 ((equal goto '(16))
8556 (org-refile-goto-last-stored))
8558 (let* ((buffer-orig (buffer-name))
8559 (marker (or (org-get-at-bol 'org-hd-marker)
8560 (org-agenda-error)))
8561 (buffer (marker-buffer marker))
8562 (pos (marker-position marker))
8563 (rfloc (or rfloc
8564 (org-refile-get-location
8565 (if goto "Goto" "Refile to") buffer
8566 org-refile-allow-creating-parent-nodes))))
8567 (with-current-buffer buffer
8568 (save-excursion
8569 (save-restriction
8570 (widen)
8571 (goto-char marker)
8572 (let ((org-agenda-buffer-name buffer-orig))
8573 (org-remove-subtree-entries-from-agenda))
8574 (org-refile goto buffer rfloc)))))
8575 (unless no-update (org-agenda-redo)))))
8577 (defun org-agenda-open-link (&optional arg)
8578 "Open the link(s) in the current entry, if any.
8579 This looks for a link in the displayed line in the agenda.
8580 It also looks at the text of the entry itself."
8581 (interactive "P")
8582 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8583 (org-get-at-bol 'org-marker)))
8584 (buffer (and marker (marker-buffer marker)))
8585 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8586 (lkall (and buffer (org-offer-links-in-entry
8587 buffer marker arg prefix)))
8588 (lk0 (car lkall))
8589 (lk (if (stringp lk0) (list lk0) lk0))
8590 (lkend (cdr lkall))
8591 trg)
8592 (cond
8593 ((and buffer lk)
8594 (mapcar (lambda(l)
8595 (with-current-buffer buffer
8596 (setq trg (and (string-match org-bracket-link-regexp l)
8597 (match-string 1 l)))
8598 (if (or (not trg) (string-match org-any-link-re trg))
8599 (save-excursion
8600 (save-restriction
8601 (widen)
8602 (goto-char marker)
8603 (when (search-forward l nil lkend)
8604 (goto-char (match-beginning 0))
8605 (org-open-at-point))))
8606 ;; This is an internal link, widen the buffer
8607 (switch-to-buffer-other-window buffer)
8608 (widen)
8609 (goto-char marker)
8610 (when (search-forward l nil lkend)
8611 (goto-char (match-beginning 0))
8612 (org-open-at-point)))))
8613 lk))
8614 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8615 (save-excursion
8616 (beginning-of-line 1)
8617 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8618 (org-open-link-from-string (match-string 1)))
8619 (t (message "No link to open here")))))
8621 (defun org-agenda-copy-local-variable (var)
8622 "Get a variable from a referenced buffer and install it here."
8623 (let ((m (org-get-at-bol 'org-marker)))
8624 (when (and m (buffer-live-p (marker-buffer m)))
8625 (org-set-local var (with-current-buffer (marker-buffer m)
8626 (symbol-value var))))))
8628 (defun org-agenda-switch-to (&optional delete-other-windows)
8629 "Go to the Org mode file which contains the item at point.
8630 When optional argument DELETE-OTHER-WINDOWS is non-nil, the
8631 displayed Org file fills the frame."
8632 (interactive)
8633 (if (and org-return-follows-link
8634 (not (org-get-at-bol 'org-marker))
8635 (org-in-regexp org-bracket-link-regexp))
8636 (org-open-link-from-string (match-string 0))
8637 (let* ((marker (or (org-get-at-bol 'org-marker)
8638 (org-agenda-error)))
8639 (buffer (marker-buffer marker))
8640 (pos (marker-position marker)))
8641 (unless buffer (user-error "Trying to switch to non-existent buffer"))
8642 (org-pop-to-buffer-same-window buffer)
8643 (when delete-other-windows (delete-other-windows))
8644 (widen)
8645 (goto-char pos)
8646 (when (derived-mode-p 'org-mode)
8647 (org-show-context 'agenda)
8648 (run-hooks 'org-agenda-after-show-hook)))))
8650 (defun org-agenda-goto-mouse (ev)
8651 "Go to the Org-mode file which contains the item at the mouse click."
8652 (interactive "e")
8653 (mouse-set-point ev)
8654 (org-agenda-goto))
8656 (defun org-agenda-show (&optional full-entry)
8657 "Display the Org-mode file which contains the item at point.
8658 With prefix argument FULL-ENTRY, make the entire entry visible
8659 if it was hidden in the outline."
8660 (interactive "P")
8661 (let ((win (selected-window)))
8662 (org-agenda-goto t)
8663 (when full-entry (org-show-entry))
8664 (select-window win)))
8666 (defvar org-agenda-show-window nil)
8667 (defun org-agenda-show-and-scroll-up (&optional arg)
8668 "Display the Org-mode file which contains the item at point.
8669 When called repeatedly, scroll the window that is displaying the buffer.
8670 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8671 `show-subtree' to display the item, so that drawers and logbooks stay
8672 folded."
8673 (interactive "P")
8674 (let ((win (selected-window)))
8675 (if (and (window-live-p org-agenda-show-window)
8676 (eq this-command last-command))
8677 (progn
8678 (select-window org-agenda-show-window)
8679 (ignore-errors (scroll-up)))
8680 (org-agenda-goto t)
8681 (if arg (org-show-entry) (outline-show-subtree))
8682 (setq org-agenda-show-window (selected-window)))
8683 (select-window win)))
8685 (defun org-agenda-show-scroll-down ()
8686 "Scroll down the window showing the agenda."
8687 (interactive)
8688 (let ((win (selected-window)))
8689 (when (window-live-p org-agenda-show-window)
8690 (select-window org-agenda-show-window)
8691 (ignore-errors (scroll-down))
8692 (select-window win))))
8694 (defun org-agenda-show-1 (&optional more)
8695 "Display the Org-mode file which contains the item at point.
8696 The prefix arg selects the amount of information to display:
8698 0 hide the subtree
8699 1 just show the entry according to defaults.
8700 2 show the children view
8701 3 show the subtree view
8702 4 show the entire subtree and any LOGBOOK drawers
8703 5 show the entire subtree and any drawers
8704 With prefix argument FULL-ENTRY, make the entire entry visible
8705 if it was hidden in the outline."
8706 (interactive "p")
8707 (let ((win (selected-window)))
8708 (org-agenda-goto t)
8709 (org-back-to-heading)
8710 (set-window-start (selected-window) (point-at-bol))
8711 (cond
8712 ((= more 0)
8713 (outline-hide-subtree)
8714 (save-excursion
8715 (org-back-to-heading)
8716 (run-hook-with-args 'org-cycle-hook 'folded))
8717 (message "Remote: FOLDED"))
8718 ((and (org-called-interactively-p 'any) (= more 1))
8719 (message "Remote: show with default settings"))
8720 ((= more 2)
8721 (outline-show-entry)
8722 (org-show-children)
8723 (save-excursion
8724 (org-back-to-heading)
8725 (run-hook-with-args 'org-cycle-hook 'children))
8726 (message "Remote: CHILDREN"))
8727 ((= more 3)
8728 (outline-show-subtree)
8729 (save-excursion
8730 (org-back-to-heading)
8731 (run-hook-with-args 'org-cycle-hook 'subtree))
8732 (message "Remote: SUBTREE"))
8733 ((= more 4)
8734 (outline-show-subtree)
8735 (save-excursion
8736 (org-back-to-heading)
8737 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8738 (message "Remote: SUBTREE AND LOGBOOK"))
8739 ((> more 4)
8740 (outline-show-subtree)
8741 (message "Remote: SUBTREE AND ALL DRAWERS")))
8742 (select-window win)))
8744 (defvar org-agenda-cycle-counter nil)
8745 (defun org-agenda-cycle-show (&optional n)
8746 "Show the current entry in another window, with default settings.
8748 Default settings are taken from `org-show-context-detail'. When
8749 use repeatedly in immediate succession, the remote entry will
8750 cycle through visibility
8752 children -> subtree -> folded
8754 When called with a numeric prefix arg, that arg will be passed through to
8755 `org-agenda-show-1'. For the interpretation of that argument, see the
8756 docstring of `org-agenda-show-1'."
8757 (interactive "P")
8758 (if (integerp n)
8759 (setq org-agenda-cycle-counter n)
8760 (if (not (eq last-command this-command))
8761 (setq org-agenda-cycle-counter 1)
8762 (if (equal org-agenda-cycle-counter 0)
8763 (setq org-agenda-cycle-counter 2)
8764 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8765 (if (> org-agenda-cycle-counter 3)
8766 (setq org-agenda-cycle-counter 0)))))
8767 (org-agenda-show-1 org-agenda-cycle-counter))
8769 (defun org-agenda-recenter (arg)
8770 "Display the Org-mode file which contains the item at point and recenter."
8771 (interactive "P")
8772 (let ((win (selected-window)))
8773 (org-agenda-goto t)
8774 (recenter arg)
8775 (select-window win)))
8777 (defun org-agenda-show-mouse (ev)
8778 "Display the Org-mode file which contains the item at the mouse click."
8779 (interactive "e")
8780 (mouse-set-point ev)
8781 (org-agenda-show))
8783 (defun org-agenda-check-no-diary ()
8784 "Check if the entry is a diary link and abort if yes."
8785 (if (org-get-at-bol 'org-agenda-diary-link)
8786 (org-agenda-error)))
8788 (defun org-agenda-error ()
8789 "Throw an error when a command is not allowed in the agenda."
8790 (user-error "Command not allowed in this line"))
8792 (defun org-agenda-tree-to-indirect-buffer (arg)
8793 "Show the subtree corresponding to the current entry in an indirect buffer.
8794 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8796 With a numerical prefix ARG, go up to this level and then take that tree.
8797 With a negative numeric ARG, go up by this number of levels.
8798 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8799 use the dedicated frame)."
8800 (interactive "P")
8801 (if current-prefix-arg
8802 (org-agenda-do-tree-to-indirect-buffer arg)
8803 (let ((agenda-buffer (buffer-name))
8804 (agenda-window (selected-window))
8805 (indirect-window
8806 (and org-last-indirect-buffer
8807 (get-buffer-window org-last-indirect-buffer))))
8808 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8809 (unless (or (eq org-indirect-buffer-display 'new-frame)
8810 (eq org-indirect-buffer-display 'dedicated-frame))
8811 (unwind-protect
8812 (unless (and indirect-window (window-live-p indirect-window))
8813 (setq indirect-window (split-window agenda-window)))
8814 (and indirect-window (select-window indirect-window))
8815 (switch-to-buffer org-last-indirect-buffer :norecord)
8816 (fit-window-to-buffer indirect-window)))
8817 (select-window (get-buffer-window agenda-buffer))
8818 (setq org-agenda-last-indirect-buffer org-last-indirect-buffer))))
8820 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8821 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8822 (org-agenda-check-no-diary)
8823 (let* ((marker (or (org-get-at-bol 'org-marker)
8824 (org-agenda-error)))
8825 (buffer (marker-buffer marker))
8826 (pos (marker-position marker)))
8827 (with-current-buffer buffer
8828 (save-excursion
8829 (goto-char pos)
8830 (funcall 'org-tree-to-indirect-buffer arg)))))
8832 (defvar org-last-heading-marker (make-marker)
8833 "Marker pointing to the headline that last changed its TODO state
8834 by a remote command from the agenda.")
8836 (defun org-agenda-todo-nextset ()
8837 "Switch TODO entry to next sequence."
8838 (interactive)
8839 (org-agenda-todo 'nextset))
8841 (defun org-agenda-todo-previousset ()
8842 "Switch TODO entry to previous sequence."
8843 (interactive)
8844 (org-agenda-todo 'previousset))
8846 (defun org-agenda-todo (&optional arg)
8847 "Cycle TODO state of line at point, also in Org-mode file.
8848 This changes the line at point, all other lines in the agenda referring to
8849 the same tree node, and the headline of the tree node in the Org-mode file."
8850 (interactive "P")
8851 (org-agenda-check-no-diary)
8852 (let* ((col (current-column))
8853 (marker (or (org-get-at-bol 'org-marker)
8854 (org-agenda-error)))
8855 (buffer (marker-buffer marker))
8856 (pos (marker-position marker))
8857 (hdmarker (org-get-at-bol 'org-hd-marker))
8858 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8859 (inhibit-read-only t)
8860 org-agenda-headline-snapshot-before-repeat newhead just-one)
8861 (org-with-remote-undo buffer
8862 (with-current-buffer buffer
8863 (widen)
8864 (goto-char pos)
8865 (org-show-context 'agenda)
8866 (save-excursion
8867 (and (outline-next-heading)
8868 (org-flag-heading nil))) ; show the next heading
8869 (let ((current-prefix-arg arg))
8870 (call-interactively 'org-todo))
8871 (and (bolp) (forward-char 1))
8872 (setq newhead (org-get-heading))
8873 (when (and (org-bound-and-true-p
8874 org-agenda-headline-snapshot-before-repeat)
8875 (not (equal org-agenda-headline-snapshot-before-repeat
8876 newhead))
8877 todayp)
8878 (setq newhead org-agenda-headline-snapshot-before-repeat
8879 just-one t))
8880 (save-excursion
8881 (org-back-to-heading)
8882 (move-marker org-last-heading-marker (point))))
8883 (beginning-of-line 1)
8884 (save-window-excursion
8885 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8886 (when (org-bound-and-true-p org-clock-out-when-done)
8887 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8888 newhead)
8889 (org-agenda-unmark-clocking-task))
8890 (org-move-to-column col)
8891 (org-agenda-mark-clocking-task))))
8893 (defun org-agenda-add-note (&optional arg)
8894 "Add a time-stamped note to the entry at point."
8895 (interactive "P")
8896 (org-agenda-check-no-diary)
8897 (let* ((marker (or (org-get-at-bol 'org-marker)
8898 (org-agenda-error)))
8899 (buffer (marker-buffer marker))
8900 (pos (marker-position marker))
8901 (hdmarker (org-get-at-bol 'org-hd-marker))
8902 (inhibit-read-only t))
8903 (with-current-buffer buffer
8904 (widen)
8905 (goto-char pos)
8906 (org-show-context 'agenda)
8907 (save-excursion
8908 (and (outline-next-heading)
8909 (org-flag-heading nil))) ; show the next heading
8910 (org-add-note))))
8912 (defun org-agenda-change-all-lines (newhead hdmarker
8913 &optional fixface just-this)
8914 "Change all lines in the agenda buffer which match HDMARKER.
8915 The new content of the line will be NEWHEAD (as modified by
8916 `org-agenda-format-item'). HDMARKER is checked with
8917 `equal' against all `org-hd-marker' text properties in the file.
8918 If FIXFACE is non-nil, the face of each item is modified according to
8919 the new TODO state.
8920 If JUST-THIS is non-nil, change just the current line, not all.
8921 If FORCE-TAGS is non nil, the car of it returns the new tags."
8922 (let* ((inhibit-read-only t)
8923 (line (org-current-line))
8924 (org-agenda-buffer (current-buffer))
8925 (thetags (with-current-buffer (marker-buffer hdmarker)
8926 (save-excursion (save-restriction (widen)
8927 (goto-char hdmarker)
8928 (org-get-tags-at)))))
8929 props m pl undone-face done-face finish new dotime level cat tags)
8930 (save-excursion
8931 (goto-char (point-max))
8932 (beginning-of-line 1)
8933 (while (not finish)
8934 (setq finish (bobp))
8935 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8936 (or (not just-this) (= (org-current-line) line))
8937 (equal m hdmarker))
8938 (setq props (text-properties-at (point))
8939 dotime (org-get-at-bol 'dotime)
8940 cat (org-get-at-eol 'org-category 1)
8941 level (org-get-at-bol 'level)
8942 tags thetags
8944 (let ((org-prefix-format-compiled
8945 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8946 org-prefix-format-compiled))
8947 (extra (org-get-at-bol 'extra)))
8948 (with-current-buffer (marker-buffer hdmarker)
8949 (save-excursion
8950 (save-restriction
8951 (widen)
8952 (org-agenda-format-item extra newhead level cat tags dotime)))))
8953 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8954 undone-face (org-get-at-bol 'undone-face)
8955 done-face (org-get-at-bol 'done-face))
8956 (beginning-of-line 1)
8957 (cond
8958 ((equal new "")
8959 (and (looking-at ".*\n?") (replace-match "")))
8960 ((looking-at ".*")
8961 (replace-match new t t)
8962 (beginning-of-line 1)
8963 (add-text-properties (point-at-bol) (point-at-eol) props)
8964 (when fixface
8965 (add-text-properties
8966 (point-at-bol) (point-at-eol)
8967 (list 'face
8968 (if org-last-todo-state-is-todo
8969 undone-face done-face))))
8970 (org-agenda-highlight-todo 'line)
8971 (beginning-of-line 1))
8972 (t (error "Line update did not work")))
8973 (save-restriction
8974 (narrow-to-region (point-at-bol) (point-at-eol))
8975 (org-agenda-finalize)))
8976 (beginning-of-line 0)))))
8978 (defun org-agenda-align-tags (&optional line)
8979 "Align all tags in agenda items to `org-agenda-tags-column'."
8980 (let ((inhibit-read-only t) l c)
8981 (save-excursion
8982 (goto-char (if line (point-at-bol) (point-min)))
8983 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8984 (if line (point-at-eol) nil) t)
8985 (add-text-properties
8986 (match-beginning 2) (match-end 2)
8987 (list 'face (delq nil (let ((prop (get-text-property
8988 (match-beginning 2) 'face)))
8989 (or (listp prop) (setq prop (list prop)))
8990 (if (memq 'org-tag prop)
8991 prop
8992 (cons 'org-tag prop))))))
8993 (setq l (- (match-end 2) (match-beginning 2))
8994 c (if (< org-agenda-tags-column 0)
8995 (- (abs org-agenda-tags-column) l)
8996 org-agenda-tags-column))
8997 (delete-region (match-beginning 1) (match-end 1))
8998 (goto-char (match-beginning 1))
8999 (insert (org-add-props
9000 (make-string (max 1 (- c (current-column))) ?\ )
9001 (plist-put (copy-sequence (text-properties-at (point)))
9002 'face nil))))
9003 (goto-char (point-min))
9004 (org-font-lock-add-tag-faces (point-max)))))
9006 (defun org-agenda-priority-up ()
9007 "Increase the priority of line at point, also in Org-mode file."
9008 (interactive)
9009 (org-agenda-priority 'up))
9011 (defun org-agenda-priority-down ()
9012 "Decrease the priority of line at point, also in Org-mode file."
9013 (interactive)
9014 (org-agenda-priority 'down))
9016 (defun org-agenda-priority (&optional force-direction)
9017 "Set the priority of line at point, also in Org-mode file.
9018 This changes the line at point, all other lines in the agenda referring to
9019 the same tree node, and the headline of the tree node in the Org-mode file.
9020 Called with a universal prefix arg, show the priority instead of setting it."
9021 (interactive "P")
9022 (if (equal force-direction '(4))
9023 (org-show-priority)
9024 (unless org-enable-priority-commands
9025 (error "Priority commands are disabled"))
9026 (org-agenda-check-no-diary)
9027 (let* ((col (current-column))
9028 (marker (or (org-get-at-bol 'org-marker)
9029 (org-agenda-error)))
9030 (hdmarker (org-get-at-bol 'org-hd-marker))
9031 (buffer (marker-buffer hdmarker))
9032 (pos (marker-position hdmarker))
9033 (inhibit-read-only t)
9034 newhead)
9035 (org-with-remote-undo buffer
9036 (with-current-buffer buffer
9037 (widen)
9038 (goto-char pos)
9039 (org-show-context 'agenda)
9040 (save-excursion
9041 (and (outline-next-heading)
9042 (org-flag-heading nil))) ; show the next heading
9043 (funcall 'org-priority force-direction)
9044 (end-of-line 1)
9045 (setq newhead (org-get-heading)))
9046 (org-agenda-change-all-lines newhead hdmarker)
9047 (org-move-to-column col)))))
9049 ;; FIXME: should fix the tags property of the agenda line.
9050 (defun org-agenda-set-tags (&optional tag onoff)
9051 "Set tags for the current headline."
9052 (interactive)
9053 (org-agenda-check-no-diary)
9054 (if (and (org-region-active-p) (org-called-interactively-p 'any))
9055 (call-interactively 'org-change-tag-in-region)
9056 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9057 (org-agenda-error)))
9058 (buffer (marker-buffer hdmarker))
9059 (pos (marker-position hdmarker))
9060 (inhibit-read-only t)
9061 newhead)
9062 (org-with-remote-undo buffer
9063 (with-current-buffer buffer
9064 (widen)
9065 (goto-char pos)
9066 (save-excursion
9067 (org-show-context 'agenda))
9068 (save-excursion
9069 (and (outline-next-heading)
9070 (org-flag-heading nil))) ; show the next heading
9071 (goto-char pos)
9072 (if tag
9073 (org-toggle-tag tag onoff)
9074 (call-interactively 'org-set-tags))
9075 (end-of-line 1)
9076 (setq newhead (org-get-heading)))
9077 (org-agenda-change-all-lines newhead hdmarker)
9078 (beginning-of-line 1)))))
9080 (defun org-agenda-set-property ()
9081 "Set a property for the current headline."
9082 (interactive)
9083 (org-agenda-check-no-diary)
9084 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9085 (org-agenda-error)))
9086 (buffer (marker-buffer hdmarker))
9087 (pos (marker-position hdmarker))
9088 (inhibit-read-only t)
9089 newhead)
9090 (org-with-remote-undo buffer
9091 (with-current-buffer buffer
9092 (widen)
9093 (goto-char pos)
9094 (save-excursion
9095 (org-show-context 'agenda))
9096 (save-excursion
9097 (and (outline-next-heading)
9098 (org-flag-heading nil))) ; show the next heading
9099 (goto-char pos)
9100 (call-interactively 'org-set-property)))))
9102 (defun org-agenda-set-effort ()
9103 "Set the effort property for the current headline."
9104 (interactive)
9105 (org-agenda-check-no-diary)
9106 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9107 (org-agenda-error)))
9108 (buffer (marker-buffer hdmarker))
9109 (pos (marker-position hdmarker))
9110 (inhibit-read-only t)
9111 newhead)
9112 (org-with-remote-undo buffer
9113 (with-current-buffer buffer
9114 (widen)
9115 (goto-char pos)
9116 (save-excursion
9117 (org-show-context 'agenda))
9118 (save-excursion
9119 (and (outline-next-heading)
9120 (org-flag-heading nil))) ; show the next heading
9121 (goto-char pos)
9122 (call-interactively 'org-set-effort)
9123 (end-of-line 1)
9124 (setq newhead (org-get-heading)))
9125 (org-agenda-change-all-lines newhead hdmarker))))
9127 (defun org-agenda-toggle-archive-tag ()
9128 "Toggle the archive tag for the current entry."
9129 (interactive)
9130 (org-agenda-check-no-diary)
9131 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9132 (org-agenda-error)))
9133 (buffer (marker-buffer hdmarker))
9134 (pos (marker-position hdmarker))
9135 (inhibit-read-only t)
9136 newhead)
9137 (org-with-remote-undo buffer
9138 (with-current-buffer buffer
9139 (widen)
9140 (goto-char pos)
9141 (org-show-context 'agenda)
9142 (save-excursion
9143 (and (outline-next-heading)
9144 (org-flag-heading nil))) ; show the next heading
9145 (call-interactively 'org-toggle-archive-tag)
9146 (end-of-line 1)
9147 (setq newhead (org-get-heading)))
9148 (org-agenda-change-all-lines newhead hdmarker)
9149 (beginning-of-line 1))))
9151 (defun org-agenda-do-date-later (arg)
9152 (interactive "P")
9153 (cond
9154 ((or (equal arg '(16))
9155 (memq last-command
9156 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9157 (setq this-command 'org-agenda-date-later-minutes)
9158 (org-agenda-date-later-minutes 1))
9159 ((or (equal arg '(4))
9160 (memq last-command
9161 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9162 (setq this-command 'org-agenda-date-later-hours)
9163 (org-agenda-date-later-hours 1))
9165 (org-agenda-date-later (prefix-numeric-value arg)))))
9167 (defun org-agenda-do-date-earlier (arg)
9168 (interactive "P")
9169 (cond
9170 ((or (equal arg '(16))
9171 (memq last-command
9172 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9173 (setq this-command 'org-agenda-date-earlier-minutes)
9174 (org-agenda-date-earlier-minutes 1))
9175 ((or (equal arg '(4))
9176 (memq last-command
9177 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9178 (setq this-command 'org-agenda-date-earlier-hours)
9179 (org-agenda-date-earlier-hours 1))
9181 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9183 (defun org-agenda-date-later (arg &optional what)
9184 "Change the date of this item to ARG day(s) later."
9185 (interactive "p")
9186 (org-agenda-check-type t 'agenda 'timeline)
9187 (org-agenda-check-no-diary)
9188 (let* ((marker (or (org-get-at-bol 'org-marker)
9189 (org-agenda-error)))
9190 (buffer (marker-buffer marker))
9191 (pos (marker-position marker))
9192 cdate today)
9193 (org-with-remote-undo buffer
9194 (with-current-buffer buffer
9195 (widen)
9196 (goto-char pos)
9197 (if (not (org-at-timestamp-p))
9198 (error "Cannot find time stamp"))
9199 (when (and org-agenda-move-date-from-past-immediately-to-today
9200 (equal arg 1)
9201 (or (not what) (eq what 'day))
9202 (not (save-match-data (org-at-date-range-p))))
9203 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9204 cdate (calendar-absolute-from-gregorian
9205 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9206 today (org-today))
9207 (if (> today cdate)
9208 ;; immediately shift to today
9209 (setq arg (- today cdate))))
9210 (org-timestamp-change arg (or what 'day))
9211 (when (and (org-at-date-range-p)
9212 (re-search-backward org-tr-regexp-both (point-at-bol)))
9213 (let ((end org-last-changed-timestamp))
9214 (org-timestamp-change arg (or what 'day))
9215 (setq org-last-changed-timestamp
9216 (concat org-last-changed-timestamp "--" end)))))
9217 (org-agenda-show-new-time marker org-last-changed-timestamp))
9218 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9220 (defun org-agenda-date-earlier (arg &optional what)
9221 "Change the date of this item to ARG day(s) earlier."
9222 (interactive "p")
9223 (org-agenda-date-later (- arg) what))
9225 (defun org-agenda-date-later-minutes (arg)
9226 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9227 (interactive "p")
9228 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9229 (org-agenda-date-later arg 'minute))
9231 (defun org-agenda-date-earlier-minutes (arg)
9232 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9233 (interactive "p")
9234 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9235 (org-agenda-date-earlier arg 'minute))
9237 (defun org-agenda-date-later-hours (arg)
9238 "Change the time of this item, in hour steps."
9239 (interactive "p")
9240 (org-agenda-date-later arg 'hour))
9242 (defun org-agenda-date-earlier-hours (arg)
9243 "Change the time of this item, in hour steps."
9244 (interactive "p")
9245 (org-agenda-date-earlier arg 'hour))
9247 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9248 "Show new date stamp via text properties."
9249 ;; We use text properties to make this undoable
9250 (let ((inhibit-read-only t))
9251 (setq stamp (concat prefix " => " stamp " "))
9252 (save-excursion
9253 (goto-char (point-max))
9254 (while (not (bobp))
9255 (when (equal marker (org-get-at-bol 'org-marker))
9256 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9257 (org-move-to-column (- (window-width) (length stamp)) t)
9258 (if (featurep 'xemacs)
9259 ;; Use `duplicable' property to trigger undo recording
9260 (let ((ex (make-extent nil nil))
9261 (gl (make-glyph stamp)))
9262 (set-glyph-face gl 'secondary-selection)
9263 (set-extent-properties
9264 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9265 (insert-extent ex (1- (point)) (point-at-eol)))
9266 (add-text-properties
9267 (1- (point)) (point-at-eol)
9268 (list 'display (org-add-props stamp nil
9269 'face '(secondary-selection default)))))
9270 (beginning-of-line 1))
9271 (beginning-of-line 0)))))
9273 (defun org-agenda-date-prompt (arg)
9274 "Change the date of this item. Date is prompted for, with default today.
9275 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9276 be used to request time specification in the time stamp."
9277 (interactive "P")
9278 (org-agenda-check-type t 'agenda 'timeline)
9279 (org-agenda-check-no-diary)
9280 (let* ((marker (or (org-get-at-bol 'org-marker)
9281 (org-agenda-error)))
9282 (buffer (marker-buffer marker))
9283 (pos (marker-position marker)))
9284 (org-with-remote-undo buffer
9285 (with-current-buffer buffer
9286 (widen)
9287 (goto-char pos)
9288 (if (not (org-at-timestamp-p t))
9289 (error "Cannot find time stamp"))
9290 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9291 (org-agenda-show-new-time marker org-last-changed-timestamp))
9292 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9294 (defun org-agenda-schedule (arg &optional time)
9295 "Schedule the item at point.
9296 ARG is passed through to `org-schedule'."
9297 (interactive "P")
9298 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9299 (org-agenda-check-no-diary)
9300 (let* ((marker (or (org-get-at-bol 'org-marker)
9301 (org-agenda-error)))
9302 (type (marker-insertion-type marker))
9303 (buffer (marker-buffer marker))
9304 (pos (marker-position marker))
9306 (set-marker-insertion-type marker t)
9307 (org-with-remote-undo buffer
9308 (with-current-buffer buffer
9309 (widen)
9310 (goto-char pos)
9311 (setq ts (org-schedule arg time)))
9312 (org-agenda-show-new-time marker ts " S"))
9313 (message "%s" ts)))
9315 (defun org-agenda-deadline (arg &optional time)
9316 "Schedule the item at point.
9317 ARG is passed through to `org-deadline'."
9318 (interactive "P")
9319 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9320 (org-agenda-check-no-diary)
9321 (let* ((marker (or (org-get-at-bol 'org-marker)
9322 (org-agenda-error)))
9323 (buffer (marker-buffer marker))
9324 (pos (marker-position marker))
9326 (org-with-remote-undo buffer
9327 (with-current-buffer buffer
9328 (widen)
9329 (goto-char pos)
9330 (setq ts (org-deadline arg time)))
9331 (org-agenda-show-new-time marker ts " D"))
9332 (message "%s" ts)))
9334 (defun org-agenda-clock-in (&optional arg)
9335 "Start the clock on the currently selected item."
9336 (interactive "P")
9337 (org-agenda-check-no-diary)
9338 (if (equal arg '(4))
9339 (org-clock-in arg)
9340 (let* ((marker (or (org-get-at-bol 'org-marker)
9341 (org-agenda-error)))
9342 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9343 (pos (marker-position marker))
9344 (col (current-column))
9345 newhead)
9346 (org-with-remote-undo (marker-buffer marker)
9347 (with-current-buffer (marker-buffer marker)
9348 (widen)
9349 (goto-char pos)
9350 (org-show-context 'agenda)
9351 (org-show-entry)
9352 (org-cycle-hide-drawers 'children)
9353 (org-clock-in arg)
9354 (setq newhead (org-get-heading)))
9355 (org-agenda-change-all-lines newhead hdmarker))
9356 (org-move-to-column col))))
9358 (defun org-agenda-clock-out ()
9359 "Stop the currently running clock."
9360 (interactive)
9361 (unless (marker-buffer org-clock-marker)
9362 (error "No running clock"))
9363 (let ((marker (make-marker)) (col (current-column)) newhead)
9364 (org-with-remote-undo (marker-buffer org-clock-marker)
9365 (with-current-buffer (marker-buffer org-clock-marker)
9366 (save-excursion
9367 (save-restriction
9368 (widen)
9369 (goto-char org-clock-marker)
9370 (org-back-to-heading t)
9371 (move-marker marker (point))
9372 (org-clock-out)
9373 (setq newhead (org-get-heading))))))
9374 (org-agenda-change-all-lines newhead marker)
9375 (move-marker marker nil)
9376 (org-move-to-column col)
9377 (org-agenda-unmark-clocking-task)))
9379 (defun org-agenda-clock-cancel (&optional arg)
9380 "Cancel the currently running clock."
9381 (interactive "P")
9382 (unless (marker-buffer org-clock-marker)
9383 (user-error "No running clock"))
9384 (org-with-remote-undo (marker-buffer org-clock-marker)
9385 (org-clock-cancel)))
9387 (defun org-agenda-clock-goto ()
9388 "Jump to the currently clocked in task within the agenda.
9389 If the currently clocked in task is not listed in the agenda
9390 buffer, display it in another window."
9391 (interactive)
9392 (let (pos)
9393 (mapc (lambda (o)
9394 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9395 (setq pos (overlay-start o))))
9396 (overlays-in (point-min) (point-max)))
9397 (cond (pos (goto-char pos))
9398 ;; If the currently clocked entry is not in the agenda
9399 ;; buffer, we visit it in another window:
9400 (org-clock-current-task
9401 (org-switch-to-buffer-other-window (org-clock-goto)))
9402 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9404 (defun org-agenda-diary-entry-in-org-file ()
9405 "Make a diary entry in the file `org-agenda-diary-file'."
9406 (let (d1 d2 char (text "") dp1 dp2)
9407 (if (equal (buffer-name) "*Calendar*")
9408 (setq d1 (calendar-cursor-to-date t)
9409 d2 (car calendar-mark-ring))
9410 (setq dp1 (get-text-property (point-at-bol) 'day))
9411 (unless dp1 (user-error "No date defined in current line"))
9412 (setq d1 (calendar-gregorian-from-absolute dp1)
9413 d2 (and (ignore-errors (mark))
9414 (save-excursion
9415 (goto-char (mark))
9416 (setq dp2 (get-text-property (point-at-bol) 'day)))
9417 (calendar-gregorian-from-absolute dp2))))
9418 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9419 (setq char (read-char-exclusive))
9420 (cond
9421 ((equal char ?d)
9422 (setq text (read-string "Day entry: "))
9423 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9424 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9425 ((equal char ?a)
9426 (setq d1 (list (car d1) (nth 1 d1)
9427 (read-number (format "Reference year [%d]: " (nth 2 d1))
9428 (nth 2 d1))))
9429 (setq text (read-string "Anniversary (use %d to show years): "))
9430 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9431 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9432 ((equal char ?b)
9433 (setq text (read-string "Block entry: "))
9434 (unless (and d1 d2 (not (equal d1 d2)))
9435 (user-error "No block of days selected"))
9436 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9437 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9438 ((equal char ?j)
9439 (org-switch-to-buffer-other-window
9440 (find-file-noselect org-agenda-diary-file))
9441 (require 'org-datetree)
9442 (org-datetree-find-date-create d1)
9443 (org-reveal t))
9444 (t (user-error "Invalid selection character `%c'" char)))))
9446 (defcustom org-agenda-insert-diary-strategy 'date-tree
9447 "Where in `org-agenda-diary-file' should new entries be added?
9448 Valid values:
9450 date-tree in the date tree, as first child of the date
9451 date-tree-last in the date tree, as last child of the date
9452 top-level as top-level entries at the end of the file."
9453 :group 'org-agenda
9454 :type '(choice
9455 (const :tag "first in a date tree" date-tree)
9456 (const :tag "last in a date tree" date-tree-last)
9457 (const :tag "as top level at end of file" top-level)))
9459 (defcustom org-agenda-insert-diary-extract-time nil
9460 "Non-nil means extract any time specification from the diary entry."
9461 :group 'org-agenda
9462 :version "24.1"
9463 :type 'boolean)
9465 (defcustom org-agenda-bulk-mark-char ">"
9466 "A single-character string to be used as the bulk mark."
9467 :group 'org-agenda
9468 :version "24.1"
9469 :type 'string)
9471 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9472 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9473 If TEXT is not empty, it will become the headline of the new entry, and
9474 the resulting entry will not be shown. When TEXT is empty, switch to
9475 `org-agenda-diary-file' and let the user finish the entry there."
9476 (let ((cw (current-window-configuration)))
9477 (org-switch-to-buffer-other-window
9478 (find-file-noselect org-agenda-diary-file))
9479 (widen)
9480 (goto-char (point-min))
9481 (cond
9482 ((eq type 'anniversary)
9483 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9484 (progn
9485 (or (org-at-heading-p t)
9486 (progn
9487 (outline-next-heading)
9488 (insert "* Anniversaries\n\n")
9489 (beginning-of-line -1)))))
9490 (outline-next-heading)
9491 (org-back-over-empty-lines)
9492 (backward-char 1)
9493 (insert "\n")
9494 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9495 (nth 2 d1) (car d1) (nth 1 d1) text)))
9496 ((eq type 'day)
9497 (let ((org-prefix-has-time t)
9498 (org-agenda-time-leading-zero t)
9499 fmt time time2)
9500 (if org-agenda-insert-diary-extract-time
9501 ;; Use org-agenda-format-item to parse text for a time-range and
9502 ;; remove it. FIXME: This is a hack, we should refactor
9503 ;; that function to make time extraction available separately
9504 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9505 time (get-text-property 0 'time fmt)
9506 time2 (if (> (length time) 0)
9507 ;; split-string removes trailing ...... if
9508 ;; no end time given. First space
9509 ;; separates time from date.
9510 (concat " " (car (split-string time "\\.")))
9511 nil)
9512 text (get-text-property 0 'txt fmt)))
9513 (if (eq org-agenda-insert-diary-strategy 'top-level)
9514 (org-agenda-insert-diary-as-top-level text)
9515 (require 'org-datetree)
9516 (org-datetree-find-date-create d1)
9517 (org-agenda-insert-diary-make-new-entry text))
9518 (org-insert-time-stamp (org-time-from-absolute
9519 (calendar-absolute-from-gregorian d1))
9520 nil nil nil nil time2))
9521 (end-of-line 0))
9522 ((eq type 'block)
9523 (if (> (calendar-absolute-from-gregorian d1)
9524 (calendar-absolute-from-gregorian d2))
9525 (setq d1 (prog1 d2 (setq d2 d1))))
9526 (if (eq org-agenda-insert-diary-strategy 'top-level)
9527 (org-agenda-insert-diary-as-top-level text)
9528 (require 'org-datetree)
9529 (org-datetree-find-date-create d1)
9530 (org-agenda-insert-diary-make-new-entry text))
9531 (org-insert-time-stamp (org-time-from-absolute
9532 (calendar-absolute-from-gregorian d1)))
9533 (insert "--")
9534 (org-insert-time-stamp (org-time-from-absolute
9535 (calendar-absolute-from-gregorian d2)))
9536 (end-of-line 0)))
9537 (if (string-match "\\S-" text)
9538 (progn
9539 (set-window-configuration cw)
9540 (message "%s entry added to %s"
9541 (capitalize (symbol-name type))
9542 (abbreviate-file-name org-agenda-diary-file)))
9543 (org-reveal t)
9544 (message "Please finish entry here"))))
9546 (defun org-agenda-insert-diary-as-top-level (text)
9547 "Make new entry as a top-level entry at the end of the file.
9548 Add TEXT as headline, and position the cursor in the second line so that
9549 a timestamp can be added there."
9550 (widen)
9551 (goto-char (point-max))
9552 (unless (bolp) (insert "\n"))
9553 (org-insert-heading nil t t)
9554 (insert text)
9555 (org-end-of-meta-data)
9556 (unless (bolp) (insert "\n"))
9557 (when org-adapt-indentation (org-indent-to-column 2)))
9559 (defun org-agenda-insert-diary-make-new-entry (text)
9560 "Make a new entry with TEXT as a child of the current subtree.
9561 Position the point in the heading's first body line so that
9562 a timestamp can be added there."
9563 (cond
9564 ((eq org-agenda-insert-diary-strategy 'date-tree-last)
9565 (end-of-line)
9566 (org-insert-heading '(4) t)
9567 (org-do-demote))
9569 (outline-next-heading)
9570 (org-back-over-empty-lines)
9571 (unless (looking-at "[ \t]*$") (save-excursion (insert "\n")))
9572 (org-insert-heading nil t)
9573 (org-do-demote)))
9574 (let ((col (current-column)))
9575 (insert text)
9576 (org-end-of-meta-data)
9577 ;; Ensure point is left on a blank line, at proper indentation.
9578 (unless (bolp) (insert "\n"))
9579 (unless (org-looking-at-p "^[ \t]*$") (save-excursion (insert "\n")))
9580 (when org-adapt-indentation (org-indent-to-column col)))
9581 (org-show-set-visibility 'lineage))
9583 (defun org-agenda-diary-entry ()
9584 "Make a diary entry, like the `i' command from the calendar.
9585 All the standard commands work: block, weekly etc.
9586 When `org-agenda-diary-file' points to a file,
9587 `org-agenda-diary-entry-in-org-file' is called instead to create
9588 entries in that Org-mode file."
9589 (interactive)
9590 (if (not (eq org-agenda-diary-file 'diary-file))
9591 (org-agenda-diary-entry-in-org-file)
9592 (require 'diary-lib)
9593 (let* ((char (progn
9594 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9595 (read-char-exclusive)))
9596 (cmd (cdr (assoc char
9597 '((?d . diary-insert-entry)
9598 (?w . diary-insert-weekly-entry)
9599 (?m . diary-insert-monthly-entry)
9600 (?y . diary-insert-yearly-entry)
9601 (?a . diary-insert-anniversary-entry)
9602 (?b . diary-insert-block-entry)
9603 (?c . diary-insert-cyclic-entry)))))
9604 (oldf (symbol-function 'calendar-cursor-to-date))
9605 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9606 (point (point))
9607 (mark (or (mark t) (point))))
9608 (unless cmd
9609 (user-error "No command associated with <%c>" char))
9610 (unless (and (get-text-property point 'day)
9611 (or (not (equal ?b char))
9612 (get-text-property mark 'day)))
9613 (user-error "Don't know which date to use for diary entry"))
9614 ;; We implement this by hacking the `calendar-cursor-to-date' function
9615 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9616 (let ((calendar-mark-ring
9617 (list (calendar-gregorian-from-absolute
9618 (or (get-text-property mark 'day)
9619 (get-text-property point 'day))))))
9620 (unwind-protect
9621 (progn
9622 (fset 'calendar-cursor-to-date
9623 (lambda (&optional error dummy)
9624 (calendar-gregorian-from-absolute
9625 (get-text-property point 'day))))
9626 (call-interactively cmd))
9627 (fset 'calendar-cursor-to-date oldf))))))
9629 (defun org-agenda-execute-calendar-command (cmd)
9630 "Execute a calendar command from the agenda with date from cursor."
9631 (org-agenda-check-type t 'agenda 'timeline)
9632 (require 'diary-lib)
9633 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9634 (user-error "Don't know which date to use for the calendar command"))
9635 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9636 (point (point))
9637 (date (calendar-gregorian-from-absolute
9638 (get-text-property point 'day)))
9639 ;; the following 2 vars are needed in the calendar
9640 (displayed-month (car date))
9641 (displayed-year (nth 2 date)))
9642 (unwind-protect
9643 (progn
9644 (fset 'calendar-cursor-to-date
9645 (lambda (&optional error dummy)
9646 (calendar-gregorian-from-absolute
9647 (get-text-property point 'day))))
9648 (call-interactively cmd))
9649 (fset 'calendar-cursor-to-date oldf))))
9651 (defun org-agenda-phases-of-moon ()
9652 "Display the phases of the moon for the 3 months around the cursor date."
9653 (interactive)
9654 (org-agenda-execute-calendar-command 'calendar-lunar-phases))
9656 (defun org-agenda-holidays ()
9657 "Display the holidays for the 3 months around the cursor date."
9658 (interactive)
9659 (org-agenda-execute-calendar-command 'calendar-list-holidays))
9661 (defvar calendar-longitude) ; defined in calendar.el
9662 (defvar calendar-latitude) ; defined in calendar.el
9663 (defvar calendar-location-name) ; defined in calendar.el
9665 (defun org-agenda-sunrise-sunset (arg)
9666 "Display sunrise and sunset for the cursor date.
9667 Latitude and longitude can be specified with the variables
9668 `calendar-latitude' and `calendar-longitude'. When called with prefix
9669 argument, latitude and longitude will be prompted for."
9670 (interactive "P")
9671 (require 'solar)
9672 (let ((calendar-longitude (if arg nil calendar-longitude))
9673 (calendar-latitude (if arg nil calendar-latitude))
9674 (calendar-location-name
9675 (if arg "the given coordinates" calendar-location-name)))
9676 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9678 (defun org-agenda-goto-calendar ()
9679 "Open the Emacs calendar with the date at the cursor."
9680 (interactive)
9681 (org-agenda-check-type t 'agenda 'timeline)
9682 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9683 (user-error "Don't know which date to open in calendar")))
9684 (date (calendar-gregorian-from-absolute day))
9685 (calendar-move-hook nil)
9686 (calendar-view-holidays-initially-flag nil)
9687 (calendar-view-diary-initially-flag nil))
9688 (calendar)
9689 (calendar-goto-date date)))
9691 ;;;###autoload
9692 (defun org-calendar-goto-agenda ()
9693 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9694 This is a command that has to be installed in `calendar-mode-map'."
9695 (interactive)
9696 ;; Temporarily disable sticky agenda since user clearly wants to
9697 ;; refresh view anyway.
9698 (let ((org-agenda-buffer-tmp-name "*Org Agenda(a)*")
9699 (org-agenda-sticky nil))
9700 (org-agenda-list nil (calendar-absolute-from-gregorian
9701 (calendar-cursor-to-date))
9702 nil)))
9704 (defun org-agenda-convert-date ()
9705 (interactive)
9706 (org-agenda-check-type t 'agenda 'timeline)
9707 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9708 date s)
9709 (unless day
9710 (user-error "Don't know which date to convert"))
9711 (setq date (calendar-gregorian-from-absolute day))
9712 (setq s (concat
9713 "Gregorian: " (calendar-date-string date) "\n"
9714 "ISO: " (calendar-iso-date-string date) "\n"
9715 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9716 "Julian: " (calendar-julian-date-string date) "\n"
9717 "Astron. JD: " (calendar-astro-date-string date)
9718 " (Julian date number at noon UTC)\n"
9719 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9720 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9721 "French: " (calendar-french-date-string date) "\n"
9722 "Bahá’í: " (calendar-bahai-date-string date) " (until sunset)\n"
9723 "Mayan: " (calendar-mayan-date-string date) "\n"
9724 "Coptic: " (calendar-coptic-date-string date) "\n"
9725 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9726 "Persian: " (calendar-persian-date-string date) "\n"
9727 "Chinese: " (calendar-chinese-date-string date) "\n"))
9728 (with-output-to-temp-buffer "*Dates*"
9729 (princ s))
9730 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9732 ;;; Bulk commands
9734 (defun org-agenda-bulk-marked-p ()
9735 (eq (get-char-property (point-at-bol) 'type)
9736 'org-marked-entry-overlay))
9738 (defun org-agenda-bulk-mark (&optional arg)
9739 "Mark the entry at point for future bulk action."
9740 (interactive "p")
9741 (dotimes (i (or arg 1))
9742 (unless (org-get-at-bol 'org-agenda-diary-link)
9743 (let* ((m (org-get-at-bol 'org-hd-marker))
9745 (unless (org-agenda-bulk-marked-p)
9746 (unless m (user-error "Nothing to mark at point"))
9747 (push m org-agenda-bulk-marked-entries)
9748 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9749 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9750 (org-get-todo-face "TODO")
9751 'evaporate)
9752 (overlay-put ov 'type 'org-marked-entry-overlay))
9753 (end-of-line 1)
9754 (or (ignore-errors
9755 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9756 (beginning-of-line 2))
9757 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9758 (beginning-of-line 2))
9759 (message "%d entries marked for bulk action"
9760 (length org-agenda-bulk-marked-entries))))))
9762 (defun org-agenda-bulk-mark-all ()
9763 "Mark all entries for future agenda bulk action."
9764 (interactive)
9765 (org-agenda-bulk-mark-regexp "."))
9767 (defun org-agenda-bulk-mark-regexp (regexp)
9768 "Mark entries matching REGEXP for future agenda bulk action."
9769 (interactive "sMark entries matching regexp: ")
9770 (let ((entries-marked 0) txt-at-point)
9771 (save-excursion
9772 (goto-char (point-min))
9773 (goto-char (next-single-property-change (point) 'org-hd-marker))
9774 (while (and (re-search-forward regexp nil t)
9775 (setq txt-at-point (get-text-property (point) 'txt)))
9776 (when (string-match regexp txt-at-point)
9777 (setq entries-marked (1+ entries-marked))
9778 (call-interactively 'org-agenda-bulk-mark))))
9779 (if (not entries-marked)
9780 (message "No entry matching this regexp."))))
9782 (defun org-agenda-bulk-unmark (&optional arg)
9783 "Unmark the entry at point for future bulk action."
9784 (interactive "P")
9785 (if arg
9786 (org-agenda-bulk-unmark-all)
9787 (cond ((org-agenda-bulk-marked-p)
9788 (org-agenda-bulk-remove-overlays
9789 (point-at-bol) (+ 2 (point-at-bol)))
9790 (setq org-agenda-bulk-marked-entries
9791 (delete (org-get-at-bol 'org-hd-marker)
9792 org-agenda-bulk-marked-entries))
9793 (end-of-line 1)
9794 (or (ignore-errors
9795 (goto-char (next-single-property-change (point) 'txt)))
9796 (beginning-of-line 2))
9797 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9798 (beginning-of-line 2))
9799 (message "%d entries left marked for bulk action"
9800 (length org-agenda-bulk-marked-entries)))
9801 (t (message "No entry to unmark here")))))
9803 (defun org-agenda-bulk-toggle-all ()
9804 "Toggle all marks for bulk action."
9805 (interactive)
9806 (save-excursion
9807 (goto-char (point-min))
9808 (while (ignore-errors
9809 (goto-char (next-single-property-change (point) 'org-hd-marker)))
9810 (org-agenda-bulk-toggle))))
9812 (defun org-agenda-bulk-toggle ()
9813 "Toggle the mark at point for bulk action."
9814 (interactive)
9815 (if (org-agenda-bulk-marked-p)
9816 (org-agenda-bulk-unmark)
9817 (org-agenda-bulk-mark)))
9819 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9820 "Remove the mark overlays between BEG and END in the agenda buffer.
9821 BEG and END default to the buffer limits.
9823 This only removes the overlays, it does not remove the markers
9824 from the list in `org-agenda-bulk-marked-entries'."
9825 (interactive)
9826 (mapc (lambda (ov)
9827 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9828 (delete-overlay ov)))
9829 (overlays-in (or beg (point-min)) (or end (point-max)))))
9831 (defun org-agenda-bulk-unmark-all ()
9832 "Remove all marks in the agenda buffer.
9833 This will remove the markers and the overlays."
9834 (interactive)
9835 (if (null org-agenda-bulk-marked-entries)
9836 (message "No entry to unmark")
9837 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9838 (setq org-agenda-bulk-marked-entries nil)
9839 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9841 (defcustom org-agenda-persistent-marks nil
9842 "Non-nil means marked items will stay marked after a bulk action.
9843 You can toggle this interactively by typing `p' when prompted for a
9844 bulk action."
9845 :group 'org-agenda
9846 :version "24.1"
9847 :type 'boolean)
9849 (defun org-agenda-bulk-action (&optional arg)
9850 "Execute an remote-editing action on all marked entries.
9851 The prefix arg is passed through to the command if possible."
9852 (interactive "P")
9853 ;; Make sure we have markers, and only valid ones
9854 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9855 (mapc
9856 (lambda (m)
9857 (unless (and (markerp m)
9858 (marker-buffer m)
9859 (buffer-live-p (marker-buffer m))
9860 (marker-position m))
9861 (user-error "Marker %s for bulk command is invalid" m)))
9862 org-agenda-bulk-marked-entries)
9864 ;; Prompt for the bulk command
9865 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9866 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9867 "[S]catter [f]unction "
9868 (when org-agenda-bulk-custom-functions
9869 (concat " Custom: ["
9870 (mapconcat (lambda(f) (char-to-string (car f)))
9871 org-agenda-bulk-custom-functions "")
9872 "]"))))
9873 (catch 'exit
9874 (let* ((action (read-char-exclusive))
9875 (org-log-refile (if org-log-refile 'time nil))
9876 (entries (reverse org-agenda-bulk-marked-entries))
9877 (org-overriding-default-time
9878 (if (get-text-property (point) 'org-agenda-date-header)
9879 (org-get-cursor-date)))
9880 redo-at-end
9881 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9882 (cond
9883 ((equal action ?p)
9884 (let ((org-agenda-persistent-marks
9885 (not org-agenda-persistent-marks)))
9886 (org-agenda-bulk-action)
9887 (throw 'exit nil)))
9889 ((equal action ?$)
9890 (setq cmd '(org-agenda-archive)))
9892 ((equal action ?A)
9893 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9895 ((member action '(?r ?w))
9896 (setq rfloc (org-refile-get-location
9897 "Refile to"
9898 (marker-buffer (car entries))
9899 org-refile-allow-creating-parent-nodes))
9900 (if (nth 3 rfloc)
9901 (setcar (nthcdr 3 rfloc)
9902 (move-marker (make-marker) (nth 3 rfloc)
9903 (or (get-file-buffer (nth 1 rfloc))
9904 (find-buffer-visiting (nth 1 rfloc))
9905 (error "This should not happen")))))
9907 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9908 redo-at-end t))
9910 ((equal action ?t)
9911 (setq state (org-icompleting-read
9912 "Todo state: "
9913 (with-current-buffer (marker-buffer (car entries))
9914 (mapcar 'list org-todo-keywords-1))))
9915 (setq cmd `(let ((org-inhibit-blocking t)
9916 (org-inhibit-logging 'note))
9917 (org-agenda-todo ,state))))
9919 ((memq action '(?- ?+))
9920 (setq tag (org-icompleting-read
9921 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9922 (with-current-buffer (marker-buffer (car entries))
9923 (delq nil
9924 (mapcar (lambda (x)
9925 (if (stringp (car x)) x)) org-tag-alist)))))
9926 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9928 ((memq action '(?s ?d))
9929 (let* ((time
9930 (unless arg
9931 (org-read-date
9932 nil nil nil
9933 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9934 org-overriding-default-time)))
9935 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9936 (setq cmd `(eval '(,c1 arg ,time)))))
9938 ((equal action ?S)
9939 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9940 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9941 (let ((days (read-number
9942 (format "Scatter tasks across how many %sdays: "
9943 (if arg "week" "")) 7)))
9944 (setq cmd
9945 `(let ((distance (1+ (random ,days))))
9946 (if arg
9947 (let ((dist distance)
9948 (day-of-week
9949 (calendar-day-of-week
9950 (calendar-gregorian-from-absolute (org-today)))))
9951 (dotimes (i (1+ dist))
9952 (while (member day-of-week org-agenda-weekend-days)
9953 (incf distance)
9954 (incf day-of-week)
9955 (if (= day-of-week 7)
9956 (setq day-of-week 0)))
9957 (incf day-of-week)
9958 (if (= day-of-week 7)
9959 (setq day-of-week 0)))))
9960 ;; silently fail when try to replan a sexp entry
9961 (condition-case nil
9962 (let* ((date (calendar-gregorian-from-absolute
9963 (+ (org-today) distance)))
9964 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9965 (nth 2 date))))
9966 (org-agenda-schedule nil time))
9967 (error nil)))))))
9969 ((assoc action org-agenda-bulk-custom-functions)
9970 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9971 redo-at-end t))
9973 ((equal action ?f)
9974 (setq cmd (list (intern
9975 (org-icompleting-read "Function: "
9976 obarray 'fboundp t nil nil)))))
9978 (t (user-error "Invalid bulk action")))
9980 ;; Sort the markers, to make sure that parents are handled before children
9981 (setq entries (sort entries
9982 (lambda (a b)
9983 (cond
9984 ((equal (marker-buffer a) (marker-buffer b))
9985 (< (marker-position a) (marker-position b)))
9987 (string< (buffer-name (marker-buffer a))
9988 (buffer-name (marker-buffer b))))))))
9990 ;; Now loop over all markers and apply cmd
9991 (while (setq e (pop entries))
9992 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9993 (if (not pos)
9994 (progn (message "Skipping removed entry at %s" e)
9995 (setq cntskip (1+ cntskip)))
9996 (goto-char pos)
9997 (let (org-loop-over-headlines-in-active-region)
9998 (eval cmd))
9999 ;; `post-command-hook' is not run yet. We make sure any
10000 ;; pending log note is processed.
10001 (when (or (memq 'org-add-log-note (default-value 'post-command-hook))
10002 (memq 'org-add-log-note post-command-hook))
10003 (org-add-log-note))
10004 (setq cnt (1+ cnt))))
10005 (when redo-at-end (org-agenda-redo))
10006 (unless org-agenda-persistent-marks
10007 (org-agenda-bulk-unmark-all))
10008 (message "Acted on %d entries%s%s"
10010 (if (= cntskip 0)
10012 (format ", skipped %d (disappeared before their turn)"
10013 cntskip))
10014 (if (not org-agenda-persistent-marks)
10015 "" " (kept marked)"))))))
10017 (defun org-agenda-capture (&optional with-time)
10018 "Call `org-capture' with the date at point.
10019 With a `C-1' prefix, use the HH:MM value at point (if any) or the
10020 current HH:MM time."
10021 (interactive "P")
10022 (if (not (eq major-mode 'org-agenda-mode))
10023 (user-error "You cannot do this outside of agenda buffers")
10024 (let ((org-overriding-default-time
10025 (org-get-cursor-date (equal with-time 1))))
10026 (call-interactively 'org-capture))))
10028 ;;; Dragging agenda lines forward/backward
10030 (defun org-agenda-reapply-filters ()
10031 "Re-apply all agenda filters."
10032 (mapcar
10033 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f) t)))
10034 `((,org-agenda-tag-filter tag)
10035 (,org-agenda-category-filter category)
10036 (,org-agenda-regexp-filter regexp)
10037 (,org-agenda-effort-filter effort)
10038 (,(get 'org-agenda-tag-filter :preset-filter) tag)
10039 (,(get 'org-agenda-category-filter :preset-filter) category)
10040 (,(get 'org-agenda-effort-filter :preset-filter) effort)
10041 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
10043 (defun org-agenda-drag-line-forward (arg &optional backward)
10044 "Drag an agenda line forward by ARG lines.
10045 When the optional argument `backward' is non-nil, move backward."
10046 (interactive "p")
10047 (let ((inhibit-read-only t) lst line)
10048 (if (or (not (get-text-property (point) 'txt))
10049 (save-excursion
10050 (dotimes (n arg)
10051 (move-beginning-of-line (if backward 0 2))
10052 (push (not (get-text-property (point) 'txt)) lst))
10053 (delq nil lst)))
10054 (message "Cannot move line forward")
10055 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
10056 (move-beginning-of-line 1)
10057 (setq line (buffer-substring (point) end))
10058 (delete-region (point) end)
10059 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
10060 (insert line)
10061 (org-agenda-reapply-filters)
10062 (org-agenda-mark-clocking-task)
10063 (move-beginning-of-line 0)))))
10065 (defun org-agenda-drag-line-backward (arg)
10066 "Drag an agenda line backward by ARG lines."
10067 (interactive "p")
10068 (org-agenda-drag-line-forward arg t))
10070 ;;; Flagging notes
10072 (defun org-agenda-show-the-flagging-note ()
10073 "Display the flagging note in the other window.
10074 When called a second time in direct sequence, offer to remove the FLAGGING
10075 tag and (if present) the flagging note."
10076 (interactive)
10077 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
10078 (win (selected-window))
10079 note heading newhead)
10080 (unless hdmarker
10081 (user-error "No linked entry at point"))
10082 (if (and (eq this-command last-command)
10083 (y-or-n-p "Unflag and remove any flagging note? "))
10084 (progn
10085 (org-agenda-remove-flag hdmarker)
10086 (let ((win (get-buffer-window "*Flagging Note*")))
10087 (and win (delete-window win)))
10088 (message "Entry unflagged"))
10089 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10090 (unless note
10091 (user-error "No flagging note"))
10092 (org-kill-new note)
10093 (org-switch-to-buffer-other-window "*Flagging Note*")
10094 (erase-buffer)
10095 (insert note)
10096 (goto-char (point-min))
10097 (while (re-search-forward "\\\\n" nil t)
10098 (replace-match "\n" t t))
10099 (goto-char (point-min))
10100 (select-window win)
10101 (message (substitute-command-keys "Flagging note pushed to kill ring. \
10102 Press \\[org-agenda-show-the-flagging-note] again to remove tag and note")))))
10104 (defun org-agenda-remove-flag (marker)
10105 "Remove the FLAGGED tag and any flagging note in the entry."
10106 (let (newhead)
10107 (org-with-point-at marker
10108 (org-toggle-tag "FLAGGED" 'off)
10109 (org-entry-delete nil "THEFLAGGINGNOTE")
10110 (setq newhead (org-get-heading)))
10111 (org-agenda-change-all-lines newhead marker)
10112 (message "Entry unflagged")))
10114 (defun org-agenda-get-any-marker (&optional pos)
10115 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10116 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10118 ;;; Appointment reminders
10120 (defvar appt-time-msg-list) ; defined in appt.el
10122 ;;;###autoload
10123 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10124 "Activate appointments found in `org-agenda-files'.
10125 With a \\[universal-argument] prefix, refresh the list of
10126 appointments.
10128 If FILTER is t, interactively prompt the user for a regular
10129 expression, and filter out entries that don't match it.
10131 If FILTER is a string, use this string as a regular expression
10132 for filtering entries out.
10134 If FILTER is a function, filter out entries against which
10135 calling the function returns nil. This function takes one
10136 argument: an entry from `org-agenda-get-day-entries'.
10138 FILTER can also be an alist with the car of each cell being
10139 either `headline' or `category'. For example:
10141 \\='((headline \"IMPORTANT\")
10142 (category \"Work\"))
10144 will only add headlines containing IMPORTANT or headlines
10145 belonging to the \"Work\" category.
10147 ARGS are symbols indicating what kind of entries to consider.
10148 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10149 \(i.e., deadlines and scheduled items with a hh:mm specification)
10150 and :timestamp entries. See the docstring of `org-diary' for
10151 details and examples.
10153 If an entry has a APPT_WARNTIME property, its value will be used
10154 to override `appt-message-warning-time'."
10155 (interactive "P")
10156 (if refresh (setq appt-time-msg-list nil))
10157 (if (eq filter t)
10158 (setq filter (read-from-minibuffer "Regexp filter: ")))
10159 (let* ((cnt 0) ; count added events
10160 (scope (or args '(:deadline* :scheduled* :timestamp)))
10161 (org-agenda-new-buffers nil)
10162 (org-deadline-warning-days 0)
10163 ;; Do not use `org-today' here because appt only takes
10164 ;; time and without date as argument, so it may pass wrong
10165 ;; information otherwise
10166 (today (org-date-to-gregorian
10167 (time-to-days (current-time))))
10168 (org-agenda-restrict nil)
10169 (files (org-agenda-files 'unrestricted)) entries file
10170 (org-agenda-buffer nil))
10171 ;; Get all entries which may contain an appt
10172 (org-agenda-prepare-buffers files)
10173 (while (setq file (pop files))
10174 (setq entries
10175 (delq nil
10176 (append entries
10177 (apply 'org-agenda-get-day-entries
10178 file today scope)))))
10179 ;; Map thru entries and find if we should filter them out
10180 (mapc
10181 (lambda(x)
10182 (let* ((evt (org-trim
10183 (replace-regexp-in-string
10184 org-bracket-link-regexp "\\3"
10185 (or (get-text-property 1 'txt x) ""))))
10186 (cat (get-text-property (1- (length x)) 'org-category x))
10187 (tod (get-text-property 1 'time-of-day x))
10188 (ok (or (null filter)
10189 (and (stringp filter) (string-match filter evt))
10190 (and (functionp filter) (funcall filter x))
10191 (and (listp filter)
10192 (let ((cat-filter (cadr (assoc 'category filter)))
10193 (evt-filter (cadr (assoc 'headline filter))))
10194 (or (and (stringp cat-filter)
10195 (string-match cat-filter cat))
10196 (and (stringp evt-filter)
10197 (string-match evt-filter evt)))))))
10198 (wrn (get-text-property 1 'warntime x)))
10199 ;; FIXME: Shall we remove text-properties for the appt text?
10200 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10201 (when (and ok tod)
10202 (setq tod (concat "00" (number-to-string tod))
10203 tod (when (string-match
10204 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10205 (concat (match-string 1 tod) ":"
10206 (match-string 2 tod))))
10207 (if (version< emacs-version "23.3")
10208 (appt-add tod evt)
10209 (appt-add tod evt wrn))
10210 (setq cnt (1+ cnt))))) entries)
10211 (org-release-buffers org-agenda-new-buffers)
10212 (if (eq cnt 0)
10213 (message "No event to add")
10214 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10216 (defun org-agenda-todayp (date)
10217 "Does DATE mean today, when considering `org-extend-today-until'?"
10218 (let ((today (org-today))
10219 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10220 date)))
10221 (eq date today)))
10223 (defun org-agenda-todo-yesterday (&optional arg)
10224 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10225 (interactive "P")
10226 (let* ((org-use-effective-time t)
10227 (hour (third (decode-time
10228 (org-current-time))))
10229 (org-extend-today-until (1+ hour)))
10230 (org-agenda-todo arg)))
10232 (provide 'org-agenda)
10234 ;;; org-agenda.el ends here