org.el (org-overview): Don't call `recenter'
[org-mode/org-kjn.git] / lisp / org-agenda.el
blob2833fcafe928d9e3bb84246f298d70c43a7d1f33
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
86 (declare-function org-capture "org-capture" (&optional goto keys))
88 (defvar calendar-mode-map) ; defined in calendar.el
89 (defvar org-clock-current-task nil) ; defined in org-clock.el
90 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91 (defvar org-habit-show-habits) ; defined in org-habit.el
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
95 ;; Defined somewhere in this file, but used before definition.
96 (defvar org-agenda-buffer-name "*Org Agenda*")
97 (defvar org-agenda-overriding-header nil)
98 (defvar org-agenda-title-append nil)
99 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
101 (defvar original-date) ; dynamically scoped, calendar.el does scope this
103 (defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105 (defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
108 (defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110 When t, a confirmation is always needed. When a number N, confirmation is
111 only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
116 (integer :tag "When more than N lines")))
118 (defcustom org-agenda-compact-blocks nil
119 "Non-nil means make the block agenda more compact.
120 This is done globally by leaving out lines like the agenda span
121 name and week number or the separator lines."
122 :group 'org-agenda
123 :type 'boolean)
125 (defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127 If this is a string, it will be used as the separator, with a newline added.
128 If it is a character, it will be repeated to fill the window width.
129 If nil the separator is disabled. In `org-agenda-custom-commands' this
130 addresses the separator between the current and the previous block."
131 :group 'org-agenda
132 :type '(choice
133 (const :tag "Disabled" nil)
134 (character)
135 (string)))
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
147 (defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
149 This is a good place to set options for ps-print and for htmlize.
150 Note that the way this is implemented, the values will be evaluated
151 before assigned to the variables. So make sure to quote values you do
152 *not* want evaluated, for example
154 (setq org-agenda-exporter-settings
155 '((ps-print-color-p 'black-white)))"
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
162 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164 A useful function for this hook is `org-agenda-add-entry-text'."
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
169 (defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171 This is only relevant when `org-agenda-add-entry-text' is part of
172 `org-agenda-before-write-hook', which is the default.
173 When this is 0, nothing will happen. When it is greater than 0, it
174 specifies the maximum number of lines that will be added for each entry
175 that is listed in the agenda view.
177 Note that this variable is not used during display, only when exporting
178 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179 and `org-agenda-entry-text-maxlines'."
180 :group 'org-agenda
181 :type 'integer)
183 (defcustom org-agenda-add-entry-text-descriptive-links t
184 "Non-nil means export org-links as descriptive links in agenda added text.
185 This variable applies to the text added to the agenda when
186 `org-agenda-add-entry-text-maxlines' is larger than 0.
187 When this variable nil, the URL will (also) be shown."
188 :group 'org-agenda
189 :type 'boolean)
191 (defcustom org-agenda-export-html-style nil
192 "The style specification for exported HTML Agenda files.
193 If this variable contains a string, it will replace the default <style>
194 section as produced by `htmlize'.
195 Since there are different ways of setting style information, this variable
196 needs to contain the full HTML structure to provide a style, including the
197 surrounding HTML tags. The style specifications should include definitions
198 the fonts used by the agenda, here is an example:
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
211 .org-agenda-done {
212 color: #339933;
214 .org-done {
215 color: #339933;
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
222 or, if you want to keep the style in a file,
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
226 As the value of this option simply gets inserted into the HTML <head> header,
227 you can \"misuse\" it to also add other text to the header."
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type '(choice
231 (const nil)
232 (string)))
234 (defcustom org-agenda-persistent-filter nil
235 "When set, keep filters from one agenda view to the next."
236 :group 'org-agenda
237 :type 'boolean)
239 (defgroup org-agenda-custom-commands nil
240 "Options concerning agenda views in Org-mode."
241 :tag "Org Agenda Custom Commands"
242 :group 'org-agenda)
244 (defconst org-sorting-choice
245 '(choice
246 (const time-up) (const time-down)
247 (const timestamp-up) (const timestamp-down)
248 (const scheduled-up) (const scheduled-down)
249 (const deadline-up) (const deadline-down)
250 (const ts-up) (const ts-down)
251 (const tsia-up) (const tsia-down)
252 (const category-keep) (const category-up) (const category-down)
253 (const tag-down) (const tag-up)
254 (const priority-up) (const priority-down)
255 (const todo-state-up) (const todo-state-down)
256 (const effort-up) (const effort-down)
257 (const habit-up) (const habit-down)
258 (const alpha-up) (const alpha-down)
259 (const user-defined-up) (const user-defined-down))
260 "Sorting choices.")
262 ;; Keep custom values for `org-agenda-filter-preset' compatible with
263 ;; the new variable `org-agenda-tag-filter-preset'.
264 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
267 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
268 "List of types searched for when creating the daily/weekly agenda.
269 This variable is a list of symbols that controls the types of
270 items that appear in the daily/weekly agenda. Allowed symbols in this
271 list are are
273 :timestamp List items containing a date stamp or date range matching
274 the selected date. This includes sexp entries in angular
275 brackets.
277 :sexp List entries resulting from plain diary-like sexps.
279 :deadline List deadline due on that date. When the date is today,
280 also list any deadlines past due, or due within
281 `org-deadline-warning-days'. `:deadline' must appear before
282 `:scheduled' if the setting of
283 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
284 any effect.
286 :deadline* Same as above, but only include the deadline if it has an
287 hour specification as [h]h:mm.
289 :scheduled List all items which are scheduled for the given date.
290 The diary for *today* also contains items which were
291 scheduled earlier and are not yet marked DONE.
293 :scheduled* Same as above, but only include the scheduled item if it
294 has an hour specification as [h]h:mm.
296 By default, all four non-starred types are turned on.
298 When :scheduled* or :deadline* are included, :schedule or :deadline
299 will be ignored.
301 Never set this variable globally using `setq', because then it
302 will apply to all future agenda commands. Instead, bind it with
303 `let' to scope it dynamically into the agenda-constructing
304 command. A good way to set it is through options in
305 `org-agenda-custom-commands'. For a more flexible (though
306 somewhat less efficient) way of determining what is included in
307 the daily/weekly agenda, see `org-agenda-skip-function'.")
309 (defconst org-agenda-custom-commands-local-options
310 `(repeat :tag "Local settings for this command. Remember to quote values"
311 (choice :tag "Setting"
312 (list :tag "Heading for this block"
313 (const org-agenda-overriding-header)
314 (string :tag "Headline"))
315 (list :tag "Files to be searched"
316 (const org-agenda-files)
317 (list
318 (const :format "" quote)
319 (repeat (file))))
320 (list :tag "Sorting strategy"
321 (const org-agenda-sorting-strategy)
322 (list
323 (const :format "" quote)
324 (repeat
325 ,org-sorting-choice)))
326 (list :tag "Prefix format"
327 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
328 (string))
329 (list :tag "Number of days in agenda"
330 (const org-agenda-span)
331 (choice (const :tag "Day" day)
332 (const :tag "Week" week)
333 (const :tag "Fortnight" fortnight)
334 (const :tag "Month" month)
335 (const :tag "Year" year)
336 (integer :tag "Custom")))
337 (list :tag "Fixed starting date"
338 (const org-agenda-start-day)
339 (string :value "2007-11-01"))
340 (list :tag "Start on day of week"
341 (const org-agenda-start-on-weekday)
342 (choice :value 1
343 (const :tag "Today" nil)
344 (integer :tag "Weekday No.")))
345 (list :tag "Include data from diary"
346 (const org-agenda-include-diary)
347 (boolean))
348 (list :tag "Deadline Warning days"
349 (const org-deadline-warning-days)
350 (integer :value 1))
351 (list :tag "Category filter preset"
352 (const org-agenda-category-filter-preset)
353 (list
354 (const :format "" quote)
355 (repeat
356 (string :tag "+category or -category"))))
357 (list :tag "Tags filter preset"
358 (const org-agenda-tag-filter-preset)
359 (list
360 (const :format "" quote)
361 (repeat
362 (string :tag "+tag or -tag"))))
363 (list :tag "Regexp filter preset"
364 (const org-agenda-regexp-filter-preset)
365 (list
366 (const :format "" quote)
367 (repeat
368 (string :tag "+regexp or -regexp"))))
369 (list :tag "Set daily/weekly entry types"
370 (const org-agenda-entry-types)
371 (list
372 (const :format "" quote)
373 (set :greedy t :value ,org-agenda-entry-types
374 (const :deadline)
375 (const :scheduled)
376 (const :deadline*)
377 (const :scheduled*)
378 (const :timestamp)
379 (const :sexp))))
380 (list :tag "Standard skipping condition"
381 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
382 (const org-agenda-skip-function)
383 (list
384 (const :format "" quote)
385 (list
386 (choice
387 :tag "Skipping range"
388 (const :tag "Skip entry" org-agenda-skip-entry-if)
389 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
390 (repeat :inline t :tag "Conditions for skipping"
391 (choice
392 :tag "Condition type"
393 (list :tag "Regexp matches" :inline t
394 (const :format "" 'regexp)
395 (regexp))
396 (list :tag "Regexp does not match" :inline t
397 (const :format "" 'notregexp)
398 (regexp))
399 (list :tag "TODO state is" :inline t
400 (const 'todo)
401 (choice
402 (const :tag "Any not-done state" 'todo)
403 (const :tag "Any done state" 'done)
404 (const :tag "Any state" 'any)
405 (list :tag "Keyword list"
406 (const :format "" quote)
407 (repeat (string :tag "Keyword")))))
408 (list :tag "TODO state is not" :inline t
409 (const 'nottodo)
410 (choice
411 (const :tag "Any not-done state" 'todo)
412 (const :tag "Any done state" 'done)
413 (const :tag "Any state" 'any)
414 (list :tag "Keyword list"
415 (const :format "" quote)
416 (repeat (string :tag "Keyword")))))
417 (const :tag "scheduled" 'scheduled)
418 (const :tag "not scheduled" 'notscheduled)
419 (const :tag "deadline" 'deadline)
420 (const :tag "no deadline" 'notdeadline)
421 (const :tag "timestamp" 'timestamp)
422 (const :tag "no timestamp" 'nottimestamp))))))
423 (list :tag "Non-standard skipping condition"
424 :value (org-agenda-skip-function)
425 (const org-agenda-skip-function)
426 (sexp :tag "Function or form (quoted!)"))
427 (list :tag "Any variable"
428 (variable :tag "Variable")
429 (sexp :tag "Value (sexp)"))))
430 "Selection of examples for agenda command settings.
431 This will be spliced into the custom type of
432 `org-agenda-custom-commands'.")
435 (defcustom org-agenda-custom-commands
436 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
437 "Custom commands for the agenda.
438 These commands will be offered on the splash screen displayed by the
439 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
441 (key desc type match settings files)
443 key The key (one or more characters as a string) to be associated
444 with the command.
445 desc A description of the command, when omitted or nil, a default
446 description is built using MATCH.
447 type The command type, any of the following symbols:
448 agenda The daily/weekly agenda.
449 todo Entries with a specific TODO keyword, in all agenda files.
450 search Entries containing search words entry or headline.
451 tags Tags/Property/TODO match in all agenda files.
452 tags-todo Tags/P/T match in all agenda files, TODO entries only.
453 todo-tree Sparse tree of specific TODO keyword in *current* file.
454 tags-tree Sparse tree with all tags matches in *current* file.
455 occur-tree Occur sparse tree for *current* file.
456 ... A user-defined function.
457 match What to search for:
458 - a single keyword for TODO keyword searches
459 - a tags match expression for tags searches
460 - a word search expression for text searches.
461 - a regular expression for occur searches
462 For all other commands, this should be the empty string.
463 settings A list of option settings, similar to that in a let form, so like
464 this: ((opt1 val1) (opt2 val2) ...). The values will be
465 evaluated at the moment of execution, so quote them when needed.
466 files A list of files file to write the produced agenda buffer to
467 with the command `org-store-agenda-views'.
468 If a file name ends in \".html\", an HTML version of the buffer
469 is written out. If it ends in \".ps\", a postscript version is
470 produced. Otherwise, only the plain text is written to the file.
472 You can also define a set of commands, to create a composite agenda buffer.
473 In this case, an entry looks like this:
475 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
477 where
479 desc A description string to be displayed in the dispatcher menu.
480 cmd An agenda command, similar to the above. However, tree commands
481 are not allowed, but instead you can get agenda and global todo list.
482 So valid commands for a set are:
483 (agenda \"\" settings)
484 (alltodo \"\" settings)
485 (stuck \"\" settings)
486 (todo \"match\" settings files)
487 (search \"match\" settings files)
488 (tags \"match\" settings files)
489 (tags-todo \"match\" settings files)
491 Each command can carry a list of options, and another set of options can be
492 given for the whole set of commands. Individual command options take
493 precedence over the general options.
495 When using several characters as key to a command, the first characters
496 are prefix commands. For the dispatcher to display useful information, you
497 should provide a description for the prefix, like
499 (setq org-agenda-custom-commands
500 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
501 (\"hl\" tags \"+HOME+Lisa\")
502 (\"hp\" tags \"+HOME+Peter\")
503 (\"hk\" tags \"+HOME+Kim\")))"
504 :group 'org-agenda-custom-commands
505 :type `(repeat
506 (choice :value ("x" "Describe command here" tags "" nil)
507 (list :tag "Single command"
508 (string :tag "Access Key(s) ")
509 (option (string :tag "Description"))
510 (choice
511 (const :tag "Agenda" agenda)
512 (const :tag "TODO list" alltodo)
513 (const :tag "Search words" search)
514 (const :tag "Stuck projects" stuck)
515 (const :tag "Tags/Property match (all agenda files)" tags)
516 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
517 (const :tag "TODO keyword search (all agenda files)" todo)
518 (const :tag "Tags sparse tree (current buffer)" tags-tree)
519 (const :tag "TODO keyword tree (current buffer)" todo-tree)
520 (const :tag "Occur tree (current buffer)" occur-tree)
521 (sexp :tag "Other, user-defined function"))
522 (string :tag "Match (only for some commands)")
523 ,org-agenda-custom-commands-local-options
524 (option (repeat :tag "Export" (file :tag "Export to"))))
525 (list :tag "Command series, all agenda files"
526 (string :tag "Access Key(s)")
527 (string :tag "Description ")
528 (repeat :tag "Component"
529 (choice
530 (list :tag "Agenda"
531 (const :format "" agenda)
532 (const :tag "" :format "" "")
533 ,org-agenda-custom-commands-local-options)
534 (list :tag "TODO list (all keywords)"
535 (const :format "" alltodo)
536 (const :tag "" :format "" "")
537 ,org-agenda-custom-commands-local-options)
538 (list :tag "Search words"
539 (const :format "" search)
540 (string :tag "Match")
541 ,org-agenda-custom-commands-local-options)
542 (list :tag "Stuck projects"
543 (const :format "" stuck)
544 (const :tag "" :format "" "")
545 ,org-agenda-custom-commands-local-options)
546 (list :tag "Tags search"
547 (const :format "" tags)
548 (string :tag "Match")
549 ,org-agenda-custom-commands-local-options)
550 (list :tag "Tags search, TODO entries only"
551 (const :format "" tags-todo)
552 (string :tag "Match")
553 ,org-agenda-custom-commands-local-options)
554 (list :tag "TODO keyword search"
555 (const :format "" todo)
556 (string :tag "Match")
557 ,org-agenda-custom-commands-local-options)
558 (list :tag "Other, user-defined function"
559 (symbol :tag "function")
560 (string :tag "Match")
561 ,org-agenda-custom-commands-local-options)))
563 (repeat :tag "Settings for entire command set"
564 (list (variable :tag "Any variable")
565 (sexp :tag "Value")))
566 (option (repeat :tag "Export" (file :tag "Export to"))))
567 (cons :tag "Prefix key documentation"
568 (string :tag "Access Key(s)")
569 (string :tag "Description ")))))
571 (defcustom org-agenda-query-register ?o
572 "The register holding the current query string.
573 The purpose of this is that if you construct a query string interactively,
574 you can then use it to define a custom command."
575 :group 'org-agenda-custom-commands
576 :type 'character)
578 (defcustom org-stuck-projects
579 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
580 "How to identify stuck projects.
581 This is a list of four items:
582 1. A tags/todo/property matcher string that is used to identify a project.
583 See the manual for a description of tag and property searches.
584 The entire tree below a headline matched by this is considered one project.
585 2. A list of TODO keywords identifying non-stuck projects.
586 If the project subtree contains any headline with one of these todo
587 keywords, the project is considered to be not stuck. If you specify
588 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
589 3. A list of tags identifying non-stuck projects.
590 If the project subtree contains any headline with one of these tags,
591 the project is considered to be not stuck. If you specify \"*\" as
592 a tag, any tag will mark the project unstuck. Note that this is about
593 the explicit presence of a tag somewhere in the subtree, inherited
594 tags do not count here. If inherited tags make a project not stuck,
595 use \"-TAG\" in the tags part of the matcher under (1.) above.
596 4. An arbitrary regular expression matching non-stuck projects.
598 If the project turns out to be not stuck, search continues also in the
599 subtree to see if any of the subtasks have project status.
601 See also the variable `org-tags-match-list-sublevels' which applies
602 to projects matched by this search as well.
604 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
605 or `C-c a #' to produce the list."
606 :group 'org-agenda-custom-commands
607 :type '(list
608 (string :tag "Tags/TODO match to identify a project")
609 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
610 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
611 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
613 (defcustom org-agenda-filter-effort-default-operator "<"
614 "The default operator for effort estimate filtering.
615 If you select an effort estimate limit without first pressing an operator,
616 this one will be used."
617 :group 'org-agenda-custom-commands
618 :type '(choice (const :tag "less or equal" "<")
619 (const :tag "greater or equal"">")
620 (const :tag "equal" "=")))
622 (defgroup org-agenda-skip nil
623 "Options concerning skipping parts of agenda files."
624 :tag "Org Agenda Skip"
625 :group 'org-agenda)
627 (defcustom org-agenda-skip-function-global nil
628 "Function to be called at each match during agenda construction.
629 If this function returns nil, the current match should not be skipped.
630 If the function decided to skip an agenda match, is must return the
631 buffer position from which the search should be continued.
632 This may also be a Lisp form, which will be evaluated.
634 This variable will be applied to every agenda match, including
635 tags/property searches and TODO lists. So try to make the test function
636 do its checking as efficiently as possible. To implement a skipping
637 condition just for specific agenda commands, use the variable
638 `org-agenda-skip-function' which can be set in the options section
639 of custom agenda commands."
640 :group 'org-agenda-skip
641 :type 'sexp)
643 (defgroup org-agenda-daily/weekly nil
644 "Options concerning the daily/weekly agenda."
645 :tag "Org Agenda Daily/Weekly"
646 :group 'org-agenda)
647 (defgroup org-agenda-todo-list nil
648 "Options concerning the global todo list agenda view."
649 :tag "Org Agenda Todo List"
650 :group 'org-agenda)
651 (defgroup org-agenda-match-view nil
652 "Options concerning the general tags/property/todo match agenda view."
653 :tag "Org Agenda Match View"
654 :group 'org-agenda)
655 (defgroup org-agenda-search-view nil
656 "Options concerning the search agenda view."
657 :tag "Org Agenda Search View"
658 :group 'org-agenda)
660 (defvar org-agenda-archives-mode nil
661 "Non-nil means the agenda will include archived items.
662 If this is the symbol `trees', trees in the selected agenda scope
663 that are marked with the ARCHIVE tag will be included anyway. When this is
664 t, also all archive files associated with the current selection of agenda
665 files will be included.")
667 (defcustom org-agenda-restriction-lock-highlight-subtree t
668 "Non-nil means highlight the whole subtree when restriction is active.
669 Otherwise only highlight the headline. Highlighting the whole subtree is
670 useful to ensure no edits happen beyond the restricted region."
671 :group 'org-agenda
672 :type 'boolean)
674 (defcustom org-agenda-skip-comment-trees t
675 "Non-nil means skip trees that start with the COMMENT keyword.
676 When nil, these trees are also scanned by agenda commands."
677 :group 'org-agenda-skip
678 :type 'boolean)
680 (defcustom org-agenda-todo-list-sublevels t
681 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
682 When nil, the sublevels of a TODO entry are not checked, resulting in
683 potentially much shorter TODO lists."
684 :group 'org-agenda-skip
685 :group 'org-agenda-todo-list
686 :type 'boolean)
688 (defcustom org-agenda-todo-ignore-with-date nil
689 "Non-nil means don't show entries with a date in the global todo list.
690 You can use this if you prefer to mark mere appointments with a TODO keyword,
691 but don't want them to show up in the TODO list.
692 When this is set, it also covers deadlines and scheduled items, the settings
693 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
694 will be ignored.
695 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
696 :group 'org-agenda-skip
697 :group 'org-agenda-todo-list
698 :type 'boolean)
700 (defcustom org-agenda-todo-ignore-timestamp nil
701 "Non-nil means don't show entries with a timestamp.
702 This applies when creating the global todo list.
703 Valid values are:
705 past Don't show entries for today or in the past.
707 future Don't show entries with a timestamp in the future.
708 The idea behind this is that if it has a future
709 timestamp, you don't want to think about it until the
710 date.
712 all Don't show any entries with a timestamp in the global todo list.
713 The idea behind this is that by setting a timestamp, you
714 have already \"taken care\" of this item.
716 This variable can also have an integer as a value. If positive (N),
717 todos with a timestamp N or more days in the future will be ignored. If
718 negative (-N), todos with a timestamp N or more days in the past will be
719 ignored. If 0, todos with a timestamp either today or in the future will
720 be ignored. For example, a value of -1 will exclude todos with a
721 timestamp in the past (yesterday or earlier), while a value of 7 will
722 exclude todos with a timestamp a week or more in the future.
724 See also `org-agenda-todo-ignore-with-date'.
725 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
726 to make his option also apply to the tags-todo list."
727 :group 'org-agenda-skip
728 :group 'org-agenda-todo-list
729 :version "24.1"
730 :type '(choice
731 (const :tag "Ignore future timestamp todos" future)
732 (const :tag "Ignore past or present timestamp todos" past)
733 (const :tag "Ignore all timestamp todos" all)
734 (const :tag "Show timestamp todos" nil)
735 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
737 (defcustom org-agenda-todo-ignore-scheduled nil
738 "Non-nil means, ignore some scheduled TODO items when making TODO list.
739 This applies when creating the global todo list.
740 Valid values are:
742 past Don't show entries scheduled today or in the past.
744 future Don't show entries scheduled in the future.
745 The idea behind this is that by scheduling it, you don't want to
746 think about it until the scheduled date.
748 all Don't show any scheduled entries in the global todo list.
749 The idea behind this is that by scheduling it, you have already
750 \"taken care\" of this item.
752 t Same as `all', for backward compatibility.
754 This variable can also have an integer as a value. See
755 `org-agenda-todo-ignore-timestamp' for more details.
757 See also `org-agenda-todo-ignore-with-date'.
758 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
759 to make his option also apply to the tags-todo list."
760 :group 'org-agenda-skip
761 :group 'org-agenda-todo-list
762 :type '(choice
763 (const :tag "Ignore future-scheduled todos" future)
764 (const :tag "Ignore past- or present-scheduled todos" past)
765 (const :tag "Ignore all scheduled todos" all)
766 (const :tag "Ignore all scheduled todos (compatibility)" t)
767 (const :tag "Show scheduled todos" nil)
768 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
770 (defcustom org-agenda-todo-ignore-deadlines nil
771 "Non-nil means ignore some deadline TODO items when making TODO list.
772 There are different motivations for using different values, please think
773 carefully when configuring this variable.
775 This applies when creating the global todo list.
776 Valid values are:
778 near Don't show near deadline entries. A deadline is near when it is
779 closer than `org-deadline-warning-days' days. The idea behind this
780 is that such items will appear in the agenda anyway.
782 far Don't show TODO entries where a deadline has been defined, but
783 the deadline is not near. This is useful if you don't want to
784 use the todo list to figure out what to do now.
786 past Don't show entries with a deadline timestamp for today or in the past.
788 future Don't show entries with a deadline timestamp in the future, not even
789 when they become `near' ones. Use it with caution.
791 all Ignore all TODO entries that do have a deadline.
793 t Same as `near', for backward compatibility.
795 This variable can also have an integer as a value. See
796 `org-agenda-todo-ignore-timestamp' for more details.
798 See also `org-agenda-todo-ignore-with-date'.
799 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
800 to make his option also apply to the tags-todo list."
801 :group 'org-agenda-skip
802 :group 'org-agenda-todo-list
803 :type '(choice
804 (const :tag "Ignore near deadlines" near)
805 (const :tag "Ignore near deadlines (compatibility)" t)
806 (const :tag "Ignore far deadlines" far)
807 (const :tag "Ignore all TODOs with a deadlines" all)
808 (const :tag "Show all TODOs, even if they have a deadline" nil)
809 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
811 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
812 "Time unit to use when possibly ignoring an agenda item.
814 See the docstring of various `org-agenda-todo-ignore-*' options.
815 The default is to compare time stamps using days. An item is thus
816 considered to be in the future if it is at least one day after today.
817 Non-nil means to compare time stamps using seconds. An item is then
818 considered future if it has a time value later than current time."
819 :group 'org-agenda-skip
820 :group 'org-agenda-todo-list
821 :version "24.4"
822 :package-version '(Org . "8.0")
823 :type '(choice
824 (const :tag "Compare time with days" nil)
825 (const :tag "Compare time with seconds" t)))
827 (defcustom org-agenda-tags-todo-honor-ignore-options nil
828 "Non-nil means honor todo-list ignores options also in tags-todo search.
829 The variables
830 `org-agenda-todo-ignore-with-date',
831 `org-agenda-todo-ignore-timestamp',
832 `org-agenda-todo-ignore-scheduled',
833 `org-agenda-todo-ignore-deadlines'
834 make the global TODO list skip entries that have time stamps of certain
835 kinds. If this option is set, the same options will also apply for the
836 tags-todo search, which is the general tags/property matcher
837 restricted to unfinished TODO entries only."
838 :group 'org-agenda-skip
839 :group 'org-agenda-todo-list
840 :group 'org-agenda-match-view
841 :type 'boolean)
843 (defcustom org-agenda-skip-scheduled-if-done nil
844 "Non-nil means don't show scheduled items in agenda when they are done.
845 This is relevant for the daily/weekly agenda, not for the TODO list. And
846 it applies only to the actual date of the scheduling. Warnings about
847 an item with a past scheduling dates are always turned off when the item
848 is DONE."
849 :group 'org-agenda-skip
850 :group 'org-agenda-daily/weekly
851 :type 'boolean)
853 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
854 "Non-nil means skip scheduling line if same entry shows because of deadline.
856 In the agenda of today, an entry can show up multiple times
857 because it is both scheduled and has a nearby deadline, and maybe
858 a plain time stamp as well.
860 When this variable is nil, the entry will be shown several times.
862 When set to t, then only the deadline is shown and the fact that
863 the entry is scheduled today or was scheduled previously is not
864 shown.
866 When set to the symbol `not-today', skip scheduled previously,
867 but not scheduled today.
869 When set to the symbol `repeated-after-deadline', skip scheduled
870 items if they are repeated beyond the current deadline."
871 :group 'org-agenda-skip
872 :group 'org-agenda-daily/weekly
873 :type '(choice
874 (const :tag "Never" nil)
875 (const :tag "Always" t)
876 (const :tag "Not when scheduled today" not-today)
877 (const :tag "When repeated past deadline" repeated-after-deadline)))
879 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
880 "Non-nil means skip timestamp line if same entry shows because of deadline.
881 In the agenda of today, an entry can show up multiple times
882 because it has both a plain timestamp and has a nearby deadline.
883 When this variable is t, then only the deadline is shown and the
884 fact that the entry has a timestamp for or including today is not
885 shown. When this variable is nil, the entry will be shown
886 several times."
887 :group 'org-agenda-skip
888 :group 'org-agenda-daily/weekly
889 :version "24.1"
890 :type '(choice
891 (const :tag "Never" nil)
892 (const :tag "Always" t)))
894 (defcustom org-agenda-skip-deadline-if-done nil
895 "Non-nil means don't show deadlines when the corresponding item is done.
896 When nil, the deadline is still shown and should give you a happy feeling.
897 This is relevant for the daily/weekly agenda. And it applied only to the
898 actually date of the deadline. Warnings about approaching and past-due
899 deadlines are always turned off when the item is DONE."
900 :group 'org-agenda-skip
901 :group 'org-agenda-daily/weekly
902 :type 'boolean)
904 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
905 "Non-nil means skip deadline prewarning when entry is also scheduled.
906 This will apply on all days where a prewarning for the deadline would
907 be shown, but not at the day when the entry is actually due. On that day,
908 the deadline will be shown anyway.
909 This variable may be set to nil, t, the symbol `pre-scheduled',
910 or a number which will then give the number of days before the actual
911 deadline when the prewarnings should resume. The symbol `pre-scheduled'
912 eliminates the deadline prewarning only prior to the scheduled date.
913 This can be used in a workflow where the first showing of the deadline will
914 trigger you to schedule it, and then you don't want to be reminded of it
915 because you will take care of it on the day when scheduled."
916 :group 'org-agenda-skip
917 :group 'org-agenda-daily/weekly
918 :version "24.1"
919 :type '(choice
920 (const :tag "Always show prewarning" nil)
921 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
922 (const :tag "Remove prewarning if entry is scheduled" t)
923 (integer :tag "Restart prewarning N days before deadline")))
925 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
926 "Non-nil means skip scheduled delay when entry also has a deadline.
927 This variable may be set to nil, t, the symbol `post-deadline',
928 or a number which will then give the number of days after the actual
929 scheduled date when the delay should expire. The symbol `post-deadline'
930 eliminates the schedule delay when the date is posterior to the deadline."
931 :group 'org-agenda-skip
932 :group 'org-agenda-daily/weekly
933 :version "24.4"
934 :package-version '(Org . "8.0")
935 :type '(choice
936 (const :tag "Always honor delay" nil)
937 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
938 (const :tag "Ignore delay if entry has a deadline" t)
939 (integer :tag "Honor delay up until N days after the scheduled date")))
941 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
942 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
943 When non-nil, after the search for timestamps has matched once in an
944 entry, the rest of the entry will not be searched."
945 :group 'org-agenda-skip
946 :type 'boolean)
948 (defcustom org-agenda-skip-timestamp-if-done nil
949 "Non-nil means don't select item by timestamp or -range if it is DONE."
950 :group 'org-agenda-skip
951 :group 'org-agenda-daily/weekly
952 :type 'boolean)
954 (defcustom org-agenda-dim-blocked-tasks t
955 "Non-nil means dim blocked tasks in the agenda display.
956 This causes some overhead during agenda construction, but if you
957 have turned on `org-enforce-todo-dependencies',
958 `org-enforce-todo-checkbox-dependencies', or any other blocking
959 mechanism, this will create useful feedback in the agenda.
961 Instead of t, this variable can also have the value `invisible'.
962 Then blocked tasks will be invisible and only become visible when
963 they become unblocked. An exemption to this behavior is when a task is
964 blocked because of unchecked checkboxes below it. Since checkboxes do
965 not show up in the agenda views, making this task invisible you remove any
966 trace from agenda views that there is something to do. Therefore, a task
967 that is blocked because of checkboxes will never be made invisible, it
968 will only be dimmed."
969 :group 'org-agenda-daily/weekly
970 :group 'org-agenda-todo-list
971 :version "24.3"
972 :type '(choice
973 (const :tag "Do not dim" nil)
974 (const :tag "Dim to a gray face" t)
975 (const :tag "Make invisible" invisible)))
977 (defcustom org-timeline-show-empty-dates 3
978 "Non-nil means `org-timeline' also shows dates without an entry.
979 When nil, only the days which actually have entries are shown.
980 When t, all days between the first and the last date are shown.
981 When an integer, show also empty dates, but if there is a gap of more than
982 N days, just insert a special line indicating the size of the gap."
983 :group 'org-agenda-skip
984 :type '(choice
985 (const :tag "None" nil)
986 (const :tag "All" t)
987 (integer :tag "at most")))
989 (defgroup org-agenda-startup nil
990 "Options concerning initial settings in the Agenda in Org Mode."
991 :tag "Org Agenda Startup"
992 :group 'org-agenda)
994 (defcustom org-agenda-menu-show-matcher t
995 "Non-nil means show the match string in the agenda dispatcher menu.
996 When nil, the matcher string is not shown, but is put into the help-echo
997 property so than moving the mouse over the command shows it.
998 Setting it to nil is good if matcher strings are very long and/or if
999 you want to use two-columns display (see `org-agenda-menu-two-columns')."
1000 :group 'org-agenda
1001 :version "24.1"
1002 :type 'boolean)
1004 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1006 (defcustom org-agenda-menu-two-columns nil
1007 "Non-nil means, use two columns to show custom commands in the dispatcher.
1008 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1009 to nil."
1010 :group 'org-agenda
1011 :version "24.1"
1012 :type 'boolean)
1014 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1015 (defcustom org-agenda-finalize-hook nil
1016 "Hook run just before displaying an agenda buffer.
1017 The buffer is still writable when the hook is called.
1019 You can modify some of the buffer substrings but you should be
1020 extra careful not to modify the text properties of the agenda
1021 headlines as the agenda display heavily relies on them."
1022 :group 'org-agenda-startup
1023 :type 'hook)
1025 (defcustom org-agenda-mouse-1-follows-link nil
1026 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1027 A longer mouse click will still set point. Does not work on XEmacs.
1028 Needs to be set before org.el is loaded."
1029 :group 'org-agenda-startup
1030 :type 'boolean)
1032 (defcustom org-agenda-start-with-follow-mode nil
1033 "The initial value of follow mode in a newly created agenda window."
1034 :group 'org-agenda-startup
1035 :type 'boolean)
1037 (defcustom org-agenda-follow-indirect nil
1038 "Non-nil means `org-agenda-follow-mode' displays only the
1039 current item's tree, in an indirect buffer."
1040 :group 'org-agenda
1041 :version "24.1"
1042 :type 'boolean)
1044 (defcustom org-agenda-show-outline-path t
1045 "Non-nil means show outline path in echo area after line motion."
1046 :group 'org-agenda-startup
1047 :type 'boolean)
1049 (defcustom org-agenda-start-with-entry-text-mode nil
1050 "The initial value of entry-text-mode in a newly created agenda window."
1051 :group 'org-agenda-startup
1052 :type 'boolean)
1054 (defcustom org-agenda-entry-text-maxlines 5
1055 "Number of text lines to be added when `E' is pressed in the agenda.
1057 Note that this variable only used during agenda display. Add add entry text
1058 when exporting the agenda, configure the variable
1059 `org-agenda-add-entry-ext-maxlines'."
1060 :group 'org-agenda
1061 :type 'integer)
1063 (defcustom org-agenda-entry-text-exclude-regexps nil
1064 "List of regular expressions to clean up entry text.
1065 The complete matches of all regular expressions in this list will be
1066 removed from entry text before it is shown in the agenda."
1067 :group 'org-agenda
1068 :type '(repeat (regexp)))
1070 (defcustom org-agenda-entry-text-leaders " > "
1071 "Text prepended to the entry text in agenda buffers."
1072 :version "24.4"
1073 :package-version '(Org . "8.0")
1074 :group 'org-agenda
1075 :type 'string)
1077 (defvar org-agenda-entry-text-cleanup-hook nil
1078 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1079 This cleanup is done in a temporary buffer, so the function may inspect and
1080 change the entire buffer.
1081 Some default stuff like drawers and scheduling/deadline dates will already
1082 have been removed when this is called, as will any matches for regular
1083 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1085 (defvar org-agenda-include-inactive-timestamps nil
1086 "Non-nil means include inactive time stamps in agenda and timeline.
1087 Dynamically scoped.")
1089 (defgroup org-agenda-windows nil
1090 "Options concerning the windows used by the Agenda in Org Mode."
1091 :tag "Org Agenda Windows"
1092 :group 'org-agenda)
1094 (defcustom org-agenda-window-setup 'reorganize-frame
1095 "How the agenda buffer should be displayed.
1096 Possible values for this option are:
1098 current-window Show agenda in the current window, keeping all other windows.
1099 other-window Use `switch-to-buffer-other-window' to display agenda.
1100 reorganize-frame Show only two windows on the current frame, the current
1101 window and the agenda.
1102 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1103 Also, when exiting the agenda, kill that frame.
1104 See also the variable `org-agenda-restore-windows-after-quit'."
1105 :group 'org-agenda-windows
1106 :type '(choice
1107 (const current-window)
1108 (const other-frame)
1109 (const other-window)
1110 (const reorganize-frame)))
1112 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1113 "The min and max height of the agenda window as a fraction of frame height.
1114 The value of the variable is a cons cell with two numbers between 0 and 1.
1115 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1116 :group 'org-agenda-windows
1117 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1119 (defcustom org-agenda-restore-windows-after-quit nil
1120 "Non-nil means restore window configuration upon exiting agenda.
1121 Before the window configuration is changed for displaying the agenda,
1122 the current status is recorded. When the agenda is exited with
1123 `q' or `x' and this option is set, the old state is restored. If
1124 `org-agenda-window-setup' is `other-frame', the value of this
1125 option will be ignored."
1126 :group 'org-agenda-windows
1127 :type 'boolean)
1129 (defcustom org-agenda-ndays nil
1130 "Number of days to include in overview display.
1131 Should be 1 or 7.
1132 Obsolete, see `org-agenda-span'."
1133 :group 'org-agenda-daily/weekly
1134 :type '(choice (const nil)
1135 (integer)))
1137 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1139 (defcustom org-agenda-span 'week
1140 "Number of days to include in overview display.
1141 Can be day, week, month, year, or any number of days.
1142 Custom commands can set this variable in the options section."
1143 :group 'org-agenda-daily/weekly
1144 :type '(choice (const :tag "Day" day)
1145 (const :tag "Week" week)
1146 (const :tag "Fortnight" fortnight)
1147 (const :tag "Month" month)
1148 (const :tag "Year" year)
1149 (integer :tag "Custom")))
1151 (defcustom org-agenda-start-on-weekday 1
1152 "Non-nil means start the overview always on the specified weekday.
1153 0 denotes Sunday, 1 denotes Monday, etc.
1154 When nil, always start on the current day.
1155 Custom commands can set this variable in the options section."
1156 :group 'org-agenda-daily/weekly
1157 :type '(choice (const :tag "Today" nil)
1158 (integer :tag "Weekday No.")))
1160 (defcustom org-agenda-show-all-dates t
1161 "Non-nil means `org-agenda' shows every day in the selected range.
1162 When nil, only the days which actually have entries are shown."
1163 :group 'org-agenda-daily/weekly
1164 :type 'boolean)
1166 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1167 "Format string for displaying dates in the agenda.
1168 Used by the daily/weekly agenda and by the timeline. This should be
1169 a format string understood by `format-time-string', or a function returning
1170 the formatted date as a string. The function must take a single argument,
1171 a calendar-style date list like (month day year)."
1172 :group 'org-agenda-daily/weekly
1173 :type '(choice
1174 (string :tag "Format string")
1175 (function :tag "Function")))
1177 (defun org-agenda-format-date-aligned (date)
1178 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1179 This function makes sure that dates are aligned for easy reading."
1180 (require 'cal-iso)
1181 (let* ((dayname (calendar-day-name date))
1182 (day (cadr date))
1183 (day-of-week (calendar-day-of-week date))
1184 (month (car date))
1185 (monthname (calendar-month-name month))
1186 (year (nth 2 date))
1187 (iso-week (org-days-to-iso-week
1188 (calendar-absolute-from-gregorian date)))
1189 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1190 (1- year))
1191 ((and (= month 12) (<= iso-week 1))
1192 (1+ year))
1193 (t year)))
1194 (weekstring (if (= day-of-week 1)
1195 (format " W%02d" iso-week)
1196 "")))
1197 (format "%-10s %2d %s %4d%s"
1198 dayname day monthname year weekstring)))
1200 (defcustom org-agenda-time-leading-zero nil
1201 "Non-nil means use leading zero for military times in agenda.
1202 For example, 9:30am would become 09:30 rather than 9:30."
1203 :group 'org-agenda-daily/weekly
1204 :version "24.1"
1205 :type 'boolean)
1207 (defcustom org-agenda-timegrid-use-ampm nil
1208 "When set, show AM/PM style timestamps on the timegrid."
1209 :group 'org-agenda
1210 :version "24.1"
1211 :type 'boolean)
1213 (defun org-agenda-time-of-day-to-ampm (time)
1214 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1215 (let* ((hour-number (string-to-number (substring time 0 -3)))
1216 (minute (substring time -2))
1217 (ampm "am"))
1218 (cond
1219 ((equal hour-number 12)
1220 (setq ampm "pm"))
1221 ((> hour-number 12)
1222 (setq ampm "pm")
1223 (setq hour-number (- hour-number 12))))
1224 (concat
1225 (if org-agenda-time-leading-zero
1226 (format "%02d" hour-number)
1227 (format "%02s" (number-to-string hour-number)))
1228 ":" minute ampm)))
1230 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1231 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1232 (if org-agenda-timegrid-use-ampm
1233 (org-agenda-time-of-day-to-ampm time)
1234 time))
1236 (defcustom org-agenda-weekend-days '(6 0)
1237 "Which days are weekend?
1238 These days get the special face `org-agenda-date-weekend' in the agenda
1239 and timeline buffers."
1240 :group 'org-agenda-daily/weekly
1241 :type '(set :greedy t
1242 (const :tag "Monday" 1)
1243 (const :tag "Tuesday" 2)
1244 (const :tag "Wednesday" 3)
1245 (const :tag "Thursday" 4)
1246 (const :tag "Friday" 5)
1247 (const :tag "Saturday" 6)
1248 (const :tag "Sunday" 0)))
1250 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1251 "Non-nil means jump to today when moving a past date forward in time.
1252 When using S-right in the agenda to move a a date forward, and the date
1253 stamp currently points to the past, the first key press will move it
1254 to today. WHen nil, just move one day forward even if the date stays
1255 in the past."
1256 :group 'org-agenda-daily/weekly
1257 :version "24.1"
1258 :type 'boolean)
1260 (defcustom org-agenda-include-diary nil
1261 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :type 'boolean)
1266 (defcustom org-agenda-include-deadlines t
1267 "If non-nil, include entries within their deadline warning period.
1268 Custom commands can set this variable in the options section."
1269 :group 'org-agenda-daily/weekly
1270 :version "24.1"
1271 :type 'boolean)
1273 (defcustom org-agenda-repeating-timestamp-show-all t
1274 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1275 When set to a list of strings, only show occurrences of repeating
1276 stamps for these TODO keywords. When nil, only one occurrence is
1277 shown, either today or the nearest into the future."
1278 :group 'org-agenda-daily/weekly
1279 :type '(choice
1280 (const :tag "Show repeating stamps" t)
1281 (repeat :tag "Show repeating stamps for these TODO keywords"
1282 (string :tag "TODO Keyword"))
1283 (const :tag "Don't show repeating stamps" nil)))
1285 (defcustom org-scheduled-past-days 10000
1286 "Number of days to continue listing scheduled items not marked DONE.
1287 When an item is scheduled on a date, it shows up in the agenda on this
1288 day and will be listed until it is marked done for the number of days
1289 given here."
1290 :group 'org-agenda-daily/weekly
1291 :type 'integer)
1293 (defcustom org-agenda-log-mode-items '(closed clock)
1294 "List of items that should be shown in agenda log mode.
1295 This list may contain the following symbols:
1297 closed Show entries that have been closed on that day.
1298 clock Show entries that have received clocked time on that day.
1299 state Show all logged state changes.
1300 Note that instead of changing this variable, you can also press `C-u l' in
1301 the agenda to display all available LOG items temporarily."
1302 :group 'org-agenda-daily/weekly
1303 :type '(set :greedy t (const closed) (const clock) (const state)))
1305 (defcustom org-agenda-clock-consistency-checks
1306 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1307 :gap-ok-around ("4:00")
1308 :default-face ((:background "DarkRed") (:foreground "white"))
1309 :overlap-face nil :gap-face nil :no-end-time-face nil
1310 :long-face nil :short-face nil)
1311 "This is a property list, with the following keys:
1313 :max-duration Mark clocking chunks that are longer than this time.
1314 This is a time string like \"HH:MM\", or the number
1315 of minutes as an integer.
1317 :min-duration Mark clocking chunks that are shorter that this.
1318 This is a time string like \"HH:MM\", or the number
1319 of minutes as an integer.
1321 :max-gap Mark gaps between clocking chunks that are longer than
1322 this duration. A number of minutes, or a string
1323 like \"HH:MM\".
1325 :gap-ok-around List of times during the day which are usually not working
1326 times. When a gap is detected, but the gap contains any
1327 of these times, the gap is *not* reported. For example,
1328 if this is (\"4:00\" \"13:00\") then gaps that contain
1329 4:00 in the morning (i.e. the night) and 13:00
1330 (i.e. a typical lunch time) do not cause a warning.
1331 You should have at least one time during the night in this
1332 list, or otherwise the first task each morning will trigger
1333 a warning because it follows a long gap.
1335 Furthermore, the following properties can be used to define faces for
1336 issue display.
1338 :default-face the default face, if the specific face is undefined
1339 :overlap-face face for overlapping clocks
1340 :gap-face face for gaps between clocks
1341 :no-end-time-face face for incomplete clocks
1342 :long-face face for clock intervals that are too long
1343 :short-face face for clock intervals that are too short"
1344 :group 'org-agenda-daily/weekly
1345 :group 'org-clock
1346 :version "24.1"
1347 :type 'plist)
1349 (defcustom org-agenda-log-mode-add-notes t
1350 "Non-nil means add first line of notes to log entries in agenda views.
1351 If a log item like a state change or a clock entry is associated with
1352 notes, the first line of these notes will be added to the entry in the
1353 agenda display."
1354 :group 'org-agenda-daily/weekly
1355 :type 'boolean)
1357 (defcustom org-agenda-start-with-log-mode nil
1358 "The initial value of log-mode in a newly created agenda window.
1359 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1360 explanations on the possible values."
1361 :group 'org-agenda-startup
1362 :group 'org-agenda-daily/weekly
1363 :type '(choice (const :tag "Don't show log items" nil)
1364 (const :tag "Show only log items" only)
1365 (const :tag "Show all possible log items" clockcheck)
1366 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1367 (choice (const :tag "Show closed log items" closed)
1368 (const :tag "Show clocked log items" clock)
1369 (const :tag "Show all logged state changes" state)))))
1371 (defcustom org-agenda-start-with-clockreport-mode nil
1372 "The initial value of clockreport-mode in a newly created agenda window."
1373 :group 'org-agenda-startup
1374 :group 'org-agenda-daily/weekly
1375 :type 'boolean)
1377 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1378 "Property list with parameters for the clocktable in clockreport mode.
1379 This is the display mode that shows a clock table in the daily/weekly
1380 agenda, the properties for this dynamic block can be set here.
1381 The usual clocktable parameters are allowed here, but you cannot set
1382 the properties :name, :tstart, :tend, :block, and :scope - these will
1383 be overwritten to make sure the content accurately reflects the
1384 current display in the agenda."
1385 :group 'org-agenda-daily/weekly
1386 :type 'plist)
1388 (defcustom org-agenda-search-view-always-boolean nil
1389 "Non-nil means the search string is interpreted as individual parts.
1391 The search string for search view can either be interpreted as a phrase,
1392 or as a list of snippets that define a boolean search for a number of
1393 strings.
1395 When this is non-nil, the string will be split on whitespace, and each
1396 snippet will be searched individually, and all must match in order to
1397 select an entry. A snippet is then a single string of non-white
1398 characters, or a string in double quotes, or a regexp in {} braces.
1399 If a snippet is preceded by \"-\", the snippet must *not* match.
1400 \"+\" is syntactic sugar for positive selection. Each snippet may
1401 be found as a full word or a partial word, but see the variable
1402 `org-agenda-search-view-force-full-words'.
1404 When this is nil, search will look for the entire search phrase as one,
1405 with each space character matching any amount of whitespace, including
1406 line breaks.
1408 Even when this is nil, you can still switch to Boolean search dynamically
1409 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1410 is a regexp marked with braces like \"{abc}\", this will also switch to
1411 boolean search."
1412 :group 'org-agenda-search-view
1413 :version "24.1"
1414 :type 'boolean)
1416 (org-defvaralias 'org-agenda-search-view-search-words-only
1417 'org-agenda-search-view-always-boolean)
1419 (defcustom org-agenda-search-view-force-full-words nil
1420 "Non-nil means, search words must be matches as complete words.
1421 When nil, they may also match part of a word."
1422 :group 'org-agenda-search-view
1423 :version "24.1"
1424 :type 'boolean)
1426 (defcustom org-agenda-search-view-max-outline-level 0
1427 "Maximum outline level to display in search view.
1428 E.g. when this is set to 1, the search view will only
1429 show headlines of level 1. When set to 0, the default
1430 value, don't limit agenda view by outline level."
1431 :group 'org-agenda-search-view
1432 :version "24.4"
1433 :package-version '(Org . "8.3")
1434 :type 'integer)
1436 (defgroup org-agenda-time-grid nil
1437 "Options concerning the time grid in the Org-mode Agenda."
1438 :tag "Org Agenda Time Grid"
1439 :group 'org-agenda)
1441 (defcustom org-agenda-search-headline-for-time t
1442 "Non-nil means search headline for a time-of-day.
1443 If the headline contains a time-of-day in one format or another, it will
1444 be used to sort the entry into the time sequence of items for a day.
1445 Some people have time stamps in the headline that refer to the creation
1446 time or so, and then this produces an unwanted side effect. If this is
1447 the case for your, use this variable to turn off searching the headline
1448 for a time."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1452 (defcustom org-agenda-use-time-grid t
1453 "Non-nil means show a time grid in the agenda schedule.
1454 A time grid is a set of lines for specific times (like every two hours between
1455 8:00 and 20:00). The items scheduled for a day at specific times are
1456 sorted in between these lines.
1457 For details about when the grid will be shown, and what it will look like, see
1458 the variable `org-agenda-time-grid'."
1459 :group 'org-agenda-time-grid
1460 :type 'boolean)
1462 (defcustom org-agenda-time-grid
1463 '((daily today require-timed)
1464 "----------------"
1465 (800 1000 1200 1400 1600 1800 2000))
1467 "The settings for time grid for agenda display.
1468 This is a list of three items. The first item is again a list. It contains
1469 symbols specifying conditions when the grid should be displayed:
1471 daily if the agenda shows a single day
1472 weekly if the agenda shows an entire week
1473 today show grid on current date, independent of daily/weekly display
1474 require-timed show grid only if at least one item has a time specification
1476 The second item is a string which will be placed behind the grid time.
1478 The third item is a list of integers, indicating the times that should have
1479 a grid line."
1480 :group 'org-agenda-time-grid
1481 :type
1482 '(list
1483 (set :greedy t :tag "Grid Display Options"
1484 (const :tag "Show grid in single day agenda display" daily)
1485 (const :tag "Show grid in weekly agenda display" weekly)
1486 (const :tag "Always show grid for today" today)
1487 (const :tag "Show grid only if any timed entries are present"
1488 require-timed)
1489 (const :tag "Skip grid times already present in an entry"
1490 remove-match))
1491 (string :tag "Grid String")
1492 (repeat :tag "Grid Times" (integer :tag "Time"))))
1494 (defcustom org-agenda-show-current-time-in-grid t
1495 "Non-nil means show the current time in the time grid."
1496 :group 'org-agenda-time-grid
1497 :version "24.1"
1498 :type 'boolean)
1500 (defcustom org-agenda-current-time-string
1501 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1502 "The string for the current time marker in the agenda."
1503 :group 'org-agenda-time-grid
1504 :version "24.1"
1505 :type 'string)
1507 (defgroup org-agenda-sorting nil
1508 "Options concerning sorting in the Org-mode Agenda."
1509 :tag "Org Agenda Sorting"
1510 :group 'org-agenda)
1512 (defcustom org-agenda-sorting-strategy
1513 '((agenda habit-down time-up priority-down category-keep)
1514 (todo priority-down category-keep)
1515 (tags priority-down category-keep)
1516 (search category-keep))
1517 "Sorting structure for the agenda items of a single day.
1518 This is a list of symbols which will be used in sequence to determine
1519 if an entry should be listed before another entry. The following
1520 symbols are recognized:
1522 time-up Put entries with time-of-day indications first, early first
1523 time-down Put entries with time-of-day indications first, late first
1524 timestamp-up Sort by any timestamp, early first
1525 timestamp-down Sort by any timestamp, late first
1526 scheduled-up Sort by scheduled timestamp, early first
1527 scheduled-down Sort by scheduled timestamp, late first
1528 deadline-up Sort by deadline timestamp, early first
1529 deadline-down Sort by deadline timestamp, late first
1530 ts-up Sort by active timestamp, early first
1531 ts-down Sort by active timestamp, late first
1532 tsia-up Sort by inactive timestamp, early first
1533 tsia-down Sort by inactive timestamp, late first
1534 category-keep Keep the default order of categories, corresponding to the
1535 sequence in `org-agenda-files'.
1536 category-up Sort alphabetically by category, A-Z.
1537 category-down Sort alphabetically by category, Z-A.
1538 tag-up Sort alphabetically by last tag, A-Z.
1539 tag-down Sort alphabetically by last tag, Z-A.
1540 priority-up Sort numerically by priority, high priority last.
1541 priority-down Sort numerically by priority, high priority first.
1542 todo-state-up Sort by todo state, tasks that are done last.
1543 todo-state-down Sort by todo state, tasks that are done first.
1544 effort-up Sort numerically by estimated effort, high effort last.
1545 effort-down Sort numerically by estimated effort, high effort first.
1546 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1547 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1548 habit-up Put entries that are habits first
1549 habit-down Put entries that are habits last
1550 alpha-up Sort headlines alphabetically
1551 alpha-down Sort headlines alphabetically, reversed
1553 The different possibilities will be tried in sequence, and testing stops
1554 if one comparison returns a \"not-equal\". For example, the default
1555 '(time-up category-keep priority-down)
1556 means: Pull out all entries having a specified time of day and sort them,
1557 in order to make a time schedule for the current day the first thing in the
1558 agenda listing for the day. Of the entries without a time indication, keep
1559 the grouped in categories, don't sort the categories, but keep them in
1560 the sequence given in `org-agenda-files'. Within each category sort by
1561 priority.
1563 Leaving out `category-keep' would mean that items will be sorted across
1564 categories by priority.
1566 Instead of a single list, this can also be a set of list for specific
1567 contents, with a context symbol in the car of the list, any of
1568 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1570 Custom commands can bind this variable in the options section."
1571 :group 'org-agenda-sorting
1572 :type `(choice
1573 (repeat :tag "General" ,org-sorting-choice)
1574 (list :tag "Individually"
1575 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1576 (repeat ,org-sorting-choice))
1577 (cons (const :tag "Strategy for TODO lists" todo)
1578 (repeat ,org-sorting-choice))
1579 (cons (const :tag "Strategy for Tags matches" tags)
1580 (repeat ,org-sorting-choice))
1581 (cons (const :tag "Strategy for search matches" search)
1582 (repeat ,org-sorting-choice)))))
1584 (defcustom org-agenda-cmp-user-defined nil
1585 "A function to define the comparison `user-defined'.
1586 This function must receive two arguments, agenda entry a and b.
1587 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1588 the user comparison, return nil.
1589 When this is defined, you can make `user-defined-up' and `user-defined-down'
1590 part of an agenda sorting strategy."
1591 :group 'org-agenda-sorting
1592 :type 'symbol)
1594 (defcustom org-sort-agenda-notime-is-late t
1595 "Non-nil means items without time are considered late.
1596 This is only relevant for sorting. When t, items which have no explicit
1597 time like 15:30 will be considered as 99:01, i.e. later than any items which
1598 do have a time. When nil, the default time is before 0:00. You can use this
1599 option to decide if the schedule for today should come before or after timeless
1600 agenda entries."
1601 :group 'org-agenda-sorting
1602 :type 'boolean)
1604 (defcustom org-sort-agenda-noeffort-is-high t
1605 "Non-nil means items without effort estimate are sorted as high effort.
1606 This also applies when filtering an agenda view with respect to the
1607 < or > effort operator. Then, tasks with no effort defined will be treated
1608 as tasks with high effort.
1609 When nil, such items are sorted as 0 minutes effort."
1610 :group 'org-agenda-sorting
1611 :type 'boolean)
1613 (defgroup org-agenda-line-format nil
1614 "Options concerning the entry prefix in the Org-mode agenda display."
1615 :tag "Org Agenda Line Format"
1616 :group 'org-agenda)
1618 (defcustom org-agenda-prefix-format
1619 '((agenda . " %i %-12:c%?-12t% s")
1620 (timeline . " % s")
1621 (todo . " %i %-12:c")
1622 (tags . " %i %-12:c")
1623 (search . " %i %-12:c"))
1624 "Format specifications for the prefix of items in the agenda views.
1625 An alist with five entries, each for the different agenda types. The
1626 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1627 The values are format strings.
1629 This format works similar to a printf format, with the following meaning:
1631 %c the category of the item, \"Diary\" for entries from the diary,
1632 or as given by the CATEGORY keyword or derived from the file name
1633 %e the effort required by the item
1634 %l the level of the item (insert X space(s) if item is of level X)
1635 %i the icon category of the item, see `org-agenda-category-icon-alist'
1636 %T the last tag of the item (ignore inherited tags, which come first)
1637 %t the HH:MM time-of-day specification if one applies to the entry
1638 %s Scheduling/Deadline information, a short string
1639 %b show breadcrumbs, i.e., the names of the higher levels
1640 %(expression) Eval EXPRESSION and replace the control string
1641 by the result
1643 All specifiers work basically like the standard `%s' of printf, but may
1644 contain two additional characters: a question mark just after the `%'
1645 and a whitespace/punctuation character just before the final letter.
1647 If the first character after `%' is a question mark, the entire field
1648 will only be included if the corresponding value applies to the current
1649 entry. This is useful for fields which should have fixed width when
1650 present, but zero width when absent. For example, \"%?-12t\" will
1651 result in a 12 character time field if a time of the day is specified,
1652 but will completely disappear in entries which do not contain a time.
1654 If there is punctuation or whitespace character just before the
1655 final format letter, this character will be appended to the field
1656 value if the value is not empty. For example, the format
1657 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1658 the category is empty, no additional colon is inserted.
1660 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1661 which means:
1663 - Indent the line with two space characters
1664 - Give the category a 12 chars wide field, padded with whitespace on
1665 the right (because of `-'). Append a colon if there is a category
1666 (because of `:').
1667 - If there is a time-of-day, put it into a 12 chars wide field. If no
1668 time, don't put in an empty field, just skip it (because of '?').
1669 - Finally, put the scheduling information.
1671 See also the variables `org-agenda-remove-times-when-in-prefix' and
1672 `org-agenda-remove-tags'.
1674 Custom commands can set this variable in the options section."
1675 :type '(choice
1676 (string :tag "General format")
1677 (list :greedy t :tag "View dependent"
1678 (cons (const agenda) (string :tag "Format"))
1679 (cons (const timeline) (string :tag "Format"))
1680 (cons (const todo) (string :tag "Format"))
1681 (cons (const tags) (string :tag "Format"))
1682 (cons (const search) (string :tag "Format"))))
1683 :group 'org-agenda-line-format)
1685 (defvar org-prefix-format-compiled nil
1686 "The compiled prefix format and associated variables.
1687 This is a list where first element is a list of variable bindings, and second
1688 element is the compiled format expression. See the variable
1689 `org-agenda-prefix-format'.")
1691 (defcustom org-agenda-todo-keyword-format "%-1s"
1692 "Format for the TODO keyword in agenda lines.
1693 Set this to something like \"%-12s\" if you want all TODO keywords
1694 to occupy a fixed space in the agenda display."
1695 :group 'org-agenda-line-format
1696 :type 'string)
1698 (defcustom org-agenda-diary-sexp-prefix nil
1699 "A regexp that matches part of a diary sexp entry
1700 which should be treated as scheduling/deadline information in
1701 `org-agenda'.
1703 For example, you can use this to extract the `diary-remind-message' from
1704 `diary-remind' entries."
1705 :group 'org-agenda-line-format
1706 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1708 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1709 "Text preceding timerange entries in the agenda view.
1710 This is a list with two strings. The first applies when the range
1711 is entirely on one day. The second applies if the range spans several days.
1712 The strings may have two \"%d\" format specifiers which will be filled
1713 with the sequence number of the days, and the total number of days in the
1714 range, respectively."
1715 :group 'org-agenda-line-format
1716 :type '(list
1717 (string :tag "Deadline today ")
1718 (choice :tag "Deadline relative"
1719 (string :tag "Format string")
1720 (function))))
1722 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1723 "Text preceding scheduled items in the agenda view.
1724 This is a list with two strings. The first applies when the item is
1725 scheduled on the current day. The second applies when it has been scheduled
1726 previously, it may contain a %d indicating that this is the nth time that
1727 this item is scheduled, due to automatic rescheduling of unfinished items
1728 for the following day. So this number is one larger than the number of days
1729 that passed since this item was scheduled first."
1730 :group 'org-agenda-line-format
1731 :version "24.4"
1732 :package-version '(Org . "8.0")
1733 :type '(list
1734 (string :tag "Scheduled today ")
1735 (string :tag "Scheduled previously")))
1737 (defcustom org-agenda-inactive-leader "["
1738 "Text preceding item pulled into the agenda by inactive time stamps.
1739 These entries are added to the agenda when pressing \"[\"."
1740 :group 'org-agenda-line-format
1741 :version "24.1"
1742 :type 'string)
1744 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1745 "Text preceding deadline items in the agenda view.
1746 This is a list with three strings. The first applies when the item has its
1747 deadline on the current day. The second applies when the deadline is in the
1748 future, the third one when it is in the past. The strings may contain %d
1749 to capture the number of days."
1750 :group 'org-agenda-line-format
1751 :version "24.4"
1752 :package-version '(Org . "8.0")
1753 :type '(list
1754 (string :tag "Deadline today ")
1755 (string :tag "Deadline in the future ")
1756 (string :tag "Deadline in the past ")))
1758 (defcustom org-agenda-remove-times-when-in-prefix t
1759 "Non-nil means remove duplicate time specifications in agenda items.
1760 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1761 time-of-day specification in a headline or diary entry is extracted and
1762 placed into the prefix. If this option is non-nil, the original specification
1763 \(a timestamp or -range, or just a plain time(range) specification like
1764 11:30-4pm) will be removed for agenda display. This makes the agenda less
1765 cluttered.
1766 The option can be t or nil. It may also be the symbol `beg', indicating
1767 that the time should only be removed when it is located at the beginning of
1768 the headline/diary entry."
1769 :group 'org-agenda-line-format
1770 :type '(choice
1771 (const :tag "Always" t)
1772 (const :tag "Never" nil)
1773 (const :tag "When at beginning of entry" beg)))
1775 (defcustom org-agenda-remove-timeranges-from-blocks nil
1776 "Non-nil means remove time ranges specifications in agenda
1777 items that span on several days."
1778 :group 'org-agenda-line-format
1779 :version "24.1"
1780 :type 'boolean)
1782 (defcustom org-agenda-default-appointment-duration nil
1783 "Default duration for appointments that only have a starting time.
1784 When nil, no duration is specified in such cases.
1785 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1786 :group 'org-agenda-line-format
1787 :type '(choice
1788 (integer :tag "Minutes")
1789 (const :tag "No default duration")))
1791 (defcustom org-agenda-show-inherited-tags t
1792 "Non-nil means show inherited tags in each agenda line.
1794 When this option is set to 'always, it take precedences over
1795 `org-agenda-use-tag-inheritance' and inherited tags are shown
1796 in every agenda.
1798 When this option is set to t (the default), inherited tags are
1799 shown when they are available, i.e. when the value of
1800 `org-agenda-use-tag-inheritance' has been taken into account.
1802 This can be set to a list of agenda types in which the agenda
1803 must display the inherited tags. Available types are 'todo,
1804 'agenda, 'search and 'timeline.
1806 When set to nil, never show inherited tags in agenda lines."
1807 :group 'org-agenda-line-format
1808 :group 'org-agenda
1809 :version "24.3"
1810 :type '(choice
1811 (const :tag "Show inherited tags when available" t)
1812 (const :tag "Always show inherited tags" always)
1813 (repeat :tag "Show inherited tags only in selected agenda types"
1814 (symbol :tag "Agenda type"))))
1816 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1817 "List of agenda view types where to use tag inheritance.
1819 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1820 controlled by `org-use-tag-inheritance'. In other agenda types,
1821 `org-use-tag-inheritance' is not used for the selection of the
1822 agenda entries. Still, you may want the agenda to be aware of
1823 the inherited tags anyway, e.g. for later tag filtering.
1825 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1827 This variable has no effect if `org-agenda-show-inherited-tags'
1828 is set to 'always. In that case, the agenda is aware of those
1829 tags.
1831 The default value sets tags in every agenda type. Setting this
1832 option to nil will speed up non-tags agenda view a lot."
1833 :group 'org-agenda
1834 :version "24.3"
1835 :type '(choice
1836 (const :tag "Use tag inheritance in all agenda types" t)
1837 (repeat :tag "Use tag inheritance in selected agenda types"
1838 (symbol :tag "Agenda type"))))
1840 (defcustom org-agenda-hide-tags-regexp nil
1841 "Regular expression used to filter away specific tags in agenda views.
1842 This means that these tags will be present, but not be shown in the agenda
1843 line. Secondary filtering will still work on the hidden tags.
1844 Nil means don't hide any tags."
1845 :group 'org-agenda-line-format
1846 :type '(choice
1847 (const :tag "Hide none" nil)
1848 (string :tag "Regexp ")))
1850 (defcustom org-agenda-remove-tags nil
1851 "Non-nil means remove the tags from the headline copy in the agenda.
1852 When this is the symbol `prefix', only remove tags when
1853 `org-agenda-prefix-format' contains a `%T' specifier."
1854 :group 'org-agenda-line-format
1855 :type '(choice
1856 (const :tag "Always" t)
1857 (const :tag "Never" nil)
1858 (const :tag "When prefix format contains %T" prefix)))
1860 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1861 'org-agenda-remove-tags)
1863 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1864 "Shift tags in agenda items to this column.
1865 If this number is positive, it specifies the column. If it is negative,
1866 it means that the tags should be flushright to that column. For example,
1867 -80 works well for a normal 80 character screen."
1868 :group 'org-agenda-line-format
1869 :type 'integer)
1871 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1873 (defcustom org-agenda-fontify-priorities 'cookies
1874 "Non-nil means highlight low and high priorities in agenda.
1875 When t, the highest priority entries are bold, lowest priority italic.
1876 However, settings in `org-priority-faces' will overrule these faces.
1877 When this variable is the symbol `cookies', only fontify the
1878 cookies, not the entire task.
1879 This may also be an association list of priority faces, whose
1880 keys are the character values of `org-highest-priority',
1881 `org-default-priority', and `org-lowest-priority' (the default values
1882 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1883 color as a string, or a list like `(:background \"Red\")'.
1884 If it is a color, the variable `org-faces-easy-properties'
1885 determines if it is a foreground or a background color."
1886 :group 'org-agenda-line-format
1887 :type '(choice
1888 (const :tag "Never" nil)
1889 (const :tag "Defaults" t)
1890 (const :tag "Cookies only" cookies)
1891 (repeat :tag "Specify"
1892 (list (character :tag "Priority" :value ?A)
1893 (choice :tag "Face "
1894 (string :tag "Color")
1895 (sexp :tag "Face"))))))
1897 (defcustom org-agenda-day-face-function nil
1898 "Function called to determine what face should be used to display a day.
1899 The only argument passed to that function is the day. It should
1900 returns a face, or nil if does not want to specify a face and let
1901 the normal rules apply."
1902 :group 'org-agenda-line-format
1903 :version "24.1"
1904 :type '(choice (const nil) (function)))
1906 (defcustom org-agenda-category-icon-alist nil
1907 "Alist of category icon to be displayed in agenda views.
1909 Each entry should have the following format:
1911 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1913 Where CATEGORY-REGEXP is a regexp matching the categories where
1914 the icon should be displayed.
1915 FILE-OR-DATA either a file path or a string containing image data.
1917 The other fields can be omitted safely if not needed:
1918 TYPE indicates the image type.
1919 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1920 image data.
1921 PROPS are additional image attributes to assign to the image,
1922 like, e.g. `:ascent center'.
1924 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1926 If you want to set the display properties yourself, just put a
1927 list as second element:
1929 (CATEGORY-REGEXP (MY PROPERTY LIST))
1931 For example, to display a 16px horizontal space for Emacs
1932 category, you can use:
1934 (\"Emacs\" '(space . (:width (16))))"
1935 :group 'org-agenda-line-format
1936 :version "24.1"
1937 :type '(alist :key-type (string :tag "Regexp matching category")
1938 :value-type (choice (list :tag "Icon"
1939 (string :tag "File or data")
1940 (symbol :tag "Type")
1941 (boolean :tag "Data?")
1942 (repeat :tag "Extra image properties" :inline t symbol))
1943 (list :tag "Display properties" sexp))))
1945 (defgroup org-agenda-column-view nil
1946 "Options concerning column view in the agenda."
1947 :tag "Org Agenda Column View"
1948 :group 'org-agenda)
1950 (defcustom org-agenda-columns-show-summaries t
1951 "Non-nil means show summaries for columns displayed in the agenda view."
1952 :group 'org-agenda-column-view
1953 :type 'boolean)
1955 (defcustom org-agenda-columns-compute-summary-properties t
1956 "Non-nil means recompute all summary properties before column view.
1957 When column view in the agenda is listing properties that have a summary
1958 operator, it can go to all relevant buffers and recompute the summaries
1959 there. This can mean overhead for the agenda column view, but is necessary
1960 to have thing up to date.
1961 As a special case, a CLOCKSUM property also makes sure that the clock
1962 computations are current."
1963 :group 'org-agenda-column-view
1964 :type 'boolean)
1966 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1967 "Non-nil means the duration of an appointment will add to day effort.
1968 The property to which appointment durations will be added is the one given
1969 in the option `org-effort-property'. If an appointment does not have
1970 an end time, `org-agenda-default-appointment-duration' will be used. If that
1971 is not set, an appointment without end time will not contribute to the time
1972 estimate."
1973 :group 'org-agenda-column-view
1974 :type 'boolean)
1976 (defcustom org-agenda-auto-exclude-function nil
1977 "A function called with a tag to decide if it is filtered on '/ RET'.
1978 The sole argument to the function, which is called once for each
1979 possible tag, is a string giving the name of the tag. The
1980 function should return either nil if the tag should be included
1981 as normal, or \"-<TAG>\" to exclude the tag.
1982 Note that for the purpose of tag filtering, only the lower-case version of
1983 all tags will be considered, so that this function will only ever see
1984 the lower-case version of all tags."
1985 :group 'org-agenda
1986 :type '(choice (const nil) (function)))
1988 (defcustom org-agenda-bulk-custom-functions nil
1989 "Alist of characters and custom functions for bulk actions.
1990 For example, this value makes those two functions available:
1992 '((?R set-category)
1993 (?C bulk-cut))
1995 With selected entries in an agenda buffer, `B R' will call
1996 the custom function `set-category' on the selected entries.
1997 Note that functions in this alist don't need to be quoted."
1998 :type 'alist
1999 :version "24.1"
2000 :group 'org-agenda)
2002 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2003 "Execute BODY with point at location given by `org-hd-marker' property.
2004 If STRING is non-nil, the text property will be fetched from position 0
2005 in that string. If STRING is nil, it will be fetched from the beginning
2006 of the current line."
2007 (org-with-gensyms (marker)
2008 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2009 'org-hd-marker ,string)))
2010 (with-current-buffer (marker-buffer ,marker)
2011 (save-excursion
2012 (goto-char ,marker)
2013 ,@body)))))
2014 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2016 (defun org-add-agenda-custom-command (entry)
2017 "Replace or add a command in `org-agenda-custom-commands'.
2018 This is mostly for hacking and trying a new command - once the command
2019 works you probably want to add it to `org-agenda-custom-commands' for good."
2020 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2021 (if ass
2022 (setcdr ass (cdr entry))
2023 (push entry org-agenda-custom-commands))))
2025 ;;; Define the org-agenda-mode
2027 (defvar org-agenda-mode-map (make-sparse-keymap)
2028 "Keymap for `org-agenda-mode'.")
2029 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2031 (defvar org-agenda-menu) ; defined later in this file.
2032 (defvar org-agenda-restrict nil) ; defined later in this file.
2033 (defvar org-agenda-follow-mode nil)
2034 (defvar org-agenda-entry-text-mode nil)
2035 (defvar org-agenda-clockreport-mode nil)
2036 (defvar org-agenda-show-log nil)
2037 (defvar org-agenda-redo-command nil)
2038 (defvar org-agenda-query-string nil)
2039 (defvar org-agenda-mode-hook nil
2040 "Hook run after `org-agenda-mode' is turned on.
2041 The buffer is still writable when this hook is called.")
2042 (defvar org-agenda-type nil)
2043 (defvar org-agenda-force-single-file nil)
2044 (defvar org-agenda-bulk-marked-entries nil
2045 "List of markers that refer to marked entries in the agenda.")
2047 ;;; Multiple agenda buffers support
2049 (defcustom org-agenda-sticky nil
2050 "Non-nil means agenda q key will bury agenda buffers.
2051 Agenda commands will then show existing buffer instead of generating new ones.
2052 When nil, `q' will kill the single agenda buffer."
2053 :group 'org-agenda
2054 :version "24.3"
2055 :type 'boolean)
2058 ;;;###autoload
2059 (defun org-toggle-sticky-agenda (&optional arg)
2060 "Toggle `org-agenda-sticky'."
2061 (interactive "P")
2062 (let ((new-value (if arg
2063 (> (prefix-numeric-value arg) 0)
2064 (not org-agenda-sticky))))
2065 (if (equal new-value org-agenda-sticky)
2066 (and (org-called-interactively-p 'interactive)
2067 (message "Sticky agenda was already %s"
2068 (if org-agenda-sticky "enabled" "disabled")))
2069 (setq org-agenda-sticky new-value)
2070 (org-agenda-kill-all-agenda-buffers)
2071 (and (org-called-interactively-p 'interactive)
2072 (message "Sticky agenda was %s"
2073 (if org-agenda-sticky "enabled" "disabled"))))))
2075 (defvar org-agenda-buffer nil
2076 "Agenda buffer currently being generated.")
2078 (defvar org-agenda-last-prefix-arg nil)
2079 (defvar org-agenda-this-buffer-name nil)
2080 (defvar org-agenda-doing-sticky-redo nil)
2081 (defvar org-agenda-this-buffer-is-sticky nil)
2083 (defconst org-agenda-local-vars
2084 '(org-agenda-this-buffer-name
2085 org-agenda-undo-list
2086 org-agenda-pending-undo-list
2087 org-agenda-follow-mode
2088 org-agenda-entry-text-mode
2089 org-agenda-clockreport-mode
2090 org-agenda-show-log
2091 org-agenda-redo-command
2092 org-agenda-query-string
2093 org-agenda-type
2094 org-agenda-bulk-marked-entries
2095 org-agenda-undo-has-started-in
2096 org-agenda-info
2097 org-agenda-pre-window-conf
2098 org-agenda-columns-active
2099 org-agenda-tag-filter
2100 org-agenda-category-filter
2101 org-agenda-top-headline-filter
2102 org-agenda-regexp-filter
2103 org-agenda-markers
2104 org-agenda-last-search-view-search-was-boolean
2105 org-agenda-filtered-by-category
2106 org-agenda-filter-form
2107 org-agenda-cycle-counter
2108 org-agenda-last-prefix-arg)
2109 "Variables that must be local in agenda buffers to allow multiple buffers.")
2111 (defun org-agenda-mode ()
2112 "Mode for time-sorted view on action items in Org-mode files.
2114 The following commands are available:
2116 \\{org-agenda-mode-map}"
2117 (interactive)
2118 (cond (org-agenda-doing-sticky-redo
2119 ;; Refreshing sticky agenda-buffer
2121 ;; Preserve the value of `org-agenda-local-vars' variables,
2122 ;; while letting `kill-all-local-variables' kill the rest
2123 (let ((save (buffer-local-variables)))
2124 (kill-all-local-variables)
2125 (mapc 'make-local-variable org-agenda-local-vars)
2126 (dolist (elem save)
2127 (let ((var (car elem))
2128 (val (cdr elem)))
2129 (when (and val
2130 (member var org-agenda-local-vars))
2131 (set var val)))))
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2133 (org-agenda-sticky
2134 ;; Creating a sticky Agenda buffer for the first time
2135 (kill-all-local-variables)
2136 (mapc 'make-local-variable org-agenda-local-vars)
2137 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2139 ;; Creating a non-sticky agenda buffer
2140 (kill-all-local-variables)
2141 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2142 (setq org-agenda-undo-list nil
2143 org-agenda-pending-undo-list nil
2144 org-agenda-bulk-marked-entries nil)
2145 (setq major-mode 'org-agenda-mode)
2146 ;; Keep global-font-lock-mode from turning on font-lock-mode
2147 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2148 (setq mode-name "Org-Agenda")
2149 (setq indent-tabs-mode nil)
2150 (use-local-map org-agenda-mode-map)
2151 (easy-menu-add org-agenda-menu)
2152 (if org-startup-truncated (setq truncate-lines t))
2153 (org-set-local 'line-move-visual nil)
2154 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2155 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2156 ;; Make sure properties are removed when copying text
2157 (org-add-hook 'filter-buffer-substring-functions
2158 (lambda (fun start end delete)
2159 (substring-no-properties (funcall fun start end delete)))
2160 nil t)
2161 (unless org-agenda-keep-modes
2162 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2163 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2164 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2165 org-agenda-show-log org-agenda-start-with-log-mode))
2166 (add-to-invisibility-spec '(org-filtered))
2167 (add-to-invisibility-spec '(org-link))
2168 (easy-menu-change
2169 '("Agenda") "Agenda Files"
2170 (append
2171 (list
2172 (vector
2173 (if (get 'org-agenda-files 'org-restrict)
2174 "Restricted to single file"
2175 "Edit File List")
2176 '(org-edit-agenda-file-list)
2177 (not (get 'org-agenda-files 'org-restrict)))
2178 "--")
2179 (mapcar 'org-file-menu-entry (org-agenda-files))))
2180 (org-agenda-set-mode-name)
2181 (apply
2182 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2183 (list 'org-agenda-mode-hook)))
2185 (substitute-key-definition 'undo 'org-agenda-undo
2186 org-agenda-mode-map global-map)
2187 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2188 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2189 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2190 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2191 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2192 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2193 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2194 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2195 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2196 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2197 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2198 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2199 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2200 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2201 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2202 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2203 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2204 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2205 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2206 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2207 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2208 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2209 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2210 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2211 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2212 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2213 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2214 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2215 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2216 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2217 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2218 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2219 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2220 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2221 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2222 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2223 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2224 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2225 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2226 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2227 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2228 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2229 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2230 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2231 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2232 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2233 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2234 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2235 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2236 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2238 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2239 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2240 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2241 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2242 (while l (org-defkey org-agenda-mode-map
2243 (int-to-string (pop l)) 'digit-argument)))
2245 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2246 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2247 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2248 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2249 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2250 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2251 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2252 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2253 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2254 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2255 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2256 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2257 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2258 'org-clock-modify-effort-estimate)
2259 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2260 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2261 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2262 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2263 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2264 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2265 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2266 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2267 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2268 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2269 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2270 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2271 (substitute-key-definition 'next-line 'org-agenda-next-line
2272 org-agenda-mode-map global-map)
2273 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2274 org-agenda-mode-map global-map)
2275 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2276 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2277 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2278 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2279 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2280 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2281 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2282 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2283 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2284 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2285 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2286 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2287 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2288 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2289 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2290 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2291 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2292 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2293 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2294 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2295 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2296 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2297 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2298 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2299 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2300 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2301 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2302 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2303 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2304 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2306 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2307 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2308 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2309 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2310 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2311 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2312 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2313 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2314 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2315 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2316 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2317 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2318 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2319 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2320 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2322 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2323 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2324 (when org-agenda-mouse-1-follows-link
2325 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2326 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2327 '("Agenda"
2328 ("Agenda Files")
2329 "--"
2330 ("Agenda Dates"
2331 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2332 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2333 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2334 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2335 "--"
2336 ("View"
2337 ["Day View" org-agenda-day-view
2338 :active (org-agenda-check-type nil 'agenda)
2339 :style radio :selected (eq org-agenda-current-span 'day)
2340 :keys "v d (or just d)"]
2341 ["Week View" org-agenda-week-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'week)
2344 :keys "v w"]
2345 ["Fortnight View" org-agenda-fortnight-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'fortnight)
2348 :keys "v f"]
2349 ["Month View" org-agenda-month-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'month)
2352 :keys "v m"]
2353 ["Year View" org-agenda-year-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'year)
2356 :keys "v y"]
2357 "--"
2358 ["Include Diary" org-agenda-toggle-diary
2359 :style toggle :selected org-agenda-include-diary
2360 :active (org-agenda-check-type nil 'agenda)]
2361 ["Include Deadlines" org-agenda-toggle-deadlines
2362 :style toggle :selected org-agenda-include-deadlines
2363 :active (org-agenda-check-type nil 'agenda)]
2364 ["Use Time Grid" org-agenda-toggle-time-grid
2365 :style toggle :selected org-agenda-use-time-grid
2366 :active (org-agenda-check-type nil 'agenda)]
2367 "--"
2368 ["Show clock report" org-agenda-clockreport-mode
2369 :style toggle :selected org-agenda-clockreport-mode
2370 :active (org-agenda-check-type nil 'agenda)]
2371 ["Show some entry text" org-agenda-entry-text-mode
2372 :style toggle :selected org-agenda-entry-text-mode
2373 :active t]
2374 "--"
2375 ["Show Logbook entries" org-agenda-log-mode
2376 :style toggle :selected org-agenda-show-log
2377 :active (org-agenda-check-type nil 'agenda 'timeline)
2378 :keys "v l (or just l)"]
2379 ["Include archived trees" org-agenda-archives-mode
2380 :style toggle :selected org-agenda-archives-mode :active t
2381 :keys "v a"]
2382 ["Include archive files" (org-agenda-archives-mode t)
2383 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2384 :keys "v A"]
2385 "--"
2386 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2387 ["Write view to file" org-agenda-write t]
2388 ["Rebuild buffer" org-agenda-redo t]
2389 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2390 "--"
2391 ["Show original entry" org-agenda-show t]
2392 ["Go To (other window)" org-agenda-goto t]
2393 ["Go To (this window)" org-agenda-switch-to t]
2394 ["Capture with cursor date" org-agenda-capture t]
2395 ["Follow Mode" org-agenda-follow-mode
2396 :style toggle :selected org-agenda-follow-mode :active t]
2397 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2398 "--"
2399 ("TODO"
2400 ["Cycle TODO" org-agenda-todo t]
2401 ["Next TODO set" org-agenda-todo-nextset t]
2402 ["Previous TODO set" org-agenda-todo-previousset t]
2403 ["Add note" org-agenda-add-note t])
2404 ("Archive/Refile/Delete"
2405 ["Archive default" org-agenda-archive-default t]
2406 ["Archive default" org-agenda-archive-default-with-confirmation t]
2407 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2408 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2409 ["Archive subtree" org-agenda-archive t]
2410 "--"
2411 ["Refile" org-agenda-refile t]
2412 "--"
2413 ["Delete subtree" org-agenda-kill t])
2414 ("Bulk action"
2415 ["Mark entry" org-agenda-bulk-mark t]
2416 ["Mark all" org-agenda-bulk-mark-all t]
2417 ["Unmark entry" org-agenda-bulk-unmark t]
2418 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2419 ["Toggle mark" org-agenda-bulk-toggle t]
2420 ["Toggle all" org-agenda-bulk-toggle-all t]
2421 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2422 ["Act on all marked" org-agenda-bulk-action t]
2423 "--"
2424 ("Tags and Properties"
2425 ["Show all Tags" org-agenda-show-tags t]
2426 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2427 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2428 "--"
2429 ["Column View" org-columns t])
2430 ("Deadline/Schedule"
2431 ["Schedule" org-agenda-schedule t]
2432 ["Set Deadline" org-agenda-deadline t]
2433 "--"
2434 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2435 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2436 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2437 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2438 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2439 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2440 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2441 ("Clock and Effort"
2442 ["Clock in" org-agenda-clock-in t]
2443 ["Clock out" org-agenda-clock-out t]
2444 ["Clock cancel" org-agenda-clock-cancel t]
2445 ["Goto running clock" org-clock-goto t]
2446 "--"
2447 ["Set Effort" org-agenda-set-effort t]
2448 ["Change clocked effort" org-clock-modify-effort-estimate
2449 (org-clock-is-active)])
2450 ("Priority"
2451 ["Set Priority" org-agenda-priority t]
2452 ["Increase Priority" org-agenda-priority-up t]
2453 ["Decrease Priority" org-agenda-priority-down t]
2454 ["Show Priority" org-show-priority t])
2455 ("Calendar/Diary"
2456 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2457 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2458 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2459 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2460 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2462 "--"
2463 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2464 "--"
2465 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2466 "--"
2467 ("MobileOrg"
2468 ["Push Files and Views" org-mobile-push t]
2469 ["Get Captured and Flagged" org-mobile-pull t]
2470 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2471 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2472 "--"
2473 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2474 "--"
2475 ["Quit" org-agenda-quit t]
2476 ["Exit and Release Buffers" org-agenda-exit t]
2479 ;;; Agenda undo
2481 (defvar org-agenda-allow-remote-undo t
2482 "Non-nil means allow remote undo from the agenda buffer.")
2483 (defvar org-agenda-undo-has-started-in nil
2484 "Buffers that have already seen `undo-start' in the current undo sequence.")
2486 (defun org-agenda-undo ()
2487 "Undo a remote editing step in the agenda.
2488 This undoes changes both in the agenda buffer and in the remote buffer
2489 that have been changed along."
2490 (interactive)
2491 (or org-agenda-allow-remote-undo
2492 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2493 (if (not (eq this-command last-command))
2494 (setq org-agenda-undo-has-started-in nil
2495 org-agenda-pending-undo-list org-agenda-undo-list))
2496 (if (not org-agenda-pending-undo-list)
2497 (user-error "No further undo information"))
2498 (let* ((entry (pop org-agenda-pending-undo-list))
2499 buf line cmd rembuf)
2500 (setq cmd (pop entry) line (pop entry))
2501 (setq rembuf (nth 2 entry))
2502 (org-with-remote-undo rembuf
2503 (while (bufferp (setq buf (pop entry)))
2504 (if (pop entry)
2505 (with-current-buffer buf
2506 (let ((last-undo-buffer buf)
2507 (inhibit-read-only t))
2508 (unless (memq buf org-agenda-undo-has-started-in)
2509 (push buf org-agenda-undo-has-started-in)
2510 (make-local-variable 'pending-undo-list)
2511 (undo-start))
2512 (while (and pending-undo-list
2513 (listp pending-undo-list)
2514 (not (car pending-undo-list)))
2515 (pop pending-undo-list))
2516 (undo-more 1))))))
2517 (org-goto-line line)
2518 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2520 (defun org-verify-change-for-undo (l1 l2)
2521 "Verify that a real change occurred between the undo lists L1 and L2."
2522 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2523 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2524 (not (eq l1 l2)))
2526 ;;; Agenda dispatch
2528 (defvar org-agenda-restrict-begin (make-marker))
2529 (defvar org-agenda-restrict-end (make-marker))
2530 (defvar org-agenda-last-dispatch-buffer nil)
2531 (defvar org-agenda-overriding-restriction nil)
2533 (defcustom org-agenda-custom-commands-contexts nil
2534 "Alist of custom agenda keys and contextual rules.
2536 For example, if you have a custom agenda command \"p\" and you
2537 want this command to be accessible only from plain text files,
2538 use this:
2540 '((\"p\" ((in-file . \"\\.txt\"))))
2542 Here are the available contexts definitions:
2544 in-file: command displayed only in matching files
2545 in-mode: command displayed only in matching modes
2546 not-in-file: command not displayed in matching files
2547 not-in-mode: command not displayed in matching modes
2548 in-buffer: command displayed only in matching buffers
2549 not-in-buffer: command not displayed in matching buffers
2550 [function]: a custom function taking no argument
2552 If you define several checks, the agenda command will be
2553 accessible if there is at least one valid check.
2555 You can also bind a key to another agenda custom command
2556 depending on contextual rules.
2558 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2560 Here it means: in .txt files, use \"p\" as the key for the
2561 agenda command otherwise associated with \"q\". (The command
2562 originally associated with \"q\" is not displayed to avoid
2563 duplicates.)"
2564 :version "24.3"
2565 :group 'org-agenda-custom-commands
2566 :type '(repeat (list :tag "Rule"
2567 (string :tag " Agenda key")
2568 (string :tag "Replace by command")
2569 (repeat :tag "Available when"
2570 (choice
2571 (cons :tag "Condition"
2572 (choice
2573 (const :tag "In file" in-file)
2574 (const :tag "Not in file" not-in-file)
2575 (const :tag "In buffer" in-buffer)
2576 (const :tag "Not in buffer" not-in-buffer)
2577 (const :tag "In mode" in-mode)
2578 (const :tag "Not in mode" not-in-mode))
2579 (regexp))
2580 (function :tag "Custom function"))))))
2582 (defcustom org-agenda-max-entries nil
2583 "Maximum number of entries to display in an agenda.
2584 This can be nil (no limit) or an integer or an alist of agenda
2585 types with an associated number of entries to display in this
2586 type."
2587 :version "24.4"
2588 :package-version '(Org . "8.0")
2589 :group 'org-agenda-custom-commands
2590 :type '(choice (symbol :tag "No limit" nil)
2591 (integer :tag "Max number of entries")
2592 (repeat
2593 (cons (choice :tag "Agenda type"
2594 (const agenda)
2595 (const todo)
2596 (const tags)
2597 (const search)
2598 (const timeline))
2599 (integer :tag "Max number of entries")))))
2601 (defcustom org-agenda-max-todos nil
2602 "Maximum number of TODOs to display in an agenda.
2603 This can be nil (no limit) or an integer or an alist of agenda
2604 types with an associated number of entries to display in this
2605 type."
2606 :version "24.4"
2607 :package-version '(Org . "8.0")
2608 :group 'org-agenda-custom-commands
2609 :type '(choice (symbol :tag "No limit" nil)
2610 (integer :tag "Max number of TODOs")
2611 (repeat
2612 (cons (choice :tag "Agenda type"
2613 (const agenda)
2614 (const todo)
2615 (const tags)
2616 (const search)
2617 (const timeline))
2618 (integer :tag "Max number of TODOs")))))
2620 (defcustom org-agenda-max-tags nil
2621 "Maximum number of tagged entries to display in an agenda.
2622 This can be nil (no limit) or an integer or an alist of agenda
2623 types with an associated number of entries to display in this
2624 type."
2625 :version "24.4"
2626 :package-version '(Org . "8.0")
2627 :group 'org-agenda-custom-commands
2628 :type '(choice (symbol :tag "No limit" nil)
2629 (integer :tag "Max number of tagged entries")
2630 (repeat
2631 (cons (choice :tag "Agenda type"
2632 (const agenda)
2633 (const todo)
2634 (const tags)
2635 (const search)
2636 (const timeline))
2637 (integer :tag "Max number of tagged entries")))))
2639 (defcustom org-agenda-max-effort nil
2640 "Maximum cumulated effort duration for the agenda.
2641 This can be nil (no limit) or a number of minutes (as an integer)
2642 or an alist of agenda types with an associated number of minutes
2643 to limit entries to in this type."
2644 :version "24.4"
2645 :package-version '(Org . "8.0")
2646 :group 'org-agenda-custom-commands
2647 :type '(choice (symbol :tag "No limit" nil)
2648 (integer :tag "Max number of minutes")
2649 (repeat
2650 (cons (choice :tag "Agenda type"
2651 (const agenda)
2652 (const todo)
2653 (const tags)
2654 (const search)
2655 (const timeline))
2656 (integer :tag "Max number of minutes")))))
2658 (defvar org-keys nil)
2659 (defvar org-match nil)
2660 ;;;###autoload
2661 (defun org-agenda (&optional arg org-keys restriction)
2662 "Dispatch agenda commands to collect entries to the agenda buffer.
2663 Prompts for a command to execute. Any prefix arg will be passed
2664 on to the selected command. The default selections are:
2666 a Call `org-agenda-list' to display the agenda for current day or week.
2667 t Call `org-todo-list' to display the global todo list.
2668 T Call `org-todo-list' to display the global todo list, select only
2669 entries with a specific TODO keyword (the user gets a prompt).
2670 m Call `org-tags-view' to display headlines with tags matching
2671 a condition (the user is prompted for the condition).
2672 M Like `m', but select only TODO entries, no ordinary headlines.
2673 L Create a timeline for the current buffer.
2674 e Export views to associated files.
2675 s Search entries for keywords.
2676 S Search entries for keywords, only with TODO keywords.
2677 / Multi occur across all agenda files and also files listed
2678 in `org-agenda-text-search-extra-files'.
2679 < Restrict agenda commands to buffer, subtree, or region.
2680 Press several times to get the desired effect.
2681 > Remove a previous restriction.
2682 # List \"stuck\" projects.
2683 ! Configure what \"stuck\" means.
2684 C Configure custom agenda commands.
2686 More commands can be added by configuring the variable
2687 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2688 searches can be pre-defined in this way.
2690 If the current buffer is in Org-mode and visiting a file, you can also
2691 first press `<' once to indicate that the agenda should be temporarily
2692 \(until the next use of \\[org-agenda]) restricted to the current file.
2693 Pressing `<' twice means to restrict to the current subtree or region
2694 \(if active)."
2695 (interactive "P")
2696 (catch 'exit
2697 (let* ((prefix-descriptions nil)
2698 (org-agenda-buffer-name org-agenda-buffer-name)
2699 (org-agenda-window-setup (if (equal (buffer-name)
2700 org-agenda-buffer-name)
2701 'current-window
2702 org-agenda-window-setup))
2703 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2704 (org-agenda-custom-commands
2705 ;; normalize different versions
2706 (delq nil
2707 (mapcar
2708 (lambda (x)
2709 (cond ((stringp (cdr x))
2710 (push x prefix-descriptions)
2711 nil)
2712 ((stringp (nth 1 x)) x)
2713 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2714 (t (cons (car x) (cons "" (cdr x))))))
2715 org-agenda-custom-commands)))
2716 (org-agenda-custom-commands
2717 (org-contextualize-keys
2718 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2719 (buf (current-buffer))
2720 (bfn (buffer-file-name (buffer-base-buffer)))
2721 entry key type org-match lprops ans)
2722 ;; Turn off restriction unless there is an overriding one,
2723 (unless org-agenda-overriding-restriction
2724 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2725 ;; There is a request to keep the file list in place
2726 (put 'org-agenda-files 'org-restrict nil))
2727 (setq org-agenda-restrict nil)
2728 (move-marker org-agenda-restrict-begin nil)
2729 (move-marker org-agenda-restrict-end nil))
2730 ;; Delete old local properties
2731 (put 'org-agenda-redo-command 'org-lprops nil)
2732 ;; Delete previously set last-arguments
2733 (put 'org-agenda-redo-command 'last-args nil)
2734 ;; Remember where this call originated
2735 (setq org-agenda-last-dispatch-buffer (current-buffer))
2736 (unless org-keys
2737 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2738 org-keys (car ans)
2739 restriction (cdr ans)))
2740 ;; If we have sticky agenda buffers, set a name for the buffer,
2741 ;; depending on the invoking keys. The user may still set this
2742 ;; as a command option, which will overwrite what we do here.
2743 (if org-agenda-sticky
2744 (setq org-agenda-buffer-name
2745 (format "*Org Agenda(%s)*" org-keys)))
2746 ;; Establish the restriction, if any
2747 (when (and (not org-agenda-overriding-restriction) restriction)
2748 (put 'org-agenda-files 'org-restrict (list bfn))
2749 (cond
2750 ((eq restriction 'region)
2751 (setq org-agenda-restrict (current-buffer))
2752 (move-marker org-agenda-restrict-begin (region-beginning))
2753 (move-marker org-agenda-restrict-end (region-end)))
2754 ((eq restriction 'subtree)
2755 (save-excursion
2756 (setq org-agenda-restrict (current-buffer))
2757 (org-back-to-heading t)
2758 (move-marker org-agenda-restrict-begin (point))
2759 (move-marker org-agenda-restrict-end
2760 (progn (org-end-of-subtree t)))))))
2762 ;; For example the todo list should not need it (but does...)
2763 (cond
2764 ((setq entry (assoc org-keys org-agenda-custom-commands))
2765 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2766 (progn
2767 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2768 lprops (nth 4 entry))
2769 (if org-agenda-sticky
2770 (setq org-agenda-buffer-name
2771 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2772 (format "*Org Agenda(%s)*" org-keys))))
2773 (put 'org-agenda-redo-command 'org-lprops lprops)
2774 (cond
2775 ((eq type 'agenda)
2776 (org-let lprops '(org-agenda-list current-prefix-arg)))
2777 ((eq type 'agenda*)
2778 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2779 ((eq type 'alltodo)
2780 (org-let lprops '(org-todo-list current-prefix-arg)))
2781 ((eq type 'search)
2782 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2783 ((eq type 'stuck)
2784 (org-let lprops '(org-agenda-list-stuck-projects
2785 current-prefix-arg)))
2786 ((eq type 'tags)
2787 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2788 ((eq type 'tags-todo)
2789 (org-let lprops '(org-tags-view '(4) org-match)))
2790 ((eq type 'todo)
2791 (org-let lprops '(org-todo-list org-match)))
2792 ((eq type 'tags-tree)
2793 (org-check-for-org-mode)
2794 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2795 ((eq type 'todo-tree)
2796 (org-check-for-org-mode)
2797 (org-let lprops
2798 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2799 (regexp-quote org-match) "\\>"))))
2800 ((eq type 'occur-tree)
2801 (org-check-for-org-mode)
2802 (org-let lprops '(org-occur org-match)))
2803 ((functionp type)
2804 (org-let lprops '(funcall type org-match)))
2805 ((fboundp type)
2806 (org-let lprops '(funcall type org-match)))
2807 (t (user-error "Invalid custom agenda command type %s" type))))
2808 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2809 ((equal org-keys "C")
2810 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2811 (customize-variable 'org-agenda-custom-commands))
2812 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2813 ((equal org-keys "s") (call-interactively 'org-search-view))
2814 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2815 ((equal org-keys "t") (call-interactively 'org-todo-list))
2816 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2817 ((equal org-keys "m") (call-interactively 'org-tags-view))
2818 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2819 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2820 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2821 (org-add-hook
2822 'post-command-hook
2823 (lambda ()
2824 (unless (current-message)
2825 (let* ((m (org-agenda-get-any-marker))
2826 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2827 (when note
2828 (message (concat
2829 "FLAGGING-NOTE ([?] for more info): "
2830 (org-add-props
2831 (replace-regexp-in-string
2832 "\\\\n" "//"
2833 (copy-sequence note))
2834 nil 'face 'org-warning)))))))
2835 t t))
2836 ((equal org-keys "L")
2837 (unless (derived-mode-p 'org-mode)
2838 (user-error "This is not an Org-mode file"))
2839 (unless restriction
2840 (put 'org-agenda-files 'org-restrict (list bfn))
2841 (org-call-with-arg 'org-timeline arg)))
2842 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2843 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2844 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2845 (t (user-error "Invalid agenda key"))))))
2847 (defvar org-agenda-multi)
2849 (defun org-agenda-append-agenda ()
2850 "Append another agenda view to the current one.
2851 This function allows interactive building of block agendas.
2852 Agenda views are separated by `org-agenda-block-separator'."
2853 (interactive)
2854 (unless (derived-mode-p 'org-agenda-mode)
2855 (user-error "Can only append from within agenda buffer"))
2856 (let ((org-agenda-multi t))
2857 (org-agenda)
2858 (widen)
2859 (org-agenda-finalize)
2860 (setq buffer-read-only t)
2861 (org-agenda-fit-window-to-buffer)))
2863 (defun org-agenda-normalize-custom-commands (cmds)
2864 "Normalize custom commands CMDS."
2865 (delq nil
2866 (mapcar
2867 (lambda (x)
2868 (cond ((stringp (cdr x)) nil)
2869 ((stringp (nth 1 x)) x)
2870 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2871 (t (cons (car x) (cons "" (cdr x))))))
2872 cmds)))
2874 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2875 "The user interface for selecting an agenda command."
2876 (catch 'exit
2877 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2878 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2879 (region-p (org-region-active-p))
2880 (custom org-agenda-custom-commands)
2881 (selstring "")
2882 restriction second-time
2883 c entry key type match prefixes rmheader header-end custom1 desc
2884 line lines left right n n1)
2885 (save-window-excursion
2886 (delete-other-windows)
2887 (org-switch-to-buffer-other-window " *Agenda Commands*")
2888 (erase-buffer)
2889 (insert (eval-when-compile
2890 (let ((header
2891 "Press key for an agenda command: < Buffer, subtree/region restriction
2892 -------------------------------- > Remove restriction
2893 a Agenda for current week or day e Export agenda views
2894 t List of all TODO entries T Entries with special TODO kwd
2895 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2896 s Search for keywords S Like s, but only TODO entries
2897 L Timeline for current buffer # List stuck projects (!=configure)
2898 / Multi-occur C Configure custom agenda commands
2899 ? Find :FLAGGED: entries * Toggle sticky agenda views
2901 (start 0))
2902 (while (string-match
2903 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2904 header start)
2905 (setq start (match-end 0))
2906 (add-text-properties (match-beginning 2) (match-end 2)
2907 '(face bold) header))
2908 header)))
2909 (setq header-end (point-marker))
2910 (while t
2911 (setq custom1 custom)
2912 (when (eq rmheader t)
2913 (org-goto-line 1)
2914 (re-search-forward ":" nil t)
2915 (delete-region (match-end 0) (point-at-eol))
2916 (forward-char 1)
2917 (looking-at "-+")
2918 (delete-region (match-end 0) (point-at-eol))
2919 (move-marker header-end (match-end 0)))
2920 (goto-char header-end)
2921 (delete-region (point) (point-max))
2923 ;; Produce all the lines that describe custom commands and prefixes
2924 (setq lines nil)
2925 (while (setq entry (pop custom1))
2926 (setq key (car entry) desc (nth 1 entry)
2927 type (nth 2 entry)
2928 match (nth 3 entry))
2929 (if (> (length key) 1)
2930 (add-to-list 'prefixes (string-to-char key))
2931 (setq line
2932 (format
2933 "%-4s%-14s"
2934 (org-add-props (copy-sequence key)
2935 '(face bold))
2936 (cond
2937 ((string-match "\\S-" desc) desc)
2938 ((eq type 'agenda) "Agenda for current week or day")
2939 ((eq type 'agenda*) "Appointments for current week or day")
2940 ((eq type 'alltodo) "List of all TODO entries")
2941 ((eq type 'search) "Word search")
2942 ((eq type 'stuck) "List of stuck projects")
2943 ((eq type 'todo) "TODO keyword")
2944 ((eq type 'tags) "Tags query")
2945 ((eq type 'tags-todo) "Tags (TODO)")
2946 ((eq type 'tags-tree) "Tags tree")
2947 ((eq type 'todo-tree) "TODO kwd tree")
2948 ((eq type 'occur-tree) "Occur tree")
2949 ((functionp type) (if (symbolp type)
2950 (symbol-name type)
2951 "Lambda expression"))
2952 (t "???"))))
2953 (if org-agenda-menu-show-matcher
2954 (setq line
2955 (concat line ": "
2956 (cond
2957 ((stringp match)
2958 (setq match (copy-sequence match))
2959 (org-add-props match nil 'face 'org-warning))
2960 ((listp type)
2961 (format "set of %d commands" (length type))))))
2962 (if (org-string-nw-p match)
2963 (add-text-properties
2964 0 (length line) (list 'help-echo
2965 (concat "Matcher: " match)) line)))
2966 (push line lines)))
2967 (setq lines (nreverse lines))
2968 (when prefixes
2969 (mapc (lambda (x)
2970 (push
2971 (format "%s %s"
2972 (org-add-props (char-to-string x)
2973 nil 'face 'bold)
2974 (or (cdr (assoc (concat selstring
2975 (char-to-string x))
2976 prefix-descriptions))
2977 "Prefix key"))
2978 lines))
2979 prefixes))
2981 ;; Check if we should display in two columns
2982 (if org-agenda-menu-two-columns
2983 (progn
2984 (setq n (length lines)
2985 n1 (+ (/ n 2) (mod n 2))
2986 right (nthcdr n1 lines)
2987 left (copy-sequence lines))
2988 (setcdr (nthcdr (1- n1) left) nil))
2989 (setq left lines right nil))
2990 (while left
2991 (insert "\n" (pop left))
2992 (when right
2993 (if (< (current-column) 40)
2994 (move-to-column 40 t)
2995 (insert " "))
2996 (insert (pop right))))
2998 ;; Make the window the right size
2999 (goto-char (point-min))
3000 (if second-time
3001 (if (not (pos-visible-in-window-p (point-max)))
3002 (org-fit-window-to-buffer))
3003 (setq second-time t)
3004 (org-fit-window-to-buffer))
3006 ;; Ask for selection
3007 (message "Press key for agenda command%s:"
3008 (if (or restrict-ok org-agenda-overriding-restriction)
3009 (if org-agenda-overriding-restriction
3010 " (restriction lock active)"
3011 (if restriction
3012 (format " (restricted to %s)" restriction)
3013 " (unrestricted)"))
3014 ""))
3015 (setq c (read-char-exclusive))
3016 (message "")
3017 (cond
3018 ((assoc (char-to-string c) custom)
3019 (setq selstring (concat selstring (char-to-string c)))
3020 (throw 'exit (cons selstring restriction)))
3021 ((memq c prefixes)
3022 (setq selstring (concat selstring (char-to-string c))
3023 prefixes nil
3024 rmheader (or rmheader t)
3025 custom (delq nil (mapcar
3026 (lambda (x)
3027 (if (or (= (length (car x)) 1)
3028 (/= (string-to-char (car x)) c))
3030 (cons (substring (car x) 1) (cdr x))))
3031 custom))))
3032 ((eq c ?*)
3033 (call-interactively 'org-toggle-sticky-agenda)
3034 (sit-for 2))
3035 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3036 (message "Restriction is only possible in Org-mode buffers")
3037 (ding) (sit-for 1))
3038 ((eq c ?1)
3039 (org-agenda-remove-restriction-lock 'noupdate)
3040 (setq restriction 'buffer))
3041 ((eq c ?0)
3042 (org-agenda-remove-restriction-lock 'noupdate)
3043 (setq restriction (if region-p 'region 'subtree)))
3044 ((eq c ?<)
3045 (org-agenda-remove-restriction-lock 'noupdate)
3046 (setq restriction
3047 (cond
3048 ((eq restriction 'buffer)
3049 (if region-p 'region 'subtree))
3050 ((memq restriction '(subtree region))
3051 nil)
3052 (t 'buffer))))
3053 ((eq c ?>)
3054 (org-agenda-remove-restriction-lock 'noupdate)
3055 (setq restriction nil))
3056 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3057 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3058 ((and (> (length selstring) 0) (eq c ?\d))
3059 (delete-window)
3060 (org-agenda-get-restriction-and-command prefix-descriptions))
3062 ((equal c ?q) (error "Abort"))
3063 (t (user-error "Invalid key %c" c))))))))
3065 (defun org-agenda-fit-window-to-buffer ()
3066 "Fit the window to the buffer size."
3067 (and (memq org-agenda-window-setup '(reorganize-frame))
3068 (fboundp 'fit-window-to-buffer)
3069 (org-fit-window-to-buffer
3071 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3072 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3074 (defvar org-cmd nil)
3075 (defvar org-agenda-overriding-cmd nil)
3076 (defvar org-agenda-overriding-arguments nil)
3077 (defvar org-agenda-overriding-cmd-arguments nil)
3078 (defun org-agenda-run-series (name series)
3079 "Run agenda NAME as a SERIES of agenda commands."
3080 (org-let (nth 1 series) '(org-agenda-prepare name))
3081 ;; We need to reset agenda markers here, because when constructing a
3082 ;; block agenda, the individual blocks do not do that.
3083 (org-agenda-reset-markers)
3084 (let* ((org-agenda-multi t)
3085 (redo (list 'org-agenda-run-series name (list 'quote series)))
3086 (cmds (car series))
3087 (gprops (nth 1 series))
3088 match ;; The byte compiler incorrectly complains about this. Keep it!
3089 org-cmd type lprops)
3090 (while (setq org-cmd (pop cmds))
3091 (setq type (car org-cmd)
3092 match (eval (nth 1 org-cmd))
3093 lprops (nth 2 org-cmd))
3094 (let ((org-agenda-overriding-arguments
3095 (if (eq org-agenda-overriding-cmd org-cmd)
3096 (or org-agenda-overriding-arguments
3097 org-agenda-overriding-cmd-arguments))))
3098 (cond
3099 ((eq type 'agenda)
3100 (org-let2 gprops lprops
3101 '(call-interactively 'org-agenda-list)))
3102 ((eq type 'agenda*)
3103 (org-let2 gprops lprops
3104 '(funcall 'org-agenda-list nil nil t)))
3105 ((eq type 'alltodo)
3106 (org-let2 gprops lprops
3107 '(call-interactively 'org-todo-list)))
3108 ((eq type 'search)
3109 (org-let2 gprops lprops
3110 '(org-search-view current-prefix-arg match nil)))
3111 ((eq type 'stuck)
3112 (org-let2 gprops lprops
3113 '(call-interactively 'org-agenda-list-stuck-projects)))
3114 ((eq type 'tags)
3115 (org-let2 gprops lprops
3116 '(org-tags-view current-prefix-arg match)))
3117 ((eq type 'tags-todo)
3118 (org-let2 gprops lprops
3119 '(org-tags-view '(4) match)))
3120 ((eq type 'todo)
3121 (org-let2 gprops lprops
3122 '(org-todo-list match)))
3123 ((fboundp type)
3124 (org-let2 gprops lprops
3125 '(funcall type match)))
3126 (t (error "Invalid type in command series")))))
3127 (widen)
3128 (let ((inhibit-read-only t))
3129 (add-text-properties (point-min) (point-max)
3130 `(org-series t org-series-redo-cmd ,redo)))
3131 (setq org-agenda-redo-command redo)
3132 (goto-char (point-min)))
3133 (org-agenda-fit-window-to-buffer)
3134 (org-let (nth 1 series) '(org-agenda-finalize)))
3136 ;;;###autoload
3137 (defmacro org-batch-agenda (cmd-key &rest parameters)
3138 "Run an agenda command in batch mode and send the result to STDOUT.
3139 If CMD-KEY is a string of length 1, it is used as a key in
3140 `org-agenda-custom-commands' and triggers this command. If it is a
3141 longer string it is used as a tags/todo match string.
3142 Parameters are alternating variable names and values that will be bound
3143 before running the agenda command."
3144 (org-eval-in-environment (org-make-parameter-alist parameters)
3145 (let (org-agenda-sticky)
3146 (if (> (length cmd-key) 2)
3147 (org-tags-view nil cmd-key)
3148 (org-agenda nil cmd-key))))
3149 (set-buffer org-agenda-buffer-name)
3150 (princ (buffer-string)))
3152 (defvar org-agenda-info nil)
3154 ;;;###autoload
3155 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3156 "Run an agenda command in batch mode and send the result to STDOUT.
3157 If CMD-KEY is a string of length 1, it is used as a key in
3158 `org-agenda-custom-commands' and triggers this command. If it is a
3159 longer string it is used as a tags/todo match string.
3160 Parameters are alternating variable names and values that will be bound
3161 before running the agenda command.
3163 The output gives a line for each selected agenda item. Each
3164 item is a list of comma-separated values, like this:
3166 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3168 category The category of the item
3169 head The headline, without TODO kwd, TAGS and PRIORITY
3170 type The type of the agenda entry, can be
3171 todo selected in TODO match
3172 tagsmatch selected in tags match
3173 diary imported from diary
3174 deadline a deadline on given date
3175 scheduled scheduled on given date
3176 timestamp entry has timestamp on given date
3177 closed entry was closed on given date
3178 upcoming-deadline warning about deadline
3179 past-scheduled forwarded scheduled item
3180 block entry has date block including g. date
3181 todo The todo keyword, if any
3182 tags All tags including inherited ones, separated by colons
3183 date The relevant date, like 2007-2-14
3184 time The time, like 15:00-16:50
3185 extra Sting with extra planning info
3186 priority-l The priority letter if any was given
3187 priority-n The computed numerical priority
3188 agenda-day The day in the agenda where this is listed"
3189 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3190 (org-make-parameter-alist parameters))
3191 (if (> (length cmd-key) 2)
3192 (org-tags-view nil cmd-key)
3193 (org-agenda nil cmd-key)))
3194 (set-buffer org-agenda-buffer-name)
3195 (let* ((lines (org-split-string (buffer-string) "\n"))
3196 line)
3197 (while (setq line (pop lines))
3198 (catch 'next
3199 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3200 (setq org-agenda-info
3201 (org-fix-agenda-info (text-properties-at 0 line)))
3202 (princ
3203 (mapconcat 'org-agenda-export-csv-mapper
3204 '(org-category txt type todo tags date time extra
3205 priority-letter priority agenda-day)
3206 ","))
3207 (princ "\n")))))
3209 (defun org-fix-agenda-info (props)
3210 "Make sure all properties on an agenda item have a canonical form.
3211 This ensures the export commands can easily use it."
3212 (let (tmp re)
3213 (when (setq tmp (plist-get props 'tags))
3214 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3215 (when (setq tmp (plist-get props 'date))
3216 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3217 (let ((calendar-date-display-form '(year "-" month "-" day)))
3218 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3220 (setq tmp (calendar-date-string tmp)))
3221 (setq props (plist-put props 'date tmp)))
3222 (when (setq tmp (plist-get props 'day))
3223 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3224 (let ((calendar-date-display-form '(year "-" month "-" day)))
3225 (setq tmp (calendar-date-string tmp)))
3226 (setq props (plist-put props 'day tmp))
3227 (setq props (plist-put props 'agenda-day tmp)))
3228 (when (setq tmp (plist-get props 'txt))
3229 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3230 (plist-put props 'priority-letter (match-string 1 tmp))
3231 (setq tmp (replace-match "" t t tmp)))
3232 (when (and (setq re (plist-get props 'org-todo-regexp))
3233 (setq re (concat "\\`\\.*" re " ?"))
3234 (string-match re tmp))
3235 (plist-put props 'todo (match-string 1 tmp))
3236 (setq tmp (replace-match "" t t tmp)))
3237 (plist-put props 'txt tmp)))
3238 props)
3240 (defun org-agenda-export-csv-mapper (prop)
3241 (let ((res (plist-get org-agenda-info prop)))
3242 (setq res
3243 (cond
3244 ((not res) "")
3245 ((stringp res) res)
3246 (t (prin1-to-string res))))
3247 (while (string-match "," res)
3248 (setq res (replace-match ";" t t res)))
3249 (org-trim res)))
3251 ;;;###autoload
3252 (defun org-store-agenda-views (&rest parameters)
3253 "Store agenda views."
3254 (interactive)
3255 (eval (list 'org-batch-store-agenda-views)))
3257 ;;;###autoload
3258 (defmacro org-batch-store-agenda-views (&rest parameters)
3259 "Run all custom agenda commands that have a file argument."
3260 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3261 (pop-up-frames nil)
3262 (dir default-directory)
3263 (pars (org-make-parameter-alist parameters))
3264 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3265 (save-window-excursion
3266 (while cmds
3267 (setq cmd (pop cmds)
3268 thiscmdkey (car cmd)
3269 thiscmdcmd (cdr cmd)
3270 match (nth 2 thiscmdcmd)
3271 bufname (if org-agenda-sticky
3272 (or (and (stringp match)
3273 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3274 (format "*Org Agenda(%s)*" thiscmdkey))
3275 org-agenda-buffer-name)
3276 cmd-or-set (nth 2 cmd)
3277 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3278 files (nth (if (listp cmd-or-set) 4 5) cmd))
3279 (if (stringp files) (setq files (list files)))
3280 (when files
3281 (org-eval-in-environment (append org-agenda-exporter-settings
3282 opts pars)
3283 (org-agenda nil thiscmdkey))
3284 (set-buffer bufname)
3285 (while files
3286 (org-eval-in-environment (append org-agenda-exporter-settings
3287 opts pars)
3288 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3289 (and (get-buffer bufname)
3290 (kill-buffer bufname)))))))
3292 (defvar org-agenda-current-span nil
3293 "The current span used in the agenda view.") ; local variable in the agenda buffer
3294 (defun org-agenda-mark-header-line (pos)
3295 "Mark the line at POS as an agenda structure header."
3296 (save-excursion
3297 (goto-char pos)
3298 (put-text-property (point-at-bol) (point-at-eol)
3299 'org-agenda-structural-header t)
3300 (when org-agenda-title-append
3301 (put-text-property (point-at-bol) (point-at-eol)
3302 'org-agenda-title-append org-agenda-title-append))))
3304 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3305 (defvar org-agenda-write-buffer-name "Agenda View")
3306 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3307 "Write the current buffer (an agenda view) as a file.
3308 Depending on the extension of the file name, plain text (.txt),
3309 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3310 If the extension is .ics, run icalendar export over all files used
3311 to construct the agenda and limit the export to entries listed in the
3312 agenda now.
3313 If the extension is .org, collect all subtrees corresponding to the
3314 agenda entries and add them in an .org file.
3315 With prefix argument OPEN, open the new file immediately.
3316 If NOSETTINGS is given, do not scope the settings of
3317 `org-agenda-exporter-settings' into the export commands. This is used when
3318 the settings have already been scoped and we do not wish to overrule other,
3319 higher priority settings.
3320 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3321 (interactive "FWrite agenda to file: \nP")
3322 (if (or (not (file-writable-p file))
3323 (and (file-exists-p file)
3324 (if (org-called-interactively-p 'any)
3325 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3326 (user-error "Cannot write agenda to file %s" file))
3327 (org-let (if nosettings nil org-agenda-exporter-settings)
3328 '(save-excursion
3329 (save-window-excursion
3330 (let ((bs (copy-sequence (buffer-string))) beg content)
3331 (with-temp-buffer
3332 (rename-buffer org-agenda-write-buffer-name t)
3333 (set-buffer-modified-p nil)
3334 (insert bs)
3335 (org-agenda-remove-marked-text 'org-filtered)
3336 (run-hooks 'org-agenda-before-write-hook)
3337 (cond
3338 ((org-bound-and-true-p org-mobile-creating-agendas)
3339 (org-mobile-write-agenda-for-mobile file))
3340 ((string-match "\\.org\\'" file)
3341 (let (content p m message-log-max)
3342 (goto-char (point-min))
3343 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3344 (goto-char p)
3345 (setq m (get-text-property (point) 'org-hd-marker))
3346 (when m
3347 (push (save-excursion
3348 (set-buffer (marker-buffer m))
3349 (goto-char m)
3350 (org-copy-subtree 1 nil t t)
3351 org-subtree-clip)
3352 content)))
3353 (find-file file)
3354 (erase-buffer)
3355 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3356 (write-file file)
3357 (kill-buffer (current-buffer))
3358 (message "Org file written to %s" file)))
3359 ((string-match "\\.html?\\'" file)
3360 (require 'htmlize)
3361 (set-buffer (htmlize-buffer (current-buffer)))
3362 (when org-agenda-export-html-style
3363 ;; replace <style> section with org-agenda-export-html-style
3364 (goto-char (point-min))
3365 (kill-region (- (search-forward "<style") 6)
3366 (search-forward "</style>"))
3367 (insert org-agenda-export-html-style))
3368 (write-file file)
3369 (kill-buffer (current-buffer))
3370 (message "HTML written to %s" file))
3371 ((string-match "\\.ps\\'" file)
3372 (require 'ps-print)
3373 (ps-print-buffer-with-faces file)
3374 (message "Postscript written to %s" file))
3375 ((string-match "\\.pdf\\'" file)
3376 (require 'ps-print)
3377 (ps-print-buffer-with-faces
3378 (concat (file-name-sans-extension file) ".ps"))
3379 (call-process "ps2pdf" nil nil nil
3380 (expand-file-name
3381 (concat (file-name-sans-extension file) ".ps"))
3382 (expand-file-name file))
3383 (delete-file (concat (file-name-sans-extension file) ".ps"))
3384 (message "PDF written to %s" file))
3385 ((string-match "\\.ics\\'" file)
3386 (require 'ox-icalendar)
3387 (org-icalendar-export-current-agenda (expand-file-name file)))
3389 (let ((bs (buffer-string)))
3390 (find-file file)
3391 (erase-buffer)
3392 (insert bs)
3393 (save-buffer 0)
3394 (kill-buffer (current-buffer))
3395 (message "Plain text written to %s" file))))))))
3396 (set-buffer (or agenda-bufname
3397 (and (org-called-interactively-p 'any) (buffer-name))
3398 org-agenda-buffer-name)))
3399 (when open (org-open-file file)))
3401 (defun org-agenda-remove-marked-text (property &optional value)
3402 "Delete all text marked with VALUE of PROPERTY.
3403 VALUE defaults to t."
3404 (let (beg)
3405 (setq value (or value t))
3406 (while (setq beg (text-property-any (point-min) (point-max)
3407 property value))
3408 (delete-region
3409 beg (or (next-single-property-change beg property)
3410 (point-max))))))
3412 (defun org-agenda-add-entry-text ()
3413 "Add entry text to agenda lines.
3414 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3415 entry text following headings shown in the agenda.
3416 Drawers will be excluded, also the line with scheduling/deadline info."
3417 (when (and (> org-agenda-add-entry-text-maxlines 0)
3418 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3419 (let (m txt)
3420 (goto-char (point-min))
3421 (while (not (eobp))
3422 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3423 (beginning-of-line 2)
3424 (setq txt (org-agenda-get-some-entry-text
3425 m org-agenda-add-entry-text-maxlines " > "))
3426 (end-of-line 1)
3427 (if (string-match "\\S-" txt)
3428 (insert "\n" txt)
3429 (or (eobp) (forward-char 1))))))))
3431 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3432 &rest keep)
3433 "Extract entry text from MARKER, at most N-LINES lines.
3434 This will ignore drawers etc, just get the text.
3435 If INDENT is given, prefix every line with this string. If KEEP is
3436 given, it is a list of symbols, defining stuff that should not be
3437 removed from the entry content. Currently only `planning' is allowed here."
3438 (let (txt drawer-re kwd-time-re ind)
3439 (save-excursion
3440 (with-current-buffer (marker-buffer marker)
3441 (if (not (derived-mode-p 'org-mode))
3442 (setq txt "")
3443 (save-excursion
3444 (save-restriction
3445 (widen)
3446 (goto-char marker)
3447 (end-of-line 1)
3448 (setq txt (buffer-substring
3449 (min (1+ (point)) (point-max))
3450 (progn (outline-next-heading) (point)))
3451 drawer-re org-drawer-regexp
3452 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3453 ".*\n?"))
3454 (with-temp-buffer
3455 (insert txt)
3456 (when org-agenda-add-entry-text-descriptive-links
3457 (goto-char (point-min))
3458 (while (org-activate-bracket-links (point-max))
3459 (add-text-properties (match-beginning 0) (match-end 0)
3460 '(face org-link))))
3461 (goto-char (point-min))
3462 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3463 (set-text-properties (match-beginning 0) (match-end 0)
3464 nil))
3465 (goto-char (point-min))
3466 (while (re-search-forward drawer-re nil t)
3467 (delete-region
3468 (match-beginning 0)
3469 (progn (re-search-forward
3470 "^[ \t]*:END:.*\n?" nil 'move)
3471 (point))))
3472 (unless (member 'planning keep)
3473 (goto-char (point-min))
3474 (while (re-search-forward kwd-time-re nil t)
3475 (replace-match "")))
3476 (goto-char (point-min))
3477 (when org-agenda-entry-text-exclude-regexps
3478 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3479 (while (setq re (pop re-list))
3480 (goto-char (point-min))
3481 (while (re-search-forward re nil t)
3482 (replace-match "")))))
3483 (goto-char (point-max))
3484 (skip-chars-backward " \t\n")
3485 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3487 ;; find and remove min common indentation
3488 (goto-char (point-min))
3489 (untabify (point-min) (point-max))
3490 (setq ind (org-get-indentation))
3491 (while (not (eobp))
3492 (unless (looking-at "[ \t]*$")
3493 (setq ind (min ind (org-get-indentation))))
3494 (beginning-of-line 2))
3495 (goto-char (point-min))
3496 (while (not (eobp))
3497 (unless (looking-at "[ \t]*$")
3498 (move-to-column ind)
3499 (delete-region (point-at-bol) (point)))
3500 (beginning-of-line 2))
3502 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3504 (goto-char (point-min))
3505 (when indent
3506 (while (and (not (eobp)) (re-search-forward "^" nil t))
3507 (replace-match indent t t)))
3508 (goto-char (point-min))
3509 (while (looking-at "[ \t]*\n") (replace-match ""))
3510 (goto-char (point-max))
3511 (when (> (org-current-line)
3512 n-lines)
3513 (org-goto-line (1+ n-lines))
3514 (backward-char 1))
3515 (setq txt (buffer-substring (point-min) (point)))))))))
3516 txt))
3518 (defun org-check-for-org-mode ()
3519 "Make sure current buffer is in org-mode. Error if not."
3520 (or (derived-mode-p 'org-mode)
3521 (error "Cannot execute org-mode agenda command on buffer in %s"
3522 major-mode)))
3524 ;;; Agenda prepare and finalize
3526 (defvar org-agenda-multi nil) ; dynamically scoped
3527 (defvar org-agenda-pre-window-conf nil)
3528 (defvar org-agenda-columns-active nil)
3529 (defvar org-agenda-name nil)
3530 (defvar org-agenda-tag-filter nil)
3531 (defvar org-agenda-category-filter nil)
3532 (defvar org-agenda-regexp-filter nil)
3533 (defvar org-agenda-top-headline-filter nil)
3534 (defvar org-agenda-tag-filter-while-redo nil)
3535 (defvar org-agenda-tag-filter-preset nil
3536 "A preset of the tags filter used for secondary agenda filtering.
3537 This must be a list of strings, each string must be a single tag preceded
3538 by \"+\" or \"-\".
3539 This variable should not be set directly, but agenda custom commands can
3540 bind it in the options section. The preset filter is a global property of
3541 the entire agenda view. In a block agenda, it will not work reliably to
3542 define a filter for one of the individual blocks. You need to set it in
3543 the global options and expect it to be applied to the entire view.")
3545 (defvar org-agenda-category-filter-preset nil
3546 "A preset of the category filter used for secondary agenda filtering.
3547 This must be a list of strings, each string must be a single category
3548 preceded by \"+\" or \"-\".
3549 This variable should not be set directly, but agenda custom commands can
3550 bind it in the options section. The preset filter is a global property of
3551 the entire agenda view. In a block agenda, it will not work reliably to
3552 define a filter for one of the individual blocks. You need to set it in
3553 the global options and expect it to be applied to the entire view.")
3555 (defvar org-agenda-regexp-filter-preset nil
3556 "A preset of the regexp filter used for secondary agenda filtering.
3557 This must be a list of strings, each string must be a single regexp
3558 preceded by \"+\" or \"-\".
3559 This variable should not be set directly, but agenda custom commands can
3560 bind it in the options section. The preset filter is a global property of
3561 the entire agenda view. In a block agenda, it will not work reliably to
3562 define a filter for one of the individual blocks. You need to set it in
3563 the global options and expect it to be applied to the entire view.")
3565 (defun org-agenda-use-sticky-p ()
3566 "Return non-nil if an agenda buffer named
3567 `org-agenda-buffer-name' exists and should be shown instead of
3568 generating a new one."
3569 (and
3570 ;; turned off by user
3571 org-agenda-sticky
3572 ;; For multi-agenda buffer already exists
3573 (not org-agenda-multi)
3574 ;; buffer found
3575 (get-buffer org-agenda-buffer-name)
3576 ;; C-u parameter is same as last call
3577 (with-current-buffer (get-buffer org-agenda-buffer-name)
3578 (and
3579 (equal current-prefix-arg
3580 org-agenda-last-prefix-arg)
3581 ;; In case user turned stickiness on, while having existing
3582 ;; Agenda buffer active, don't reuse that buffer, because it
3583 ;; does not have org variables local
3584 org-agenda-this-buffer-is-sticky))))
3586 (defun org-agenda-prepare-window (abuf filter-alist)
3587 "Setup agenda buffer in the window.
3588 ABUF is the buffer for the agenda window.
3589 FILTER-ALIST is an alist of filters we need to apply when
3590 `org-agenda-persistent-filter' is non-nil."
3591 (let* ((awin (get-buffer-window abuf)) wconf)
3592 (cond
3593 ((equal (current-buffer) abuf) nil)
3594 (awin (select-window awin))
3595 ((not (setq wconf (current-window-configuration))))
3596 ((equal org-agenda-window-setup 'current-window)
3597 (org-pop-to-buffer-same-window abuf))
3598 ((equal org-agenda-window-setup 'other-window)
3599 (org-switch-to-buffer-other-window abuf))
3600 ((equal org-agenda-window-setup 'other-frame)
3601 (switch-to-buffer-other-frame abuf))
3602 ((equal org-agenda-window-setup 'reorganize-frame)
3603 (delete-other-windows)
3604 (org-switch-to-buffer-other-window abuf)))
3605 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3606 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3607 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3608 ;; Additional test in case agenda is invoked from within agenda
3609 ;; buffer via elisp link.
3610 (unless (equal (current-buffer) abuf)
3611 (org-pop-to-buffer-same-window abuf))
3612 (setq org-agenda-pre-window-conf
3613 (or org-agenda-pre-window-conf wconf))))
3615 (defun org-agenda-prepare (&optional name)
3616 (let ((filter-alist (if org-agenda-persistent-filter
3617 (list `(tag . ,org-agenda-tag-filter)
3618 `(re . ,org-agenda-regexp-filter)
3619 `(car . ,org-agenda-category-filter)))))
3620 (if (org-agenda-use-sticky-p)
3621 (progn
3622 ;; Popup existing buffer
3623 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3624 filter-alist)
3625 (message "Sticky Agenda buffer, use `r' to refresh")
3626 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3627 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3628 (setq org-todo-keywords-for-agenda nil)
3629 (setq org-drawers-for-agenda nil)
3630 (put 'org-agenda-tag-filter :preset-filter
3631 org-agenda-tag-filter-preset)
3632 (put 'org-agenda-category-filter :preset-filter
3633 org-agenda-category-filter-preset)
3634 (put 'org-agenda-regexp-filter :preset-filter
3635 org-agenda-regexp-filter-preset)
3636 (if org-agenda-multi
3637 (progn
3638 (setq buffer-read-only nil)
3639 (goto-char (point-max))
3640 (unless (or (bobp) org-agenda-compact-blocks
3641 (not org-agenda-block-separator))
3642 (insert "\n"
3643 (if (stringp org-agenda-block-separator)
3644 org-agenda-block-separator
3645 (make-string (window-width) org-agenda-block-separator))
3646 "\n"))
3647 (narrow-to-region (point) (point-max)))
3648 (setq org-done-keywords-for-agenda nil)
3650 ;; Setting any org variables that are in org-agenda-local-vars
3651 ;; list need to be done after the prepare call
3652 (org-agenda-prepare-window
3653 (get-buffer-create org-agenda-buffer-name) filter-alist)
3654 (setq buffer-read-only nil)
3655 (org-agenda-reset-markers)
3656 (let ((inhibit-read-only t)) (erase-buffer))
3657 (org-agenda-mode)
3658 (setq org-agenda-buffer (current-buffer))
3659 (setq org-agenda-contributing-files nil)
3660 (setq org-agenda-columns-active nil)
3661 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3662 (setq org-todo-keywords-for-agenda
3663 (org-uniquify org-todo-keywords-for-agenda))
3664 (setq org-done-keywords-for-agenda
3665 (org-uniquify org-done-keywords-for-agenda))
3666 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3667 (setq org-agenda-last-prefix-arg current-prefix-arg)
3668 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3669 (and name (not org-agenda-name)
3670 (org-set-local 'org-agenda-name name)))
3671 (setq buffer-read-only nil))))
3673 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3674 (defun org-agenda-finalize ()
3675 "Finishing touch for the agenda buffer, called just before displaying it."
3676 (unless org-agenda-multi
3677 (save-excursion
3678 (let ((inhibit-read-only t))
3679 (goto-char (point-min))
3680 (save-excursion
3681 (while (org-activate-bracket-links (point-max))
3682 (add-text-properties (match-beginning 0) (match-end 0)
3683 '(face org-link))))
3684 (save-excursion
3685 (while (org-activate-plain-links (point-max))
3686 (add-text-properties (match-beginning 0) (match-end 0)
3687 '(face org-link))))
3688 (unless (eq org-agenda-remove-tags t)
3689 (org-agenda-align-tags))
3690 (unless org-agenda-with-colors
3691 (remove-text-properties (point-min) (point-max) '(face nil)))
3692 (if (and (boundp 'org-agenda-overriding-columns-format)
3693 org-agenda-overriding-columns-format)
3694 (org-set-local 'org-agenda-overriding-columns-format
3695 org-agenda-overriding-columns-format))
3696 (if (and (boundp 'org-agenda-view-columns-initially)
3697 org-agenda-view-columns-initially)
3698 (org-agenda-columns))
3699 (when org-agenda-fontify-priorities
3700 (org-agenda-fontify-priorities))
3701 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3702 (org-agenda-dim-blocked-tasks))
3703 (org-agenda-mark-clocking-task)
3704 (when org-agenda-entry-text-mode
3705 (org-agenda-entry-text-hide)
3706 (org-agenda-entry-text-show))
3707 (if (and (functionp 'org-habit-insert-consistency-graphs)
3708 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3709 (org-habit-insert-consistency-graphs))
3710 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3711 (unless (or (eq org-agenda-show-inherited-tags 'always)
3712 (and (listp org-agenda-show-inherited-tags)
3713 (memq org-agenda-type org-agenda-show-inherited-tags))
3714 (and (eq org-agenda-show-inherited-tags t)
3715 (or (eq org-agenda-use-tag-inheritance t)
3716 (and (listp org-agenda-use-tag-inheritance)
3717 (not (memq org-agenda-type
3718 org-agenda-use-tag-inheritance))))))
3719 (let (mrk)
3720 (save-excursion
3721 (goto-char (point-min))
3722 (while (equal (forward-line) 0)
3723 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3724 (put-text-property (point-at-bol) (point-at-eol)
3725 'tags (org-with-point-at mrk
3726 (delete-dups
3727 (mapcar 'downcase (org-get-tags-at))))))))))
3728 (run-hooks 'org-agenda-finalize-hook)
3729 (when org-agenda-top-headline-filter
3730 (org-agenda-filter-top-headline-apply
3731 org-agenda-top-headline-filter))
3732 (when org-agenda-tag-filter
3733 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3734 (when (get 'org-agenda-tag-filter :preset-filter)
3735 (org-agenda-filter-apply
3736 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3737 (when org-agenda-category-filter
3738 (org-agenda-filter-apply org-agenda-category-filter 'category))
3739 (when (get 'org-agenda-category-filter :preset-filter)
3740 (org-agenda-filter-apply
3741 (get 'org-agenda-category-filter :preset-filter) 'category))
3742 (when org-agenda-regexp-filter
3743 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3744 (when (get 'org-agenda-regexp-filter :preset-filter)
3745 (org-agenda-filter-apply
3746 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3747 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3749 (defun org-agenda-mark-clocking-task ()
3750 "Mark the current clock entry in the agenda if it is present."
3751 ;; We need to widen when `org-agenda-finalize' is called from
3752 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3753 (when org-clock-current-task
3754 (save-restriction
3755 (widen)
3756 (org-agenda-unmark-clocking-task)
3757 (when (marker-buffer org-clock-hd-marker)
3758 (save-excursion
3759 (goto-char (point-min))
3760 (let (s ov)
3761 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3762 (goto-char s)
3763 (when (equal (org-get-at-bol 'org-hd-marker)
3764 org-clock-hd-marker)
3765 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3766 (overlay-put ov 'type 'org-agenda-clocking)
3767 (overlay-put ov 'face 'org-agenda-clocking)
3768 (overlay-put ov 'help-echo
3769 "The clock is running in this item")))))))))
3771 (defun org-agenda-unmark-clocking-task ()
3772 "Unmark the current clocking task."
3773 (mapc (lambda (o)
3774 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3775 (delete-overlay o)))
3776 (overlays-in (point-min) (point-max))))
3778 (defun org-agenda-fontify-priorities ()
3779 "Make highest priority lines bold, and lowest italic."
3780 (interactive)
3781 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3782 (delete-overlay o)))
3783 (overlays-in (point-min) (point-max)))
3784 (save-excursion
3785 (let (b e p ov h l)
3786 (goto-char (point-min))
3787 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3788 (setq h (or (get-char-property (point) 'org-highest-priority)
3789 org-highest-priority)
3790 l (or (get-char-property (point) 'org-lowest-priority)
3791 org-lowest-priority)
3792 p (string-to-char (match-string 1))
3793 b (match-beginning 0)
3794 e (if (eq org-agenda-fontify-priorities 'cookies)
3795 (match-end 0)
3796 (point-at-eol))
3797 ov (make-overlay b e))
3798 (overlay-put
3799 ov 'face
3800 (cons (cond ((org-face-from-face-or-color
3801 'priority nil
3802 (cdr (assoc p org-priority-faces))))
3803 ((and (listp org-agenda-fontify-priorities)
3804 (org-face-from-face-or-color
3805 'priority nil
3806 (cdr (assoc p org-agenda-fontify-priorities)))))
3807 ((equal p l) 'italic)
3808 ((equal p h) 'bold))
3809 'org-priority))
3810 (overlay-put ov 'org-type 'org-priority)))))
3812 (defvar org-depend-tag-blocked)
3814 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3815 "Dim currently blocked TODO's in the agenda display.
3816 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3817 dimming them."
3818 (interactive "P")
3819 (when (org-called-interactively-p 'interactive)
3820 (message "Dim or hide blocked tasks..."))
3821 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3822 (delete-overlay o)))
3823 (overlays-in (point-min) (point-max)))
3824 (save-excursion
3825 (let ((inhibit-read-only t)
3826 (org-depend-tag-blocked nil)
3827 (invis (or (not (null invisible))
3828 (eq org-agenda-dim-blocked-tasks 'invisible)))
3829 org-blocked-by-checkboxes
3830 invis1 b e p ov h l)
3831 (goto-char (point-min))
3832 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3833 (and pos (goto-char (1+ pos))))
3834 (setq org-blocked-by-checkboxes nil invis1 invis)
3835 (let ((marker (org-get-at-bol 'org-hd-marker)))
3836 (when (and marker
3837 (with-current-buffer (marker-buffer marker)
3838 (save-excursion (goto-char marker)
3839 (org-entry-blocked-p))))
3840 (if org-blocked-by-checkboxes (setq invis1 nil))
3841 (setq b (if invis1
3842 (max (point-min) (1- (point-at-bol)))
3843 (point-at-bol))
3844 e (point-at-eol)
3845 ov (make-overlay b e))
3846 (if invis1
3847 (progn (overlay-put ov 'invisible t)
3848 (overlay-put ov 'intangible t))
3849 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3850 (overlay-put ov 'org-type 'org-blocked-todo))))))
3851 (when (org-called-interactively-p 'interactive)
3852 (message "Dim or hide blocked tasks...done")))
3854 (defvar org-agenda-skip-function nil
3855 "Function to be called at each match during agenda construction.
3856 If this function returns nil, the current match should not be skipped.
3857 Otherwise, the function must return a position from where the search
3858 should be continued.
3859 This may also be a Lisp form, it will be evaluated.
3860 Never set this variable using `setq' or so, because then it will apply
3861 to all future agenda commands. If you do want a global skipping condition,
3862 use the option `org-agenda-skip-function-global' instead.
3863 The correct usage for `org-agenda-skip-function' is to bind it with
3864 `let' to scope it dynamically into the agenda-constructing command.
3865 A good way to set it is through options in `org-agenda-custom-commands'.")
3867 (defun org-agenda-skip ()
3868 "Throw to `:skip' in places that should be skipped.
3869 Also moves point to the end of the skipped region, so that search can
3870 continue from there."
3871 (let ((p (point-at-bol)) to)
3872 (when (or
3873 (save-excursion (goto-char p) (looking-at comment-start-skip))
3874 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3875 (get-text-property p :org-archived)
3876 (org-end-of-subtree t))
3877 (and org-agenda-skip-comment-trees
3878 (get-text-property p :org-comment)
3879 (org-end-of-subtree t))
3880 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3881 (org-agenda-skip-eval org-agenda-skip-function)))
3882 (goto-char to))
3883 (org-in-src-block-p t))
3884 (throw :skip t))))
3886 (defun org-agenda-skip-eval (form)
3887 "If FORM is a function or a list, call (or eval) it and return the result.
3888 `save-excursion' and `save-match-data' are wrapped around the call, so point
3889 and match data are returned to the previous state no matter what these
3890 functions do."
3891 (let (fp)
3892 (and form
3893 (or (setq fp (functionp form))
3894 (consp form))
3895 (save-excursion
3896 (save-match-data
3897 (if fp
3898 (funcall form)
3899 (eval form)))))))
3901 (defvar org-agenda-markers nil
3902 "List of all currently active markers created by `org-agenda'.")
3903 (defvar org-agenda-last-marker-time (org-float-time)
3904 "Creation time of the last agenda marker.")
3906 (defun org-agenda-new-marker (&optional pos)
3907 "Return a new agenda marker.
3908 Org-mode keeps a list of these markers and resets them when they are
3909 no longer in use."
3910 (let ((m (copy-marker (or pos (point)))))
3911 (setq org-agenda-last-marker-time (org-float-time))
3912 (if org-agenda-buffer
3913 (with-current-buffer org-agenda-buffer
3914 (push m org-agenda-markers))
3915 (push m org-agenda-markers))
3918 (defun org-agenda-reset-markers ()
3919 "Reset markers created by `org-agenda'."
3920 (while org-agenda-markers
3921 (move-marker (pop org-agenda-markers) nil)))
3923 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3924 "Save relative positions of markers in region.
3925 This check for agenda markers in all agenda buffers currently active."
3926 (dolist (buf (buffer-list))
3927 (with-current-buffer buf
3928 (when (eq major-mode 'org-agenda-mode)
3929 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3930 org-agenda-markers)))))
3932 ;;; Entry text mode
3934 (defun org-agenda-entry-text-show-here ()
3935 "Add some text from the entry as context to the current line."
3936 (let (m txt o)
3937 (setq m (org-get-at-bol 'org-hd-marker))
3938 (unless (marker-buffer m)
3939 (error "No marker points to an entry here"))
3940 (setq txt (concat "\n" (org-no-properties
3941 (org-agenda-get-some-entry-text
3942 m org-agenda-entry-text-maxlines
3943 org-agenda-entry-text-leaders))))
3944 (when (string-match "\\S-" txt)
3945 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3946 (overlay-put o 'evaporate t)
3947 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3948 (overlay-put o 'after-string txt))))
3950 (defun org-agenda-entry-text-show ()
3951 "Add entry context for all agenda lines."
3952 (interactive)
3953 (save-excursion
3954 (goto-char (point-max))
3955 (beginning-of-line 1)
3956 (while (not (bobp))
3957 (when (org-get-at-bol 'org-hd-marker)
3958 (org-agenda-entry-text-show-here))
3959 (beginning-of-line 0))))
3961 (defun org-agenda-entry-text-hide ()
3962 "Remove any shown entry context."
3963 (delq nil
3964 (mapcar (lambda (o)
3965 (if (eq (overlay-get o 'org-overlay-type)
3966 'agenda-entry-content)
3967 (progn (delete-overlay o) t)))
3968 (overlays-in (point-min) (point-max)))))
3970 (defun org-agenda-get-day-face (date)
3971 "Return the face DATE should be displayed with."
3972 (or (and (functionp org-agenda-day-face-function)
3973 (funcall org-agenda-day-face-function date))
3974 (cond ((org-agenda-todayp date)
3975 'org-agenda-date-today)
3976 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3977 'org-agenda-date-weekend)
3978 (t 'org-agenda-date))))
3980 ;;; Agenda timeline
3982 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3983 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
3985 (defun org-timeline (&optional dotodo)
3986 "Show a time-sorted view of the entries in the current org file.
3987 Only entries with a time stamp of today or later will be listed. With
3988 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3989 under the current date.
3990 If the buffer contains an active region, only check the region for
3991 dates."
3992 (interactive "P")
3993 (let* ((dopast t)
3994 (org-agenda-show-log-scoped org-agenda-show-log)
3995 (org-agenda-show-log org-agenda-show-log-scoped)
3996 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3997 (current-buffer))))
3998 (date (calendar-current-date))
3999 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4000 (end (if (org-region-active-p) (region-end) (point-max)))
4001 (day-numbers (org-get-all-dates
4002 beg end 'no-ranges
4003 t org-agenda-show-log-scoped ; always include today
4004 org-timeline-show-empty-dates))
4005 (org-deadline-warning-days 0)
4006 (org-agenda-only-exact-dates t)
4007 (today (org-today))
4008 (past t)
4009 args
4010 s e rtn d emptyp)
4011 (setq org-agenda-redo-command
4012 (list 'let
4013 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4014 (list 'org-switch-to-buffer-other-window (current-buffer))
4015 (list 'org-timeline (list 'quote dotodo))))
4016 (put 'org-agenda-redo-command 'org-lprops nil)
4017 (if (not dopast)
4018 ;; Remove past dates from the list of dates.
4019 (setq day-numbers (delq nil (mapcar (lambda(x)
4020 (if (>= x today) x nil))
4021 day-numbers))))
4022 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4023 (org-compile-prefix-format 'timeline)
4024 (org-set-sorting-strategy 'timeline)
4025 (if org-agenda-show-log-scoped (push :closed args))
4026 (push :timestamp args)
4027 (push :deadline args)
4028 (push :scheduled args)
4029 (push :sexp args)
4030 (if dotodo (push :todo args))
4031 (insert "Timeline of file " entry "\n")
4032 (add-text-properties (point-min) (point)
4033 (list 'face 'org-agenda-structure))
4034 (org-agenda-mark-header-line (point-min))
4035 (while (setq d (pop day-numbers))
4036 (if (and (listp d) (eq (car d) :omitted))
4037 (progn
4038 (setq s (point))
4039 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4040 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4041 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4042 (if (and (>= d today)
4043 dopast
4044 past)
4045 (progn
4046 (setq past nil)
4047 (insert (make-string 79 ?-) "\n")))
4048 (setq date (calendar-gregorian-from-absolute d))
4049 (setq s (point))
4050 (setq rtn (and (not emptyp)
4051 (apply 'org-agenda-get-day-entries entry
4052 date args)))
4053 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4054 (progn
4055 (insert
4056 (if (stringp org-agenda-format-date)
4057 (format-time-string org-agenda-format-date
4058 (org-time-from-absolute date))
4059 (funcall org-agenda-format-date date))
4060 "\n")
4061 (put-text-property s (1- (point)) 'face
4062 (org-agenda-get-day-face date))
4063 (put-text-property s (1- (point)) 'org-date-line t)
4064 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4065 (if (equal d today)
4066 (put-text-property s (1- (point)) 'org-today t))
4067 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4068 (put-text-property s (1- (point)) 'day d)))))
4069 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4070 (point-min)))
4071 (add-text-properties
4072 (point-min) (point-max)
4073 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4074 (org-agenda-finalize)
4075 (setq buffer-read-only t)))
4077 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4078 "Return a list of all relevant day numbers from BEG to END buffer positions.
4079 If NO-RANGES is non-nil, include only the start and end dates of a range,
4080 not every single day in the range. If FORCE-TODAY is non-nil, make
4081 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4082 inactive time stamps (those in square brackets) are included.
4083 When EMPTY is non-nil, also include days without any entries."
4084 (let ((re (concat
4085 (if pre-re pre-re "")
4086 (if inactive org-ts-regexp-both org-ts-regexp)))
4087 dates dates1 date day day1 day2 ts1 ts2 pos)
4088 (if force-today
4089 (setq dates (list (org-today))))
4090 (save-excursion
4091 (goto-char beg)
4092 (while (re-search-forward re end t)
4093 (setq day (time-to-days (org-time-string-to-time
4094 (substring (match-string 1) 0 10)
4095 (current-buffer) (match-beginning 0))))
4096 (or (memq day dates) (push day dates)))
4097 (unless no-ranges
4098 (goto-char beg)
4099 (while (re-search-forward org-tr-regexp end t)
4100 (setq pos (match-beginning 0))
4101 (setq ts1 (substring (match-string 1) 0 10)
4102 ts2 (substring (match-string 2) 0 10)
4103 day1 (time-to-days (org-time-string-to-time
4104 ts1 (current-buffer) pos))
4105 day2 (time-to-days (org-time-string-to-time
4106 ts2 (current-buffer) pos)))
4107 (while (< (setq day1 (1+ day1)) day2)
4108 (or (memq day1 dates) (push day1 dates)))))
4109 (setq dates (sort dates '<))
4110 (when empty
4111 (while (setq day (pop dates))
4112 (setq day2 (car dates))
4113 (push day dates1)
4114 (when (and day2 empty)
4115 (if (or (eq empty t)
4116 (and (numberp empty) (<= (- day2 day) empty)))
4117 (while (< (setq day (1+ day)) day2)
4118 (push (list day) dates1))
4119 (push (cons :omitted (- day2 day)) dates1))))
4120 (setq dates (nreverse dates1)))
4121 dates)))
4123 ;;; Agenda Daily/Weekly
4125 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4126 "Start day for the agenda view.
4127 Custom commands can set this variable in the options section.
4128 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4129 input allowed when reading a date through the Org calendar.
4130 See the docstring of `org-read-date' for details.")
4131 (defvar org-starting-day nil) ; local variable in the agenda buffer
4132 (defvar org-arg-loc nil) ; local variable
4134 (defvar org-agenda-buffer-tmp-name nil)
4135 ;;;###autoload
4136 (defun org-agenda-list (&optional arg start-day span with-hour)
4137 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4138 The view will be for the current day or week, but from the overview buffer
4139 you will be able to go to other days/weeks.
4141 With a numeric prefix argument in an interactive call, the agenda will
4142 span ARG days. Lisp programs should instead specify SPAN to change
4143 the number of days. SPAN defaults to `org-agenda-span'.
4145 START-DAY defaults to TODAY, or to the most recent match for the weekday
4146 given in `org-agenda-start-on-weekday'.
4148 When WITH-HOUR is non-nil, only include scheduled and deadline
4149 items if they have an hour specification like [h]h:mm."
4150 (interactive "P")
4151 (if org-agenda-overriding-arguments
4152 (setq arg (car org-agenda-overriding-arguments)
4153 start-day (nth 1 org-agenda-overriding-arguments)
4154 span (nth 2 org-agenda-overriding-arguments)))
4155 (if (and (integerp arg) (> arg 0))
4156 (setq span arg arg nil))
4157 (catch 'exit
4158 (setq org-agenda-buffer-name
4159 (or org-agenda-buffer-tmp-name
4160 (if org-agenda-sticky
4161 (cond ((and org-keys (stringp org-match))
4162 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4163 (org-keys
4164 (format "*Org Agenda(%s)*" org-keys))
4165 (t "*Org Agenda(a)*")))
4166 org-agenda-buffer-name))
4167 (org-agenda-prepare "Day/Week")
4168 (setq start-day (or start-day org-agenda-start-day))
4169 (if (stringp start-day)
4170 ;; Convert to an absolute day number
4171 (setq start-day (time-to-days (org-read-date nil t start-day))))
4172 (org-compile-prefix-format 'agenda)
4173 (org-set-sorting-strategy 'agenda)
4174 (let* ((span (org-agenda-ndays-to-span
4175 (or span org-agenda-ndays org-agenda-span)))
4176 (today (org-today))
4177 (sd (or start-day today))
4178 (ndays (org-agenda-span-to-ndays span sd))
4179 (org-agenda-start-on-weekday
4180 (if (or (eq ndays 7) (eq ndays 14))
4181 org-agenda-start-on-weekday))
4182 (thefiles (org-agenda-files nil 'ifmode))
4183 (files thefiles)
4184 (start (if (or (null org-agenda-start-on-weekday)
4185 (< ndays 7))
4187 (let* ((nt (calendar-day-of-week
4188 (calendar-gregorian-from-absolute sd)))
4189 (n1 org-agenda-start-on-weekday)
4190 (d (- nt n1)))
4191 (- sd (+ (if (< d 0) 7 0) d)))))
4192 (day-numbers (list start))
4193 (day-cnt 0)
4194 (inhibit-redisplay (not debug-on-error))
4195 (org-agenda-show-log-scoped org-agenda-show-log)
4196 s e rtn rtnall file date d start-pos end-pos todayp
4197 clocktable-start clocktable-end filter)
4198 (setq org-agenda-redo-command
4199 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4200 (dotimes (n (1- ndays))
4201 (push (1+ (car day-numbers)) day-numbers))
4202 (setq day-numbers (nreverse day-numbers))
4203 (setq clocktable-start (car day-numbers)
4204 clocktable-end (1+ (or (org-last day-numbers) 0)))
4205 (org-set-local 'org-starting-day (car day-numbers))
4206 (org-set-local 'org-arg-loc arg)
4207 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4208 (unless org-agenda-compact-blocks
4209 (let* ((d1 (car day-numbers))
4210 (d2 (org-last day-numbers))
4211 (w1 (org-days-to-iso-week d1))
4212 (w2 (org-days-to-iso-week d2)))
4213 (setq s (point))
4214 (if org-agenda-overriding-header
4215 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4216 nil 'face 'org-agenda-structure) "\n")
4217 (insert (org-agenda-span-name span)
4218 "-agenda"
4219 (if (< (- d2 d1) 350)
4220 (if (= w1 w2)
4221 (format " (W%02d)" w1)
4222 (format " (W%02d-W%02d)" w1 w2))
4224 ":\n")))
4225 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4226 'org-date-line t))
4227 (org-agenda-mark-header-line s))
4228 (while (setq d (pop day-numbers))
4229 (setq date (calendar-gregorian-from-absolute d)
4230 s (point))
4231 (if (or (setq todayp (= d today))
4232 (and (not start-pos) (= d sd)))
4233 (setq start-pos (point))
4234 (if (and start-pos (not end-pos))
4235 (setq end-pos (point))))
4236 (setq files thefiles
4237 rtnall nil)
4238 (while (setq file (pop files))
4239 (catch 'nextfile
4240 (org-check-agenda-file file)
4241 (let ((org-agenda-entry-types org-agenda-entry-types))
4242 ;; Starred types override non-starred equivalents
4243 (when (member :deadline* org-agenda-entry-types)
4244 (setq org-agenda-entry-types
4245 (delq :deadline org-agenda-entry-types)))
4246 (when (member :scheduled* org-agenda-entry-types)
4247 (setq org-agenda-entry-types
4248 (delq :scheduled org-agenda-entry-types)))
4249 ;; Honor with-hour
4250 (when with-hour
4251 (when (member :deadline org-agenda-entry-types)
4252 (setq org-agenda-entry-types
4253 (delq :deadline org-agenda-entry-types))
4254 (push :deadline* org-agenda-entry-types))
4255 (when (member :scheduled org-agenda-entry-types)
4256 (setq org-agenda-entry-types
4257 (delq :scheduled org-agenda-entry-types))
4258 (push :scheduled* org-agenda-entry-types)))
4259 (unless org-agenda-include-deadlines
4260 (setq org-agenda-entry-types
4261 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4262 (cond
4263 ((memq org-agenda-show-log-scoped '(only clockcheck))
4264 (setq rtn (org-agenda-get-day-entries
4265 file date :closed)))
4266 (org-agenda-show-log-scoped
4267 (setq rtn (apply 'org-agenda-get-day-entries
4268 file date
4269 (append '(:closed) org-agenda-entry-types))))
4271 (setq rtn (apply 'org-agenda-get-day-entries
4272 file date
4273 org-agenda-entry-types)))))
4274 (setq rtnall (append rtnall rtn)))) ;; all entries
4275 (if org-agenda-include-diary
4276 (let ((org-agenda-search-headline-for-time t))
4277 (require 'diary-lib)
4278 (setq rtn (org-get-entries-from-diary date))
4279 (setq rtnall (append rtnall rtn))))
4280 (if (or rtnall org-agenda-show-all-dates)
4281 (progn
4282 (setq day-cnt (1+ day-cnt))
4283 (insert
4284 (if (stringp org-agenda-format-date)
4285 (format-time-string org-agenda-format-date
4286 (org-time-from-absolute date))
4287 (funcall org-agenda-format-date date))
4288 "\n")
4289 (put-text-property s (1- (point)) 'face
4290 (org-agenda-get-day-face date))
4291 (put-text-property s (1- (point)) 'org-date-line t)
4292 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4293 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4294 (when todayp
4295 (put-text-property s (1- (point)) 'org-today t))
4296 (setq rtnall
4297 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4298 (if rtnall (insert ;; all entries
4299 (org-agenda-finalize-entries rtnall 'agenda)
4300 "\n"))
4301 (put-text-property s (1- (point)) 'day d)
4302 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4303 (when (and org-agenda-clockreport-mode clocktable-start)
4304 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4305 ;; the above line is to ensure the restricted range!
4306 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4307 tbl)
4308 (setq p (org-plist-delete p :block))
4309 (setq p (plist-put p :tstart clocktable-start))
4310 (setq p (plist-put p :tend clocktable-end))
4311 (setq p (plist-put p :scope 'agenda))
4312 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4313 (setq filter (or org-agenda-tag-filter-while-redo
4314 (get 'org-agenda-tag-filter :preset-filter))))
4315 (setq p (plist-put p :tags (mapconcat (lambda (x)
4316 (if (string-match "[<>=]" x)
4319 filter ""))))
4320 (setq tbl (apply 'org-clock-get-clocktable p))
4321 (insert tbl)))
4322 (goto-char (point-min))
4323 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4324 (unless (and (pos-visible-in-window-p (point-min))
4325 (pos-visible-in-window-p (point-max)))
4326 (goto-char (1- (point-max)))
4327 (recenter -1)
4328 (if (not (pos-visible-in-window-p (or start-pos 1)))
4329 (progn
4330 (goto-char (or start-pos 1))
4331 (recenter 1))))
4332 (goto-char (or start-pos 1))
4333 (add-text-properties (point-min) (point-max)
4334 `(org-agenda-type agenda
4335 org-last-args (,arg ,start-day ,span)
4336 org-redo-cmd ,org-agenda-redo-command
4337 org-series-cmd ,org-cmd))
4338 (if (eq org-agenda-show-log-scoped 'clockcheck)
4339 (org-agenda-show-clocking-issues))
4340 (org-agenda-finalize)
4341 (setq buffer-read-only t)
4342 (message ""))))
4344 (defun org-agenda-ndays-to-span (n)
4345 "Return a span symbol for a span of N days, or N if none matches."
4346 (cond ((symbolp n) n)
4347 ((= n 1) 'day)
4348 ((= n 7) 'week)
4349 ((= n 14) 'fortnight)
4350 (t n)))
4352 (defun org-agenda-span-to-ndays (span &optional start-day)
4353 "Return ndays from SPAN, possibly starting at START-DAY.
4354 START-DAY is an absolute time value."
4355 (cond ((numberp span) span)
4356 ((eq span 'day) 1)
4357 ((eq span 'week) 7)
4358 ((eq span 'fortnight) 14)
4359 ((eq span 'month)
4360 (let ((date (calendar-gregorian-from-absolute start-day)))
4361 (calendar-last-day-of-month (car date) (caddr date))))
4362 ((eq span 'year)
4363 (let ((date (calendar-gregorian-from-absolute start-day)))
4364 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4366 (defun org-agenda-span-name (span)
4367 "Return a SPAN name."
4368 (if (null span)
4370 (if (symbolp span)
4371 (capitalize (symbol-name span))
4372 (format "%d days" span))))
4374 ;;; Agenda word search
4376 (defvar org-agenda-search-history nil)
4378 (defvar org-search-syntax-table nil
4379 "Special syntax table for org-mode search.
4380 In this table, we have single quotes not as word constituents, to
4381 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4383 (defvar org-mode-syntax-table) ; From org.el
4384 (defun org-search-syntax-table ()
4385 (unless org-search-syntax-table
4386 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4387 (modify-syntax-entry ?' "." org-search-syntax-table)
4388 (modify-syntax-entry ?` "." org-search-syntax-table))
4389 org-search-syntax-table)
4391 (defvar org-agenda-last-search-view-search-was-boolean nil)
4393 ;;;###autoload
4394 (defun org-search-view (&optional todo-only string edit-at)
4395 "Show all entries that contain a phrase or words or regular expressions.
4397 With optional prefix argument TODO-ONLY, only consider entries that are
4398 TODO entries. The argument STRING can be used to pass a default search
4399 string into this function. If EDIT-AT is non-nil, it means that the
4400 user should get a chance to edit this string, with cursor at position
4401 EDIT-AT.
4403 The search string can be viewed either as a phrase that should be found as
4404 is, or it can be broken into a number of snippets, each of which must match
4405 in a Boolean way to select an entry. The default depends on the variable
4406 `org-agenda-search-view-always-boolean'.
4407 Even if this is turned off (the default) you can always switch to
4408 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4410 The default is a direct search of the whole phrase, where each space in
4411 the search string can expand to an arbitrary amount of whitespace,
4412 including newlines.
4414 If using a Boolean search, the search string is split on whitespace and
4415 each snippet is searched separately, with logical AND to select an entry.
4416 Words prefixed with a minus must *not* occur in the entry. Words without
4417 a prefix or prefixed with a plus must occur in the entry. Matching is
4418 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4419 match whole words, not parts of a word) if
4420 `org-agenda-search-view-force-full-words' is set (default is nil).
4422 Boolean search snippets enclosed by curly braces are interpreted as
4423 regular expressions that must or (when preceded with \"-\") must not
4424 match in the entry. Snippets enclosed into double quotes will be taken
4425 as a whole, to include whitespace.
4427 - If the search string starts with an asterisk, search only in headlines.
4428 - If (possibly after the leading star) the search string starts with an
4429 exclamation mark, this also means to look at TODO entries only, an effect
4430 that can also be achieved with a prefix argument.
4431 - If (possibly after star and exclamation mark) the search string starts
4432 with a colon, this will mean that the (non-regexp) snippets of the
4433 Boolean search must match as full words.
4435 This command searches the agenda files, and in addition the files listed
4436 in `org-agenda-text-search-extra-files'."
4437 (interactive "P")
4438 (if org-agenda-overriding-arguments
4439 (setq todo-only (car org-agenda-overriding-arguments)
4440 string (nth 1 org-agenda-overriding-arguments)
4441 edit-at (nth 2 org-agenda-overriding-arguments)))
4442 (let* ((props (list 'face nil
4443 'done-face 'org-agenda-done
4444 'org-not-done-regexp org-not-done-regexp
4445 'org-todo-regexp org-todo-regexp
4446 'org-complex-heading-regexp org-complex-heading-regexp
4447 'mouse-face 'highlight
4448 'help-echo (format "mouse-2 or RET jump to location")))
4449 (full-words org-agenda-search-view-force-full-words)
4450 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4451 regexp rtn rtnall files file pos inherited-tags
4452 marker category category-pos level tags c neg re boolean
4453 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4454 (unless (and (not edit-at)
4455 (stringp string)
4456 (string-match "\\S-" string))
4457 (setq string (read-string
4458 (if org-agenda-search-view-always-boolean
4459 "[+-]Word/{Regexp} ...: "
4460 "Phrase or [+-]Word/{Regexp} ...: ")
4461 (cond
4462 ((integerp edit-at) (cons string edit-at))
4463 (edit-at string))
4464 'org-agenda-search-history)))
4465 (catch 'exit
4466 (if org-agenda-sticky
4467 (setq org-agenda-buffer-name
4468 (if (stringp string)
4469 (format "*Org Agenda(%s:%s)*"
4470 (or org-keys (or (and todo-only "S") "s")) string)
4471 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4472 (org-agenda-prepare "SEARCH")
4473 (org-compile-prefix-format 'search)
4474 (org-set-sorting-strategy 'search)
4475 (setq org-agenda-redo-command
4476 (list 'org-search-view (if todo-only t nil)
4477 (list 'if 'current-prefix-arg nil string)))
4478 (setq org-agenda-query-string string)
4479 (if (equal (string-to-char string) ?*)
4480 (setq hdl-only t
4481 words (substring string 1))
4482 (setq words string))
4483 (when (equal (string-to-char words) ?!)
4484 (setq todo-only t
4485 words (substring words 1)))
4486 (when (equal (string-to-char words) ?:)
4487 (setq full-words t
4488 words (substring words 1)))
4489 (if (or org-agenda-search-view-always-boolean
4490 (member (string-to-char words) '(?- ?+ ?\{)))
4491 (setq boolean t))
4492 (setq words (org-split-string words))
4493 (let (www w)
4494 (while (setq w (pop words))
4495 (while (and (string-match "\\\\\\'" w) words)
4496 (setq w (concat (substring w 0 -1) " " (pop words))))
4497 (push w www))
4498 (setq words (nreverse www) www nil)
4499 (while (setq w (pop words))
4500 (when (and (string-match "\\`[-+]?{" w)
4501 (not (string-match "}\\'" w)))
4502 (while (and words (not (string-match "}\\'" (car words))))
4503 (setq w (concat w " " (pop words))))
4504 (setq w (concat w " " (pop words))))
4505 (push w www))
4506 (setq words (nreverse www)))
4507 (setq org-agenda-last-search-view-search-was-boolean boolean)
4508 (when boolean
4509 (let (wds w)
4510 (while (setq w (pop words))
4511 (if (or (equal (substring w 0 1) "\"")
4512 (and (> (length w) 1)
4513 (member (substring w 0 1) '("+" "-"))
4514 (equal (substring w 1 2) "\"")))
4515 (while (and words (not (equal (substring w -1) "\"")))
4516 (setq w (concat w " " (pop words)))))
4517 (and (string-match "\\`\\([-+]?\\)\"" w)
4518 (setq w (replace-match "\\1" nil nil w)))
4519 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4520 (push w wds))
4521 (setq words (nreverse wds))))
4522 (if boolean
4523 (mapc (lambda (w)
4524 (setq c (string-to-char w))
4525 (if (equal c ?-)
4526 (setq neg t w (substring w 1))
4527 (if (equal c ?+)
4528 (setq neg nil w (substring w 1))
4529 (setq neg nil)))
4530 (if (string-match "\\`{.*}\\'" w)
4531 (setq re (substring w 1 -1))
4532 (if full-words
4533 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4534 (setq re (regexp-quote (downcase w)))))
4535 (if neg (push re regexps-) (push re regexps+)))
4536 words)
4537 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4538 regexps+))
4539 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4540 (if (not regexps+)
4541 (setq regexp org-outline-regexp-bol)
4542 (setq regexp (pop regexps+))
4543 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4544 regexp))))
4545 (setq files (org-agenda-files nil 'ifmode))
4546 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4547 (pop org-agenda-text-search-extra-files)
4548 (setq files (org-add-archive-files files)))
4549 (setq files (append files org-agenda-text-search-extra-files)
4550 rtnall nil)
4551 (while (setq file (pop files))
4552 (setq ee nil)
4553 (catch 'nextfile
4554 (org-check-agenda-file file)
4555 (setq buffer (if (file-exists-p file)
4556 (org-get-agenda-file-buffer file)
4557 (error "No such file %s" file)))
4558 (if (not buffer)
4559 ;; If file does not exist, make sure an error message is sent
4560 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4561 file))))
4562 (with-current-buffer buffer
4563 (with-syntax-table (org-search-syntax-table)
4564 (unless (derived-mode-p 'org-mode)
4565 (error "Agenda file %s is not in `org-mode'" file))
4566 (let ((case-fold-search t))
4567 (save-excursion
4568 (save-restriction
4569 (if (eq buffer org-agenda-restrict)
4570 (narrow-to-region org-agenda-restrict-begin
4571 org-agenda-restrict-end)
4572 (widen))
4573 (goto-char (point-min))
4574 (unless (or (org-at-heading-p)
4575 (outline-next-heading))
4576 (throw 'nextfile t))
4577 (goto-char (max (point-min) (1- (point))))
4578 (while (re-search-forward regexp nil t)
4579 (org-back-to-heading t)
4580 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4581 (> (org-reduced-level (org-outline-level))
4582 org-agenda-search-view-max-outline-level)
4583 (forward-line -1)
4584 (outline-back-to-heading t)))
4585 (skip-chars-forward "* ")
4586 (setq beg (point-at-bol)
4587 beg1 (point)
4588 end (progn
4589 (outline-next-heading)
4590 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4591 (> (org-reduced-level (org-outline-level))
4592 org-agenda-search-view-max-outline-level)
4593 (forward-line 1)
4594 (outline-next-heading)))
4595 (point)))
4597 (catch :skip
4598 (goto-char beg)
4599 (org-agenda-skip)
4600 (setq str (buffer-substring-no-properties
4601 (point-at-bol)
4602 (if hdl-only (point-at-eol) end)))
4603 (mapc (lambda (wr) (when (string-match wr str)
4604 (goto-char (1- end))
4605 (throw :skip t)))
4606 regexps-)
4607 (mapc (lambda (wr) (unless (string-match wr str)
4608 (goto-char (1- end))
4609 (throw :skip t)))
4610 (if todo-only
4611 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4612 regexps+)
4613 regexps+))
4614 (goto-char beg)
4615 (setq marker (org-agenda-new-marker (point))
4616 category (org-get-category)
4617 level (make-string (org-reduced-level (org-outline-level)) ? )
4618 category-pos (get-text-property (point) 'org-category-position)
4619 inherited-tags
4620 (or (eq org-agenda-show-inherited-tags 'always)
4621 (and (listp org-agenda-show-inherited-tags)
4622 (memq 'todo org-agenda-show-inherited-tags))
4623 (and (eq org-agenda-show-inherited-tags t)
4624 (or (eq org-agenda-use-tag-inheritance t)
4625 (memq 'todo org-agenda-use-tag-inheritance))))
4626 tags (org-get-tags-at nil (not inherited-tags))
4627 txt (org-agenda-format-item
4629 (buffer-substring-no-properties
4630 beg1 (point-at-eol))
4631 level category tags t))
4632 (org-add-props txt props
4633 'org-marker marker 'org-hd-marker marker
4634 'org-todo-regexp org-todo-regexp
4635 'level level
4636 'org-complex-heading-regexp org-complex-heading-regexp
4637 'priority 1000 'org-category category
4638 'org-category-position category-pos
4639 'type "search")
4640 (push txt ee)
4641 (goto-char (1- end))))))))))
4642 (setq rtn (nreverse ee))
4643 (setq rtnall (append rtnall rtn)))
4644 (if org-agenda-overriding-header
4645 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4646 nil 'face 'org-agenda-structure) "\n")
4647 (insert "Search words: ")
4648 (add-text-properties (point-min) (1- (point))
4649 (list 'face 'org-agenda-structure))
4650 (setq pos (point))
4651 (insert string "\n")
4652 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4653 (setq pos (point))
4654 (unless org-agenda-multi
4655 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4656 (add-text-properties pos (1- (point))
4657 (list 'face 'org-agenda-structure))))
4658 (org-agenda-mark-header-line (point-min))
4659 (when rtnall
4660 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4661 (goto-char (point-min))
4662 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4663 (add-text-properties (point-min) (point-max)
4664 `(org-agenda-type search
4665 org-last-args (,todo-only ,string ,edit-at)
4666 org-redo-cmd ,org-agenda-redo-command
4667 org-series-cmd ,org-cmd))
4668 (org-agenda-finalize)
4669 (setq buffer-read-only t))))
4671 ;;; Agenda TODO list
4673 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4674 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4675 (concat
4676 (if (or (equal keywords "ALL") (not keywords))
4677 (propertize "ALL" 'face 'warning)
4678 (mapconcat
4679 (lambda (kw)
4680 (propertize kw 'face (org-get-todo-face kw)))
4681 (org-split-string keywords "|")
4682 "|"))
4683 "\n"))
4685 (defvar org-select-this-todo-keyword nil)
4686 (defvar org-last-arg nil)
4688 ;;;###autoload
4689 (defun org-todo-list (&optional arg)
4690 "Show all (not done) TODO entries from all agenda file in a single list.
4691 The prefix arg can be used to select a specific TODO keyword and limit
4692 the list to these. When using \\[universal-argument], you will be prompted
4693 for a keyword. A numeric prefix directly selects the Nth keyword in
4694 `org-todo-keywords-1'."
4695 (interactive "P")
4696 (if org-agenda-overriding-arguments
4697 (setq arg org-agenda-overriding-arguments))
4698 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4699 (let* ((today (org-today))
4700 (date (calendar-gregorian-from-absolute today))
4701 (kwds org-todo-keywords-for-agenda)
4702 (completion-ignore-case t)
4703 (org-select-this-todo-keyword
4704 (if (stringp arg) arg
4705 (and arg (integerp arg) (> arg 0)
4706 (nth (1- arg) kwds))))
4707 rtn rtnall files file pos)
4708 (when (equal arg '(4))
4709 (setq org-select-this-todo-keyword
4710 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4711 (mapcar 'list kwds) nil nil)))
4712 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4713 (catch 'exit
4714 (if org-agenda-sticky
4715 (setq org-agenda-buffer-name
4716 (if (stringp org-select-this-todo-keyword)
4717 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4718 org-select-this-todo-keyword)
4719 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4720 (org-agenda-prepare "TODO")
4721 (org-compile-prefix-format 'todo)
4722 (org-set-sorting-strategy 'todo)
4723 (setq org-agenda-redo-command
4724 `(org-todo-list (or (and (numberp current-prefix-arg)
4725 current-prefix-arg)
4726 ,org-select-this-todo-keyword
4727 current-prefix-arg ,arg)))
4728 (setq files (org-agenda-files nil 'ifmode)
4729 rtnall nil)
4730 (while (setq file (pop files))
4731 (catch 'nextfile
4732 (org-check-agenda-file file)
4733 (setq rtn (org-agenda-get-day-entries file date :todo))
4734 (setq rtnall (append rtnall rtn))))
4735 (if org-agenda-overriding-header
4736 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4737 nil 'face 'org-agenda-structure) "\n")
4738 (insert "Global list of TODO items of type: ")
4739 (add-text-properties (point-min) (1- (point))
4740 (list 'face 'org-agenda-structure
4741 'short-heading
4742 (concat "ToDo: "
4743 (or org-select-this-todo-keyword "ALL"))))
4744 (org-agenda-mark-header-line (point-min))
4745 (insert (org-agenda-propertize-selected-todo-keywords
4746 org-select-this-todo-keyword))
4747 (setq pos (point))
4748 (unless org-agenda-multi
4749 (insert "Available with `N r': (0)[ALL]")
4750 (let ((n 0) s)
4751 (mapc (lambda (x)
4752 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4753 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4754 (insert "\n "))
4755 (insert " " s))
4756 kwds))
4757 (insert "\n"))
4758 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4759 (org-agenda-mark-header-line (point-min))
4760 (when rtnall
4761 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4762 (goto-char (point-min))
4763 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4764 (add-text-properties (point-min) (point-max)
4765 `(org-agenda-type todo
4766 org-last-args ,arg
4767 org-redo-cmd ,org-agenda-redo-command
4768 org-series-cmd ,org-cmd))
4769 (org-agenda-finalize)
4770 (setq buffer-read-only t))))
4772 ;;; Agenda tags match
4774 ;;;###autoload
4775 (defun org-tags-view (&optional todo-only match)
4776 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4777 The prefix arg TODO-ONLY limits the search to TODO entries."
4778 (interactive "P")
4779 (if org-agenda-overriding-arguments
4780 (setq todo-only (car org-agenda-overriding-arguments)
4781 match (nth 1 org-agenda-overriding-arguments)))
4782 (let* ((org-tags-match-list-sublevels
4783 org-tags-match-list-sublevels)
4784 (completion-ignore-case t)
4785 rtn rtnall files file pos matcher
4786 buffer)
4787 (when (and (stringp match) (not (string-match "\\S-" match)))
4788 (setq match nil))
4789 (catch 'exit
4790 (if org-agenda-sticky
4791 (setq org-agenda-buffer-name
4792 (if (stringp match)
4793 (format "*Org Agenda(%s:%s)*"
4794 (or org-keys (or (and todo-only "M") "m")) match)
4795 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4796 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4797 ;; expanding tags within `org-make-tags-matcher'
4798 (org-agenda-prepare (concat "TAGS " match))
4799 (setq matcher (org-make-tags-matcher match)
4800 match (car matcher) matcher (cdr matcher))
4801 (org-compile-prefix-format 'tags)
4802 (org-set-sorting-strategy 'tags)
4803 (setq org-agenda-query-string match)
4804 (setq org-agenda-redo-command
4805 (list 'org-tags-view `(quote ,todo-only)
4806 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4807 (setq files (org-agenda-files nil 'ifmode)
4808 rtnall nil)
4809 (while (setq file (pop files))
4810 (catch 'nextfile
4811 (org-check-agenda-file file)
4812 (setq buffer (if (file-exists-p file)
4813 (org-get-agenda-file-buffer file)
4814 (error "No such file %s" file)))
4815 (if (not buffer)
4816 ;; If file does not exist, error message to agenda
4817 (setq rtn (list
4818 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4819 rtnall (append rtnall rtn))
4820 (with-current-buffer buffer
4821 (unless (derived-mode-p 'org-mode)
4822 (error "Agenda file %s is not in `org-mode'" file))
4823 (save-excursion
4824 (save-restriction
4825 (if (eq buffer org-agenda-restrict)
4826 (narrow-to-region org-agenda-restrict-begin
4827 org-agenda-restrict-end)
4828 (widen))
4829 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4830 (setq rtnall (append rtnall rtn))))))))
4831 (if org-agenda-overriding-header
4832 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4833 nil 'face 'org-agenda-structure) "\n")
4834 (insert "Headlines with TAGS match: ")
4835 (add-text-properties (point-min) (1- (point))
4836 (list 'face 'org-agenda-structure
4837 'short-heading
4838 (concat "Match: " match)))
4839 (setq pos (point))
4840 (insert match "\n")
4841 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4842 (setq pos (point))
4843 (unless org-agenda-multi
4844 (insert "Press `C-u r' to search again with new search string\n"))
4845 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4846 (org-agenda-mark-header-line (point-min))
4847 (when rtnall
4848 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4849 (goto-char (point-min))
4850 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4851 (add-text-properties (point-min) (point-max)
4852 `(org-agenda-type tags
4853 org-last-args (,todo-only ,match)
4854 org-redo-cmd ,org-agenda-redo-command
4855 org-series-cmd ,org-cmd))
4856 (org-agenda-finalize)
4857 (setq buffer-read-only t))))
4859 ;;; Agenda Finding stuck projects
4861 (defvar org-agenda-skip-regexp nil
4862 "Regular expression used in skipping subtrees for the agenda.
4863 This is basically a temporary global variable that can be set and then
4864 used by user-defined selections using `org-agenda-skip-function'.")
4866 (defvar org-agenda-overriding-header nil
4867 "When set during agenda, todo and tags searches it replaces the header.
4868 This variable should not be set directly, but custom commands can bind it
4869 in the options section.")
4871 (defun org-agenda-skip-entry-when-regexp-matches ()
4872 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4873 If yes, it returns the end position of this entry, causing agenda commands
4874 to skip the entry but continuing the search in the subtree. This is a
4875 function that can be put into `org-agenda-skip-function' for the duration
4876 of a command."
4877 (let ((end (save-excursion (org-end-of-subtree t)))
4878 skip)
4879 (save-excursion
4880 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4881 (and skip end)))
4883 (defun org-agenda-skip-subtree-when-regexp-matches ()
4884 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4885 If yes, it returns the end position of this tree, causing agenda commands
4886 to skip this subtree. This is a function that can be put into
4887 `org-agenda-skip-function' for the duration of a command."
4888 (let ((end (save-excursion (org-end-of-subtree t)))
4889 skip)
4890 (save-excursion
4891 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4892 (and skip end)))
4894 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4895 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4896 If yes, it returns the end position of the current entry (NOT the tree),
4897 causing agenda commands to skip the entry but continuing the search in
4898 the subtree. This is a function that can be put into
4899 `org-agenda-skip-function' for the duration of a command. An important
4900 use of this function is for the stuck project list."
4901 (let ((end (save-excursion (org-end-of-subtree t)))
4902 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4903 skip)
4904 (save-excursion
4905 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4906 (and skip entry-end)))
4908 (defun org-agenda-skip-entry-if (&rest conditions)
4909 "Skip entry if any of CONDITIONS is true.
4910 See `org-agenda-skip-if' for details."
4911 (org-agenda-skip-if nil conditions))
4913 (defun org-agenda-skip-subtree-if (&rest conditions)
4914 "Skip subtree if any of CONDITIONS is true.
4915 See `org-agenda-skip-if' for details."
4916 (org-agenda-skip-if t conditions))
4918 (defun org-agenda-skip-if (subtree conditions)
4919 "Checks current entity for CONDITIONS.
4920 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4921 the entry (i.e. the text before the next heading) is checked.
4923 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4924 from different tests. Valid conditions are:
4926 scheduled Check if there is a scheduled cookie
4927 notscheduled Check if there is no scheduled cookie
4928 deadline Check if there is a deadline
4929 notdeadline Check if there is no deadline
4930 timestamp Check if there is a timestamp (also deadline or scheduled)
4931 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4932 regexp Check if regexp matches
4933 notregexp Check if regexp does not match.
4934 todo Check if TODO keyword matches
4935 nottodo Check if TODO keyword does not match
4937 The regexp is taken from the conditions list, it must come right after
4938 the `regexp' or `notregexp' element.
4940 `todo' and `nottodo' accept as an argument a list of todo
4941 keywords, which may include \"*\" to match any todo keyword.
4943 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4945 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4947 Instead of a list, a keyword class may be given. For example:
4949 (org-agenda-skip-entry-if 'nottodo 'done)
4951 would skip entries that haven't been marked with any of \"DONE\"
4952 keywords. Possible classes are: `todo', `done', `any'.
4954 If any of these conditions is met, this function returns the end point of
4955 the entity, causing the search to continue from there. This is a function
4956 that can be put into `org-agenda-skip-function' for the duration of a command."
4957 (let (beg end m)
4958 (org-back-to-heading t)
4959 (setq beg (point)
4960 end (if subtree
4961 (progn (org-end-of-subtree t) (point))
4962 (progn (outline-next-heading) (1- (point)))))
4963 (goto-char beg)
4964 (and
4966 (and (memq 'scheduled conditions)
4967 (re-search-forward org-scheduled-time-regexp end t))
4968 (and (memq 'notscheduled conditions)
4969 (not (re-search-forward org-scheduled-time-regexp end t)))
4970 (and (memq 'deadline conditions)
4971 (re-search-forward org-deadline-time-regexp end t))
4972 (and (memq 'notdeadline conditions)
4973 (not (re-search-forward org-deadline-time-regexp end t)))
4974 (and (memq 'timestamp conditions)
4975 (re-search-forward org-ts-regexp end t))
4976 (and (memq 'nottimestamp conditions)
4977 (not (re-search-forward org-ts-regexp end t)))
4978 (and (setq m (memq 'regexp conditions))
4979 (stringp (nth 1 m))
4980 (re-search-forward (nth 1 m) end t))
4981 (and (setq m (memq 'notregexp conditions))
4982 (stringp (nth 1 m))
4983 (not (re-search-forward (nth 1 m) end t)))
4984 (and (or
4985 (setq m (memq 'nottodo conditions))
4986 (setq m (memq 'todo-unblocked conditions))
4987 (setq m (memq 'nottodo-unblocked conditions))
4988 (setq m (memq 'todo conditions)))
4989 (org-agenda-skip-if-todo m end)))
4990 end)))
4992 (defun org-agenda-skip-if-todo (args end)
4993 "Helper function for `org-agenda-skip-if', do not use it directly.
4994 ARGS is a list with first element either `todo', `nottodo',
4995 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4996 a list of TODO keywords, or a state symbol `todo' or `done' or
4997 `any'."
4998 (let ((kw (car args))
4999 (arg (cadr args))
5000 todo-wds todo-re)
5001 (setq todo-wds
5002 (org-uniquify
5003 (cond
5004 ((listp arg) ;; list of keywords
5005 (if (member "*" arg)
5006 (mapcar 'substring-no-properties org-todo-keywords-1)
5007 arg))
5008 ((symbolp arg) ;; keyword class name
5009 (cond
5010 ((eq arg 'todo)
5011 (org-delete-all org-done-keywords
5012 (mapcar 'substring-no-properties
5013 org-todo-keywords-1)))
5014 ((eq arg 'done) org-done-keywords)
5015 ((eq arg 'any)
5016 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5017 (setq todo-re
5018 (concat "^\\*+[ \t]+\\<\\("
5019 (mapconcat 'identity todo-wds "\\|")
5020 "\\)\\>"))
5021 (cond
5022 ((eq kw 'todo) (re-search-forward todo-re end t))
5023 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5024 ((eq kw 'todo-unblocked)
5025 (catch 'unblocked
5026 (while (re-search-forward todo-re end t)
5027 (or (org-entry-blocked-p) (throw 'unblocked t)))
5028 nil))
5029 ((eq kw 'nottodo-unblocked)
5030 (catch 'unblocked
5031 (while (re-search-forward todo-re end t)
5032 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5036 ;;;###autoload
5037 (defun org-agenda-list-stuck-projects (&rest ignore)
5038 "Create agenda view for projects that are stuck.
5039 Stuck projects are project that have no next actions. For the definitions
5040 of what a project is and how to check if it stuck, customize the variable
5041 `org-stuck-projects'."
5042 (interactive)
5043 (let* ((org-agenda-skip-function
5044 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5045 ;; We could have used org-agenda-skip-if here.
5046 (org-agenda-overriding-header
5047 (or org-agenda-overriding-header "List of stuck projects: "))
5048 (matcher (nth 0 org-stuck-projects))
5049 (todo (nth 1 org-stuck-projects))
5050 (todo-wds (if (member "*" todo)
5051 (progn
5052 (org-agenda-prepare-buffers (org-agenda-files
5053 nil 'ifmode))
5054 (org-delete-all
5055 org-done-keywords-for-agenda
5056 (copy-sequence org-todo-keywords-for-agenda)))
5057 todo))
5058 (todo-re (concat "^\\*+[ \t]+\\("
5059 (mapconcat 'identity todo-wds "\\|")
5060 "\\)\\>"))
5061 (tags (nth 2 org-stuck-projects))
5062 (tags-re (if (member "*" tags)
5063 (concat org-outline-regexp-bol
5064 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5065 (if tags
5066 (concat org-outline-regexp-bol
5067 ".*:\\("
5068 (mapconcat 'identity tags "\\|")
5069 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5070 (gen-re (nth 3 org-stuck-projects))
5071 (re-list
5072 (delq nil
5073 (list
5074 (if todo todo-re)
5075 (if tags tags-re)
5076 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5077 gen-re)))))
5078 (setq org-agenda-skip-regexp
5079 (if re-list
5080 (mapconcat 'identity re-list "\\|")
5081 (error "No information how to identify unstuck projects")))
5082 (org-tags-view nil matcher)
5083 (setq org-agenda-buffer-name (buffer-name))
5084 (with-current-buffer org-agenda-buffer-name
5085 (setq org-agenda-redo-command
5086 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5088 ;;; Diary integration
5090 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5091 (defvar diary-list-entries-hook)
5092 (defvar diary-time-regexp)
5093 (defun org-get-entries-from-diary (date)
5094 "Get the (Emacs Calendar) diary entries for DATE."
5095 (require 'diary-lib)
5096 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5097 (diary-display-hook '(fancy-diary-display))
5098 (diary-display-function 'fancy-diary-display)
5099 (pop-up-frames nil)
5100 (diary-list-entries-hook
5101 (cons 'org-diary-default-entry diary-list-entries-hook))
5102 (diary-file-name-prefix nil) ; turn this feature off
5103 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5104 entries
5105 (org-disable-agenda-to-diary t))
5106 (save-excursion
5107 (save-window-excursion
5108 (funcall (if (fboundp 'diary-list-entries)
5109 'diary-list-entries 'list-diary-entries)
5110 date 1)))
5111 (if (not (get-buffer diary-fancy-buffer))
5112 (setq entries nil)
5113 (with-current-buffer diary-fancy-buffer
5114 (setq buffer-read-only nil)
5115 (if (zerop (buffer-size))
5116 ;; No entries
5117 (setq entries nil)
5118 ;; Omit the date and other unnecessary stuff
5119 (org-agenda-cleanup-fancy-diary)
5120 ;; Add prefix to each line and extend the text properties
5121 (if (zerop (buffer-size))
5122 (setq entries nil)
5123 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5124 (setq entries
5125 (with-temp-buffer
5126 (insert entries) (goto-char (point-min))
5127 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5128 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5129 (replace-match (concat "; " (match-string 1)))))
5130 (buffer-string)))))
5131 (set-buffer-modified-p nil)
5132 (kill-buffer diary-fancy-buffer)))
5133 (when entries
5134 (setq entries (org-split-string entries "\n"))
5135 (setq entries
5136 (mapcar
5137 (lambda (x)
5138 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5139 ;; Extend the text properties to the beginning of the line
5140 (org-add-props x (text-properties-at (1- (length x)) x)
5141 'type "diary" 'date date 'face 'org-agenda-diary))
5142 entries)))))
5144 (defvar org-agenda-cleanup-fancy-diary-hook nil
5145 "Hook run when the fancy diary buffer is cleaned up.")
5147 (defun org-agenda-cleanup-fancy-diary ()
5148 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5149 This gets rid of the date, the underline under the date, and
5150 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5151 date. It also removes lines that contain only whitespace."
5152 (goto-char (point-min))
5153 (if (looking-at ".*?:[ \t]*")
5154 (progn
5155 (replace-match "")
5156 (re-search-forward "\n=+$" nil t)
5157 (replace-match "")
5158 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5159 (re-search-forward "\n=+$" nil t)
5160 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5161 (goto-char (point-min))
5162 (while (re-search-forward "^ +\n" nil t)
5163 (replace-match ""))
5164 (goto-char (point-min))
5165 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5166 (replace-match ""))
5167 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5169 ;; Make sure entries from the diary have the right text properties.
5170 (eval-after-load "diary-lib"
5171 '(if (boundp 'diary-modify-entry-list-string-function)
5172 ;; We can rely on the hook, nothing to do
5174 ;; Hook not available, must use advice to make this work
5175 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5176 "Make the position visible."
5177 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5178 (stringp string)
5179 buffer-file-name)
5180 (setq string (org-modify-diary-entry-string string))))))
5182 (defun org-modify-diary-entry-string (string)
5183 "Add text properties to string, allowing org-mode to act on it."
5184 (org-add-props string nil
5185 'mouse-face 'highlight
5186 'help-echo (if buffer-file-name
5187 (format "mouse-2 or RET jump to diary file %s"
5188 (abbreviate-file-name buffer-file-name))
5190 'org-agenda-diary-link t
5191 'org-marker (org-agenda-new-marker (point-at-bol))))
5193 (defun org-diary-default-entry ()
5194 "Add a dummy entry to the diary.
5195 Needed to avoid empty dates which mess up holiday display."
5196 ;; Catch the error if dealing with the new add-to-diary-alist
5197 (when org-disable-agenda-to-diary
5198 (condition-case nil
5199 (org-add-to-diary-list original-date "Org-mode dummy" "")
5200 (error
5201 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5203 (defun org-add-to-diary-list (&rest args)
5204 (if (fboundp 'diary-add-to-list)
5205 (apply 'diary-add-to-list args)
5206 (apply 'add-to-diary-list args)))
5208 (defvar org-diary-last-run-time nil)
5210 ;;;###autoload
5211 (defun org-diary (&rest args)
5212 "Return diary information from org files.
5213 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5214 It accesses org files and extracts information from those files to be
5215 listed in the diary. The function accepts arguments specifying what
5216 items should be listed. For a list of arguments allowed here, see the
5217 variable `org-agenda-entry-types'.
5219 The call in the diary file should look like this:
5221 &%%(org-diary) ~/path/to/some/orgfile.org
5223 Use a separate line for each org file to check. Or, if you omit the file name,
5224 all files listed in `org-agenda-files' will be checked automatically:
5226 &%%(org-diary)
5228 If you don't give any arguments (as in the example above), the default value
5229 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5230 So the example above may also be written as
5232 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5234 The function expects the lisp variables `entry' and `date' to be provided
5235 by the caller, because this is how the calendar works. Don't use this
5236 function from a program - use `org-agenda-get-day-entries' instead."
5237 (when (> (- (org-float-time)
5238 org-agenda-last-marker-time)
5240 ;; I am not sure if this works with sticky agendas, because the marker
5241 ;; list is then no longer a global variable.
5242 (org-agenda-reset-markers))
5243 (org-compile-prefix-format 'agenda)
5244 (org-set-sorting-strategy 'agenda)
5245 (setq args (or args org-agenda-entry-types))
5246 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5247 (list entry)
5248 (org-agenda-files t)))
5249 (time (org-float-time))
5250 file rtn results)
5251 (when (or (not org-diary-last-run-time)
5252 (> (- time
5253 org-diary-last-run-time)
5255 (org-agenda-prepare-buffers files))
5256 (setq org-diary-last-run-time time)
5257 ;; If this is called during org-agenda, don't return any entries to
5258 ;; the calendar. Org Agenda will list these entries itself.
5259 (if org-disable-agenda-to-diary (setq files nil))
5260 (while (setq file (pop files))
5261 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5262 (setq results (append results rtn)))
5263 (when results
5264 (setq results
5265 (mapcar (lambda (i) (replace-regexp-in-string
5266 org-bracket-link-regexp "\\3" i)) results))
5267 (concat (org-agenda-finalize-entries results) "\n"))))
5269 ;;; Agenda entry finders
5271 (defun org-agenda-get-day-entries (file date &rest args)
5272 "Does the work for `org-diary' and `org-agenda'.
5273 FILE is the path to a file to be checked for entries. DATE is date like
5274 the one returned by `calendar-current-date'. ARGS are symbols indicating
5275 which kind of entries should be extracted. For details about these, see
5276 the documentation of `org-diary'."
5277 (setq args (or args org-agenda-entry-types))
5278 (let* ((org-startup-folded nil)
5279 (org-startup-align-all-tables nil)
5280 (buffer (if (file-exists-p file)
5281 (org-get-agenda-file-buffer file)
5282 (error "No such file %s" file)))
5283 arg results rtn deadline-results)
5284 (if (not buffer)
5285 ;; If file does not exist, make sure an error message ends up in diary
5286 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5287 (with-current-buffer buffer
5288 (unless (derived-mode-p 'org-mode)
5289 (error "Agenda file %s is not in `org-mode'" file))
5290 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5291 (let ((case-fold-search nil))
5292 (save-excursion
5293 (save-restriction
5294 (if (eq buffer org-agenda-restrict)
5295 (narrow-to-region org-agenda-restrict-begin
5296 org-agenda-restrict-end)
5297 (widen))
5298 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5299 (while (setq arg (pop args))
5300 (cond
5301 ((and (eq arg :todo)
5302 (equal date (calendar-gregorian-from-absolute
5303 (org-today))))
5304 (setq rtn (org-agenda-get-todos))
5305 (setq results (append results rtn)))
5306 ((eq arg :timestamp)
5307 (setq rtn (org-agenda-get-blocks))
5308 (setq results (append results rtn))
5309 (setq rtn (org-agenda-get-timestamps deadline-results))
5310 (setq results (append results rtn)))
5311 ((eq arg :sexp)
5312 (setq rtn (org-agenda-get-sexps))
5313 (setq results (append results rtn)))
5314 ((eq arg :scheduled)
5315 (setq rtn (org-agenda-get-scheduled deadline-results))
5316 (setq results (append results rtn)))
5317 ((eq arg :scheduled*)
5318 (setq rtn (org-agenda-get-scheduled deadline-results t))
5319 (setq results (append results rtn)))
5320 ((eq arg :closed)
5321 (setq rtn (org-agenda-get-progress))
5322 (setq results (append results rtn)))
5323 ((eq arg :deadline)
5324 (setq rtn (org-agenda-get-deadlines))
5325 (setq deadline-results (copy-sequence rtn))
5326 (setq results (append results rtn)))
5327 ((eq arg :deadline*)
5328 (setq rtn (org-agenda-get-deadlines t))
5329 (setq deadline-results (copy-sequence rtn))
5330 (setq results (append results rtn))))))))
5331 results))))
5333 (defsubst org-em (x y list)
5334 "Is X or Y a member of LIST?"
5335 (or (memq x list) (memq y list)))
5337 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5338 (defvar org-agenda-sorting-strategy-selected nil)
5340 (defun org-agenda-get-todos ()
5341 "Return the TODO information for agenda display."
5342 (let* ((props (list 'face nil
5343 'done-face 'org-agenda-done
5344 'org-not-done-regexp org-not-done-regexp
5345 'org-todo-regexp org-todo-regexp
5346 'org-complex-heading-regexp org-complex-heading-regexp
5347 'mouse-face 'highlight
5348 'help-echo
5349 (format "mouse-2 or RET jump to org file %s"
5350 (abbreviate-file-name buffer-file-name))))
5351 (regexp (format org-heading-keyword-regexp-format
5352 (cond
5353 ((and org-select-this-todo-keyword
5354 (equal org-select-this-todo-keyword "*"))
5355 org-todo-regexp)
5356 (org-select-this-todo-keyword
5357 (concat "\\("
5358 (mapconcat 'identity
5359 (org-split-string
5360 org-select-this-todo-keyword
5361 "|")
5362 "\\|") "\\)"))
5363 (t org-not-done-regexp))))
5364 marker priority category category-pos level tags todo-state ts-date ts-date-type
5365 ee txt beg end inherited-tags todo-state-end-pos)
5366 (goto-char (point-min))
5367 (while (re-search-forward regexp nil t)
5368 (catch :skip
5369 (save-match-data
5370 (beginning-of-line)
5371 (org-agenda-skip)
5372 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5373 (unless (and (setq todo-state (org-get-todo-state))
5374 (setq todo-state-end-pos (match-end 2)))
5375 (goto-char end)
5376 (throw :skip nil))
5377 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5378 (goto-char (1+ beg))
5379 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5380 (throw :skip nil)))
5381 (goto-char (match-beginning 2))
5382 (setq marker (org-agenda-new-marker (match-beginning 0))
5383 category (org-get-category)
5384 ts-date (let (ts)
5385 (save-match-data
5386 (cond ((org-em 'scheduled-up 'scheduled-down
5387 org-agenda-sorting-strategy-selected)
5388 (setq ts (org-entry-get (point) "SCHEDULED")
5389 ts-date-type " scheduled"))
5390 ((org-em 'deadline-up 'deadline-down
5391 org-agenda-sorting-strategy-selected)
5392 (setq ts (org-entry-get (point) "DEADLINE")
5393 ts-date-type " deadline"))
5394 ((org-em 'ts-up 'ts-down
5395 org-agenda-sorting-strategy-selected)
5396 (setq ts (org-entry-get (point) "TIMESTAMP")
5397 ts-date-type " timestamp"))
5398 ((org-em 'tsia-up 'tsia-down
5399 org-agenda-sorting-strategy-selected)
5400 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5401 ts-date-type " timestamp_ia"))
5402 ((org-em 'timestamp-up 'timestamp-down
5403 org-agenda-sorting-strategy-selected)
5404 (setq ts (or (org-entry-get (point) "SCHEDULED")
5405 (org-entry-get (point) "DEADLINE")
5406 (org-entry-get (point) "TIMESTAMP")
5407 (org-entry-get (point) "TIMESTAMP_IA"))
5408 ts-date-type ""))
5409 (t (setq ts-date-type "")))
5410 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5411 category-pos (get-text-property (point) 'org-category-position)
5412 txt (org-trim
5413 (buffer-substring (match-beginning 2) (match-end 0)))
5414 inherited-tags
5415 (or (eq org-agenda-show-inherited-tags 'always)
5416 (and (listp org-agenda-show-inherited-tags)
5417 (memq 'todo org-agenda-show-inherited-tags))
5418 (and (eq org-agenda-show-inherited-tags t)
5419 (or (eq org-agenda-use-tag-inheritance t)
5420 (memq 'todo org-agenda-use-tag-inheritance))))
5421 tags (org-get-tags-at nil (not inherited-tags))
5422 level (make-string (org-reduced-level (org-outline-level)) ? )
5423 txt (org-agenda-format-item "" txt level category tags t)
5424 priority (1+ (org-get-priority txt)))
5425 (org-add-props txt props
5426 'org-marker marker 'org-hd-marker marker
5427 'priority priority 'org-category category
5428 'level level
5429 'ts-date ts-date
5430 'org-category-position category-pos
5431 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5432 (push txt ee)
5433 (if org-agenda-todo-list-sublevels
5434 (goto-char todo-state-end-pos)
5435 (org-end-of-subtree 'invisible))))
5436 (nreverse ee)))
5438 (defun org-agenda-todo-custom-ignore-p (time n)
5439 "Check whether timestamp is farther away than n number of days.
5440 This function is invoked if `org-agenda-todo-ignore-deadlines',
5441 `org-agenda-todo-ignore-scheduled' or
5442 `org-agenda-todo-ignore-timestamp' is set to an integer."
5443 (let ((days (org-time-stamp-to-now
5444 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5445 (if (>= n 0)
5446 (>= days n)
5447 (<= days n))))
5449 ;;;###autoload
5450 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5451 (&optional end)
5452 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5453 (when (or org-agenda-todo-ignore-with-date
5454 org-agenda-todo-ignore-scheduled
5455 org-agenda-todo-ignore-deadlines
5456 org-agenda-todo-ignore-timestamp)
5457 (setq end (or end (save-excursion (outline-next-heading) (point))))
5458 (save-excursion
5459 (or (and org-agenda-todo-ignore-with-date
5460 (re-search-forward org-ts-regexp end t))
5461 (and org-agenda-todo-ignore-scheduled
5462 (re-search-forward org-scheduled-time-regexp end t)
5463 (cond
5464 ((eq org-agenda-todo-ignore-scheduled 'future)
5465 (> (org-time-stamp-to-now
5466 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5467 ((eq org-agenda-todo-ignore-scheduled 'past)
5468 (<= (org-time-stamp-to-now
5469 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5470 ((numberp org-agenda-todo-ignore-scheduled)
5471 (org-agenda-todo-custom-ignore-p
5472 (match-string 1) org-agenda-todo-ignore-scheduled))
5473 (t)))
5474 (and org-agenda-todo-ignore-deadlines
5475 (re-search-forward org-deadline-time-regexp end t)
5476 (cond
5477 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5478 ((eq org-agenda-todo-ignore-deadlines 'far)
5479 (not (org-deadline-close (match-string 1))))
5480 ((eq org-agenda-todo-ignore-deadlines 'future)
5481 (> (org-time-stamp-to-now
5482 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5483 ((eq org-agenda-todo-ignore-deadlines 'past)
5484 (<= (org-time-stamp-to-now
5485 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5486 ((numberp org-agenda-todo-ignore-deadlines)
5487 (org-agenda-todo-custom-ignore-p
5488 (match-string 1) org-agenda-todo-ignore-deadlines))
5489 (t (org-deadline-close (match-string 1)))))
5490 (and org-agenda-todo-ignore-timestamp
5491 (let ((buffer (current-buffer))
5492 (regexp
5493 (concat
5494 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5495 (start (point)))
5496 ;; Copy current buffer into a temporary one
5497 (with-temp-buffer
5498 (insert-buffer-substring buffer start end)
5499 (goto-char (point-min))
5500 ;; Delete SCHEDULED and DEADLINE items
5501 (while (re-search-forward regexp end t)
5502 (delete-region (match-beginning 0) (match-end 0)))
5503 (goto-char (point-min))
5504 ;; No search for timestamp left
5505 (when (re-search-forward org-ts-regexp nil t)
5506 (cond
5507 ((eq org-agenda-todo-ignore-timestamp 'future)
5508 (> (org-time-stamp-to-now
5509 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5510 ((eq org-agenda-todo-ignore-timestamp 'past)
5511 (<= (org-time-stamp-to-now
5512 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5513 ((numberp org-agenda-todo-ignore-timestamp)
5514 (org-agenda-todo-custom-ignore-p
5515 (match-string 1) org-agenda-todo-ignore-timestamp))
5516 (t))))))))))
5518 (defun org-agenda-get-timestamps (&optional deadline-results)
5519 "Return the date stamp information for agenda display."
5520 (let* ((props (list 'face 'org-agenda-calendar-event
5521 'org-not-done-regexp org-not-done-regexp
5522 'org-todo-regexp org-todo-regexp
5523 'org-complex-heading-regexp org-complex-heading-regexp
5524 'mouse-face 'highlight
5525 'help-echo
5526 (format "mouse-2 or RET jump to org file %s"
5527 (abbreviate-file-name buffer-file-name))))
5528 (d1 (calendar-absolute-from-gregorian date))
5530 (deadline-position-alist
5531 (mapcar (lambda (a) (and (setq mm (get-text-property
5532 0 'org-hd-marker a))
5533 (cons (marker-position mm) a)))
5534 deadline-results))
5535 (remove-re org-ts-regexp)
5536 (regexp
5537 (concat
5538 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5539 (regexp-quote
5540 (substring
5541 (format-time-string
5542 (car org-time-stamp-formats)
5543 (apply 'encode-time ; DATE bound by calendar
5544 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5545 1 11))
5546 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5547 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5548 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5549 donep tmp priority category category-pos level ee txt timestr tags
5550 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5551 inherited-tags ts-date)
5552 (goto-char (point-min))
5553 (while (setq end-of-match (re-search-forward regexp nil t))
5554 (setq b0 (match-beginning 0)
5555 b3 (match-beginning 3) e3 (match-end 3)
5556 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5557 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5558 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5559 (member todo-state
5560 org-agenda-repeating-timestamp-show-all)))
5561 (catch :skip
5562 (and (org-at-date-range-p) (throw :skip nil))
5563 (org-agenda-skip)
5564 (if (and (match-end 1)
5565 (not (= d1 (org-time-string-to-absolute
5566 (match-string 1) d1 nil show-all
5567 (current-buffer) b0))))
5568 (throw :skip nil))
5569 (if (and e3
5570 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5571 (throw :skip nil))
5572 (setq tmp (buffer-substring (max (point-min)
5573 (- b0 org-ds-keyword-length))
5575 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5576 inactivep (= (char-after b0) ?\[)
5577 deadlinep (string-match org-deadline-regexp tmp)
5578 scheduledp (string-match org-scheduled-regexp tmp)
5579 closedp (and org-agenda-include-inactive-timestamps
5580 (string-match org-closed-string tmp))
5581 clockp (and org-agenda-include-inactive-timestamps
5582 (or (string-match org-clock-string tmp)
5583 (string-match "]-+\\'" tmp)))
5584 warntime (get-text-property (point) 'org-appt-warntime)
5585 donep (member todo-state org-done-keywords))
5586 (if (or scheduledp deadlinep closedp clockp
5587 (and donep org-agenda-skip-timestamp-if-done))
5588 (throw :skip t))
5589 (if (string-match ">" timestr)
5590 ;; substring should only run to end of time stamp
5591 (setq timestr (substring timestr 0 (match-end 0))))
5592 (setq marker (org-agenda-new-marker b0)
5593 category (org-get-category b0)
5594 category-pos (get-text-property b0 'org-category-position))
5595 (save-excursion
5596 (if (not (re-search-backward org-outline-regexp-bol nil t))
5597 (throw :skip nil)
5598 (goto-char (match-beginning 0))
5599 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5600 (assoc (point) deadline-position-alist))
5601 (throw :skip nil))
5602 (setq hdmarker (org-agenda-new-marker)
5603 inherited-tags
5604 (or (eq org-agenda-show-inherited-tags 'always)
5605 (and (listp org-agenda-show-inherited-tags)
5606 (memq 'agenda org-agenda-show-inherited-tags))
5607 (and (eq org-agenda-show-inherited-tags t)
5608 (or (eq org-agenda-use-tag-inheritance t)
5609 (memq 'agenda org-agenda-use-tag-inheritance))))
5610 tags (org-get-tags-at nil (not inherited-tags))
5611 level (make-string (org-reduced-level (org-outline-level)) ? ))
5612 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5613 (setq head (or (match-string 1) ""))
5614 (setq txt (org-agenda-format-item
5615 (if inactivep org-agenda-inactive-leader nil)
5616 head level category tags timestr
5617 remove-re habitp)))
5618 (setq priority (org-get-priority txt))
5619 (org-add-props txt props 'priority priority
5620 'org-marker marker 'org-hd-marker hdmarker
5621 'org-category category 'date date
5622 'level level
5623 'ts-date
5624 (ignore-errors (org-time-string-to-absolute timestr))
5625 'org-category-position category-pos
5626 'todo-state todo-state
5627 'warntime warntime
5628 'type "timestamp")
5629 (push txt ee))
5630 (if org-agenda-skip-additional-timestamps-same-entry
5631 (outline-next-heading)
5632 (goto-char end-of-match))))
5633 (nreverse ee)))
5635 (defun org-agenda-get-sexps ()
5636 "Return the sexp information for agenda display."
5637 (require 'diary-lib)
5638 (let* ((props (list 'face 'org-agenda-calendar-sexp
5639 'mouse-face 'highlight
5640 'help-echo
5641 (format "mouse-2 or RET jump to org file %s"
5642 (abbreviate-file-name buffer-file-name))))
5643 (regexp "^&?%%(")
5644 marker category extra category-pos level ee txt tags entry
5645 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5646 (goto-char (point-min))
5647 (while (re-search-forward regexp nil t)
5648 (catch :skip
5649 (org-agenda-skip)
5650 (setq beg (match-beginning 0))
5651 (goto-char (1- (match-end 0)))
5652 (setq b (point))
5653 (forward-sexp 1)
5654 (setq sexp (buffer-substring b (point)))
5655 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5656 (org-trim (match-string 1))
5657 ""))
5658 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5659 (when result
5660 (setq marker (org-agenda-new-marker beg)
5661 level (make-string (org-reduced-level (org-outline-level)) ? )
5662 category (org-get-category beg)
5663 category-pos (get-text-property beg 'org-category-position)
5664 inherited-tags
5665 (or (eq org-agenda-show-inherited-tags 'always)
5666 (and (listp org-agenda-show-inherited-tags)
5667 (memq 'agenda org-agenda-show-inherited-tags))
5668 (and (eq org-agenda-show-inherited-tags t)
5669 (or (eq org-agenda-use-tag-inheritance t)
5670 (memq 'agenda org-agenda-use-tag-inheritance))))
5671 tags (org-get-tags-at nil (not inherited-tags))
5672 todo-state (org-get-todo-state)
5673 warntime (get-text-property (point) 'org-appt-warntime)
5674 extra nil)
5676 (dolist (r (if (stringp result)
5677 (list result)
5678 result)) ;; we expect a list here
5679 (when (and org-agenda-diary-sexp-prefix
5680 (string-match org-agenda-diary-sexp-prefix r))
5681 (setq extra (match-string 0 r)
5682 r (replace-match "" nil nil r)))
5683 (if (string-match "\\S-" r)
5684 (setq txt r)
5685 (setq txt "SEXP entry returned empty string"))
5686 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5687 (org-add-props txt props 'org-marker marker
5688 'org-category category 'date date 'todo-state todo-state
5689 'org-category-position category-pos 'tags tags
5690 'level level
5691 'type "sexp" 'warntime warntime)
5692 (push txt ee)))))
5693 (nreverse ee)))
5695 ;; Calendar sanity: define some functions that are independent of
5696 ;; `calendar-date-style'.
5697 ;; Normally I would like to use ISO format when calling the diary functions,
5698 ;; but to make sure we still have Emacs 22 compatibility we bind
5699 ;; also `european-calendar-style' and use european format
5700 (defun org-anniversary (year month day &optional mark)
5701 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5702 (org-no-warnings
5703 (let ((calendar-date-style 'european) (european-calendar-style t))
5704 (diary-anniversary day month year mark))))
5705 (defun org-cyclic (N year month day &optional mark)
5706 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5707 (org-no-warnings
5708 (let ((calendar-date-style 'european) (european-calendar-style t))
5709 (diary-cyclic N day month year mark))))
5710 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5711 "Like `diary-block', but with fixed (ISO) order of arguments."
5712 (org-no-warnings
5713 (let ((calendar-date-style 'european) (european-calendar-style t))
5714 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5715 (defun org-date (year month day &optional mark)
5716 "Like `diary-date', but with fixed (ISO) order of arguments."
5717 (org-no-warnings
5718 (let ((calendar-date-style 'european) (european-calendar-style t))
5719 (diary-date day month year mark))))
5721 ;; Define the` org-class' function
5722 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5723 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5724 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5725 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5726 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5727 `holidays', then any date that is known by the Emacs calendar to be a
5728 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5729 then those holidays will be skipped."
5730 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5731 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5732 (d (calendar-absolute-from-gregorian date))
5733 (h (when skip-weeks (calendar-check-holidays date))))
5734 (and
5735 (<= date1 d)
5736 (<= d date2)
5737 (= (calendar-day-of-week date) dayname)
5738 (or (not skip-weeks)
5739 (progn
5740 (require 'cal-iso)
5741 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5742 (not (or (and h (memq 'holidays skip-weeks))
5743 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5744 entry)))
5746 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5747 "Like `org-class', but honor `calendar-date-style'.
5748 The order of the first 2 times 3 arguments depends on the variable
5749 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5750 So for American calendars, give this as MONTH DAY YEAR, for European as
5751 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5752 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5753 is any number of ISO weeks in the block period for which the item should
5754 be skipped.
5756 This function is here only for backward compatibility and it is deprecated,
5757 please use `org-class' instead."
5758 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5759 (date2 (org-order-calendar-date-args m2 d2 y2)))
5760 (org-class
5761 (nth 2 date1) (car date1) (nth 1 date1)
5762 (nth 2 date2) (car date2) (nth 1 date2)
5763 dayname skip-weeks)))
5764 (make-obsolete 'org-diary-class 'org-class "")
5766 (defalias 'org-get-closed 'org-agenda-get-progress)
5767 (defun org-agenda-get-progress ()
5768 "Return the logged TODO entries for agenda display."
5769 (let* ((props (list 'mouse-face 'highlight
5770 'org-not-done-regexp org-not-done-regexp
5771 'org-todo-regexp org-todo-regexp
5772 'org-complex-heading-regexp org-complex-heading-regexp
5773 'help-echo
5774 (format "mouse-2 or RET jump to org file %s"
5775 (abbreviate-file-name buffer-file-name))))
5776 (items (if (consp org-agenda-show-log-scoped)
5777 org-agenda-show-log-scoped
5778 (if (eq org-agenda-show-log-scoped 'clockcheck)
5779 '(clock)
5780 org-agenda-log-mode-items)))
5781 (parts
5782 (delq nil
5783 (list
5784 (if (memq 'closed items) (concat "\\<" org-closed-string))
5785 (if (memq 'clock items) (concat "\\<" org-clock-string))
5786 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5787 (parts-re (if parts (mapconcat 'identity parts "\\|")
5788 (error "`org-agenda-log-mode-items' is empty")))
5789 (regexp (concat
5790 "\\(" parts-re "\\)"
5791 " *\\["
5792 (regexp-quote
5793 (substring
5794 (format-time-string
5795 (car org-time-stamp-formats)
5796 (apply 'encode-time ; DATE bound by calendar
5797 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5798 1 11))))
5799 (org-agenda-search-headline-for-time nil)
5800 marker hdmarker priority category category-pos level tags closedp
5801 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5802 (goto-char (point-min))
5803 (while (re-search-forward regexp nil t)
5804 (catch :skip
5805 (org-agenda-skip)
5806 (setq marker (org-agenda-new-marker (match-beginning 0))
5807 closedp (equal (match-string 1) org-closed-string)
5808 statep (equal (string-to-char (match-string 1)) ?-)
5809 clockp (not (or closedp statep))
5810 state (and statep (match-string 2))
5811 category (org-get-category (match-beginning 0))
5812 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5813 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5814 (when (string-match "\\]" timestr)
5815 ;; substring should only run to end of time stamp
5816 (setq rest (substring timestr (match-end 0))
5817 timestr (substring timestr 0 (match-end 0)))
5818 (if (and (not closedp) (not statep)
5819 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5820 rest))
5821 (progn (setq timestr (concat (substring timestr 0 -1)
5822 "-" (match-string 1 rest) "]"))
5823 (setq clocked (match-string 2 rest)))
5824 (setq clocked "-")))
5825 (save-excursion
5826 (setq extra
5827 (cond
5828 ((not org-agenda-log-mode-add-notes) nil)
5829 (statep
5830 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5831 (match-string 1)))
5832 (clockp
5833 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5834 (match-string 1)))))
5835 (if (not (re-search-backward org-outline-regexp-bol nil t))
5836 (throw :skip nil)
5837 (goto-char (match-beginning 0))
5838 (setq hdmarker (org-agenda-new-marker)
5839 inherited-tags
5840 (or (eq org-agenda-show-inherited-tags 'always)
5841 (and (listp org-agenda-show-inherited-tags)
5842 (memq 'todo org-agenda-show-inherited-tags))
5843 (and (eq org-agenda-show-inherited-tags t)
5844 (or (eq org-agenda-use-tag-inheritance t)
5845 (memq 'todo org-agenda-use-tag-inheritance))))
5846 tags (org-get-tags-at nil (not inherited-tags))
5847 level (make-string (org-reduced-level (org-outline-level)) ? ))
5848 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5849 (setq txt (match-string 1))
5850 (when extra
5851 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5852 (setq txt (concat (substring txt 0 (match-beginning 1))
5853 " - " extra " " (match-string 2 txt)))
5854 (setq txt (concat txt " - " extra))))
5855 (setq txt (org-agenda-format-item
5856 (cond
5857 (closedp "Closed: ")
5858 (statep (concat "State: (" state ")"))
5859 (t (concat "Clocked: (" clocked ")")))
5860 txt level category tags timestr)))
5861 (setq priority 100000)
5862 (org-add-props txt props
5863 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5864 'priority priority 'org-category category
5865 'org-category-position category-pos
5866 'level level
5867 'type "closed" 'date date
5868 'undone-face 'org-warning 'done-face 'org-agenda-done)
5869 (push txt ee))
5870 (goto-char (point-at-eol))))
5871 (nreverse ee)))
5873 (defun org-agenda-show-clocking-issues ()
5874 "Add overlays, showing issues with clocking.
5875 See also the user option `org-agenda-clock-consistency-checks'."
5876 (interactive)
5877 (let* ((org-time-clocksum-use-effort-durations nil)
5878 (pl org-agenda-clock-consistency-checks)
5879 (re (concat "^[ \t]*"
5880 org-clock-string
5881 "[ \t]+"
5882 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5883 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5884 (tlstart 0.)
5885 (tlend 0.)
5886 (maxtime (org-hh:mm-string-to-minutes
5887 (or (plist-get pl :max-duration) "24:00")))
5888 (mintime (org-hh:mm-string-to-minutes
5889 (or (plist-get pl :min-duration) 0)))
5890 (maxgap (org-hh:mm-string-to-minutes
5891 ;; default 30:00 means never complain
5892 (or (plist-get pl :max-gap) "30:00")))
5893 (gapok (mapcar 'org-hh:mm-string-to-minutes
5894 (plist-get pl :gap-ok-around)))
5895 (def-face (or (plist-get pl :default-face)
5896 '((:background "DarkRed") (:foreground "white"))))
5897 issue face m te ts dt ov)
5898 (goto-char (point-min))
5899 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5900 (setq issue nil face def-face)
5901 (catch 'next
5902 (setq m (org-get-at-bol 'org-marker)
5903 te nil ts nil)
5904 (unless (and m (markerp m))
5905 (setq issue "No valid clock line") (throw 'next t))
5906 (org-with-point-at m
5907 (save-excursion
5908 (goto-char (point-at-bol))
5909 (unless (looking-at re)
5910 (error "No valid Clock line")
5911 (throw 'next t))
5912 (unless (match-end 3)
5913 (setq issue "No end time"
5914 face (or (plist-get pl :no-end-time-face) face))
5915 (throw 'next t))
5916 (setq ts (match-string 1)
5917 te (match-string 3)
5918 ts (org-float-time
5919 (apply 'encode-time (org-parse-time-string ts)))
5920 te (org-float-time
5921 (apply 'encode-time (org-parse-time-string te)))
5922 dt (- te ts))))
5923 (cond
5924 ((> dt (* 60 maxtime))
5925 ;; a very long clocking chunk
5926 (setq issue (format "Clocking interval is very long: %s"
5927 (org-minutes-to-clocksum-string
5928 (floor (/ (float dt) 60.))))
5929 face (or (plist-get pl :long-face) face)))
5930 ((< dt (* 60 mintime))
5931 ;; a very short clocking chunk
5932 (setq issue (format "Clocking interval is very short: %s"
5933 (org-minutes-to-clocksum-string
5934 (floor (/ (float dt) 60.))))
5935 face (or (plist-get pl :short-face) face)))
5936 ((and (> tlend 0) (< ts tlend))
5937 ;; Two clock entries are overlapping
5938 (setq issue (format "Clocking overlap: %d minutes"
5939 (/ (- tlend ts) 60))
5940 face (or (plist-get pl :overlap-face) face)))
5941 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5942 ;; There is a gap, lets see if we need to report it
5943 (unless (org-agenda-check-clock-gap tlend ts gapok)
5944 (setq issue (format "Clocking gap: %d minutes"
5945 (/ (- ts tlend) 60))
5946 face (or (plist-get pl :gap-face) face))))
5947 (t nil)))
5948 (setq tlend (or te tlend) tlstart (or ts tlstart))
5949 (when issue
5950 ;; OK, there was some issue, add an overlay to show the issue
5951 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5952 (overlay-put ov 'before-string
5953 (concat
5954 (org-add-props
5955 (format "%-43s" (concat " " issue))
5957 'face face)
5958 "\n"))
5959 (overlay-put ov 'evaporate t)))))
5961 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5962 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5963 (catch 'exit
5964 (unless ok-list
5965 ;; there are no OK times for gaps...
5966 (throw 'exit nil))
5967 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5968 ;; This is more than 24 hours, so it is OK.
5969 ;; because we have at least one OK time, that must be in the
5970 ;; 24 hour interval.
5971 (throw 'exit t))
5972 ;; We have a shorter gap.
5973 ;; Now we have to get the minute of the day when these times are
5974 (let* ((t1dec (decode-time (seconds-to-time t1)))
5975 (t2dec (decode-time (seconds-to-time t2)))
5976 ;; compute the minute on the day
5977 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5978 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5979 (when (< min2 min1)
5980 ;; if min2 is smaller than min1, this means it is on the next day.
5981 ;; Wrap it to after midnight.
5982 (setq min2 (+ min2 1440)))
5983 ;; Now check if any of the OK times is in the gap
5984 (mapc (lambda (x)
5985 ;; Wrap the time to after midnight if necessary
5986 (if (< x min1) (setq x (+ x 1440)))
5987 ;; Check if in interval
5988 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5989 ok-list)
5990 ;; Nope, this gap is not OK
5991 nil)))
5993 (defun org-agenda-get-deadlines (&optional with-hour)
5994 "Return the deadline information for agenda display.
5995 When WITH-HOUR is non-nil, only return deadlines with an hour
5996 specification like [h]h:mm."
5997 (let* ((props (list 'mouse-face 'highlight
5998 'org-not-done-regexp org-not-done-regexp
5999 'org-todo-regexp org-todo-regexp
6000 'org-complex-heading-regexp org-complex-heading-regexp
6001 'help-echo
6002 (format "mouse-2 or RET jump to org file %s"
6003 (abbreviate-file-name buffer-file-name))))
6004 (regexp (if with-hour
6005 org-deadline-time-hour-regexp
6006 org-deadline-time-regexp))
6007 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6008 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6009 (dl0 (car org-agenda-deadline-leaders))
6010 (dl1 (nth 1 org-agenda-deadline-leaders))
6011 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6012 d2 diff dfrac wdays pos pos1 category category-pos level
6013 tags suppress-prewarning ee txt head face s todo-state
6014 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6015 (goto-char (point-min))
6016 (while (re-search-forward regexp nil t)
6017 (catch :skip
6018 (org-agenda-skip)
6019 (setq s (match-string 1)
6020 txt nil
6021 pos (1- (match-beginning 1))
6022 todo-state (save-match-data (org-get-todo-state))
6023 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6024 (member todo-state
6025 org-agenda-repeating-timestamp-show-all))
6026 d2 (org-time-string-to-absolute
6027 s d1 'past show-all (current-buffer) pos)
6028 diff (- d2 d1))
6029 (setq suppress-prewarning
6030 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6031 (let ((item (buffer-substring (point-at-bol)
6032 (point-at-eol))))
6033 (save-match-data
6034 (and (string-match
6035 org-scheduled-time-regexp item)
6036 (match-string 1 item)))))))
6037 (cond
6038 ((not ds) nil)
6039 ;; The current item has a scheduled date (in ds), so
6040 ;; evaluate its prewarning lead time.
6041 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6042 ;; Use global prewarning-restart lead time.
6043 org-agenda-skip-deadline-prewarning-if-scheduled)
6044 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6045 'pre-scheduled)
6046 ;; Set prewarning to no earlier than scheduled.
6047 (min (- d2 (org-time-string-to-absolute
6048 ds d1 'past show-all (current-buffer) pos))
6049 org-deadline-warning-days))
6050 ;; Set prewarning to deadline.
6051 (t 0))))
6052 (setq wdays (if suppress-prewarning
6053 (let ((org-deadline-warning-days suppress-prewarning))
6054 (org-get-wdays s))
6055 (org-get-wdays s))
6056 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6057 upcomingp (and todayp (> diff 0)))
6058 ;; When to show a deadline in the calendar:
6059 ;; If the expiration is within wdays warning time.
6060 ;; Past-due deadlines are only shown on the current date
6061 (if (and (or (and (<= diff wdays)
6062 (and todayp (not org-agenda-only-exact-dates)))
6063 (= diff 0)))
6064 (save-excursion
6065 ;; (setq todo-state (org-get-todo-state))
6066 (setq donep (member todo-state org-done-keywords))
6067 (if (and donep
6068 (or org-agenda-skip-deadline-if-done
6069 (not (= diff 0))))
6070 (setq txt nil)
6071 (setq category (org-get-category)
6072 warntime (get-text-property (point) 'org-appt-warntime)
6073 category-pos (get-text-property (point) 'org-category-position))
6074 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6075 (throw :skip nil)
6076 (goto-char (match-end 0))
6077 (setq pos1 (match-beginning 0))
6078 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6079 (setq inherited-tags
6080 (or (eq org-agenda-show-inherited-tags 'always)
6081 (and (listp org-agenda-show-inherited-tags)
6082 (memq 'agenda org-agenda-show-inherited-tags))
6083 (and (eq org-agenda-show-inherited-tags t)
6084 (or (eq org-agenda-use-tag-inheritance t)
6085 (memq 'agenda org-agenda-use-tag-inheritance))))
6086 tags (org-get-tags-at pos1 (not inherited-tags)))
6087 (setq head (buffer-substring
6088 (point)
6089 (progn (skip-chars-forward "^\r\n")
6090 (point))))
6091 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6092 (setq timestr
6093 (concat (substring s (match-beginning 1)) " "))
6094 (setq timestr 'time))
6095 (setq txt (org-agenda-format-item
6096 (cond ((= diff 0) dl0)
6097 ((> diff 0)
6098 (if (functionp dl1)
6099 (funcall dl1 diff date)
6100 (format dl1 diff)))
6102 (if (functionp dl2)
6103 (funcall dl2 diff date)
6104 (format dl2 (if (string= dl2 dl1)
6105 diff (abs diff))))))
6106 head level category tags
6107 (if (not (= diff 0)) nil timestr)))))
6108 (when txt
6109 (setq face (org-agenda-deadline-face dfrac))
6110 (org-add-props txt props
6111 'org-marker (org-agenda-new-marker pos)
6112 'warntime warntime
6113 'level level
6114 'ts-date d2
6115 'org-hd-marker (org-agenda-new-marker pos1)
6116 'priority (+ (- diff)
6117 (org-get-priority txt))
6118 'org-category category
6119 'org-category-position category-pos
6120 'todo-state todo-state
6121 'type (if upcomingp "upcoming-deadline" "deadline")
6122 'date (if upcomingp date d2)
6123 'face (if donep 'org-agenda-done face)
6124 'undone-face face 'done-face 'org-agenda-done)
6125 (push txt ee))))))
6126 (nreverse ee)))
6128 (defun org-agenda-deadline-face (fraction)
6129 "Return the face to displaying a deadline item.
6130 FRACTION is what fraction of the head-warning time has passed."
6131 (let ((faces org-agenda-deadline-faces) f)
6132 (catch 'exit
6133 (while (setq f (pop faces))
6134 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6136 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6137 "Return the scheduled information for agenda display.
6138 When WITH-HOUR is non-nil, only return scheduled items with
6139 an hour specification like [h]h:mm."
6140 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6141 'org-todo-regexp org-todo-regexp
6142 'org-complex-heading-regexp org-complex-heading-regexp
6143 'done-face 'org-agenda-done
6144 'mouse-face 'highlight
6145 'help-echo
6146 (format "mouse-2 or RET jump to org file %s"
6147 (abbreviate-file-name buffer-file-name))))
6148 (regexp (if with-hour
6149 org-scheduled-time-hour-regexp
6150 org-scheduled-time-regexp))
6151 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6152 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6154 (deadline-position-alist
6155 (mapcar (lambda (a) (and (setq mm (get-text-property
6156 0 'org-hd-marker a))
6157 (cons (marker-position mm) a)))
6158 deadline-results))
6159 d2 diff pos pos1 category category-pos level tags donep
6160 ee txt head pastschedp todo-state face timestr s habitp show-all
6161 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6162 ddays)
6163 (goto-char (point-min))
6164 (while (re-search-forward regexp nil t)
6165 (catch :skip
6166 (org-agenda-skip)
6167 (setq s (match-string 1)
6168 txt nil
6169 pos (1- (match-beginning 1))
6170 todo-state (save-match-data (org-get-todo-state))
6171 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6172 (member todo-state
6173 org-agenda-repeating-timestamp-show-all))
6174 d2 (org-time-string-to-absolute
6175 s d1 'past show-all (current-buffer) pos)
6176 diff (- d2 d1)
6177 warntime (get-text-property (point) 'org-appt-warntime))
6178 (setq pastschedp (and todayp (< diff 0)))
6179 (setq did-habit-check-p nil)
6180 (setq suppress-delay
6181 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6182 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6183 (save-match-data
6184 (and (string-match
6185 org-deadline-time-regexp item)
6186 (match-string 1 item)))))))
6187 (cond
6188 ((not ds) nil)
6189 ;; The current item has a deadline date (in ds), so
6190 ;; evaluate its delay time.
6191 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6192 ;; Use global delay time.
6193 (- org-agenda-skip-scheduled-delay-if-deadline))
6194 ((eq org-agenda-skip-scheduled-delay-if-deadline
6195 'post-deadline)
6196 ;; Set delay to no later than deadline.
6197 (min (- d2 (org-time-string-to-absolute
6198 ds d1 'past show-all (current-buffer) pos))
6199 org-scheduled-delay-days))
6200 (t 0))))
6201 (setq ddays (if suppress-delay
6202 (let ((org-scheduled-delay-days suppress-delay))
6203 (org-get-wdays s t t))
6204 (org-get-wdays s t)))
6205 ;; Use a delay of 0 when there is a repeater and the delay is
6206 ;; of the form --3d
6207 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6208 (< (org-time-string-to-absolute s)
6209 (org-time-string-to-absolute
6210 s d2 'past nil (current-buffer) pos)))
6211 (setq ddays 0))
6212 ;; When to show a scheduled item in the calendar:
6213 ;; If it is on or past the date.
6214 (when (or (and (> ddays 0) (= diff (- ddays)))
6215 (and (zerop ddays) (= diff 0))
6216 (and (< (+ diff ddays) 0)
6217 (< (abs diff) org-scheduled-past-days)
6218 (and todayp (not org-agenda-only-exact-dates)))
6219 ;; org-is-habit-p uses org-entry-get, which is expansive
6220 ;; so we go extra mile to only call it once
6221 (and todayp
6222 (boundp 'org-habit-show-all-today)
6223 org-habit-show-all-today
6224 (setq did-habit-check-p t)
6225 (setq habitp (and (functionp 'org-is-habit-p)
6226 (org-is-habit-p)))))
6227 (save-excursion
6228 (setq donep (member todo-state org-done-keywords))
6229 (if (and donep
6230 (or org-agenda-skip-scheduled-if-done
6231 (not (= diff 0))
6232 (and (functionp 'org-is-habit-p)
6233 (org-is-habit-p))))
6234 (setq txt nil)
6235 (setq habitp (if did-habit-check-p habitp
6236 (and (functionp 'org-is-habit-p)
6237 (org-is-habit-p))))
6238 (setq category (org-get-category)
6239 category-pos (get-text-property (point) 'org-category-position))
6240 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6241 'repeated-after-deadline)
6242 (org-get-deadline-time (point))
6243 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6244 (throw :skip nil))
6245 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6246 (throw :skip nil)
6247 (goto-char (match-end 0))
6248 (setq pos1 (match-beginning 0))
6249 (if habitp
6250 (if (or (not org-habit-show-habits)
6251 (and (not todayp)
6252 (boundp 'org-habit-show-habits-only-for-today)
6253 org-habit-show-habits-only-for-today))
6254 (throw :skip nil))
6255 (if (and
6256 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6257 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6258 pastschedp))
6259 (setq mm (assoc pos1 deadline-position-alist)))
6260 (throw :skip nil)))
6261 (setq inherited-tags
6262 (or (eq org-agenda-show-inherited-tags 'always)
6263 (and (listp org-agenda-show-inherited-tags)
6264 (memq 'agenda org-agenda-show-inherited-tags))
6265 (and (eq org-agenda-show-inherited-tags t)
6266 (or (eq org-agenda-use-tag-inheritance t)
6267 (memq 'agenda org-agenda-use-tag-inheritance))))
6269 tags (org-get-tags-at nil (not inherited-tags)))
6270 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6271 (setq head (buffer-substring
6272 (point)
6273 (progn (skip-chars-forward "^\r\n") (point))))
6274 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6275 (setq timestr
6276 (concat (substring s (match-beginning 1)) " "))
6277 (setq timestr 'time))
6278 (setq txt (org-agenda-format-item
6279 (if (= diff 0)
6280 (car org-agenda-scheduled-leaders)
6281 (format (nth 1 org-agenda-scheduled-leaders)
6282 (- 1 diff)))
6283 head level category tags
6284 (if (not (= diff 0)) nil timestr)
6285 nil habitp))))
6286 (when txt
6287 (setq face
6288 (cond
6289 ((and (not habitp) pastschedp)
6290 'org-scheduled-previously)
6291 (todayp 'org-scheduled-today)
6292 (t 'org-scheduled))
6293 habitp (and habitp (org-habit-parse-todo)))
6294 (org-add-props txt props
6295 'undone-face face
6296 'face (if donep 'org-agenda-done face)
6297 'org-marker (org-agenda-new-marker pos)
6298 'org-hd-marker (org-agenda-new-marker pos1)
6299 'type (if pastschedp "past-scheduled" "scheduled")
6300 'date (if pastschedp d2 date)
6301 'ts-date d2
6302 'warntime warntime
6303 'level level
6304 'priority (if habitp
6305 (org-habit-get-priority habitp)
6306 (+ 94 (- 5 diff) (org-get-priority txt)))
6307 'org-category category
6308 'category-position category-pos
6309 'org-habit-p habitp
6310 'todo-state todo-state)
6311 (push txt ee))))))
6312 (nreverse ee)))
6314 (defun org-agenda-get-blocks ()
6315 "Return the date-range information for agenda display."
6316 (let* ((props (list 'face nil
6317 'org-not-done-regexp org-not-done-regexp
6318 'org-todo-regexp org-todo-regexp
6319 'org-complex-heading-regexp org-complex-heading-regexp
6320 'mouse-face 'highlight
6321 'help-echo
6322 (format "mouse-2 or RET jump to org file %s"
6323 (abbreviate-file-name buffer-file-name))))
6324 (regexp org-tr-regexp)
6325 (d0 (calendar-absolute-from-gregorian date))
6326 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6327 level todo-state tags pos head donep inherited-tags)
6328 (goto-char (point-min))
6329 (while (re-search-forward regexp nil t)
6330 (catch :skip
6331 (org-agenda-skip)
6332 (setq pos (point))
6333 (let ((start-time (match-string 1))
6334 (end-time (match-string 2)))
6335 (setq s1 (match-string 1)
6336 s2 (match-string 2)
6337 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6338 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6339 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6340 ;; Only allow days between the limits, because the normal
6341 ;; date stamps will catch the limits.
6342 (save-excursion
6343 (setq todo-state (org-get-todo-state))
6344 (setq donep (member todo-state org-done-keywords))
6345 (if (and donep org-agenda-skip-timestamp-if-done)
6346 (throw :skip t))
6347 (setq marker (org-agenda-new-marker (point)))
6348 (setq category (org-get-category)
6349 category-pos (get-text-property (point) 'org-category-position))
6350 (if (not (re-search-backward org-outline-regexp-bol nil t))
6351 (throw :skip nil)
6352 (goto-char (match-beginning 0))
6353 (setq hdmarker (org-agenda-new-marker (point))
6354 inherited-tags
6355 (or (eq org-agenda-show-inherited-tags 'always)
6356 (and (listp org-agenda-show-inherited-tags)
6357 (memq 'agenda org-agenda-show-inherited-tags))
6358 (and (eq org-agenda-show-inherited-tags t)
6359 (or (eq org-agenda-use-tag-inheritance t)
6360 (memq 'agenda org-agenda-use-tag-inheritance))))
6362 tags (org-get-tags-at nil (not inherited-tags)))
6363 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6364 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6365 (setq head (match-string 1))
6366 (let ((remove-re
6367 (if org-agenda-remove-timeranges-from-blocks
6368 (concat
6369 "<" (regexp-quote s1) ".*?>"
6370 "--"
6371 "<" (regexp-quote s2) ".*?>")
6372 nil)))
6373 (setq txt (org-agenda-format-item
6374 (format
6375 (nth (if (= d1 d2) 0 1)
6376 org-agenda-timerange-leaders)
6377 (1+ (- d0 d1)) (1+ (- d2 d1)))
6378 head level category tags
6379 (cond ((and (= d1 d0) (= d2 d0))
6380 (concat "<" start-time ">--<" end-time ">"))
6381 ((= d1 d0)
6382 (concat "<" start-time ">"))
6383 ((= d2 d0)
6384 (concat "<" end-time ">")))
6385 remove-re))))
6386 (org-add-props txt props
6387 'org-marker marker 'org-hd-marker hdmarker
6388 'type "block" 'date date
6389 'level level
6390 'todo-state todo-state
6391 'priority (org-get-priority txt) 'org-category category
6392 'org-category-position category-pos)
6393 (push txt ee))))
6394 (goto-char pos)))
6395 ;; Sort the entries by expiration date.
6396 (nreverse ee)))
6398 ;;; Agenda presentation and sorting
6400 (defvar org-prefix-has-time nil
6401 "A flag, set by `org-compile-prefix-format'.
6402 The flag is set if the currently compiled format contains a `%t'.")
6403 (defvar org-prefix-has-tag nil
6404 "A flag, set by `org-compile-prefix-format'.
6405 The flag is set if the currently compiled format contains a `%T'.")
6406 (defvar org-prefix-has-effort nil
6407 "A flag, set by `org-compile-prefix-format'.
6408 The flag is set if the currently compiled format contains a `%e'.")
6409 (defvar org-prefix-has-breadcrumbs nil
6410 "A flag, set by `org-compile-prefix-format'.
6411 The flag is set if the currently compiled format contains a `%b'.")
6412 (defvar org-prefix-category-length nil
6413 "Used by `org-compile-prefix-format' to remember the category field width.")
6414 (defvar org-prefix-category-max-length nil
6415 "Used by `org-compile-prefix-format' to remember the category field width.")
6417 (defun org-agenda-get-category-icon (category)
6418 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6419 (dolist (entry org-agenda-category-icon-alist)
6420 (when (org-string-match-p (car entry) category)
6421 (if (listp (cadr entry))
6422 (return (cadr entry))
6423 (return (apply 'create-image (cdr entry)))))))
6425 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6426 remove-re habitp)
6427 "Format TXT to be inserted into the agenda buffer.
6428 In particular, add the prefix and corresponding text properties.
6430 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6431 LEVEL may be a string to replace the `%l' specifier.
6432 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6433 category taken from local variable or file name. It will replace the `%c'
6434 specifier in the format.
6435 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6436 TXT for sorting of this entry, and for the `%t' specifier in the format.
6437 When DOTIME is a string, this string is searched for a time before TXT is.
6438 TAGS can be the tags of the headline.
6439 Any match of REMOVE-RE will be removed from TXT."
6440 ;; We keep the org-prefix-* variable values along with a compiled
6441 ;; formatter, so that multiple agendas existing at the same time do
6442 ;; not step on each other toes.
6444 ;; It was inconvenient to make these variables buffer local in
6445 ;; Agenda buffers, because this function expects to be called with
6446 ;; the buffer where item comes from being current, and not agenda
6447 ;; buffer
6448 (let* ((bindings (car org-prefix-format-compiled))
6449 (formatter (cadr org-prefix-format-compiled)))
6450 (loop for (var value) in bindings
6451 do (set var value))
6452 (save-match-data
6453 ;; Diary entries sometimes have extra whitespace at the beginning
6454 (setq txt (org-trim txt))
6456 ;; Fix the tags part in txt
6457 (setq txt (org-agenda-fix-displayed-tags
6458 txt tags
6459 org-agenda-show-inherited-tags
6460 org-agenda-hide-tags-regexp))
6462 (let* ((category (or category
6463 (if (stringp org-category)
6464 org-category
6465 (and org-category (symbol-name org-category)))
6466 (if buffer-file-name
6467 (file-name-sans-extension
6468 (file-name-nondirectory buffer-file-name))
6469 "")))
6470 (category-icon (org-agenda-get-category-icon category))
6471 (category-icon (if category-icon
6472 (propertize " " 'display category-icon)
6473 ""))
6474 ;; time, tag, effort are needed for the eval of the prefix format
6475 (tag (if tags (nth (1- (length tags)) tags) ""))
6476 time effort neffort
6477 (ts (if dotime (concat
6478 (if (stringp dotime) dotime "")
6479 (and org-agenda-search-headline-for-time txt))))
6480 (time-of-day (and dotime (org-get-time-of-day ts)))
6481 stamp plain s0 s1 s2 rtn srp l
6482 duration thecategory breadcrumbs)
6483 (and (derived-mode-p 'org-mode) buffer-file-name
6484 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6485 (when (and dotime time-of-day)
6486 ;; Extract starting and ending time and move them to prefix
6487 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6488 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6489 (setq s0 (match-string 0 ts)
6490 srp (and stamp (match-end 3))
6491 s1 (match-string (if plain 1 2) ts)
6492 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6494 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6495 ;; them, we might want to remove them there to avoid duplication.
6496 ;; The user can turn this off with a variable.
6497 (if (and org-prefix-has-time
6498 org-agenda-remove-times-when-in-prefix (or stamp plain)
6499 (string-match (concat (regexp-quote s0) " *") txt)
6500 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6501 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6502 (= (match-beginning 0) 0)
6504 (setq txt (replace-match "" nil nil txt))))
6505 ;; Normalize the time(s) to 24 hour
6506 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6507 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6509 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6510 (let (org-time-clocksum-use-effort-durations)
6511 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6512 (setq s2
6513 (org-minutes-to-clocksum-string
6514 (+ (org-hh:mm-string-to-minutes s1)
6515 org-agenda-default-appointment-duration)))))
6517 ;; Compute the duration
6518 (when s2
6519 (setq duration (- (org-hh:mm-string-to-minutes s2)
6520 (org-hh:mm-string-to-minutes s1)))))
6522 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6523 txt)
6524 ;; Tags are in the string
6525 (if (or (eq org-agenda-remove-tags t)
6526 (and org-agenda-remove-tags
6527 org-prefix-has-tag))
6528 (setq txt (replace-match "" t t txt))
6529 (setq txt (replace-match
6530 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6531 (match-string 2 txt))
6532 t t txt))))
6533 (when (derived-mode-p 'org-mode)
6534 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6536 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6537 ;; current buffer, so move this check outside of above
6538 (if effort
6539 (setq neffort (org-duration-string-to-minutes effort)
6540 effort (setq effort (concat "[" effort "]")))
6541 ;; prevent erroring out with %e format when there is no effort
6542 (setq effort ""))
6544 (when remove-re
6545 (while (string-match remove-re txt)
6546 (setq txt (replace-match "" t t txt))))
6548 ;; Set org-heading property on `txt' to mark the start of the
6549 ;; heading.
6550 (add-text-properties 0 (length txt) '(org-heading t) txt)
6552 ;; Prepare the variables needed in the eval of the compiled format
6553 (if org-prefix-has-breadcrumbs
6554 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6555 (let ((s (org-display-outline-path nil nil "->" t)))
6556 (if (eq "" s) "" (concat s "->"))))))
6557 (setq time (cond (s2 (concat
6558 (org-agenda-time-of-day-to-ampm-maybe s1)
6559 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6560 (if org-agenda-timegrid-use-ampm " ")))
6561 (s1 (concat
6562 (org-agenda-time-of-day-to-ampm-maybe s1)
6563 (if org-agenda-timegrid-use-ampm
6564 "........ "
6565 "......")))
6566 (t ""))
6567 extra (or (and (not habitp) extra) "")
6568 category (if (symbolp category) (symbol-name category) category)
6569 thecategory (copy-sequence category)
6570 level (or level ""))
6571 (if (string-match org-bracket-link-regexp category)
6572 (progn
6573 (setq l (if (match-end 3)
6574 (- (match-end 3) (match-beginning 3))
6575 (- (match-end 1) (match-beginning 1))))
6576 (when (< l (or org-prefix-category-length 0))
6577 (setq category (copy-sequence category))
6578 (org-add-props category nil
6579 'extra-space (make-string
6580 (- org-prefix-category-length l 1) ?\ ))))
6581 (if (and org-prefix-category-max-length
6582 (>= (length category) org-prefix-category-max-length))
6583 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6584 ;; Evaluate the compiled format
6585 (setq rtn (concat (eval formatter) txt))
6587 ;; And finally add the text properties
6588 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6589 (org-add-props rtn nil
6590 'org-category (if thecategory (downcase thecategory) category)
6591 'tags (mapcar 'org-downcase-keep-props tags)
6592 'org-highest-priority org-highest-priority
6593 'org-lowest-priority org-lowest-priority
6594 'time-of-day time-of-day
6595 'duration duration
6596 'effort effort
6597 'effort-minutes neffort
6598 'breadcrumbs breadcrumbs
6599 'txt txt
6600 'level level
6601 'time time
6602 'extra extra
6603 'format org-prefix-format-compiled
6604 'dotime dotime)))))
6606 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6607 "Remove tags string from TXT, and add a modified list of tags.
6608 The modified list may contain inherited tags, and tags matched by
6609 `org-agenda-hide-tags-regexp' will be removed."
6610 (when (or add-inherited hide-re)
6611 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6612 (setq txt (substring txt 0 (match-beginning 0))))
6613 (setq tags
6614 (delq nil
6615 (mapcar (lambda (tg)
6616 (if (or (and hide-re (string-match hide-re tg))
6617 (and (not add-inherited)
6618 (get-text-property 0 'inherited tg)))
6620 tg))
6621 tags)))
6622 (when tags
6623 (let ((have-i (get-text-property 0 'inherited (car tags)))
6625 (setq txt (concat txt " :"
6626 (mapconcat
6627 (lambda (x)
6628 (setq i (get-text-property 0 'inherited x))
6629 (if (and have-i (not i))
6630 (progn
6631 (setq have-i nil)
6632 (concat ":" x))
6634 tags ":")
6635 (if have-i "::" ":"))))))
6636 txt)
6638 (defun org-downcase-keep-props (s)
6639 (let ((props (text-properties-at 0 s)))
6640 (setq s (downcase s))
6641 (add-text-properties 0 (length s) props s)
6644 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6646 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6647 "Add a time-grid for agenda items which need it.
6649 LIST is the list of agenda items formatted by `org-agenda-list'.
6650 NDAYS is the span of the current agenda view.
6651 TODAYP is `t' when the current agenda view is on today."
6652 (catch 'exit
6653 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6654 ((and todayp (member 'today (car org-agenda-time-grid))))
6655 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6656 ((member 'weekly (car org-agenda-time-grid)))
6657 (t (throw 'exit list)))
6658 (let* ((have (delq nil (mapcar
6659 (lambda (x) (get-text-property 1 'time-of-day x))
6660 list)))
6661 (string (nth 1 org-agenda-time-grid))
6662 (gridtimes (nth 2 org-agenda-time-grid))
6663 (req (car org-agenda-time-grid))
6664 (remove (member 'remove-match req))
6665 new time)
6666 (if (and (member 'require-timed req) (not have))
6667 ;; don't show empty grid
6668 (throw 'exit list))
6669 (while (setq time (pop gridtimes))
6670 (unless (and remove (member time have))
6671 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6672 (push (org-agenda-format-item
6673 nil string nil "" nil
6674 (concat (substring time 0 -2) ":" (substring time -2)))
6675 new)
6676 (put-text-property
6677 2 (length (car new)) 'face 'org-time-grid (car new))))
6678 (when (and todayp org-agenda-show-current-time-in-grid)
6679 (push (org-agenda-format-item
6680 nil org-agenda-current-time-string nil "" nil
6681 (format-time-string "%H:%M "))
6682 new)
6683 (put-text-property
6684 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6686 (if (member 'time-up org-agenda-sorting-strategy-selected)
6687 (append new list)
6688 (append list new)))))
6690 (defun org-compile-prefix-format (key)
6691 "Compile the prefix format into a Lisp form that can be evaluated.
6692 The resulting form and associated variable bindings is returned
6693 and stored in the variable `org-prefix-format-compiled'."
6694 (setq org-prefix-has-time nil
6695 org-prefix-has-tag nil
6696 org-prefix-category-length nil
6697 org-prefix-has-effort nil
6698 org-prefix-has-breadcrumbs nil)
6699 (let ((s (cond
6700 ((stringp org-agenda-prefix-format)
6701 org-agenda-prefix-format)
6702 ((assq key org-agenda-prefix-format)
6703 (cdr (assq key org-agenda-prefix-format)))
6704 (t " %-12:c%?-12t% s")))
6705 (start 0)
6706 varform vars var e c f opt)
6707 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6708 s start)
6709 (setq var (or (cdr (assoc (match-string 4 s)
6710 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6711 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6712 'eval)
6713 c (or (match-string 3 s) "")
6714 opt (match-beginning 1)
6715 start (1+ (match-beginning 0)))
6716 (if (equal var 'time) (setq org-prefix-has-time t))
6717 (if (equal var 'tag) (setq org-prefix-has-tag t))
6718 (if (equal var 'effort) (setq org-prefix-has-effort t))
6719 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6720 (setq f (concat "%" (match-string 2 s) "s"))
6721 (when (equal var 'category)
6722 (setq org-prefix-category-length
6723 (floor (abs (string-to-number (match-string 2 s)))))
6724 (setq org-prefix-category-max-length
6725 (let ((x (match-string 2 s)))
6726 (save-match-data
6727 (if (string-match "\\.[0-9]+" x)
6728 (string-to-number (substring (match-string 0 x) 1)))))))
6729 (if (eq var 'eval)
6730 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6731 (if opt
6732 (setq varform
6733 `(if (equal "" ,var)
6735 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6736 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6737 (setq s (replace-match "%s" t nil s))
6738 (push varform vars))
6739 (setq vars (nreverse vars))
6740 (with-current-buffer (or org-agenda-buffer (current-buffer))
6741 (setq org-prefix-format-compiled
6742 (list
6743 `((org-prefix-has-time ,org-prefix-has-time)
6744 (org-prefix-has-tag ,org-prefix-has-tag)
6745 (org-prefix-category-length ,org-prefix-category-length)
6746 (org-prefix-has-effort ,org-prefix-has-effort)
6747 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6748 `(format ,s ,@vars))))))
6750 (defun org-set-sorting-strategy (key)
6751 (if (symbolp (car org-agenda-sorting-strategy))
6752 ;; the old format
6753 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6754 (setq org-agenda-sorting-strategy-selected
6755 (or (cdr (assq key org-agenda-sorting-strategy))
6756 (cdr (assq 'agenda org-agenda-sorting-strategy))
6757 '(time-up category-keep priority-down)))))
6759 (defun org-get-time-of-day (s &optional string mod24)
6760 "Check string S for a time of day.
6761 If found, return it as a military time number between 0 and 2400.
6762 If not found, return nil.
6763 The optional STRING argument forces conversion into a 5 character wide string
6764 HH:MM."
6765 (save-match-data
6766 (when
6767 (and
6768 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6769 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6770 (not (eq (get-text-property 1 'face s) 'org-link)))
6771 (let* ((h (string-to-number (match-string 1 s)))
6772 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6773 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6774 (am-p (equal ampm "am"))
6775 (h1 (cond ((not ampm) h)
6776 ((= h 12) (if am-p 0 12))
6777 (t (+ h (if am-p 0 12)))))
6778 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6779 (mod h1 24) h1))
6780 (t0 (+ (* 100 h2) m))
6781 (t1 (concat (if (>= h1 24) "+" " ")
6782 (if (and org-agenda-time-leading-zero
6783 (< t0 1000)) "0" "")
6784 (if (< t0 100) "0" "")
6785 (if (< t0 10) "0" "")
6786 (int-to-string t0))))
6787 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6789 (defvar org-agenda-before-sorting-filter-function nil
6790 "Function to be applied to agenda items prior to sorting.
6791 Prior to sorting also means just before they are inserted into the agenda.
6793 To aid sorting, you may revisit the original entries and add more text
6794 properties which will later be used by the sorting functions.
6796 The function should take a string argument, an agenda line.
6797 It has access to the text properties in that line, which contain among
6798 other things, the property `org-hd-marker' that points to the entry
6799 where the line comes from. Note that not all lines going into the agenda
6800 have this property, only most.
6802 The function should return the modified string. It is probably best
6803 to ONLY change text properties.
6805 You can also use this function as a filter, by returning nil for lines
6806 you don't want to have in the agenda at all. For this application, you
6807 could bind the variable in the options section of a custom command.")
6809 (defun org-agenda-finalize-entries (list &optional type)
6810 "Sort, limit and concatenate the LIST of agenda items.
6811 The optional argument TYPE tells the agenda type."
6812 (let ((max-effort (cond ((listp org-agenda-max-effort)
6813 (cdr (assoc type org-agenda-max-effort)))
6814 (t org-agenda-max-effort)))
6815 (max-todo (cond ((listp org-agenda-max-todos)
6816 (cdr (assoc type org-agenda-max-todos)))
6817 (t org-agenda-max-todos)))
6818 (max-tags (cond ((listp org-agenda-max-tags)
6819 (cdr (assoc type org-agenda-max-tags)))
6820 (t org-agenda-max-tags)))
6821 (max-entries (cond ((listp org-agenda-max-entries)
6822 (cdr (assoc type org-agenda-max-entries)))
6823 (t org-agenda-max-entries))) l)
6824 (when org-agenda-before-sorting-filter-function
6825 (setq list
6826 (delq nil
6827 (mapcar
6828 org-agenda-before-sorting-filter-function list))))
6829 (setq list (mapcar 'org-agenda-highlight-todo list)
6830 list (mapcar 'identity (sort list 'org-entries-lessp)))
6831 (when max-effort
6832 (setq list (org-agenda-limit-entries
6833 list 'effort-minutes max-effort 'identity)))
6834 (when max-todo
6835 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6836 (when max-tags
6837 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6838 (when max-entries
6839 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6840 (mapconcat 'identity list "\n")))
6842 (defun org-agenda-limit-entries (list prop limit &optional fn)
6843 "Limit the number of agenda entries."
6844 (let ((include (and limit (< limit 0))))
6845 (if limit
6846 (let ((fun (or fn (lambda (p) (if p 1))))
6847 (lim 0))
6848 (delq nil
6849 (mapcar
6850 (lambda (e)
6851 (let ((pval (funcall fun (get-text-property 1 prop e))))
6852 (if pval (setq lim (+ lim pval)))
6853 (cond ((and pval (<= lim (abs limit))) e)
6854 ((and include (not pval)) e))))
6855 list)))
6856 list)))
6858 (defun org-agenda-limit-interactively ()
6859 "In agenda, interactively limit entries to various maximums."
6860 (interactive)
6861 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6862 (num (string-to-number (read-from-minibuffer "How many? "))))
6863 (cond ((equal max ?e)
6864 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6865 ((equal max ?t)
6866 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6867 ((equal max ?T)
6868 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6869 ((equal max ?E)
6870 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6871 (org-agenda-fit-window-to-buffer))
6873 (defun org-agenda-highlight-todo (x)
6874 (let ((org-done-keywords org-done-keywords-for-agenda)
6875 (case-fold-search nil)
6877 (if (eq x 'line)
6878 (save-excursion
6879 (beginning-of-line 1)
6880 (setq re (org-get-at-bol 'org-todo-regexp))
6881 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6882 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6883 (add-text-properties (match-beginning 0) (match-end 1)
6884 (list 'face (org-get-todo-face 1)))
6885 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6886 (delete-region (match-beginning 1) (1- (match-end 0)))
6887 (goto-char (match-beginning 1))
6888 (insert (format org-agenda-todo-keyword-format s)))))
6889 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6890 (setq re (get-text-property 0 'org-todo-regexp x))
6891 (when (and re
6892 ;; Test `pl' because if there's no heading content,
6893 ;; there's no point matching to highlight. Note
6894 ;; that if we didn't test `pl' first, and there
6895 ;; happened to be no keyword from `org-todo-regexp'
6896 ;; on this heading line, then the `equal' comparison
6897 ;; afterwards would spuriously succeed in the case
6898 ;; where `pl' is nil -- causing an args-out-of-range
6899 ;; error when we try to add text properties to text
6900 ;; that isn't there.
6902 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6903 x pl) pl))
6904 (add-text-properties
6905 (or (match-end 1) (match-end 0)) (match-end 0)
6906 (list 'face (org-get-todo-face (match-string 2 x)))
6908 (when (match-end 1)
6909 (setq x (concat (substring x 0 (match-end 1))
6910 (format org-agenda-todo-keyword-format
6911 (match-string 2 x))
6912 (org-add-props " " (text-properties-at 0 x))
6913 (substring x (match-end 3)))))))
6914 x)))
6916 (defsubst org-cmp-priority (a b)
6917 "Compare the priorities of string A and B."
6918 (let ((pa (or (get-text-property 1 'priority a) 0))
6919 (pb (or (get-text-property 1 'priority b) 0)))
6920 (cond ((> pa pb) +1)
6921 ((< pa pb) -1))))
6923 (defsubst org-cmp-effort (a b)
6924 "Compare the effort values of string A and B."
6925 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6926 (ea (or (get-text-property 1 'effort-minutes a) def))
6927 (eb (or (get-text-property 1 'effort-minutes b) def)))
6928 (cond ((> ea eb) +1)
6929 ((< ea eb) -1))))
6931 (defsubst org-cmp-category (a b)
6932 "Compare the string values of categories of strings A and B."
6933 (let ((ca (or (get-text-property 1 'org-category a) ""))
6934 (cb (or (get-text-property 1 'org-category b) "")))
6935 (cond ((string-lessp ca cb) -1)
6936 ((string-lessp cb ca) +1))))
6938 (defsubst org-cmp-todo-state (a b)
6939 "Compare the todo states of strings A and B."
6940 (let* ((ma (or (get-text-property 1 'org-marker a)
6941 (get-text-property 1 'org-hd-marker a)))
6942 (mb (or (get-text-property 1 'org-marker b)
6943 (get-text-property 1 'org-hd-marker b)))
6944 (fa (and ma (marker-buffer ma)))
6945 (fb (and mb (marker-buffer mb)))
6946 (todo-kwds
6947 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6948 (and fb (with-current-buffer fb org-todo-keywords-1))))
6949 (ta (or (get-text-property 1 'todo-state a) ""))
6950 (tb (or (get-text-property 1 'todo-state b) ""))
6951 (la (- (length (member ta todo-kwds))))
6952 (lb (- (length (member tb todo-kwds))))
6953 (donepa (member ta org-done-keywords-for-agenda))
6954 (donepb (member tb org-done-keywords-for-agenda)))
6955 (cond ((and donepa (not donepb)) -1)
6956 ((and (not donepa) donepb) +1)
6957 ((< la lb) -1)
6958 ((< lb la) +1))))
6960 (defsubst org-cmp-alpha (a b)
6961 "Compare the headlines, alphabetically."
6962 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6963 (plb (text-property-any 0 (length b) 'org-heading t b))
6964 (ta (and pla (substring a pla)))
6965 (tb (and plb (substring b plb))))
6966 (when pla
6967 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6968 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6969 (setq ta (substring ta (match-end 0))))
6970 (setq ta (downcase ta)))
6971 (when plb
6972 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6973 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6974 (setq tb (substring tb (match-end 0))))
6975 (setq tb (downcase tb)))
6976 (cond ((not ta) +1)
6977 ((not tb) -1)
6978 ((string-lessp ta tb) -1)
6979 ((string-lessp tb ta) +1))))
6981 (defsubst org-cmp-tag (a b)
6982 "Compare the string values of the first tags of A and B."
6983 (let ((ta (car (last (get-text-property 1 'tags a))))
6984 (tb (car (last (get-text-property 1 'tags b)))))
6985 (cond ((not ta) +1)
6986 ((not tb) -1)
6987 ((string-lessp ta tb) -1)
6988 ((string-lessp tb ta) +1))))
6990 (defsubst org-cmp-time (a b)
6991 "Compare the time-of-day values of strings A and B."
6992 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6993 (ta (or (get-text-property 1 'time-of-day a) def))
6994 (tb (or (get-text-property 1 'time-of-day b) def)))
6995 (cond ((< ta tb) -1)
6996 ((< tb ta) +1))))
6998 (defsubst org-cmp-ts (a b type)
6999 "Compare the timestamps values of entries A and B.
7000 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7001 \"timestamp_ia\", compare within each of these type. When TYPE
7002 is the empty string, compare all timestamps without respect of
7003 their type."
7004 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7005 (ta (or (and (string-match type (or (get-text-property 1 type a) ""))
7006 (get-text-property 1 'ts-date a)) def))
7007 (tb (or (and (string-match type (or (get-text-property 1 type b) ""))
7008 (get-text-property 1 'ts-date b)) def)))
7009 (cond ((< ta tb) -1)
7010 ((< tb ta) +1))))
7012 (defsubst org-cmp-habit-p (a b)
7013 "Compare the todo states of strings A and B."
7014 (let ((ha (get-text-property 1 'org-habit-p a))
7015 (hb (get-text-property 1 'org-habit-p b)))
7016 (cond ((and ha (not hb)) -1)
7017 ((and (not ha) hb) +1))))
7019 (defun org-entries-lessp (a b)
7020 "Predicate for sorting agenda entries."
7021 ;; The following variables will be used when the form is evaluated.
7022 ;; So even though the compiler complains, keep them.
7023 (let* ((ss org-agenda-sorting-strategy-selected)
7024 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7025 (org-cmp-ts a b "")))
7026 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7027 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7028 (org-cmp-ts a b "scheduled")))
7029 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7030 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7031 (org-cmp-ts a b "deadline")))
7032 (deadline-down (if deadline-up (- deadline-up) nil))
7033 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7034 (org-cmp-ts a b "iatimestamp_ia")))
7035 (tsia-down (if tsia-up (- tsia-up) nil))
7036 (ts-up (and (org-em 'ts-up 'ts-down ss)
7037 (org-cmp-ts a b "timestamp")))
7038 (ts-down (if ts-up (- ts-up) nil))
7039 (time-up (and (org-em 'time-up 'time-down ss)
7040 (org-cmp-time a b)))
7041 (time-down (if time-up (- time-up) nil))
7042 (priority-up (and (org-em 'priority-up 'priority-down ss)
7043 (org-cmp-priority a b)))
7044 (priority-down (if priority-up (- priority-up) nil))
7045 (effort-up (and (org-em 'effort-up 'effort-down ss)
7046 (org-cmp-effort a b)))
7047 (effort-down (if effort-up (- effort-up) nil))
7048 (category-up (and (or (org-em 'category-up 'category-down ss)
7049 (memq 'category-keep ss))
7050 (org-cmp-category a b)))
7051 (category-down (if category-up (- category-up) nil))
7052 (category-keep (if category-up +1 nil))
7053 (tag-up (and (org-em 'tag-up 'tag-down ss)
7054 (org-cmp-tag a b)))
7055 (tag-down (if tag-up (- tag-up) nil))
7056 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7057 (org-cmp-todo-state a b)))
7058 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7059 (habit-up (and (org-em 'habit-up 'habit-down ss)
7060 (org-cmp-habit-p a b)))
7061 (habit-down (if habit-up (- habit-up) nil))
7062 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7063 (org-cmp-alpha a b)))
7064 (alpha-down (if alpha-up (- alpha-up) nil))
7065 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7066 user-defined-up user-defined-down)
7067 (if (and need-user-cmp org-agenda-cmp-user-defined
7068 (functionp org-agenda-cmp-user-defined))
7069 (setq user-defined-up
7070 (funcall org-agenda-cmp-user-defined a b)
7071 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7072 (cdr (assoc
7073 (eval (cons 'or org-agenda-sorting-strategy-selected))
7074 '((-1 . t) (1 . nil) (nil . nil))))))
7076 ;;; Agenda restriction lock
7078 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7079 "Overlay to mark the headline to which agenda commands are restricted.")
7080 (overlay-put org-agenda-restriction-lock-overlay
7081 'face 'org-agenda-restriction-lock)
7082 (overlay-put org-agenda-restriction-lock-overlay
7083 'help-echo "Agendas are currently limited to this subtree.")
7084 (org-detach-overlay org-agenda-restriction-lock-overlay)
7086 ;;;###autoload
7087 (defun org-agenda-set-restriction-lock (&optional type)
7088 "Set restriction lock for agenda, to current subtree or file.
7089 Restriction will be the file if TYPE is `file', or if type is the
7090 universal prefix '(4), or if the cursor is before the first headline
7091 in the file. Otherwise, restriction will be to the current subtree."
7092 (interactive "P")
7093 (and (equal type '(4)) (setq type 'file))
7094 (setq type (cond
7095 (type type)
7096 ((org-at-heading-p) 'subtree)
7097 ((condition-case nil (org-back-to-heading t) (error nil))
7098 'subtree)
7099 (t 'file)))
7100 (if (eq type 'subtree)
7101 (progn
7102 (setq org-agenda-restrict (current-buffer))
7103 (setq org-agenda-overriding-restriction 'subtree)
7104 (put 'org-agenda-files 'org-restrict
7105 (list (buffer-file-name (buffer-base-buffer))))
7106 (org-back-to-heading t)
7107 (move-overlay org-agenda-restriction-lock-overlay
7108 (point)
7109 (if org-agenda-restriction-lock-highlight-subtree
7110 (save-excursion (org-end-of-subtree t t) (point))
7111 (point-at-eol)))
7112 (move-marker org-agenda-restrict-begin (point))
7113 (move-marker org-agenda-restrict-end
7114 (save-excursion (org-end-of-subtree t t)))
7115 (message "Locking agenda restriction to subtree"))
7116 (put 'org-agenda-files 'org-restrict
7117 (list (buffer-file-name (buffer-base-buffer))))
7118 (setq org-agenda-restrict nil)
7119 (setq org-agenda-overriding-restriction 'file)
7120 (move-marker org-agenda-restrict-begin nil)
7121 (move-marker org-agenda-restrict-end nil)
7122 (message "Locking agenda restriction to file"))
7123 (setq current-prefix-arg nil)
7124 (org-agenda-maybe-redo))
7126 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7127 "Remove the agenda restriction lock."
7128 (interactive "P")
7129 (org-detach-overlay org-agenda-restriction-lock-overlay)
7130 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7131 (setq org-agenda-overriding-restriction nil)
7132 (setq org-agenda-restrict nil)
7133 (put 'org-agenda-files 'org-restrict nil)
7134 (move-marker org-agenda-restrict-begin nil)
7135 (move-marker org-agenda-restrict-end nil)
7136 (setq current-prefix-arg nil)
7137 (message "Agenda restriction lock removed")
7138 (or noupdate (org-agenda-maybe-redo)))
7140 (defun org-agenda-maybe-redo ()
7141 "If there is any window showing the agenda view, update it."
7142 (let ((w (get-buffer-window org-agenda-buffer-name t))
7143 (w0 (selected-window)))
7144 (when w
7145 (select-window w)
7146 (org-agenda-redo)
7147 (select-window w0)
7148 (if org-agenda-overriding-restriction
7149 (message "Agenda view shifted to new %s restriction"
7150 org-agenda-overriding-restriction)
7151 (message "Agenda restriction lock removed")))))
7153 ;;; Agenda commands
7155 (defun org-agenda-check-type (error &rest types)
7156 "Check if agenda buffer is of allowed type.
7157 If ERROR is non-nil, throw an error, otherwise just return nil.
7158 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7159 (if (not org-agenda-type)
7160 (error "No Org agenda currently displayed")
7161 (if (memq org-agenda-type types)
7163 (if error
7164 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7165 nil))))
7167 (defun org-agenda-Quit ()
7168 "Exit the agenda and kill buffers loaded by `org-agenda'.
7169 Also restore the window configuration."
7170 (interactive)
7171 (if org-agenda-columns-active
7172 (org-columns-quit)
7173 (let ((buf (current-buffer)))
7174 (if (eq org-agenda-window-setup 'other-frame)
7175 (progn
7176 (org-agenda-reset-markers)
7177 (kill-buffer buf)
7178 (org-columns-remove-overlays)
7179 (setq org-agenda-archives-mode nil)
7180 (delete-frame))
7181 (and (not (eq org-agenda-window-setup 'current-window))
7182 (not (one-window-p))
7183 (delete-window))
7184 (org-agenda-reset-markers)
7185 (kill-buffer buf)
7186 (org-columns-remove-overlays)
7187 (setq org-agenda-archives-mode nil)))
7188 (setq org-agenda-buffer nil)
7189 ;; Maybe restore the pre-agenda window configuration.
7190 (and org-agenda-restore-windows-after-quit
7191 (not (eq org-agenda-window-setup 'other-frame))
7192 org-agenda-pre-window-conf
7193 (set-window-configuration org-agenda-pre-window-conf)
7194 (setq org-agenda-pre-window-conf nil))))
7196 (defun org-agenda-quit ()
7197 "Exit the agenda and restore the window configuration.
7198 When `org-agenda-sticky' is non-nil, only bury the agenda."
7199 (interactive)
7200 (if (and (eq org-indirect-buffer-display 'other-window)
7201 org-last-indirect-buffer)
7202 (let ((org-last-indirect-window
7203 (get-buffer-window org-last-indirect-buffer)))
7204 (if org-last-indirect-window
7205 (delete-window org-last-indirect-window))))
7206 (if org-agenda-columns-active
7207 (org-columns-quit)
7208 (if org-agenda-sticky
7209 (let ((buf (current-buffer)))
7210 (if (eq org-agenda-window-setup 'other-frame)
7211 (progn
7212 (delete-frame))
7213 (and (not (eq org-agenda-window-setup 'current-window))
7214 (not (one-window-p))
7215 (delete-window)))
7216 (with-current-buffer buf
7217 (bury-buffer)
7218 ;; Maybe restore the pre-agenda window configuration.
7219 (and org-agenda-restore-windows-after-quit
7220 (not (eq org-agenda-window-setup 'other-frame))
7221 org-agenda-pre-window-conf
7222 (set-window-configuration org-agenda-pre-window-conf)
7223 (setq org-agenda-pre-window-conf nil))))
7224 (org-agenda-Quit))))
7226 (defun org-agenda-exit ()
7227 "Exit the agenda and restore the window configuration.
7228 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7229 buffers visited directly by the user will not be touched."
7230 (interactive)
7231 (org-release-buffers org-agenda-new-buffers)
7232 (setq org-agenda-new-buffers nil)
7233 (org-agenda-Quit))
7235 (defun org-agenda-kill-all-agenda-buffers ()
7236 "Kill all buffers in `org-agenda-mode'.
7237 This is used when toggling sticky agendas.
7238 You can also explicitly invoke it with `C-c a C-k'."
7239 (interactive)
7240 (let (blist)
7241 (dolist (buf (buffer-list))
7242 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7243 (push buf blist)))
7244 (mapc 'kill-buffer blist)))
7246 (defun org-agenda-execute (arg)
7247 "Execute another agenda command, keeping same window.
7248 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7249 in the agenda."
7250 (interactive "P")
7251 (let ((org-agenda-window-setup 'current-window))
7252 (org-agenda arg)))
7254 (defun org-agenda-redo (&optional all)
7255 "Rebuild possibly ALL agenda view(s) in the current buffer."
7256 (interactive "P")
7257 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7258 (cpa (unless (eq all t) current-prefix-arg))
7259 (org-agenda-doing-sticky-redo org-agenda-sticky)
7260 (org-agenda-sticky nil)
7261 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7262 org-agenda-buffer-name))
7263 (org-agenda-keep-modes t)
7264 (tag-filter org-agenda-tag-filter)
7265 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7266 (top-hl-filter org-agenda-top-headline-filter)
7267 (cat-filter org-agenda-category-filter)
7268 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7269 (re-filter org-agenda-regexp-filter)
7270 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7271 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7272 (cols org-agenda-columns-active)
7273 (line (org-current-line))
7274 (window-line (- line (org-current-line (window-start))))
7275 (lprops (get 'org-agenda-redo-command 'org-lprops))
7276 (redo-cmd (get-text-property p 'org-redo-cmd))
7277 (last-args (get-text-property p 'org-last-args))
7278 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7279 (org-agenda-overriding-cmd-arguments
7280 (unless (eq all t)
7281 (cond ((listp last-args)
7282 (cons (or cpa (car last-args)) (cdr last-args)))
7283 ((stringp last-args)
7284 last-args))))
7285 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7286 (put 'org-agenda-tag-filter :preset-filter nil)
7287 (put 'org-agenda-category-filter :preset-filter nil)
7288 (put 'org-agenda-regexp-filter :preset-filter nil)
7289 (and cols (org-columns-quit))
7290 (message "Rebuilding agenda buffer...")
7291 (if series-redo-cmd
7292 (eval series-redo-cmd)
7293 (org-let lprops redo-cmd))
7294 (setq org-agenda-undo-list nil
7295 org-agenda-pending-undo-list nil
7296 org-agenda-tag-filter tag-filter
7297 org-agenda-category-filter cat-filter
7298 org-agenda-regexp-filter re-filter
7299 org-agenda-top-headline-filter top-hl-filter)
7300 (message "Rebuilding agenda buffer...done")
7301 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7302 (put 'org-agenda-category-filter :preset-filter cat-preset)
7303 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7304 (let ((tag (or tag-filter tag-preset))
7305 (cat (or cat-filter cat-preset))
7306 (re (or re-filter re-preset)))
7307 (when tag (org-agenda-filter-apply tag 'tag))
7308 (when cat (org-agenda-filter-apply cat 'category))
7309 (when re (org-agenda-filter-apply re 'regexp)))
7310 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7311 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7312 (org-goto-line line)
7313 (recenter window-line)))
7315 (defvar org-global-tags-completion-table nil)
7316 (defvar org-agenda-filter-form nil)
7317 (defvar org-agenda-filtered-by-category nil)
7319 (defun org-agenda-filter-by-category (strip)
7320 "Keep only those lines in the agenda buffer that have a specific category.
7321 The category is that of the current line."
7322 (interactive "P")
7323 (if (and org-agenda-filtered-by-category
7324 org-agenda-category-filter)
7325 (org-agenda-filter-show-all-cat)
7326 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7327 (cond
7328 ((and cat strip)
7329 (org-agenda-filter-apply
7330 (push (concat "-" cat) org-agenda-category-filter) 'category))
7331 ((and cat)
7332 (org-agenda-filter-apply
7333 (setq org-agenda-category-filter
7334 (list (concat "+" cat))) 'category))
7335 (t (error "No category at point"))))))
7337 (defun org-find-top-headline (&optional pos)
7338 "Find the topmost parent headline and return it."
7339 (save-excursion
7340 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7341 (if pos (goto-char pos))
7342 ;; Skip up to the topmost parent
7343 (while (ignore-errors (outline-up-heading 1) t))
7344 (ignore-errors
7345 (nth 4 (org-heading-components))))))
7347 (defvar org-agenda-filtered-by-top-headline nil)
7348 (defun org-agenda-filter-by-top-headline (strip)
7349 "Keep only those lines that are descendants from the same top headline.
7350 The top headline is that of the current line."
7351 (interactive "P")
7352 (if org-agenda-filtered-by-top-headline
7353 (progn
7354 (setq org-agenda-filtered-by-top-headline nil
7355 org-agenda-top-headline-filter nil)
7356 (org-agenda-filter-show-all-top-filter))
7357 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7358 (if toph (org-agenda-filter-top-headline-apply toph strip)
7359 (error "No top-level headline at point")))))
7361 (defvar org-agenda-regexp-filter nil)
7362 (defun org-agenda-filter-by-regexp (strip)
7363 "Filter agenda entries by a regular expression.
7364 Regexp filters are cumulative.
7365 With no prefix argument, keep entries matching the regexp.
7366 With one prefix argument, filter out entries matching the regexp.
7367 With two prefix arguments, remove the regexp filters."
7368 (interactive "P")
7369 (if (not (equal strip '(16)))
7370 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7371 (read-from-minibuffer
7372 (if (equal strip '(4))
7373 "Filter out entries matching regexp: "
7374 "Narrow to entries matching regexp: ")))))
7375 (push flt org-agenda-regexp-filter)
7376 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7377 (org-agenda-filter-show-all-re)
7378 (message "Regexp filter removed")))
7380 (defun org-agenda-filter-remove-all ()
7381 "Remove all filters from the current agenda buffer."
7382 (interactive)
7383 (when org-agenda-tag-filter
7384 (org-agenda-filter-show-all-tag))
7385 (when org-agenda-category-filter
7386 (org-agenda-filter-show-all-cat))
7387 (when org-agenda-regexp-filter
7388 (org-agenda-filter-show-all-re))
7389 (when org-agenda-top-headline-filter
7390 (org-agenda-filter-show-all-top-filter))
7391 (org-agenda-finalize))
7393 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7394 "Keep only those lines in the agenda buffer that have a specific tag.
7395 The tag is selected with its fast selection letter, as configured.
7396 With prefix argument STRIP, remove all lines that do have the tag.
7397 A lisp caller can specify CHAR. NARROW means that the new tag should be
7398 used to narrow the search - the interactive user can also press `-' or `+'
7399 to switch to narrowing."
7400 (interactive "P")
7401 (let* ((alist org-tag-alist-for-agenda)
7402 (tag-chars (mapconcat
7403 (lambda (x) (if (and (not (symbolp (car x)))
7404 (cdr x))
7405 (char-to-string (cdr x))
7406 ""))
7407 alist ""))
7408 (efforts (org-split-string
7409 (or (cdr (assoc (concat org-effort-property "_ALL")
7410 org-global-properties))
7411 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7412 "")))
7413 (effort-op org-agenda-filter-effort-default-operator)
7414 (effort-prompt "")
7415 (inhibit-read-only t)
7416 (current org-agenda-tag-filter)
7417 maybe-refresh a n tag)
7418 (unless char
7419 (message
7420 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7421 (if narrow "Narrow" "Filter") tag-chars
7422 (if org-agenda-auto-exclude-function "[RET], " ""))
7423 (setq char (read-char-exclusive)))
7424 (when (member char '(?+ ?-))
7425 ;; Narrowing down
7426 (cond ((equal char ?-) (setq strip t narrow t))
7427 ((equal char ?+) (setq strip nil narrow t)))
7428 (message
7429 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7430 (setq char (read-char-exclusive)))
7431 (when (member char '(?< ?> ?= ??))
7432 ;; An effort operator
7433 (setq effort-op (char-to-string char))
7434 (setq alist nil) ; to make sure it will be interpreted as effort.
7435 (unless (equal char ??)
7436 (loop for i from 0 to 9 do
7437 (setq effort-prompt
7438 (concat
7439 effort-prompt " ["
7440 (if (= i 9) "0" (int-to-string (1+ i)))
7441 "]" (nth i efforts))))
7442 (message "Effort%s: %s " effort-op effort-prompt)
7443 (setq char (read-char-exclusive))
7444 (when (or (< char ?0) (> char ?9))
7445 (error "Need 1-9,0 to select effort"))))
7446 (when (equal char ?\t)
7447 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7448 (org-set-local 'org-global-tags-completion-table
7449 (org-global-tags-completion-table)))
7450 (let ((completion-ignore-case t))
7451 (setq tag (org-icompleting-read
7452 "Tag: " org-global-tags-completion-table))))
7453 (cond
7454 ((equal char ?\r)
7455 (org-agenda-filter-show-all-tag)
7456 (when org-agenda-auto-exclude-function
7457 (setq org-agenda-tag-filter nil)
7458 (dolist (tag (org-agenda-get-represented-tags))
7459 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7460 (if modifier
7461 (push modifier org-agenda-tag-filter))))
7462 (if (not (null org-agenda-tag-filter))
7463 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7464 (setq maybe-refresh t))
7465 ((equal char ?/)
7466 (org-agenda-filter-show-all-tag)
7467 (when (get 'org-agenda-tag-filter :preset-filter)
7468 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7469 (setq maybe-refresh t))
7470 ((equal char ?. )
7471 (setq org-agenda-tag-filter
7472 (mapcar (lambda(tag) (concat "+" tag))
7473 (org-get-at-bol 'tags)))
7474 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7475 (setq maybe-refresh t))
7476 ((or (equal char ?\ )
7477 (setq a (rassoc char alist))
7478 (and (>= char ?0) (<= char ?9)
7479 (setq n (if (= char ?0) 9 (- char ?0 1))
7480 tag (concat effort-op (nth n efforts))
7481 a (cons tag nil)))
7482 (and (= char ??)
7483 (setq tag "?eff")
7484 a (cons tag nil))
7485 (and tag (setq a (cons tag nil))))
7486 (org-agenda-filter-show-all-tag)
7487 (setq tag (car a))
7488 (setq org-agenda-tag-filter
7489 (cons (concat (if strip "-" "+") tag)
7490 (if narrow current nil)))
7491 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7492 (setq maybe-refresh t))
7493 (t (error "Invalid tag selection character %c" char)))
7494 (when (and maybe-refresh
7495 (eq org-agenda-clockreport-mode 'with-filter))
7496 (org-agenda-redo))))
7498 (defun org-agenda-get-represented-tags ()
7499 "Get a list of all tags currently represented in the agenda."
7500 (let (p tags)
7501 (save-excursion
7502 (goto-char (point-min))
7503 (while (setq p (next-single-property-change (point) 'tags))
7504 (goto-char p)
7505 (mapc (lambda (x) (add-to-list 'tags x))
7506 (get-text-property (point) 'tags))))
7507 tags))
7509 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7510 "Refine the current filter. See `org-agenda-filter-by-tag'."
7511 (interactive "P")
7512 (org-agenda-filter-by-tag strip char 'refine))
7514 (defun org-agenda-filter-make-matcher (filter type)
7515 "Create the form that tests a line for agenda filter."
7516 (let (f f1)
7517 (cond
7518 ;; Tag filter
7519 ((eq type 'tag)
7520 (setq filter
7521 (delete-dups
7522 (append (get 'org-agenda-tag-filter :preset-filter)
7523 filter)))
7524 (dolist (x filter)
7525 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7526 (ffunc
7527 (lambda (nf0 nf01 fltr notgroup op)
7528 (dolist (x fltr)
7529 (if (member x '("-" "+"))
7530 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7531 (if (string-match "[<=>?]" x)
7532 (setq nf01 (org-agenda-filter-effort-form x))
7533 (setq nf01 (list 'member (downcase (substring x 1))
7534 'tags)))
7535 (when (equal (string-to-char x) ?-)
7536 (setq nf01 (list 'not nf01))
7537 (when (not notgroup) (setq op 'and))))
7538 (push nf01 nf0))
7539 (if notgroup
7540 (push (cons 'and nf0) f)
7541 (push (cons (or op 'or) nf0) f)))))
7542 (cond ((equal filter '("+"))
7543 (setq f (list (list 'not 'tags))))
7544 ((equal nfilter filter)
7545 (funcall ffunc f1 f filter t nil))
7546 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7547 ;; Category filter
7548 ((eq type 'category)
7549 (setq filter
7550 (delete-dups
7551 (append (get 'org-agenda-category-filter :preset-filter)
7552 filter)))
7553 (dolist (x filter)
7554 (if (equal "-" (substring x 0 1))
7555 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7556 (setq f1 (list 'equal (substring x 1) 'cat)))
7557 (push f1 f)))
7558 ;; Regexp filter
7559 ((eq type 'regexp)
7560 (setq filter
7561 (delete-dups
7562 (append (get 'org-agenda-regexp-filter :preset-filter)
7563 filter)))
7564 (dolist (x filter)
7565 (if (equal "-" (substring x 0 1))
7566 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7567 (setq f1 (list 'string-match (substring x 1) 'txt)))
7568 (push f1 f))))
7569 (cons 'and (nreverse f))))
7571 (defun org-agenda-filter-effort-form (e)
7572 "Return the form to compare the effort of the current line with what E says.
7573 E looks like \"+<2:25\"."
7574 (let (op)
7575 (setq e (substring e 1))
7576 (setq op (string-to-char e) e (substring e 1))
7577 (setq op (cond ((equal op ?<) '<=)
7578 ((equal op ?>) '>=)
7579 ((equal op ??) op)
7580 (t '=)))
7581 (list 'org-agenda-compare-effort (list 'quote op)
7582 (org-duration-string-to-minutes e))))
7584 (defun org-agenda-compare-effort (op value)
7585 "Compare the effort of the current line with VALUE, using OP.
7586 If the line does not have an effort defined, return nil."
7587 (let ((eff (org-get-at-bol 'effort-minutes)))
7588 (if (equal op ??)
7589 (not eff)
7590 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7591 value))))
7593 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7594 "Expand group tags in FILTER for the agenda.
7595 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7596 (if org-group-tags
7597 (let ((case-fold-search t) rtn)
7598 (mapc
7599 (lambda (f)
7600 (let (f0 dir)
7601 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7602 (setq dir (match-string 1 f) f0 (match-string 2 f))
7603 (setq dir (if no-operator "" "+") f0 f))
7604 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7605 (org-tags-expand f0 t t))
7606 rtn))))
7607 filter)
7608 (reverse rtn))
7609 filter))
7611 (defun org-agenda-filter-apply (filter type)
7612 "Set FILTER as the new agenda filter and apply it."
7613 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7614 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7615 (let (tags cat txt)
7616 (setq org-agenda-filter-form
7617 (org-agenda-filter-make-matcher filter type))
7618 (if (and (eq type 'category)
7619 (not (equal (substring (car filter) 0 1) "-")))
7620 ;; Only set `org-agenda-filtered-by-category' to t
7621 ;; when a unique category is used as the filter
7622 (setq org-agenda-filtered-by-category t))
7623 (org-agenda-set-mode-name)
7624 (save-excursion
7625 (goto-char (point-min))
7626 (while (not (eobp))
7627 (if (org-get-at-bol 'org-marker)
7628 (progn
7629 (setq tags ; used in eval
7630 (apply 'append
7631 (mapcar (lambda (f)
7632 (org-agenda-filter-expand-tags (list f) t))
7633 (org-get-at-bol 'tags)))
7634 cat (get-text-property (point) 'org-category)
7635 txt (get-text-property (point) 'txt))
7636 (if (not (eval org-agenda-filter-form))
7637 (org-agenda-filter-hide-line type))
7638 (beginning-of-line 2))
7639 (beginning-of-line 2))))
7640 (if (get-char-property (point) 'invisible)
7641 (ignore-errors (org-agenda-previous-line)))))
7643 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7644 "Filter by top headline HL."
7645 (org-agenda-set-mode-name)
7646 (save-excursion
7647 (goto-char (point-min))
7648 (while (not (eobp))
7649 (let* ((pos (org-get-at-bol 'org-hd-marker))
7650 (tophl (and pos (org-find-top-headline pos))))
7651 (if (and tophl (funcall (if negative 'identity 'not)
7652 (string= hl tophl)))
7653 (org-agenda-filter-hide-line 'top-headline)))
7654 (beginning-of-line 2)))
7655 (if (get-char-property (point) 'invisible)
7656 (org-agenda-previous-line))
7657 (setq org-agenda-top-headline-filter hl
7658 org-agenda-filtered-by-top-headline t))
7660 (defun org-agenda-filter-hide-line (type)
7661 "Hide lines with TYPE in the agenda buffer."
7662 (let* ((b (max (point-min) (1- (point-at-bol))))
7663 (e (point-at-eol)))
7664 (let ((inhibit-read-only t))
7665 (add-text-properties
7666 b e `(invisible org-filtered org-filter-type ,type)))))
7668 (defun org-agenda-remove-filter (type)
7669 (interactive)
7670 "Remove filter of type TYPE from the agenda buffer."
7671 (save-excursion
7672 (goto-char (point-min))
7673 (let ((inhibit-read-only t) pos)
7674 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7675 (goto-char pos)
7676 (remove-text-properties
7677 (point) (next-single-property-change (point) 'org-filter-type)
7678 `(invisible org-filtered org-filter-type ,type))))
7679 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7680 (setq org-agenda-filter-form nil)
7681 (org-agenda-set-mode-name)
7682 (org-agenda-finalize)))
7684 (defun org-agenda-filter-show-all-tag nil
7685 (org-agenda-remove-filter 'tag))
7686 (defun org-agenda-filter-show-all-re nil
7687 (org-agenda-remove-filter 'regexp))
7688 (defun org-agenda-filter-show-all-cat nil
7689 (org-agenda-remove-filter 'category))
7690 (defun org-agenda-filter-show-all-top-filter nil
7691 (org-agenda-remove-filter 'top-headline))
7693 (defun org-agenda-manipulate-query-add ()
7694 "Manipulate the query by adding a search term with positive selection.
7695 Positive selection means the term must be matched for selection of an entry."
7696 (interactive)
7697 (org-agenda-manipulate-query ?\[))
7698 (defun org-agenda-manipulate-query-subtract ()
7699 "Manipulate the query by adding a search term with negative selection.
7700 Negative selection means term must not be matched for selection of an entry."
7701 (interactive)
7702 (org-agenda-manipulate-query ?\]))
7703 (defun org-agenda-manipulate-query-add-re ()
7704 "Manipulate the query by adding a search regexp with positive selection.
7705 Positive selection means the regexp must match for selection of an entry."
7706 (interactive)
7707 (org-agenda-manipulate-query ?\{))
7708 (defun org-agenda-manipulate-query-subtract-re ()
7709 "Manipulate the query by adding a search regexp with negative selection.
7710 Negative selection means regexp must not match for selection of an entry."
7711 (interactive)
7712 (org-agenda-manipulate-query ?\}))
7713 (defun org-agenda-manipulate-query (char)
7714 (cond
7715 ((memq org-agenda-type '(timeline agenda))
7716 (let ((org-agenda-include-inactive-timestamps t))
7717 (org-agenda-redo))
7718 (message "Display now includes inactive timestamps as well"))
7719 ((eq org-agenda-type 'search)
7720 (org-add-to-string
7721 'org-agenda-query-string
7722 (if org-agenda-last-search-view-search-was-boolean
7723 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7724 (?\{ . " +{}") (?\} . " -{}"))))
7725 " "))
7726 (setq org-agenda-redo-command
7727 (list 'org-search-view
7728 (car (get-text-property (min (1- (point-max)) (point))
7729 'org-last-args))
7730 org-agenda-query-string
7731 (+ (length org-agenda-query-string)
7732 (if (member char '(?\{ ?\})) 0 1))))
7733 (set-register org-agenda-query-register org-agenda-query-string)
7734 (let ((org-agenda-overriding-arguments
7735 (cdr org-agenda-redo-command)))
7736 (org-agenda-redo)))
7737 (t (error "Cannot manipulate query for %s-type agenda buffers"
7738 org-agenda-type))))
7740 (defun org-add-to-string (var string)
7741 (set var (concat (symbol-value var) string)))
7743 (defun org-agenda-goto-date (span)
7744 "Jump to DATE in agenda."
7745 (interactive "P")
7746 (let* ((org-read-date-prefer-future
7747 (eval org-agenda-jump-prefer-future))
7748 (date (org-read-date))
7749 (day (time-to-days (org-time-string-to-time date)))
7750 (org-agenda-sticky-orig org-agenda-sticky)
7751 (org-agenda-buffer-tmp-name (buffer-name))
7752 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7753 (0-arg (or current-prefix-arg (car args)))
7754 (2-arg (nth 2 args))
7755 (with-hour-p (nth 4 org-agenda-redo-command))
7756 (newcmd (list 'org-agenda-list 0-arg date
7757 (org-agenda-span-to-ndays
7758 2-arg (org-time-string-to-absolute date))
7759 with-hour-p))
7760 (newargs (cdr newcmd))
7761 (inhibit-read-only t)
7762 org-agenda-sticky)
7763 (if (not (org-agenda-check-type t 'agenda))
7764 (error "Not available in non-agenda views")
7765 (add-text-properties (point-min) (point-max)
7766 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7767 (org-agenda-redo)
7768 (goto-char (point-min))
7769 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7770 (save-excursion (move-beginning-of-line 2) (eobp))))
7771 (move-beginning-of-line 2))
7772 (setq org-agenda-sticky org-agenda-sticky-orig
7773 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7775 (defun org-agenda-goto-today ()
7776 "Go to today."
7777 (interactive)
7778 (org-agenda-check-type t 'timeline 'agenda)
7779 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7780 (curspan (nth 2 args))
7781 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7782 (cond
7783 (tdpos (goto-char tdpos))
7784 ((eq org-agenda-type 'agenda)
7785 (let* ((sd (org-agenda-compute-starting-span
7786 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7787 (org-agenda-overriding-arguments args))
7788 (setf (nth 1 org-agenda-overriding-arguments) sd)
7789 (org-agenda-redo)
7790 (org-agenda-find-same-or-today-or-agenda)))
7791 (t (error "Cannot find today")))))
7793 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7794 (goto-char
7795 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7796 (text-property-any (point-min) (point-max) 'org-today t)
7797 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7798 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7799 (org-agenda-goto-block-beginning))
7800 (point-min))))
7802 (defun org-agenda-goto-block-beginning ()
7803 "Go the agenda block beginning."
7804 (interactive)
7805 (if (not (derived-mode-p 'org-agenda-mode))
7806 (error "Cannot execute this command outside of org-agenda-mode buffers")
7807 (let (dest)
7808 (save-excursion
7809 (unless (looking-at "\\'")
7810 (forward-char))
7811 (let* ((prop 'org-agenda-structural-header)
7812 (p (previous-single-property-change (point) prop))
7813 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7814 (1- (point))) prop)))
7815 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7816 (if (not dest)
7817 (error "Cannot find the beginning of the blog")
7818 (goto-char dest)
7819 (move-beginning-of-line 1)))))
7821 (defun org-agenda-later (arg)
7822 "Go forward in time by the current span.
7823 With prefix ARG, go forward that many times the current span."
7824 (interactive "p")
7825 (org-agenda-check-type t 'agenda)
7826 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7827 (span (or (nth 2 args) org-agenda-current-span))
7828 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7829 (greg (calendar-gregorian-from-absolute sd))
7830 (cnt (org-get-at-bol 'org-day-cnt))
7831 greg2)
7832 (cond
7833 ((numberp span)
7834 (setq sd (+ (* span arg) sd)))
7835 ((eq span 'day)
7836 (setq sd (+ arg sd)))
7837 ((eq span 'week)
7838 (setq sd (+ (* 7 arg) sd)))
7839 ((eq span 'fortnight)
7840 (setq sd (+ (* 14 arg) sd)))
7841 ((eq span 'month)
7842 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7843 sd (calendar-absolute-from-gregorian greg2))
7844 (setcar greg2 (1+ (car greg2))))
7845 ((eq span 'year)
7846 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7847 sd (calendar-absolute-from-gregorian greg2))
7848 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7850 (setq sd (+ (* span arg) sd))))
7851 (let ((org-agenda-overriding-cmd
7852 ;; `cmd' may have been set by `org-agenda-run-series' which
7853 ;; uses `org-agenda-overriding-cmd' to decide whether
7854 ;; overriding is allowed for `cmd'
7855 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7856 (org-agenda-overriding-arguments
7857 (list (car args) sd span)))
7858 (org-agenda-redo)
7859 (org-agenda-find-same-or-today-or-agenda cnt))))
7861 (defun org-agenda-earlier (arg)
7862 "Go backward in time by the current span.
7863 With prefix ARG, go backward that many times the current span."
7864 (interactive "p")
7865 (org-agenda-later (- arg)))
7867 (defun org-agenda-view-mode-dispatch ()
7868 "Call one of the view mode commands."
7869 (interactive)
7870 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7871 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7872 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7873 (let ((a (read-char-exclusive)))
7874 (case a
7875 (?\ (call-interactively 'org-agenda-reset-view))
7876 (?d (call-interactively 'org-agenda-day-view))
7877 (?w (call-interactively 'org-agenda-week-view))
7878 (?t (call-interactively 'org-agenda-fortnight-view))
7879 (?m (call-interactively 'org-agenda-month-view))
7880 (?y (call-interactively 'org-agenda-year-view))
7881 (?l (call-interactively 'org-agenda-log-mode))
7882 (?L (org-agenda-log-mode '(4)))
7883 (?c (org-agenda-log-mode 'clockcheck))
7884 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7885 (?a (call-interactively 'org-agenda-archives-mode))
7886 (?A (org-agenda-archives-mode 'files))
7887 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7888 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7889 (?G (call-interactively 'org-agenda-toggle-time-grid))
7890 (?D (call-interactively 'org-agenda-toggle-diary))
7891 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7892 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7893 (org-agenda-check-type t 'timeline 'agenda)
7894 (org-agenda-redo))
7895 (message "Display now includes inactive timestamps as well"))
7896 (?q (message "Abort"))
7897 (otherwise (error "Invalid key" )))))
7899 (defun org-agenda-reset-view ()
7900 "Switch to default view for agenda."
7901 (interactive)
7902 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7903 (defun org-agenda-day-view (&optional day-of-month)
7904 "Switch to daily view for agenda.
7905 With argument DAY-OF-MONTH, switch to that day of the month."
7906 (interactive "P")
7907 (org-agenda-change-time-span 'day day-of-month))
7908 (defun org-agenda-week-view (&optional iso-week)
7909 "Switch to daily view for agenda.
7910 With argument ISO-WEEK, switch to the corresponding ISO week.
7911 If ISO-WEEK has more then 2 digits, only the last two encode the
7912 week. Any digits before this encode a year. So 200712 means
7913 week 12 of year 2007. Years in the range 1938-2037 can also be
7914 written as 2-digit years."
7915 (interactive "P")
7916 (org-agenda-change-time-span 'week iso-week))
7917 (defun org-agenda-fortnight-view (&optional iso-week)
7918 "Switch to daily view for agenda.
7919 With argument ISO-WEEK, switch to the corresponding ISO week.
7920 If ISO-WEEK has more then 2 digits, only the last two encode the
7921 week. Any digits before this encode a year. So 200712 means
7922 week 12 of year 2007. Years in the range 1938-2037 can also be
7923 written as 2-digit years."
7924 (interactive "P")
7925 (org-agenda-change-time-span 'fortnight iso-week))
7926 (defun org-agenda-month-view (&optional month)
7927 "Switch to monthly view for agenda.
7928 With argument MONTH, switch to that month."
7929 (interactive "P")
7930 (org-agenda-change-time-span 'month month))
7931 (defun org-agenda-year-view (&optional year)
7932 "Switch to yearly view for agenda.
7933 With argument YEAR, switch to that year.
7934 If MONTH has more then 2 digits, only the last two encode the
7935 month. Any digits before this encode a year. So 200712 means
7936 December year 2007. Years in the range 1938-2037 can also be
7937 written as 2-digit years."
7938 (interactive "P")
7939 (when year
7940 (setq year (org-small-year-to-year year)))
7941 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7942 (org-agenda-change-time-span 'year year)
7943 (error "Abort")))
7945 (defun org-agenda-change-time-span (span &optional n)
7946 "Change the agenda view to SPAN.
7947 SPAN may be `day', `week', `fortnight', `month', `year'."
7948 (org-agenda-check-type t 'agenda)
7949 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7950 (curspan (nth 2 args)))
7951 (if (and (not n) (equal curspan span))
7952 (error "Viewing span is already \"%s\"" span))
7953 (let* ((sd (or (org-get-at-bol 'day)
7954 (nth 1 args)
7955 org-starting-day))
7956 (sd (org-agenda-compute-starting-span sd span n))
7957 (org-agenda-overriding-cmd
7958 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7959 (org-agenda-overriding-arguments
7960 (list (car args) sd span)))
7961 (org-agenda-redo)
7962 (org-agenda-find-same-or-today-or-agenda))
7963 (org-agenda-set-mode-name)
7964 (message "Switched to %s view" span)))
7966 (defun org-agenda-compute-starting-span (sd span &optional n)
7967 "Compute starting date for agenda.
7968 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7969 is a cons cell with the starting date and the number of days,
7970 so that the date SD will be in that range."
7971 (let* ((greg (calendar-gregorian-from-absolute sd))
7972 (dg (nth 1 greg))
7973 (mg (car greg))
7974 (yg (nth 2 greg)))
7975 (cond
7976 ((eq span 'day)
7977 (when n
7978 (setq sd (+ (calendar-absolute-from-gregorian
7979 (list mg 1 yg))
7980 n -1))))
7981 ((or (eq span 'week) (eq span 'fortnight))
7982 (let* ((nt (calendar-day-of-week
7983 (calendar-gregorian-from-absolute sd)))
7984 (d (if org-agenda-start-on-weekday
7985 (- nt org-agenda-start-on-weekday)
7988 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7989 (when n
7990 (require 'cal-iso)
7991 (when (> n 99)
7992 (setq y1 (org-small-year-to-year (/ n 100))
7993 n (mod n 100)))
7994 (setq sd
7995 (calendar-absolute-from-iso
7996 (list n 1
7997 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7998 ((eq span 'month)
7999 (let (y1)
8000 (when (and n (> n 99))
8001 (setq y1 (org-small-year-to-year (/ n 100))
8002 n (mod n 100)))
8003 (setq sd (calendar-absolute-from-gregorian
8004 (list (or n mg) 1 (or y1 yg))))))
8005 ((eq span 'year)
8006 (setq sd (calendar-absolute-from-gregorian
8007 (list 1 1 (or n yg))))))
8008 sd))
8010 (defun org-agenda-next-date-line (&optional arg)
8011 "Jump to the next line indicating a date in agenda buffer."
8012 (interactive "p")
8013 (org-agenda-check-type t 'agenda 'timeline)
8014 (beginning-of-line 1)
8015 ;; This does not work if user makes date format that starts with a blank
8016 (if (looking-at "^\\S-") (forward-char 1))
8017 (if (not (re-search-forward "^\\S-" nil t arg))
8018 (progn
8019 (backward-char 1)
8020 (error "No next date after this line in this buffer")))
8021 (goto-char (match-beginning 0)))
8023 (defun org-agenda-previous-date-line (&optional arg)
8024 "Jump to the previous line indicating a date in agenda buffer."
8025 (interactive "p")
8026 (org-agenda-check-type t 'agenda 'timeline)
8027 (beginning-of-line 1)
8028 (if (not (re-search-backward "^\\S-" nil t arg))
8029 (error "No previous date before this line in this buffer")))
8031 ;; Initialize the highlight
8032 (defvar org-hl (make-overlay 1 1))
8033 (overlay-put org-hl 'face 'highlight)
8035 (defun org-highlight (begin end &optional buffer)
8036 "Highlight a region with overlay."
8037 (move-overlay org-hl begin end (or buffer (current-buffer))))
8039 (defun org-unhighlight ()
8040 "Detach overlay INDEX."
8041 (org-detach-overlay org-hl))
8043 (defun org-unhighlight-once ()
8044 "Remove the highlight from its position, and this function from the hook."
8045 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8046 (org-unhighlight))
8048 (defvar org-agenda-pre-follow-window-conf nil)
8049 (defun org-agenda-follow-mode ()
8050 "Toggle follow mode in an agenda buffer."
8051 (interactive)
8052 (unless org-agenda-follow-mode
8053 (setq org-agenda-pre-follow-window-conf
8054 (current-window-configuration)))
8055 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8056 (unless org-agenda-follow-mode
8057 (set-window-configuration org-agenda-pre-follow-window-conf))
8058 (org-agenda-set-mode-name)
8059 (org-agenda-do-context-action)
8060 (message "Follow mode is %s"
8061 (if org-agenda-follow-mode "on" "off")))
8063 (defun org-agenda-entry-text-mode (&optional arg)
8064 "Toggle entry text mode in an agenda buffer."
8065 (interactive "P")
8066 (if (or org-agenda-tag-filter
8067 org-agenda-category-filter
8068 org-agenda-regexp-filter
8069 org-agenda-top-headline-filter)
8070 (user-error "Can't show entry text in filtered views")
8071 (setq org-agenda-entry-text-mode (or (integerp arg)
8072 (not org-agenda-entry-text-mode)))
8073 (org-agenda-entry-text-hide)
8074 (and org-agenda-entry-text-mode
8075 (let ((org-agenda-entry-text-maxlines
8076 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8077 (org-agenda-entry-text-show)))
8078 (org-agenda-set-mode-name)
8079 (message "Entry text mode is %s%s"
8080 (if org-agenda-entry-text-mode "on" "off")
8081 (if (not org-agenda-entry-text-mode) ""
8082 (format " (maximum number of lines is %d)"
8083 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8085 (defun org-agenda-clockreport-mode (&optional with-filter)
8086 "Toggle clocktable mode in an agenda buffer.
8087 With prefix arg WITH-FILTER, make the clocktable respect the current
8088 agenda filter."
8089 (interactive "P")
8090 (org-agenda-check-type t 'agenda)
8091 (if with-filter
8092 (setq org-agenda-clockreport-mode 'with-filter)
8093 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8094 (org-agenda-set-mode-name)
8095 (org-agenda-redo)
8096 (message "Clocktable mode is %s"
8097 (if org-agenda-clockreport-mode "on" "off")))
8099 (defun org-agenda-log-mode (&optional special)
8100 "Toggle log mode in an agenda buffer.
8101 With argument SPECIAL, show all possible log items, not only the ones
8102 configured in `org-agenda-log-mode-items'.
8103 With a double `C-u' prefix arg, show *only* log items, nothing else."
8104 (interactive "P")
8105 (org-agenda-check-type t 'agenda 'timeline)
8106 (setq org-agenda-show-log
8107 (cond
8108 ((equal special '(16)) 'only)
8109 ((eq special 'clockcheck)
8110 (if (eq org-agenda-show-log 'clockcheck)
8111 nil 'clockcheck))
8112 (special '(closed clock state))
8113 (t (not org-agenda-show-log))))
8114 (org-agenda-set-mode-name)
8115 (org-agenda-redo)
8116 (message "Log mode is %s"
8117 (if org-agenda-show-log "on" "off")))
8119 (defun org-agenda-archives-mode (&optional with-files)
8120 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8121 When called with a prefix argument, include all archive files as well."
8122 (interactive "P")
8123 (setq org-agenda-archives-mode
8124 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8125 (org-agenda-set-mode-name)
8126 (org-agenda-redo)
8127 (message
8128 "%s"
8129 (cond
8130 ((eq org-agenda-archives-mode nil)
8131 "No archives are included")
8132 ((eq org-agenda-archives-mode 'trees)
8133 (format "Trees with :%s: tag are included" org-archive-tag))
8134 ((eq org-agenda-archives-mode t)
8135 (format "Trees with :%s: tag and all active archive files are included"
8136 org-archive-tag)))))
8138 (defun org-agenda-toggle-diary ()
8139 "Toggle diary inclusion in an agenda buffer."
8140 (interactive)
8141 (org-agenda-check-type t 'agenda)
8142 (setq org-agenda-include-diary (not org-agenda-include-diary))
8143 (org-agenda-redo)
8144 (org-agenda-set-mode-name)
8145 (message "Diary inclusion turned %s"
8146 (if org-agenda-include-diary "on" "off")))
8148 (defun org-agenda-toggle-deadlines ()
8149 "Toggle inclusion of entries with a deadline in an agenda buffer."
8150 (interactive)
8151 (org-agenda-check-type t 'agenda)
8152 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8153 (org-agenda-redo)
8154 (org-agenda-set-mode-name)
8155 (message "Deadlines inclusion turned %s"
8156 (if org-agenda-include-deadlines "on" "off")))
8158 (defun org-agenda-toggle-time-grid ()
8159 "Toggle time grid in an agenda buffer."
8160 (interactive)
8161 (org-agenda-check-type t 'agenda)
8162 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8163 (org-agenda-redo)
8164 (org-agenda-set-mode-name)
8165 (message "Time-grid turned %s"
8166 (if org-agenda-use-time-grid "on" "off")))
8168 (defun org-agenda-set-mode-name ()
8169 "Set the mode name to indicate all the small mode settings."
8170 (setq mode-name
8171 (list "Org-Agenda"
8172 (if (get 'org-agenda-files 'org-restrict) " []" "")
8174 '(:eval (org-agenda-span-name org-agenda-current-span))
8175 (if org-agenda-follow-mode " Follow" "")
8176 (if org-agenda-entry-text-mode " ETxt" "")
8177 (if org-agenda-include-diary " Diary" "")
8178 (if org-agenda-include-deadlines " Ddl" "")
8179 (if org-agenda-use-time-grid " Grid" "")
8180 (if (and (boundp 'org-habit-show-habits)
8181 org-habit-show-habits) " Habit" "")
8182 (cond
8183 ((consp org-agenda-show-log) " LogAll")
8184 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8185 (org-agenda-show-log " Log")
8186 (t ""))
8187 (if (or org-agenda-category-filter
8188 (get 'org-agenda-category-filter :preset-filter))
8189 '(:eval (org-propertize
8190 (concat " <"
8191 (mapconcat
8192 'identity
8193 (append
8194 (get 'org-agenda-category-filter :preset-filter)
8195 org-agenda-category-filter)
8197 ">")
8198 'face 'org-agenda-filter-category
8199 'help-echo "Category used in filtering")) "")
8200 (if (or org-agenda-tag-filter
8201 (get 'org-agenda-tag-filter :preset-filter))
8202 '(:eval (org-propertize
8203 (concat " {"
8204 (mapconcat
8205 'identity
8206 (append
8207 (get 'org-agenda-tag-filter :preset-filter)
8208 org-agenda-tag-filter)
8210 "}")
8211 'face 'org-agenda-filter-tags
8212 'help-echo "Tags used in filtering")) "")
8213 (if (or org-agenda-regexp-filter
8214 (get 'org-agenda-regexp-filter :preset-filter))
8215 '(:eval (org-propertize
8216 (concat " ["
8217 (mapconcat
8218 'identity
8219 (append
8220 (get 'org-agenda-regexp-filter :preset-filter)
8221 org-agenda-regexp-filter)
8223 "]")
8224 'face 'org-agenda-filter-regexp
8225 'help-echo "Regexp used in filtering")) "")
8226 (if org-agenda-archives-mode
8227 (if (eq org-agenda-archives-mode t)
8228 " Archives"
8229 (format " :%s:" org-archive-tag))
8231 (if org-agenda-clockreport-mode
8232 (if (eq org-agenda-clockreport-mode 'with-filter)
8233 " Clock{}" " Clock")
8234 "")))
8235 (force-mode-line-update))
8237 (define-obsolete-function-alias
8238 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8240 (defun org-agenda-update-agenda-type ()
8241 "Update the agenda type after each command."
8242 (setq org-agenda-type
8243 (or (get-text-property (point) 'org-agenda-type)
8244 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8246 (defun org-agenda-next-line ()
8247 "Move cursor to the next line, and show if follow mode is active."
8248 (interactive)
8249 (call-interactively 'next-line)
8250 (org-agenda-do-context-action))
8252 (defun org-agenda-previous-line ()
8253 "Move cursor to the previous line, and show if follow-mode is active."
8254 (interactive)
8255 (call-interactively 'previous-line)
8256 (org-agenda-do-context-action))
8258 (defun org-agenda-next-item (n)
8259 "Move cursor to next agenda item."
8260 (interactive "p")
8261 (let ((col (current-column)))
8262 (dotimes (c n)
8263 (when (next-single-property-change (point-at-eol) 'org-marker)
8264 (move-end-of-line 1)
8265 (goto-char (next-single-property-change (point) 'org-marker))))
8266 (org-move-to-column col))
8267 (org-agenda-do-context-action))
8269 (defun org-agenda-previous-item (n)
8270 "Move cursor to next agenda item."
8271 (interactive "p")
8272 (dotimes (c n)
8273 (let ((col (current-column))
8274 (goto (save-excursion
8275 (move-end-of-line 0)
8276 (previous-single-property-change (point) 'org-marker))))
8277 (if goto (goto-char goto))
8278 (org-move-to-column col)))
8279 (org-agenda-do-context-action))
8281 (defun org-agenda-do-context-action ()
8282 "Show outline path and, maybe, follow mode window."
8283 (let ((m (org-get-at-bol 'org-marker)))
8284 (when (and (markerp m) (marker-buffer m))
8285 (and org-agenda-follow-mode
8286 (if org-agenda-follow-indirect
8287 (org-agenda-tree-to-indirect-buffer nil)
8288 (org-agenda-show)))
8289 (and org-agenda-show-outline-path
8290 (org-with-point-at m (org-display-outline-path t))))))
8292 (defun org-agenda-show-tags ()
8293 "Show the tags applicable to the current item."
8294 (interactive)
8295 (let* ((tags (org-get-at-bol 'tags)))
8296 (if tags
8297 (message "Tags are :%s:"
8298 (org-no-properties (mapconcat 'identity tags ":")))
8299 (message "No tags associated with this line"))))
8301 (defun org-agenda-goto (&optional highlight)
8302 "Go to the Org-mode file which contains the item at point."
8303 (interactive)
8304 (let* ((marker (or (org-get-at-bol 'org-marker)
8305 (org-agenda-error)))
8306 (buffer (marker-buffer marker))
8307 (pos (marker-position marker)))
8308 (switch-to-buffer-other-window buffer)
8309 (widen)
8310 (push-mark)
8311 (goto-char pos)
8312 (when (derived-mode-p 'org-mode)
8313 (org-show-context 'agenda)
8314 (save-excursion
8315 (and (outline-next-heading)
8316 (org-flag-heading nil)))) ; show the next heading
8317 (when (outline-invisible-p)
8318 (show-entry)) ; display invisible text
8319 (recenter (/ (window-height) 2))
8320 (run-hooks 'org-agenda-after-show-hook)
8321 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8323 (defvar org-agenda-after-show-hook nil
8324 "Normal hook run after an item has been shown from the agenda.
8325 Point is in the buffer where the item originated.")
8327 (defun org-agenda-kill ()
8328 "Kill the entry or subtree belonging to the current agenda entry."
8329 (interactive)
8330 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8331 (let* ((bufname-orig (buffer-name))
8332 (marker (or (org-get-at-bol 'org-marker)
8333 (org-agenda-error)))
8334 (buffer (marker-buffer marker))
8335 (pos (marker-position marker))
8336 (type (org-get-at-bol 'type))
8337 dbeg dend (n 0) conf)
8338 (org-with-remote-undo buffer
8339 (with-current-buffer buffer
8340 (save-excursion
8341 (goto-char pos)
8342 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8343 (setq dbeg (progn (org-back-to-heading t) (point))
8344 dend (org-end-of-subtree t t))
8345 (setq dbeg (point-at-bol)
8346 dend (min (point-max) (1+ (point-at-eol)))))
8347 (goto-char dbeg)
8348 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8349 (setq conf (or (eq t org-agenda-confirm-kill)
8350 (and (numberp org-agenda-confirm-kill)
8351 (> n org-agenda-confirm-kill))))
8352 (and conf
8353 (not (y-or-n-p
8354 (format "Delete entry with %d lines in buffer \"%s\"? "
8355 n (buffer-name buffer))))
8356 (error "Abort"))
8357 (let ((org-agenda-buffer-name bufname-orig))
8358 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8359 (with-current-buffer buffer (delete-region dbeg dend))
8360 (message "Agenda item and source killed"))))
8362 (defvar org-archive-default-command) ; defined in org-archive.el
8363 (defun org-agenda-archive-default ()
8364 "Archive the entry or subtree belonging to the current agenda entry."
8365 (interactive)
8366 (require 'org-archive)
8367 (org-agenda-archive-with org-archive-default-command))
8369 (defun org-agenda-archive-default-with-confirmation ()
8370 "Archive the entry or subtree belonging to the current agenda entry."
8371 (interactive)
8372 (require 'org-archive)
8373 (org-agenda-archive-with org-archive-default-command 'confirm))
8375 (defun org-agenda-archive ()
8376 "Archive the entry or subtree belonging to the current agenda entry."
8377 (interactive)
8378 (org-agenda-archive-with 'org-archive-subtree))
8380 (defun org-agenda-archive-to-archive-sibling ()
8381 "Move the entry to the archive sibling."
8382 (interactive)
8383 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8385 (defun org-agenda-archive-with (cmd &optional confirm)
8386 "Move the entry to the archive sibling."
8387 (interactive)
8388 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8389 (let* ((bufname-orig (buffer-name))
8390 (marker (or (org-get-at-bol 'org-marker)
8391 (org-agenda-error)))
8392 (buffer (marker-buffer marker))
8393 (pos (marker-position marker)))
8394 (org-with-remote-undo buffer
8395 (with-current-buffer buffer
8396 (if (derived-mode-p 'org-mode)
8397 (if (and confirm
8398 (not (y-or-n-p "Archive this subtree or entry? ")))
8399 (error "Abort")
8400 (save-window-excursion
8401 (goto-char pos)
8402 (let ((org-agenda-buffer-name bufname-orig))
8403 (org-remove-subtree-entries-from-agenda))
8404 (org-back-to-heading t)
8405 (funcall cmd)))
8406 (error "Archiving works only in Org-mode files"))))))
8408 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8409 "Remove all lines in the agenda that correspond to a given subtree.
8410 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8411 If this information is not given, the function uses the tree at point."
8412 (let ((buf (or buf (current-buffer))) m p)
8413 (save-excursion
8414 (unless (and beg end)
8415 (org-back-to-heading t)
8416 (setq beg (point))
8417 (org-end-of-subtree t)
8418 (setq end (point)))
8419 (set-buffer (get-buffer org-agenda-buffer-name))
8420 (save-excursion
8421 (goto-char (point-max))
8422 (beginning-of-line 1)
8423 (while (not (bobp))
8424 (when (and (setq m (org-get-at-bol 'org-marker))
8425 (equal buf (marker-buffer m))
8426 (setq p (marker-position m))
8427 (>= p beg)
8428 (< p end))
8429 (let ((inhibit-read-only t))
8430 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8431 (beginning-of-line 0))))))
8433 (defun org-agenda-refile (&optional goto rfloc no-update)
8434 "Refile the item at point.
8436 When GOTO is 0 or '(64), clear the refile cache.
8437 When GOTO is '(16), go to the location of the last refiled item.
8438 RFLOC can be a refile location obtained in a different way.
8439 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8440 (interactive "P")
8441 (cond
8442 ((member goto '(0 (64)))
8443 (org-refile-cache-clear))
8444 ((equal goto '(16))
8445 (org-refile-goto-last-stored))
8447 (let* ((buffer-orig (buffer-name))
8448 (marker (or (org-get-at-bol 'org-hd-marker)
8449 (org-agenda-error)))
8450 (buffer (marker-buffer marker))
8451 (pos (marker-position marker))
8452 (rfloc (or rfloc
8453 (org-refile-get-location
8454 (if goto "Goto" "Refile to") buffer
8455 org-refile-allow-creating-parent-nodes))))
8456 (with-current-buffer buffer
8457 (save-excursion
8458 (save-restriction
8459 (widen)
8460 (goto-char marker)
8461 (let ((org-agenda-buffer-name buffer-orig))
8462 (org-remove-subtree-entries-from-agenda))
8463 (org-refile goto buffer rfloc)))))
8464 (unless no-update (org-agenda-redo)))))
8466 (defun org-agenda-open-link (&optional arg)
8467 "Open the link(s) in the current entry, if any.
8468 This looks for a link in the displayed line in the agenda.
8469 It also looks at the text of the entry itself."
8470 (interactive "P")
8471 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8472 (org-get-at-bol 'org-marker)))
8473 (buffer (and marker (marker-buffer marker)))
8474 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8475 (lkall (and buffer (org-offer-links-in-entry
8476 buffer marker arg prefix)))
8477 (lk0 (car lkall))
8478 (lk (if (stringp lk0) (list lk0) lk0))
8479 (lkend (cdr lkall))
8480 trg)
8481 (cond
8482 ((and buffer lk)
8483 (mapcar (lambda(l)
8484 (with-current-buffer buffer
8485 (setq trg (and (string-match org-bracket-link-regexp l)
8486 (match-string 1 l)))
8487 (if (or (not trg) (string-match org-any-link-re trg))
8488 (save-excursion
8489 (save-restriction
8490 (widen)
8491 (goto-char marker)
8492 (when (search-forward l nil lkend)
8493 (goto-char (match-beginning 0))
8494 (org-open-at-point))))
8495 ;; This is an internal link, widen the buffer
8496 (switch-to-buffer-other-window buffer)
8497 (widen)
8498 (goto-char marker)
8499 (when (search-forward l nil lkend)
8500 (goto-char (match-beginning 0))
8501 (org-open-at-point)))))
8502 lk))
8503 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8504 (save-excursion
8505 (beginning-of-line 1)
8506 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8507 (org-open-link-from-string (match-string 1)))
8508 (t (message "No link to open here")))))
8510 (defun org-agenda-copy-local-variable (var)
8511 "Get a variable from a referenced buffer and install it here."
8512 (let ((m (org-get-at-bol 'org-marker)))
8513 (when (and m (buffer-live-p (marker-buffer m)))
8514 (org-set-local var (with-current-buffer (marker-buffer m)
8515 (symbol-value var))))))
8517 (defun org-agenda-switch-to (&optional delete-other-windows)
8518 "Go to the Org-mode file which contains the item at point."
8519 (interactive)
8520 (if (and org-return-follows-link
8521 (not (org-get-at-bol 'org-marker))
8522 (org-in-regexp org-bracket-link-regexp))
8523 (org-open-link-from-string (match-string 0))
8524 (let* ((marker (or (org-get-at-bol 'org-marker)
8525 (org-agenda-error)))
8526 (buffer (marker-buffer marker))
8527 (pos (marker-position marker)))
8528 (org-pop-to-buffer-same-window buffer)
8529 (and delete-other-windows (delete-other-windows))
8530 (widen)
8531 (goto-char pos)
8532 (when (derived-mode-p 'org-mode)
8533 (org-show-context 'agenda)
8534 (save-excursion
8535 (and (outline-next-heading)
8536 (org-flag-heading nil))) ; show the next heading
8537 (when (outline-invisible-p)
8538 (show-entry)) ; display invisible text
8539 (run-hooks 'org-agenda-after-show-hook)))))
8541 (defun org-agenda-goto-mouse (ev)
8542 "Go to the Org-mode file which contains the item at the mouse click."
8543 (interactive "e")
8544 (mouse-set-point ev)
8545 (org-agenda-goto))
8547 (defun org-agenda-show (&optional full-entry)
8548 "Display the Org-mode file which contains the item at point.
8549 With prefix argument FULL-ENTRY, make the entire entry visible
8550 if it was hidden in the outline."
8551 (interactive "P")
8552 (let ((win (selected-window)))
8553 (if full-entry
8554 (let ((org-show-entry-below t))
8555 (org-agenda-goto t))
8556 (org-agenda-goto t))
8557 (select-window win)))
8559 (defvar org-agenda-show-window nil)
8560 (defun org-agenda-show-and-scroll-up (&optional arg)
8561 "Display the Org-mode file which contains the item at point.
8562 When called repeatedly, scroll the window that is displaying the buffer.
8563 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8564 `show-subtree' to display the item, so that drawers and logbooks stay
8565 folded."
8566 (interactive "P")
8567 (let ((win (selected-window)))
8568 (if (and (window-live-p org-agenda-show-window)
8569 (eq this-command last-command))
8570 (progn
8571 (select-window org-agenda-show-window)
8572 (ignore-errors (scroll-up)))
8573 (org-agenda-goto t)
8574 (if arg (org-show-entry) (show-subtree))
8575 (setq org-agenda-show-window (selected-window)))
8576 (select-window win)))
8578 (defun org-agenda-show-scroll-down ()
8579 "Scroll down the window showing the agenda."
8580 (interactive)
8581 (let ((win (selected-window)))
8582 (when (window-live-p org-agenda-show-window)
8583 (select-window org-agenda-show-window)
8584 (ignore-errors (scroll-down))
8585 (select-window win))))
8587 (defun org-agenda-show-1 (&optional more)
8588 "Display the Org-mode file which contains the item at point.
8589 The prefix arg selects the amount of information to display:
8591 0 hide the subtree
8592 1 just show the entry according to defaults.
8593 2 show the children view
8594 3 show the subtree view
8595 4 show the entire subtree and any LOGBOOK drawers
8596 5 show the entire subtree and any drawers
8597 With prefix argument FULL-ENTRY, make the entire entry visible
8598 if it was hidden in the outline."
8599 (interactive "p")
8600 (let ((win (selected-window)))
8601 (org-agenda-goto t)
8602 (org-recenter-heading 1)
8603 (cond
8604 ((= more 0)
8605 (hide-subtree)
8606 (save-excursion
8607 (org-back-to-heading)
8608 (run-hook-with-args 'org-cycle-hook 'folded))
8609 (message "Remote: FOLDED"))
8610 ((and (org-called-interactively-p 'any) (= more 1))
8611 (message "Remote: show with default settings"))
8612 ((= more 2)
8613 (show-entry)
8614 (show-children)
8615 (save-excursion
8616 (org-back-to-heading)
8617 (run-hook-with-args 'org-cycle-hook 'children))
8618 (message "Remote: CHILDREN"))
8619 ((= more 3)
8620 (show-subtree)
8621 (save-excursion
8622 (org-back-to-heading)
8623 (run-hook-with-args 'org-cycle-hook 'subtree))
8624 (message "Remote: SUBTREE"))
8625 ((= more 4)
8626 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8627 (org-drawer-regexp
8628 (concat "^[ \t]*:\\("
8629 (mapconcat 'regexp-quote org-drawers "\\|")
8630 "\\):[ \t]*$")))
8631 (show-subtree)
8632 (save-excursion
8633 (org-back-to-heading)
8634 (org-cycle-hide-drawers 'subtree)))
8635 (message "Remote: SUBTREE AND LOGBOOK"))
8636 ((> more 4)
8637 (show-subtree)
8638 (message "Remote: SUBTREE AND ALL DRAWERS")))
8639 (select-window win)))
8641 (defun org-recenter-heading (n)
8642 (save-excursion
8643 (org-back-to-heading)
8644 (recenter n)))
8646 (defvar org-agenda-cycle-counter nil)
8647 (defun org-agenda-cycle-show (&optional n)
8648 "Show the current entry in another window, with default settings.
8649 Default settings are taken from `org-show-hierarchy-above' and siblings.
8650 When use repeatedly in immediate succession, the remote entry will cycle
8651 through visibility
8653 children -> subtree -> folded
8655 When called with a numeric prefix arg, that arg will be passed through to
8656 `org-agenda-show-1'. For the interpretation of that argument, see the
8657 docstring of `org-agenda-show-1'."
8658 (interactive "P")
8659 (if (integerp n)
8660 (setq org-agenda-cycle-counter n)
8661 (if (not (eq last-command this-command))
8662 (setq org-agenda-cycle-counter 1)
8663 (if (equal org-agenda-cycle-counter 0)
8664 (setq org-agenda-cycle-counter 2)
8665 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8666 (if (> org-agenda-cycle-counter 3)
8667 (setq org-agenda-cycle-counter 0)))))
8668 (org-agenda-show-1 org-agenda-cycle-counter))
8670 (defun org-agenda-recenter (arg)
8671 "Display the Org-mode file which contains the item at point and recenter."
8672 (interactive "P")
8673 (let ((win (selected-window)))
8674 (org-agenda-goto t)
8675 (recenter arg)
8676 (select-window win)))
8678 (defun org-agenda-show-mouse (ev)
8679 "Display the Org-mode file which contains the item at the mouse click."
8680 (interactive "e")
8681 (mouse-set-point ev)
8682 (org-agenda-show))
8684 (defun org-agenda-check-no-diary ()
8685 "Check if the entry is a diary link and abort if yes."
8686 (if (org-get-at-bol 'org-agenda-diary-link)
8687 (org-agenda-error)))
8689 (defun org-agenda-error ()
8690 (error "Command not allowed in this line"))
8692 (defun org-agenda-tree-to-indirect-buffer (arg)
8693 "Show the subtree corresponding to the current entry in an indirect buffer.
8694 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8696 With a numerical prefix ARG, go up to this level and then take that tree.
8697 With a negative numeric ARG, go up by this number of levels.
8698 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8699 use the dedicated frame)."
8700 (interactive "P")
8701 (if current-prefix-arg
8702 (org-agenda-do-tree-to-indirect-buffer arg)
8703 (let ((agenda-buffer (buffer-name))
8704 (agenda-window (selected-window))
8705 (indirect-window
8706 (and org-last-indirect-buffer
8707 (get-buffer-window org-last-indirect-buffer))))
8708 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8709 (unless (or (eq org-indirect-buffer-display 'new-frame)
8710 (eq org-indirect-buffer-display 'dedicated-frame))
8711 (unwind-protect
8712 (unless (and indirect-window (window-live-p indirect-window))
8713 (setq indirect-window (split-window agenda-window)))
8714 (and indirect-window (select-window indirect-window))
8715 (switch-to-buffer org-last-indirect-buffer :norecord)
8716 (fit-window-to-buffer indirect-window)))
8717 (select-window (get-buffer-window agenda-buffer)))))
8719 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8720 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8721 (org-agenda-check-no-diary)
8722 (let* ((marker (or (org-get-at-bol 'org-marker)
8723 (org-agenda-error)))
8724 (buffer (marker-buffer marker))
8725 (pos (marker-position marker)))
8726 (with-current-buffer buffer
8727 (save-excursion
8728 (goto-char pos)
8729 (funcall 'org-tree-to-indirect-buffer arg)))))
8731 (defvar org-last-heading-marker (make-marker)
8732 "Marker pointing to the headline that last changed its TODO state
8733 by a remote command from the agenda.")
8735 (defun org-agenda-todo-nextset ()
8736 "Switch TODO entry to next sequence."
8737 (interactive)
8738 (org-agenda-todo 'nextset))
8740 (defun org-agenda-todo-previousset ()
8741 "Switch TODO entry to previous sequence."
8742 (interactive)
8743 (org-agenda-todo 'previousset))
8745 (defun org-agenda-todo (&optional arg)
8746 "Cycle TODO state of line at point, also in Org-mode file.
8747 This changes the line at point, all other lines in the agenda referring to
8748 the same tree node, and the headline of the tree node in the Org-mode file."
8749 (interactive "P")
8750 (org-agenda-check-no-diary)
8751 (let* ((col (current-column))
8752 (marker (or (org-get-at-bol 'org-marker)
8753 (org-agenda-error)))
8754 (buffer (marker-buffer marker))
8755 (pos (marker-position marker))
8756 (hdmarker (org-get-at-bol 'org-hd-marker))
8757 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8758 (inhibit-read-only t)
8759 org-agenda-headline-snapshot-before-repeat newhead just-one)
8760 (org-with-remote-undo buffer
8761 (with-current-buffer buffer
8762 (widen)
8763 (goto-char pos)
8764 (org-show-context 'agenda)
8765 (save-excursion
8766 (and (outline-next-heading)
8767 (org-flag-heading nil))) ; show the next heading
8768 (let ((current-prefix-arg arg))
8769 (call-interactively 'org-todo))
8770 (and (bolp) (forward-char 1))
8771 (setq newhead (org-get-heading))
8772 (when (and (org-bound-and-true-p
8773 org-agenda-headline-snapshot-before-repeat)
8774 (not (equal org-agenda-headline-snapshot-before-repeat
8775 newhead))
8776 todayp)
8777 (setq newhead org-agenda-headline-snapshot-before-repeat
8778 just-one t))
8779 (save-excursion
8780 (org-back-to-heading)
8781 (move-marker org-last-heading-marker (point))))
8782 (beginning-of-line 1)
8783 (save-window-excursion
8784 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8785 (when (org-bound-and-true-p org-clock-out-when-done)
8786 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8787 newhead)
8788 (org-agenda-unmark-clocking-task))
8789 (org-move-to-column col))))
8791 (defun org-agenda-add-note (&optional arg)
8792 "Add a time-stamped note to the entry at point."
8793 (interactive "P")
8794 (org-agenda-check-no-diary)
8795 (let* ((marker (or (org-get-at-bol 'org-marker)
8796 (org-agenda-error)))
8797 (buffer (marker-buffer marker))
8798 (pos (marker-position marker))
8799 (hdmarker (org-get-at-bol 'org-hd-marker))
8800 (inhibit-read-only t))
8801 (with-current-buffer buffer
8802 (widen)
8803 (goto-char pos)
8804 (org-show-context 'agenda)
8805 (save-excursion
8806 (and (outline-next-heading)
8807 (org-flag-heading nil))) ; show the next heading
8808 (org-add-note))))
8810 (defun org-agenda-change-all-lines (newhead hdmarker
8811 &optional fixface just-this)
8812 "Change all lines in the agenda buffer which match HDMARKER.
8813 The new content of the line will be NEWHEAD (as modified by
8814 `org-agenda-format-item'). HDMARKER is checked with
8815 `equal' against all `org-hd-marker' text properties in the file.
8816 If FIXFACE is non-nil, the face of each item is modified according to
8817 the new TODO state.
8818 If JUST-THIS is non-nil, change just the current line, not all.
8819 If FORCE-TAGS is non nil, the car of it returns the new tags."
8820 (let* ((inhibit-read-only t)
8821 (line (org-current-line))
8822 (org-agenda-buffer (current-buffer))
8823 (thetags (with-current-buffer (marker-buffer hdmarker)
8824 (save-excursion (save-restriction (widen)
8825 (goto-char hdmarker)
8826 (org-get-tags-at)))))
8827 props m pl undone-face done-face finish new dotime level cat tags)
8828 (save-excursion
8829 (goto-char (point-max))
8830 (beginning-of-line 1)
8831 (while (not finish)
8832 (setq finish (bobp))
8833 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8834 (or (not just-this) (= (org-current-line) line))
8835 (equal m hdmarker))
8836 (setq props (text-properties-at (point))
8837 dotime (org-get-at-bol 'dotime)
8838 cat (org-get-at-bol 'org-category)
8839 level (org-get-at-bol 'level)
8840 tags thetags
8842 (let ((org-prefix-format-compiled
8843 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8844 org-prefix-format-compiled))
8845 (extra (org-get-at-bol 'extra)))
8846 (with-current-buffer (marker-buffer hdmarker)
8847 (save-excursion
8848 (save-restriction
8849 (widen)
8850 (org-agenda-format-item extra newhead level cat tags dotime)))))
8851 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8852 undone-face (org-get-at-bol 'undone-face)
8853 done-face (org-get-at-bol 'done-face))
8854 (beginning-of-line 1)
8855 (cond
8856 ((equal new "")
8857 (and (looking-at ".*\n?") (replace-match "")))
8858 ((looking-at ".*")
8859 (replace-match new t t)
8860 (beginning-of-line 1)
8861 (add-text-properties (point-at-bol) (point-at-eol) props)
8862 (when fixface
8863 (add-text-properties
8864 (point-at-bol) (point-at-eol)
8865 (list 'face
8866 (if org-last-todo-state-is-todo
8867 undone-face done-face))))
8868 (org-agenda-highlight-todo 'line)
8869 (beginning-of-line 1))
8870 (t (error "Line update did not work")))
8871 (save-restriction
8872 (narrow-to-region (point-at-bol) (point-at-eol))
8873 (org-agenda-finalize)))
8874 (beginning-of-line 0)))))
8876 (defun org-agenda-align-tags (&optional line)
8877 "Align all tags in agenda items to `org-agenda-tags-column'."
8878 (let ((inhibit-read-only t) l c)
8879 (save-excursion
8880 (goto-char (if line (point-at-bol) (point-min)))
8881 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8882 (if line (point-at-eol) nil) t)
8883 (add-text-properties
8884 (match-beginning 2) (match-end 2)
8885 (list 'face (delq nil (let ((prop (get-text-property
8886 (match-beginning 2) 'face)))
8887 (or (listp prop) (setq prop (list prop)))
8888 (if (memq 'org-tag prop)
8889 prop
8890 (cons 'org-tag prop))))))
8891 (setq l (- (match-end 2) (match-beginning 2))
8892 c (if (< org-agenda-tags-column 0)
8893 (- (abs org-agenda-tags-column) l)
8894 org-agenda-tags-column))
8895 (delete-region (match-beginning 1) (match-end 1))
8896 (goto-char (match-beginning 1))
8897 (insert (org-add-props
8898 (make-string (max 1 (- c (current-column))) ?\ )
8899 (plist-put (copy-sequence (text-properties-at (point)))
8900 'face nil))))
8901 (goto-char (point-min))
8902 (org-font-lock-add-tag-faces (point-max)))))
8904 (defun org-agenda-priority-up ()
8905 "Increase the priority of line at point, also in Org-mode file."
8906 (interactive)
8907 (org-agenda-priority 'up))
8909 (defun org-agenda-priority-down ()
8910 "Decrease the priority of line at point, also in Org-mode file."
8911 (interactive)
8912 (org-agenda-priority 'down))
8914 (defun org-agenda-priority (&optional force-direction)
8915 "Set the priority of line at point, also in Org-mode file.
8916 This changes the line at point, all other lines in the agenda referring to
8917 the same tree node, and the headline of the tree node in the Org-mode file.
8918 Called with a universal prefix arg, show the priority instead of setting it."
8919 (interactive "P")
8920 (if (equal force-direction '(4))
8921 (org-show-priority)
8922 (unless org-enable-priority-commands
8923 (error "Priority commands are disabled"))
8924 (org-agenda-check-no-diary)
8925 (let* ((col (current-column))
8926 (marker (or (org-get-at-bol 'org-marker)
8927 (org-agenda-error)))
8928 (hdmarker (org-get-at-bol 'org-hd-marker))
8929 (buffer (marker-buffer hdmarker))
8930 (pos (marker-position hdmarker))
8931 (inhibit-read-only t)
8932 newhead)
8933 (org-with-remote-undo buffer
8934 (with-current-buffer buffer
8935 (widen)
8936 (goto-char pos)
8937 (org-show-context 'agenda)
8938 (save-excursion
8939 (and (outline-next-heading)
8940 (org-flag-heading nil))) ; show the next heading
8941 (funcall 'org-priority force-direction)
8942 (end-of-line 1)
8943 (setq newhead (org-get-heading)))
8944 (org-agenda-change-all-lines newhead hdmarker)
8945 (org-move-to-column col)))))
8947 ;; FIXME: should fix the tags property of the agenda line.
8948 (defun org-agenda-set-tags (&optional tag onoff)
8949 "Set tags for the current headline."
8950 (interactive)
8951 (org-agenda-check-no-diary)
8952 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8953 (call-interactively 'org-change-tag-in-region)
8954 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8955 (org-agenda-error)))
8956 (buffer (marker-buffer hdmarker))
8957 (pos (marker-position hdmarker))
8958 (inhibit-read-only t)
8959 newhead)
8960 (org-with-remote-undo buffer
8961 (with-current-buffer buffer
8962 (widen)
8963 (goto-char pos)
8964 (save-excursion
8965 (org-show-context 'agenda))
8966 (save-excursion
8967 (and (outline-next-heading)
8968 (org-flag-heading nil))) ; show the next heading
8969 (goto-char pos)
8970 (if tag
8971 (org-toggle-tag tag onoff)
8972 (call-interactively 'org-set-tags))
8973 (end-of-line 1)
8974 (setq newhead (org-get-heading)))
8975 (org-agenda-change-all-lines newhead hdmarker)
8976 (beginning-of-line 1)))))
8978 (defun org-agenda-set-property ()
8979 "Set a property for the current headline."
8980 (interactive)
8981 (org-agenda-check-no-diary)
8982 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8983 (org-agenda-error)))
8984 (buffer (marker-buffer hdmarker))
8985 (pos (marker-position hdmarker))
8986 (inhibit-read-only t)
8987 newhead)
8988 (org-with-remote-undo buffer
8989 (with-current-buffer buffer
8990 (widen)
8991 (goto-char pos)
8992 (save-excursion
8993 (org-show-context 'agenda))
8994 (save-excursion
8995 (and (outline-next-heading)
8996 (org-flag-heading nil))) ; show the next heading
8997 (goto-char pos)
8998 (call-interactively 'org-set-property)))))
9000 (defun org-agenda-set-effort ()
9001 "Set the effort property for the current headline."
9002 (interactive)
9003 (org-agenda-check-no-diary)
9004 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9005 (org-agenda-error)))
9006 (buffer (marker-buffer hdmarker))
9007 (pos (marker-position hdmarker))
9008 (inhibit-read-only t)
9009 newhead)
9010 (org-with-remote-undo buffer
9011 (with-current-buffer buffer
9012 (widen)
9013 (goto-char pos)
9014 (save-excursion
9015 (org-show-context 'agenda))
9016 (save-excursion
9017 (and (outline-next-heading)
9018 (org-flag-heading nil))) ; show the next heading
9019 (goto-char pos)
9020 (call-interactively 'org-set-effort)
9021 (end-of-line 1)
9022 (setq newhead (org-get-heading)))
9023 (org-agenda-change-all-lines newhead hdmarker))))
9025 (defun org-agenda-toggle-archive-tag ()
9026 "Toggle the archive tag for the current entry."
9027 (interactive)
9028 (org-agenda-check-no-diary)
9029 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9030 (org-agenda-error)))
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 (call-interactively 'org-toggle-archive-tag)
9044 (end-of-line 1)
9045 (setq newhead (org-get-heading)))
9046 (org-agenda-change-all-lines newhead hdmarker)
9047 (beginning-of-line 1))))
9049 (defun org-agenda-do-date-later (arg)
9050 (interactive "P")
9051 (cond
9052 ((or (equal arg '(16))
9053 (memq last-command
9054 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9055 (setq this-command 'org-agenda-date-later-minutes)
9056 (org-agenda-date-later-minutes 1))
9057 ((or (equal arg '(4))
9058 (memq last-command
9059 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9060 (setq this-command 'org-agenda-date-later-hours)
9061 (org-agenda-date-later-hours 1))
9063 (org-agenda-date-later (prefix-numeric-value arg)))))
9065 (defun org-agenda-do-date-earlier (arg)
9066 (interactive "P")
9067 (cond
9068 ((or (equal arg '(16))
9069 (memq last-command
9070 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9071 (setq this-command 'org-agenda-date-earlier-minutes)
9072 (org-agenda-date-earlier-minutes 1))
9073 ((or (equal arg '(4))
9074 (memq last-command
9075 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9076 (setq this-command 'org-agenda-date-earlier-hours)
9077 (org-agenda-date-earlier-hours 1))
9079 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9081 (defun org-agenda-date-later (arg &optional what)
9082 "Change the date of this item to ARG day(s) later."
9083 (interactive "p")
9084 (org-agenda-check-type t 'agenda 'timeline)
9085 (org-agenda-check-no-diary)
9086 (let* ((marker (or (org-get-at-bol 'org-marker)
9087 (org-agenda-error)))
9088 (buffer (marker-buffer marker))
9089 (pos (marker-position marker))
9090 cdate today)
9091 (org-with-remote-undo buffer
9092 (with-current-buffer buffer
9093 (widen)
9094 (goto-char pos)
9095 (if (not (org-at-timestamp-p))
9096 (error "Cannot find time stamp"))
9097 (when (and org-agenda-move-date-from-past-immediately-to-today
9098 (equal arg 1)
9099 (or (not what) (eq what 'day))
9100 (not (save-match-data (org-at-date-range-p))))
9101 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9102 cdate (calendar-absolute-from-gregorian
9103 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9104 today (org-today))
9105 (if (> today cdate)
9106 ;; immediately shift to today
9107 (setq arg (- today cdate))))
9108 (org-timestamp-change arg (or what 'day))
9109 (when (and (org-at-date-range-p)
9110 (re-search-backward org-tr-regexp-both (point-at-bol)))
9111 (let ((end org-last-changed-timestamp))
9112 (org-timestamp-change arg (or what 'day))
9113 (setq org-last-changed-timestamp
9114 (concat org-last-changed-timestamp "--" end)))))
9115 (org-agenda-show-new-time marker org-last-changed-timestamp))
9116 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9118 (defun org-agenda-date-earlier (arg &optional what)
9119 "Change the date of this item to ARG day(s) earlier."
9120 (interactive "p")
9121 (org-agenda-date-later (- arg) what))
9123 (defun org-agenda-date-later-minutes (arg)
9124 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9125 (interactive "p")
9126 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9127 (org-agenda-date-later arg 'minute))
9129 (defun org-agenda-date-earlier-minutes (arg)
9130 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9131 (interactive "p")
9132 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9133 (org-agenda-date-earlier arg 'minute))
9135 (defun org-agenda-date-later-hours (arg)
9136 "Change the time of this item, in hour steps."
9137 (interactive "p")
9138 (org-agenda-date-later arg 'hour))
9140 (defun org-agenda-date-earlier-hours (arg)
9141 "Change the time of this item, in hour steps."
9142 (interactive "p")
9143 (org-agenda-date-earlier arg 'hour))
9145 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9146 "Show new date stamp via text properties."
9147 ;; We use text properties to make this undoable
9148 (let ((inhibit-read-only t))
9149 (setq stamp (concat prefix " => " stamp " "))
9150 (save-excursion
9151 (goto-char (point-max))
9152 (while (not (bobp))
9153 (when (equal marker (org-get-at-bol 'org-marker))
9154 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9155 (org-move-to-column (- (window-width) (length stamp)) t)
9156 (if (featurep 'xemacs)
9157 ;; Use `duplicable' property to trigger undo recording
9158 (let ((ex (make-extent nil nil))
9159 (gl (make-glyph stamp)))
9160 (set-glyph-face gl 'secondary-selection)
9161 (set-extent-properties
9162 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9163 (insert-extent ex (1- (point)) (point-at-eol)))
9164 (add-text-properties
9165 (1- (point)) (point-at-eol)
9166 (list 'display (org-add-props stamp nil
9167 'face '(secondary-selection default)))))
9168 (beginning-of-line 1))
9169 (beginning-of-line 0)))))
9171 (defun org-agenda-date-prompt (arg)
9172 "Change the date of this item. Date is prompted for, with default today.
9173 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9174 be used to request time specification in the time stamp."
9175 (interactive "P")
9176 (org-agenda-check-type t 'agenda 'timeline)
9177 (org-agenda-check-no-diary)
9178 (let* ((marker (or (org-get-at-bol 'org-marker)
9179 (org-agenda-error)))
9180 (buffer (marker-buffer marker))
9181 (pos (marker-position marker)))
9182 (org-with-remote-undo buffer
9183 (with-current-buffer buffer
9184 (widen)
9185 (goto-char pos)
9186 (if (not (org-at-timestamp-p t))
9187 (error "Cannot find time stamp"))
9188 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9189 (org-agenda-show-new-time marker org-last-changed-timestamp))
9190 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9192 (defun org-agenda-schedule (arg &optional time)
9193 "Schedule the item at point.
9194 ARG is passed through to `org-schedule'."
9195 (interactive "P")
9196 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9197 (org-agenda-check-no-diary)
9198 (let* ((marker (or (org-get-at-bol 'org-marker)
9199 (org-agenda-error)))
9200 (type (marker-insertion-type marker))
9201 (buffer (marker-buffer marker))
9202 (pos (marker-position marker))
9203 (org-insert-labeled-timestamps-at-point nil)
9205 (set-marker-insertion-type marker t)
9206 (org-with-remote-undo buffer
9207 (with-current-buffer buffer
9208 (widen)
9209 (goto-char pos)
9210 (setq ts (org-schedule arg time)))
9211 (org-agenda-show-new-time marker ts " S"))
9212 (message "%s" ts)))
9214 (defun org-agenda-deadline (arg &optional time)
9215 "Schedule the item at point.
9216 ARG is passed through to `org-deadline'."
9217 (interactive "P")
9218 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9219 (org-agenda-check-no-diary)
9220 (let* ((marker (or (org-get-at-bol 'org-marker)
9221 (org-agenda-error)))
9222 (buffer (marker-buffer marker))
9223 (pos (marker-position marker))
9224 (org-insert-labeled-timestamps-at-point nil)
9226 (org-with-remote-undo buffer
9227 (with-current-buffer buffer
9228 (widen)
9229 (goto-char pos)
9230 (setq ts (org-deadline arg time)))
9231 (org-agenda-show-new-time marker ts " D"))
9232 (message "%s" ts)))
9234 (defun org-agenda-clock-in (&optional arg)
9235 "Start the clock on the currently selected item."
9236 (interactive "P")
9237 (org-agenda-check-no-diary)
9238 (if (equal arg '(4))
9239 (org-clock-in arg)
9240 (let* ((marker (or (org-get-at-bol 'org-marker)
9241 (org-agenda-error)))
9242 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9243 (pos (marker-position marker))
9244 (col (current-column))
9245 newhead)
9246 (org-with-remote-undo (marker-buffer marker)
9247 (with-current-buffer (marker-buffer marker)
9248 (widen)
9249 (goto-char pos)
9250 (org-show-context 'agenda)
9251 (org-show-entry)
9252 (org-cycle-hide-drawers 'children)
9253 (org-clock-in arg)
9254 (setq newhead (org-get-heading)))
9255 (org-agenda-change-all-lines newhead hdmarker))
9256 (org-move-to-column col))))
9258 (defun org-agenda-clock-out ()
9259 "Stop the currently running clock."
9260 (interactive)
9261 (unless (marker-buffer org-clock-marker)
9262 (error "No running clock"))
9263 (let ((marker (make-marker)) (col (current-column)) newhead)
9264 (org-with-remote-undo (marker-buffer org-clock-marker)
9265 (with-current-buffer (marker-buffer org-clock-marker)
9266 (save-excursion
9267 (save-restriction
9268 (widen)
9269 (goto-char org-clock-marker)
9270 (org-back-to-heading t)
9271 (move-marker marker (point))
9272 (org-clock-out)
9273 (setq newhead (org-get-heading))))))
9274 (org-agenda-change-all-lines newhead marker)
9275 (move-marker marker nil)
9276 (org-move-to-column col)
9277 (org-agenda-unmark-clocking-task)))
9279 (defun org-agenda-clock-cancel (&optional arg)
9280 "Cancel the currently running clock."
9281 (interactive "P")
9282 (unless (marker-buffer org-clock-marker)
9283 (user-error "No running clock"))
9284 (org-with-remote-undo (marker-buffer org-clock-marker)
9285 (org-clock-cancel)))
9287 (defun org-agenda-clock-goto ()
9288 "Jump to the currently clocked in task within the agenda.
9289 If the currently clocked in task is not listed in the agenda
9290 buffer, display it in another window."
9291 (interactive)
9292 (let (pos)
9293 (mapc (lambda (o)
9294 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9295 (setq pos (overlay-start o))))
9296 (overlays-in (point-min) (point-max)))
9297 (cond (pos (goto-char pos))
9298 ;; If the currently clocked entry is not in the agenda
9299 ;; buffer, we visit it in another window:
9300 (org-clock-current-task
9301 (org-switch-to-buffer-other-window (org-clock-goto)))
9302 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9304 (defun org-agenda-diary-entry-in-org-file ()
9305 "Make a diary entry in the file `org-agenda-diary-file'."
9306 (let (d1 d2 char (text "") dp1 dp2)
9307 (if (equal (buffer-name) "*Calendar*")
9308 (setq d1 (calendar-cursor-to-date t)
9309 d2 (car calendar-mark-ring))
9310 (setq dp1 (get-text-property (point-at-bol) 'day))
9311 (unless dp1 (user-error "No date defined in current line"))
9312 (setq d1 (calendar-gregorian-from-absolute dp1)
9313 d2 (and (ignore-errors (mark))
9314 (save-excursion
9315 (goto-char (mark))
9316 (setq dp2 (get-text-property (point-at-bol) 'day)))
9317 (calendar-gregorian-from-absolute dp2))))
9318 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9319 (setq char (read-char-exclusive))
9320 (cond
9321 ((equal char ?d)
9322 (setq text (read-string "Day entry: "))
9323 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9324 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9325 ((equal char ?a)
9326 (setq d1 (list (car d1) (nth 1 d1)
9327 (read-number (format "Reference year [%d]: " (nth 2 d1))
9328 (nth 2 d1))))
9329 (setq text (read-string "Anniversary (use %d to show years): "))
9330 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9331 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9332 ((equal char ?b)
9333 (setq text (read-string "Block entry: "))
9334 (unless (and d1 d2 (not (equal d1 d2)))
9335 (user-error "No block of days selected"))
9336 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9337 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9338 ((equal char ?j)
9339 (org-switch-to-buffer-other-window
9340 (find-file-noselect org-agenda-diary-file))
9341 (require 'org-datetree)
9342 (org-datetree-find-date-create d1)
9343 (org-reveal t))
9344 (t (user-error "Invalid selection character `%c'" char)))))
9346 (defcustom org-agenda-insert-diary-strategy 'date-tree
9347 "Where in `org-agenda-diary-file' should new entries be added?
9348 Valid values:
9350 date-tree in the date tree, as child of the date
9351 top-level as top-level entries at the end of the file."
9352 :group 'org-agenda
9353 :type '(choice
9354 (const :tag "in a date tree" date-tree)
9355 (const :tag "as top level at end of file" top-level)))
9357 (defcustom org-agenda-insert-diary-extract-time nil
9358 "Non-nil means extract any time specification from the diary entry."
9359 :group 'org-agenda
9360 :version "24.1"
9361 :type 'boolean)
9363 (defcustom org-agenda-bulk-mark-char ">"
9364 "A single-character string to be used as the bulk mark."
9365 :group 'org-agenda
9366 :version "24.1"
9367 :type 'string)
9369 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9370 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9371 If TEXT is not empty, it will become the headline of the new entry, and
9372 the resulting entry will not be shown. When TEXT is empty, switch to
9373 `org-agenda-diary-file' and let the user finish the entry there."
9374 (let ((cw (current-window-configuration)))
9375 (org-switch-to-buffer-other-window
9376 (find-file-noselect org-agenda-diary-file))
9377 (widen)
9378 (goto-char (point-min))
9379 (cond
9380 ((eq type 'anniversary)
9381 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9382 (progn
9383 (or (org-at-heading-p t)
9384 (progn
9385 (outline-next-heading)
9386 (insert "* Anniversaries\n\n")
9387 (beginning-of-line -1)))))
9388 (outline-next-heading)
9389 (org-back-over-empty-lines)
9390 (backward-char 1)
9391 (insert "\n")
9392 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9393 (nth 2 d1) (car d1) (nth 1 d1) text)))
9394 ((eq type 'day)
9395 (let ((org-prefix-has-time t)
9396 (org-agenda-time-leading-zero t)
9397 fmt time time2)
9398 (if org-agenda-insert-diary-extract-time
9399 ;; Use org-agenda-format-item to parse text for a time-range and
9400 ;; remove it. FIXME: This is a hack, we should refactor
9401 ;; that function to make time extraction available separately
9402 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9403 time (get-text-property 0 'time fmt)
9404 time2 (if (> (length time) 0)
9405 ;; split-string removes trailing ...... if
9406 ;; no end time given. First space
9407 ;; separates time from date.
9408 (concat " " (car (split-string time "\\.")))
9409 nil)
9410 text (get-text-property 0 'txt fmt)))
9411 (if (eq org-agenda-insert-diary-strategy 'top-level)
9412 (org-agenda-insert-diary-as-top-level text)
9413 (require 'org-datetree)
9414 (org-datetree-find-date-create d1)
9415 (org-agenda-insert-diary-make-new-entry text))
9416 (org-insert-time-stamp (org-time-from-absolute
9417 (calendar-absolute-from-gregorian d1))
9418 nil nil nil nil time2))
9419 (end-of-line 0))
9420 ((eq type 'block)
9421 (if (> (calendar-absolute-from-gregorian d1)
9422 (calendar-absolute-from-gregorian d2))
9423 (setq d1 (prog1 d2 (setq d2 d1))))
9424 (if (eq org-agenda-insert-diary-strategy 'top-level)
9425 (org-agenda-insert-diary-as-top-level text)
9426 (require 'org-datetree)
9427 (org-datetree-find-date-create d1)
9428 (org-agenda-insert-diary-make-new-entry text))
9429 (org-insert-time-stamp (org-time-from-absolute
9430 (calendar-absolute-from-gregorian d1)))
9431 (insert "--")
9432 (org-insert-time-stamp (org-time-from-absolute
9433 (calendar-absolute-from-gregorian d2)))
9434 (end-of-line 0)))
9435 (if (string-match "\\S-" text)
9436 (progn
9437 (set-window-configuration cw)
9438 (message "%s entry added to %s"
9439 (capitalize (symbol-name type))
9440 (abbreviate-file-name org-agenda-diary-file)))
9441 (org-reveal t)
9442 (message "Please finish entry here"))))
9444 (defun org-agenda-insert-diary-as-top-level (text)
9445 "Make new entry as a top-level entry at the end of the file.
9446 Add TEXT as headline, and position the cursor in the second line so that
9447 a timestamp can be added there."
9448 (widen)
9449 (goto-char (point-max))
9450 (or (bolp) (insert "\n"))
9451 (insert "* " text "\n")
9452 (if org-adapt-indentation (org-indent-to-column 2)))
9454 (defun org-agenda-insert-diary-make-new-entry (text)
9455 "Make a new entry with TEXT as the first child of the current subtree.
9456 Position the point in the line right after the new heading so
9457 that a timestamp can be added there."
9458 (let ((org-show-following-heading t)
9459 (org-show-siblings t)
9460 (org-show-hierarchy-above t)
9461 (org-show-entry-below t)
9462 col)
9463 (outline-next-heading)
9464 (org-back-over-empty-lines)
9465 (or (looking-at "[ \t]*$")
9466 (progn (insert "\n") (backward-char 1)))
9467 (org-insert-heading nil t)
9468 (org-do-demote)
9469 (setq col (current-column))
9470 (insert text "\n")
9471 (if org-adapt-indentation (org-indent-to-column col))
9472 (let ((org-show-following-heading t)
9473 (org-show-siblings t)
9474 (org-show-hierarchy-above t)
9475 (org-show-entry-below t))
9476 (org-show-context))))
9478 (defun org-agenda-diary-entry ()
9479 "Make a diary entry, like the `i' command from the calendar.
9480 All the standard commands work: block, weekly etc.
9481 When `org-agenda-diary-file' points to a file,
9482 `org-agenda-diary-entry-in-org-file' is called instead to create
9483 entries in that Org-mode file."
9484 (interactive)
9485 (if (not (eq org-agenda-diary-file 'diary-file))
9486 (org-agenda-diary-entry-in-org-file)
9487 (require 'diary-lib)
9488 (let* ((char (progn
9489 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9490 (read-char-exclusive)))
9491 (cmd (cdr (assoc char
9492 '((?d . insert-diary-entry)
9493 (?w . insert-weekly-diary-entry)
9494 (?m . insert-monthly-diary-entry)
9495 (?y . insert-yearly-diary-entry)
9496 (?a . insert-anniversary-diary-entry)
9497 (?b . insert-block-diary-entry)
9498 (?c . insert-cyclic-diary-entry)))))
9499 (oldf (symbol-function 'calendar-cursor-to-date))
9500 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9501 (point (point))
9502 (mark (or (mark t) (point))))
9503 (unless cmd
9504 (user-error "No command associated with <%c>" char))
9505 (unless (and (get-text-property point 'day)
9506 (or (not (equal ?b char))
9507 (get-text-property mark 'day)))
9508 (user-error "Don't know which date to use for diary entry"))
9509 ;; We implement this by hacking the `calendar-cursor-to-date' function
9510 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9511 (let ((calendar-mark-ring
9512 (list (calendar-gregorian-from-absolute
9513 (or (get-text-property mark 'day)
9514 (get-text-property point 'day))))))
9515 (unwind-protect
9516 (progn
9517 (fset 'calendar-cursor-to-date
9518 (lambda (&optional error dummy)
9519 (calendar-gregorian-from-absolute
9520 (get-text-property point 'day))))
9521 (call-interactively cmd))
9522 (fset 'calendar-cursor-to-date oldf))))))
9524 (defun org-agenda-execute-calendar-command (cmd)
9525 "Execute a calendar command from the agenda with date from cursor."
9526 (org-agenda-check-type t 'agenda 'timeline)
9527 (require 'diary-lib)
9528 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9529 (user-error "Don't know which date to use for the calendar command"))
9530 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9531 (point (point))
9532 (date (calendar-gregorian-from-absolute
9533 (get-text-property point 'day)))
9534 ;; the following 2 vars are needed in the calendar
9535 (displayed-month (car date))
9536 (displayed-year (nth 2 date)))
9537 (unwind-protect
9538 (progn
9539 (fset 'calendar-cursor-to-date
9540 (lambda (&optional error dummy)
9541 (calendar-gregorian-from-absolute
9542 (get-text-property point 'day))))
9543 (call-interactively cmd))
9544 (fset 'calendar-cursor-to-date oldf))))
9546 (defun org-agenda-phases-of-moon ()
9547 "Display the phases of the moon for the 3 months around the cursor date."
9548 (interactive)
9549 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9551 (defun org-agenda-holidays ()
9552 "Display the holidays for the 3 months around the cursor date."
9553 (interactive)
9554 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9556 (defvar calendar-longitude) ; defined in calendar.el
9557 (defvar calendar-latitude) ; defined in calendar.el
9558 (defvar calendar-location-name) ; defined in calendar.el
9560 (defun org-agenda-sunrise-sunset (arg)
9561 "Display sunrise and sunset for the cursor date.
9562 Latitude and longitude can be specified with the variables
9563 `calendar-latitude' and `calendar-longitude'. When called with prefix
9564 argument, latitude and longitude will be prompted for."
9565 (interactive "P")
9566 (require 'solar)
9567 (let ((calendar-longitude (if arg nil calendar-longitude))
9568 (calendar-latitude (if arg nil calendar-latitude))
9569 (calendar-location-name
9570 (if arg "the given coordinates" calendar-location-name)))
9571 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9573 (defun org-agenda-goto-calendar ()
9574 "Open the Emacs calendar with the date at the cursor."
9575 (interactive)
9576 (org-agenda-check-type t 'agenda 'timeline)
9577 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9578 (user-error "Don't know which date to open in calendar")))
9579 (date (calendar-gregorian-from-absolute day))
9580 (calendar-move-hook nil)
9581 (calendar-view-holidays-initially-flag nil)
9582 (calendar-view-diary-initially-flag nil))
9583 (calendar)
9584 (calendar-goto-date date)))
9586 ;;;###autoload
9587 (defun org-calendar-goto-agenda ()
9588 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9589 This is a command that has to be installed in `calendar-mode-map'."
9590 (interactive)
9591 (org-agenda-list nil (calendar-absolute-from-gregorian
9592 (calendar-cursor-to-date))
9593 nil))
9595 (defun org-agenda-convert-date ()
9596 (interactive)
9597 (org-agenda-check-type t 'agenda 'timeline)
9598 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9599 date s)
9600 (unless day
9601 (user-error "Don't know which date to convert"))
9602 (setq date (calendar-gregorian-from-absolute day))
9603 (setq s (concat
9604 "Gregorian: " (calendar-date-string date) "\n"
9605 "ISO: " (calendar-iso-date-string date) "\n"
9606 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9607 "Julian: " (calendar-julian-date-string date) "\n"
9608 "Astron. JD: " (calendar-astro-date-string date)
9609 " (Julian date number at noon UTC)\n"
9610 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9611 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9612 "French: " (calendar-french-date-string date) "\n"
9613 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9614 "Mayan: " (calendar-mayan-date-string date) "\n"
9615 "Coptic: " (calendar-coptic-date-string date) "\n"
9616 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9617 "Persian: " (calendar-persian-date-string date) "\n"
9618 "Chinese: " (calendar-chinese-date-string date) "\n"))
9619 (with-output-to-temp-buffer "*Dates*"
9620 (princ s))
9621 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9623 ;;; Bulk commands
9625 (defun org-agenda-bulk-marked-p ()
9626 (eq (get-char-property (point-at-bol) 'type)
9627 'org-marked-entry-overlay))
9629 (defun org-agenda-bulk-mark (&optional arg)
9630 "Mark the entry at point for future bulk action."
9631 (interactive "p")
9632 (dotimes (i (or arg 1))
9633 (unless (org-get-at-bol 'org-agenda-diary-link)
9634 (let* ((m (org-get-at-bol 'org-hd-marker))
9636 (unless (org-agenda-bulk-marked-p)
9637 (unless m (user-error "Nothing to mark at point"))
9638 (push m org-agenda-bulk-marked-entries)
9639 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9640 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9641 (org-get-todo-face "TODO")
9642 'evaporate)
9643 (overlay-put ov 'type 'org-marked-entry-overlay))
9644 (end-of-line 1)
9645 (or (ignore-errors
9646 (goto-char (next-single-property-change (point) 'txt)))
9647 (beginning-of-line 2))
9648 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9649 (beginning-of-line 2))
9650 (message "%d entries marked for bulk action"
9651 (length org-agenda-bulk-marked-entries))))))
9653 (defun org-agenda-bulk-mark-all ()
9654 "Mark all entries for future agenda bulk action."
9655 (interactive)
9656 (org-agenda-bulk-mark-regexp "."))
9658 (defun org-agenda-bulk-mark-regexp (regexp)
9659 "Mark entries matching REGEXP for future agenda bulk action."
9660 (interactive "sMark entries matching regexp: ")
9661 (let ((entries-marked 0) txt-at-point)
9662 (save-excursion
9663 (goto-char (point-min))
9664 (goto-char (next-single-property-change (point) 'txt))
9665 (while (and (re-search-forward regexp nil t)
9666 (setq txt-at-point (get-text-property (point) 'txt)))
9667 (when (string-match regexp txt-at-point)
9668 (setq entries-marked (1+ entries-marked))
9669 (call-interactively 'org-agenda-bulk-mark))))
9670 (if (not entries-marked)
9671 (message "No entry matching this regexp."))))
9673 (defun org-agenda-bulk-unmark (&optional arg)
9674 "Unmark the entry at point for future bulk action."
9675 (interactive "P")
9676 (if arg
9677 (org-agenda-bulk-unmark-all)
9678 (cond ((org-agenda-bulk-marked-p)
9679 (org-agenda-bulk-remove-overlays
9680 (point-at-bol) (+ 2 (point-at-bol)))
9681 (setq org-agenda-bulk-marked-entries
9682 (delete (org-get-at-bol 'org-hd-marker)
9683 org-agenda-bulk-marked-entries))
9684 (end-of-line 1)
9685 (or (ignore-errors
9686 (goto-char (next-single-property-change (point) 'txt)))
9687 (beginning-of-line 2))
9688 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9689 (beginning-of-line 2))
9690 (message "%d entries left marked for bulk action"
9691 (length org-agenda-bulk-marked-entries)))
9692 (t (message "No entry to unmark here")))))
9694 (defun org-agenda-bulk-toggle-all ()
9695 "Toggle all marks for bulk action."
9696 (interactive)
9697 (save-excursion
9698 (goto-char (point-min))
9699 (while (ignore-errors
9700 (goto-char (next-single-property-change (point) 'txt)))
9701 (org-agenda-bulk-toggle))))
9703 (defun org-agenda-bulk-toggle ()
9704 "Toggle the mark at point for bulk action."
9705 (interactive)
9706 (if (org-agenda-bulk-marked-p)
9707 (org-agenda-bulk-unmark)
9708 (org-agenda-bulk-mark)))
9710 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9711 "Remove the mark overlays between BEG and END in the agenda buffer.
9712 BEG and END default to the buffer limits.
9714 This only removes the overlays, it does not remove the markers
9715 from the list in `org-agenda-bulk-marked-entries'."
9716 (interactive)
9717 (mapc (lambda (ov)
9718 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9719 (delete-overlay ov)))
9720 (overlays-in (or beg (point-min)) (or end (point-max)))))
9722 (defun org-agenda-bulk-unmark-all ()
9723 "Remove all marks in the agenda buffer.
9724 This will remove the markers and the overlays."
9725 (interactive)
9726 (if (null org-agenda-bulk-marked-entries)
9727 (message "No entry to unmark")
9728 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9729 (setq org-agenda-bulk-marked-entries nil)
9730 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9732 (defcustom org-agenda-persistent-marks nil
9733 "Non-nil means marked items will stay marked after a bulk action.
9734 You can toggle this interactively by typing `p' when prompted for a
9735 bulk action."
9736 :group 'org-agenda
9737 :version "24.1"
9738 :type 'boolean)
9740 (defun org-agenda-bulk-action (&optional arg)
9741 "Execute an remote-editing action on all marked entries.
9742 The prefix arg is passed through to the command if possible."
9743 (interactive "P")
9744 ;; Make sure we have markers, and only valid ones
9745 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9746 (mapc
9747 (lambda (m)
9748 (unless (and (markerp m)
9749 (marker-buffer m)
9750 (buffer-live-p (marker-buffer m))
9751 (marker-position m))
9752 (user-error "Marker %s for bulk command is invalid" m)))
9753 org-agenda-bulk-marked-entries)
9755 ;; Prompt for the bulk command
9756 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9757 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9758 "[S]catter [f]unction "
9759 (when org-agenda-bulk-custom-functions
9760 (concat " Custom: ["
9761 (mapconcat (lambda(f) (char-to-string (car f)))
9762 org-agenda-bulk-custom-functions "")
9763 "]"))))
9764 (catch 'exit
9765 (let* ((action (read-char-exclusive))
9766 (org-log-refile (if org-log-refile 'time nil))
9767 (entries (reverse org-agenda-bulk-marked-entries))
9768 (org-overriding-default-time
9769 (if (get-text-property (point) 'org-agenda-date-header)
9770 (org-get-cursor-date)))
9771 redo-at-end
9772 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9773 (cond
9774 ((equal action ?p)
9775 (let ((org-agenda-persistent-marks
9776 (not org-agenda-persistent-marks)))
9777 (org-agenda-bulk-action)
9778 (throw 'exit nil)))
9780 ((equal action ?$)
9781 (setq cmd '(org-agenda-archive)))
9783 ((equal action ?A)
9784 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9786 ((member action '(?r ?w))
9787 (setq rfloc (org-refile-get-location
9788 "Refile to"
9789 (marker-buffer (car entries))
9790 org-refile-allow-creating-parent-nodes))
9791 (if (nth 3 rfloc)
9792 (setcar (nthcdr 3 rfloc)
9793 (move-marker (make-marker) (nth 3 rfloc)
9794 (or (get-file-buffer (nth 1 rfloc))
9795 (find-buffer-visiting (nth 1 rfloc))
9796 (error "This should not happen")))))
9798 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9799 redo-at-end t))
9801 ((equal action ?t)
9802 (setq state (org-icompleting-read
9803 "Todo state: "
9804 (with-current-buffer (marker-buffer (car entries))
9805 (mapcar 'list org-todo-keywords-1))))
9806 (setq cmd `(let ((org-inhibit-blocking t)
9807 (org-inhibit-logging 'note))
9808 (org-agenda-todo ,state))))
9810 ((memq action '(?- ?+))
9811 (setq tag (org-icompleting-read
9812 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9813 (with-current-buffer (marker-buffer (car entries))
9814 (delq nil
9815 (mapcar (lambda (x)
9816 (if (stringp (car x)) x)) org-tag-alist)))))
9817 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9819 ((memq action '(?s ?d))
9820 (let* ((time
9821 (unless arg
9822 (org-read-date
9823 nil nil nil
9824 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9825 org-overriding-default-time)))
9826 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9827 (setq cmd `(eval '(,c1 arg ,time)))))
9829 ((equal action ?S)
9830 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9831 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9832 (let ((days (read-number
9833 (format "Scatter tasks across how many %sdays: "
9834 (if arg "week" "")) 7)))
9835 (setq cmd
9836 `(let ((distance (1+ (random ,days))))
9837 (if arg
9838 (let ((dist distance)
9839 (day-of-week
9840 (calendar-day-of-week
9841 (calendar-gregorian-from-absolute (org-today)))))
9842 (dotimes (i (1+ dist))
9843 (while (member day-of-week org-agenda-weekend-days)
9844 (incf distance)
9845 (incf day-of-week)
9846 (if (= day-of-week 7)
9847 (setq day-of-week 0)))
9848 (incf day-of-week)
9849 (if (= day-of-week 7)
9850 (setq day-of-week 0)))))
9851 ;; silently fail when try to replan a sexp entry
9852 (condition-case nil
9853 (let* ((date (calendar-gregorian-from-absolute
9854 (+ (org-today) distance)))
9855 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9856 (nth 2 date))))
9857 (org-agenda-schedule nil time))
9858 (error nil)))))))
9860 ((assoc action org-agenda-bulk-custom-functions)
9861 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9862 redo-at-end t))
9864 ((equal action ?f)
9865 (setq cmd (list (intern
9866 (org-icompleting-read "Function: "
9867 obarray 'fboundp t nil nil)))))
9869 (t (user-error "Invalid bulk action")))
9871 ;; Sort the markers, to make sure that parents are handled before children
9872 (setq entries (sort entries
9873 (lambda (a b)
9874 (cond
9875 ((equal (marker-buffer a) (marker-buffer b))
9876 (< (marker-position a) (marker-position b)))
9878 (string< (buffer-name (marker-buffer a))
9879 (buffer-name (marker-buffer b))))))))
9881 ;; Now loop over all markers and apply cmd
9882 (while (setq e (pop entries))
9883 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9884 (if (not pos)
9885 (progn (message "Skipping removed entry at %s" e)
9886 (setq cntskip (1+ cntskip)))
9887 (goto-char pos)
9888 (let (org-loop-over-headlines-in-active-region)
9889 (eval cmd))
9890 (setq cnt (1+ cnt))))
9891 (when redo-at-end (org-agenda-redo))
9892 (unless org-agenda-persistent-marks
9893 (org-agenda-bulk-unmark-all))
9894 (message "Acted on %d entries%s%s"
9896 (if (= cntskip 0)
9898 (format ", skipped %d (disappeared before their turn)"
9899 cntskip))
9900 (if (not org-agenda-persistent-marks)
9901 "" " (kept marked)"))))))
9903 (defun org-agenda-capture (&optional with-time)
9904 "Call `org-capture' with the date at point.
9905 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9906 current HH:MM time."
9907 (interactive "P")
9908 (if (not (eq major-mode 'org-agenda-mode))
9909 (user-error "You cannot do this outside of agenda buffers")
9910 (let ((org-overriding-default-time
9911 (org-get-cursor-date (equal with-time 1))))
9912 (call-interactively 'org-capture))))
9914 ;;; Dragging agenda lines forward/backward
9916 (defun org-agenda-reapply-filters ()
9917 "Re-apply all agenda filters."
9918 (mapcar
9919 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9920 `((,org-agenda-tag-filter tag)
9921 (,org-agenda-category-filter category)
9922 (,org-agenda-regexp-filter regexp)
9923 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9924 (,(get 'org-agenda-category-filter :preset-filter) category)
9925 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9927 (defun org-agenda-drag-line-forward (arg &optional backward)
9928 "Drag an agenda line forward by ARG lines.
9929 When the optional argument `backward' is non-nil, move backward."
9930 (interactive "p")
9931 (let ((inhibit-read-only t) lst line)
9932 (if (or (not (get-text-property (point) 'txt))
9933 (save-excursion
9934 (dotimes (n arg)
9935 (move-beginning-of-line (if backward 0 2))
9936 (push (not (get-text-property (point) 'txt)) lst))
9937 (delq nil lst)))
9938 (message "Cannot move line forward")
9939 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9940 (move-beginning-of-line 1)
9941 (setq line (buffer-substring (point) end))
9942 (delete-region (point) end)
9943 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9944 (insert line)
9945 (org-agenda-reapply-filters)
9946 (org-agenda-mark-clocking-task)
9947 (move-beginning-of-line 0)))))
9949 (defun org-agenda-drag-line-backward (arg)
9950 "Drag an agenda line backward by ARG lines."
9951 (interactive "p")
9952 (org-agenda-drag-line-forward arg t))
9954 ;;; Flagging notes
9956 (defun org-agenda-show-the-flagging-note ()
9957 "Display the flagging note in the other window.
9958 When called a second time in direct sequence, offer to remove the FLAGGING
9959 tag and (if present) the flagging note."
9960 (interactive)
9961 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9962 (win (selected-window))
9963 note heading newhead)
9964 (unless hdmarker
9965 (user-error "No linked entry at point"))
9966 (if (and (eq this-command last-command)
9967 (y-or-n-p "Unflag and remove any flagging note? "))
9968 (progn
9969 (org-agenda-remove-flag hdmarker)
9970 (let ((win (get-buffer-window "*Flagging Note*")))
9971 (and win (delete-window win)))
9972 (message "Entry unflagged"))
9973 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9974 (unless note
9975 (user-error "No flagging note"))
9976 (org-kill-new note)
9977 (org-switch-to-buffer-other-window "*Flagging Note*")
9978 (erase-buffer)
9979 (insert note)
9980 (goto-char (point-min))
9981 (while (re-search-forward "\\\\n" nil t)
9982 (replace-match "\n" t t))
9983 (goto-char (point-min))
9984 (select-window win)
9985 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9987 (defun org-agenda-remove-flag (marker)
9988 "Remove the FLAGGED tag and any flagging note in the entry."
9989 (let (newhead)
9990 (org-with-point-at marker
9991 (org-toggle-tag "FLAGGED" 'off)
9992 (org-entry-delete nil "THEFLAGGINGNOTE")
9993 (setq newhead (org-get-heading)))
9994 (org-agenda-change-all-lines newhead marker)
9995 (message "Entry unflagged")))
9997 (defun org-agenda-get-any-marker (&optional pos)
9998 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9999 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10001 ;;; Appointment reminders
10003 (defvar appt-time-msg-list) ; defined in appt.el
10005 ;;;###autoload
10006 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10007 "Activate appointments found in `org-agenda-files'.
10008 With a \\[universal-argument] prefix, refresh the list of
10009 appointments.
10011 If FILTER is t, interactively prompt the user for a regular
10012 expression, and filter out entries that don't match it.
10014 If FILTER is a string, use this string as a regular expression
10015 for filtering entries out.
10017 If FILTER is a function, filter out entries against which
10018 calling the function returns nil. This function takes one
10019 argument: an entry from `org-agenda-get-day-entries'.
10021 FILTER can also be an alist with the car of each cell being
10022 either 'headline or 'category. For example:
10024 '((headline \"IMPORTANT\")
10025 (category \"Work\"))
10027 will only add headlines containing IMPORTANT or headlines
10028 belonging to the \"Work\" category.
10030 ARGS are symbols indicating what kind of entries to consider.
10031 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10032 \(i.e., deadlines and scheduled items with a hh:mm specification)
10033 and :timestamp entries. See the docstring of `org-diary' for
10034 details and examples.
10036 If an entry has a APPT_WARNTIME property, its value will be used
10037 to override `appt-message-warning-time'."
10038 (interactive "P")
10039 (if refresh (setq appt-time-msg-list nil))
10040 (if (eq filter t)
10041 (setq filter (read-from-minibuffer "Regexp filter: ")))
10042 (let* ((cnt 0) ; count added events
10043 (scope (or args '(:deadline* :scheduled* :timestamp)))
10044 (org-agenda-new-buffers nil)
10045 (org-deadline-warning-days 0)
10046 ;; Do not use `org-today' here because appt only takes
10047 ;; time and without date as argument, so it may pass wrong
10048 ;; information otherwise
10049 (today (org-date-to-gregorian
10050 (time-to-days (current-time))))
10051 (org-agenda-restrict nil)
10052 (files (org-agenda-files 'unrestricted)) entries file
10053 (org-agenda-buffer nil))
10054 ;; Get all entries which may contain an appt
10055 (org-agenda-prepare-buffers files)
10056 (while (setq file (pop files))
10057 (setq entries
10058 (delq nil
10059 (append entries
10060 (apply 'org-agenda-get-day-entries
10061 file today scope)))))
10062 ;; Map thru entries and find if we should filter them out
10063 (mapc
10064 (lambda(x)
10065 (let* ((evt (org-trim
10066 (replace-regexp-in-string
10067 org-bracket-link-regexp "\\3"
10068 (or (get-text-property 1 'txt x) ""))))
10069 (cat (get-text-property 1 'org-category x))
10070 (tod (get-text-property 1 'time-of-day x))
10071 (ok (or (null filter)
10072 (and (stringp filter) (string-match filter evt))
10073 (and (functionp filter) (funcall filter x))
10074 (and (listp filter)
10075 (let ((cat-filter (cadr (assoc 'category filter)))
10076 (evt-filter (cadr (assoc 'headline filter))))
10077 (or (and (stringp cat-filter)
10078 (string-match cat-filter cat))
10079 (and (stringp evt-filter)
10080 (string-match evt-filter evt)))))))
10081 (wrn (get-text-property 1 'warntime x)))
10082 ;; FIXME: Shall we remove text-properties for the appt text?
10083 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10084 (when (and ok tod)
10085 (setq tod (concat "00" (number-to-string tod))
10086 tod (when (string-match
10087 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10088 (concat (match-string 1 tod) ":"
10089 (match-string 2 tod))))
10090 (if (version< emacs-version "23.3")
10091 (appt-add tod evt)
10092 (appt-add tod evt wrn))
10093 (setq cnt (1+ cnt))))) entries)
10094 (org-release-buffers org-agenda-new-buffers)
10095 (if (eq cnt 0)
10096 (message "No event to add")
10097 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10099 (defun org-agenda-todayp (date)
10100 "Does DATE mean today, when considering `org-extend-today-until'?"
10101 (let ((today (org-today))
10102 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10103 date)))
10104 (eq date today)))
10106 (defun org-agenda-todo-yesterday (&optional arg)
10107 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10108 (interactive "P")
10109 (let* ((hour (third (decode-time
10110 (org-current-time))))
10111 (org-extend-today-until (1+ hour)))
10112 (org-agenda-todo arg)))
10114 (provide 'org-agenda)
10116 ;;; org-agenda.el ends here