contrib/lisp/ox-extra.el: fix typos/thinkos
[org-mode/org-kjn.git] / lisp / org-agenda.el
blobf24e1f66a636d7aaa07ef416ea369347d3fd89b7
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-columns-remove-overlays "org-colview" ())
73 (declare-function org-datetree-find-date-create "org-datetree"
74 (date &optional keep-restriction))
75 (declare-function org-columns-quit "org-colview" ())
76 (declare-function diary-date-display-form "diary-lib" (&optional type))
77 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
78 (declare-function org-habit-insert-consistency-graphs
79 "org-habit" (&optional line))
80 (declare-function org-is-habit-p "org-habit" (&optional pom))
81 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
82 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
83 (declare-function org-pop-to-buffer-same-window "org-compat"
84 (&optional buffer-or-name norecord label))
85 (declare-function org-agenda-columns "org-colview" ())
86 (declare-function org-add-archive-files "org-archive" (files))
87 (declare-function org-capture "org-capture" (&optional goto keys))
89 (defvar calendar-mode-map) ; defined in calendar.el
90 (defvar org-clock-current-task nil) ; defined in org-clock.el
91 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
92 (defvar org-habit-show-habits) ; defined in org-habit.el
93 (defvar org-habit-show-habits-only-for-today)
94 (defvar org-habit-show-all-today)
96 ;; Defined somewhere in this file, but used before definition.
97 (defvar org-agenda-buffer-name "*Org Agenda*")
98 (defvar org-agenda-overriding-header nil)
99 (defvar org-agenda-title-append nil)
100 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
101 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
102 (defvar original-date) ; dynamically scoped, calendar.el does scope this
104 (defvar org-agenda-undo-list nil
105 "List of undoable operations in the agenda since last refresh.")
106 (defvar org-agenda-pending-undo-list nil
107 "In a series of undo commands, this is the list of remaining undo items.")
109 (defcustom org-agenda-confirm-kill 1
110 "When set, remote killing from the agenda buffer needs confirmation.
111 When t, a confirmation is always needed. When a number N, confirmation is
112 only needed when the text to be killed contains more than N non-white lines."
113 :group 'org-agenda
114 :type '(choice
115 (const :tag "Never" nil)
116 (const :tag "Always" t)
117 (integer :tag "When more than N lines")))
119 (defcustom org-agenda-compact-blocks nil
120 "Non-nil means make the block agenda more compact.
121 This is done globally by leaving out lines like the agenda span
122 name and week number or the separator lines."
123 :group 'org-agenda
124 :type 'boolean)
126 (defcustom org-agenda-block-separator ?=
127 "The separator between blocks in the agenda.
128 If this is a string, it will be used as the separator, with a newline added.
129 If it is a character, it will be repeated to fill the window width.
130 If nil the separator is disabled. In `org-agenda-custom-commands' this
131 addresses the separator between the current and the previous block."
132 :group 'org-agenda
133 :type '(choice
134 (const :tag "Disabled" nil)
135 (character)
136 (string)))
138 (defgroup org-agenda-export nil
139 "Options concerning exporting agenda views in Org-mode."
140 :tag "Org Agenda Export"
141 :group 'org-agenda)
143 (defcustom org-agenda-with-colors t
144 "Non-nil means use colors in agenda views."
145 :group 'org-agenda-export
146 :type 'boolean)
148 (defcustom org-agenda-exporter-settings nil
149 "Alist of variable/value pairs that should be active during agenda export.
150 This is a good place to set options for ps-print and for htmlize.
151 Note that the way this is implemented, the values will be evaluated
152 before assigned to the variables. So make sure to quote values you do
153 *not* want evaluated, for example
155 (setq org-agenda-exporter-settings
156 '((ps-print-color-p 'black-white)))"
157 :group 'org-agenda-export
158 :type '(repeat
159 (list
160 (variable)
161 (sexp :tag "Value"))))
163 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
164 "Hook run in a temporary buffer before writing the agenda to an export file.
165 A useful function for this hook is `org-agenda-add-entry-text'."
166 :group 'org-agenda-export
167 :type 'hook
168 :options '(org-agenda-add-entry-text))
170 (defcustom org-agenda-add-entry-text-maxlines 0
171 "Maximum number of entry text lines to be added to agenda.
172 This is only relevant when `org-agenda-add-entry-text' is part of
173 `org-agenda-before-write-hook', which is the default.
174 When this is 0, nothing will happen. When it is greater than 0, it
175 specifies the maximum number of lines that will be added for each entry
176 that is listed in the agenda view.
178 Note that this variable is not used during display, only when exporting
179 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
180 and `org-agenda-entry-text-maxlines'."
181 :group 'org-agenda
182 :type 'integer)
184 (defcustom org-agenda-add-entry-text-descriptive-links t
185 "Non-nil means export org-links as descriptive links in agenda added text.
186 This variable applies to the text added to the agenda when
187 `org-agenda-add-entry-text-maxlines' is larger than 0.
188 When this variable nil, the URL will (also) be shown."
189 :group 'org-agenda
190 :type 'boolean)
192 (defcustom org-agenda-export-html-style nil
193 "The style specification for exported HTML Agenda files.
194 If this variable contains a string, it will replace the default <style>
195 section as produced by `htmlize'.
196 Since there are different ways of setting style information, this variable
197 needs to contain the full HTML structure to provide a style, including the
198 surrounding HTML tags. The style specifications should include definitions
199 the fonts used by the agenda, here is an example:
201 <style type=\"text/css\">
202 p { font-weight: normal; color: gray; }
203 .org-agenda-structure {
204 font-size: 110%;
205 color: #003399;
206 font-weight: 600;
208 .org-todo {
209 color: #cc6666;
210 font-weight: bold;
212 .org-agenda-done {
213 color: #339933;
215 .org-done {
216 color: #339933;
218 .title { text-align: center; }
219 .todo, .deadline { color: red; }
220 .done { color: green; }
221 </style>
223 or, if you want to keep the style in a file,
225 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
227 As the value of this option simply gets inserted into the HTML <head> header,
228 you can \"misuse\" it to also add other text to the header."
229 :group 'org-agenda-export
230 :group 'org-export-html
231 :type '(choice
232 (const nil)
233 (string)))
235 (defcustom org-agenda-persistent-filter nil
236 "When set, keep filters from one agenda view to the next."
237 :group 'org-agenda
238 :type 'boolean)
240 (defgroup org-agenda-custom-commands nil
241 "Options concerning agenda views in Org-mode."
242 :tag "Org Agenda Custom Commands"
243 :group 'org-agenda)
245 (defconst org-sorting-choice
246 '(choice
247 (const time-up) (const time-down)
248 (const timestamp-up) (const timestamp-down)
249 (const scheduled-up) (const scheduled-down)
250 (const deadline-up) (const deadline-down)
251 (const ts-up) (const ts-down)
252 (const tsia-up) (const tsia-down)
253 (const category-keep) (const category-up) (const category-down)
254 (const tag-down) (const tag-up)
255 (const priority-up) (const priority-down)
256 (const todo-state-up) (const todo-state-down)
257 (const effort-up) (const effort-down)
258 (const habit-up) (const habit-down)
259 (const alpha-up) (const alpha-down)
260 (const user-defined-up) (const user-defined-down))
261 "Sorting choices.")
263 ;; Keep custom values for `org-agenda-filter-preset' compatible with
264 ;; the new variable `org-agenda-tag-filter-preset'.
265 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
266 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
268 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
269 "List of types searched for when creating the daily/weekly agenda.
270 This variable is a list of symbols that controls the types of
271 items that appear in the daily/weekly agenda. Allowed symbols in this
272 list are are
274 :timestamp List items containing a date stamp or date range matching
275 the selected date. This includes sexp entries in angular
276 brackets.
278 :sexp List entries resulting from plain diary-like sexps.
280 :deadline List deadline due on that date. When the date is today,
281 also list any deadlines past due, or due within
282 `org-deadline-warning-days'. `:deadline' must appear before
283 `:scheduled' if the setting of
284 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
285 any effect.
287 :deadline* Same as above, but only include the deadline if it has an
288 hour specification as [h]h:mm.
290 :scheduled List all items which are scheduled for the given date.
291 The diary for *today* also contains items which were
292 scheduled earlier and are not yet marked DONE.
294 :scheduled* Same as above, but only include the scheduled item if it
295 has an hour specification as [h]h:mm.
297 By default, all four non-starred types are turned on.
299 When :scheduled* or :deadline* are included, :schedule or :deadline
300 will be ignored.
302 Never set this variable globally using `setq', because then it
303 will apply to all future agenda commands. Instead, bind it with
304 `let' to scope it dynamically into the agenda-constructing
305 command. A good way to set it is through options in
306 `org-agenda-custom-commands'. For a more flexible (though
307 somewhat less efficient) way of determining what is included in
308 the daily/weekly agenda, see `org-agenda-skip-function'.")
310 (defconst org-agenda-custom-commands-local-options
311 `(repeat :tag "Local settings for this command. Remember to quote values"
312 (choice :tag "Setting"
313 (list :tag "Heading for this block"
314 (const org-agenda-overriding-header)
315 (string :tag "Headline"))
316 (list :tag "Files to be searched"
317 (const org-agenda-files)
318 (list
319 (const :format "" quote)
320 (repeat (file))))
321 (list :tag "Sorting strategy"
322 (const org-agenda-sorting-strategy)
323 (list
324 (const :format "" quote)
325 (repeat
326 ,org-sorting-choice)))
327 (list :tag "Prefix format"
328 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
329 (string))
330 (list :tag "Number of days in agenda"
331 (const org-agenda-span)
332 (choice (const :tag "Day" day)
333 (const :tag "Week" week)
334 (const :tag "Fortnight" fortnight)
335 (const :tag "Month" month)
336 (const :tag "Year" year)
337 (integer :tag "Custom")))
338 (list :tag "Fixed starting date"
339 (const org-agenda-start-day)
340 (string :value "2007-11-01"))
341 (list :tag "Start on day of week"
342 (const org-agenda-start-on-weekday)
343 (choice :value 1
344 (const :tag "Today" nil)
345 (integer :tag "Weekday No.")))
346 (list :tag "Include data from diary"
347 (const org-agenda-include-diary)
348 (boolean))
349 (list :tag "Deadline Warning days"
350 (const org-deadline-warning-days)
351 (integer :value 1))
352 (list :tag "Category filter preset"
353 (const org-agenda-category-filter-preset)
354 (list
355 (const :format "" quote)
356 (repeat
357 (string :tag "+category or -category"))))
358 (list :tag "Tags filter preset"
359 (const org-agenda-tag-filter-preset)
360 (list
361 (const :format "" quote)
362 (repeat
363 (string :tag "+tag or -tag"))))
364 (list :tag "Effort filter preset"
365 (const org-agenda-effort-filter-preset)
366 (list
367 (const :format "" quote)
368 (repeat
369 (string :tag "+=10 or -=10 or +<10 or ->10"))))
370 (list :tag "Regexp filter preset"
371 (const org-agenda-regexp-filter-preset)
372 (list
373 (const :format "" quote)
374 (repeat
375 (string :tag "+regexp or -regexp"))))
376 (list :tag "Set daily/weekly entry types"
377 (const org-agenda-entry-types)
378 (list
379 (const :format "" quote)
380 (set :greedy t :value ,org-agenda-entry-types
381 (const :deadline)
382 (const :scheduled)
383 (const :deadline*)
384 (const :scheduled*)
385 (const :timestamp)
386 (const :sexp))))
387 (list :tag "Standard skipping condition"
388 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
389 (const org-agenda-skip-function)
390 (list
391 (const :format "" quote)
392 (list
393 (choice
394 :tag "Skipping range"
395 (const :tag "Skip entry" org-agenda-skip-entry-if)
396 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
397 (repeat :inline t :tag "Conditions for skipping"
398 (choice
399 :tag "Condition type"
400 (list :tag "Regexp matches" :inline t
401 (const :format "" 'regexp)
402 (regexp))
403 (list :tag "Regexp does not match" :inline t
404 (const :format "" 'notregexp)
405 (regexp))
406 (list :tag "TODO state is" :inline t
407 (const 'todo)
408 (choice
409 (const :tag "Any not-done state" 'todo)
410 (const :tag "Any done state" 'done)
411 (const :tag "Any state" 'any)
412 (list :tag "Keyword list"
413 (const :format "" quote)
414 (repeat (string :tag "Keyword")))))
415 (list :tag "TODO state is not" :inline t
416 (const 'nottodo)
417 (choice
418 (const :tag "Any not-done state" 'todo)
419 (const :tag "Any done state" 'done)
420 (const :tag "Any state" 'any)
421 (list :tag "Keyword list"
422 (const :format "" quote)
423 (repeat (string :tag "Keyword")))))
424 (const :tag "scheduled" 'scheduled)
425 (const :tag "not scheduled" 'notscheduled)
426 (const :tag "deadline" 'deadline)
427 (const :tag "no deadline" 'notdeadline)
428 (const :tag "timestamp" 'timestamp)
429 (const :tag "no timestamp" 'nottimestamp))))))
430 (list :tag "Non-standard skipping condition"
431 :value (org-agenda-skip-function)
432 (const org-agenda-skip-function)
433 (sexp :tag "Function or form (quoted!)"))
434 (list :tag "Any variable"
435 (variable :tag "Variable")
436 (sexp :tag "Value (sexp)"))))
437 "Selection of examples for agenda command settings.
438 This will be spliced into the custom type of
439 `org-agenda-custom-commands'.")
442 (defcustom org-agenda-custom-commands
443 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
444 "Custom commands for the agenda.
445 These commands will be offered on the splash screen displayed by the
446 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
448 (key desc type match settings files)
450 key The key (one or more characters as a string) to be associated
451 with the command.
452 desc A description of the command, when omitted or nil, a default
453 description is built using MATCH.
454 type The command type, any of the following symbols:
455 agenda The daily/weekly agenda.
456 todo Entries with a specific TODO keyword, in all agenda files.
457 search Entries containing search words entry or headline.
458 tags Tags/Property/TODO match in all agenda files.
459 tags-todo Tags/P/T match in all agenda files, TODO entries only.
460 todo-tree Sparse tree of specific TODO keyword in *current* file.
461 tags-tree Sparse tree with all tags matches in *current* file.
462 occur-tree Occur sparse tree for *current* file.
463 ... A user-defined function.
464 match What to search for:
465 - a single keyword for TODO keyword searches
466 - a tags match expression for tags searches
467 - a word search expression for text searches.
468 - a regular expression for occur searches
469 For all other commands, this should be the empty string.
470 settings A list of option settings, similar to that in a let form, so like
471 this: ((opt1 val1) (opt2 val2) ...). The values will be
472 evaluated at the moment of execution, so quote them when needed.
473 files A list of files file to write the produced agenda buffer to
474 with the command `org-store-agenda-views'.
475 If a file name ends in \".html\", an HTML version of the buffer
476 is written out. If it ends in \".ps\", a postscript version is
477 produced. Otherwise, only the plain text is written to the file.
479 You can also define a set of commands, to create a composite agenda buffer.
480 In this case, an entry looks like this:
482 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
484 where
486 desc A description string to be displayed in the dispatcher menu.
487 cmd An agenda command, similar to the above. However, tree commands
488 are not allowed, but instead you can get agenda and global todo list.
489 So valid commands for a set are:
490 (agenda \"\" settings)
491 (alltodo \"\" settings)
492 (stuck \"\" settings)
493 (todo \"match\" settings files)
494 (search \"match\" settings files)
495 (tags \"match\" settings files)
496 (tags-todo \"match\" settings files)
498 Each command can carry a list of options, and another set of options can be
499 given for the whole set of commands. Individual command options take
500 precedence over the general options.
502 When using several characters as key to a command, the first characters
503 are prefix commands. For the dispatcher to display useful information, you
504 should provide a description for the prefix, like
506 (setq org-agenda-custom-commands
507 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
508 (\"hl\" tags \"+HOME+Lisa\")
509 (\"hp\" tags \"+HOME+Peter\")
510 (\"hk\" tags \"+HOME+Kim\")))"
511 :group 'org-agenda-custom-commands
512 :type `(repeat
513 (choice :value ("x" "Describe command here" tags "" nil)
514 (list :tag "Single command"
515 (string :tag "Access Key(s) ")
516 (option (string :tag "Description"))
517 (choice
518 (const :tag "Agenda" agenda)
519 (const :tag "TODO list" alltodo)
520 (const :tag "Search words" search)
521 (const :tag "Stuck projects" stuck)
522 (const :tag "Tags/Property match (all agenda files)" tags)
523 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
524 (const :tag "TODO keyword search (all agenda files)" todo)
525 (const :tag "Tags sparse tree (current buffer)" tags-tree)
526 (const :tag "TODO keyword tree (current buffer)" todo-tree)
527 (const :tag "Occur tree (current buffer)" occur-tree)
528 (sexp :tag "Other, user-defined function"))
529 (string :tag "Match (only for some commands)")
530 ,org-agenda-custom-commands-local-options
531 (option (repeat :tag "Export" (file :tag "Export to"))))
532 (list :tag "Command series, all agenda files"
533 (string :tag "Access Key(s)")
534 (string :tag "Description ")
535 (repeat :tag "Component"
536 (choice
537 (list :tag "Agenda"
538 (const :format "" agenda)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "TODO list (all keywords)"
542 (const :format "" alltodo)
543 (const :tag "" :format "" "")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Search words"
546 (const :format "" search)
547 (string :tag "Match")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "Stuck projects"
550 (const :format "" stuck)
551 (const :tag "" :format "" "")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Tags search"
554 (const :format "" tags)
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)
557 (list :tag "Tags search, TODO entries only"
558 (const :format "" tags-todo)
559 (string :tag "Match")
560 ,org-agenda-custom-commands-local-options)
561 (list :tag "TODO keyword search"
562 (const :format "" todo)
563 (string :tag "Match")
564 ,org-agenda-custom-commands-local-options)
565 (list :tag "Other, user-defined function"
566 (symbol :tag "function")
567 (string :tag "Match")
568 ,org-agenda-custom-commands-local-options)))
570 (repeat :tag "Settings for entire command set"
571 (list (variable :tag "Any variable")
572 (sexp :tag "Value")))
573 (option (repeat :tag "Export" (file :tag "Export to"))))
574 (cons :tag "Prefix key documentation"
575 (string :tag "Access Key(s)")
576 (string :tag "Description ")))))
578 (defcustom org-agenda-query-register ?o
579 "The register holding the current query string.
580 The purpose of this is that if you construct a query string interactively,
581 you can then use it to define a custom command."
582 :group 'org-agenda-custom-commands
583 :type 'character)
585 (defcustom org-stuck-projects
586 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
587 "How to identify stuck projects.
588 This is a list of four items:
589 1. A tags/todo/property matcher string that is used to identify a project.
590 See the manual for a description of tag and property searches.
591 The entire tree below a headline matched by this is considered one project.
592 2. A list of TODO keywords identifying non-stuck projects.
593 If the project subtree contains any headline with one of these todo
594 keywords, the project is considered to be not stuck. If you specify
595 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
596 3. A list of tags identifying non-stuck projects.
597 If the project subtree contains any headline with one of these tags,
598 the project is considered to be not stuck. If you specify \"*\" as
599 a tag, any tag will mark the project unstuck. Note that this is about
600 the explicit presence of a tag somewhere in the subtree, inherited
601 tags do not count here. If inherited tags make a project not stuck,
602 use \"-TAG\" in the tags part of the matcher under (1.) above.
603 4. An arbitrary regular expression matching non-stuck projects.
605 If the project turns out to be not stuck, search continues also in the
606 subtree to see if any of the subtasks have project status.
608 See also the variable `org-tags-match-list-sublevels' which applies
609 to projects matched by this search as well.
611 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
612 or `C-c a #' to produce the list."
613 :group 'org-agenda-custom-commands
614 :type '(list
615 (string :tag "Tags/TODO match to identify a project")
616 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
617 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
618 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
620 (defgroup org-agenda-skip nil
621 "Options concerning skipping parts of agenda files."
622 :tag "Org Agenda Skip"
623 :group 'org-agenda)
625 (defcustom org-agenda-skip-function-global nil
626 "Function to be called at each match during agenda construction.
627 If this function returns nil, the current match should not be skipped.
628 If the function decided to skip an agenda match, is must return the
629 buffer position from which the search should be continued.
630 This may also be a Lisp form, which will be evaluated.
632 This variable will be applied to every agenda match, including
633 tags/property searches and TODO lists. So try to make the test function
634 do its checking as efficiently as possible. To implement a skipping
635 condition just for specific agenda commands, use the variable
636 `org-agenda-skip-function' which can be set in the options section
637 of custom agenda commands."
638 :group 'org-agenda-skip
639 :type 'sexp)
641 (defgroup org-agenda-daily/weekly nil
642 "Options concerning the daily/weekly agenda."
643 :tag "Org Agenda Daily/Weekly"
644 :group 'org-agenda)
645 (defgroup org-agenda-todo-list nil
646 "Options concerning the global todo list agenda view."
647 :tag "Org Agenda Todo List"
648 :group 'org-agenda)
649 (defgroup org-agenda-match-view nil
650 "Options concerning the general tags/property/todo match agenda view."
651 :tag "Org Agenda Match View"
652 :group 'org-agenda)
653 (defgroup org-agenda-search-view nil
654 "Options concerning the search agenda view."
655 :tag "Org Agenda Search View"
656 :group 'org-agenda)
658 (defvar org-agenda-archives-mode nil
659 "Non-nil means the agenda will include archived items.
660 If this is the symbol `trees', trees in the selected agenda scope
661 that are marked with the ARCHIVE tag will be included anyway. When this is
662 t, also all archive files associated with the current selection of agenda
663 files will be included.")
665 (defcustom org-agenda-restriction-lock-highlight-subtree t
666 "Non-nil means highlight the whole subtree when restriction is active.
667 Otherwise only highlight the headline. Highlighting the whole subtree is
668 useful to ensure no edits happen beyond the restricted region."
669 :group 'org-agenda
670 :type 'boolean)
672 (defcustom org-agenda-skip-comment-trees t
673 "Non-nil means skip trees that start with the COMMENT keyword.
674 When nil, these trees are also scanned by agenda commands."
675 :group 'org-agenda-skip
676 :type 'boolean)
678 (defcustom org-agenda-todo-list-sublevels t
679 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
680 When nil, the sublevels of a TODO entry are not checked, resulting in
681 potentially much shorter TODO lists."
682 :group 'org-agenda-skip
683 :group 'org-agenda-todo-list
684 :type 'boolean)
686 (defcustom org-agenda-todo-ignore-with-date nil
687 "Non-nil means don't show entries with a date in the global todo list.
688 You can use this if you prefer to mark mere appointments with a TODO keyword,
689 but don't want them to show up in the TODO list.
690 When this is set, it also covers deadlines and scheduled items, the settings
691 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
692 will be ignored.
693 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
694 :group 'org-agenda-skip
695 :group 'org-agenda-todo-list
696 :type 'boolean)
698 (defcustom org-agenda-todo-ignore-timestamp nil
699 "Non-nil means don't show entries with a timestamp.
700 This applies when creating the global todo list.
701 Valid values are:
703 past Don't show entries for today or in the past.
705 future Don't show entries with a timestamp in the future.
706 The idea behind this is that if it has a future
707 timestamp, you don't want to think about it until the
708 date.
710 all Don't show any entries with a timestamp in the global todo list.
711 The idea behind this is that by setting a timestamp, you
712 have already \"taken care\" of this item.
714 This variable can also have an integer as a value. If positive (N),
715 todos with a timestamp N or more days in the future will be ignored. If
716 negative (-N), todos with a timestamp N or more days in the past will be
717 ignored. If 0, todos with a timestamp either today or in the future will
718 be ignored. For example, a value of -1 will exclude todos with a
719 timestamp in the past (yesterday or earlier), while a value of 7 will
720 exclude todos with a timestamp a week or more in the future.
722 See also `org-agenda-todo-ignore-with-date'.
723 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
724 to make his option also apply to the tags-todo list."
725 :group 'org-agenda-skip
726 :group 'org-agenda-todo-list
727 :version "24.1"
728 :type '(choice
729 (const :tag "Ignore future timestamp todos" future)
730 (const :tag "Ignore past or present timestamp todos" past)
731 (const :tag "Ignore all timestamp todos" all)
732 (const :tag "Show timestamp todos" nil)
733 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
735 (defcustom org-agenda-todo-ignore-scheduled nil
736 "Non-nil means, ignore some scheduled TODO items when making TODO list.
737 This applies when creating the global todo list.
738 Valid values are:
740 past Don't show entries scheduled today or in the past.
742 future Don't show entries scheduled in the future.
743 The idea behind this is that by scheduling it, you don't want to
744 think about it until the scheduled date.
746 all Don't show any scheduled entries in the global todo list.
747 The idea behind this is that by scheduling it, you have already
748 \"taken care\" of this item.
750 t Same as `all', for backward compatibility.
752 This variable can also have an integer as a value. See
753 `org-agenda-todo-ignore-timestamp' for more details.
755 See also `org-agenda-todo-ignore-with-date'.
756 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
757 to make his option also apply to the tags-todo list."
758 :group 'org-agenda-skip
759 :group 'org-agenda-todo-list
760 :type '(choice
761 (const :tag "Ignore future-scheduled todos" future)
762 (const :tag "Ignore past- or present-scheduled todos" past)
763 (const :tag "Ignore all scheduled todos" all)
764 (const :tag "Ignore all scheduled todos (compatibility)" t)
765 (const :tag "Show scheduled todos" nil)
766 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
768 (defcustom org-agenda-todo-ignore-deadlines nil
769 "Non-nil means ignore some deadline TODO items when making TODO list.
770 There are different motivations for using different values, please think
771 carefully when configuring this variable.
773 This applies when creating the global todo list.
774 Valid values are:
776 near Don't show near deadline entries. A deadline is near when it is
777 closer than `org-deadline-warning-days' days. The idea behind this
778 is that such items will appear in the agenda anyway.
780 far Don't show TODO entries where a deadline has been defined, but
781 the deadline is not near. This is useful if you don't want to
782 use the todo list to figure out what to do now.
784 past Don't show entries with a deadline timestamp for today or in the past.
786 future Don't show entries with a deadline timestamp in the future, not even
787 when they become `near' ones. Use it with caution.
789 all Ignore all TODO entries that do have a deadline.
791 t Same as `near', for backward compatibility.
793 This variable can also have an integer as a value. See
794 `org-agenda-todo-ignore-timestamp' for more details.
796 See also `org-agenda-todo-ignore-with-date'.
797 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
798 to make his option also apply to the tags-todo list."
799 :group 'org-agenda-skip
800 :group 'org-agenda-todo-list
801 :type '(choice
802 (const :tag "Ignore near deadlines" near)
803 (const :tag "Ignore near deadlines (compatibility)" t)
804 (const :tag "Ignore far deadlines" far)
805 (const :tag "Ignore all TODOs with a deadlines" all)
806 (const :tag "Show all TODOs, even if they have a deadline" nil)
807 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
809 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
810 "Time unit to use when possibly ignoring an agenda item.
812 See the docstring of various `org-agenda-todo-ignore-*' options.
813 The default is to compare time stamps using days. An item is thus
814 considered to be in the future if it is at least one day after today.
815 Non-nil means to compare time stamps using seconds. An item is then
816 considered future if it has a time value later than current time."
817 :group 'org-agenda-skip
818 :group 'org-agenda-todo-list
819 :version "24.4"
820 :package-version '(Org . "8.0")
821 :type '(choice
822 (const :tag "Compare time with days" nil)
823 (const :tag "Compare time with seconds" t)))
825 (defcustom org-agenda-tags-todo-honor-ignore-options nil
826 "Non-nil means honor todo-list ignores options also in tags-todo search.
827 The variables
828 `org-agenda-todo-ignore-with-date',
829 `org-agenda-todo-ignore-timestamp',
830 `org-agenda-todo-ignore-scheduled',
831 `org-agenda-todo-ignore-deadlines'
832 make the global TODO list skip entries that have time stamps of certain
833 kinds. If this option is set, the same options will also apply for the
834 tags-todo search, which is the general tags/property matcher
835 restricted to unfinished TODO entries only."
836 :group 'org-agenda-skip
837 :group 'org-agenda-todo-list
838 :group 'org-agenda-match-view
839 :type 'boolean)
841 (defcustom org-agenda-skip-scheduled-if-done nil
842 "Non-nil means don't show scheduled items in agenda when they are done.
843 This is relevant for the daily/weekly agenda, not for the TODO list. And
844 it applies only to the actual date of the scheduling. Warnings about
845 an item with a past scheduling dates are always turned off when the item
846 is DONE."
847 :group 'org-agenda-skip
848 :group 'org-agenda-daily/weekly
849 :type 'boolean)
851 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
852 "Non-nil means skip scheduling line if same entry shows because of deadline.
854 In the agenda of today, an entry can show up multiple times
855 because it is both scheduled and has a nearby deadline, and maybe
856 a plain time stamp as well.
858 When this variable is nil, the entry will be shown several times.
860 When set to t, then only the deadline is shown and the fact that
861 the entry is scheduled today or was scheduled previously is not
862 shown.
864 When set to the symbol `not-today', skip scheduled previously,
865 but not scheduled today.
867 When set to the symbol `repeated-after-deadline', skip scheduled
868 items if they are repeated beyond the current deadline."
869 :group 'org-agenda-skip
870 :group 'org-agenda-daily/weekly
871 :type '(choice
872 (const :tag "Never" nil)
873 (const :tag "Always" t)
874 (const :tag "Not when scheduled today" not-today)
875 (const :tag "When repeated past deadline" repeated-after-deadline)))
877 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
878 "Non-nil means skip timestamp line if same entry shows because of deadline.
879 In the agenda of today, an entry can show up multiple times
880 because it has both a plain timestamp and has a nearby deadline.
881 When this variable is t, then only the deadline is shown and the
882 fact that the entry has a timestamp for or including today is not
883 shown. When this variable is nil, the entry will be shown
884 several times."
885 :group 'org-agenda-skip
886 :group 'org-agenda-daily/weekly
887 :version "24.1"
888 :type '(choice
889 (const :tag "Never" nil)
890 (const :tag "Always" t)))
892 (defcustom org-agenda-skip-deadline-if-done nil
893 "Non-nil means don't show deadlines when the corresponding item is done.
894 When nil, the deadline is still shown and should give you a happy feeling.
895 This is relevant for the daily/weekly agenda. And it applied only to the
896 actually date of the deadline. Warnings about approaching and past-due
897 deadlines are always turned off when the item is DONE."
898 :group 'org-agenda-skip
899 :group 'org-agenda-daily/weekly
900 :type 'boolean)
902 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
903 "Non-nil means skip deadline prewarning when entry is also scheduled.
904 This will apply on all days where a prewarning for the deadline would
905 be shown, but not at the day when the entry is actually due. On that day,
906 the deadline will be shown anyway.
907 This variable may be set to nil, t, the symbol `pre-scheduled',
908 or a number which will then give the number of days before the actual
909 deadline when the prewarnings should resume. The symbol `pre-scheduled'
910 eliminates the deadline prewarning only prior to the scheduled date.
911 This can be used in a workflow where the first showing of the deadline will
912 trigger you to schedule it, and then you don't want to be reminded of it
913 because you will take care of it on the day when scheduled."
914 :group 'org-agenda-skip
915 :group 'org-agenda-daily/weekly
916 :version "24.1"
917 :type '(choice
918 (const :tag "Always show prewarning" nil)
919 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
920 (const :tag "Remove prewarning if entry is scheduled" t)
921 (integer :tag "Restart prewarning N days before deadline")))
923 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
924 "Non-nil means skip scheduled delay when entry also has a deadline.
925 This variable may be set to nil, t, the symbol `post-deadline',
926 or a number which will then give the number of days after the actual
927 scheduled date when the delay should expire. The symbol `post-deadline'
928 eliminates the schedule delay when the date is posterior to the deadline."
929 :group 'org-agenda-skip
930 :group 'org-agenda-daily/weekly
931 :version "24.4"
932 :package-version '(Org . "8.0")
933 :type '(choice
934 (const :tag "Always honor delay" nil)
935 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
936 (const :tag "Ignore delay if entry has a deadline" t)
937 (integer :tag "Honor delay up until N days after the scheduled date")))
939 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
940 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
941 When non-nil, after the search for timestamps has matched once in an
942 entry, the rest of the entry will not be searched."
943 :group 'org-agenda-skip
944 :type 'boolean)
946 (defcustom org-agenda-skip-timestamp-if-done nil
947 "Non-nil means don't select item by timestamp or -range if it is DONE."
948 :group 'org-agenda-skip
949 :group 'org-agenda-daily/weekly
950 :type 'boolean)
952 (defcustom org-agenda-dim-blocked-tasks t
953 "Non-nil means dim blocked tasks in the agenda display.
954 This causes some overhead during agenda construction, but if you
955 have turned on `org-enforce-todo-dependencies',
956 `org-enforce-todo-checkbox-dependencies', or any other blocking
957 mechanism, this will create useful feedback in the agenda.
959 Instead of t, this variable can also have the value `invisible'.
960 Then blocked tasks will be invisible and only become visible when
961 they become unblocked. An exemption to this behavior is when a task is
962 blocked because of unchecked checkboxes below it. Since checkboxes do
963 not show up in the agenda views, making this task invisible you remove any
964 trace from agenda views that there is something to do. Therefore, a task
965 that is blocked because of checkboxes will never be made invisible, it
966 will only be dimmed."
967 :group 'org-agenda-daily/weekly
968 :group 'org-agenda-todo-list
969 :version "24.3"
970 :type '(choice
971 (const :tag "Do not dim" nil)
972 (const :tag "Dim to a gray face" t)
973 (const :tag "Make invisible" invisible)))
975 (defcustom org-timeline-show-empty-dates 3
976 "Non-nil means `org-timeline' also shows dates without an entry.
977 When nil, only the days which actually have entries are shown.
978 When t, all days between the first and the last date are shown.
979 When an integer, show also empty dates, but if there is a gap of more than
980 N days, just insert a special line indicating the size of the gap."
981 :group 'org-agenda-skip
982 :type '(choice
983 (const :tag "None" nil)
984 (const :tag "All" t)
985 (integer :tag "at most")))
987 (defgroup org-agenda-startup nil
988 "Options concerning initial settings in the Agenda in Org Mode."
989 :tag "Org Agenda Startup"
990 :group 'org-agenda)
992 (defcustom org-agenda-menu-show-matcher t
993 "Non-nil means show the match string in the agenda dispatcher menu.
994 When nil, the matcher string is not shown, but is put into the help-echo
995 property so than moving the mouse over the command shows it.
996 Setting it to nil is good if matcher strings are very long and/or if
997 you want to use two-columns display (see `org-agenda-menu-two-columns')."
998 :group 'org-agenda
999 :version "24.1"
1000 :type 'boolean)
1002 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1004 (defcustom org-agenda-menu-two-columns nil
1005 "Non-nil means, use two columns to show custom commands in the dispatcher.
1006 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1007 to nil."
1008 :group 'org-agenda
1009 :version "24.1"
1010 :type 'boolean)
1012 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1013 (defcustom org-agenda-finalize-hook nil
1014 "Hook run just before displaying an agenda buffer.
1015 The buffer is still writable when the hook is called.
1017 You can modify some of the buffer substrings but you should be
1018 extra careful not to modify the text properties of the agenda
1019 headlines as the agenda display heavily relies on them."
1020 :group 'org-agenda-startup
1021 :type 'hook)
1023 (defcustom org-agenda-mouse-1-follows-link nil
1024 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1025 A longer mouse click will still set point. Does not work on XEmacs.
1026 Needs to be set before org.el is loaded."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-start-with-follow-mode nil
1031 "The initial value of follow mode in a newly created agenda window."
1032 :group 'org-agenda-startup
1033 :type 'boolean)
1035 (defcustom org-agenda-follow-indirect nil
1036 "Non-nil means `org-agenda-follow-mode' displays only the
1037 current item's tree, in an indirect buffer."
1038 :group 'org-agenda
1039 :version "24.1"
1040 :type 'boolean)
1042 (defcustom org-agenda-show-outline-path t
1043 "Non-nil means show outline path in echo area after line motion."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-start-with-entry-text-mode nil
1048 "The initial value of entry-text-mode in a newly created agenda window."
1049 :group 'org-agenda-startup
1050 :type 'boolean)
1052 (defcustom org-agenda-entry-text-maxlines 5
1053 "Number of text lines to be added when `E' is pressed in the agenda.
1055 Note that this variable only used during agenda display. Add add entry text
1056 when exporting the agenda, configure the variable
1057 `org-agenda-add-entry-ext-maxlines'."
1058 :group 'org-agenda
1059 :type 'integer)
1061 (defcustom org-agenda-entry-text-exclude-regexps nil
1062 "List of regular expressions to clean up entry text.
1063 The complete matches of all regular expressions in this list will be
1064 removed from entry text before it is shown in the agenda."
1065 :group 'org-agenda
1066 :type '(repeat (regexp)))
1068 (defcustom org-agenda-entry-text-leaders " > "
1069 "Text prepended to the entry text in agenda buffers."
1070 :version "24.4"
1071 :package-version '(Org . "8.0")
1072 :group 'org-agenda
1073 :type 'string)
1075 (defvar org-agenda-entry-text-cleanup-hook nil
1076 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1077 This cleanup is done in a temporary buffer, so the function may inspect and
1078 change the entire buffer.
1079 Some default stuff like drawers and scheduling/deadline dates will already
1080 have been removed when this is called, as will any matches for regular
1081 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1083 (defvar org-agenda-include-inactive-timestamps nil
1084 "Non-nil means include inactive time stamps in agenda and timeline.
1085 Dynamically scoped.")
1087 (defgroup org-agenda-windows nil
1088 "Options concerning the windows used by the Agenda in Org Mode."
1089 :tag "Org Agenda Windows"
1090 :group 'org-agenda)
1092 (defcustom org-agenda-window-setup 'reorganize-frame
1093 "How the agenda buffer should be displayed.
1094 Possible values for this option are:
1096 current-window Show agenda in the current window, keeping all other windows.
1097 other-window Use `switch-to-buffer-other-window' to display agenda.
1098 reorganize-frame Show only two windows on the current frame, the current
1099 window and the agenda.
1100 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1101 Also, when exiting the agenda, kill that frame.
1102 See also the variable `org-agenda-restore-windows-after-quit'."
1103 :group 'org-agenda-windows
1104 :type '(choice
1105 (const current-window)
1106 (const other-frame)
1107 (const other-window)
1108 (const reorganize-frame)))
1110 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1111 "The min and max height of the agenda window as a fraction of frame height.
1112 The value of the variable is a cons cell with two numbers between 0 and 1.
1113 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1114 :group 'org-agenda-windows
1115 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1117 (defcustom org-agenda-restore-windows-after-quit nil
1118 "Non-nil means restore window configuration upon exiting agenda.
1119 Before the window configuration is changed for displaying the agenda,
1120 the current status is recorded. When the agenda is exited with
1121 `q' or `x' and this option is set, the old state is restored. If
1122 `org-agenda-window-setup' is `other-frame', the value of this
1123 option will be ignored."
1124 :group 'org-agenda-windows
1125 :type 'boolean)
1127 (defcustom org-agenda-ndays nil
1128 "Number of days to include in overview display.
1129 Should be 1 or 7.
1130 Obsolete, see `org-agenda-span'."
1131 :group 'org-agenda-daily/weekly
1132 :type '(choice (const nil)
1133 (integer)))
1135 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1137 (defcustom org-agenda-span 'week
1138 "Number of days to include in overview display.
1139 Can be day, week, month, year, or any number of days.
1140 Custom commands can set this variable in the options section."
1141 :group 'org-agenda-daily/weekly
1142 :type '(choice (const :tag "Day" day)
1143 (const :tag "Week" week)
1144 (const :tag "Fortnight" fortnight)
1145 (const :tag "Month" month)
1146 (const :tag "Year" year)
1147 (integer :tag "Custom")))
1149 (defcustom org-agenda-start-on-weekday 1
1150 "Non-nil means start the overview always on the specified weekday.
1151 0 denotes Sunday, 1 denotes Monday, etc.
1152 When nil, always start on the current day.
1153 Custom commands can set this variable in the options section."
1154 :group 'org-agenda-daily/weekly
1155 :type '(choice (const :tag "Today" nil)
1156 (integer :tag "Weekday No.")))
1158 (defcustom org-agenda-show-all-dates t
1159 "Non-nil means `org-agenda' shows every day in the selected range.
1160 When nil, only the days which actually have entries are shown."
1161 :group 'org-agenda-daily/weekly
1162 :type 'boolean)
1164 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1165 "Format string for displaying dates in the agenda.
1166 Used by the daily/weekly agenda and by the timeline. This should be
1167 a format string understood by `format-time-string', or a function returning
1168 the formatted date as a string. The function must take a single argument,
1169 a calendar-style date list like (month day year)."
1170 :group 'org-agenda-daily/weekly
1171 :type '(choice
1172 (string :tag "Format string")
1173 (function :tag "Function")))
1175 (defun org-agenda-format-date-aligned (date)
1176 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1177 This function makes sure that dates are aligned for easy reading."
1178 (require 'cal-iso)
1179 (let* ((dayname (calendar-day-name date))
1180 (day (cadr date))
1181 (day-of-week (calendar-day-of-week date))
1182 (month (car date))
1183 (monthname (calendar-month-name month))
1184 (year (nth 2 date))
1185 (iso-week (org-days-to-iso-week
1186 (calendar-absolute-from-gregorian date)))
1187 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1188 (1- year))
1189 ((and (= month 12) (<= iso-week 1))
1190 (1+ year))
1191 (t year)))
1192 (weekstring (if (= day-of-week 1)
1193 (format " W%02d" iso-week)
1194 "")))
1195 (format "%-10s %2d %s %4d%s"
1196 dayname day monthname year weekstring)))
1198 (defcustom org-agenda-time-leading-zero nil
1199 "Non-nil means use leading zero for military times in agenda.
1200 For example, 9:30am would become 09:30 rather than 9:30."
1201 :group 'org-agenda-daily/weekly
1202 :version "24.1"
1203 :type 'boolean)
1205 (defcustom org-agenda-timegrid-use-ampm nil
1206 "When set, show AM/PM style timestamps on the timegrid."
1207 :group 'org-agenda
1208 :version "24.1"
1209 :type 'boolean)
1211 (defun org-agenda-time-of-day-to-ampm (time)
1212 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1213 (let* ((hour-number (string-to-number (substring time 0 -3)))
1214 (minute (substring time -2))
1215 (ampm "am"))
1216 (cond
1217 ((equal hour-number 12)
1218 (setq ampm "pm"))
1219 ((> hour-number 12)
1220 (setq ampm "pm")
1221 (setq hour-number (- hour-number 12))))
1222 (concat
1223 (if org-agenda-time-leading-zero
1224 (format "%02d" hour-number)
1225 (format "%02s" (number-to-string hour-number)))
1226 ":" minute ampm)))
1228 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1229 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1230 (if org-agenda-timegrid-use-ampm
1231 (org-agenda-time-of-day-to-ampm time)
1232 time))
1234 (defcustom org-agenda-weekend-days '(6 0)
1235 "Which days are weekend?
1236 These days get the special face `org-agenda-date-weekend' in the agenda
1237 and timeline buffers."
1238 :group 'org-agenda-daily/weekly
1239 :type '(set :greedy t
1240 (const :tag "Monday" 1)
1241 (const :tag "Tuesday" 2)
1242 (const :tag "Wednesday" 3)
1243 (const :tag "Thursday" 4)
1244 (const :tag "Friday" 5)
1245 (const :tag "Saturday" 6)
1246 (const :tag "Sunday" 0)))
1248 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1249 "Non-nil means jump to today when moving a past date forward in time.
1250 When using S-right in the agenda to move a a date forward, and the date
1251 stamp currently points to the past, the first key press will move it
1252 to today. WHen nil, just move one day forward even if the date stays
1253 in the past."
1254 :group 'org-agenda-daily/weekly
1255 :version "24.1"
1256 :type 'boolean)
1258 (defcustom org-agenda-include-diary nil
1259 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1260 Custom commands can set this variable in the options section."
1261 :group 'org-agenda-daily/weekly
1262 :type 'boolean)
1264 (defcustom org-agenda-include-deadlines t
1265 "If non-nil, include entries within their deadline warning period.
1266 Custom commands can set this variable in the options section."
1267 :group 'org-agenda-daily/weekly
1268 :version "24.1"
1269 :type 'boolean)
1271 (defcustom org-agenda-repeating-timestamp-show-all t
1272 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1273 When set to a list of strings, only show occurrences of repeating
1274 stamps for these TODO keywords. When nil, only one occurrence is
1275 shown, either today or the nearest into the future."
1276 :group 'org-agenda-daily/weekly
1277 :type '(choice
1278 (const :tag "Show repeating stamps" t)
1279 (repeat :tag "Show repeating stamps for these TODO keywords"
1280 (string :tag "TODO Keyword"))
1281 (const :tag "Don't show repeating stamps" nil)))
1283 (defcustom org-scheduled-past-days 10000
1284 "Number of days to continue listing scheduled items not marked DONE.
1285 When an item is scheduled on a date, it shows up in the agenda on this
1286 day and will be listed until it is marked done for the number of days
1287 given here."
1288 :group 'org-agenda-daily/weekly
1289 :type 'integer)
1291 (defcustom org-agenda-log-mode-items '(closed clock)
1292 "List of items that should be shown in agenda log mode.
1293 This list may contain the following symbols:
1295 closed Show entries that have been closed on that day.
1296 clock Show entries that have received clocked time on that day.
1297 state Show all logged state changes.
1298 Note that instead of changing this variable, you can also press `C-u l' in
1299 the agenda to display all available LOG items temporarily."
1300 :group 'org-agenda-daily/weekly
1301 :type '(set :greedy t (const closed) (const clock) (const state)))
1303 (defcustom org-agenda-clock-consistency-checks
1304 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1305 :gap-ok-around ("4:00")
1306 :default-face ((:background "DarkRed") (:foreground "white"))
1307 :overlap-face nil :gap-face nil :no-end-time-face nil
1308 :long-face nil :short-face nil)
1309 "This is a property list, with the following keys:
1311 :max-duration Mark clocking chunks that are longer than this time.
1312 This is a time string like \"HH:MM\", or the number
1313 of minutes as an integer.
1315 :min-duration Mark clocking chunks that are shorter that this.
1316 This is a time string like \"HH:MM\", or the number
1317 of minutes as an integer.
1319 :max-gap Mark gaps between clocking chunks that are longer than
1320 this duration. A number of minutes, or a string
1321 like \"HH:MM\".
1323 :gap-ok-around List of times during the day which are usually not working
1324 times. When a gap is detected, but the gap contains any
1325 of these times, the gap is *not* reported. For example,
1326 if this is (\"4:00\" \"13:00\") then gaps that contain
1327 4:00 in the morning (i.e. the night) and 13:00
1328 (i.e. a typical lunch time) do not cause a warning.
1329 You should have at least one time during the night in this
1330 list, or otherwise the first task each morning will trigger
1331 a warning because it follows a long gap.
1333 Furthermore, the following properties can be used to define faces for
1334 issue display.
1336 :default-face the default face, if the specific face is undefined
1337 :overlap-face face for overlapping clocks
1338 :gap-face face for gaps between clocks
1339 :no-end-time-face face for incomplete clocks
1340 :long-face face for clock intervals that are too long
1341 :short-face face for clock intervals that are too short"
1342 :group 'org-agenda-daily/weekly
1343 :group 'org-clock
1344 :version "24.1"
1345 :type 'plist)
1347 (defcustom org-agenda-log-mode-add-notes t
1348 "Non-nil means add first line of notes to log entries in agenda views.
1349 If a log item like a state change or a clock entry is associated with
1350 notes, the first line of these notes will be added to the entry in the
1351 agenda display."
1352 :group 'org-agenda-daily/weekly
1353 :type 'boolean)
1355 (defcustom org-agenda-start-with-log-mode nil
1356 "The initial value of log-mode in a newly created agenda window.
1357 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1358 explanations on the possible values."
1359 :group 'org-agenda-startup
1360 :group 'org-agenda-daily/weekly
1361 :type '(choice (const :tag "Don't show log items" nil)
1362 (const :tag "Show only log items" only)
1363 (const :tag "Show all possible log items" clockcheck)
1364 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1365 (choice (const :tag "Show closed log items" closed)
1366 (const :tag "Show clocked log items" clock)
1367 (const :tag "Show all logged state changes" state)))))
1369 (defcustom org-agenda-start-with-clockreport-mode nil
1370 "The initial value of clockreport-mode in a newly created agenda window."
1371 :group 'org-agenda-startup
1372 :group 'org-agenda-daily/weekly
1373 :type 'boolean)
1375 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1376 "Property list with parameters for the clocktable in clockreport mode.
1377 This is the display mode that shows a clock table in the daily/weekly
1378 agenda, the properties for this dynamic block can be set here.
1379 The usual clocktable parameters are allowed here, but you cannot set
1380 the properties :name, :tstart, :tend, :block, and :scope - these will
1381 be overwritten to make sure the content accurately reflects the
1382 current display in the agenda."
1383 :group 'org-agenda-daily/weekly
1384 :type 'plist)
1386 (defcustom org-agenda-search-view-always-boolean nil
1387 "Non-nil means the search string is interpreted as individual parts.
1389 The search string for search view can either be interpreted as a phrase,
1390 or as a list of snippets that define a boolean search for a number of
1391 strings.
1393 When this is non-nil, the string will be split on whitespace, and each
1394 snippet will be searched individually, and all must match in order to
1395 select an entry. A snippet is then a single string of non-white
1396 characters, or a string in double quotes, or a regexp in {} braces.
1397 If a snippet is preceded by \"-\", the snippet must *not* match.
1398 \"+\" is syntactic sugar for positive selection. Each snippet may
1399 be found as a full word or a partial word, but see the variable
1400 `org-agenda-search-view-force-full-words'.
1402 When this is nil, search will look for the entire search phrase as one,
1403 with each space character matching any amount of whitespace, including
1404 line breaks.
1406 Even when this is nil, you can still switch to Boolean search dynamically
1407 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1408 is a regexp marked with braces like \"{abc}\", this will also switch to
1409 boolean search."
1410 :group 'org-agenda-search-view
1411 :version "24.1"
1412 :type 'boolean)
1414 (org-defvaralias 'org-agenda-search-view-search-words-only
1415 'org-agenda-search-view-always-boolean)
1417 (defcustom org-agenda-search-view-force-full-words nil
1418 "Non-nil means, search words must be matches as complete words.
1419 When nil, they may also match part of a word."
1420 :group 'org-agenda-search-view
1421 :version "24.1"
1422 :type 'boolean)
1424 (defcustom org-agenda-search-view-max-outline-level 0
1425 "Maximum outline level to display in search view.
1426 E.g. when this is set to 1, the search view will only
1427 show headlines of level 1. When set to 0, the default
1428 value, don't limit agenda view by outline level."
1429 :group 'org-agenda-search-view
1430 :version "24.4"
1431 :package-version '(Org . "8.3")
1432 :type 'integer)
1434 (defgroup org-agenda-time-grid nil
1435 "Options concerning the time grid in the Org-mode Agenda."
1436 :tag "Org Agenda Time Grid"
1437 :group 'org-agenda)
1439 (defcustom org-agenda-search-headline-for-time t
1440 "Non-nil means search headline for a time-of-day.
1441 If the headline contains a time-of-day in one format or another, it will
1442 be used to sort the entry into the time sequence of items for a day.
1443 Some people have time stamps in the headline that refer to the creation
1444 time or so, and then this produces an unwanted side effect. If this is
1445 the case for your, use this variable to turn off searching the headline
1446 for a time."
1447 :group 'org-agenda-time-grid
1448 :type 'boolean)
1450 (defcustom org-agenda-use-time-grid t
1451 "Non-nil means show a time grid in the agenda schedule.
1452 A time grid is a set of lines for specific times (like every two hours between
1453 8:00 and 20:00). The items scheduled for a day at specific times are
1454 sorted in between these lines.
1455 For details about when the grid will be shown, and what it will look like, see
1456 the variable `org-agenda-time-grid'."
1457 :group 'org-agenda-time-grid
1458 :type 'boolean)
1460 (defcustom org-agenda-time-grid
1461 '((daily today require-timed)
1462 "----------------"
1463 (800 1000 1200 1400 1600 1800 2000))
1465 "The settings for time grid for agenda display.
1466 This is a list of three items. The first item is again a list. It contains
1467 symbols specifying conditions when the grid should be displayed:
1469 daily if the agenda shows a single day
1470 weekly if the agenda shows an entire week
1471 today show grid on current date, independent of daily/weekly display
1472 require-timed show grid only if at least one item has a time specification
1474 The second item is a string which will be placed behind the grid time.
1476 The third item is a list of integers, indicating the times that should have
1477 a grid line."
1478 :group 'org-agenda-time-grid
1479 :type
1480 '(list
1481 (set :greedy t :tag "Grid Display Options"
1482 (const :tag "Show grid in single day agenda display" daily)
1483 (const :tag "Show grid in weekly agenda display" weekly)
1484 (const :tag "Always show grid for today" today)
1485 (const :tag "Show grid only if any timed entries are present"
1486 require-timed)
1487 (const :tag "Skip grid times already present in an entry"
1488 remove-match))
1489 (string :tag "Grid String")
1490 (repeat :tag "Grid Times" (integer :tag "Time"))))
1492 (defcustom org-agenda-show-current-time-in-grid t
1493 "Non-nil means show the current time in the time grid."
1494 :group 'org-agenda-time-grid
1495 :version "24.1"
1496 :type 'boolean)
1498 (defcustom org-agenda-current-time-string
1499 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1500 "The string for the current time marker in the agenda."
1501 :group 'org-agenda-time-grid
1502 :version "24.1"
1503 :type 'string)
1505 (defgroup org-agenda-sorting nil
1506 "Options concerning sorting in the Org-mode Agenda."
1507 :tag "Org Agenda Sorting"
1508 :group 'org-agenda)
1510 (defcustom org-agenda-sorting-strategy
1511 '((agenda habit-down time-up priority-down category-keep)
1512 (todo priority-down category-keep)
1513 (tags priority-down category-keep)
1514 (search category-keep))
1515 "Sorting structure for the agenda items of a single day.
1516 This is a list of symbols which will be used in sequence to determine
1517 if an entry should be listed before another entry. The following
1518 symbols are recognized:
1520 time-up Put entries with time-of-day indications first, early first
1521 time-down Put entries with time-of-day indications first, late first
1522 timestamp-up Sort by any timestamp, early first
1523 timestamp-down Sort by any timestamp, late first
1524 scheduled-up Sort by scheduled timestamp, early first
1525 scheduled-down Sort by scheduled timestamp, late first
1526 deadline-up Sort by deadline timestamp, early first
1527 deadline-down Sort by deadline timestamp, late first
1528 ts-up Sort by active timestamp, early first
1529 ts-down Sort by active timestamp, late first
1530 tsia-up Sort by inactive timestamp, early first
1531 tsia-down Sort by inactive timestamp, late first
1532 category-keep Keep the default order of categories, corresponding to the
1533 sequence in `org-agenda-files'.
1534 category-up Sort alphabetically by category, A-Z.
1535 category-down Sort alphabetically by category, Z-A.
1536 tag-up Sort alphabetically by last tag, A-Z.
1537 tag-down Sort alphabetically by last tag, Z-A.
1538 priority-up Sort numerically by priority, high priority last.
1539 priority-down Sort numerically by priority, high priority first.
1540 todo-state-up Sort by todo state, tasks that are done last.
1541 todo-state-down Sort by todo state, tasks that are done first.
1542 effort-up Sort numerically by estimated effort, high effort last.
1543 effort-down Sort numerically by estimated effort, high effort first.
1544 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1545 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1546 habit-up Put entries that are habits first
1547 habit-down Put entries that are habits last
1548 alpha-up Sort headlines alphabetically
1549 alpha-down Sort headlines alphabetically, reversed
1551 The different possibilities will be tried in sequence, and testing stops
1552 if one comparison returns a \"not-equal\". For example, the default
1553 '(time-up category-keep priority-down)
1554 means: Pull out all entries having a specified time of day and sort them,
1555 in order to make a time schedule for the current day the first thing in the
1556 agenda listing for the day. Of the entries without a time indication, keep
1557 the grouped in categories, don't sort the categories, but keep them in
1558 the sequence given in `org-agenda-files'. Within each category sort by
1559 priority.
1561 Leaving out `category-keep' would mean that items will be sorted across
1562 categories by priority.
1564 Instead of a single list, this can also be a set of list for specific
1565 contents, with a context symbol in the car of the list, any of
1566 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1568 Custom commands can bind this variable in the options section."
1569 :group 'org-agenda-sorting
1570 :type `(choice
1571 (repeat :tag "General" ,org-sorting-choice)
1572 (list :tag "Individually"
1573 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1574 (repeat ,org-sorting-choice))
1575 (cons (const :tag "Strategy for TODO lists" todo)
1576 (repeat ,org-sorting-choice))
1577 (cons (const :tag "Strategy for Tags matches" tags)
1578 (repeat ,org-sorting-choice))
1579 (cons (const :tag "Strategy for search matches" search)
1580 (repeat ,org-sorting-choice)))))
1582 (defcustom org-agenda-cmp-user-defined nil
1583 "A function to define the comparison `user-defined'.
1584 This function must receive two arguments, agenda entry a and b.
1585 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1586 the user comparison, return nil.
1587 When this is defined, you can make `user-defined-up' and `user-defined-down'
1588 part of an agenda sorting strategy."
1589 :group 'org-agenda-sorting
1590 :type 'symbol)
1592 (defcustom org-sort-agenda-notime-is-late t
1593 "Non-nil means items without time are considered late.
1594 This is only relevant for sorting. When t, items which have no explicit
1595 time like 15:30 will be considered as 99:01, i.e. later than any items which
1596 do have a time. When nil, the default time is before 0:00. You can use this
1597 option to decide if the schedule for today should come before or after timeless
1598 agenda entries."
1599 :group 'org-agenda-sorting
1600 :type 'boolean)
1602 (defcustom org-sort-agenda-noeffort-is-high t
1603 "Non-nil means items without effort estimate are sorted as high effort.
1604 This also applies when filtering an agenda view with respect to the
1605 < or > effort operator. Then, tasks with no effort defined will be treated
1606 as tasks with high effort.
1607 When nil, such items are sorted as 0 minutes effort."
1608 :group 'org-agenda-sorting
1609 :type 'boolean)
1611 (defgroup org-agenda-line-format nil
1612 "Options concerning the entry prefix in the Org-mode agenda display."
1613 :tag "Org Agenda Line Format"
1614 :group 'org-agenda)
1616 (defcustom org-agenda-prefix-format
1617 '((agenda . " %i %-12:c%?-12t% s")
1618 (timeline . " % s")
1619 (todo . " %i %-12:c")
1620 (tags . " %i %-12:c")
1621 (search . " %i %-12:c"))
1622 "Format specifications for the prefix of items in the agenda views.
1623 An alist with five entries, each for the different agenda types. The
1624 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1625 The values are format strings.
1627 This format works similar to a printf format, with the following meaning:
1629 %c the category of the item, \"Diary\" for entries from the diary,
1630 or as given by the CATEGORY keyword or derived from the file name
1631 %e the effort required by the item
1632 %l the level of the item (insert X space(s) if item is of level X)
1633 %i the icon category of the item, see `org-agenda-category-icon-alist'
1634 %T the last tag of the item (ignore inherited tags, which come first)
1635 %t the HH:MM time-of-day specification if one applies to the entry
1636 %s Scheduling/Deadline information, a short string
1637 %b show breadcrumbs, i.e., the names of the higher levels
1638 %(expression) Eval EXPRESSION and replace the control string
1639 by the result
1641 All specifiers work basically like the standard `%s' of printf, but may
1642 contain two additional characters: a question mark just after the `%'
1643 and a whitespace/punctuation character just before the final letter.
1645 If the first character after `%' is a question mark, the entire field
1646 will only be included if the corresponding value applies to the current
1647 entry. This is useful for fields which should have fixed width when
1648 present, but zero width when absent. For example, \"%?-12t\" will
1649 result in a 12 character time field if a time of the day is specified,
1650 but will completely disappear in entries which do not contain a time.
1652 If there is punctuation or whitespace character just before the
1653 final format letter, this character will be appended to the field
1654 value if the value is not empty. For example, the format
1655 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1656 the category is empty, no additional colon is inserted.
1658 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1659 which means:
1661 - Indent the line with two space characters
1662 - Give the category a 12 chars wide field, padded with whitespace on
1663 the right (because of `-'). Append a colon if there is a category
1664 (because of `:').
1665 - If there is a time-of-day, put it into a 12 chars wide field. If no
1666 time, don't put in an empty field, just skip it (because of '?').
1667 - Finally, put the scheduling information.
1669 See also the variables `org-agenda-remove-times-when-in-prefix' and
1670 `org-agenda-remove-tags'.
1672 Custom commands can set this variable in the options section."
1673 :type '(choice
1674 (string :tag "General format")
1675 (list :greedy t :tag "View dependent"
1676 (cons (const agenda) (string :tag "Format"))
1677 (cons (const timeline) (string :tag "Format"))
1678 (cons (const todo) (string :tag "Format"))
1679 (cons (const tags) (string :tag "Format"))
1680 (cons (const search) (string :tag "Format"))))
1681 :group 'org-agenda-line-format)
1683 (defvar org-prefix-format-compiled nil
1684 "The compiled prefix format and associated variables.
1685 This is a list where first element is a list of variable bindings, and second
1686 element is the compiled format expression. See the variable
1687 `org-agenda-prefix-format'.")
1689 (defcustom org-agenda-todo-keyword-format "%-1s"
1690 "Format for the TODO keyword in agenda lines.
1691 Set this to something like \"%-12s\" if you want all TODO keywords
1692 to occupy a fixed space in the agenda display."
1693 :group 'org-agenda-line-format
1694 :type 'string)
1696 (defcustom org-agenda-diary-sexp-prefix nil
1697 "A regexp that matches part of a diary sexp entry
1698 which should be treated as scheduling/deadline information in
1699 `org-agenda'.
1701 For example, you can use this to extract the `diary-remind-message' from
1702 `diary-remind' entries."
1703 :group 'org-agenda-line-format
1704 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1706 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1707 "Text preceding timerange entries in the agenda view.
1708 This is a list with two strings. The first applies when the range
1709 is entirely on one day. The second applies if the range spans several days.
1710 The strings may have two \"%d\" format specifiers which will be filled
1711 with the sequence number of the days, and the total number of days in the
1712 range, respectively."
1713 :group 'org-agenda-line-format
1714 :type '(list
1715 (string :tag "Deadline today ")
1716 (choice :tag "Deadline relative"
1717 (string :tag "Format string")
1718 (function))))
1720 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1721 "Text preceding scheduled items in the agenda view.
1722 This is a list with two strings. The first applies when the item is
1723 scheduled on the current day. The second applies when it has been scheduled
1724 previously, it may contain a %d indicating that this is the nth time that
1725 this item is scheduled, due to automatic rescheduling of unfinished items
1726 for the following day. So this number is one larger than the number of days
1727 that passed since this item was scheduled first."
1728 :group 'org-agenda-line-format
1729 :version "24.4"
1730 :package-version '(Org . "8.0")
1731 :type '(list
1732 (string :tag "Scheduled today ")
1733 (string :tag "Scheduled previously")))
1735 (defcustom org-agenda-inactive-leader "["
1736 "Text preceding item pulled into the agenda by inactive time stamps.
1737 These entries are added to the agenda when pressing \"[\"."
1738 :group 'org-agenda-line-format
1739 :version "24.1"
1740 :type 'string)
1742 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1743 "Text preceding deadline items in the agenda view.
1744 This is a list with three strings. The first applies when the item has its
1745 deadline on the current day. The second applies when the deadline is in the
1746 future, the third one when it is in the past. The strings may contain %d
1747 to capture the number of days."
1748 :group 'org-agenda-line-format
1749 :version "24.4"
1750 :package-version '(Org . "8.0")
1751 :type '(list
1752 (string :tag "Deadline today ")
1753 (string :tag "Deadline in the future ")
1754 (string :tag "Deadline in the past ")))
1756 (defcustom org-agenda-remove-times-when-in-prefix t
1757 "Non-nil means remove duplicate time specifications in agenda items.
1758 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1759 time-of-day specification in a headline or diary entry is extracted and
1760 placed into the prefix. If this option is non-nil, the original specification
1761 \(a timestamp or -range, or just a plain time(range) specification like
1762 11:30-4pm) will be removed for agenda display. This makes the agenda less
1763 cluttered.
1764 The option can be t or nil. It may also be the symbol `beg', indicating
1765 that the time should only be removed when it is located at the beginning of
1766 the headline/diary entry."
1767 :group 'org-agenda-line-format
1768 :type '(choice
1769 (const :tag "Always" t)
1770 (const :tag "Never" nil)
1771 (const :tag "When at beginning of entry" beg)))
1773 (defcustom org-agenda-remove-timeranges-from-blocks nil
1774 "Non-nil means remove time ranges specifications in agenda
1775 items that span on several days."
1776 :group 'org-agenda-line-format
1777 :version "24.1"
1778 :type 'boolean)
1780 (defcustom org-agenda-default-appointment-duration nil
1781 "Default duration for appointments that only have a starting time.
1782 When nil, no duration is specified in such cases.
1783 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1784 :group 'org-agenda-line-format
1785 :type '(choice
1786 (integer :tag "Minutes")
1787 (const :tag "No default duration")))
1789 (defcustom org-agenda-show-inherited-tags t
1790 "Non-nil means show inherited tags in each agenda line.
1792 When this option is set to 'always, it take precedences over
1793 `org-agenda-use-tag-inheritance' and inherited tags are shown
1794 in every agenda.
1796 When this option is set to t (the default), inherited tags are
1797 shown when they are available, i.e. when the value of
1798 `org-agenda-use-tag-inheritance' has been taken into account.
1800 This can be set to a list of agenda types in which the agenda
1801 must display the inherited tags. Available types are 'todo,
1802 'agenda, 'search and 'timeline.
1804 When set to nil, never show inherited tags in agenda lines."
1805 :group 'org-agenda-line-format
1806 :group 'org-agenda
1807 :version "24.3"
1808 :type '(choice
1809 (const :tag "Show inherited tags when available" t)
1810 (const :tag "Always show inherited tags" always)
1811 (repeat :tag "Show inherited tags only in selected agenda types"
1812 (symbol :tag "Agenda type"))))
1814 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1815 "List of agenda view types where to use tag inheritance.
1817 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1818 controlled by `org-use-tag-inheritance'. In other agenda types,
1819 `org-use-tag-inheritance' is not used for the selection of the
1820 agenda entries. Still, you may want the agenda to be aware of
1821 the inherited tags anyway, e.g. for later tag filtering.
1823 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1825 This variable has no effect if `org-agenda-show-inherited-tags'
1826 is set to 'always. In that case, the agenda is aware of those
1827 tags.
1829 The default value sets tags in every agenda type. Setting this
1830 option to nil will speed up non-tags agenda view a lot."
1831 :group 'org-agenda
1832 :version "24.3"
1833 :type '(choice
1834 (const :tag "Use tag inheritance in all agenda types" t)
1835 (repeat :tag "Use tag inheritance in selected agenda types"
1836 (symbol :tag "Agenda type"))))
1838 (defcustom org-agenda-hide-tags-regexp nil
1839 "Regular expression used to filter away specific tags in agenda views.
1840 This means that these tags will be present, but not be shown in the agenda
1841 line. Secondary filtering will still work on the hidden tags.
1842 Nil means don't hide any tags."
1843 :group 'org-agenda-line-format
1844 :type '(choice
1845 (const :tag "Hide none" nil)
1846 (string :tag "Regexp ")))
1848 (defcustom org-agenda-remove-tags nil
1849 "Non-nil means remove the tags from the headline copy in the agenda.
1850 When this is the symbol `prefix', only remove tags when
1851 `org-agenda-prefix-format' contains a `%T' specifier."
1852 :group 'org-agenda-line-format
1853 :type '(choice
1854 (const :tag "Always" t)
1855 (const :tag "Never" nil)
1856 (const :tag "When prefix format contains %T" prefix)))
1858 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1859 'org-agenda-remove-tags)
1861 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1862 "Shift tags in agenda items to this column.
1863 If this number is positive, it specifies the column. If it is negative,
1864 it means that the tags should be flushright to that column. For example,
1865 -80 works well for a normal 80 character screen."
1866 :group 'org-agenda-line-format
1867 :type 'integer)
1869 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1871 (defcustom org-agenda-fontify-priorities 'cookies
1872 "Non-nil means highlight low and high priorities in agenda.
1873 When t, the highest priority entries are bold, lowest priority italic.
1874 However, settings in `org-priority-faces' will overrule these faces.
1875 When this variable is the symbol `cookies', only fontify the
1876 cookies, not the entire task.
1877 This may also be an association list of priority faces, whose
1878 keys are the character values of `org-highest-priority',
1879 `org-default-priority', and `org-lowest-priority' (the default values
1880 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1881 color as a string, or a list like `(:background \"Red\")'.
1882 If it is a color, the variable `org-faces-easy-properties'
1883 determines if it is a foreground or a background color."
1884 :group 'org-agenda-line-format
1885 :type '(choice
1886 (const :tag "Never" nil)
1887 (const :tag "Defaults" t)
1888 (const :tag "Cookies only" cookies)
1889 (repeat :tag "Specify"
1890 (list (character :tag "Priority" :value ?A)
1891 (choice :tag "Face "
1892 (string :tag "Color")
1893 (sexp :tag "Face"))))))
1895 (defcustom org-agenda-day-face-function nil
1896 "Function called to determine what face should be used to display a day.
1897 The only argument passed to that function is the day. It should
1898 returns a face, or nil if does not want to specify a face and let
1899 the normal rules apply."
1900 :group 'org-agenda-line-format
1901 :version "24.1"
1902 :type '(choice (const nil) (function)))
1904 (defcustom org-agenda-category-icon-alist nil
1905 "Alist of category icon to be displayed in agenda views.
1907 Each entry should have the following format:
1909 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1911 Where CATEGORY-REGEXP is a regexp matching the categories where
1912 the icon should be displayed.
1913 FILE-OR-DATA either a file path or a string containing image data.
1915 The other fields can be omitted safely if not needed:
1916 TYPE indicates the image type.
1917 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1918 image data.
1919 PROPS are additional image attributes to assign to the image,
1920 like, e.g. `:ascent center'.
1922 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1924 If you want to set the display properties yourself, just put a
1925 list as second element:
1927 (CATEGORY-REGEXP (MY PROPERTY LIST))
1929 For example, to display a 16px horizontal space for Emacs
1930 category, you can use:
1932 (\"Emacs\" '(space . (:width (16))))"
1933 :group 'org-agenda-line-format
1934 :version "24.1"
1935 :type '(alist :key-type (string :tag "Regexp matching category")
1936 :value-type (choice (list :tag "Icon"
1937 (string :tag "File or data")
1938 (symbol :tag "Type")
1939 (boolean :tag "Data?")
1940 (repeat :tag "Extra image properties" :inline t symbol))
1941 (list :tag "Display properties" sexp))))
1943 (defgroup org-agenda-column-view nil
1944 "Options concerning column view in the agenda."
1945 :tag "Org Agenda Column View"
1946 :group 'org-agenda)
1948 (defcustom org-agenda-columns-show-summaries t
1949 "Non-nil means show summaries for columns displayed in the agenda view."
1950 :group 'org-agenda-column-view
1951 :type 'boolean)
1953 (defcustom org-agenda-columns-compute-summary-properties t
1954 "Non-nil means recompute all summary properties before column view.
1955 When column view in the agenda is listing properties that have a summary
1956 operator, it can go to all relevant buffers and recompute the summaries
1957 there. This can mean overhead for the agenda column view, but is necessary
1958 to have thing up to date.
1959 As a special case, a CLOCKSUM property also makes sure that the clock
1960 computations are current."
1961 :group 'org-agenda-column-view
1962 :type 'boolean)
1964 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1965 "Non-nil means the duration of an appointment will add to day effort.
1966 The property to which appointment durations will be added is the one given
1967 in the option `org-effort-property'. If an appointment does not have
1968 an end time, `org-agenda-default-appointment-duration' will be used. If that
1969 is not set, an appointment without end time will not contribute to the time
1970 estimate."
1971 :group 'org-agenda-column-view
1972 :type 'boolean)
1974 (defcustom org-agenda-auto-exclude-function nil
1975 "A function called with a tag to decide if it is filtered on '/ RET'.
1976 The sole argument to the function, which is called once for each
1977 possible tag, is a string giving the name of the tag. The
1978 function should return either nil if the tag should be included
1979 as normal, or \"-<TAG>\" to exclude the tag.
1980 Note that for the purpose of tag filtering, only the lower-case version of
1981 all tags will be considered, so that this function will only ever see
1982 the lower-case version of all tags."
1983 :group 'org-agenda
1984 :type '(choice (const nil) (function)))
1986 (defcustom org-agenda-bulk-custom-functions nil
1987 "Alist of characters and custom functions for bulk actions.
1988 For example, this value makes those two functions available:
1990 '((?R set-category)
1991 (?C bulk-cut))
1993 With selected entries in an agenda buffer, `B R' will call
1994 the custom function `set-category' on the selected entries.
1995 Note that functions in this alist don't need to be quoted."
1996 :type 'alist
1997 :version "24.1"
1998 :group 'org-agenda)
2000 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
2001 "Execute BODY with point at location given by `org-hd-marker' property.
2002 If STRING is non-nil, the text property will be fetched from position 0
2003 in that string. If STRING is nil, it will be fetched from the beginning
2004 of the current line."
2005 (org-with-gensyms (marker)
2006 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2007 'org-hd-marker ,string)))
2008 (with-current-buffer (marker-buffer ,marker)
2009 (save-excursion
2010 (goto-char ,marker)
2011 ,@body)))))
2012 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2014 (defun org-add-agenda-custom-command (entry)
2015 "Replace or add a command in `org-agenda-custom-commands'.
2016 This is mostly for hacking and trying a new command - once the command
2017 works you probably want to add it to `org-agenda-custom-commands' for good."
2018 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2019 (if ass
2020 (setcdr ass (cdr entry))
2021 (push entry org-agenda-custom-commands))))
2023 ;;; Define the org-agenda-mode
2025 (defvar org-agenda-mode-map (make-sparse-keymap)
2026 "Keymap for `org-agenda-mode'.")
2027 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2029 (defvar org-agenda-menu) ; defined later in this file.
2030 (defvar org-agenda-restrict nil) ; defined later in this file.
2031 (defvar org-agenda-follow-mode nil)
2032 (defvar org-agenda-entry-text-mode nil)
2033 (defvar org-agenda-clockreport-mode nil)
2034 (defvar org-agenda-show-log nil)
2035 (defvar org-agenda-redo-command nil)
2036 (defvar org-agenda-query-string nil)
2037 (defvar org-agenda-mode-hook nil
2038 "Hook run after `org-agenda-mode' is turned on.
2039 The buffer is still writable when this hook is called.")
2040 (defvar org-agenda-type nil)
2041 (defvar org-agenda-force-single-file nil)
2042 (defvar org-agenda-bulk-marked-entries nil
2043 "List of markers that refer to marked entries in the agenda.")
2045 ;;; Multiple agenda buffers support
2047 (defcustom org-agenda-sticky nil
2048 "Non-nil means agenda q key will bury agenda buffers.
2049 Agenda commands will then show existing buffer instead of generating new ones.
2050 When nil, `q' will kill the single agenda buffer."
2051 :group 'org-agenda
2052 :version "24.3"
2053 :type 'boolean)
2056 ;;;###autoload
2057 (defun org-toggle-sticky-agenda (&optional arg)
2058 "Toggle `org-agenda-sticky'."
2059 (interactive "P")
2060 (let ((new-value (if arg
2061 (> (prefix-numeric-value arg) 0)
2062 (not org-agenda-sticky))))
2063 (if (equal new-value org-agenda-sticky)
2064 (and (org-called-interactively-p 'interactive)
2065 (message "Sticky agenda was already %s"
2066 (if org-agenda-sticky "enabled" "disabled")))
2067 (setq org-agenda-sticky new-value)
2068 (org-agenda-kill-all-agenda-buffers)
2069 (and (org-called-interactively-p 'interactive)
2070 (message "Sticky agenda was %s"
2071 (if org-agenda-sticky "enabled" "disabled"))))))
2073 (defvar org-agenda-buffer nil
2074 "Agenda buffer currently being generated.")
2076 (defvar org-agenda-last-prefix-arg nil)
2077 (defvar org-agenda-this-buffer-name nil)
2078 (defvar org-agenda-doing-sticky-redo nil)
2079 (defvar org-agenda-this-buffer-is-sticky nil)
2081 (defconst org-agenda-local-vars
2082 '(org-agenda-this-buffer-name
2083 org-agenda-undo-list
2084 org-agenda-pending-undo-list
2085 org-agenda-follow-mode
2086 org-agenda-entry-text-mode
2087 org-agenda-clockreport-mode
2088 org-agenda-show-log
2089 org-agenda-redo-command
2090 org-agenda-query-string
2091 org-agenda-type
2092 org-agenda-bulk-marked-entries
2093 org-agenda-undo-has-started-in
2094 org-agenda-info
2095 org-agenda-pre-window-conf
2096 org-agenda-columns-active
2097 org-agenda-tag-filter
2098 org-agenda-category-filter
2099 org-agenda-top-headline-filter
2100 org-agenda-regexp-filter
2101 org-agenda-effort-filter
2102 org-agenda-markers
2103 org-agenda-last-search-view-search-was-boolean
2104 org-agenda-filtered-by-category
2105 org-agenda-filter-form
2106 org-agenda-cycle-counter
2107 org-agenda-last-prefix-arg)
2108 "Variables that must be local in agenda buffers to allow multiple buffers.")
2110 (defun org-agenda-mode ()
2111 "Mode for time-sorted view on action items in Org-mode files.
2113 The following commands are available:
2115 \\{org-agenda-mode-map}"
2116 (interactive)
2117 (cond (org-agenda-doing-sticky-redo
2118 ;; Refreshing sticky agenda-buffer
2120 ;; Preserve the value of `org-agenda-local-vars' variables,
2121 ;; while letting `kill-all-local-variables' kill the rest
2122 (let ((save (buffer-local-variables)))
2123 (kill-all-local-variables)
2124 (mapc 'make-local-variable org-agenda-local-vars)
2125 (dolist (elem save)
2126 (let ((var (car elem))
2127 (val (cdr elem)))
2128 (when (and val
2129 (member var org-agenda-local-vars))
2130 (set var val)))))
2131 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2132 (org-agenda-sticky
2133 ;; Creating a sticky Agenda buffer for the first time
2134 (kill-all-local-variables)
2135 (mapc 'make-local-variable org-agenda-local-vars)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2138 ;; Creating a non-sticky agenda buffer
2139 (kill-all-local-variables)
2140 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2141 (setq org-agenda-undo-list nil
2142 org-agenda-pending-undo-list nil
2143 org-agenda-bulk-marked-entries nil)
2144 (setq major-mode 'org-agenda-mode)
2145 ;; Keep global-font-lock-mode from turning on font-lock-mode
2146 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2147 (setq mode-name "Org-Agenda")
2148 (setq indent-tabs-mode nil)
2149 (use-local-map org-agenda-mode-map)
2150 (easy-menu-add org-agenda-menu)
2151 (if org-startup-truncated (setq truncate-lines t))
2152 (org-set-local 'line-move-visual nil)
2153 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2154 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2155 ;; Make sure properties are removed when copying text
2156 (org-add-hook 'filter-buffer-substring-functions
2157 (lambda (fun start end delete)
2158 (substring-no-properties (funcall fun start end delete)))
2159 nil t)
2160 (unless org-agenda-keep-modes
2161 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2162 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode))
2163 (setq org-agenda-show-log org-agenda-start-with-log-mode)
2164 (setq org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode)
2165 (add-to-invisibility-spec '(org-filtered))
2166 (add-to-invisibility-spec '(org-link))
2167 (easy-menu-change
2168 '("Agenda") "Agenda Files"
2169 (append
2170 (list
2171 (vector
2172 (if (get 'org-agenda-files 'org-restrict)
2173 "Restricted to single file"
2174 "Edit File List")
2175 '(org-edit-agenda-file-list)
2176 (not (get 'org-agenda-files 'org-restrict)))
2177 "--")
2178 (mapcar 'org-file-menu-entry (org-agenda-files))))
2179 (org-agenda-set-mode-name)
2180 (apply
2181 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2182 (list 'org-agenda-mode-hook)))
2184 (substitute-key-definition 'undo 'org-agenda-undo
2185 org-agenda-mode-map global-map)
2186 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2187 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2188 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2189 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2190 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2191 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2192 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2193 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2194 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2195 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2196 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2197 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2198 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2199 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2200 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2201 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2202 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2203 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2204 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2205 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2206 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2207 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2208 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2209 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2210 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2211 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2212 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2213 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2214 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2215 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2216 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2217 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2218 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2219 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2220 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2221 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2222 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2223 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2224 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2225 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2226 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2227 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2228 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2229 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2230 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2231 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2232 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2233 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2234 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2235 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2237 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2238 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2239 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2240 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2241 (while l (org-defkey org-agenda-mode-map
2242 (int-to-string (pop l)) 'digit-argument)))
2244 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2245 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2246 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2247 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2248 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2249 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2250 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2251 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2252 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2253 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2254 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2255 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2256 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2257 'org-clock-modify-effort-estimate)
2258 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2259 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2260 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2261 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2262 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2263 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2264 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2265 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2266 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2267 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2268 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2269 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2270 (substitute-key-definition 'next-line 'org-agenda-next-line
2271 org-agenda-mode-map global-map)
2272 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2273 org-agenda-mode-map global-map)
2274 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2275 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2276 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2277 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2278 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2279 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2280 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2281 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2282 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2283 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2284 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2285 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2286 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2287 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2288 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2289 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2290 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2291 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2293 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2294 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2295 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2296 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2297 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2298 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2299 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2300 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2301 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2302 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2303 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2305 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2306 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2307 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2308 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2309 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2310 (org-defkey org-agenda-mode-map "_" 'org-agenda-filter-by-effort)
2311 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2312 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2313 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2314 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2315 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2316 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2317 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2318 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2319 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2320 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2322 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2323 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2325 (define-key org-agenda-mode-map [remap forward-paragraph] 'org-agenda-forward-block)
2326 (define-key org-agenda-mode-map [remap backward-paragraph] 'org-agenda-backward-block)
2328 (when org-agenda-mouse-1-follows-link
2329 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2330 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2331 '("Agenda"
2332 ("Agenda Files")
2333 "--"
2334 ("Agenda Dates"
2335 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2336 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2337 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2338 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2339 "--"
2340 ("View"
2341 ["Day View" org-agenda-day-view
2342 :active (org-agenda-check-type nil 'agenda)
2343 :style radio :selected (eq org-agenda-current-span 'day)
2344 :keys "v d (or just d)"]
2345 ["Week View" org-agenda-week-view
2346 :active (org-agenda-check-type nil 'agenda)
2347 :style radio :selected (eq org-agenda-current-span 'week)
2348 :keys "v w"]
2349 ["Fortnight View" org-agenda-fortnight-view
2350 :active (org-agenda-check-type nil 'agenda)
2351 :style radio :selected (eq org-agenda-current-span 'fortnight)
2352 :keys "v f"]
2353 ["Month View" org-agenda-month-view
2354 :active (org-agenda-check-type nil 'agenda)
2355 :style radio :selected (eq org-agenda-current-span 'month)
2356 :keys "v m"]
2357 ["Year View" org-agenda-year-view
2358 :active (org-agenda-check-type nil 'agenda)
2359 :style radio :selected (eq org-agenda-current-span 'year)
2360 :keys "v y"]
2361 "--"
2362 ["Include Diary" org-agenda-toggle-diary
2363 :style toggle :selected org-agenda-include-diary
2364 :active (org-agenda-check-type nil 'agenda)]
2365 ["Include Deadlines" org-agenda-toggle-deadlines
2366 :style toggle :selected org-agenda-include-deadlines
2367 :active (org-agenda-check-type nil 'agenda)]
2368 ["Use Time Grid" org-agenda-toggle-time-grid
2369 :style toggle :selected org-agenda-use-time-grid
2370 :active (org-agenda-check-type nil 'agenda)]
2371 "--"
2372 ["Show clock report" org-agenda-clockreport-mode
2373 :style toggle :selected org-agenda-clockreport-mode
2374 :active (org-agenda-check-type nil 'agenda)]
2375 ["Show some entry text" org-agenda-entry-text-mode
2376 :style toggle :selected org-agenda-entry-text-mode
2377 :active t]
2378 "--"
2379 ["Show Logbook entries" org-agenda-log-mode
2380 :style toggle :selected org-agenda-show-log
2381 :active (org-agenda-check-type nil 'agenda 'timeline)
2382 :keys "v l (or just l)"]
2383 ["Include archived trees" org-agenda-archives-mode
2384 :style toggle :selected org-agenda-archives-mode :active t
2385 :keys "v a"]
2386 ["Include archive files" (org-agenda-archives-mode t)
2387 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2388 :keys "v A"]
2389 "--"
2390 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2391 ["Write view to file" org-agenda-write t]
2392 ["Rebuild buffer" org-agenda-redo t]
2393 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2394 "--"
2395 ["Show original entry" org-agenda-show t]
2396 ["Go To (other window)" org-agenda-goto t]
2397 ["Go To (this window)" org-agenda-switch-to t]
2398 ["Capture with cursor date" org-agenda-capture t]
2399 ["Follow Mode" org-agenda-follow-mode
2400 :style toggle :selected org-agenda-follow-mode :active t]
2401 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2402 "--"
2403 ("TODO"
2404 ["Cycle TODO" org-agenda-todo t]
2405 ["Next TODO set" org-agenda-todo-nextset t]
2406 ["Previous TODO set" org-agenda-todo-previousset t]
2407 ["Add note" org-agenda-add-note t])
2408 ("Archive/Refile/Delete"
2409 ["Archive default" org-agenda-archive-default t]
2410 ["Archive default" org-agenda-archive-default-with-confirmation t]
2411 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2412 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2413 ["Archive subtree" org-agenda-archive t]
2414 "--"
2415 ["Refile" org-agenda-refile t]
2416 "--"
2417 ["Delete subtree" org-agenda-kill t])
2418 ("Bulk action"
2419 ["Mark entry" org-agenda-bulk-mark t]
2420 ["Mark all" org-agenda-bulk-mark-all t]
2421 ["Unmark entry" org-agenda-bulk-unmark t]
2422 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2423 ["Toggle mark" org-agenda-bulk-toggle t]
2424 ["Toggle all" org-agenda-bulk-toggle-all t]
2425 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2426 ["Act on all marked" org-agenda-bulk-action t]
2427 "--"
2428 ("Tags and Properties"
2429 ["Show all Tags" org-agenda-show-tags t]
2430 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2431 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2432 "--"
2433 ["Column View" org-columns t])
2434 ("Deadline/Schedule"
2435 ["Schedule" org-agenda-schedule t]
2436 ["Set Deadline" org-agenda-deadline t]
2437 "--"
2438 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2439 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2440 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2441 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2442 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2443 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2444 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2445 ("Clock and Effort"
2446 ["Clock in" org-agenda-clock-in t]
2447 ["Clock out" org-agenda-clock-out t]
2448 ["Clock cancel" org-agenda-clock-cancel t]
2449 ["Goto running clock" org-clock-goto t]
2450 "--"
2451 ["Set Effort" org-agenda-set-effort t]
2452 ["Change clocked effort" org-clock-modify-effort-estimate
2453 (org-clock-is-active)])
2454 ("Priority"
2455 ["Set Priority" org-agenda-priority t]
2456 ["Increase Priority" org-agenda-priority-up t]
2457 ["Decrease Priority" org-agenda-priority-down t]
2458 ["Show Priority" org-show-priority t])
2459 ("Calendar/Diary"
2460 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2461 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2462 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2463 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2464 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2465 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2466 "--"
2467 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2468 "--"
2469 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2470 "--"
2471 ("MobileOrg"
2472 ["Push Files and Views" org-mobile-push t]
2473 ["Get Captured and Flagged" org-mobile-pull t]
2474 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2475 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2476 "--"
2477 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2478 "--"
2479 ["Quit" org-agenda-quit t]
2480 ["Exit and Release Buffers" org-agenda-exit t]
2483 ;;; Agenda undo
2485 (defvar org-agenda-allow-remote-undo t
2486 "Non-nil means allow remote undo from the agenda buffer.")
2487 (defvar org-agenda-undo-has-started-in nil
2488 "Buffers that have already seen `undo-start' in the current undo sequence.")
2490 (defun org-agenda-undo ()
2491 "Undo a remote editing step in the agenda.
2492 This undoes changes both in the agenda buffer and in the remote buffer
2493 that have been changed along."
2494 (interactive)
2495 (or org-agenda-allow-remote-undo
2496 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2497 (if (not (eq this-command last-command))
2498 (setq org-agenda-undo-has-started-in nil
2499 org-agenda-pending-undo-list org-agenda-undo-list))
2500 (if (not org-agenda-pending-undo-list)
2501 (user-error "No further undo information"))
2502 (let* ((entry (pop org-agenda-pending-undo-list))
2503 buf line cmd rembuf)
2504 (setq cmd (pop entry) line (pop entry))
2505 (setq rembuf (nth 2 entry))
2506 (org-with-remote-undo rembuf
2507 (while (bufferp (setq buf (pop entry)))
2508 (if (pop entry)
2509 (with-current-buffer buf
2510 (let ((last-undo-buffer buf)
2511 (inhibit-read-only t))
2512 (unless (memq buf org-agenda-undo-has-started-in)
2513 (push buf org-agenda-undo-has-started-in)
2514 (make-local-variable 'pending-undo-list)
2515 (undo-start))
2516 (while (and pending-undo-list
2517 (listp pending-undo-list)
2518 (not (car pending-undo-list)))
2519 (pop pending-undo-list))
2520 (undo-more 1))))))
2521 (org-goto-line line)
2522 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2524 (defun org-verify-change-for-undo (l1 l2)
2525 "Verify that a real change occurred between the undo lists L1 and L2."
2526 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2527 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2528 (not (eq l1 l2)))
2530 ;;; Agenda dispatch
2532 (defvar org-agenda-restrict-begin (make-marker))
2533 (defvar org-agenda-restrict-end (make-marker))
2534 (defvar org-agenda-last-dispatch-buffer nil)
2535 (defvar org-agenda-overriding-restriction nil)
2537 (defcustom org-agenda-custom-commands-contexts nil
2538 "Alist of custom agenda keys and contextual rules.
2540 For example, if you have a custom agenda command \"p\" and you
2541 want this command to be accessible only from plain text files,
2542 use this:
2544 '((\"p\" ((in-file . \"\\\\.txt\\\\'\"))))
2546 Here are the available contexts definitions:
2548 in-file: command displayed only in matching files
2549 in-mode: command displayed only in matching modes
2550 not-in-file: command not displayed in matching files
2551 not-in-mode: command not displayed in matching modes
2552 in-buffer: command displayed only in matching buffers
2553 not-in-buffer: command not displayed in matching buffers
2554 [function]: a custom function taking no argument
2556 If you define several checks, the agenda command will be
2557 accessible if there is at least one valid check.
2559 You can also bind a key to another agenda custom command
2560 depending on contextual rules.
2562 '((\"p\" \"q\" ((in-file . \"\\\\.txt\\\\'\"))))
2564 Here it means: in .txt files, use \"p\" as the key for the
2565 agenda command otherwise associated with \"q\". (The command
2566 originally associated with \"q\" is not displayed to avoid
2567 duplicates.)"
2568 :version "24.3"
2569 :group 'org-agenda-custom-commands
2570 :type '(repeat (list :tag "Rule"
2571 (string :tag " Agenda key")
2572 (string :tag "Replace by command")
2573 (repeat :tag "Available when"
2574 (choice
2575 (cons :tag "Condition"
2576 (choice
2577 (const :tag "In file" in-file)
2578 (const :tag "Not in file" not-in-file)
2579 (const :tag "In buffer" in-buffer)
2580 (const :tag "Not in buffer" not-in-buffer)
2581 (const :tag "In mode" in-mode)
2582 (const :tag "Not in mode" not-in-mode))
2583 (regexp))
2584 (function :tag "Custom function"))))))
2586 (defcustom org-agenda-max-entries nil
2587 "Maximum number of entries to display in an agenda.
2588 This can be nil (no limit) or an integer or an alist of agenda
2589 types with an associated number of entries to display in this
2590 type."
2591 :version "24.4"
2592 :package-version '(Org . "8.0")
2593 :group 'org-agenda-custom-commands
2594 :type '(choice (symbol :tag "No limit" nil)
2595 (integer :tag "Max number of entries")
2596 (repeat
2597 (cons (choice :tag "Agenda type"
2598 (const agenda)
2599 (const todo)
2600 (const tags)
2601 (const search)
2602 (const timeline))
2603 (integer :tag "Max number of entries")))))
2605 (defcustom org-agenda-max-todos nil
2606 "Maximum number of TODOs to display in an agenda.
2607 This can be nil (no limit) or an integer or an alist of agenda
2608 types with an associated number of entries to display in this
2609 type."
2610 :version "24.4"
2611 :package-version '(Org . "8.0")
2612 :group 'org-agenda-custom-commands
2613 :type '(choice (symbol :tag "No limit" nil)
2614 (integer :tag "Max number of TODOs")
2615 (repeat
2616 (cons (choice :tag "Agenda type"
2617 (const agenda)
2618 (const todo)
2619 (const tags)
2620 (const search)
2621 (const timeline))
2622 (integer :tag "Max number of TODOs")))))
2624 (defcustom org-agenda-max-tags nil
2625 "Maximum number of tagged entries to display in an agenda.
2626 This can be nil (no limit) or an integer or an alist of agenda
2627 types with an associated number of entries to display in this
2628 type."
2629 :version "24.4"
2630 :package-version '(Org . "8.0")
2631 :group 'org-agenda-custom-commands
2632 :type '(choice (symbol :tag "No limit" nil)
2633 (integer :tag "Max number of tagged entries")
2634 (repeat
2635 (cons (choice :tag "Agenda type"
2636 (const agenda)
2637 (const todo)
2638 (const tags)
2639 (const search)
2640 (const timeline))
2641 (integer :tag "Max number of tagged entries")))))
2643 (defcustom org-agenda-max-effort nil
2644 "Maximum cumulated effort duration for the agenda.
2645 This can be nil (no limit) or a number of minutes (as an integer)
2646 or an alist of agenda types with an associated number of minutes
2647 to limit entries to in this type."
2648 :version "24.4"
2649 :package-version '(Org . "8.0")
2650 :group 'org-agenda-custom-commands
2651 :type '(choice (symbol :tag "No limit" nil)
2652 (integer :tag "Max number of minutes")
2653 (repeat
2654 (cons (choice :tag "Agenda type"
2655 (const agenda)
2656 (const todo)
2657 (const tags)
2658 (const search)
2659 (const timeline))
2660 (integer :tag "Max number of minutes")))))
2662 (defvar org-keys nil)
2663 (defvar org-match nil)
2664 ;;;###autoload
2665 (defun org-agenda (&optional arg org-keys restriction)
2666 "Dispatch agenda commands to collect entries to the agenda buffer.
2667 Prompts for a command to execute. Any prefix arg will be passed
2668 on to the selected command. The default selections are:
2670 a Call `org-agenda-list' to display the agenda for current day or week.
2671 t Call `org-todo-list' to display the global todo list.
2672 T Call `org-todo-list' to display the global todo list, select only
2673 entries with a specific TODO keyword (the user gets a prompt).
2674 m Call `org-tags-view' to display headlines with tags matching
2675 a condition (the user is prompted for the condition).
2676 M Like `m', but select only TODO entries, no ordinary headlines.
2677 L Create a timeline for the current buffer.
2678 e Export views to associated files.
2679 s Search entries for keywords.
2680 S Search entries for keywords, only with TODO keywords.
2681 / Multi occur across all agenda files and also files listed
2682 in `org-agenda-text-search-extra-files'.
2683 < Restrict agenda commands to buffer, subtree, or region.
2684 Press several times to get the desired effect.
2685 > Remove a previous restriction.
2686 # List \"stuck\" projects.
2687 ! Configure what \"stuck\" means.
2688 C Configure custom agenda commands.
2690 More commands can be added by configuring the variable
2691 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2692 searches can be pre-defined in this way.
2694 If the current buffer is in Org-mode and visiting a file, you can also
2695 first press `<' once to indicate that the agenda should be temporarily
2696 \(until the next use of \\[org-agenda]) restricted to the current file.
2697 Pressing `<' twice means to restrict to the current subtree or region
2698 \(if active)."
2699 (interactive "P")
2700 (catch 'exit
2701 (let* ((prefix-descriptions nil)
2702 (org-agenda-buffer-name org-agenda-buffer-name)
2703 (org-agenda-window-setup (if (equal (buffer-name)
2704 org-agenda-buffer-name)
2705 'current-window
2706 org-agenda-window-setup))
2707 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2708 (org-agenda-custom-commands
2709 ;; normalize different versions
2710 (delq nil
2711 (mapcar
2712 (lambda (x)
2713 (cond ((stringp (cdr x))
2714 (push x prefix-descriptions)
2715 nil)
2716 ((stringp (nth 1 x)) x)
2717 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2718 (t (cons (car x) (cons "" (cdr x))))))
2719 org-agenda-custom-commands)))
2720 (org-agenda-custom-commands
2721 (org-contextualize-keys
2722 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2723 (buf (current-buffer))
2724 (bfn (buffer-file-name (buffer-base-buffer)))
2725 entry key type org-match lprops ans)
2726 ;; Turn off restriction unless there is an overriding one,
2727 (unless org-agenda-overriding-restriction
2728 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2729 ;; There is a request to keep the file list in place
2730 (put 'org-agenda-files 'org-restrict nil))
2731 (setq org-agenda-restrict nil)
2732 (move-marker org-agenda-restrict-begin nil)
2733 (move-marker org-agenda-restrict-end nil))
2734 ;; Delete old local properties
2735 (put 'org-agenda-redo-command 'org-lprops nil)
2736 ;; Delete previously set last-arguments
2737 (put 'org-agenda-redo-command 'last-args nil)
2738 ;; Remember where this call originated
2739 (setq org-agenda-last-dispatch-buffer (current-buffer))
2740 (unless org-keys
2741 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2742 org-keys (car ans)
2743 restriction (cdr ans)))
2744 ;; If we have sticky agenda buffers, set a name for the buffer,
2745 ;; depending on the invoking keys. The user may still set this
2746 ;; as a command option, which will overwrite what we do here.
2747 (if org-agenda-sticky
2748 (setq org-agenda-buffer-name
2749 (format "*Org Agenda(%s)*" org-keys)))
2750 ;; Establish the restriction, if any
2751 (when (and (not org-agenda-overriding-restriction) restriction)
2752 (put 'org-agenda-files 'org-restrict (list bfn))
2753 (cond
2754 ((eq restriction 'region)
2755 (setq org-agenda-restrict (current-buffer))
2756 (move-marker org-agenda-restrict-begin (region-beginning))
2757 (move-marker org-agenda-restrict-end (region-end)))
2758 ((eq restriction 'subtree)
2759 (save-excursion
2760 (setq org-agenda-restrict (current-buffer))
2761 (org-back-to-heading t)
2762 (move-marker org-agenda-restrict-begin (point))
2763 (move-marker org-agenda-restrict-end
2764 (progn (org-end-of-subtree t)))))))
2766 ;; For example the todo list should not need it (but does...)
2767 (cond
2768 ((setq entry (assoc org-keys org-agenda-custom-commands))
2769 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2770 (progn
2771 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2772 lprops (nth 4 entry))
2773 (if org-agenda-sticky
2774 (setq org-agenda-buffer-name
2775 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2776 (format "*Org Agenda(%s)*" org-keys))))
2777 (put 'org-agenda-redo-command 'org-lprops lprops)
2778 (cond
2779 ((eq type 'agenda)
2780 (org-let lprops '(org-agenda-list current-prefix-arg)))
2781 ((eq type 'agenda*)
2782 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2783 ((eq type 'alltodo)
2784 (org-let lprops '(org-todo-list current-prefix-arg)))
2785 ((eq type 'search)
2786 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2787 ((eq type 'stuck)
2788 (org-let lprops '(org-agenda-list-stuck-projects
2789 current-prefix-arg)))
2790 ((eq type 'tags)
2791 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2792 ((eq type 'tags-todo)
2793 (org-let lprops '(org-tags-view '(4) org-match)))
2794 ((eq type 'todo)
2795 (org-let lprops '(org-todo-list org-match)))
2796 ((eq type 'tags-tree)
2797 (org-check-for-org-mode)
2798 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2799 ((eq type 'todo-tree)
2800 (org-check-for-org-mode)
2801 (org-let lprops
2802 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2803 (regexp-quote org-match) "\\>"))))
2804 ((eq type 'occur-tree)
2805 (org-check-for-org-mode)
2806 (org-let lprops '(org-occur org-match)))
2807 ((functionp type)
2808 (org-let lprops '(funcall type org-match)))
2809 ((fboundp type)
2810 (org-let lprops '(funcall type org-match)))
2811 (t (user-error "Invalid custom agenda command type %s" type))))
2812 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2813 ((equal org-keys "C")
2814 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2815 (customize-variable 'org-agenda-custom-commands))
2816 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2817 ((equal org-keys "s") (call-interactively 'org-search-view))
2818 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2819 ((equal org-keys "t") (call-interactively 'org-todo-list))
2820 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2821 ((equal org-keys "m") (call-interactively 'org-tags-view))
2822 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2823 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2824 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2825 (org-add-hook
2826 'post-command-hook
2827 (lambda ()
2828 (unless (current-message)
2829 (let* ((m (org-agenda-get-any-marker))
2830 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2831 (when note
2832 (message (concat
2833 "FLAGGING-NOTE ([?] for more info): "
2834 (org-add-props
2835 (replace-regexp-in-string
2836 "\\\\n" "//"
2837 (copy-sequence note))
2838 nil 'face 'org-warning)))))))
2839 t t))
2840 ((equal org-keys "L")
2841 (unless (derived-mode-p 'org-mode)
2842 (user-error "This is not an Org-mode file"))
2843 (unless restriction
2844 (put 'org-agenda-files 'org-restrict (list bfn))
2845 (org-call-with-arg 'org-timeline arg)))
2846 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2847 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2848 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2849 (t (user-error "Invalid agenda key"))))))
2851 (defvar org-agenda-multi)
2853 (defun org-agenda-append-agenda ()
2854 "Append another agenda view to the current one.
2855 This function allows interactive building of block agendas.
2856 Agenda views are separated by `org-agenda-block-separator'."
2857 (interactive)
2858 (unless (derived-mode-p 'org-agenda-mode)
2859 (user-error "Can only append from within agenda buffer"))
2860 (let ((org-agenda-multi t))
2861 (org-agenda)
2862 (widen)
2863 (org-agenda-finalize)
2864 (setq buffer-read-only t)
2865 (org-agenda-fit-window-to-buffer)))
2867 (defun org-agenda-normalize-custom-commands (cmds)
2868 "Normalize custom commands CMDS."
2869 (delq nil
2870 (mapcar
2871 (lambda (x)
2872 (cond ((stringp (cdr x)) nil)
2873 ((stringp (nth 1 x)) x)
2874 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2875 (t (cons (car x) (cons "" (cdr x))))))
2876 cmds)))
2878 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2879 "The user interface for selecting an agenda command."
2880 (catch 'exit
2881 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2882 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2883 (region-p (org-region-active-p))
2884 (custom org-agenda-custom-commands)
2885 (selstring "")
2886 restriction second-time
2887 c entry key type match prefixes rmheader header-end custom1 desc
2888 line lines left right n n1)
2889 (save-window-excursion
2890 (delete-other-windows)
2891 (org-switch-to-buffer-other-window " *Agenda Commands*")
2892 (erase-buffer)
2893 (insert (eval-when-compile
2894 (let ((header
2895 "Press key for an agenda command: < Buffer, subtree/region restriction
2896 -------------------------------- > Remove restriction
2897 a Agenda for current week or day e Export agenda views
2898 t List of all TODO entries T Entries with special TODO kwd
2899 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2900 s Search for keywords S Like s, but only TODO entries
2901 L Timeline for current buffer # List stuck projects (!=configure)
2902 / Multi-occur C Configure custom agenda commands
2903 ? Find :FLAGGED: entries * Toggle sticky agenda views
2905 (start 0))
2906 (while (string-match
2907 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2908 header start)
2909 (setq start (match-end 0))
2910 (add-text-properties (match-beginning 2) (match-end 2)
2911 '(face bold) header))
2912 header)))
2913 (setq header-end (point-marker))
2914 (while t
2915 (setq custom1 custom)
2916 (when (eq rmheader t)
2917 (org-goto-line 1)
2918 (re-search-forward ":" nil t)
2919 (delete-region (match-end 0) (point-at-eol))
2920 (forward-char 1)
2921 (looking-at "-+")
2922 (delete-region (match-end 0) (point-at-eol))
2923 (move-marker header-end (match-end 0)))
2924 (goto-char header-end)
2925 (delete-region (point) (point-max))
2927 ;; Produce all the lines that describe custom commands and prefixes
2928 (setq lines nil)
2929 (while (setq entry (pop custom1))
2930 (setq key (car entry) desc (nth 1 entry)
2931 type (nth 2 entry)
2932 match (nth 3 entry))
2933 (if (> (length key) 1)
2934 (add-to-list 'prefixes (string-to-char key))
2935 (setq line
2936 (format
2937 "%-4s%-14s"
2938 (org-add-props (copy-sequence key)
2939 '(face bold))
2940 (cond
2941 ((string-match "\\S-" desc) desc)
2942 ((eq type 'agenda) "Agenda for current week or day")
2943 ((eq type 'agenda*) "Appointments for current week or day")
2944 ((eq type 'alltodo) "List of all TODO entries")
2945 ((eq type 'search) "Word search")
2946 ((eq type 'stuck) "List of stuck projects")
2947 ((eq type 'todo) "TODO keyword")
2948 ((eq type 'tags) "Tags query")
2949 ((eq type 'tags-todo) "Tags (TODO)")
2950 ((eq type 'tags-tree) "Tags tree")
2951 ((eq type 'todo-tree) "TODO kwd tree")
2952 ((eq type 'occur-tree) "Occur tree")
2953 ((functionp type) (if (symbolp type)
2954 (symbol-name type)
2955 "Lambda expression"))
2956 (t "???"))))
2957 (if org-agenda-menu-show-matcher
2958 (setq line
2959 (concat line ": "
2960 (cond
2961 ((stringp match)
2962 (setq match (copy-sequence match))
2963 (org-add-props match nil 'face 'org-warning))
2964 ((listp type)
2965 (format "set of %d commands" (length type))))))
2966 (if (org-string-nw-p match)
2967 (add-text-properties
2968 0 (length line) (list 'help-echo
2969 (concat "Matcher: " match)) line)))
2970 (push line lines)))
2971 (setq lines (nreverse lines))
2972 (when prefixes
2973 (mapc (lambda (x)
2974 (push
2975 (format "%s %s"
2976 (org-add-props (char-to-string x)
2977 nil 'face 'bold)
2978 (or (cdr (assoc (concat selstring
2979 (char-to-string x))
2980 prefix-descriptions))
2981 "Prefix key"))
2982 lines))
2983 prefixes))
2985 ;; Check if we should display in two columns
2986 (if org-agenda-menu-two-columns
2987 (progn
2988 (setq n (length lines)
2989 n1 (+ (/ n 2) (mod n 2))
2990 right (nthcdr n1 lines)
2991 left (copy-sequence lines))
2992 (setcdr (nthcdr (1- n1) left) nil))
2993 (setq left lines right nil))
2994 (while left
2995 (insert "\n" (pop left))
2996 (when right
2997 (if (< (current-column) 40)
2998 (move-to-column 40 t)
2999 (insert " "))
3000 (insert (pop right))))
3002 ;; Make the window the right size
3003 (goto-char (point-min))
3004 (if second-time
3005 (if (not (pos-visible-in-window-p (point-max)))
3006 (org-fit-window-to-buffer))
3007 (setq second-time t)
3008 (org-fit-window-to-buffer))
3010 ;; Ask for selection
3011 (message "Press key for agenda command%s:"
3012 (if (or restrict-ok org-agenda-overriding-restriction)
3013 (if org-agenda-overriding-restriction
3014 " (restriction lock active)"
3015 (if restriction
3016 (format " (restricted to %s)" restriction)
3017 " (unrestricted)"))
3018 ""))
3019 (setq c (read-char-exclusive))
3020 (message "")
3021 (cond
3022 ((assoc (char-to-string c) custom)
3023 (setq selstring (concat selstring (char-to-string c)))
3024 (throw 'exit (cons selstring restriction)))
3025 ((memq c prefixes)
3026 (setq selstring (concat selstring (char-to-string c))
3027 prefixes nil
3028 rmheader (or rmheader t)
3029 custom (delq nil (mapcar
3030 (lambda (x)
3031 (if (or (= (length (car x)) 1)
3032 (/= (string-to-char (car x)) c))
3034 (cons (substring (car x) 1) (cdr x))))
3035 custom))))
3036 ((eq c ?*)
3037 (call-interactively 'org-toggle-sticky-agenda)
3038 (sit-for 2))
3039 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3040 (message "Restriction is only possible in Org-mode buffers")
3041 (ding) (sit-for 1))
3042 ((eq c ?1)
3043 (org-agenda-remove-restriction-lock 'noupdate)
3044 (setq restriction 'buffer))
3045 ((eq c ?0)
3046 (org-agenda-remove-restriction-lock 'noupdate)
3047 (setq restriction (if region-p 'region 'subtree)))
3048 ((eq c ?<)
3049 (org-agenda-remove-restriction-lock 'noupdate)
3050 (setq restriction
3051 (cond
3052 ((eq restriction 'buffer)
3053 (if region-p 'region 'subtree))
3054 ((memq restriction '(subtree region))
3055 nil)
3056 (t 'buffer))))
3057 ((eq c ?>)
3058 (org-agenda-remove-restriction-lock 'noupdate)
3059 (setq restriction nil))
3060 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3061 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3062 ((and (> (length selstring) 0) (eq c ?\d))
3063 (delete-window)
3064 (org-agenda-get-restriction-and-command prefix-descriptions))
3066 ((equal c ?q) (error "Abort"))
3067 (t (user-error "Invalid key %c" c))))))))
3069 (defun org-agenda-fit-window-to-buffer ()
3070 "Fit the window to the buffer size."
3071 (and (memq org-agenda-window-setup '(reorganize-frame))
3072 (fboundp 'fit-window-to-buffer)
3073 (if (and (= (cdr org-agenda-window-frame-fractions) 1.0)
3074 (= (car org-agenda-window-frame-fractions) 1.0))
3075 (delete-other-windows)
3076 (org-fit-window-to-buffer
3078 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3079 (floor (* (frame-height) (car org-agenda-window-frame-fractions)))))))
3081 (defvar org-cmd nil)
3082 (defvar org-agenda-overriding-cmd nil)
3083 (defvar org-agenda-overriding-arguments nil)
3084 (defvar org-agenda-overriding-cmd-arguments nil)
3085 (defun org-agenda-run-series (name series)
3086 "Run agenda NAME as a SERIES of agenda commands."
3087 (org-let (nth 1 series) '(org-agenda-prepare name))
3088 ;; We need to reset agenda markers here, because when constructing a
3089 ;; block agenda, the individual blocks do not do that.
3090 (org-agenda-reset-markers)
3091 (let* ((org-agenda-multi t)
3092 (redo (list 'org-agenda-run-series name (list 'quote series)))
3093 (cmds (car series))
3094 (gprops (nth 1 series))
3095 match ;; The byte compiler incorrectly complains about this. Keep it!
3096 org-cmd type lprops)
3097 (while (setq org-cmd (pop cmds))
3098 (setq type (car org-cmd)
3099 match (eval (nth 1 org-cmd))
3100 lprops (nth 2 org-cmd))
3101 (let ((org-agenda-overriding-arguments
3102 (if (eq org-agenda-overriding-cmd org-cmd)
3103 (or org-agenda-overriding-arguments
3104 org-agenda-overriding-cmd-arguments))))
3105 (cond
3106 ((eq type 'agenda)
3107 (org-let2 gprops lprops
3108 '(call-interactively 'org-agenda-list)))
3109 ((eq type 'agenda*)
3110 (org-let2 gprops lprops
3111 '(funcall 'org-agenda-list nil nil t)))
3112 ((eq type 'alltodo)
3113 (org-let2 gprops lprops
3114 '(call-interactively 'org-todo-list)))
3115 ((eq type 'search)
3116 (org-let2 gprops lprops
3117 '(org-search-view current-prefix-arg match nil)))
3118 ((eq type 'stuck)
3119 (org-let2 gprops lprops
3120 '(call-interactively 'org-agenda-list-stuck-projects)))
3121 ((eq type 'tags)
3122 (org-let2 gprops lprops
3123 '(org-tags-view current-prefix-arg match)))
3124 ((eq type 'tags-todo)
3125 (org-let2 gprops lprops
3126 '(org-tags-view '(4) match)))
3127 ((eq type 'todo)
3128 (org-let2 gprops lprops
3129 '(org-todo-list match)))
3130 ((fboundp type)
3131 (org-let2 gprops lprops
3132 '(funcall type match)))
3133 (t (error "Invalid type in command series")))))
3134 (widen)
3135 (let ((inhibit-read-only t))
3136 (add-text-properties (point-min) (point-max)
3137 `(org-series t org-series-redo-cmd ,redo)))
3138 (setq org-agenda-redo-command redo)
3139 (goto-char (point-min)))
3140 (org-agenda-fit-window-to-buffer)
3141 (org-let (nth 1 series) '(org-agenda-finalize)))
3143 ;;;###autoload
3144 (defmacro org-batch-agenda (cmd-key &rest parameters)
3145 "Run an agenda command in batch mode and send the result to STDOUT.
3146 If CMD-KEY is a string of length 1, it is used as a key in
3147 `org-agenda-custom-commands' and triggers this command. If it is a
3148 longer string it is used as a tags/todo match string.
3149 Parameters are alternating variable names and values that will be bound
3150 before running the agenda command."
3151 (org-eval-in-environment (org-make-parameter-alist parameters)
3152 (let (org-agenda-sticky)
3153 (if (> (length cmd-key) 2)
3154 (org-tags-view nil cmd-key)
3155 (org-agenda nil cmd-key))))
3156 (set-buffer org-agenda-buffer-name)
3157 (princ (buffer-string)))
3159 (defvar org-agenda-info nil)
3161 ;;;###autoload
3162 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3163 "Run an agenda command in batch mode and send the result to STDOUT.
3164 If CMD-KEY is a string of length 1, it is used as a key in
3165 `org-agenda-custom-commands' and triggers this command. If it is a
3166 longer string it is used as a tags/todo match string.
3167 Parameters are alternating variable names and values that will be bound
3168 before running the agenda command.
3170 The output gives a line for each selected agenda item. Each
3171 item is a list of comma-separated values, like this:
3173 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3175 category The category of the item
3176 head The headline, without TODO kwd, TAGS and PRIORITY
3177 type The type of the agenda entry, can be
3178 todo selected in TODO match
3179 tagsmatch selected in tags match
3180 diary imported from diary
3181 deadline a deadline on given date
3182 scheduled scheduled on given date
3183 timestamp entry has timestamp on given date
3184 closed entry was closed on given date
3185 upcoming-deadline warning about deadline
3186 past-scheduled forwarded scheduled item
3187 block entry has date block including g. date
3188 todo The todo keyword, if any
3189 tags All tags including inherited ones, separated by colons
3190 date The relevant date, like 2007-2-14
3191 time The time, like 15:00-16:50
3192 extra Sting with extra planning info
3193 priority-l The priority letter if any was given
3194 priority-n The computed numerical priority
3195 agenda-day The day in the agenda where this is listed"
3196 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3197 (org-make-parameter-alist parameters))
3198 (if (> (length cmd-key) 2)
3199 (org-tags-view nil cmd-key)
3200 (org-agenda nil cmd-key)))
3201 (set-buffer org-agenda-buffer-name)
3202 (let* ((lines (org-split-string (buffer-string) "\n"))
3203 line)
3204 (while (setq line (pop lines))
3205 (catch 'next
3206 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3207 (setq org-agenda-info
3208 (org-fix-agenda-info (text-properties-at 0 line)))
3209 (princ
3210 (mapconcat 'org-agenda-export-csv-mapper
3211 '(org-category txt type todo tags date time extra
3212 priority-letter priority agenda-day)
3213 ","))
3214 (princ "\n")))))
3216 (defun org-fix-agenda-info (props)
3217 "Make sure all properties on an agenda item have a canonical form.
3218 This ensures the export commands can easily use it."
3219 (let (tmp re)
3220 (when (setq tmp (plist-get props 'tags))
3221 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3222 (when (setq tmp (plist-get props 'date))
3223 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3224 (let ((calendar-date-display-form '(year "-" month "-" day)))
3225 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3227 (setq tmp (calendar-date-string tmp)))
3228 (setq props (plist-put props 'date tmp)))
3229 (when (setq tmp (plist-get props 'day))
3230 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3231 (let ((calendar-date-display-form '(year "-" month "-" day)))
3232 (setq tmp (calendar-date-string tmp)))
3233 (setq props (plist-put props 'day tmp))
3234 (setq props (plist-put props 'agenda-day tmp)))
3235 (when (setq tmp (plist-get props 'txt))
3236 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3237 (plist-put props 'priority-letter (match-string 1 tmp))
3238 (setq tmp (replace-match "" t t tmp)))
3239 (when (and (setq re (plist-get props 'org-todo-regexp))
3240 (setq re (concat "\\`\\.*" re " ?"))
3241 (string-match re tmp))
3242 (plist-put props 'todo (match-string 1 tmp))
3243 (setq tmp (replace-match "" t t tmp)))
3244 (plist-put props 'txt tmp)))
3245 props)
3247 (defun org-agenda-export-csv-mapper (prop)
3248 (let ((res (plist-get org-agenda-info prop)))
3249 (setq res
3250 (cond
3251 ((not res) "")
3252 ((stringp res) res)
3253 (t (prin1-to-string res))))
3254 (while (string-match "," res)
3255 (setq res (replace-match ";" t t res)))
3256 (org-trim res)))
3258 ;;;###autoload
3259 (defun org-store-agenda-views (&rest parameters)
3260 "Store agenda views."
3261 (interactive)
3262 (eval (list 'org-batch-store-agenda-views)))
3264 ;;;###autoload
3265 (defmacro org-batch-store-agenda-views (&rest parameters)
3266 "Run all custom agenda commands that have a file argument."
3267 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3268 (pop-up-frames nil)
3269 (dir default-directory)
3270 (pars (org-make-parameter-alist parameters))
3271 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3272 (save-window-excursion
3273 (while cmds
3274 (setq cmd (pop cmds)
3275 thiscmdkey (car cmd)
3276 thiscmdcmd (cdr cmd)
3277 match (nth 2 thiscmdcmd)
3278 bufname (if org-agenda-sticky
3279 (or (and (stringp match)
3280 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3281 (format "*Org Agenda(%s)*" thiscmdkey))
3282 org-agenda-buffer-name)
3283 cmd-or-set (nth 2 cmd)
3284 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3285 files (nth (if (listp cmd-or-set) 4 5) cmd))
3286 (if (stringp files) (setq files (list files)))
3287 (when files
3288 (org-eval-in-environment (append org-agenda-exporter-settings
3289 opts pars)
3290 (org-agenda nil thiscmdkey))
3291 (set-buffer bufname)
3292 (while files
3293 (org-eval-in-environment (append org-agenda-exporter-settings
3294 opts pars)
3295 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3296 (and (get-buffer bufname)
3297 (kill-buffer bufname)))))))
3299 (defvar org-agenda-current-span nil
3300 "The current span used in the agenda view.") ; local variable in the agenda buffer
3301 (defun org-agenda-mark-header-line (pos)
3302 "Mark the line at POS as an agenda structure header."
3303 (save-excursion
3304 (goto-char pos)
3305 (put-text-property (point-at-bol) (point-at-eol)
3306 'org-agenda-structural-header t)
3307 (when org-agenda-title-append
3308 (put-text-property (point-at-bol) (point-at-eol)
3309 'org-agenda-title-append org-agenda-title-append))))
3311 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3312 (defvar org-agenda-write-buffer-name "Agenda View")
3313 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3314 "Write the current buffer (an agenda view) as a file.
3315 Depending on the extension of the file name, plain text (.txt),
3316 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3317 If the extension is .ics, run icalendar export over all files used
3318 to construct the agenda and limit the export to entries listed in the
3319 agenda now.
3320 If the extension is .org, collect all subtrees corresponding to the
3321 agenda entries and add them in an .org file.
3322 With prefix argument OPEN, open the new file immediately.
3323 If NOSETTINGS is given, do not scope the settings of
3324 `org-agenda-exporter-settings' into the export commands. This is used when
3325 the settings have already been scoped and we do not wish to overrule other,
3326 higher priority settings.
3327 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3328 (interactive "FWrite agenda to file: \nP")
3329 (if (or (not (file-writable-p file))
3330 (and (file-exists-p file)
3331 (if (org-called-interactively-p 'any)
3332 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3333 (user-error "Cannot write agenda to file %s" file))
3334 (org-let (if nosettings nil org-agenda-exporter-settings)
3335 '(save-excursion
3336 (save-window-excursion
3337 (let ((bs (copy-sequence (buffer-string))) beg content)
3338 (with-temp-buffer
3339 (rename-buffer org-agenda-write-buffer-name t)
3340 (set-buffer-modified-p nil)
3341 (insert bs)
3342 (org-agenda-remove-marked-text 'org-filtered)
3343 (run-hooks 'org-agenda-before-write-hook)
3344 (cond
3345 ((org-bound-and-true-p org-mobile-creating-agendas)
3346 (org-mobile-write-agenda-for-mobile file))
3347 ((string-match "\\.org\\'" file)
3348 (let (content p m message-log-max)
3349 (goto-char (point-min))
3350 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3351 (goto-char p)
3352 (setq m (get-text-property (point) 'org-hd-marker))
3353 (when m
3354 (push (save-excursion
3355 (set-buffer (marker-buffer m))
3356 (goto-char m)
3357 (org-copy-subtree 1 nil t t)
3358 org-subtree-clip)
3359 content)))
3360 (find-file file)
3361 (erase-buffer)
3362 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3363 (write-file file)
3364 (kill-buffer (current-buffer))
3365 (message "Org file written to %s" file)))
3366 ((string-match "\\.html?\\'" file)
3367 (require 'htmlize)
3368 (set-buffer (htmlize-buffer (current-buffer)))
3369 (when org-agenda-export-html-style
3370 ;; replace <style> section with org-agenda-export-html-style
3371 (goto-char (point-min))
3372 (kill-region (- (search-forward "<style") 6)
3373 (search-forward "</style>"))
3374 (insert org-agenda-export-html-style))
3375 (write-file file)
3376 (kill-buffer (current-buffer))
3377 (message "HTML written to %s" file))
3378 ((string-match "\\.ps\\'" file)
3379 (require 'ps-print)
3380 (ps-print-buffer-with-faces file)
3381 (message "Postscript written to %s" file))
3382 ((string-match "\\.pdf\\'" file)
3383 (require 'ps-print)
3384 (ps-print-buffer-with-faces
3385 (concat (file-name-sans-extension file) ".ps"))
3386 (call-process "ps2pdf" nil nil nil
3387 (expand-file-name
3388 (concat (file-name-sans-extension file) ".ps"))
3389 (expand-file-name file))
3390 (delete-file (concat (file-name-sans-extension file) ".ps"))
3391 (message "PDF written to %s" file))
3392 ((string-match "\\.ics\\'" file)
3393 (require 'ox-icalendar)
3394 (org-icalendar-export-current-agenda (expand-file-name file)))
3396 (let ((bs (buffer-string)))
3397 (find-file file)
3398 (erase-buffer)
3399 (insert bs)
3400 (save-buffer 0)
3401 (kill-buffer (current-buffer))
3402 (message "Plain text written to %s" file))))))))
3403 (set-buffer (or agenda-bufname
3404 (and (org-called-interactively-p 'any) (buffer-name))
3405 org-agenda-buffer-name)))
3406 (when open (org-open-file file)))
3408 (defun org-agenda-remove-marked-text (property &optional value)
3409 "Delete all text marked with VALUE of PROPERTY.
3410 VALUE defaults to t."
3411 (let (beg)
3412 (setq value (or value t))
3413 (while (setq beg (text-property-any (point-min) (point-max)
3414 property value))
3415 (delete-region
3416 beg (or (next-single-property-change beg property)
3417 (point-max))))))
3419 (defun org-agenda-add-entry-text ()
3420 "Add entry text to agenda lines.
3421 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3422 entry text following headings shown in the agenda.
3423 Drawers will be excluded, also the line with scheduling/deadline info."
3424 (when (and (> org-agenda-add-entry-text-maxlines 0)
3425 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3426 (let (m txt)
3427 (goto-char (point-min))
3428 (while (not (eobp))
3429 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3430 (beginning-of-line 2)
3431 (setq txt (org-agenda-get-some-entry-text
3432 m org-agenda-add-entry-text-maxlines " > "))
3433 (end-of-line 1)
3434 (if (string-match "\\S-" txt)
3435 (insert "\n" txt)
3436 (or (eobp) (forward-char 1))))))))
3438 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3439 &rest keep)
3440 "Extract entry text from MARKER, at most N-LINES lines.
3441 This will ignore drawers etc, just get the text.
3442 If INDENT is given, prefix every line with this string. If KEEP is
3443 given, it is a list of symbols, defining stuff that should not be
3444 removed from the entry content. Currently only `planning' is allowed here."
3445 (let (txt drawer-re kwd-time-re ind)
3446 (save-excursion
3447 (with-current-buffer (marker-buffer marker)
3448 (if (not (derived-mode-p 'org-mode))
3449 (setq txt "")
3450 (save-excursion
3451 (save-restriction
3452 (widen)
3453 (goto-char marker)
3454 (end-of-line 1)
3455 (setq txt (buffer-substring
3456 (min (1+ (point)) (point-max))
3457 (progn (outline-next-heading) (point)))
3458 drawer-re org-drawer-regexp
3459 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3460 ".*\n?"))
3461 (with-temp-buffer
3462 (insert txt)
3463 (when org-agenda-add-entry-text-descriptive-links
3464 (goto-char (point-min))
3465 (while (org-activate-bracket-links (point-max))
3466 (add-text-properties (match-beginning 0) (match-end 0)
3467 '(face org-link))))
3468 (goto-char (point-min))
3469 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3470 (set-text-properties (match-beginning 0) (match-end 0)
3471 nil))
3472 (goto-char (point-min))
3473 (while (re-search-forward drawer-re nil t)
3474 (delete-region
3475 (match-beginning 0)
3476 (progn (re-search-forward
3477 "^[ \t]*:END:.*\n?" nil 'move)
3478 (point))))
3479 (unless (member 'planning keep)
3480 (goto-char (point-min))
3481 (while (re-search-forward kwd-time-re nil t)
3482 (replace-match "")))
3483 (goto-char (point-min))
3484 (when org-agenda-entry-text-exclude-regexps
3485 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3486 (while (setq re (pop re-list))
3487 (goto-char (point-min))
3488 (while (re-search-forward re nil t)
3489 (replace-match "")))))
3490 (goto-char (point-max))
3491 (skip-chars-backward " \t\n")
3492 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3494 ;; find and remove min common indentation
3495 (goto-char (point-min))
3496 (untabify (point-min) (point-max))
3497 (setq ind (org-get-indentation))
3498 (while (not (eobp))
3499 (unless (looking-at "[ \t]*$")
3500 (setq ind (min ind (org-get-indentation))))
3501 (beginning-of-line 2))
3502 (goto-char (point-min))
3503 (while (not (eobp))
3504 (unless (looking-at "[ \t]*$")
3505 (move-to-column ind)
3506 (delete-region (point-at-bol) (point)))
3507 (beginning-of-line 2))
3509 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3511 (goto-char (point-min))
3512 (when indent
3513 (while (and (not (eobp)) (re-search-forward "^" nil t))
3514 (replace-match indent t t)))
3515 (goto-char (point-min))
3516 (while (looking-at "[ \t]*\n") (replace-match ""))
3517 (goto-char (point-max))
3518 (when (> (org-current-line)
3519 n-lines)
3520 (org-goto-line (1+ n-lines))
3521 (backward-char 1))
3522 (setq txt (buffer-substring (point-min) (point)))))))))
3523 txt))
3525 (defun org-check-for-org-mode ()
3526 "Make sure current buffer is in org-mode. Error if not."
3527 (or (derived-mode-p 'org-mode)
3528 (error "Cannot execute org-mode agenda command on buffer in %s"
3529 major-mode)))
3531 ;;; Agenda prepare and finalize
3533 (defvar org-agenda-multi nil) ; dynamically scoped
3534 (defvar org-agenda-pre-window-conf nil)
3535 (defvar org-agenda-columns-active nil)
3536 (defvar org-agenda-name nil)
3537 (defvar org-agenda-tag-filter nil)
3538 (defvar org-agenda-category-filter nil)
3539 (defvar org-agenda-regexp-filter nil)
3540 (defvar org-agenda-effort-filter nil)
3541 (defvar org-agenda-top-headline-filter nil)
3542 (defvar org-agenda-tag-filter-preset nil
3543 "A preset of the tags filter used for secondary agenda filtering.
3544 This must be a list of strings, each string must be a single tag preceded
3545 by \"+\" or \"-\".
3546 This variable should not be set directly, but agenda custom commands can
3547 bind it in the options section. The preset filter is a global property of
3548 the entire agenda view. In a block agenda, it will not work reliably to
3549 define a filter for one of the individual blocks. You need to set it in
3550 the global options and expect it to be applied to the entire view.")
3552 (defvar org-agenda-category-filter-preset nil
3553 "A preset of the category filter used for secondary agenda filtering.
3554 This must be a list of strings, each string must be a single category
3555 preceded by \"+\" or \"-\".
3556 This variable should not be set directly, but agenda custom commands can
3557 bind it in the options section. The preset filter is a global property of
3558 the entire agenda view. In a block agenda, it will not work reliably to
3559 define a filter for one of the individual blocks. You need to set it in
3560 the global options and expect it to be applied to the entire view.")
3562 (defvar org-agenda-regexp-filter-preset nil
3563 "A preset of the regexp filter used for secondary agenda filtering.
3564 This must be a list of strings, each string must be a single regexp
3565 preceded by \"+\" or \"-\".
3566 This variable should not be set directly, but agenda custom commands can
3567 bind it in the options section. The preset filter is a global property of
3568 the entire agenda view. In a block agenda, it will not work reliably to
3569 define a filter for one of the individual blocks. You need to set it in
3570 the global options and expect it to be applied to the entire view.")
3572 (defvar org-agenda-effort-filter-preset nil
3573 "A preset of the effort condition used for secondary agenda filtering.
3574 This must be a list of strings, each string must be a single regexp
3575 preceded by \"+\" or \"-\".
3576 This variable should not be set directly, but agenda custom commands can
3577 bind it in the options section. The preset filter is a global property of
3578 the entire agenda view. In a block agenda, it will not work reliably to
3579 define a filter for one of the individual blocks. You need to set it in
3580 the global options and expect it to be applied to the entire view.")
3582 (defun org-agenda-use-sticky-p ()
3583 "Return non-nil if an agenda buffer named
3584 `org-agenda-buffer-name' exists and should be shown instead of
3585 generating a new one."
3586 (and
3587 ;; turned off by user
3588 org-agenda-sticky
3589 ;; For multi-agenda buffer already exists
3590 (not org-agenda-multi)
3591 ;; buffer found
3592 (get-buffer org-agenda-buffer-name)
3593 ;; C-u parameter is same as last call
3594 (with-current-buffer (get-buffer org-agenda-buffer-name)
3595 (and
3596 (equal current-prefix-arg
3597 org-agenda-last-prefix-arg)
3598 ;; In case user turned stickiness on, while having existing
3599 ;; Agenda buffer active, don't reuse that buffer, because it
3600 ;; does not have org variables local
3601 org-agenda-this-buffer-is-sticky))))
3603 (defun org-agenda-prepare-window (abuf filter-alist)
3604 "Setup agenda buffer in the window.
3605 ABUF is the buffer for the agenda window.
3606 FILTER-ALIST is an alist of filters we need to apply when
3607 `org-agenda-persistent-filter' is non-nil."
3608 (let* ((awin (get-buffer-window abuf)) wconf)
3609 (cond
3610 ((equal (current-buffer) abuf) nil)
3611 (awin (select-window awin))
3612 ((not (setq wconf (current-window-configuration))))
3613 ((equal org-agenda-window-setup 'current-window)
3614 (org-pop-to-buffer-same-window abuf))
3615 ((equal org-agenda-window-setup 'other-window)
3616 (org-switch-to-buffer-other-window abuf))
3617 ((equal org-agenda-window-setup 'other-frame)
3618 (switch-to-buffer-other-frame abuf))
3619 ((equal org-agenda-window-setup 'reorganize-frame)
3620 (delete-other-windows)
3621 (org-switch-to-buffer-other-window abuf)))
3622 (setq org-agenda-tag-filter (cdr (assoc 'tag filter-alist)))
3623 (setq org-agenda-category-filter (cdr (assoc 'cat filter-alist)))
3624 (setq org-agenda-effort-filter (cdr (assoc 'effort filter-alist)))
3625 (setq org-agenda-regexp-filter (cdr (assoc 're filter-alist)))
3626 ;; Additional test in case agenda is invoked from within agenda
3627 ;; buffer via elisp link.
3628 (unless (equal (current-buffer) abuf)
3629 (org-pop-to-buffer-same-window abuf))
3630 (setq org-agenda-pre-window-conf
3631 (or org-agenda-pre-window-conf wconf))))
3633 (defun org-agenda-prepare (&optional name)
3634 (let ((filter-alist (if org-agenda-persistent-filter
3635 (list `(tag . ,org-agenda-tag-filter)
3636 `(re . ,org-agenda-regexp-filter)
3637 `(effort . ,org-agenda-effort-filter)
3638 `(car . ,org-agenda-category-filter)))))
3639 (if (org-agenda-use-sticky-p)
3640 (progn
3641 ;; Popup existing buffer
3642 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name)
3643 filter-alist)
3644 (message "Sticky Agenda buffer, use `r' to refresh")
3645 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3646 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3647 (setq org-todo-keywords-for-agenda nil)
3648 (put 'org-agenda-tag-filter :preset-filter
3649 org-agenda-tag-filter-preset)
3650 (put 'org-agenda-category-filter :preset-filter
3651 org-agenda-category-filter-preset)
3652 (put 'org-agenda-regexp-filter :preset-filter
3653 org-agenda-regexp-filter-preset)
3654 (put 'org-agenda-effort-filter :preset-filter
3655 org-agenda-effort-filter-preset)
3656 (if org-agenda-multi
3657 (progn
3658 (setq buffer-read-only nil)
3659 (goto-char (point-max))
3660 (unless (or (bobp) org-agenda-compact-blocks
3661 (not org-agenda-block-separator))
3662 (insert "\n"
3663 (if (stringp org-agenda-block-separator)
3664 org-agenda-block-separator
3665 (make-string (window-width) org-agenda-block-separator))
3666 "\n"))
3667 (narrow-to-region (point) (point-max)))
3668 (setq org-done-keywords-for-agenda nil)
3669 ;; Setting any org variables that are in org-agenda-local-vars
3670 ;; list need to be done after the prepare call
3671 (org-agenda-prepare-window
3672 (get-buffer-create org-agenda-buffer-name) filter-alist)
3673 (setq buffer-read-only nil)
3674 (org-agenda-reset-markers)
3675 (let ((inhibit-read-only t)) (erase-buffer))
3676 (org-agenda-mode)
3677 (setq org-agenda-buffer (current-buffer))
3678 (setq org-agenda-contributing-files nil)
3679 (setq org-agenda-columns-active nil)
3680 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3681 (setq org-todo-keywords-for-agenda
3682 (org-uniquify org-todo-keywords-for-agenda))
3683 (setq org-done-keywords-for-agenda
3684 (org-uniquify org-done-keywords-for-agenda))
3685 (setq org-agenda-last-prefix-arg current-prefix-arg)
3686 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3687 (and name (not org-agenda-name)
3688 (org-set-local 'org-agenda-name name)))
3689 (setq buffer-read-only nil))))
3691 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3692 (defun org-agenda-finalize ()
3693 "Finishing touch for the agenda buffer, called just before displaying it."
3694 (unless org-agenda-multi
3695 (save-excursion
3696 (let ((inhibit-read-only t))
3697 (goto-char (point-min))
3698 (save-excursion
3699 (while (org-activate-bracket-links (point-max))
3700 (add-text-properties (match-beginning 0) (match-end 0)
3701 '(face org-link))))
3702 (save-excursion
3703 (while (org-activate-plain-links (point-max))
3704 (add-text-properties (match-beginning 0) (match-end 0)
3705 '(face org-link))))
3706 (unless (eq org-agenda-remove-tags t)
3707 (org-agenda-align-tags))
3708 (unless org-agenda-with-colors
3709 (remove-text-properties (point-min) (point-max) '(face nil)))
3710 (if (and (boundp 'org-agenda-overriding-columns-format)
3711 org-agenda-overriding-columns-format)
3712 (org-set-local 'org-agenda-overriding-columns-format
3713 org-agenda-overriding-columns-format))
3714 (if (and (boundp 'org-agenda-view-columns-initially)
3715 org-agenda-view-columns-initially)
3716 (org-agenda-columns))
3717 (when org-agenda-fontify-priorities
3718 (org-agenda-fontify-priorities))
3719 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3720 (org-agenda-dim-blocked-tasks))
3721 (org-agenda-mark-clocking-task)
3722 (when org-agenda-entry-text-mode
3723 (org-agenda-entry-text-hide)
3724 (org-agenda-entry-text-show))
3725 (if (and (functionp 'org-habit-insert-consistency-graphs)
3726 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3727 (org-habit-insert-consistency-graphs))
3728 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3729 (unless (or (eq org-agenda-show-inherited-tags 'always)
3730 (and (listp org-agenda-show-inherited-tags)
3731 (memq org-agenda-type org-agenda-show-inherited-tags))
3732 (and (eq org-agenda-show-inherited-tags t)
3733 (or (eq org-agenda-use-tag-inheritance t)
3734 (and (listp org-agenda-use-tag-inheritance)
3735 (not (memq org-agenda-type
3736 org-agenda-use-tag-inheritance))))))
3737 (let (mrk)
3738 (save-excursion
3739 (goto-char (point-min))
3740 (while (equal (forward-line) 0)
3741 (when (setq mrk (get-text-property (point) 'org-hd-marker))
3742 (put-text-property (point-at-bol) (point-at-eol)
3743 'tags (org-with-point-at mrk
3744 (delete-dups
3745 (mapcar 'downcase (org-get-tags-at))))))))))
3746 (run-hooks 'org-agenda-finalize-hook)
3747 (when org-agenda-top-headline-filter
3748 (org-agenda-filter-top-headline-apply
3749 org-agenda-top-headline-filter))
3750 (when org-agenda-tag-filter
3751 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3752 (when (get 'org-agenda-tag-filter :preset-filter)
3753 (org-agenda-filter-apply
3754 (get 'org-agenda-tag-filter :preset-filter) 'tag))
3755 (when org-agenda-category-filter
3756 (org-agenda-filter-apply org-agenda-category-filter 'category))
3757 (when (get 'org-agenda-category-filter :preset-filter)
3758 (org-agenda-filter-apply
3759 (get 'org-agenda-category-filter :preset-filter) 'category))
3760 (when org-agenda-regexp-filter
3761 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3762 (when (get 'org-agenda-regexp-filter :preset-filter)
3763 (org-agenda-filter-apply
3764 (get 'org-agenda-regexp-filter :preset-filter) 'regexp))
3765 (when org-agenda-effort-filter
3766 (org-agenda-filter-apply org-agenda-effort-filter 'effort))
3767 (when (get 'org-agenda-effort-filter :preset-filter)
3768 (org-agenda-filter-apply
3769 (get 'org-agenda-effort-filter :preset-filter) 'effort))
3770 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3772 (defun org-agenda-mark-clocking-task ()
3773 "Mark the current clock entry in the agenda if it is present."
3774 ;; We need to widen when `org-agenda-finalize' is called from
3775 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3776 (when org-clock-current-task
3777 (save-restriction
3778 (widen)
3779 (org-agenda-unmark-clocking-task)
3780 (when (marker-buffer org-clock-hd-marker)
3781 (save-excursion
3782 (goto-char (point-min))
3783 (let (s ov)
3784 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3785 (goto-char s)
3786 (when (equal (org-get-at-bol 'org-hd-marker)
3787 org-clock-hd-marker)
3788 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3789 (overlay-put ov 'type 'org-agenda-clocking)
3790 (overlay-put ov 'face 'org-agenda-clocking)
3791 (overlay-put ov 'help-echo
3792 "The clock is running in this item")))))))))
3794 (defun org-agenda-unmark-clocking-task ()
3795 "Unmark the current clocking task."
3796 (mapc (lambda (o)
3797 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3798 (delete-overlay o)))
3799 (overlays-in (point-min) (point-max))))
3801 (defun org-agenda-fontify-priorities ()
3802 "Make highest priority lines bold, and lowest italic."
3803 (interactive)
3804 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3805 (delete-overlay o)))
3806 (overlays-in (point-min) (point-max)))
3807 (save-excursion
3808 (let (b e p ov h l)
3809 (goto-char (point-min))
3810 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3811 (setq h (or (get-char-property (point) 'org-highest-priority)
3812 org-highest-priority)
3813 l (or (get-char-property (point) 'org-lowest-priority)
3814 org-lowest-priority)
3815 p (string-to-char (match-string 1))
3816 b (match-beginning 0)
3817 e (if (eq org-agenda-fontify-priorities 'cookies)
3818 (match-end 0)
3819 (point-at-eol))
3820 ov (make-overlay b e))
3821 (overlay-put
3822 ov 'face
3823 (cons (cond ((org-face-from-face-or-color
3824 'priority nil
3825 (cdr (assoc p org-priority-faces))))
3826 ((and (listp org-agenda-fontify-priorities)
3827 (org-face-from-face-or-color
3828 'priority nil
3829 (cdr (assoc p org-agenda-fontify-priorities)))))
3830 ((equal p l) 'italic)
3831 ((equal p h) 'bold))
3832 'org-priority))
3833 (overlay-put ov 'org-type 'org-priority)))))
3835 (defvar org-depend-tag-blocked)
3837 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3838 "Dim currently blocked TODO's in the agenda display.
3839 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3840 dimming them."
3841 (interactive "P")
3842 (when (org-called-interactively-p 'interactive)
3843 (message "Dim or hide blocked tasks..."))
3844 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3845 (delete-overlay o)))
3846 (overlays-in (point-min) (point-max)))
3847 (save-excursion
3848 (let ((inhibit-read-only t)
3849 (org-depend-tag-blocked nil)
3850 (invis (or (not (null invisible))
3851 (eq org-agenda-dim-blocked-tasks 'invisible)))
3852 org-blocked-by-checkboxes
3853 invis1 b e p ov h l)
3854 (goto-char (point-min))
3855 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3856 (and pos (goto-char (1+ pos))))
3857 (setq org-blocked-by-checkboxes nil invis1 invis)
3858 (let ((marker (org-get-at-bol 'org-hd-marker)))
3859 (when (and marker
3860 (with-current-buffer (marker-buffer marker)
3861 (save-excursion (goto-char marker)
3862 (org-entry-blocked-p))))
3863 (if org-blocked-by-checkboxes (setq invis1 nil))
3864 (setq b (if invis1
3865 (max (point-min) (1- (point-at-bol)))
3866 (point-at-bol))
3867 e (point-at-eol)
3868 ov (make-overlay b e))
3869 (if invis1
3870 (progn (overlay-put ov 'invisible t)
3871 (overlay-put ov 'intangible t))
3872 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3873 (overlay-put ov 'org-type 'org-blocked-todo))))))
3874 (when (org-called-interactively-p 'interactive)
3875 (message "Dim or hide blocked tasks...done")))
3877 (defvar org-agenda-skip-function nil
3878 "Function to be called at each match during agenda construction.
3879 If this function returns nil, the current match should not be skipped.
3880 Otherwise, the function must return a position from where the search
3881 should be continued.
3882 This may also be a Lisp form, it will be evaluated.
3883 Never set this variable using `setq' or so, because then it will apply
3884 to all future agenda commands. If you do want a global skipping condition,
3885 use the option `org-agenda-skip-function-global' instead.
3886 The correct usage for `org-agenda-skip-function' is to bind it with
3887 `let' to scope it dynamically into the agenda-constructing command.
3888 A good way to set it is through options in `org-agenda-custom-commands'.")
3890 (defun org-agenda-skip ()
3891 "Throw to `:skip' in places that should be skipped.
3892 Also moves point to the end of the skipped region, so that search can
3893 continue from there."
3894 (let ((p (point-at-bol)) to)
3895 (when (or
3896 (save-excursion (goto-char p) (looking-at comment-start-skip))
3897 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3898 (get-text-property p :org-archived)
3899 (org-end-of-subtree t))
3900 (and org-agenda-skip-comment-trees
3901 (get-text-property p :org-comment)
3902 (org-end-of-subtree t))
3903 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3904 (org-agenda-skip-eval org-agenda-skip-function)))
3905 (goto-char to))
3906 (org-in-src-block-p t))
3907 (throw :skip t))))
3909 (defun org-agenda-skip-eval (form)
3910 "If FORM is a function or a list, call (or eval) it and return the result.
3911 `save-excursion' and `save-match-data' are wrapped around the call, so point
3912 and match data are returned to the previous state no matter what these
3913 functions do."
3914 (let (fp)
3915 (and form
3916 (or (setq fp (functionp form))
3917 (consp form))
3918 (save-excursion
3919 (save-match-data
3920 (if fp
3921 (funcall form)
3922 (eval form)))))))
3924 (defvar org-agenda-markers nil
3925 "List of all currently active markers created by `org-agenda'.")
3926 (defvar org-agenda-last-marker-time (org-float-time)
3927 "Creation time of the last agenda marker.")
3929 (defun org-agenda-new-marker (&optional pos)
3930 "Return a new agenda marker.
3931 Org-mode keeps a list of these markers and resets them when they are
3932 no longer in use."
3933 (let ((m (copy-marker (or pos (point)))))
3934 (setq org-agenda-last-marker-time (org-float-time))
3935 (if org-agenda-buffer
3936 (with-current-buffer org-agenda-buffer
3937 (push m org-agenda-markers))
3938 (push m org-agenda-markers))
3941 (defun org-agenda-reset-markers ()
3942 "Reset markers created by `org-agenda'."
3943 (while org-agenda-markers
3944 (move-marker (pop org-agenda-markers) nil)))
3946 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3947 "Save relative positions of markers in region.
3948 This check for agenda markers in all agenda buffers currently active."
3949 (dolist (buf (buffer-list))
3950 (with-current-buffer buf
3951 (when (eq major-mode 'org-agenda-mode)
3952 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3953 org-agenda-markers)))))
3955 ;;; Entry text mode
3957 (defun org-agenda-entry-text-show-here ()
3958 "Add some text from the entry as context to the current line."
3959 (let (m txt o)
3960 (setq m (org-get-at-bol 'org-hd-marker))
3961 (unless (marker-buffer m)
3962 (error "No marker points to an entry here"))
3963 (setq txt (concat "\n" (org-no-properties
3964 (org-agenda-get-some-entry-text
3965 m org-agenda-entry-text-maxlines
3966 org-agenda-entry-text-leaders))))
3967 (when (string-match "\\S-" txt)
3968 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3969 (overlay-put o 'evaporate t)
3970 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3971 (overlay-put o 'after-string txt))))
3973 (defun org-agenda-entry-text-show ()
3974 "Add entry context for all agenda lines."
3975 (interactive)
3976 (save-excursion
3977 (goto-char (point-max))
3978 (beginning-of-line 1)
3979 (while (not (bobp))
3980 (when (org-get-at-bol 'org-hd-marker)
3981 (org-agenda-entry-text-show-here))
3982 (beginning-of-line 0))))
3984 (defun org-agenda-entry-text-hide ()
3985 "Remove any shown entry context."
3986 (delq nil
3987 (mapcar (lambda (o)
3988 (if (eq (overlay-get o 'org-overlay-type)
3989 'agenda-entry-content)
3990 (progn (delete-overlay o) t)))
3991 (overlays-in (point-min) (point-max)))))
3993 (defun org-agenda-get-day-face (date)
3994 "Return the face DATE should be displayed with."
3995 (or (and (functionp org-agenda-day-face-function)
3996 (funcall org-agenda-day-face-function date))
3997 (cond ((org-agenda-todayp date)
3998 'org-agenda-date-today)
3999 ((member (calendar-day-of-week date) org-agenda-weekend-days)
4000 'org-agenda-date-weekend)
4001 (t 'org-agenda-date))))
4003 ;;; Agenda timeline
4005 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
4006 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
4008 (defun org-timeline (&optional dotodo)
4009 "Show a time-sorted view of the entries in the current org file.
4010 Only entries with a time stamp of today or later will be listed. With
4011 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
4012 under the current date.
4013 If the buffer contains an active region, only check the region for
4014 dates."
4015 (interactive "P")
4016 (let* ((dopast t)
4017 (org-agenda-show-log-scoped org-agenda-show-log)
4018 (org-agenda-show-log org-agenda-show-log-scoped)
4019 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
4020 (current-buffer))))
4021 (date (calendar-current-date))
4022 (beg (if (org-region-active-p) (region-beginning) (point-min)))
4023 (end (if (org-region-active-p) (region-end) (point-max)))
4024 (day-numbers (org-get-all-dates
4025 beg end 'no-ranges
4026 t org-agenda-show-log-scoped ; always include today
4027 org-timeline-show-empty-dates))
4028 (org-deadline-warning-days 0)
4029 (org-agenda-only-exact-dates t)
4030 (today (org-today))
4031 (past t)
4032 args
4033 s e rtn d emptyp)
4034 (setq org-agenda-redo-command
4035 (list 'let
4036 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4037 (list 'org-switch-to-buffer-other-window (current-buffer))
4038 (list 'org-timeline (list 'quote dotodo))))
4039 (put 'org-agenda-redo-command 'org-lprops nil)
4040 (if (not dopast)
4041 ;; Remove past dates from the list of dates.
4042 (setq day-numbers (delq nil (mapcar (lambda(x)
4043 (if (>= x today) x nil))
4044 day-numbers))))
4045 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4046 (org-compile-prefix-format 'timeline)
4047 (org-set-sorting-strategy 'timeline)
4048 (if org-agenda-show-log-scoped (push :closed args))
4049 (push :timestamp args)
4050 (push :deadline args)
4051 (push :scheduled args)
4052 (push :sexp args)
4053 (if dotodo (push :todo args))
4054 (insert "Timeline of file " entry "\n")
4055 (add-text-properties (point-min) (point)
4056 (list 'face 'org-agenda-structure))
4057 (org-agenda-mark-header-line (point-min))
4058 (while (setq d (pop day-numbers))
4059 (if (and (listp d) (eq (car d) :omitted))
4060 (progn
4061 (setq s (point))
4062 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4063 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4064 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4065 (if (and (>= d today)
4066 dopast
4067 past)
4068 (progn
4069 (setq past nil)
4070 (insert (make-string 79 ?-) "\n")))
4071 (setq date (calendar-gregorian-from-absolute d))
4072 (setq s (point))
4073 (setq rtn (and (not emptyp)
4074 (apply 'org-agenda-get-day-entries entry
4075 date args)))
4076 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4077 (progn
4078 (insert
4079 (if (stringp org-agenda-format-date)
4080 (format-time-string org-agenda-format-date
4081 (org-time-from-absolute date))
4082 (funcall org-agenda-format-date date))
4083 "\n")
4084 (put-text-property s (1- (point)) 'face
4085 (org-agenda-get-day-face date))
4086 (put-text-property s (1- (point)) 'org-date-line t)
4087 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4088 (if (equal d today)
4089 (put-text-property s (1- (point)) 'org-today t))
4090 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4091 (put-text-property s (1- (point)) 'day d)))))
4092 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4093 (point-min)))
4094 (add-text-properties
4095 (point-min) (point-max)
4096 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4097 (org-agenda-finalize)
4098 (setq buffer-read-only t)))
4100 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4101 "Return a list of all relevant day numbers from BEG to END buffer positions.
4102 If NO-RANGES is non-nil, include only the start and end dates of a range,
4103 not every single day in the range. If FORCE-TODAY is non-nil, make
4104 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4105 inactive time stamps (those in square brackets) are included.
4106 When EMPTY is non-nil, also include days without any entries."
4107 (let ((re (concat
4108 (if pre-re pre-re "")
4109 (if inactive org-ts-regexp-both org-ts-regexp)))
4110 dates dates1 date day day1 day2 ts1 ts2 pos)
4111 (if force-today
4112 (setq dates (list (org-today))))
4113 (save-excursion
4114 (goto-char beg)
4115 (while (re-search-forward re end t)
4116 (setq day (time-to-days (org-time-string-to-time
4117 (substring (match-string 1) 0 10)
4118 (current-buffer) (match-beginning 0))))
4119 (or (memq day dates) (push day dates)))
4120 (unless no-ranges
4121 (goto-char beg)
4122 (while (re-search-forward org-tr-regexp end t)
4123 (setq pos (match-beginning 0))
4124 (setq ts1 (substring (match-string 1) 0 10)
4125 ts2 (substring (match-string 2) 0 10)
4126 day1 (time-to-days (org-time-string-to-time
4127 ts1 (current-buffer) pos))
4128 day2 (time-to-days (org-time-string-to-time
4129 ts2 (current-buffer) pos)))
4130 (while (< (setq day1 (1+ day1)) day2)
4131 (or (memq day1 dates) (push day1 dates)))))
4132 (setq dates (sort dates '<))
4133 (when empty
4134 (while (setq day (pop dates))
4135 (setq day2 (car dates))
4136 (push day dates1)
4137 (when (and day2 empty)
4138 (if (or (eq empty t)
4139 (and (numberp empty) (<= (- day2 day) empty)))
4140 (while (< (setq day (1+ day)) day2)
4141 (push (list day) dates1))
4142 (push (cons :omitted (- day2 day)) dates1))))
4143 (setq dates (nreverse dates1)))
4144 dates)))
4146 ;;; Agenda Daily/Weekly
4148 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4149 "Start day for the agenda view.
4150 Custom commands can set this variable in the options section.
4151 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4152 input allowed when reading a date through the Org calendar.
4153 See the docstring of `org-read-date' for details.")
4154 (defvar org-starting-day nil) ; local variable in the agenda buffer
4155 (defvar org-arg-loc nil) ; local variable
4157 (defvar org-agenda-buffer-tmp-name nil)
4158 ;;;###autoload
4159 (defun org-agenda-list (&optional arg start-day span with-hour)
4160 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4161 The view will be for the current day or week, but from the overview buffer
4162 you will be able to go to other days/weeks.
4164 With a numeric prefix argument in an interactive call, the agenda will
4165 span ARG days. Lisp programs should instead specify SPAN to change
4166 the number of days. SPAN defaults to `org-agenda-span'.
4168 START-DAY defaults to TODAY, or to the most recent match for the weekday
4169 given in `org-agenda-start-on-weekday'.
4171 When WITH-HOUR is non-nil, only include scheduled and deadline
4172 items if they have an hour specification like [h]h:mm."
4173 (interactive "P")
4174 (if org-agenda-overriding-arguments
4175 (setq arg (car org-agenda-overriding-arguments)
4176 start-day (nth 1 org-agenda-overriding-arguments)
4177 span (nth 2 org-agenda-overriding-arguments)))
4178 (if (and (integerp arg) (> arg 0))
4179 (setq span arg arg nil))
4180 (catch 'exit
4181 (setq org-agenda-buffer-name
4182 (or org-agenda-buffer-tmp-name
4183 (if org-agenda-sticky
4184 (cond ((and org-keys (stringp org-match))
4185 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4186 (org-keys
4187 (format "*Org Agenda(%s)*" org-keys))
4188 (t "*Org Agenda(a)*")))
4189 org-agenda-buffer-name))
4190 (org-agenda-prepare "Day/Week")
4191 (setq start-day (or start-day org-agenda-start-day))
4192 (if (stringp start-day)
4193 ;; Convert to an absolute day number
4194 (setq start-day (time-to-days (org-read-date nil t start-day))))
4195 (org-compile-prefix-format 'agenda)
4196 (org-set-sorting-strategy 'agenda)
4197 (let* ((span (org-agenda-ndays-to-span
4198 (or span org-agenda-ndays org-agenda-span)))
4199 (today (org-today))
4200 (sd (or start-day today))
4201 (ndays (org-agenda-span-to-ndays span sd))
4202 (org-agenda-start-on-weekday
4203 (if (or (eq ndays 7) (eq ndays 14))
4204 org-agenda-start-on-weekday))
4205 (thefiles (org-agenda-files nil 'ifmode))
4206 (files thefiles)
4207 (start (if (or (null org-agenda-start-on-weekday)
4208 (< ndays 7))
4210 (let* ((nt (calendar-day-of-week
4211 (calendar-gregorian-from-absolute sd)))
4212 (n1 org-agenda-start-on-weekday)
4213 (d (- nt n1)))
4214 (- sd (+ (if (< d 0) 7 0) d)))))
4215 (day-numbers (list start))
4216 (day-cnt 0)
4217 (inhibit-redisplay (not debug-on-error))
4218 (org-agenda-show-log-scoped org-agenda-show-log)
4219 s e rtn rtnall file date d start-pos end-pos todayp
4220 clocktable-start clocktable-end filter)
4221 (setq org-agenda-redo-command
4222 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4223 (dotimes (n (1- ndays))
4224 (push (1+ (car day-numbers)) day-numbers))
4225 (setq day-numbers (nreverse day-numbers))
4226 (setq clocktable-start (car day-numbers)
4227 clocktable-end (1+ (or (org-last day-numbers) 0)))
4228 (org-set-local 'org-starting-day (car day-numbers))
4229 (org-set-local 'org-arg-loc arg)
4230 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4231 (unless org-agenda-compact-blocks
4232 (let* ((d1 (car day-numbers))
4233 (d2 (org-last day-numbers))
4234 (w1 (org-days-to-iso-week d1))
4235 (w2 (org-days-to-iso-week d2)))
4236 (setq s (point))
4237 (if org-agenda-overriding-header
4238 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4239 nil 'face 'org-agenda-structure) "\n")
4240 (insert (org-agenda-span-name span)
4241 "-agenda"
4242 (if (< (- d2 d1) 350)
4243 (if (= w1 w2)
4244 (format " (W%02d)" w1)
4245 (format " (W%02d-W%02d)" w1 w2))
4247 ":\n")))
4248 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4249 'org-date-line t))
4250 (org-agenda-mark-header-line s))
4251 (while (setq d (pop day-numbers))
4252 (setq date (calendar-gregorian-from-absolute d)
4253 s (point))
4254 (if (or (setq todayp (= d today))
4255 (and (not start-pos) (= d sd)))
4256 (setq start-pos (point))
4257 (if (and start-pos (not end-pos))
4258 (setq end-pos (point))))
4259 (setq files thefiles
4260 rtnall nil)
4261 (while (setq file (pop files))
4262 (catch 'nextfile
4263 (org-check-agenda-file file)
4264 (let ((org-agenda-entry-types org-agenda-entry-types))
4265 ;; Starred types override non-starred equivalents
4266 (when (member :deadline* org-agenda-entry-types)
4267 (setq org-agenda-entry-types
4268 (delq :deadline org-agenda-entry-types)))
4269 (when (member :scheduled* org-agenda-entry-types)
4270 (setq org-agenda-entry-types
4271 (delq :scheduled org-agenda-entry-types)))
4272 ;; Honor with-hour
4273 (when with-hour
4274 (when (member :deadline org-agenda-entry-types)
4275 (setq org-agenda-entry-types
4276 (delq :deadline org-agenda-entry-types))
4277 (push :deadline* org-agenda-entry-types))
4278 (when (member :scheduled org-agenda-entry-types)
4279 (setq org-agenda-entry-types
4280 (delq :scheduled org-agenda-entry-types))
4281 (push :scheduled* org-agenda-entry-types)))
4282 (unless org-agenda-include-deadlines
4283 (setq org-agenda-entry-types
4284 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4285 (cond
4286 ((memq org-agenda-show-log-scoped '(only clockcheck))
4287 (setq rtn (org-agenda-get-day-entries
4288 file date :closed)))
4289 (org-agenda-show-log-scoped
4290 (setq rtn (apply 'org-agenda-get-day-entries
4291 file date
4292 (append '(:closed) org-agenda-entry-types))))
4294 (setq rtn (apply 'org-agenda-get-day-entries
4295 file date
4296 org-agenda-entry-types)))))
4297 (setq rtnall (append rtnall rtn)))) ;; all entries
4298 (if org-agenda-include-diary
4299 (let ((org-agenda-search-headline-for-time t))
4300 (require 'diary-lib)
4301 (setq rtn (org-get-entries-from-diary date))
4302 (setq rtnall (append rtnall rtn))))
4303 (if (or rtnall org-agenda-show-all-dates)
4304 (progn
4305 (setq day-cnt (1+ day-cnt))
4306 (insert
4307 (if (stringp org-agenda-format-date)
4308 (format-time-string org-agenda-format-date
4309 (org-time-from-absolute date))
4310 (funcall org-agenda-format-date date))
4311 "\n")
4312 (put-text-property s (1- (point)) 'face
4313 (org-agenda-get-day-face date))
4314 (put-text-property s (1- (point)) 'org-date-line t)
4315 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4316 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4317 (when todayp
4318 (put-text-property s (1- (point)) 'org-today t))
4319 (setq rtnall
4320 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4321 (if rtnall (insert ;; all entries
4322 (org-agenda-finalize-entries rtnall 'agenda)
4323 "\n"))
4324 (put-text-property s (1- (point)) 'day d)
4325 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4326 (when (and org-agenda-clockreport-mode clocktable-start)
4327 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4328 ;; the above line is to ensure the restricted range!
4329 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4330 tbl)
4331 (setq p (org-plist-delete p :block))
4332 (setq p (plist-put p :tstart clocktable-start))
4333 (setq p (plist-put p :tend clocktable-end))
4334 (setq p (plist-put p :scope 'agenda))
4335 (setq tbl (apply 'org-clock-get-clocktable p))
4336 (insert tbl)))
4337 (goto-char (point-min))
4338 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4339 (unless (and (pos-visible-in-window-p (point-min))
4340 (pos-visible-in-window-p (point-max)))
4341 (goto-char (1- (point-max)))
4342 (recenter -1)
4343 (if (not (pos-visible-in-window-p (or start-pos 1)))
4344 (progn
4345 (goto-char (or start-pos 1))
4346 (recenter 1))))
4347 (goto-char (or start-pos 1))
4348 (add-text-properties (point-min) (point-max)
4349 `(org-agenda-type agenda
4350 org-last-args (,arg ,start-day ,span)
4351 org-redo-cmd ,org-agenda-redo-command
4352 org-series-cmd ,org-cmd))
4353 (if (eq org-agenda-show-log-scoped 'clockcheck)
4354 (org-agenda-show-clocking-issues))
4355 (org-agenda-finalize)
4356 (setq buffer-read-only t)
4357 (message ""))))
4359 (defun org-agenda-ndays-to-span (n)
4360 "Return a span symbol for a span of N days, or N if none matches."
4361 (cond ((symbolp n) n)
4362 ((= n 1) 'day)
4363 ((= n 7) 'week)
4364 ((= n 14) 'fortnight)
4365 (t n)))
4367 (defun org-agenda-span-to-ndays (span &optional start-day)
4368 "Return ndays from SPAN, possibly starting at START-DAY.
4369 START-DAY is an absolute time value."
4370 (cond ((numberp span) span)
4371 ((eq span 'day) 1)
4372 ((eq span 'week) 7)
4373 ((eq span 'fortnight) 14)
4374 ((eq span 'month)
4375 (let ((date (calendar-gregorian-from-absolute start-day)))
4376 (calendar-last-day-of-month (car date) (caddr date))))
4377 ((eq span 'year)
4378 (let ((date (calendar-gregorian-from-absolute start-day)))
4379 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4381 (defun org-agenda-span-name (span)
4382 "Return a SPAN name."
4383 (if (null span)
4385 (if (symbolp span)
4386 (capitalize (symbol-name span))
4387 (format "%d days" span))))
4389 ;;; Agenda word search
4391 (defvar org-agenda-search-history nil)
4393 (defvar org-search-syntax-table nil
4394 "Special syntax table for org-mode search.
4395 In this table, we have single quotes not as word constituents, to
4396 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4398 (defvar org-mode-syntax-table) ; From org.el
4399 (defun org-search-syntax-table ()
4400 (unless org-search-syntax-table
4401 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4402 (modify-syntax-entry ?' "." org-search-syntax-table)
4403 (modify-syntax-entry ?` "." org-search-syntax-table))
4404 org-search-syntax-table)
4406 (defvar org-agenda-last-search-view-search-was-boolean nil)
4408 ;;;###autoload
4409 (defun org-search-view (&optional todo-only string edit-at)
4410 "Show all entries that contain a phrase or words or regular expressions.
4412 With optional prefix argument TODO-ONLY, only consider entries that are
4413 TODO entries. The argument STRING can be used to pass a default search
4414 string into this function. If EDIT-AT is non-nil, it means that the
4415 user should get a chance to edit this string, with cursor at position
4416 EDIT-AT.
4418 The search string can be viewed either as a phrase that should be found as
4419 is, or it can be broken into a number of snippets, each of which must match
4420 in a Boolean way to select an entry. The default depends on the variable
4421 `org-agenda-search-view-always-boolean'.
4422 Even if this is turned off (the default) you can always switch to
4423 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4425 The default is a direct search of the whole phrase, where each space in
4426 the search string can expand to an arbitrary amount of whitespace,
4427 including newlines.
4429 If using a Boolean search, the search string is split on whitespace and
4430 each snippet is searched separately, with logical AND to select an entry.
4431 Words prefixed with a minus must *not* occur in the entry. Words without
4432 a prefix or prefixed with a plus must occur in the entry. Matching is
4433 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4434 match whole words, not parts of a word) if
4435 `org-agenda-search-view-force-full-words' is set (default is nil).
4437 Boolean search snippets enclosed by curly braces are interpreted as
4438 regular expressions that must or (when preceded with \"-\") must not
4439 match in the entry. Snippets enclosed into double quotes will be taken
4440 as a whole, to include whitespace.
4442 - If the search string starts with an asterisk, search only in headlines.
4443 - If (possibly after the leading star) the search string starts with an
4444 exclamation mark, this also means to look at TODO entries only, an effect
4445 that can also be achieved with a prefix argument.
4446 - If (possibly after star and exclamation mark) the search string starts
4447 with a colon, this will mean that the (non-regexp) snippets of the
4448 Boolean search must match as full words.
4450 This command searches the agenda files, and in addition the files listed
4451 in `org-agenda-text-search-extra-files'."
4452 (interactive "P")
4453 (if org-agenda-overriding-arguments
4454 (setq todo-only (car org-agenda-overriding-arguments)
4455 string (nth 1 org-agenda-overriding-arguments)
4456 edit-at (nth 2 org-agenda-overriding-arguments)))
4457 (let* ((props (list 'face nil
4458 'done-face 'org-agenda-done
4459 'org-not-done-regexp org-not-done-regexp
4460 'org-todo-regexp org-todo-regexp
4461 'org-complex-heading-regexp org-complex-heading-regexp
4462 'mouse-face 'highlight
4463 'help-echo (format "mouse-2 or RET jump to location")))
4464 (full-words org-agenda-search-view-force-full-words)
4465 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4466 regexp rtn rtnall files file pos inherited-tags
4467 marker category level tags c neg re boolean
4468 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4469 (unless (and (not edit-at)
4470 (stringp string)
4471 (string-match "\\S-" string))
4472 (setq string (read-string
4473 (if org-agenda-search-view-always-boolean
4474 "[+-]Word/{Regexp} ...: "
4475 "Phrase or [+-]Word/{Regexp} ...: ")
4476 (cond
4477 ((integerp edit-at) (cons string edit-at))
4478 (edit-at string))
4479 'org-agenda-search-history)))
4480 (catch 'exit
4481 (if org-agenda-sticky
4482 (setq org-agenda-buffer-name
4483 (if (stringp string)
4484 (format "*Org Agenda(%s:%s)*"
4485 (or org-keys (or (and todo-only "S") "s")) string)
4486 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4487 (org-agenda-prepare "SEARCH")
4488 (org-compile-prefix-format 'search)
4489 (org-set-sorting-strategy 'search)
4490 (setq org-agenda-redo-command
4491 (list 'org-search-view (if todo-only t nil)
4492 (list 'if 'current-prefix-arg nil string)))
4493 (setq org-agenda-query-string string)
4494 (if (equal (string-to-char string) ?*)
4495 (setq hdl-only t
4496 words (substring string 1))
4497 (setq words string))
4498 (when (equal (string-to-char words) ?!)
4499 (setq todo-only t
4500 words (substring words 1)))
4501 (when (equal (string-to-char words) ?:)
4502 (setq full-words t
4503 words (substring words 1)))
4504 (if (or org-agenda-search-view-always-boolean
4505 (member (string-to-char words) '(?- ?+ ?\{)))
4506 (setq boolean t))
4507 (setq words (org-split-string words))
4508 (let (www w)
4509 (while (setq w (pop words))
4510 (while (and (string-match "\\\\\\'" w) words)
4511 (setq w (concat (substring w 0 -1) " " (pop words))))
4512 (push w www))
4513 (setq words (nreverse www) www nil)
4514 (while (setq w (pop words))
4515 (when (and (string-match "\\`[-+]?{" w)
4516 (not (string-match "}\\'" w)))
4517 (while (and words (not (string-match "}\\'" (car words))))
4518 (setq w (concat w " " (pop words))))
4519 (setq w (concat w " " (pop words))))
4520 (push w www))
4521 (setq words (nreverse www)))
4522 (setq org-agenda-last-search-view-search-was-boolean boolean)
4523 (when boolean
4524 (let (wds w)
4525 (while (setq w (pop words))
4526 (if (or (equal (substring w 0 1) "\"")
4527 (and (> (length w) 1)
4528 (member (substring w 0 1) '("+" "-"))
4529 (equal (substring w 1 2) "\"")))
4530 (while (and words (not (equal (substring w -1) "\"")))
4531 (setq w (concat w " " (pop words)))))
4532 (and (string-match "\\`\\([-+]?\\)\"" w)
4533 (setq w (replace-match "\\1" nil nil w)))
4534 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4535 (push w wds))
4536 (setq words (nreverse wds))))
4537 (if boolean
4538 (mapc (lambda (w)
4539 (setq c (string-to-char w))
4540 (if (equal c ?-)
4541 (setq neg t w (substring w 1))
4542 (if (equal c ?+)
4543 (setq neg nil w (substring w 1))
4544 (setq neg nil)))
4545 (if (string-match "\\`{.*}\\'" w)
4546 (setq re (substring w 1 -1))
4547 (if full-words
4548 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4549 (setq re (regexp-quote (downcase w)))))
4550 (if neg (push re regexps-) (push re regexps+)))
4551 words)
4552 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4553 regexps+))
4554 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4555 (if (not regexps+)
4556 (setq regexp org-outline-regexp-bol)
4557 (setq regexp (pop regexps+))
4558 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4559 regexp))))
4560 (setq files (org-agenda-files nil 'ifmode))
4561 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4562 (pop org-agenda-text-search-extra-files)
4563 (setq files (org-add-archive-files files)))
4564 (setq files (append files org-agenda-text-search-extra-files)
4565 rtnall nil)
4566 (while (setq file (pop files))
4567 (setq ee nil)
4568 (catch 'nextfile
4569 (org-check-agenda-file file)
4570 (setq buffer (if (file-exists-p file)
4571 (org-get-agenda-file-buffer file)
4572 (error "No such file %s" file)))
4573 (if (not buffer)
4574 ;; If file does not exist, make sure an error message is sent
4575 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4576 file))))
4577 (with-current-buffer buffer
4578 (with-syntax-table (org-search-syntax-table)
4579 (unless (derived-mode-p 'org-mode)
4580 (error "Agenda file %s is not in `org-mode'" file))
4581 (let ((case-fold-search t))
4582 (save-excursion
4583 (save-restriction
4584 (if (eq buffer org-agenda-restrict)
4585 (narrow-to-region org-agenda-restrict-begin
4586 org-agenda-restrict-end)
4587 (widen))
4588 (goto-char (point-min))
4589 (unless (or (org-at-heading-p)
4590 (outline-next-heading))
4591 (throw 'nextfile t))
4592 (goto-char (max (point-min) (1- (point))))
4593 (while (re-search-forward regexp nil t)
4594 (org-back-to-heading t)
4595 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4596 (> (org-reduced-level (org-outline-level))
4597 org-agenda-search-view-max-outline-level)
4598 (forward-line -1)
4599 (outline-back-to-heading t)))
4600 (skip-chars-forward "* ")
4601 (setq beg (point-at-bol)
4602 beg1 (point)
4603 end (progn
4604 (outline-next-heading)
4605 (while (and (not (zerop org-agenda-search-view-max-outline-level))
4606 (> (org-reduced-level (org-outline-level))
4607 org-agenda-search-view-max-outline-level)
4608 (forward-line 1)
4609 (outline-next-heading)))
4610 (point)))
4612 (catch :skip
4613 (goto-char beg)
4614 (org-agenda-skip)
4615 (setq str (buffer-substring-no-properties
4616 (point-at-bol)
4617 (if hdl-only (point-at-eol) end)))
4618 (mapc (lambda (wr) (when (string-match wr str)
4619 (goto-char (1- end))
4620 (throw :skip t)))
4621 regexps-)
4622 (mapc (lambda (wr) (unless (string-match wr str)
4623 (goto-char (1- end))
4624 (throw :skip t)))
4625 (if todo-only
4626 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4627 regexps+)
4628 regexps+))
4629 (goto-char beg)
4630 (setq marker (org-agenda-new-marker (point))
4631 category (org-get-category)
4632 level (make-string (org-reduced-level (org-outline-level)) ? )
4633 inherited-tags
4634 (or (eq org-agenda-show-inherited-tags 'always)
4635 (and (listp org-agenda-show-inherited-tags)
4636 (memq 'todo org-agenda-show-inherited-tags))
4637 (and (eq org-agenda-show-inherited-tags t)
4638 (or (eq org-agenda-use-tag-inheritance t)
4639 (memq 'todo org-agenda-use-tag-inheritance))))
4640 tags (org-get-tags-at nil (not inherited-tags))
4641 txt (org-agenda-format-item
4643 (buffer-substring-no-properties
4644 beg1 (point-at-eol))
4645 level category tags t))
4646 (org-add-props txt props
4647 'org-marker marker 'org-hd-marker marker
4648 'org-todo-regexp org-todo-regexp
4649 'level level
4650 'org-complex-heading-regexp org-complex-heading-regexp
4651 'priority 1000
4652 'type "search")
4653 (push txt ee)
4654 (goto-char (1- end))))))))))
4655 (setq rtn (nreverse ee))
4656 (setq rtnall (append rtnall rtn)))
4657 (if org-agenda-overriding-header
4658 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4659 nil 'face 'org-agenda-structure) "\n")
4660 (insert "Search words: ")
4661 (add-text-properties (point-min) (1- (point))
4662 (list 'face 'org-agenda-structure))
4663 (setq pos (point))
4664 (insert string "\n")
4665 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4666 (setq pos (point))
4667 (unless org-agenda-multi
4668 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4669 (add-text-properties pos (1- (point))
4670 (list 'face 'org-agenda-structure))))
4671 (org-agenda-mark-header-line (point-min))
4672 (when rtnall
4673 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4674 (goto-char (point-min))
4675 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4676 (add-text-properties (point-min) (point-max)
4677 `(org-agenda-type search
4678 org-last-args (,todo-only ,string ,edit-at)
4679 org-redo-cmd ,org-agenda-redo-command
4680 org-series-cmd ,org-cmd))
4681 (org-agenda-finalize)
4682 (setq buffer-read-only t))))
4684 ;;; Agenda TODO list
4686 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4687 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4688 (concat
4689 (if (or (equal keywords "ALL") (not keywords))
4690 (propertize "ALL" 'face 'warning)
4691 (mapconcat
4692 (lambda (kw)
4693 (propertize kw 'face (org-get-todo-face kw)))
4694 (org-split-string keywords "|")
4695 "|"))
4696 "\n"))
4698 (defvar org-select-this-todo-keyword nil)
4699 (defvar org-last-arg nil)
4701 ;;;###autoload
4702 (defun org-todo-list (&optional arg)
4703 "Show all (not done) TODO entries from all agenda file in a single list.
4704 The prefix arg can be used to select a specific TODO keyword and limit
4705 the list to these. When using \\[universal-argument], you will be prompted
4706 for a keyword. A numeric prefix directly selects the Nth keyword in
4707 `org-todo-keywords-1'."
4708 (interactive "P")
4709 (if org-agenda-overriding-arguments
4710 (setq arg org-agenda-overriding-arguments))
4711 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4712 (let* ((today (org-today))
4713 (date (calendar-gregorian-from-absolute today))
4714 (kwds org-todo-keywords-for-agenda)
4715 (completion-ignore-case t)
4716 (org-select-this-todo-keyword
4717 (if (stringp arg) arg
4718 (and arg (integerp arg) (> arg 0)
4719 (nth (1- arg) kwds))))
4720 rtn rtnall files file pos)
4721 (when (equal arg '(4))
4722 (setq org-select-this-todo-keyword
4723 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4724 (mapcar 'list kwds) nil nil)))
4725 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4726 (catch 'exit
4727 (if org-agenda-sticky
4728 (setq org-agenda-buffer-name
4729 (if (stringp org-select-this-todo-keyword)
4730 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4731 org-select-this-todo-keyword)
4732 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4733 (org-agenda-prepare "TODO")
4734 (org-compile-prefix-format 'todo)
4735 (org-set-sorting-strategy 'todo)
4736 (setq org-agenda-redo-command
4737 `(org-todo-list (or (and (numberp current-prefix-arg)
4738 current-prefix-arg)
4739 ,org-select-this-todo-keyword
4740 current-prefix-arg ,arg)))
4741 (setq files (org-agenda-files nil 'ifmode)
4742 rtnall nil)
4743 (while (setq file (pop files))
4744 (catch 'nextfile
4745 (org-check-agenda-file file)
4746 (setq rtn (org-agenda-get-day-entries file date :todo))
4747 (setq rtnall (append rtnall rtn))))
4748 (if org-agenda-overriding-header
4749 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4750 nil 'face 'org-agenda-structure) "\n")
4751 (insert "Global list of TODO items of type: ")
4752 (add-text-properties (point-min) (1- (point))
4753 (list 'face 'org-agenda-structure
4754 'short-heading
4755 (concat "ToDo: "
4756 (or org-select-this-todo-keyword "ALL"))))
4757 (org-agenda-mark-header-line (point-min))
4758 (insert (org-agenda-propertize-selected-todo-keywords
4759 org-select-this-todo-keyword))
4760 (setq pos (point))
4761 (unless org-agenda-multi
4762 (insert "Available with `N r': (0)[ALL]")
4763 (let ((n 0) s)
4764 (mapc (lambda (x)
4765 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4766 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4767 (insert "\n "))
4768 (insert " " s))
4769 kwds))
4770 (insert "\n"))
4771 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4772 (org-agenda-mark-header-line (point-min))
4773 (when rtnall
4774 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4775 (goto-char (point-min))
4776 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4777 (add-text-properties (point-min) (point-max)
4778 `(org-agenda-type todo
4779 org-last-args ,arg
4780 org-redo-cmd ,org-agenda-redo-command
4781 org-series-cmd ,org-cmd))
4782 (org-agenda-finalize)
4783 (setq buffer-read-only t))))
4785 ;;; Agenda tags match
4787 ;;;###autoload
4788 (defun org-tags-view (&optional todo-only match)
4789 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4790 The prefix arg TODO-ONLY limits the search to TODO entries."
4791 (interactive "P")
4792 (if org-agenda-overriding-arguments
4793 (setq todo-only (car org-agenda-overriding-arguments)
4794 match (nth 1 org-agenda-overriding-arguments)))
4795 (let* ((org-tags-match-list-sublevels
4796 org-tags-match-list-sublevels)
4797 (completion-ignore-case t)
4798 rtn rtnall files file pos matcher
4799 buffer)
4800 (when (and (stringp match) (not (string-match "\\S-" match)))
4801 (setq match nil))
4802 (catch 'exit
4803 (if org-agenda-sticky
4804 (setq org-agenda-buffer-name
4805 (if (stringp match)
4806 (format "*Org Agenda(%s:%s)*"
4807 (or org-keys (or (and todo-only "M") "m")) match)
4808 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4809 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4810 ;; expanding tags within `org-make-tags-matcher'
4811 (org-agenda-prepare (concat "TAGS " match))
4812 (setq matcher (org-make-tags-matcher match)
4813 match (car matcher) matcher (cdr matcher))
4814 (org-compile-prefix-format 'tags)
4815 (org-set-sorting-strategy 'tags)
4816 (setq org-agenda-query-string match)
4817 (setq org-agenda-redo-command
4818 (list 'org-tags-view `(quote ,todo-only)
4819 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4820 (setq files (org-agenda-files nil 'ifmode)
4821 rtnall nil)
4822 (while (setq file (pop files))
4823 (catch 'nextfile
4824 (org-check-agenda-file file)
4825 (setq buffer (if (file-exists-p file)
4826 (org-get-agenda-file-buffer file)
4827 (error "No such file %s" file)))
4828 (if (not buffer)
4829 ;; If file does not exist, error message to agenda
4830 (setq rtn (list
4831 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4832 rtnall (append rtnall rtn))
4833 (with-current-buffer buffer
4834 (unless (derived-mode-p 'org-mode)
4835 (error "Agenda file %s is not in `org-mode'" file))
4836 (save-excursion
4837 (save-restriction
4838 (if (eq buffer org-agenda-restrict)
4839 (narrow-to-region org-agenda-restrict-begin
4840 org-agenda-restrict-end)
4841 (widen))
4842 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4843 (setq rtnall (append rtnall rtn))))))))
4844 (if org-agenda-overriding-header
4845 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4846 nil 'face 'org-agenda-structure) "\n")
4847 (insert "Headlines with TAGS match: ")
4848 (add-text-properties (point-min) (1- (point))
4849 (list 'face 'org-agenda-structure
4850 'short-heading
4851 (concat "Match: " match)))
4852 (setq pos (point))
4853 (insert match "\n")
4854 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4855 (setq pos (point))
4856 (unless org-agenda-multi
4857 (insert "Press `C-u r' to search again with new search string\n"))
4858 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4859 (org-agenda-mark-header-line (point-min))
4860 (when rtnall
4861 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4862 (goto-char (point-min))
4863 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4864 (add-text-properties (point-min) (point-max)
4865 `(org-agenda-type tags
4866 org-last-args (,todo-only ,match)
4867 org-redo-cmd ,org-agenda-redo-command
4868 org-series-cmd ,org-cmd))
4869 (org-agenda-finalize)
4870 (setq buffer-read-only t))))
4872 ;;; Agenda Finding stuck projects
4874 (defvar org-agenda-skip-regexp nil
4875 "Regular expression used in skipping subtrees for the agenda.
4876 This is basically a temporary global variable that can be set and then
4877 used by user-defined selections using `org-agenda-skip-function'.")
4879 (defvar org-agenda-overriding-header nil
4880 "When set during agenda, todo and tags searches it replaces the header.
4881 This variable should not be set directly, but custom commands can bind it
4882 in the options section.")
4884 (defun org-agenda-skip-entry-when-regexp-matches ()
4885 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4886 If yes, it returns the end position of this entry, causing agenda commands
4887 to skip the entry but continuing the search in the subtree. This is a
4888 function that can be put into `org-agenda-skip-function' for the duration
4889 of a command."
4890 (let ((end (save-excursion (org-end-of-subtree t)))
4891 skip)
4892 (save-excursion
4893 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4894 (and skip end)))
4896 (defun org-agenda-skip-subtree-when-regexp-matches ()
4897 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4898 If yes, it returns the end position of this tree, causing agenda commands
4899 to skip this subtree. This is a function that can be put into
4900 `org-agenda-skip-function' for the duration of a command."
4901 (let ((end (save-excursion (org-end-of-subtree t)))
4902 skip)
4903 (save-excursion
4904 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4905 (and skip end)))
4907 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4908 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4909 If yes, it returns the end position of the current entry (NOT the tree),
4910 causing agenda commands to skip the entry but continuing the search in
4911 the subtree. This is a function that can be put into
4912 `org-agenda-skip-function' for the duration of a command. An important
4913 use of this function is for the stuck project list."
4914 (let ((end (save-excursion (org-end-of-subtree t)))
4915 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4916 skip)
4917 (save-excursion
4918 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4919 (and skip entry-end)))
4921 (defun org-agenda-skip-entry-if (&rest conditions)
4922 "Skip entry if any of CONDITIONS is true.
4923 See `org-agenda-skip-if' for details."
4924 (org-agenda-skip-if nil conditions))
4926 (defun org-agenda-skip-subtree-if (&rest conditions)
4927 "Skip subtree if any of CONDITIONS is true.
4928 See `org-agenda-skip-if' for details."
4929 (org-agenda-skip-if t conditions))
4931 (defun org-agenda-skip-if (subtree conditions)
4932 "Checks current entity for CONDITIONS.
4933 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4934 the entry (i.e. the text before the next heading) is checked.
4936 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4937 from different tests. Valid conditions are:
4939 scheduled Check if there is a scheduled cookie
4940 notscheduled Check if there is no scheduled cookie
4941 deadline Check if there is a deadline
4942 notdeadline Check if there is no deadline
4943 timestamp Check if there is a timestamp (also deadline or scheduled)
4944 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4945 regexp Check if regexp matches
4946 notregexp Check if regexp does not match.
4947 todo Check if TODO keyword matches
4948 nottodo Check if TODO keyword does not match
4950 The regexp is taken from the conditions list, it must come right after
4951 the `regexp' or `notregexp' element.
4953 `todo' and `nottodo' accept as an argument a list of todo
4954 keywords, which may include \"*\" to match any todo keyword.
4956 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4958 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4960 Instead of a list, a keyword class may be given. For example:
4962 (org-agenda-skip-entry-if 'nottodo 'done)
4964 would skip entries that haven't been marked with any of \"DONE\"
4965 keywords. Possible classes are: `todo', `done', `any'.
4967 If any of these conditions is met, this function returns the end point of
4968 the entity, causing the search to continue from there. This is a function
4969 that can be put into `org-agenda-skip-function' for the duration of a command."
4970 (let (beg end m)
4971 (org-back-to-heading t)
4972 (setq beg (point)
4973 end (if subtree
4974 (progn (org-end-of-subtree t) (point))
4975 (progn (outline-next-heading) (1- (point)))))
4976 (goto-char beg)
4977 (and
4979 (and (memq 'scheduled conditions)
4980 (re-search-forward org-scheduled-time-regexp end t))
4981 (and (memq 'notscheduled conditions)
4982 (not (re-search-forward org-scheduled-time-regexp end t)))
4983 (and (memq 'deadline conditions)
4984 (re-search-forward org-deadline-time-regexp end t))
4985 (and (memq 'notdeadline conditions)
4986 (not (re-search-forward org-deadline-time-regexp end t)))
4987 (and (memq 'timestamp conditions)
4988 (re-search-forward org-ts-regexp end t))
4989 (and (memq 'nottimestamp conditions)
4990 (not (re-search-forward org-ts-regexp end t)))
4991 (and (setq m (memq 'regexp conditions))
4992 (stringp (nth 1 m))
4993 (re-search-forward (nth 1 m) end t))
4994 (and (setq m (memq 'notregexp conditions))
4995 (stringp (nth 1 m))
4996 (not (re-search-forward (nth 1 m) end t)))
4997 (and (or
4998 (setq m (memq 'nottodo conditions))
4999 (setq m (memq 'todo-unblocked conditions))
5000 (setq m (memq 'nottodo-unblocked conditions))
5001 (setq m (memq 'todo conditions)))
5002 (org-agenda-skip-if-todo m end)))
5003 end)))
5005 (defun org-agenda-skip-if-todo (args end)
5006 "Helper function for `org-agenda-skip-if', do not use it directly.
5007 ARGS is a list with first element either `todo', `nottodo',
5008 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
5009 a list of TODO keywords, or a state symbol `todo' or `done' or
5010 `any'."
5011 (let ((kw (car args))
5012 (arg (cadr args))
5013 todo-wds todo-re)
5014 (setq todo-wds
5015 (org-uniquify
5016 (cond
5017 ((listp arg) ;; list of keywords
5018 (if (member "*" arg)
5019 (mapcar 'substring-no-properties org-todo-keywords-1)
5020 arg))
5021 ((symbolp arg) ;; keyword class name
5022 (cond
5023 ((eq arg 'todo)
5024 (org-delete-all org-done-keywords
5025 (mapcar 'substring-no-properties
5026 org-todo-keywords-1)))
5027 ((eq arg 'done) org-done-keywords)
5028 ((eq arg 'any)
5029 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5030 (setq todo-re
5031 (concat "^\\*+[ \t]+\\<\\("
5032 (mapconcat 'identity todo-wds "\\|")
5033 "\\)\\>"))
5034 (cond
5035 ((eq kw 'todo) (re-search-forward todo-re end t))
5036 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5037 ((eq kw 'todo-unblocked)
5038 (catch 'unblocked
5039 (while (re-search-forward todo-re end t)
5040 (or (org-entry-blocked-p) (throw 'unblocked t)))
5041 nil))
5042 ((eq kw 'nottodo-unblocked)
5043 (catch 'unblocked
5044 (while (re-search-forward todo-re end t)
5045 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5049 ;;;###autoload
5050 (defun org-agenda-list-stuck-projects (&rest ignore)
5051 "Create agenda view for projects that are stuck.
5052 Stuck projects are project that have no next actions. For the definitions
5053 of what a project is and how to check if it stuck, customize the variable
5054 `org-stuck-projects'."
5055 (interactive)
5056 (let* ((org-agenda-skip-function
5057 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5058 ;; We could have used org-agenda-skip-if here.
5059 (org-agenda-overriding-header
5060 (or org-agenda-overriding-header "List of stuck projects: "))
5061 (matcher (nth 0 org-stuck-projects))
5062 (todo (nth 1 org-stuck-projects))
5063 (todo-wds (if (member "*" todo)
5064 (progn
5065 (org-agenda-prepare-buffers (org-agenda-files
5066 nil 'ifmode))
5067 (org-delete-all
5068 org-done-keywords-for-agenda
5069 (copy-sequence org-todo-keywords-for-agenda)))
5070 todo))
5071 (todo-re (concat "^\\*+[ \t]+\\("
5072 (mapconcat 'identity todo-wds "\\|")
5073 "\\)\\>"))
5074 (tags (nth 2 org-stuck-projects))
5075 (tags-re (if (member "*" tags)
5076 (concat org-outline-regexp-bol
5077 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5078 (if tags
5079 (concat org-outline-regexp-bol
5080 ".*:\\("
5081 (mapconcat 'identity tags "\\|")
5082 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5083 (gen-re (nth 3 org-stuck-projects))
5084 (re-list
5085 (delq nil
5086 (list
5087 (if todo todo-re)
5088 (if tags tags-re)
5089 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5090 gen-re)))))
5091 (setq org-agenda-skip-regexp
5092 (if re-list
5093 (mapconcat 'identity re-list "\\|")
5094 (error "No information how to identify unstuck projects")))
5095 (org-tags-view nil matcher)
5096 (setq org-agenda-buffer-name (buffer-name))
5097 (with-current-buffer org-agenda-buffer-name
5098 (setq org-agenda-redo-command
5099 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5101 ;;; Diary integration
5103 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5104 (defvar diary-list-entries-hook)
5105 (defvar diary-time-regexp)
5106 (defun org-get-entries-from-diary (date)
5107 "Get the (Emacs Calendar) diary entries for DATE."
5108 (require 'diary-lib)
5109 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5110 (diary-display-hook '(fancy-diary-display))
5111 (diary-display-function 'fancy-diary-display)
5112 (pop-up-frames nil)
5113 (diary-list-entries-hook
5114 (cons 'org-diary-default-entry diary-list-entries-hook))
5115 (diary-file-name-prefix nil) ; turn this feature off
5116 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5117 entries
5118 (org-disable-agenda-to-diary t))
5119 (save-excursion
5120 (save-window-excursion
5121 (funcall (if (fboundp 'diary-list-entries)
5122 'diary-list-entries 'list-diary-entries)
5123 date 1)))
5124 (if (not (get-buffer diary-fancy-buffer))
5125 (setq entries nil)
5126 (with-current-buffer diary-fancy-buffer
5127 (setq buffer-read-only nil)
5128 (if (zerop (buffer-size))
5129 ;; No entries
5130 (setq entries nil)
5131 ;; Omit the date and other unnecessary stuff
5132 (org-agenda-cleanup-fancy-diary)
5133 ;; Add prefix to each line and extend the text properties
5134 (if (zerop (buffer-size))
5135 (setq entries nil)
5136 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5137 (setq entries
5138 (with-temp-buffer
5139 (insert entries) (goto-char (point-min))
5140 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5141 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5142 (replace-match (concat "; " (match-string 1)))))
5143 (buffer-string)))))
5144 (set-buffer-modified-p nil)
5145 (kill-buffer diary-fancy-buffer)))
5146 (when entries
5147 (setq entries (org-split-string entries "\n"))
5148 (setq entries
5149 (mapcar
5150 (lambda (x)
5151 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5152 ;; Extend the text properties to the beginning of the line
5153 (org-add-props x (text-properties-at (1- (length x)) x)
5154 'type "diary" 'date date 'face 'org-agenda-diary))
5155 entries)))))
5157 (defvar org-agenda-cleanup-fancy-diary-hook nil
5158 "Hook run when the fancy diary buffer is cleaned up.")
5160 (defun org-agenda-cleanup-fancy-diary ()
5161 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5162 This gets rid of the date, the underline under the date, and
5163 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5164 date. It also removes lines that contain only whitespace."
5165 (goto-char (point-min))
5166 (if (looking-at ".*?:[ \t]*")
5167 (progn
5168 (replace-match "")
5169 (re-search-forward "\n=+$" nil t)
5170 (replace-match "")
5171 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5172 (re-search-forward "\n=+$" nil t)
5173 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5174 (goto-char (point-min))
5175 (while (re-search-forward "^ +\n" nil t)
5176 (replace-match ""))
5177 (goto-char (point-min))
5178 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5179 (replace-match ""))
5180 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5182 ;; Make sure entries from the diary have the right text properties.
5183 (eval-after-load "diary-lib"
5184 '(if (boundp 'diary-modify-entry-list-string-function)
5185 ;; We can rely on the hook, nothing to do
5187 ;; Hook not available, must use advice to make this work
5188 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5189 "Make the position visible."
5190 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5191 (stringp string)
5192 buffer-file-name)
5193 (setq string (org-modify-diary-entry-string string))))))
5195 (defun org-modify-diary-entry-string (string)
5196 "Add text properties to string, allowing org-mode to act on it."
5197 (org-add-props string nil
5198 'mouse-face 'highlight
5199 'help-echo (if buffer-file-name
5200 (format "mouse-2 or RET jump to diary file %s"
5201 (abbreviate-file-name buffer-file-name))
5203 'org-agenda-diary-link t
5204 'org-marker (org-agenda-new-marker (point-at-bol))))
5206 (defun org-diary-default-entry ()
5207 "Add a dummy entry to the diary.
5208 Needed to avoid empty dates which mess up holiday display."
5209 ;; Catch the error if dealing with the new add-to-diary-alist
5210 (when org-disable-agenda-to-diary
5211 (condition-case nil
5212 (org-add-to-diary-list original-date "Org-mode dummy" "")
5213 (error
5214 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5216 (defun org-add-to-diary-list (&rest args)
5217 (if (fboundp 'diary-add-to-list)
5218 (apply 'diary-add-to-list args)
5219 (apply 'add-to-diary-list args)))
5221 (defvar org-diary-last-run-time nil)
5223 ;;;###autoload
5224 (defun org-diary (&rest args)
5225 "Return diary information from org files.
5226 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5227 It accesses org files and extracts information from those files to be
5228 listed in the diary. The function accepts arguments specifying what
5229 items should be listed. For a list of arguments allowed here, see the
5230 variable `org-agenda-entry-types'.
5232 The call in the diary file should look like this:
5234 &%%(org-diary) ~/path/to/some/orgfile.org
5236 Use a separate line for each org file to check. Or, if you omit the file name,
5237 all files listed in `org-agenda-files' will be checked automatically:
5239 &%%(org-diary)
5241 If you don't give any arguments (as in the example above), the default value
5242 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5243 So the example above may also be written as
5245 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5247 The function expects the lisp variables `entry' and `date' to be provided
5248 by the caller, because this is how the calendar works. Don't use this
5249 function from a program - use `org-agenda-get-day-entries' instead."
5250 (when (> (- (org-float-time)
5251 org-agenda-last-marker-time)
5253 ;; I am not sure if this works with sticky agendas, because the marker
5254 ;; list is then no longer a global variable.
5255 (org-agenda-reset-markers))
5256 (org-compile-prefix-format 'agenda)
5257 (org-set-sorting-strategy 'agenda)
5258 (setq args (or args org-agenda-entry-types))
5259 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5260 (list entry)
5261 (org-agenda-files t)))
5262 (time (org-float-time))
5263 file rtn results)
5264 (when (or (not org-diary-last-run-time)
5265 (> (- time
5266 org-diary-last-run-time)
5268 (org-agenda-prepare-buffers files))
5269 (setq org-diary-last-run-time time)
5270 ;; If this is called during org-agenda, don't return any entries to
5271 ;; the calendar. Org Agenda will list these entries itself.
5272 (if org-disable-agenda-to-diary (setq files nil))
5273 (while (setq file (pop files))
5274 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5275 (setq results (append results rtn)))
5276 (when results
5277 (setq results
5278 (mapcar (lambda (i) (replace-regexp-in-string
5279 org-bracket-link-regexp "\\3" i)) results))
5280 (concat (org-agenda-finalize-entries results) "\n"))))
5282 ;;; Agenda entry finders
5284 (defun org-agenda-get-day-entries (file date &rest args)
5285 "Does the work for `org-diary' and `org-agenda'.
5286 FILE is the path to a file to be checked for entries. DATE is date like
5287 the one returned by `calendar-current-date'. ARGS are symbols indicating
5288 which kind of entries should be extracted. For details about these, see
5289 the documentation of `org-diary'."
5290 (setq args (or args org-agenda-entry-types))
5291 (let* ((org-startup-folded nil)
5292 (org-startup-align-all-tables nil)
5293 (buffer (if (file-exists-p file)
5294 (org-get-agenda-file-buffer file)
5295 (error "No such file %s" file)))
5296 arg results rtn deadline-results)
5297 (if (not buffer)
5298 ;; If file does not exist, make sure an error message ends up in diary
5299 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5300 (with-current-buffer buffer
5301 (unless (derived-mode-p 'org-mode)
5302 (error "Agenda file %s is not in `org-mode'" file))
5303 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5304 (let ((case-fold-search nil))
5305 (save-excursion
5306 (save-restriction
5307 (if (eq buffer org-agenda-restrict)
5308 (narrow-to-region org-agenda-restrict-begin
5309 org-agenda-restrict-end)
5310 (widen))
5311 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5312 (while (setq arg (pop args))
5313 (cond
5314 ((and (eq arg :todo)
5315 (equal date (calendar-gregorian-from-absolute
5316 (org-today))))
5317 (setq rtn (org-agenda-get-todos))
5318 (setq results (append results rtn)))
5319 ((eq arg :timestamp)
5320 (setq rtn (org-agenda-get-blocks))
5321 (setq results (append results rtn))
5322 (setq rtn (org-agenda-get-timestamps deadline-results))
5323 (setq results (append results rtn)))
5324 ((eq arg :sexp)
5325 (setq rtn (org-agenda-get-sexps))
5326 (setq results (append results rtn)))
5327 ((eq arg :scheduled)
5328 (setq rtn (org-agenda-get-scheduled deadline-results))
5329 (setq results (append results rtn)))
5330 ((eq arg :scheduled*)
5331 (setq rtn (org-agenda-get-scheduled deadline-results t))
5332 (setq results (append results rtn)))
5333 ((eq arg :closed)
5334 (setq rtn (org-agenda-get-progress))
5335 (setq results (append results rtn)))
5336 ((eq arg :deadline)
5337 (setq rtn (org-agenda-get-deadlines))
5338 (setq deadline-results (copy-sequence rtn))
5339 (setq results (append results rtn)))
5340 ((eq arg :deadline*)
5341 (setq rtn (org-agenda-get-deadlines t))
5342 (setq deadline-results (copy-sequence rtn))
5343 (setq results (append results rtn))))))))
5344 results))))
5346 (defsubst org-em (x y list)
5347 "Is X or Y a member of LIST?"
5348 (or (memq x list) (memq y list)))
5350 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5351 (defvar org-agenda-sorting-strategy-selected nil)
5353 (defun org-agenda-get-todos ()
5354 "Return the TODO information for agenda display."
5355 (let* ((props (list 'face nil
5356 'done-face 'org-agenda-done
5357 'org-not-done-regexp org-not-done-regexp
5358 'org-todo-regexp org-todo-regexp
5359 'org-complex-heading-regexp org-complex-heading-regexp
5360 'mouse-face 'highlight
5361 'help-echo
5362 (format "mouse-2 or RET jump to org file %s"
5363 (abbreviate-file-name buffer-file-name))))
5364 (regexp (format org-heading-keyword-regexp-format
5365 (cond
5366 ((and org-select-this-todo-keyword
5367 (equal org-select-this-todo-keyword "*"))
5368 org-todo-regexp)
5369 (org-select-this-todo-keyword
5370 (concat "\\("
5371 (mapconcat 'identity
5372 (org-split-string
5373 org-select-this-todo-keyword
5374 "|")
5375 "\\|") "\\)"))
5376 (t org-not-done-regexp))))
5377 marker priority category level tags todo-state ts-date ts-date-type
5378 ee txt beg end inherited-tags todo-state-end-pos)
5379 (goto-char (point-min))
5380 (while (re-search-forward regexp nil t)
5381 (catch :skip
5382 (save-match-data
5383 (beginning-of-line)
5384 (org-agenda-skip)
5385 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5386 (unless (and (setq todo-state (org-get-todo-state))
5387 (setq todo-state-end-pos (match-end 2)))
5388 (goto-char end)
5389 (throw :skip nil))
5390 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5391 (goto-char (1+ beg))
5392 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5393 (throw :skip nil)))
5394 (goto-char (match-beginning 2))
5395 (setq marker (org-agenda-new-marker (match-beginning 0))
5396 category (org-get-category)
5397 ts-date (let (ts)
5398 (save-match-data
5399 (cond ((org-em 'scheduled-up 'scheduled-down
5400 org-agenda-sorting-strategy-selected)
5401 (setq ts (org-entry-get (point) "SCHEDULED")
5402 ts-date-type " scheduled"))
5403 ((org-em 'deadline-up 'deadline-down
5404 org-agenda-sorting-strategy-selected)
5405 (setq ts (org-entry-get (point) "DEADLINE")
5406 ts-date-type " deadline"))
5407 ((org-em 'ts-up 'ts-down
5408 org-agenda-sorting-strategy-selected)
5409 (setq ts (org-entry-get (point) "TIMESTAMP")
5410 ts-date-type " timestamp"))
5411 ((org-em 'tsia-up 'tsia-down
5412 org-agenda-sorting-strategy-selected)
5413 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5414 ts-date-type " timestamp_ia"))
5415 ((org-em 'timestamp-up 'timestamp-down
5416 org-agenda-sorting-strategy-selected)
5417 (setq ts (or (org-entry-get (point) "SCHEDULED")
5418 (org-entry-get (point) "DEADLINE")
5419 (org-entry-get (point) "TIMESTAMP")
5420 (org-entry-get (point) "TIMESTAMP_IA"))
5421 ts-date-type ""))
5422 (t (setq ts-date-type "")))
5423 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5424 txt (org-trim (buffer-substring (match-beginning 2) (match-end 0)))
5425 inherited-tags
5426 (or (eq org-agenda-show-inherited-tags 'always)
5427 (and (listp org-agenda-show-inherited-tags)
5428 (memq 'todo org-agenda-show-inherited-tags))
5429 (and (eq org-agenda-show-inherited-tags t)
5430 (or (eq org-agenda-use-tag-inheritance t)
5431 (memq 'todo org-agenda-use-tag-inheritance))))
5432 tags (org-get-tags-at nil (not inherited-tags))
5433 level (make-string (org-reduced-level (org-outline-level)) ? )
5434 txt (org-agenda-format-item "" txt level category tags t)
5435 priority (1+ (org-get-priority txt)))
5436 (org-add-props txt props
5437 'org-marker marker 'org-hd-marker marker
5438 'priority priority
5439 'level level
5440 'ts-date ts-date
5441 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5442 (push txt ee)
5443 (if org-agenda-todo-list-sublevels
5444 (goto-char todo-state-end-pos)
5445 (org-end-of-subtree 'invisible))))
5446 (nreverse ee)))
5448 (defun org-agenda-todo-custom-ignore-p (time n)
5449 "Check whether timestamp is farther away than n number of days.
5450 This function is invoked if `org-agenda-todo-ignore-deadlines',
5451 `org-agenda-todo-ignore-scheduled' or
5452 `org-agenda-todo-ignore-timestamp' is set to an integer."
5453 (let ((days (org-time-stamp-to-now
5454 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5455 (if (>= n 0)
5456 (>= days n)
5457 (<= days n))))
5459 ;;;###autoload
5460 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5461 (&optional end)
5462 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5463 (when (or org-agenda-todo-ignore-with-date
5464 org-agenda-todo-ignore-scheduled
5465 org-agenda-todo-ignore-deadlines
5466 org-agenda-todo-ignore-timestamp)
5467 (setq end (or end (save-excursion (outline-next-heading) (point))))
5468 (save-excursion
5469 (or (and org-agenda-todo-ignore-with-date
5470 (re-search-forward org-ts-regexp end t))
5471 (and org-agenda-todo-ignore-scheduled
5472 (re-search-forward org-scheduled-time-regexp end t)
5473 (cond
5474 ((eq org-agenda-todo-ignore-scheduled 'future)
5475 (> (org-time-stamp-to-now
5476 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5477 ((eq org-agenda-todo-ignore-scheduled 'past)
5478 (<= (org-time-stamp-to-now
5479 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5480 ((numberp org-agenda-todo-ignore-scheduled)
5481 (org-agenda-todo-custom-ignore-p
5482 (match-string 1) org-agenda-todo-ignore-scheduled))
5483 (t)))
5484 (and org-agenda-todo-ignore-deadlines
5485 (re-search-forward org-deadline-time-regexp end t)
5486 (cond
5487 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5488 ((eq org-agenda-todo-ignore-deadlines 'far)
5489 (not (org-deadline-close (match-string 1))))
5490 ((eq org-agenda-todo-ignore-deadlines 'future)
5491 (> (org-time-stamp-to-now
5492 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5493 ((eq org-agenda-todo-ignore-deadlines 'past)
5494 (<= (org-time-stamp-to-now
5495 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5496 ((numberp org-agenda-todo-ignore-deadlines)
5497 (org-agenda-todo-custom-ignore-p
5498 (match-string 1) org-agenda-todo-ignore-deadlines))
5499 (t (org-deadline-close (match-string 1)))))
5500 (and org-agenda-todo-ignore-timestamp
5501 (let ((buffer (current-buffer))
5502 (regexp
5503 (concat
5504 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5505 (start (point)))
5506 ;; Copy current buffer into a temporary one
5507 (with-temp-buffer
5508 (insert-buffer-substring buffer start end)
5509 (goto-char (point-min))
5510 ;; Delete SCHEDULED and DEADLINE items
5511 (while (re-search-forward regexp end t)
5512 (delete-region (match-beginning 0) (match-end 0)))
5513 (goto-char (point-min))
5514 ;; No search for timestamp left
5515 (when (re-search-forward org-ts-regexp nil t)
5516 (cond
5517 ((eq org-agenda-todo-ignore-timestamp 'future)
5518 (> (org-time-stamp-to-now
5519 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5520 ((eq org-agenda-todo-ignore-timestamp 'past)
5521 (<= (org-time-stamp-to-now
5522 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5523 ((numberp org-agenda-todo-ignore-timestamp)
5524 (org-agenda-todo-custom-ignore-p
5525 (match-string 1) org-agenda-todo-ignore-timestamp))
5526 (t))))))))))
5528 (defun org-agenda-get-timestamps (&optional deadline-results)
5529 "Return the date stamp information for agenda display."
5530 (let* ((props (list 'face 'org-agenda-calendar-event
5531 'org-not-done-regexp org-not-done-regexp
5532 'org-todo-regexp org-todo-regexp
5533 'org-complex-heading-regexp org-complex-heading-regexp
5534 'mouse-face 'highlight
5535 'help-echo
5536 (format "mouse-2 or RET jump to org file %s"
5537 (abbreviate-file-name buffer-file-name))))
5538 (d1 (calendar-absolute-from-gregorian date))
5540 (deadline-position-alist
5541 (mapcar (lambda (a) (and (setq mm (get-text-property
5542 0 'org-hd-marker a))
5543 (cons (marker-position mm) a)))
5544 deadline-results))
5545 (remove-re org-ts-regexp)
5546 (regexp
5547 (concat
5548 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5549 (regexp-quote
5550 (substring
5551 (format-time-string
5552 (car org-time-stamp-formats)
5553 (apply 'encode-time ; DATE bound by calendar
5554 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5555 1 11))
5556 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5557 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5558 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5559 donep tmp priority category level ee txt timestr tags
5560 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5561 inherited-tags ts-date)
5562 (goto-char (point-min))
5563 (while (setq end-of-match (re-search-forward regexp nil t))
5564 (setq b0 (match-beginning 0)
5565 b3 (match-beginning 3) e3 (match-end 3)
5566 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5567 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5568 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5569 (member todo-state
5570 org-agenda-repeating-timestamp-show-all)))
5571 (catch :skip
5572 (and (org-at-date-range-p) (throw :skip nil))
5573 (org-agenda-skip)
5574 (if (and (match-end 1)
5575 (not (= d1 (org-time-string-to-absolute
5576 (match-string 1) d1 nil show-all
5577 (current-buffer) b0))))
5578 (throw :skip nil))
5579 (if (and e3
5580 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5581 (throw :skip nil))
5582 (setq tmp (buffer-substring (max (point-min)
5583 (- b0 org-ds-keyword-length))
5585 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5586 inactivep (= (char-after b0) ?\[)
5587 deadlinep (string-match org-deadline-regexp tmp)
5588 scheduledp (string-match org-scheduled-regexp tmp)
5589 closedp (and org-agenda-include-inactive-timestamps
5590 (string-match org-closed-string tmp))
5591 clockp (and org-agenda-include-inactive-timestamps
5592 (or (string-match org-clock-string tmp)
5593 (string-match "]-+\\'" tmp)))
5594 warntime (get-text-property (point) 'org-appt-warntime)
5595 donep (member todo-state org-done-keywords))
5596 (if (or scheduledp deadlinep closedp clockp
5597 (and donep org-agenda-skip-timestamp-if-done))
5598 (throw :skip t))
5599 (if (string-match ">" timestr)
5600 ;; substring should only run to end of time stamp
5601 (setq timestr (substring timestr 0 (match-end 0))))
5602 (setq marker (org-agenda-new-marker b0)
5603 category (org-get-category b0))
5604 (save-excursion
5605 (if (not (re-search-backward org-outline-regexp-bol nil t))
5606 (throw :skip nil)
5607 (goto-char (match-beginning 0))
5608 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5609 (assoc (point) deadline-position-alist))
5610 (throw :skip nil))
5611 (setq hdmarker (org-agenda-new-marker)
5612 inherited-tags
5613 (or (eq org-agenda-show-inherited-tags 'always)
5614 (and (listp org-agenda-show-inherited-tags)
5615 (memq 'agenda org-agenda-show-inherited-tags))
5616 (and (eq org-agenda-show-inherited-tags t)
5617 (or (eq org-agenda-use-tag-inheritance t)
5618 (memq 'agenda org-agenda-use-tag-inheritance))))
5619 tags (org-get-tags-at nil (not inherited-tags))
5620 level (make-string (org-reduced-level (org-outline-level)) ? ))
5621 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5622 (setq head (or (match-string 1) ""))
5623 (setq txt (org-agenda-format-item
5624 (if inactivep org-agenda-inactive-leader nil)
5625 head level category tags timestr
5626 remove-re habitp)))
5627 (setq priority (org-get-priority txt))
5628 (org-add-props txt props 'priority priority
5629 'org-marker marker 'org-hd-marker hdmarker
5630 'date date
5631 'level level
5632 'ts-date
5633 (ignore-errors (org-time-string-to-absolute timestr))
5634 'todo-state todo-state
5635 'warntime warntime
5636 'type "timestamp")
5637 (push txt ee))
5638 (if org-agenda-skip-additional-timestamps-same-entry
5639 (outline-next-heading)
5640 (goto-char end-of-match))))
5641 (nreverse ee)))
5643 (defun org-agenda-get-sexps ()
5644 "Return the sexp information for agenda display."
5645 (require 'diary-lib)
5646 (let* ((props (list 'face 'org-agenda-calendar-sexp
5647 'mouse-face 'highlight
5648 'help-echo
5649 (format "mouse-2 or RET jump to org file %s"
5650 (abbreviate-file-name buffer-file-name))))
5651 (regexp "^&?%%(")
5652 marker category extra level ee txt tags entry
5653 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5654 (goto-char (point-min))
5655 (while (re-search-forward regexp nil t)
5656 (catch :skip
5657 (org-agenda-skip)
5658 (setq beg (match-beginning 0))
5659 (goto-char (1- (match-end 0)))
5660 (setq b (point))
5661 (forward-sexp 1)
5662 (setq sexp (buffer-substring b (point)))
5663 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5664 (org-trim (match-string 1))
5665 ""))
5666 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5667 (when result
5668 (setq marker (org-agenda-new-marker beg)
5669 level (make-string (org-reduced-level (org-outline-level)) ? )
5670 category (org-get-category beg)
5671 inherited-tags
5672 (or (eq org-agenda-show-inherited-tags 'always)
5673 (and (listp org-agenda-show-inherited-tags)
5674 (memq 'agenda org-agenda-show-inherited-tags))
5675 (and (eq org-agenda-show-inherited-tags t)
5676 (or (eq org-agenda-use-tag-inheritance t)
5677 (memq 'agenda org-agenda-use-tag-inheritance))))
5678 tags (org-get-tags-at nil (not inherited-tags))
5679 todo-state (org-get-todo-state)
5680 warntime (get-text-property (point) 'org-appt-warntime)
5681 extra nil)
5683 (dolist (r (if (stringp result)
5684 (list result)
5685 result)) ;; we expect a list here
5686 (when (and org-agenda-diary-sexp-prefix
5687 (string-match org-agenda-diary-sexp-prefix r))
5688 (setq extra (match-string 0 r)
5689 r (replace-match "" nil nil r)))
5690 (if (string-match "\\S-" r)
5691 (setq txt r)
5692 (setq txt "SEXP entry returned empty string"))
5693 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5694 (org-add-props txt props 'org-marker marker
5695 'date date 'todo-state todo-state
5696 'tags tags 'level level
5697 'type "sexp" 'warntime warntime)
5698 (push txt ee)))))
5699 (nreverse ee)))
5701 ;; Calendar sanity: define some functions that are independent of
5702 ;; `calendar-date-style'.
5703 ;; Normally I would like to use ISO format when calling the diary functions,
5704 ;; but to make sure we still have Emacs 22 compatibility we bind
5705 ;; also `european-calendar-style' and use european format
5706 (defun org-anniversary (year month day &optional mark)
5707 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5708 (org-no-warnings
5709 (let ((calendar-date-style 'european) (european-calendar-style t))
5710 (diary-anniversary day month year mark))))
5711 (defun org-cyclic (N year month day &optional mark)
5712 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5713 (org-no-warnings
5714 (let ((calendar-date-style 'european) (european-calendar-style t))
5715 (diary-cyclic N day month year mark))))
5716 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5717 "Like `diary-block', but with fixed (ISO) order of arguments."
5718 (org-no-warnings
5719 (let ((calendar-date-style 'european) (european-calendar-style t))
5720 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5721 (defun org-date (year month day &optional mark)
5722 "Like `diary-date', but with fixed (ISO) order of arguments."
5723 (org-no-warnings
5724 (let ((calendar-date-style 'european) (european-calendar-style t))
5725 (diary-date day month year mark))))
5727 ;; Define the` org-class' function
5728 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5729 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5730 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5731 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5732 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5733 `holidays', then any date that is known by the Emacs calendar to be a
5734 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5735 then those holidays will be skipped."
5736 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5737 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5738 (d (calendar-absolute-from-gregorian date))
5739 (h (when skip-weeks (calendar-check-holidays date))))
5740 (and
5741 (<= date1 d)
5742 (<= d date2)
5743 (= (calendar-day-of-week date) dayname)
5744 (or (not skip-weeks)
5745 (progn
5746 (require 'cal-iso)
5747 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5748 (not (or (and h (memq 'holidays skip-weeks))
5749 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5750 entry)))
5752 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5753 "Like `org-class', but honor `calendar-date-style'.
5754 The order of the first 2 times 3 arguments depends on the variable
5755 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5756 So for American calendars, give this as MONTH DAY YEAR, for European as
5757 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5758 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5759 is any number of ISO weeks in the block period for which the item should
5760 be skipped.
5762 This function is here only for backward compatibility and it is deprecated,
5763 please use `org-class' instead."
5764 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5765 (date2 (org-order-calendar-date-args m2 d2 y2)))
5766 (org-class
5767 (nth 2 date1) (car date1) (nth 1 date1)
5768 (nth 2 date2) (car date2) (nth 1 date2)
5769 dayname skip-weeks)))
5770 (make-obsolete 'org-diary-class 'org-class "")
5772 (defalias 'org-get-closed 'org-agenda-get-progress)
5773 (defun org-agenda-get-progress ()
5774 "Return the logged TODO entries for agenda display."
5775 (let* ((props (list 'mouse-face 'highlight
5776 'org-not-done-regexp org-not-done-regexp
5777 'org-todo-regexp org-todo-regexp
5778 'org-complex-heading-regexp org-complex-heading-regexp
5779 'help-echo
5780 (format "mouse-2 or RET jump to org file %s"
5781 (abbreviate-file-name buffer-file-name))))
5782 (items (if (consp org-agenda-show-log-scoped)
5783 org-agenda-show-log-scoped
5784 (if (eq org-agenda-show-log-scoped 'clockcheck)
5785 '(clock)
5786 org-agenda-log-mode-items)))
5787 (parts
5788 (delq nil
5789 (list
5790 (if (memq 'closed items) (concat "\\<" org-closed-string))
5791 (if (memq 'clock items) (concat "\\<" org-clock-string))
5792 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5793 (parts-re (if parts (mapconcat 'identity parts "\\|")
5794 (error "`org-agenda-log-mode-items' is empty")))
5795 (regexp (concat
5796 "\\(" parts-re "\\)"
5797 " *\\["
5798 (regexp-quote
5799 (substring
5800 (format-time-string
5801 (car org-time-stamp-formats)
5802 (apply 'encode-time ; DATE bound by calendar
5803 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5804 1 11))))
5805 (org-agenda-search-headline-for-time nil)
5806 marker hdmarker priority category level tags closedp
5807 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5808 (goto-char (point-min))
5809 (while (re-search-forward regexp nil t)
5810 (catch :skip
5811 (org-agenda-skip)
5812 (setq marker (org-agenda-new-marker (match-beginning 0))
5813 closedp (equal (match-string 1) org-closed-string)
5814 statep (equal (string-to-char (match-string 1)) ?-)
5815 clockp (not (or closedp statep))
5816 state (and statep (match-string 2))
5817 category (org-get-category (match-beginning 0))
5818 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5819 (when (string-match "\\]" timestr)
5820 ;; substring should only run to end of time stamp
5821 (setq rest (substring timestr (match-end 0))
5822 timestr (substring timestr 0 (match-end 0)))
5823 (if (and (not closedp) (not statep)
5824 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5825 rest))
5826 (progn (setq timestr (concat (substring timestr 0 -1)
5827 "-" (match-string 1 rest) "]"))
5828 (setq clocked (match-string 2 rest)))
5829 (setq clocked "-")))
5830 (save-excursion
5831 (setq extra
5832 (cond
5833 ((not org-agenda-log-mode-add-notes) nil)
5834 (statep
5835 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5836 (match-string 1)))
5837 (clockp
5838 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5839 (match-string 1)))))
5840 (if (not (re-search-backward org-outline-regexp-bol nil t))
5841 (throw :skip nil)
5842 (goto-char (match-beginning 0))
5843 (setq hdmarker (org-agenda-new-marker)
5844 inherited-tags
5845 (or (eq org-agenda-show-inherited-tags 'always)
5846 (and (listp org-agenda-show-inherited-tags)
5847 (memq 'todo org-agenda-show-inherited-tags))
5848 (and (eq org-agenda-show-inherited-tags t)
5849 (or (eq org-agenda-use-tag-inheritance t)
5850 (memq 'todo org-agenda-use-tag-inheritance))))
5851 tags (org-get-tags-at nil (not inherited-tags))
5852 level (make-string (org-reduced-level (org-outline-level)) ? ))
5853 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5854 (setq txt (match-string 1))
5855 (when extra
5856 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5857 (setq txt (concat (substring txt 0 (match-beginning 1))
5858 " - " extra " " (match-string 2 txt)))
5859 (setq txt (concat txt " - " extra))))
5860 (setq txt (org-agenda-format-item
5861 (cond
5862 (closedp "Closed: ")
5863 (statep (concat "State: (" state ")"))
5864 (t (concat "Clocked: (" clocked ")")))
5865 txt level category tags timestr)))
5866 (setq priority 100000)
5867 (org-add-props txt props
5868 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5869 'priority priority 'level level
5870 'type "closed" 'date date
5871 'undone-face 'org-warning 'done-face 'org-agenda-done)
5872 (push txt ee))
5873 (goto-char (point-at-eol))))
5874 (nreverse ee)))
5876 (defun org-agenda-show-clocking-issues ()
5877 "Add overlays, showing issues with clocking.
5878 See also the user option `org-agenda-clock-consistency-checks'."
5879 (interactive)
5880 (let* ((org-time-clocksum-use-effort-durations nil)
5881 (pl org-agenda-clock-consistency-checks)
5882 (re (concat "^[ \t]*"
5883 org-clock-string
5884 "[ \t]+"
5885 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5886 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5887 (tlstart 0.)
5888 (tlend 0.)
5889 (maxtime (org-hh:mm-string-to-minutes
5890 (or (plist-get pl :max-duration) "24:00")))
5891 (mintime (org-hh:mm-string-to-minutes
5892 (or (plist-get pl :min-duration) 0)))
5893 (maxgap (org-hh:mm-string-to-minutes
5894 ;; default 30:00 means never complain
5895 (or (plist-get pl :max-gap) "30:00")))
5896 (gapok (mapcar 'org-hh:mm-string-to-minutes
5897 (plist-get pl :gap-ok-around)))
5898 (def-face (or (plist-get pl :default-face)
5899 '((:background "DarkRed") (:foreground "white"))))
5900 issue face m te ts dt ov)
5901 (goto-char (point-min))
5902 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5903 (setq issue nil face def-face)
5904 (catch 'next
5905 (setq m (org-get-at-bol 'org-marker)
5906 te nil ts nil)
5907 (unless (and m (markerp m))
5908 (setq issue "No valid clock line") (throw 'next t))
5909 (org-with-point-at m
5910 (save-excursion
5911 (goto-char (point-at-bol))
5912 (unless (looking-at re)
5913 (error "No valid Clock line")
5914 (throw 'next t))
5915 (unless (match-end 3)
5916 (setq issue "No end time"
5917 face (or (plist-get pl :no-end-time-face) face))
5918 (throw 'next t))
5919 (setq ts (match-string 1)
5920 te (match-string 3)
5921 ts (org-float-time
5922 (apply 'encode-time (org-parse-time-string ts)))
5923 te (org-float-time
5924 (apply 'encode-time (org-parse-time-string te)))
5925 dt (- te ts))))
5926 (cond
5927 ((> dt (* 60 maxtime))
5928 ;; a very long clocking chunk
5929 (setq issue (format "Clocking interval is very long: %s"
5930 (org-minutes-to-clocksum-string
5931 (floor (/ (float dt) 60.))))
5932 face (or (plist-get pl :long-face) face)))
5933 ((< dt (* 60 mintime))
5934 ;; a very short clocking chunk
5935 (setq issue (format "Clocking interval is very short: %s"
5936 (org-minutes-to-clocksum-string
5937 (floor (/ (float dt) 60.))))
5938 face (or (plist-get pl :short-face) face)))
5939 ((and (> tlend 0) (< ts tlend))
5940 ;; Two clock entries are overlapping
5941 (setq issue (format "Clocking overlap: %d minutes"
5942 (/ (- tlend ts) 60))
5943 face (or (plist-get pl :overlap-face) face)))
5944 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5945 ;; There is a gap, lets see if we need to report it
5946 (unless (org-agenda-check-clock-gap tlend ts gapok)
5947 (setq issue (format "Clocking gap: %d minutes"
5948 (/ (- ts tlend) 60))
5949 face (or (plist-get pl :gap-face) face))))
5950 (t nil)))
5951 (setq tlend (or te tlend) tlstart (or ts tlstart))
5952 (when issue
5953 ;; OK, there was some issue, add an overlay to show the issue
5954 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5955 (overlay-put ov 'before-string
5956 (concat
5957 (org-add-props
5958 (format "%-43s" (concat " " issue))
5960 'face face)
5961 "\n"))
5962 (overlay-put ov 'evaporate t)))))
5964 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5965 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5966 (catch 'exit
5967 (unless ok-list
5968 ;; there are no OK times for gaps...
5969 (throw 'exit nil))
5970 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5971 ;; This is more than 24 hours, so it is OK.
5972 ;; because we have at least one OK time, that must be in the
5973 ;; 24 hour interval.
5974 (throw 'exit t))
5975 ;; We have a shorter gap.
5976 ;; Now we have to get the minute of the day when these times are
5977 (let* ((t1dec (decode-time (seconds-to-time t1)))
5978 (t2dec (decode-time (seconds-to-time t2)))
5979 ;; compute the minute on the day
5980 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5981 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5982 (when (< min2 min1)
5983 ;; if min2 is smaller than min1, this means it is on the next day.
5984 ;; Wrap it to after midnight.
5985 (setq min2 (+ min2 1440)))
5986 ;; Now check if any of the OK times is in the gap
5987 (mapc (lambda (x)
5988 ;; Wrap the time to after midnight if necessary
5989 (if (< x min1) (setq x (+ x 1440)))
5990 ;; Check if in interval
5991 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5992 ok-list)
5993 ;; Nope, this gap is not OK
5994 nil)))
5996 (defun org-agenda-get-deadlines (&optional with-hour)
5997 "Return the deadline information for agenda display.
5998 When WITH-HOUR is non-nil, only return deadlines with an hour
5999 specification like [h]h:mm."
6000 (let* ((props (list 'mouse-face 'highlight
6001 'org-not-done-regexp org-not-done-regexp
6002 'org-todo-regexp org-todo-regexp
6003 'org-complex-heading-regexp org-complex-heading-regexp
6004 'help-echo
6005 (format "mouse-2 or RET jump to org file %s"
6006 (abbreviate-file-name buffer-file-name))))
6007 (regexp (if with-hour
6008 org-deadline-time-hour-regexp
6009 org-deadline-time-regexp))
6010 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6011 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6012 (dl0 (car org-agenda-deadline-leaders))
6013 (dl1 (nth 1 org-agenda-deadline-leaders))
6014 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6015 d2 diff dfrac wdays pos pos1 category level
6016 tags suppress-prewarning ee txt head face s todo-state
6017 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6018 (goto-char (point-min))
6019 (while (re-search-forward regexp nil t)
6020 (catch :skip
6021 (org-agenda-skip)
6022 (setq s (match-string 1)
6023 txt nil
6024 pos (1- (match-beginning 1))
6025 todo-state (save-match-data (org-get-todo-state))
6026 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6027 (member todo-state
6028 org-agenda-repeating-timestamp-show-all))
6029 d2 (org-time-string-to-absolute
6030 s d1 'past show-all (current-buffer) pos)
6031 diff (- d2 d1))
6032 (setq suppress-prewarning
6033 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6034 (let ((item (buffer-substring (point-at-bol)
6035 (point-at-eol))))
6036 (save-match-data
6037 (and (string-match
6038 org-scheduled-time-regexp item)
6039 (match-string 1 item)))))))
6040 (cond
6041 ((not ds) nil)
6042 ;; The current item has a scheduled date (in ds), so
6043 ;; evaluate its prewarning lead time.
6044 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6045 ;; Use global prewarning-restart lead time.
6046 org-agenda-skip-deadline-prewarning-if-scheduled)
6047 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6048 'pre-scheduled)
6049 ;; Set prewarning to no earlier than scheduled.
6050 (min (- d2 (org-time-string-to-absolute
6051 ds d1 'past show-all (current-buffer) pos))
6052 org-deadline-warning-days))
6053 ;; Set prewarning to deadline.
6054 (t 0))))
6055 (setq wdays (if suppress-prewarning
6056 (let ((org-deadline-warning-days suppress-prewarning))
6057 (org-get-wdays s))
6058 (org-get-wdays s))
6059 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6060 upcomingp (and todayp (> diff 0)))
6061 ;; When to show a deadline in the calendar:
6062 ;; If the expiration is within wdays warning time.
6063 ;; Past-due deadlines are only shown on the current date
6064 (if (and (or (and (<= diff wdays)
6065 (and todayp (not org-agenda-only-exact-dates)))
6066 (= diff 0)))
6067 (save-excursion
6068 ;; (setq todo-state (org-get-todo-state))
6069 (setq donep (member todo-state org-done-keywords))
6070 (if (and donep
6071 (or org-agenda-skip-deadline-if-done
6072 (not (= diff 0))))
6073 (setq txt nil)
6074 (setq category (org-get-category)
6075 warntime (get-text-property (point) 'org-appt-warntime))
6076 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6077 (throw :skip nil)
6078 (goto-char (match-end 0))
6079 (setq pos1 (match-beginning 0))
6080 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6081 (setq inherited-tags
6082 (or (eq org-agenda-show-inherited-tags 'always)
6083 (and (listp org-agenda-show-inherited-tags)
6084 (memq 'agenda org-agenda-show-inherited-tags))
6085 (and (eq org-agenda-show-inherited-tags t)
6086 (or (eq org-agenda-use-tag-inheritance t)
6087 (memq 'agenda org-agenda-use-tag-inheritance))))
6088 tags (org-get-tags-at pos1 (not inherited-tags)))
6089 (setq head (buffer-substring
6090 (point)
6091 (progn (skip-chars-forward "^\r\n")
6092 (point))))
6093 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6094 (setq timestr
6095 (concat (substring s (match-beginning 1)) " "))
6096 (setq timestr 'time))
6097 (setq txt (org-agenda-format-item
6098 (cond ((= diff 0) dl0)
6099 ((> diff 0)
6100 (if (functionp dl1)
6101 (funcall dl1 diff date)
6102 (format dl1 diff)))
6104 (if (functionp dl2)
6105 (funcall dl2 diff date)
6106 (format dl2 (if (string= dl2 dl1)
6107 diff (abs diff))))))
6108 head level category tags
6109 (if (not (= diff 0)) nil timestr)))))
6110 (when txt
6111 (setq face (org-agenda-deadline-face dfrac))
6112 (org-add-props txt props
6113 'org-marker (org-agenda-new-marker pos)
6114 'warntime warntime
6115 'level level
6116 'ts-date d2
6117 'org-hd-marker (org-agenda-new-marker pos1)
6118 'priority (+ (- diff)
6119 (org-get-priority txt))
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 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 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6240 'repeated-after-deadline)
6241 (org-get-deadline-time (point))
6242 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6243 (throw :skip nil))
6244 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6245 (throw :skip nil)
6246 (goto-char (match-end 0))
6247 (setq pos1 (match-beginning 0))
6248 (if habitp
6249 (if (or (not org-habit-show-habits)
6250 (and (not todayp)
6251 (boundp 'org-habit-show-habits-only-for-today)
6252 org-habit-show-habits-only-for-today))
6253 (throw :skip nil))
6254 (if (and
6255 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6256 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6257 pastschedp))
6258 (setq mm (assoc pos1 deadline-position-alist)))
6259 (throw :skip nil)))
6260 (setq inherited-tags
6261 (or (eq org-agenda-show-inherited-tags 'always)
6262 (and (listp org-agenda-show-inherited-tags)
6263 (memq 'agenda org-agenda-show-inherited-tags))
6264 (and (eq org-agenda-show-inherited-tags t)
6265 (or (eq org-agenda-use-tag-inheritance t)
6266 (memq 'agenda org-agenda-use-tag-inheritance))))
6268 tags (org-get-tags-at nil (not inherited-tags)))
6269 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6270 (setq head (buffer-substring
6271 (point)
6272 (progn (skip-chars-forward "^\r\n") (point))))
6273 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6274 (setq timestr
6275 (concat (substring s (match-beginning 1)) " "))
6276 (setq timestr 'time))
6277 (setq txt (org-agenda-format-item
6278 (if (= diff 0)
6279 (car org-agenda-scheduled-leaders)
6280 (format (nth 1 org-agenda-scheduled-leaders)
6281 (- 1 diff)))
6282 head level category tags
6283 (if (not (= diff 0)) nil timestr)
6284 nil habitp))))
6285 (when txt
6286 (setq face
6287 (cond
6288 ((and (not habitp) pastschedp)
6289 'org-scheduled-previously)
6290 (todayp 'org-scheduled-today)
6291 (t 'org-scheduled))
6292 habitp (and habitp (org-habit-parse-todo)))
6293 (org-add-props txt props
6294 'undone-face face
6295 'face (if donep 'org-agenda-done face)
6296 'org-marker (org-agenda-new-marker pos)
6297 'org-hd-marker (org-agenda-new-marker pos1)
6298 'type (if pastschedp "past-scheduled" "scheduled")
6299 'date (if pastschedp d2 date)
6300 'ts-date d2
6301 'warntime warntime
6302 'level level
6303 'priority (if habitp
6304 (org-habit-get-priority habitp)
6305 (+ 94 (- 5 diff) (org-get-priority txt)))
6306 'org-habit-p habitp
6307 'todo-state todo-state)
6308 (push txt ee))))))
6309 (nreverse ee)))
6311 (defun org-agenda-get-blocks ()
6312 "Return the date-range information for agenda display."
6313 (let* ((props (list 'face nil
6314 'org-not-done-regexp org-not-done-regexp
6315 'org-todo-regexp org-todo-regexp
6316 'org-complex-heading-regexp org-complex-heading-regexp
6317 'mouse-face 'highlight
6318 'help-echo
6319 (format "mouse-2 or RET jump to org file %s"
6320 (abbreviate-file-name buffer-file-name))))
6321 (regexp org-tr-regexp)
6322 (d0 (calendar-absolute-from-gregorian date))
6323 marker hdmarker ee txt d1 d2 s1 s2 category
6324 level todo-state tags pos head donep inherited-tags)
6325 (goto-char (point-min))
6326 (while (re-search-forward regexp nil t)
6327 (catch :skip
6328 (org-agenda-skip)
6329 (setq pos (point))
6330 (let ((start-time (match-string 1))
6331 (end-time (match-string 2)))
6332 (setq s1 (match-string 1)
6333 s2 (match-string 2)
6334 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6335 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6336 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6337 ;; Only allow days between the limits, because the normal
6338 ;; date stamps will catch the limits.
6339 (save-excursion
6340 (setq todo-state (org-get-todo-state))
6341 (setq donep (member todo-state org-done-keywords))
6342 (if (and donep org-agenda-skip-timestamp-if-done)
6343 (throw :skip t))
6344 (setq marker (org-agenda-new-marker (point))
6345 category (org-get-category))
6346 (if (not (re-search-backward org-outline-regexp-bol nil t))
6347 (throw :skip nil)
6348 (goto-char (match-beginning 0))
6349 (setq hdmarker (org-agenda-new-marker (point))
6350 inherited-tags
6351 (or (eq org-agenda-show-inherited-tags 'always)
6352 (and (listp org-agenda-show-inherited-tags)
6353 (memq 'agenda org-agenda-show-inherited-tags))
6354 (and (eq org-agenda-show-inherited-tags t)
6355 (or (eq org-agenda-use-tag-inheritance t)
6356 (memq 'agenda org-agenda-use-tag-inheritance))))
6358 tags (org-get-tags-at nil (not inherited-tags)))
6359 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6360 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6361 (setq head (match-string 1))
6362 (let ((remove-re
6363 (if org-agenda-remove-timeranges-from-blocks
6364 (concat
6365 "<" (regexp-quote s1) ".*?>"
6366 "--"
6367 "<" (regexp-quote s2) ".*?>")
6368 nil)))
6369 (setq txt (org-agenda-format-item
6370 (format
6371 (nth (if (= d1 d2) 0 1)
6372 org-agenda-timerange-leaders)
6373 (1+ (- d0 d1)) (1+ (- d2 d1)))
6374 head level category tags
6375 (cond ((and (= d1 d0) (= d2 d0))
6376 (concat "<" start-time ">--<" end-time ">"))
6377 ((= d1 d0)
6378 (concat "<" start-time ">"))
6379 ((= d2 d0)
6380 (concat "<" end-time ">")))
6381 remove-re))))
6382 (org-add-props txt props
6383 'org-marker marker 'org-hd-marker hdmarker
6384 'type "block" 'date date
6385 'level level
6386 'todo-state todo-state
6387 'priority (org-get-priority txt))
6388 (push txt ee))))
6389 (goto-char pos)))
6390 ;; Sort the entries by expiration date.
6391 (nreverse ee)))
6393 ;;; Agenda presentation and sorting
6395 (defvar org-prefix-has-time nil
6396 "A flag, set by `org-compile-prefix-format'.
6397 The flag is set if the currently compiled format contains a `%t'.")
6398 (defvar org-prefix-has-tag nil
6399 "A flag, set by `org-compile-prefix-format'.
6400 The flag is set if the currently compiled format contains a `%T'.")
6401 (defvar org-prefix-has-effort nil
6402 "A flag, set by `org-compile-prefix-format'.
6403 The flag is set if the currently compiled format contains a `%e'.")
6404 (defvar org-prefix-has-breadcrumbs nil
6405 "A flag, set by `org-compile-prefix-format'.
6406 The flag is set if the currently compiled format contains a `%b'.")
6407 (defvar org-prefix-category-length nil
6408 "Used by `org-compile-prefix-format' to remember the category field width.")
6409 (defvar org-prefix-category-max-length nil
6410 "Used by `org-compile-prefix-format' to remember the category field width.")
6412 (defun org-agenda-get-category-icon (category)
6413 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6414 (dolist (entry org-agenda-category-icon-alist)
6415 (when (org-string-match-p (car entry) category)
6416 (if (listp (cadr entry))
6417 (return (cadr entry))
6418 (return (apply 'create-image (cdr entry)))))))
6420 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6421 remove-re habitp)
6422 "Format TXT to be inserted into the agenda buffer.
6423 In particular, add the prefix and corresponding text properties.
6425 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6426 LEVEL may be a string to replace the `%l' specifier.
6427 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6428 category taken from local variable or file name. It will replace the `%c'
6429 specifier in the format.
6430 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6431 TXT for sorting of this entry, and for the `%t' specifier in the format.
6432 When DOTIME is a string, this string is searched for a time before TXT is.
6433 TAGS can be the tags of the headline.
6434 Any match of REMOVE-RE will be removed from TXT."
6435 ;; We keep the org-prefix-* variable values along with a compiled
6436 ;; formatter, so that multiple agendas existing at the same time do
6437 ;; not step on each other toes.
6439 ;; It was inconvenient to make these variables buffer local in
6440 ;; Agenda buffers, because this function expects to be called with
6441 ;; the buffer where item comes from being current, and not agenda
6442 ;; buffer
6443 (let* ((bindings (car org-prefix-format-compiled))
6444 (formatter (cadr org-prefix-format-compiled)))
6445 (loop for (var value) in bindings
6446 do (set var value))
6447 (save-match-data
6448 ;; Diary entries sometimes have extra whitespace at the beginning
6449 (setq txt (org-trim txt))
6451 ;; Fix the tags part in txt
6452 (setq txt (org-agenda-fix-displayed-tags
6453 txt tags
6454 org-agenda-show-inherited-tags
6455 org-agenda-hide-tags-regexp))
6457 (let* ((category (or category
6458 (if buffer-file-name
6459 (file-name-sans-extension
6460 (file-name-nondirectory buffer-file-name))
6461 "")))
6462 (category-icon (org-agenda-get-category-icon category))
6463 (category-icon (if category-icon
6464 (propertize " " 'display category-icon)
6465 ""))
6466 (effort (and (not (string= txt ""))
6467 (get-text-property 1 'effort txt)))
6468 ;; time, tag, effort are needed for the eval of the prefix format
6469 (tag (if tags (nth (1- (length tags)) tags) ""))
6470 time
6471 (ts (if dotime (concat
6472 (if (stringp dotime) dotime "")
6473 (and org-agenda-search-headline-for-time txt))))
6474 (time-of-day (and dotime (org-get-time-of-day ts)))
6475 stamp plain s0 s1 s2 rtn srp l
6476 duration breadcrumbs)
6477 (and (derived-mode-p 'org-mode) buffer-file-name
6478 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6479 (when (and dotime time-of-day)
6480 ;; Extract starting and ending time and move them to prefix
6481 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6482 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6483 (setq s0 (match-string 0 ts)
6484 srp (and stamp (match-end 3))
6485 s1 (match-string (if plain 1 2) ts)
6486 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6488 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6489 ;; them, we might want to remove them there to avoid duplication.
6490 ;; The user can turn this off with a variable.
6491 (if (and org-prefix-has-time
6492 org-agenda-remove-times-when-in-prefix (or stamp plain)
6493 (string-match (concat (regexp-quote s0) " *") txt)
6494 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6495 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6496 (= (match-beginning 0) 0)
6498 (setq txt (replace-match "" nil nil txt))))
6499 ;; Normalize the time(s) to 24 hour
6500 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6501 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6503 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6504 (let (org-time-clocksum-use-effort-durations)
6505 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6506 (setq s2
6507 (org-minutes-to-clocksum-string
6508 (+ (org-hh:mm-string-to-minutes s1)
6509 org-agenda-default-appointment-duration)))))
6511 ;; Compute the duration
6512 (when s2
6513 (setq duration (- (org-hh:mm-string-to-minutes s2)
6514 (org-hh:mm-string-to-minutes s1)))))
6516 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6517 txt)
6518 ;; Tags are in the string
6519 (if (or (eq org-agenda-remove-tags t)
6520 (and org-agenda-remove-tags
6521 org-prefix-has-tag))
6522 (setq txt (replace-match "" t t txt))
6523 (setq txt (replace-match
6524 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6525 (match-string 2 txt))
6526 t t txt))))
6528 (when remove-re
6529 (while (string-match remove-re txt)
6530 (setq txt (replace-match "" t t txt))))
6532 ;; Set org-heading property on `txt' to mark the start of the
6533 ;; heading.
6534 (add-text-properties 0 (length txt) '(org-heading t) txt)
6536 ;; Prepare the variables needed in the eval of the compiled format
6537 (if org-prefix-has-breadcrumbs
6538 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6539 (let ((s (org-display-outline-path nil nil "->" t)))
6540 (if (eq "" s) "" (concat s "->"))))))
6541 (setq time (cond (s2 (concat
6542 (org-agenda-time-of-day-to-ampm-maybe s1)
6543 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6544 (if org-agenda-timegrid-use-ampm " ")))
6545 (s1 (concat
6546 (org-agenda-time-of-day-to-ampm-maybe s1)
6547 (if org-agenda-timegrid-use-ampm
6548 "........ "
6549 "......")))
6550 (t ""))
6551 extra (or (and (not habitp) extra) "")
6552 category (if (symbolp category) (symbol-name category) category)
6553 level (or level ""))
6554 (if (string-match org-bracket-link-regexp category)
6555 (progn
6556 (setq l (if (match-end 3)
6557 (- (match-end 3) (match-beginning 3))
6558 (- (match-end 1) (match-beginning 1))))
6559 (when (< l (or org-prefix-category-length 0))
6560 (setq category (copy-sequence category))
6561 (org-add-props category nil
6562 'extra-space (make-string
6563 (- org-prefix-category-length l 1) ?\ ))))
6564 (if (and org-prefix-category-max-length
6565 (>= (length category) org-prefix-category-max-length))
6566 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6567 ;; Evaluate the compiled format
6568 (setq rtn (concat (eval formatter) txt))
6570 ;; And finally add the text properties
6571 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6572 (org-add-props rtn nil
6573 'org-category category
6574 'tags (mapcar 'org-downcase-keep-props tags)
6575 'org-highest-priority org-highest-priority
6576 'org-lowest-priority org-lowest-priority
6577 'time-of-day time-of-day
6578 'duration duration
6579 'breadcrumbs breadcrumbs
6580 'txt txt
6581 'level level
6582 'time time
6583 'extra extra
6584 'format org-prefix-format-compiled
6585 'dotime dotime)))))
6587 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6588 "Remove tags string from TXT, and add a modified list of tags.
6589 The modified list may contain inherited tags, and tags matched by
6590 `org-agenda-hide-tags-regexp' will be removed."
6591 (when (or add-inherited hide-re)
6592 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6593 (setq txt (substring txt 0 (match-beginning 0))))
6594 (setq tags
6595 (delq nil
6596 (mapcar (lambda (tg)
6597 (if (or (and hide-re (string-match hide-re tg))
6598 (and (not add-inherited)
6599 (get-text-property 0 'inherited tg)))
6601 tg))
6602 tags)))
6603 (when tags
6604 (let ((have-i (get-text-property 0 'inherited (car tags)))
6606 (setq txt (concat txt " :"
6607 (mapconcat
6608 (lambda (x)
6609 (setq i (get-text-property 0 'inherited x))
6610 (if (and have-i (not i))
6611 (progn
6612 (setq have-i nil)
6613 (concat ":" x))
6615 tags ":")
6616 (if have-i "::" ":"))))))
6617 txt)
6619 (defun org-downcase-keep-props (s)
6620 (let ((props (text-properties-at 0 s)))
6621 (setq s (downcase s))
6622 (add-text-properties 0 (length s) props s)
6625 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6627 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6628 "Add a time-grid for agenda items which need it.
6630 LIST is the list of agenda items formatted by `org-agenda-list'.
6631 NDAYS is the span of the current agenda view.
6632 TODAYP is `t' when the current agenda view is on today."
6633 (catch 'exit
6634 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6635 ((and todayp (member 'today (car org-agenda-time-grid))))
6636 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6637 ((member 'weekly (car org-agenda-time-grid)))
6638 (t (throw 'exit list)))
6639 (let* ((have (delq nil (mapcar
6640 (lambda (x) (get-text-property 1 'time-of-day x))
6641 list)))
6642 (string (nth 1 org-agenda-time-grid))
6643 (gridtimes (nth 2 org-agenda-time-grid))
6644 (req (car org-agenda-time-grid))
6645 (remove (member 'remove-match req))
6646 new time)
6647 (if (and (member 'require-timed req) (not have))
6648 ;; don't show empty grid
6649 (throw 'exit list))
6650 (while (setq time (pop gridtimes))
6651 (unless (and remove (member time have))
6652 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6653 (push (org-agenda-format-item
6654 nil string nil "" nil
6655 (concat (substring time 0 -2) ":" (substring time -2)))
6656 new)
6657 (put-text-property
6658 2 (length (car new)) 'face 'org-time-grid (car new))))
6659 (when (and todayp org-agenda-show-current-time-in-grid)
6660 (push (org-agenda-format-item
6661 nil org-agenda-current-time-string nil "" nil
6662 (format-time-string "%H:%M "))
6663 new)
6664 (put-text-property
6665 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6667 (if (member 'time-up org-agenda-sorting-strategy-selected)
6668 (append new list)
6669 (append list new)))))
6671 (defun org-compile-prefix-format (key)
6672 "Compile the prefix format into a Lisp form that can be evaluated.
6673 The resulting form and associated variable bindings is returned
6674 and stored in the variable `org-prefix-format-compiled'."
6675 (setq org-prefix-has-time nil
6676 org-prefix-has-tag nil
6677 org-prefix-category-length nil
6678 org-prefix-has-effort nil
6679 org-prefix-has-breadcrumbs nil)
6680 (let ((s (cond
6681 ((stringp org-agenda-prefix-format)
6682 org-agenda-prefix-format)
6683 ((assq key org-agenda-prefix-format)
6684 (cdr (assq key org-agenda-prefix-format)))
6685 (t " %-12:c%?-12t% s")))
6686 (start 0)
6687 varform vars var e c f opt)
6688 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6689 s start)
6690 (setq var (or (cdr (assoc (match-string 4 s)
6691 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6692 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6693 'eval)
6694 c (or (match-string 3 s) "")
6695 opt (match-beginning 1)
6696 start (1+ (match-beginning 0)))
6697 (if (equal var 'time) (setq org-prefix-has-time t))
6698 (if (equal var 'tag) (setq org-prefix-has-tag t))
6699 (if (equal var 'effort) (setq org-prefix-has-effort t))
6700 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6701 (setq f (concat "%" (match-string 2 s) "s"))
6702 (when (equal var 'category)
6703 (setq org-prefix-category-length
6704 (floor (abs (string-to-number (match-string 2 s)))))
6705 (setq org-prefix-category-max-length
6706 (let ((x (match-string 2 s)))
6707 (save-match-data
6708 (if (string-match "\\.[0-9]+" x)
6709 (string-to-number (substring (match-string 0 x) 1)))))))
6710 (if (eq var 'eval)
6711 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6712 (if opt
6713 (setq varform
6714 `(if (or (equal "" ,var) (equal nil ,var))
6716 (format ,f (concat ,var ,c))))
6717 (setq varform
6718 `(format ,f (if (or (equal ,var "")
6719 (equal ,var nil)) ""
6720 (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6721 (setq s (replace-match "%s" t nil s))
6722 (push varform vars))
6723 (setq vars (nreverse vars))
6724 (with-current-buffer (or org-agenda-buffer (current-buffer))
6725 (setq org-prefix-format-compiled
6726 (list
6727 `((org-prefix-has-time ,org-prefix-has-time)
6728 (org-prefix-has-tag ,org-prefix-has-tag)
6729 (org-prefix-category-length ,org-prefix-category-length)
6730 (org-prefix-has-effort ,org-prefix-has-effort)
6731 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6732 `(format ,s ,@vars))))))
6734 (defun org-set-sorting-strategy (key)
6735 (if (symbolp (car org-agenda-sorting-strategy))
6736 ;; the old format
6737 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6738 (setq org-agenda-sorting-strategy-selected
6739 (or (cdr (assq key org-agenda-sorting-strategy))
6740 (cdr (assq 'agenda org-agenda-sorting-strategy))
6741 '(time-up category-keep priority-down)))))
6743 (defun org-get-time-of-day (s &optional string mod24)
6744 "Check string S for a time of day.
6745 If found, return it as a military time number between 0 and 2400.
6746 If not found, return nil.
6747 The optional STRING argument forces conversion into a 5 character wide string
6748 HH:MM."
6749 (save-match-data
6750 (when
6751 (and
6752 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6753 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6754 (not (eq (get-text-property 1 'face s) 'org-link)))
6755 (let* ((h (string-to-number (match-string 1 s)))
6756 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6757 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6758 (am-p (equal ampm "am"))
6759 (h1 (cond ((not ampm) h)
6760 ((= h 12) (if am-p 0 12))
6761 (t (+ h (if am-p 0 12)))))
6762 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6763 (mod h1 24) h1))
6764 (t0 (+ (* 100 h2) m))
6765 (t1 (concat (if (>= h1 24) "+" " ")
6766 (if (and org-agenda-time-leading-zero
6767 (< t0 1000)) "0" "")
6768 (if (< t0 100) "0" "")
6769 (if (< t0 10) "0" "")
6770 (int-to-string t0))))
6771 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6773 (defvar org-agenda-before-sorting-filter-function nil
6774 "Function to be applied to agenda items prior to sorting.
6775 Prior to sorting also means just before they are inserted into the agenda.
6777 To aid sorting, you may revisit the original entries and add more text
6778 properties which will later be used by the sorting functions.
6780 The function should take a string argument, an agenda line.
6781 It has access to the text properties in that line, which contain among
6782 other things, the property `org-hd-marker' that points to the entry
6783 where the line comes from. Note that not all lines going into the agenda
6784 have this property, only most.
6786 The function should return the modified string. It is probably best
6787 to ONLY change text properties.
6789 You can also use this function as a filter, by returning nil for lines
6790 you don't want to have in the agenda at all. For this application, you
6791 could bind the variable in the options section of a custom command.")
6793 (defun org-agenda-finalize-entries (list &optional type)
6794 "Sort, limit and concatenate the LIST of agenda items.
6795 The optional argument TYPE tells the agenda type."
6796 (let ((max-effort (cond ((listp org-agenda-max-effort)
6797 (cdr (assoc type org-agenda-max-effort)))
6798 (t org-agenda-max-effort)))
6799 (max-todo (cond ((listp org-agenda-max-todos)
6800 (cdr (assoc type org-agenda-max-todos)))
6801 (t org-agenda-max-todos)))
6802 (max-tags (cond ((listp org-agenda-max-tags)
6803 (cdr (assoc type org-agenda-max-tags)))
6804 (t org-agenda-max-tags)))
6805 (max-entries (cond ((listp org-agenda-max-entries)
6806 (cdr (assoc type org-agenda-max-entries)))
6807 (t org-agenda-max-entries))))
6808 (when org-agenda-before-sorting-filter-function
6809 (setq list
6810 (delq nil
6811 (mapcar
6812 org-agenda-before-sorting-filter-function list))))
6813 (setq list (mapcar 'org-agenda-highlight-todo list)
6814 list (mapcar 'identity (sort list 'org-entries-lessp)))
6815 (when max-effort
6816 (setq list (org-agenda-limit-entries
6817 list 'effort-minutes max-effort
6818 (lambda (e) (or e (if org-sort-agenda-noeffort-is-high
6819 32767 -1))))))
6820 (when max-todo
6821 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6822 (when max-tags
6823 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6824 (when max-entries
6825 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6826 (mapconcat 'identity list "\n")))
6828 (defun org-agenda-limit-entries (list prop limit &optional fn)
6829 "Limit the number of agenda entries."
6830 (let ((include (and limit (< limit 0))))
6831 (if limit
6832 (let ((fun (or fn (lambda (p) (if p 1))))
6833 (lim 0))
6834 (delq nil
6835 (mapcar
6836 (lambda (e)
6837 (let ((pval (funcall
6838 fun (get-text-property (1- (length e))
6839 prop e))))
6840 (if pval (setq lim (+ lim pval)))
6841 (cond ((and pval (<= lim (abs limit))) e)
6842 ((and include (not pval)) e))))
6843 list)))
6844 list)))
6846 (defun org-agenda-limit-interactively (remove)
6847 "In agenda, interactively limit entries to various maximums."
6848 (interactive "P")
6849 (if remove
6850 (progn (setq org-agenda-max-entries nil
6851 org-agenda-max-todos nil
6852 org-agenda-max-tags nil
6853 org-agenda-max-effort nil)
6854 (org-agenda-redo))
6855 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6856 (msg (cond ((= max ?E) "How many minutes? ")
6857 ((= max ?e) "How many entries? ")
6858 ((= max ?t) "How many TODO entries? ")
6859 ((= max ?T) "How many tagged entries? ")
6860 (t (user-error "Wrong input"))))
6861 (num (string-to-number (read-from-minibuffer msg))))
6862 (cond ((equal max ?e)
6863 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6864 ((equal max ?t)
6865 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6866 ((equal max ?T)
6867 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6868 ((equal max ?E)
6869 (let ((org-agenda-max-effort num)) (org-agenda-redo))))))
6870 (org-agenda-fit-window-to-buffer))
6872 (defun org-agenda-highlight-todo (x)
6873 (let ((org-done-keywords org-done-keywords-for-agenda)
6874 (case-fold-search nil)
6876 (if (eq x 'line)
6877 (save-excursion
6878 (beginning-of-line 1)
6879 (setq re (org-get-at-bol 'org-todo-regexp))
6880 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6881 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6882 (add-text-properties (match-beginning 0) (match-end 1)
6883 (list 'face (org-get-todo-face 1)))
6884 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6885 (delete-region (match-beginning 1) (1- (match-end 0)))
6886 (goto-char (match-beginning 1))
6887 (insert (format org-agenda-todo-keyword-format s)))))
6888 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6889 (setq re (get-text-property 0 'org-todo-regexp x))
6890 (when (and re
6891 ;; Test `pl' because if there's no heading content,
6892 ;; there's no point matching to highlight. Note
6893 ;; that if we didn't test `pl' first, and there
6894 ;; happened to be no keyword from `org-todo-regexp'
6895 ;; on this heading line, then the `equal' comparison
6896 ;; afterwards would spuriously succeed in the case
6897 ;; where `pl' is nil -- causing an args-out-of-range
6898 ;; error when we try to add text properties to text
6899 ;; that isn't there.
6901 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6902 x pl) pl))
6903 (add-text-properties
6904 (or (match-end 1) (match-end 0)) (match-end 0)
6905 (list 'face (org-get-todo-face (match-string 2 x)))
6907 (when (match-end 1)
6908 (setq x (concat (substring x 0 (match-end 1))
6909 (format org-agenda-todo-keyword-format
6910 (match-string 2 x))
6911 (org-add-props " " (text-properties-at 0 x))
6912 (substring x (match-end 3)))))))
6913 x)))
6915 (defsubst org-cmp-values (a b property)
6916 "Compare the numeric value of text PROPERTY for string A and B."
6917 (let ((pa (or (get-text-property (1- (length a)) property a) 0))
6918 (pb (or (get-text-property (1- (length b)) property b) 0)))
6919 (cond ((> pa pb) +1)
6920 ((< pa pb) -1))))
6922 (defsubst org-cmp-effort (a b)
6923 "Compare the effort values of string A and B."
6924 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6925 (ea (or (get-text-property (1- (length a)) 'effort-minutes a) def))
6926 (eb (or (get-text-property (1- (length b)) 'effort-minutes b) def)))
6927 (cond ((> ea eb) +1)
6928 ((< ea eb) -1))))
6930 (defsubst org-cmp-category (a b)
6931 "Compare the string values of categories of strings A and B."
6932 (let ((ca (or (get-text-property (1- (length a)) 'org-category a) ""))
6933 (cb (or (get-text-property (1- (length b)) 'org-category b) "")))
6934 (cond ((string-lessp ca cb) -1)
6935 ((string-lessp cb ca) +1))))
6937 (defsubst org-cmp-todo-state (a b)
6938 "Compare the todo states of strings A and B."
6939 (let* ((ma (or (get-text-property 1 'org-marker a)
6940 (get-text-property 1 'org-hd-marker a)))
6941 (mb (or (get-text-property 1 'org-marker b)
6942 (get-text-property 1 'org-hd-marker b)))
6943 (fa (and ma (marker-buffer ma)))
6944 (fb (and mb (marker-buffer mb)))
6945 (todo-kwds
6946 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6947 (and fb (with-current-buffer fb org-todo-keywords-1))))
6948 (ta (or (get-text-property 1 'todo-state a) ""))
6949 (tb (or (get-text-property 1 'todo-state b) ""))
6950 (la (- (length (member ta todo-kwds))))
6951 (lb (- (length (member tb todo-kwds))))
6952 (donepa (member ta org-done-keywords-for-agenda))
6953 (donepb (member tb org-done-keywords-for-agenda)))
6954 (cond ((and donepa (not donepb)) -1)
6955 ((and (not donepa) donepb) +1)
6956 ((< la lb) -1)
6957 ((< lb la) +1))))
6959 (defsubst org-cmp-alpha (a b)
6960 "Compare the headlines, alphabetically."
6961 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6962 (plb (text-property-any 0 (length b) 'org-heading t b))
6963 (ta (and pla (substring a pla)))
6964 (tb (and plb (substring b plb))))
6965 (when pla
6966 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6967 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6968 (setq ta (substring ta (match-end 0))))
6969 (setq ta (downcase ta)))
6970 (when plb
6971 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6972 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6973 (setq tb (substring tb (match-end 0))))
6974 (setq tb (downcase tb)))
6975 (cond ((not ta) +1)
6976 ((not tb) -1)
6977 ((string-lessp ta tb) -1)
6978 ((string-lessp tb ta) +1))))
6980 (defsubst org-cmp-tag (a b)
6981 "Compare the string values of the first tags of A and B."
6982 (let ((ta (car (last (get-text-property 1 'tags a))))
6983 (tb (car (last (get-text-property 1 'tags b)))))
6984 (cond ((not ta) +1)
6985 ((not tb) -1)
6986 ((string-lessp ta tb) -1)
6987 ((string-lessp tb ta) +1))))
6989 (defsubst org-cmp-time (a b)
6990 "Compare the time-of-day values of strings A and B."
6991 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6992 (ta (or (get-text-property 1 'time-of-day a) def))
6993 (tb (or (get-text-property 1 'time-of-day b) def)))
6994 (cond ((< ta tb) -1)
6995 ((< tb ta) +1))))
6997 (defsubst org-cmp-ts (a b type)
6998 "Compare the timestamps values of entries A and B.
6999 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
7000 \"timestamp_ia\", compare within each of these type. When TYPE
7001 is the empty string, compare all timestamps without respect of
7002 their type."
7003 (let* ((def (if org-sort-agenda-notime-is-late most-positive-fixnum -1))
7004 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
7005 (get-text-property 1 'ts-date a))
7006 def))
7007 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
7008 (get-text-property 1 'ts-date b))
7009 def)))
7010 (cond ((< ta tb) -1)
7011 ((< tb ta) +1))))
7013 (defsubst org-cmp-habit-p (a b)
7014 "Compare the todo states of strings A and B."
7015 (let ((ha (get-text-property 1 'org-habit-p a))
7016 (hb (get-text-property 1 'org-habit-p b)))
7017 (cond ((and ha (not hb)) -1)
7018 ((and (not ha) hb) +1))))
7020 (defun org-entries-lessp (a b)
7021 "Predicate for sorting agenda entries."
7022 ;; The following variables will be used when the form is evaluated.
7023 ;; So even though the compiler complains, keep them.
7024 (let* ((ss org-agenda-sorting-strategy-selected)
7025 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7026 (org-cmp-ts a b "")))
7027 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7028 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7029 (org-cmp-ts a b "scheduled")))
7030 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7031 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7032 (org-cmp-ts a b "deadline")))
7033 (deadline-down (if deadline-up (- deadline-up) nil))
7034 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7035 (org-cmp-ts a b "timestamp_ia")))
7036 (tsia-down (if tsia-up (- tsia-up) nil))
7037 (ts-up (and (org-em 'ts-up 'ts-down ss)
7038 (org-cmp-ts a b "timestamp")))
7039 (ts-down (if ts-up (- ts-up) nil))
7040 (time-up (and (org-em 'time-up 'time-down ss)
7041 (org-cmp-time a b)))
7042 (time-down (if time-up (- time-up) nil))
7043 (stats-up (and (org-em 'stats-up 'stats-down ss)
7044 (org-cmp-values a b 'org-stats)))
7045 (stats-down (if stats-up (- stats-up) nil))
7046 (priority-up (and (org-em 'priority-up 'priority-down ss)
7047 (org-cmp-values a b 'priority)))
7048 (priority-down (if priority-up (- priority-up) nil))
7049 (effort-up (and (org-em 'effort-up 'effort-down ss)
7050 (org-cmp-effort a b)))
7051 (effort-down (if effort-up (- effort-up) nil))
7052 (category-up (and (or (org-em 'category-up 'category-down ss)
7053 (memq 'category-keep ss))
7054 (org-cmp-category a b)))
7055 (category-down (if category-up (- category-up) nil))
7056 (category-keep (if category-up +1 nil))
7057 (tag-up (and (org-em 'tag-up 'tag-down ss)
7058 (org-cmp-tag a b)))
7059 (tag-down (if tag-up (- tag-up) nil))
7060 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7061 (org-cmp-todo-state a b)))
7062 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7063 (habit-up (and (org-em 'habit-up 'habit-down ss)
7064 (org-cmp-habit-p a b)))
7065 (habit-down (if habit-up (- habit-up) nil))
7066 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7067 (org-cmp-alpha a b)))
7068 (alpha-down (if alpha-up (- alpha-up) nil))
7069 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7070 user-defined-up user-defined-down)
7071 (if (and need-user-cmp org-agenda-cmp-user-defined
7072 (functionp org-agenda-cmp-user-defined))
7073 (setq user-defined-up
7074 (funcall org-agenda-cmp-user-defined a b)
7075 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7076 (cdr (assoc
7077 (eval (cons 'or org-agenda-sorting-strategy-selected))
7078 '((-1 . t) (1 . nil) (nil . nil))))))
7080 ;;; Agenda restriction lock
7082 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7083 "Overlay to mark the headline to which agenda commands are restricted.")
7084 (overlay-put org-agenda-restriction-lock-overlay
7085 'face 'org-agenda-restriction-lock)
7086 (overlay-put org-agenda-restriction-lock-overlay
7087 'help-echo "Agendas are currently limited to this subtree.")
7088 (org-detach-overlay org-agenda-restriction-lock-overlay)
7090 ;;;###autoload
7091 (defun org-agenda-set-restriction-lock (&optional type)
7092 "Set restriction lock for agenda, to current subtree or file.
7093 Restriction will be the file if TYPE is `file', or if type is the
7094 universal prefix '(4), or if the cursor is before the first headline
7095 in the file. Otherwise, restriction will be to the current subtree."
7096 (interactive "P")
7097 (org-agenda-remove-restriction-lock 'noupdate)
7098 (and (equal type '(4)) (setq type 'file))
7099 (setq type (cond
7100 (type type)
7101 ((org-at-heading-p) 'subtree)
7102 ((condition-case nil (org-back-to-heading t) (error nil))
7103 'subtree)
7104 (t 'file)))
7105 (if (eq type 'subtree)
7106 (progn
7107 (setq org-agenda-restrict (current-buffer))
7108 (setq org-agenda-overriding-restriction 'subtree)
7109 (put 'org-agenda-files 'org-restrict
7110 (list (buffer-file-name (buffer-base-buffer))))
7111 (org-back-to-heading t)
7112 (move-overlay org-agenda-restriction-lock-overlay
7113 (point)
7114 (if org-agenda-restriction-lock-highlight-subtree
7115 (save-excursion (org-end-of-subtree t t) (point))
7116 (point-at-eol)))
7117 (move-marker org-agenda-restrict-begin (point))
7118 (move-marker org-agenda-restrict-end
7119 (save-excursion (org-end-of-subtree t t)))
7120 (message "Locking agenda restriction to subtree"))
7121 (put 'org-agenda-files 'org-restrict
7122 (list (buffer-file-name (buffer-base-buffer))))
7123 (setq org-agenda-restrict nil)
7124 (setq org-agenda-overriding-restriction 'file)
7125 (move-marker org-agenda-restrict-begin nil)
7126 (move-marker org-agenda-restrict-end nil)
7127 (message "Locking agenda restriction to file"))
7128 (setq current-prefix-arg nil)
7129 (org-agenda-maybe-redo))
7131 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7132 "Remove the agenda restriction lock."
7133 (interactive "P")
7134 (org-detach-overlay org-agenda-restriction-lock-overlay)
7135 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7136 (setq org-agenda-overriding-restriction nil)
7137 (setq org-agenda-restrict nil)
7138 (put 'org-agenda-files 'org-restrict nil)
7139 (move-marker org-agenda-restrict-begin nil)
7140 (move-marker org-agenda-restrict-end nil)
7141 (setq current-prefix-arg nil)
7142 (message "Agenda restriction lock removed")
7143 (or noupdate (org-agenda-maybe-redo)))
7145 (defun org-agenda-maybe-redo ()
7146 "If there is any window showing the agenda view, update it."
7147 (let ((w (get-buffer-window org-agenda-buffer-name t))
7148 (w0 (selected-window)))
7149 (when w
7150 (select-window w)
7151 (org-agenda-redo)
7152 (select-window w0)
7153 (if org-agenda-overriding-restriction
7154 (message "Agenda view shifted to new %s restriction"
7155 org-agenda-overriding-restriction)
7156 (message "Agenda restriction lock removed")))))
7158 ;;; Agenda commands
7160 (defun org-agenda-check-type (error &rest types)
7161 "Check if agenda buffer is of allowed type.
7162 If ERROR is non-nil, throw an error, otherwise just return nil.
7163 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7164 (if (not org-agenda-type)
7165 (error "No Org agenda currently displayed")
7166 (if (memq org-agenda-type types)
7168 (if error
7169 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7170 nil))))
7172 (defun org-agenda-Quit ()
7173 "Exit the agenda and kill buffers loaded by `org-agenda'.
7174 Also restore the window configuration."
7175 (interactive)
7176 (if org-agenda-columns-active
7177 (org-columns-quit)
7178 (let ((buf (current-buffer)))
7179 (if (eq org-agenda-window-setup 'other-frame)
7180 (progn
7181 (org-agenda-reset-markers)
7182 (kill-buffer buf)
7183 (org-columns-remove-overlays)
7184 (setq org-agenda-archives-mode nil)
7185 (delete-frame))
7186 (and (not (eq org-agenda-window-setup 'current-window))
7187 (not (one-window-p))
7188 (delete-window))
7189 (org-agenda-reset-markers)
7190 (kill-buffer buf)
7191 (org-columns-remove-overlays)
7192 (setq org-agenda-archives-mode nil)))
7193 (setq org-agenda-buffer nil)
7194 ;; Maybe restore the pre-agenda window configuration.
7195 (and org-agenda-restore-windows-after-quit
7196 (not (eq org-agenda-window-setup 'other-frame))
7197 org-agenda-pre-window-conf
7198 (set-window-configuration org-agenda-pre-window-conf)
7199 (setq org-agenda-pre-window-conf nil))))
7201 (defun org-agenda-quit ()
7202 "Exit the agenda and restore the window configuration.
7203 When `org-agenda-sticky' is non-nil, only bury the agenda."
7204 (interactive)
7205 (if (and (eq org-indirect-buffer-display 'other-window)
7206 org-last-indirect-buffer)
7207 (let ((org-last-indirect-window
7208 (get-buffer-window org-last-indirect-buffer)))
7209 (if org-last-indirect-window
7210 (delete-window org-last-indirect-window))))
7211 (if org-agenda-columns-active
7212 (org-columns-quit)
7213 (if org-agenda-sticky
7214 (let ((buf (current-buffer)))
7215 (if (eq org-agenda-window-setup 'other-frame)
7216 (progn
7217 (delete-frame))
7218 (and (not (eq org-agenda-window-setup 'current-window))
7219 (not (one-window-p))
7220 (delete-window)))
7221 (with-current-buffer buf
7222 (bury-buffer)
7223 ;; Maybe restore the pre-agenda window configuration.
7224 (and org-agenda-restore-windows-after-quit
7225 (not (eq org-agenda-window-setup 'other-frame))
7226 org-agenda-pre-window-conf
7227 (set-window-configuration org-agenda-pre-window-conf)
7228 (setq org-agenda-pre-window-conf nil))))
7229 (org-agenda-Quit))))
7231 (defun org-agenda-exit ()
7232 "Exit the agenda and restore the window configuration.
7233 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7234 buffers visited directly by the user will not be touched."
7235 (interactive)
7236 (org-release-buffers org-agenda-new-buffers)
7237 (setq org-agenda-new-buffers nil)
7238 (org-agenda-Quit))
7240 (defun org-agenda-kill-all-agenda-buffers ()
7241 "Kill all buffers in `org-agenda-mode'.
7242 This is used when toggling sticky agendas.
7243 You can also explicitly invoke it with `C-c a C-k'."
7244 (interactive)
7245 (let (blist)
7246 (dolist (buf (buffer-list))
7247 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7248 (push buf blist)))
7249 (mapc 'kill-buffer blist)))
7251 (defun org-agenda-execute (arg)
7252 "Execute another agenda command, keeping same window.
7253 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7254 in the agenda."
7255 (interactive "P")
7256 (let ((org-agenda-window-setup 'current-window))
7257 (org-agenda arg)))
7259 (defun org-agenda-redo (&optional all)
7260 "Rebuild possibly ALL agenda view(s) in the current buffer."
7261 (interactive "P")
7262 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7263 (cpa (unless (eq all t) current-prefix-arg))
7264 (org-agenda-doing-sticky-redo org-agenda-sticky)
7265 (org-agenda-sticky nil)
7266 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7267 org-agenda-buffer-name))
7268 (org-agenda-keep-modes t)
7269 (tag-filter org-agenda-tag-filter)
7270 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7271 (top-hl-filter org-agenda-top-headline-filter)
7272 (cat-filter org-agenda-category-filter)
7273 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7274 (re-filter org-agenda-regexp-filter)
7275 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7276 (effort-filter org-agenda-effort-filter)
7277 (effort-preset (get 'org-agenda-effort-filter :preset-filter))
7278 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7279 (cols org-agenda-columns-active)
7280 (line (org-current-line))
7281 (window-line (- line (org-current-line (window-start))))
7282 (lprops (get 'org-agenda-redo-command 'org-lprops))
7283 (redo-cmd (get-text-property p 'org-redo-cmd))
7284 (last-args (get-text-property p 'org-last-args))
7285 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7286 (org-agenda-overriding-cmd-arguments
7287 (unless (eq all t)
7288 (cond ((listp last-args)
7289 (cons (or cpa (car last-args)) (cdr last-args)))
7290 ((stringp last-args)
7291 last-args))))
7292 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7293 (put 'org-agenda-tag-filter :preset-filter nil)
7294 (put 'org-agenda-category-filter :preset-filter nil)
7295 (put 'org-agenda-regexp-filter :preset-filter nil)
7296 (put 'org-agenda-effort-filter :preset-filter nil)
7297 (and cols (org-columns-quit))
7298 (message "Rebuilding agenda buffer...")
7299 (if series-redo-cmd
7300 (eval series-redo-cmd)
7301 (org-let lprops redo-cmd))
7302 (setq org-agenda-undo-list nil
7303 org-agenda-pending-undo-list nil
7304 org-agenda-tag-filter tag-filter
7305 org-agenda-category-filter cat-filter
7306 org-agenda-regexp-filter re-filter
7307 org-agenda-effort-filter effort-filter
7308 org-agenda-top-headline-filter top-hl-filter)
7309 (message "Rebuilding agenda buffer...done")
7310 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7311 (put 'org-agenda-category-filter :preset-filter cat-preset)
7312 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7313 (put 'org-agenda-effort-filter :preset-filter effort-preset)
7314 (let ((tag (or tag-filter tag-preset))
7315 (cat (or cat-filter cat-preset))
7316 (effort (or effort-filter effort-preset))
7317 (re (or re-filter re-preset)))
7318 (when tag (org-agenda-filter-apply tag 'tag))
7319 (when cat (org-agenda-filter-apply cat 'category))
7320 (when effort (org-agenda-filter-apply effort 'effort))
7321 (when re (org-agenda-filter-apply re 'regexp)))
7322 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7323 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7324 (org-goto-line line)
7325 (recenter window-line)))
7327 (defvar org-global-tags-completion-table nil)
7328 (defvar org-agenda-filter-form nil)
7329 (defvar org-agenda-filtered-by-category nil)
7331 (defun org-agenda-filter-by-category (strip)
7332 "Keep only those lines in the agenda buffer that have a specific category.
7333 The category is that of the current line."
7334 (interactive "P")
7335 (if (and org-agenda-filtered-by-category
7336 org-agenda-category-filter)
7337 (org-agenda-filter-show-all-cat)
7338 (let ((cat (org-no-properties (org-get-at-eol 'org-category 1))))
7339 (cond
7340 ((and cat strip)
7341 (org-agenda-filter-apply
7342 (push (concat "-" cat) org-agenda-category-filter) 'category))
7343 ((and cat)
7344 (org-agenda-filter-apply
7345 (setq org-agenda-category-filter
7346 (list (concat "+" cat))) 'category))
7347 (t (error "No category at point"))))))
7349 (defun org-find-top-headline (&optional pos)
7350 "Find the topmost parent headline and return it."
7351 (save-excursion
7352 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7353 (if pos (goto-char pos))
7354 ;; Skip up to the topmost parent
7355 (while (ignore-errors (outline-up-heading 1) t))
7356 (ignore-errors
7357 (nth 4 (org-heading-components))))))
7359 (defvar org-agenda-filtered-by-top-headline nil)
7360 (defun org-agenda-filter-by-top-headline (strip)
7361 "Keep only those lines that are descendants from the same top headline.
7362 The top headline is that of the current line."
7363 (interactive "P")
7364 (if org-agenda-filtered-by-top-headline
7365 (progn
7366 (setq org-agenda-filtered-by-top-headline nil
7367 org-agenda-top-headline-filter nil)
7368 (org-agenda-filter-show-all-top-filter))
7369 (let ((toph (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7370 (if toph (org-agenda-filter-top-headline-apply toph strip)
7371 (error "No top-level headline at point")))))
7373 (defvar org-agenda-regexp-filter nil)
7374 (defun org-agenda-filter-by-regexp (strip)
7375 "Filter agenda entries by a regular expression.
7376 Regexp filters are cumulative.
7377 With no prefix argument, keep entries matching the regexp.
7378 With one prefix argument, filter out entries matching the regexp.
7379 With two prefix arguments, remove the regexp filters."
7380 (interactive "P")
7381 (if (not (equal strip '(16)))
7382 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7383 (read-from-minibuffer
7384 (if (equal strip '(4))
7385 "Filter out entries matching regexp: "
7386 "Narrow to entries matching regexp: ")))))
7387 (push flt org-agenda-regexp-filter)
7388 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7389 (org-agenda-filter-show-all-re)
7390 (message "Regexp filter removed")))
7392 (defvar org-agenda-effort-filter nil)
7393 (defun org-agenda-filter-by-effort (strip)
7394 "Filter agenda entries by effort.
7395 With no prefix argument, keep entries matching the effort condition.
7396 With one prefix argument, filter out entries matching the condition.
7397 With two prefix arguments, remove the effort filters."
7398 (interactive "P")
7399 (cond ((member strip '(nil 4))
7400 (let ((efforts (org-split-string
7401 (or (cdr (assoc (concat org-effort-property "_ALL")
7402 org-global-properties))
7403 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7404 "")))
7405 (eff -1)
7406 effort-prompt op)
7407 (while (not (member op '(?< ?> ?=)))
7408 (setq op (read-char-exclusive "Effort operator? (> = or <)")))
7409 (loop for i from 0 to 9 do
7410 (setq effort-prompt
7411 (concat
7412 effort-prompt " ["
7413 (if (= i 9) "0" (int-to-string (1+ i)))
7414 "]" (nth i efforts))))
7415 (message "Effort %s%s" (char-to-string op) effort-prompt)
7416 (while (or (< eff 0) (> eff 9))
7417 (setq eff (string-to-number (char-to-string (read-char-exclusive)))))
7418 (setq org-agenda-effort-filter
7419 (list (concat (if strip "-" "+")
7420 (char-to-string op) (nth (1- eff) efforts))))
7421 (org-agenda-filter-apply org-agenda-effort-filter 'effort)))
7422 (t (org-agenda-filter-show-all-effort)
7423 (message "Effort filter removed"))))
7425 (defun org-agenda-filter-remove-all ()
7426 "Remove all filters from the current agenda buffer."
7427 (interactive)
7428 (when org-agenda-tag-filter
7429 (org-agenda-filter-show-all-tag))
7430 (when org-agenda-category-filter
7431 (org-agenda-filter-show-all-cat))
7432 (when org-agenda-regexp-filter
7433 (org-agenda-filter-show-all-re))
7434 (when org-agenda-top-headline-filter
7435 (org-agenda-filter-show-all-top-filter))
7436 (when org-agenda-effort-filter
7437 (org-agenda-filter-show-all-effort))
7438 (org-agenda-finalize))
7440 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7441 "Keep only those lines in the agenda buffer that have a specific tag.
7442 The tag is selected with its fast selection letter, as configured.
7443 With prefix argument STRIP, remove all lines that do have the tag.
7444 A lisp caller can specify CHAR. NARROW means that the new tag should be
7445 used to narrow the search - the interactive user can also press `-' or `+'
7446 to switch to narrowing."
7447 (interactive "P")
7448 (let* ((alist org-tag-alist-for-agenda)
7449 (tag-chars (mapconcat
7450 (lambda (x) (if (and (not (symbolp (car x)))
7451 (cdr x))
7452 (char-to-string (cdr x))
7453 ""))
7454 alist ""))
7455 (inhibit-read-only t)
7456 (current org-agenda-tag-filter)
7457 a n tag)
7458 (unless char
7459 (message
7460 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow"
7461 (if narrow "Narrow" "Filter") tag-chars
7462 (if org-agenda-auto-exclude-function "[RET], " ""))
7463 (setq char (read-char-exclusive)))
7464 (when (member char '(?+ ?-))
7465 ;; Narrowing down
7466 (cond ((equal char ?-) (setq strip t narrow t))
7467 ((equal char ?+) (setq strip nil narrow t)))
7468 (message
7469 "Narrow by tag [%s ], [TAB], [/]:off" tag-chars)
7470 (setq char (read-char-exclusive)))
7471 (when (equal char ?\t)
7472 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7473 (org-set-local 'org-global-tags-completion-table
7474 (org-global-tags-completion-table)))
7475 (let ((completion-ignore-case t))
7476 (setq tag (org-icompleting-read
7477 "Tag: " org-global-tags-completion-table))))
7478 (cond
7479 ((equal char ?\r)
7480 (org-agenda-filter-show-all-tag)
7481 (when org-agenda-auto-exclude-function
7482 (setq org-agenda-tag-filter nil)
7483 (dolist (tag (org-agenda-get-represented-tags))
7484 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7485 (if modifier
7486 (push modifier org-agenda-tag-filter))))
7487 (if (not (null org-agenda-tag-filter))
7488 (org-agenda-filter-apply org-agenda-tag-filter 'tag))))
7489 ((equal char ?/)
7490 (org-agenda-filter-show-all-tag)
7491 (when (get 'org-agenda-tag-filter :preset-filter)
7492 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7493 ((equal char ?. )
7494 (setq org-agenda-tag-filter
7495 (mapcar (lambda(tag) (concat "+" tag))
7496 (org-get-at-bol 'tags)))
7497 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7498 ((or (equal char ?\ )
7499 (setq a (rassoc char alist))
7500 (and tag (setq a (cons tag nil))))
7501 (org-agenda-filter-show-all-tag)
7502 (setq tag (car a))
7503 (setq org-agenda-tag-filter
7504 (cons (concat (if strip "-" "+") tag)
7505 (if narrow current nil)))
7506 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7507 (t (error "Invalid tag selection character %c" char)))))
7509 (defun org-agenda-get-represented-tags ()
7510 "Get a list of all tags currently represented in the agenda."
7511 (let (p tags)
7512 (save-excursion
7513 (goto-char (point-min))
7514 (while (setq p (next-single-property-change (point) 'tags))
7515 (goto-char p)
7516 (mapc (lambda (x) (add-to-list 'tags x))
7517 (get-text-property (point) 'tags))))
7518 tags))
7520 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7521 "Refine the current filter. See `org-agenda-filter-by-tag'."
7522 (interactive "P")
7523 (org-agenda-filter-by-tag strip char 'refine))
7525 (defun org-agenda-filter-make-matcher (filter type)
7526 "Create the form that tests a line for agenda filter."
7527 (let (f f1)
7528 (cond
7529 ;; Tag filter
7530 ((eq type 'tag)
7531 (setq filter
7532 (delete-dups
7533 (append (get 'org-agenda-tag-filter :preset-filter)
7534 filter)))
7535 (dolist (x filter)
7536 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7537 (ffunc
7538 (lambda (nf0 nf01 fltr notgroup op)
7539 (dolist (x fltr)
7540 (if (member x '("-" "+"))
7541 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7542 (setq nf01 (list 'member (downcase (substring x 1))
7543 'tags))
7544 (when (equal (string-to-char x) ?-)
7545 (setq nf01 (list 'not nf01))
7546 (when (not notgroup) (setq op 'and))))
7547 (push nf01 nf0))
7548 (if notgroup
7549 (push (cons 'and nf0) f)
7550 (push (cons (or op 'or) nf0) f)))))
7551 (cond ((equal filter '("+"))
7552 (setq f (list (list 'not 'tags))))
7553 ((equal nfilter filter)
7554 (funcall ffunc f1 f filter t nil))
7555 (t (funcall ffunc nf1 nf nfilter nil nil))))))
7556 ;; Category filter
7557 ((eq type 'category)
7558 (setq filter
7559 (delete-dups
7560 (append (get 'org-agenda-category-filter :preset-filter)
7561 filter)))
7562 (dolist (x filter)
7563 (if (equal "-" (substring x 0 1))
7564 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7565 (setq f1 (list 'equal (substring x 1) 'cat)))
7566 (push f1 f)))
7567 ;; Regexp filter
7568 ((eq type 'regexp)
7569 (setq filter
7570 (delete-dups
7571 (append (get 'org-agenda-regexp-filter :preset-filter)
7572 filter)))
7573 (dolist (x filter)
7574 (if (equal "-" (substring x 0 1))
7575 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7576 (setq f1 (list 'string-match (substring x 1) 'txt)))
7577 (push f1 f)))
7578 ;; Effort filter
7579 ((eq type 'effort)
7580 (setq filter
7581 (delete-dups
7582 (append (get 'org-agenda-effort-filter :preset-filter)
7583 filter)))
7584 (dolist (x filter)
7585 (push (org-agenda-filter-effort-form x) f))))
7586 (cons 'and (nreverse f))))
7588 (defun org-agenda-filter-effort-form (e)
7589 "Return the form to compare the effort of the current line with what E says.
7590 E looks like \"+<2:25\"."
7591 (let (op)
7592 (setq e (substring e 1))
7593 (setq op (string-to-char e) e (substring e 1))
7594 (setq op (cond ((equal op ?<) '<=)
7595 ((equal op ?>) '>=)
7596 ((equal op ??) op)
7597 (t '=)))
7598 (list 'org-agenda-compare-effort (list 'quote op)
7599 (org-duration-string-to-minutes e))))
7601 (defun org-agenda-compare-effort (op value)
7602 "Compare the effort of the current line with VALUE, using OP.
7603 If the line does not have an effort defined, return nil."
7604 (let ((eff (org-get-at-eol 'effort-minutes 1)))
7605 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 -1))
7606 value)))
7608 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7609 "Expand group tags in FILTER for the agenda.
7610 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7611 (if org-group-tags
7612 (let ((case-fold-search t) rtn)
7613 (mapc
7614 (lambda (f)
7615 (let (f0 dir)
7616 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7617 (setq dir (match-string 1 f) f0 (match-string 2 f))
7618 (setq dir (if no-operator "" "+") f0 f))
7619 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7620 (org-tags-expand f0 t t))
7621 rtn))))
7622 filter)
7623 (reverse rtn))
7624 filter))
7626 (defun org-agenda-filter-apply (filter type)
7627 "Set FILTER as the new agenda filter and apply it."
7628 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7629 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7630 (let (tags cat txt)
7631 (setq org-agenda-filter-form (org-agenda-filter-make-matcher filter type))
7632 ;; Only set `org-agenda-filtered-by-category' to t when a unique
7633 ;; category is used as the filter:
7634 (setq org-agenda-filtered-by-category
7635 (and (eq type 'category)
7636 (not (equal (substring (car filter) 0 1) "-"))))
7637 (org-agenda-set-mode-name)
7638 (save-excursion
7639 (goto-char (point-min))
7640 (while (not (eobp))
7641 (if (org-get-at-bol 'org-marker)
7642 (progn
7643 (setq tags ; used in eval
7644 (apply 'append
7645 (mapcar (lambda (f)
7646 (org-agenda-filter-expand-tags (list f) t))
7647 (org-get-at-bol 'tags)))
7648 cat (org-get-at-eol 'org-category 1)
7649 txt (org-get-at-eol 'txt 1))
7650 (if (not (eval org-agenda-filter-form))
7651 (org-agenda-filter-hide-line type))
7652 (beginning-of-line 2))
7653 (beginning-of-line 2))))
7654 (if (get-char-property (point) 'invisible)
7655 (ignore-errors (org-agenda-previous-line)))))
7657 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7658 "Filter by top headline HL."
7659 (org-agenda-set-mode-name)
7660 (save-excursion
7661 (goto-char (point-min))
7662 (while (not (eobp))
7663 (let* ((pos (org-get-at-bol 'org-hd-marker))
7664 (tophl (and pos (org-find-top-headline pos))))
7665 (if (and tophl (funcall (if negative 'identity 'not)
7666 (string= hl tophl)))
7667 (org-agenda-filter-hide-line 'top-headline)))
7668 (beginning-of-line 2)))
7669 (if (get-char-property (point) 'invisible)
7670 (org-agenda-previous-line))
7671 (setq org-agenda-top-headline-filter hl
7672 org-agenda-filtered-by-top-headline t))
7674 (defun org-agenda-filter-hide-line (type)
7675 "Hide lines with TYPE in the agenda buffer."
7676 (let* ((b (max (point-min) (1- (point-at-bol))))
7677 (e (point-at-eol)))
7678 (let ((inhibit-read-only t))
7679 (add-text-properties
7680 b e `(invisible org-filtered org-filter-type ,type)))))
7682 (defun org-agenda-remove-filter (type)
7683 (interactive)
7684 "Remove filter of type TYPE from the agenda buffer."
7685 (save-excursion
7686 (goto-char (point-min))
7687 (let ((inhibit-read-only t) pos)
7688 (while (setq pos (text-property-any (point) (point-max) 'org-filter-type type))
7689 (goto-char pos)
7690 (remove-text-properties
7691 (point) (next-single-property-change (point) 'org-filter-type)
7692 `(invisible org-filtered org-filter-type ,type))))
7693 (set (intern (format "org-agenda-%s-filter" (intern-soft type))) nil)
7694 (setq org-agenda-filter-form nil)
7695 (org-agenda-set-mode-name)
7696 (org-agenda-finalize)))
7698 (defun org-agenda-filter-show-all-tag nil
7699 (org-agenda-remove-filter 'tag))
7700 (defun org-agenda-filter-show-all-re nil
7701 (org-agenda-remove-filter 'regexp))
7702 (defun org-agenda-filter-show-all-effort nil
7703 (org-agenda-remove-filter 'effort))
7704 (defun org-agenda-filter-show-all-cat nil
7705 (org-agenda-remove-filter 'category))
7706 (defun org-agenda-filter-show-all-top-filter nil
7707 (org-agenda-remove-filter 'top-headline))
7709 (defun org-agenda-manipulate-query-add ()
7710 "Manipulate the query by adding a search term with positive selection.
7711 Positive selection means the term must be matched for selection of an entry."
7712 (interactive)
7713 (org-agenda-manipulate-query ?\[))
7714 (defun org-agenda-manipulate-query-subtract ()
7715 "Manipulate the query by adding a search term with negative selection.
7716 Negative selection means term must not be matched for selection of an entry."
7717 (interactive)
7718 (org-agenda-manipulate-query ?\]))
7719 (defun org-agenda-manipulate-query-add-re ()
7720 "Manipulate the query by adding a search regexp with positive selection.
7721 Positive selection means the regexp must match for selection of an entry."
7722 (interactive)
7723 (org-agenda-manipulate-query ?\{))
7724 (defun org-agenda-manipulate-query-subtract-re ()
7725 "Manipulate the query by adding a search regexp with negative selection.
7726 Negative selection means regexp must not match for selection of an entry."
7727 (interactive)
7728 (org-agenda-manipulate-query ?\}))
7729 (defun org-agenda-manipulate-query (char)
7730 (cond
7731 ((memq org-agenda-type '(timeline agenda))
7732 (let ((org-agenda-include-inactive-timestamps t))
7733 (org-agenda-redo))
7734 (message "Display now includes inactive timestamps as well"))
7735 ((eq org-agenda-type 'search)
7736 (org-add-to-string
7737 'org-agenda-query-string
7738 (if org-agenda-last-search-view-search-was-boolean
7739 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7740 (?\{ . " +{}") (?\} . " -{}"))))
7741 " "))
7742 (setq org-agenda-redo-command
7743 (list 'org-search-view
7744 (car (get-text-property (min (1- (point-max)) (point))
7745 'org-last-args))
7746 org-agenda-query-string
7747 (+ (length org-agenda-query-string)
7748 (if (member char '(?\{ ?\})) 0 1))))
7749 (set-register org-agenda-query-register org-agenda-query-string)
7750 (let ((org-agenda-overriding-arguments
7751 (cdr org-agenda-redo-command)))
7752 (org-agenda-redo)))
7753 (t (error "Cannot manipulate query for %s-type agenda buffers"
7754 org-agenda-type))))
7756 (defun org-add-to-string (var string)
7757 (set var (concat (symbol-value var) string)))
7759 (defun org-agenda-goto-date (span)
7760 "Jump to DATE in agenda."
7761 (interactive "P")
7762 (let* ((org-read-date-prefer-future
7763 (eval org-agenda-jump-prefer-future))
7764 (date (org-read-date))
7765 (day (time-to-days (org-time-string-to-time date)))
7766 (org-agenda-sticky-orig org-agenda-sticky)
7767 (org-agenda-buffer-tmp-name (buffer-name))
7768 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7769 (0-arg (or current-prefix-arg (car args)))
7770 (2-arg (nth 2 args))
7771 (with-hour-p (nth 4 org-agenda-redo-command))
7772 (newcmd (list 'org-agenda-list 0-arg date
7773 (org-agenda-span-to-ndays
7774 2-arg (org-time-string-to-absolute date))
7775 with-hour-p))
7776 (newargs (cdr newcmd))
7777 (inhibit-read-only t)
7778 org-agenda-sticky)
7779 (if (not (org-agenda-check-type t 'agenda))
7780 (error "Not available in non-agenda views")
7781 (add-text-properties (point-min) (point-max)
7782 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7783 (org-agenda-redo)
7784 (goto-char (point-min))
7785 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7786 (save-excursion (move-beginning-of-line 2) (eobp))))
7787 (move-beginning-of-line 2))
7788 (setq org-agenda-sticky org-agenda-sticky-orig
7789 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7791 (defun org-agenda-goto-today ()
7792 "Go to today."
7793 (interactive)
7794 (org-agenda-check-type t 'timeline 'agenda)
7795 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7796 (curspan (nth 2 args))
7797 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7798 (cond
7799 (tdpos (goto-char tdpos))
7800 ((eq org-agenda-type 'agenda)
7801 (let* ((sd (org-agenda-compute-starting-span
7802 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7803 (org-agenda-overriding-arguments args))
7804 (setf (nth 1 org-agenda-overriding-arguments) sd)
7805 (org-agenda-redo)
7806 (org-agenda-find-same-or-today-or-agenda)))
7807 (t (error "Cannot find today")))))
7809 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7810 (goto-char
7811 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7812 (text-property-any (point-min) (point-max) 'org-today t)
7813 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7814 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7815 (org-agenda-backward-block))
7816 (point-min))))
7818 (defun org-agenda-backward-block ()
7819 "Move backward by one agenda block."
7820 (interactive)
7821 (org-agenda-forward-block 'backward))
7823 (defun org-agenda-forward-block (&optional backward)
7824 "Move forward by one agenda block.
7825 When optional argument BACKWARD is set, go backward"
7826 (interactive)
7827 (cond ((not (derived-mode-p 'org-agenda-mode))
7828 (user-error
7829 "Cannot execute this command outside of org-agenda-mode buffers"))
7830 ((looking-at (if backward "\\`" "\\'"))
7831 (message "Already at the %s block" (if backward "first" "last")))
7832 (t (let ((pos (prog1 (point)
7833 (ignore-errors (if backward (backward-char 1)
7834 (move-end-of-line 1)))))
7835 (f (if backward
7836 'previous-single-property-change
7837 'next-single-property-change))
7838 moved dest)
7839 (while (and (setq dest (funcall
7840 f (point) 'org-agenda-structural-header))
7841 (not (get-text-property
7842 (point) 'org-agenda-structural-header)))
7843 (setq moved t)
7844 (goto-char dest))
7845 (if moved (move-beginning-of-line 1)
7846 (goto-char (if backward (point-min) (point-max)))
7847 (move-beginning-of-line 1)
7848 (message "No %s block" (if backward "previous" "further")))))))
7850 (defun org-agenda-later (arg)
7851 "Go forward in time by the current span.
7852 With prefix ARG, go forward that many times the current span."
7853 (interactive "p")
7854 (org-agenda-check-type t 'agenda)
7855 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7856 (span (or (nth 2 args) org-agenda-current-span))
7857 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7858 (greg (calendar-gregorian-from-absolute sd))
7859 (cnt (org-get-at-bol 'org-day-cnt))
7860 greg2)
7861 (cond
7862 ((numberp span)
7863 (setq sd (+ (* span arg) sd)))
7864 ((eq span 'day)
7865 (setq sd (+ arg sd)))
7866 ((eq span 'week)
7867 (setq sd (+ (* 7 arg) sd)))
7868 ((eq span 'fortnight)
7869 (setq sd (+ (* 14 arg) sd)))
7870 ((eq span 'month)
7871 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7872 sd (calendar-absolute-from-gregorian greg2))
7873 (setcar greg2 (1+ (car greg2))))
7874 ((eq span 'year)
7875 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7876 sd (calendar-absolute-from-gregorian greg2))
7877 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7879 (setq sd (+ (* span arg) sd))))
7880 (let ((org-agenda-overriding-cmd
7881 ;; `cmd' may have been set by `org-agenda-run-series' which
7882 ;; uses `org-agenda-overriding-cmd' to decide whether
7883 ;; overriding is allowed for `cmd'
7884 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7885 (org-agenda-overriding-arguments
7886 (list (car args) sd span)))
7887 (org-agenda-redo)
7888 (org-agenda-find-same-or-today-or-agenda cnt))))
7890 (defun org-agenda-earlier (arg)
7891 "Go backward in time by the current span.
7892 With prefix ARG, go backward that many times the current span."
7893 (interactive "p")
7894 (org-agenda-later (- arg)))
7896 (defun org-agenda-view-mode-dispatch ()
7897 "Call one of the view mode commands."
7898 (interactive)
7899 (message "View: [d]ay [w]eek for[t]night [m]onth [y]ear [SPC]reset [q]uit/abort
7900 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7901 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7902 (let ((a (read-char-exclusive)))
7903 (case a
7904 (?\ (call-interactively 'org-agenda-reset-view))
7905 (?d (call-interactively 'org-agenda-day-view))
7906 (?w (call-interactively 'org-agenda-week-view))
7907 (?t (call-interactively 'org-agenda-fortnight-view))
7908 (?m (call-interactively 'org-agenda-month-view))
7909 (?y (call-interactively 'org-agenda-year-view))
7910 (?l (call-interactively 'org-agenda-log-mode))
7911 (?L (org-agenda-log-mode '(4)))
7912 (?c (org-agenda-log-mode 'clockcheck))
7913 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7914 (?a (call-interactively 'org-agenda-archives-mode))
7915 (?A (org-agenda-archives-mode 'files))
7916 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7917 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7918 (?G (call-interactively 'org-agenda-toggle-time-grid))
7919 (?D (call-interactively 'org-agenda-toggle-diary))
7920 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7921 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7922 (org-agenda-check-type t 'timeline 'agenda)
7923 (org-agenda-redo))
7924 (message "Display now includes inactive timestamps as well"))
7925 (?q (message "Abort"))
7926 (otherwise (error "Invalid key" )))))
7928 (defun org-agenda-reset-view ()
7929 "Switch to default view for agenda."
7930 (interactive)
7931 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7932 (defun org-agenda-day-view (&optional day-of-month)
7933 "Switch to daily view for agenda.
7934 With argument DAY-OF-MONTH, switch to that day of the month."
7935 (interactive "P")
7936 (org-agenda-change-time-span 'day day-of-month))
7937 (defun org-agenda-week-view (&optional iso-week)
7938 "Switch to daily view for agenda.
7939 With argument ISO-WEEK, switch to the corresponding ISO week.
7940 If ISO-WEEK has more then 2 digits, only the last two encode the
7941 week. Any digits before this encode a year. So 200712 means
7942 week 12 of year 2007. Years in the range 1938-2037 can also be
7943 written as 2-digit years."
7944 (interactive "P")
7945 (org-agenda-change-time-span 'week iso-week))
7946 (defun org-agenda-fortnight-view (&optional iso-week)
7947 "Switch to daily view for agenda.
7948 With argument ISO-WEEK, switch to the corresponding ISO week.
7949 If ISO-WEEK has more then 2 digits, only the last two encode the
7950 week. Any digits before this encode a year. So 200712 means
7951 week 12 of year 2007. Years in the range 1938-2037 can also be
7952 written as 2-digit years."
7953 (interactive "P")
7954 (org-agenda-change-time-span 'fortnight iso-week))
7955 (defun org-agenda-month-view (&optional month)
7956 "Switch to monthly view for agenda.
7957 With argument MONTH, switch to that month."
7958 (interactive "P")
7959 (org-agenda-change-time-span 'month month))
7960 (defun org-agenda-year-view (&optional year)
7961 "Switch to yearly view for agenda.
7962 With argument YEAR, switch to that year.
7963 If MONTH has more then 2 digits, only the last two encode the
7964 month. Any digits before this encode a year. So 200712 means
7965 December year 2007. Years in the range 1938-2037 can also be
7966 written as 2-digit years."
7967 (interactive "P")
7968 (when year
7969 (setq year (org-small-year-to-year year)))
7970 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7971 (org-agenda-change-time-span 'year year)
7972 (error "Abort")))
7974 (defun org-agenda-change-time-span (span &optional n)
7975 "Change the agenda view to SPAN.
7976 SPAN may be `day', `week', `fortnight', `month', `year'."
7977 (org-agenda-check-type t 'agenda)
7978 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7979 (curspan (nth 2 args)))
7980 (if (and (not n) (equal curspan span))
7981 (error "Viewing span is already \"%s\"" span))
7982 (let* ((sd (or (org-get-at-bol 'day)
7983 (nth 1 args)
7984 org-starting-day))
7985 (sd (org-agenda-compute-starting-span sd span n))
7986 (org-agenda-overriding-cmd
7987 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7988 (org-agenda-overriding-arguments
7989 (list (car args) sd span)))
7990 (org-agenda-redo)
7991 (org-agenda-find-same-or-today-or-agenda))
7992 (org-agenda-set-mode-name)
7993 (message "Switched to %s view" span)))
7995 (defun org-agenda-compute-starting-span (sd span &optional n)
7996 "Compute starting date for agenda.
7997 SPAN may be `day', `week', `fortnight', `month', `year'. The return value
7998 is a cons cell with the starting date and the number of days,
7999 so that the date SD will be in that range."
8000 (let* ((greg (calendar-gregorian-from-absolute sd))
8001 (dg (nth 1 greg))
8002 (mg (car greg))
8003 (yg (nth 2 greg)))
8004 (cond
8005 ((eq span 'day)
8006 (when n
8007 (setq sd (+ (calendar-absolute-from-gregorian
8008 (list mg 1 yg))
8009 n -1))))
8010 ((or (eq span 'week) (eq span 'fortnight))
8011 (let* ((nt (calendar-day-of-week
8012 (calendar-gregorian-from-absolute sd)))
8013 (d (if org-agenda-start-on-weekday
8014 (- nt org-agenda-start-on-weekday)
8017 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
8018 (when n
8019 (require 'cal-iso)
8020 (when (> n 99)
8021 (setq y1 (org-small-year-to-year (/ n 100))
8022 n (mod n 100)))
8023 (setq sd
8024 (calendar-absolute-from-iso
8025 (list n 1
8026 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
8027 ((eq span 'month)
8028 (let (y1)
8029 (when (and n (> n 99))
8030 (setq y1 (org-small-year-to-year (/ n 100))
8031 n (mod n 100)))
8032 (setq sd (calendar-absolute-from-gregorian
8033 (list (or n mg) 1 (or y1 yg))))))
8034 ((eq span 'year)
8035 (setq sd (calendar-absolute-from-gregorian
8036 (list 1 1 (or n yg))))))
8037 sd))
8039 (defun org-agenda-next-date-line (&optional arg)
8040 "Jump to the next line indicating a date in agenda buffer."
8041 (interactive "p")
8042 (org-agenda-check-type t 'agenda 'timeline)
8043 (beginning-of-line 1)
8044 ;; This does not work if user makes date format that starts with a blank
8045 (if (looking-at "^\\S-") (forward-char 1))
8046 (if (not (re-search-forward "^\\S-" nil t arg))
8047 (progn
8048 (backward-char 1)
8049 (error "No next date after this line in this buffer")))
8050 (goto-char (match-beginning 0)))
8052 (defun org-agenda-previous-date-line (&optional arg)
8053 "Jump to the previous line indicating a date in agenda buffer."
8054 (interactive "p")
8055 (org-agenda-check-type t 'agenda 'timeline)
8056 (beginning-of-line 1)
8057 (if (not (re-search-backward "^\\S-" nil t arg))
8058 (error "No previous date before this line in this buffer")))
8060 ;; Initialize the highlight
8061 (defvar org-hl (make-overlay 1 1))
8062 (overlay-put org-hl 'face 'highlight)
8064 (defun org-highlight (begin end &optional buffer)
8065 "Highlight a region with overlay."
8066 (move-overlay org-hl begin end (or buffer (current-buffer))))
8068 (defun org-unhighlight ()
8069 "Detach overlay INDEX."
8070 (org-detach-overlay org-hl))
8072 (defun org-unhighlight-once ()
8073 "Remove the highlight from its position, and this function from the hook."
8074 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8075 (org-unhighlight))
8077 (defvar org-agenda-pre-follow-window-conf nil)
8078 (defun org-agenda-follow-mode ()
8079 "Toggle follow mode in an agenda buffer."
8080 (interactive)
8081 (unless org-agenda-follow-mode
8082 (setq org-agenda-pre-follow-window-conf
8083 (current-window-configuration)))
8084 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8085 (unless org-agenda-follow-mode
8086 (set-window-configuration org-agenda-pre-follow-window-conf))
8087 (org-agenda-set-mode-name)
8088 (org-agenda-do-context-action)
8089 (message "Follow mode is %s"
8090 (if org-agenda-follow-mode "on" "off")))
8092 (defun org-agenda-entry-text-mode (&optional arg)
8093 "Toggle entry text mode in an agenda buffer."
8094 (interactive "P")
8095 (if (or org-agenda-tag-filter
8096 org-agenda-category-filter
8097 org-agenda-regexp-filter
8098 org-agenda-top-headline-filter)
8099 (user-error "Can't show entry text in filtered views")
8100 (setq org-agenda-entry-text-mode (or (integerp arg)
8101 (not org-agenda-entry-text-mode)))
8102 (org-agenda-entry-text-hide)
8103 (and org-agenda-entry-text-mode
8104 (let ((org-agenda-entry-text-maxlines
8105 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8106 (org-agenda-entry-text-show)))
8107 (org-agenda-set-mode-name)
8108 (message "Entry text mode is %s%s"
8109 (if org-agenda-entry-text-mode "on" "off")
8110 (if (not org-agenda-entry-text-mode) ""
8111 (format " (maximum number of lines is %d)"
8112 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8114 (defun org-agenda-clockreport-mode ()
8115 "Toggle clocktable mode in an agenda buffer."
8116 (interactive)
8117 (org-agenda-check-type t 'agenda)
8118 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
8119 (setq org-agenda-start-with-clockreport-mode org-agenda-clockreport-mode)
8120 (org-agenda-set-mode-name)
8121 (org-agenda-redo)
8122 (message "Clocktable mode is %s"
8123 (if org-agenda-clockreport-mode "on" "off")))
8125 (defun org-agenda-log-mode (&optional special)
8126 "Toggle log mode in an agenda buffer.
8127 With argument SPECIAL, show all possible log items, not only the ones
8128 configured in `org-agenda-log-mode-items'.
8129 With a double `C-u' prefix arg, show *only* log items, nothing else."
8130 (interactive "P")
8131 (org-agenda-check-type t 'agenda 'timeline)
8132 (setq org-agenda-show-log
8133 (cond
8134 ((equal special '(16)) 'only)
8135 ((eq special 'clockcheck)
8136 (if (eq org-agenda-show-log 'clockcheck)
8137 nil 'clockcheck))
8138 (special '(closed clock state))
8139 (t (not org-agenda-show-log))))
8140 (setq org-agenda-start-with-log-mode org-agenda-show-log)
8141 (org-agenda-set-mode-name)
8142 (org-agenda-redo)
8143 (message "Log mode is %s"
8144 (if org-agenda-show-log "on" "off")))
8146 (defun org-agenda-archives-mode (&optional with-files)
8147 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8148 When called with a prefix argument, include all archive files as well."
8149 (interactive "P")
8150 (setq org-agenda-archives-mode
8151 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8152 (org-agenda-set-mode-name)
8153 (org-agenda-redo)
8154 (message
8155 "%s"
8156 (cond
8157 ((eq org-agenda-archives-mode nil)
8158 "No archives are included")
8159 ((eq org-agenda-archives-mode 'trees)
8160 (format "Trees with :%s: tag are included" org-archive-tag))
8161 ((eq org-agenda-archives-mode t)
8162 (format "Trees with :%s: tag and all active archive files are included"
8163 org-archive-tag)))))
8165 (defun org-agenda-toggle-diary ()
8166 "Toggle diary inclusion in an agenda buffer."
8167 (interactive)
8168 (org-agenda-check-type t 'agenda)
8169 (setq org-agenda-include-diary (not org-agenda-include-diary))
8170 (org-agenda-redo)
8171 (org-agenda-set-mode-name)
8172 (message "Diary inclusion turned %s"
8173 (if org-agenda-include-diary "on" "off")))
8175 (defun org-agenda-toggle-deadlines ()
8176 "Toggle inclusion of entries with a deadline in an agenda buffer."
8177 (interactive)
8178 (org-agenda-check-type t 'agenda)
8179 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8180 (org-agenda-redo)
8181 (org-agenda-set-mode-name)
8182 (message "Deadlines inclusion turned %s"
8183 (if org-agenda-include-deadlines "on" "off")))
8185 (defun org-agenda-toggle-time-grid ()
8186 "Toggle time grid in an agenda buffer."
8187 (interactive)
8188 (org-agenda-check-type t 'agenda)
8189 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8190 (org-agenda-redo)
8191 (org-agenda-set-mode-name)
8192 (message "Time-grid turned %s"
8193 (if org-agenda-use-time-grid "on" "off")))
8195 (defun org-agenda-set-mode-name ()
8196 "Set the mode name to indicate all the small mode settings."
8197 (setq mode-name
8198 (list "Org-Agenda"
8199 (if (get 'org-agenda-files 'org-restrict) " []" "")
8201 '(:eval (org-agenda-span-name org-agenda-current-span))
8202 (if org-agenda-follow-mode " Follow" "")
8203 (if org-agenda-entry-text-mode " ETxt" "")
8204 (if org-agenda-include-diary " Diary" "")
8205 (if org-agenda-include-deadlines " Ddl" "")
8206 (if org-agenda-use-time-grid " Grid" "")
8207 (if (and (boundp 'org-habit-show-habits)
8208 org-habit-show-habits) " Habit" "")
8209 (cond
8210 ((consp org-agenda-show-log) " LogAll")
8211 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8212 (org-agenda-show-log " Log")
8213 (t ""))
8214 (if (or org-agenda-category-filter
8215 (get 'org-agenda-category-filter :preset-filter))
8216 '(:eval (org-propertize
8217 (concat " <"
8218 (mapconcat
8219 'identity
8220 (append
8221 (get 'org-agenda-category-filter :preset-filter)
8222 org-agenda-category-filter)
8224 ">")
8225 'face 'org-agenda-filter-category
8226 'help-echo "Category used in filtering")) "")
8227 (if (or org-agenda-tag-filter
8228 (get 'org-agenda-tag-filter :preset-filter))
8229 '(:eval (org-propertize
8230 (concat " {"
8231 (mapconcat
8232 'identity
8233 (append
8234 (get 'org-agenda-tag-filter :preset-filter)
8235 org-agenda-tag-filter)
8237 "}")
8238 'face 'org-agenda-filter-tags
8239 'help-echo "Tags used in filtering")) "")
8240 (if (or org-agenda-effort-filter
8241 (get 'org-agenda-effort-filter :preset-filter))
8242 '(:eval (org-propertize
8243 (concat " {"
8244 (mapconcat
8245 'identity
8246 (append
8247 (get 'org-agenda-effort-filter :preset-filter)
8248 org-agenda-effort-filter)
8250 "}")
8251 'face 'org-agenda-filter-effort
8252 'help-echo "Effort conditions used in filtering")) "")
8253 (if (or org-agenda-regexp-filter
8254 (get 'org-agenda-regexp-filter :preset-filter))
8255 '(:eval (org-propertize
8256 (concat " ["
8257 (mapconcat
8258 'identity
8259 (append
8260 (get 'org-agenda-regexp-filter :preset-filter)
8261 org-agenda-regexp-filter)
8263 "]")
8264 'face 'org-agenda-filter-regexp
8265 'help-echo "Regexp used in filtering")) "")
8266 (if org-agenda-archives-mode
8267 (if (eq org-agenda-archives-mode t)
8268 " Archives"
8269 (format " :%s:" org-archive-tag))
8271 (if org-agenda-clockreport-mode " Clock" "")))
8272 (force-mode-line-update))
8274 (define-obsolete-function-alias
8275 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8277 (defun org-agenda-update-agenda-type ()
8278 "Update the agenda type after each command."
8279 (setq org-agenda-type
8280 (or (get-text-property (point) 'org-agenda-type)
8281 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8283 (defun org-agenda-next-line ()
8284 "Move cursor to the next line, and show if follow mode is active."
8285 (interactive)
8286 (call-interactively 'next-line)
8287 (org-agenda-do-context-action))
8289 (defun org-agenda-previous-line ()
8290 "Move cursor to the previous line, and show if follow-mode is active."
8291 (interactive)
8292 (call-interactively 'previous-line)
8293 (org-agenda-do-context-action))
8295 (defun org-agenda-next-item (n)
8296 "Move cursor to next agenda item."
8297 (interactive "p")
8298 (let ((col (current-column)))
8299 (dotimes (c n)
8300 (when (next-single-property-change (point-at-eol) 'org-marker)
8301 (move-end-of-line 1)
8302 (goto-char (next-single-property-change (point) 'org-marker))))
8303 (org-move-to-column col))
8304 (org-agenda-do-context-action))
8306 (defun org-agenda-previous-item (n)
8307 "Move cursor to next agenda item."
8308 (interactive "p")
8309 (dotimes (c n)
8310 (let ((col (current-column))
8311 (goto (save-excursion
8312 (move-end-of-line 0)
8313 (previous-single-property-change (point) 'org-marker))))
8314 (if goto (goto-char goto))
8315 (org-move-to-column col)))
8316 (org-agenda-do-context-action))
8318 (defun org-agenda-do-context-action ()
8319 "Show outline path and, maybe, follow mode window."
8320 (let ((m (org-get-at-bol 'org-marker)))
8321 (when (and (markerp m) (marker-buffer m))
8322 (and org-agenda-follow-mode
8323 (if org-agenda-follow-indirect
8324 (org-agenda-tree-to-indirect-buffer nil)
8325 (org-agenda-show)))
8326 (and org-agenda-show-outline-path
8327 (org-with-point-at m (org-display-outline-path t))))))
8329 (defun org-agenda-show-tags ()
8330 "Show the tags applicable to the current item."
8331 (interactive)
8332 (let* ((tags (org-get-at-bol 'tags)))
8333 (if tags
8334 (message "Tags are :%s:"
8335 (org-no-properties (mapconcat 'identity tags ":")))
8336 (message "No tags associated with this line"))))
8338 (defun org-agenda-goto (&optional highlight)
8339 "Go to the entry at point in the corresponding Org-mode file."
8340 (interactive)
8341 (let* ((marker (or (org-get-at-bol 'org-marker)
8342 (org-agenda-error)))
8343 (buffer (marker-buffer marker))
8344 (pos (marker-position marker)))
8345 (switch-to-buffer-other-window buffer)
8346 (widen)
8347 (push-mark)
8348 (goto-char pos)
8349 (when (derived-mode-p 'org-mode)
8350 (org-show-context 'agenda)
8351 (save-excursion
8352 (and (outline-next-heading)
8353 (org-flag-heading nil)))) ; show the next heading
8354 (when (outline-invisible-p)
8355 (show-entry)) ; display invisible text
8356 (recenter (/ (window-height) 2))
8357 (org-back-to-heading t)
8358 (if (re-search-forward org-complex-heading-regexp nil t)
8359 (goto-char (match-beginning 4)))
8360 (run-hooks 'org-agenda-after-show-hook)
8361 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8363 (defvar org-agenda-after-show-hook nil
8364 "Normal hook run after an item has been shown from the agenda.
8365 Point is in the buffer where the item originated.")
8367 (defun org-agenda-kill ()
8368 "Kill the entry or subtree belonging to the current agenda entry."
8369 (interactive)
8370 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8371 (let* ((bufname-orig (buffer-name))
8372 (marker (or (org-get-at-bol 'org-marker)
8373 (org-agenda-error)))
8374 (buffer (marker-buffer marker))
8375 (pos (marker-position marker))
8376 (type (org-get-at-bol 'type))
8377 dbeg dend (n 0) conf)
8378 (org-with-remote-undo buffer
8379 (with-current-buffer buffer
8380 (save-excursion
8381 (goto-char pos)
8382 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8383 (setq dbeg (progn (org-back-to-heading t) (point))
8384 dend (org-end-of-subtree t t))
8385 (setq dbeg (point-at-bol)
8386 dend (min (point-max) (1+ (point-at-eol)))))
8387 (goto-char dbeg)
8388 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8389 (setq conf (or (eq t org-agenda-confirm-kill)
8390 (and (numberp org-agenda-confirm-kill)
8391 (> n org-agenda-confirm-kill))))
8392 (and conf
8393 (not (y-or-n-p
8394 (format "Delete entry with %d lines in buffer \"%s\"? "
8395 n (buffer-name buffer))))
8396 (error "Abort"))
8397 (let ((org-agenda-buffer-name bufname-orig))
8398 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8399 (with-current-buffer buffer (delete-region dbeg dend))
8400 (message "Agenda item and source killed"))))
8402 (defvar org-archive-default-command) ; defined in org-archive.el
8403 (defun org-agenda-archive-default ()
8404 "Archive the entry or subtree belonging to the current agenda entry."
8405 (interactive)
8406 (require 'org-archive)
8407 (org-agenda-archive-with org-archive-default-command))
8409 (defun org-agenda-archive-default-with-confirmation ()
8410 "Archive the entry or subtree belonging to the current agenda entry."
8411 (interactive)
8412 (require 'org-archive)
8413 (org-agenda-archive-with org-archive-default-command 'confirm))
8415 (defun org-agenda-archive ()
8416 "Archive the entry or subtree belonging to the current agenda entry."
8417 (interactive)
8418 (org-agenda-archive-with 'org-archive-subtree))
8420 (defun org-agenda-archive-to-archive-sibling ()
8421 "Move the entry to the archive sibling."
8422 (interactive)
8423 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8425 (defun org-agenda-archive-with (cmd &optional confirm)
8426 "Move the entry to the archive sibling."
8427 (interactive)
8428 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8429 (let* ((bufname-orig (buffer-name))
8430 (marker (or (org-get-at-bol 'org-marker)
8431 (org-agenda-error)))
8432 (buffer (marker-buffer marker))
8433 (pos (marker-position marker)))
8434 (org-with-remote-undo buffer
8435 (with-current-buffer buffer
8436 (if (derived-mode-p 'org-mode)
8437 (if (and confirm
8438 (not (y-or-n-p "Archive this subtree or entry? ")))
8439 (error "Abort")
8440 (save-window-excursion
8441 (goto-char pos)
8442 (let ((org-agenda-buffer-name bufname-orig))
8443 (org-remove-subtree-entries-from-agenda))
8444 (org-back-to-heading t)
8445 (funcall cmd)))
8446 (error "Archiving works only in Org-mode files"))))))
8448 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8449 "Remove all lines in the agenda that correspond to a given subtree.
8450 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8451 If this information is not given, the function uses the tree at point."
8452 (let ((buf (or buf (current-buffer))) m p)
8453 (save-excursion
8454 (unless (and beg end)
8455 (org-back-to-heading t)
8456 (setq beg (point))
8457 (org-end-of-subtree t)
8458 (setq end (point)))
8459 (set-buffer (get-buffer org-agenda-buffer-name))
8460 (save-excursion
8461 (goto-char (point-max))
8462 (beginning-of-line 1)
8463 (while (not (bobp))
8464 (when (and (setq m (org-get-at-bol 'org-marker))
8465 (equal buf (marker-buffer m))
8466 (setq p (marker-position m))
8467 (>= p beg)
8468 (< p end))
8469 (let ((inhibit-read-only t))
8470 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8471 (beginning-of-line 0))))))
8473 (defun org-agenda-refile (&optional goto rfloc no-update)
8474 "Refile the item at point.
8476 When GOTO is 0 or '(64), clear the refile cache.
8477 When GOTO is '(16), go to the location of the last refiled item.
8478 RFLOC can be a refile location obtained in a different way.
8479 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8480 (interactive "P")
8481 (cond
8482 ((member goto '(0 (64)))
8483 (org-refile-cache-clear))
8484 ((equal goto '(16))
8485 (org-refile-goto-last-stored))
8487 (let* ((buffer-orig (buffer-name))
8488 (marker (or (org-get-at-bol 'org-hd-marker)
8489 (org-agenda-error)))
8490 (buffer (marker-buffer marker))
8491 (pos (marker-position marker))
8492 (rfloc (or rfloc
8493 (org-refile-get-location
8494 (if goto "Goto" "Refile to") buffer
8495 org-refile-allow-creating-parent-nodes))))
8496 (with-current-buffer buffer
8497 (save-excursion
8498 (save-restriction
8499 (widen)
8500 (goto-char marker)
8501 (let ((org-agenda-buffer-name buffer-orig))
8502 (org-remove-subtree-entries-from-agenda))
8503 (org-refile goto buffer rfloc)))))
8504 (unless no-update (org-agenda-redo)))))
8506 (defun org-agenda-open-link (&optional arg)
8507 "Open the link(s) in the current entry, if any.
8508 This looks for a link in the displayed line in the agenda.
8509 It also looks at the text of the entry itself."
8510 (interactive "P")
8511 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8512 (org-get-at-bol 'org-marker)))
8513 (buffer (and marker (marker-buffer marker)))
8514 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8515 (lkall (and buffer (org-offer-links-in-entry
8516 buffer marker arg prefix)))
8517 (lk0 (car lkall))
8518 (lk (if (stringp lk0) (list lk0) lk0))
8519 (lkend (cdr lkall))
8520 trg)
8521 (cond
8522 ((and buffer lk)
8523 (mapcar (lambda(l)
8524 (with-current-buffer buffer
8525 (setq trg (and (string-match org-bracket-link-regexp l)
8526 (match-string 1 l)))
8527 (if (or (not trg) (string-match org-any-link-re trg))
8528 (save-excursion
8529 (save-restriction
8530 (widen)
8531 (goto-char marker)
8532 (when (search-forward l nil lkend)
8533 (goto-char (match-beginning 0))
8534 (org-open-at-point))))
8535 ;; This is an internal link, widen the buffer
8536 (switch-to-buffer-other-window buffer)
8537 (widen)
8538 (goto-char marker)
8539 (when (search-forward l nil lkend)
8540 (goto-char (match-beginning 0))
8541 (org-open-at-point)))))
8542 lk))
8543 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8544 (save-excursion
8545 (beginning-of-line 1)
8546 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8547 (org-open-link-from-string (match-string 1)))
8548 (t (message "No link to open here")))))
8550 (defun org-agenda-copy-local-variable (var)
8551 "Get a variable from a referenced buffer and install it here."
8552 (let ((m (org-get-at-bol 'org-marker)))
8553 (when (and m (buffer-live-p (marker-buffer m)))
8554 (org-set-local var (with-current-buffer (marker-buffer m)
8555 (symbol-value var))))))
8557 (defun org-agenda-switch-to (&optional delete-other-windows)
8558 "Go to the Org-mode file which contains the item at point."
8559 (interactive)
8560 (if (and org-return-follows-link
8561 (not (org-get-at-bol 'org-marker))
8562 (org-in-regexp org-bracket-link-regexp))
8563 (org-open-link-from-string (match-string 0))
8564 (let* ((marker (or (org-get-at-bol 'org-marker)
8565 (org-agenda-error)))
8566 (buffer (marker-buffer marker))
8567 (pos (marker-position marker)))
8568 (org-pop-to-buffer-same-window buffer)
8569 (and delete-other-windows (delete-other-windows))
8570 (widen)
8571 (goto-char pos)
8572 (org-back-to-heading t)
8573 (when (derived-mode-p 'org-mode)
8574 (org-show-context 'agenda)
8575 (save-excursion
8576 (and (outline-next-heading)
8577 (org-flag-heading nil))) ; show the next heading
8578 (when (outline-invisible-p)
8579 (show-entry)) ; display invisible text
8580 (run-hooks 'org-agenda-after-show-hook)))))
8582 (defun org-agenda-goto-mouse (ev)
8583 "Go to the Org-mode file which contains the item at the mouse click."
8584 (interactive "e")
8585 (mouse-set-point ev)
8586 (org-agenda-goto))
8588 (defun org-agenda-show (&optional full-entry)
8589 "Display the Org-mode file which contains the item at point.
8590 With prefix argument FULL-ENTRY, make the entire entry visible
8591 if it was hidden in the outline."
8592 (interactive "P")
8593 (let ((win (selected-window)))
8594 (if full-entry
8595 (let ((org-show-entry-below t))
8596 (org-agenda-goto t))
8597 (org-agenda-goto t))
8598 (select-window win)))
8600 (defvar org-agenda-show-window nil)
8601 (defun org-agenda-show-and-scroll-up (&optional arg)
8602 "Display the Org-mode file which contains the item at point.
8603 When called repeatedly, scroll the window that is displaying the buffer.
8604 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8605 `show-subtree' to display the item, so that drawers and logbooks stay
8606 folded."
8607 (interactive "P")
8608 (let ((win (selected-window)))
8609 (if (and (window-live-p org-agenda-show-window)
8610 (eq this-command last-command))
8611 (progn
8612 (select-window org-agenda-show-window)
8613 (ignore-errors (scroll-up)))
8614 (org-agenda-goto t)
8615 (if arg (org-show-entry) (show-subtree))
8616 (setq org-agenda-show-window (selected-window)))
8617 (select-window win)))
8619 (defun org-agenda-show-scroll-down ()
8620 "Scroll down the window showing the agenda."
8621 (interactive)
8622 (let ((win (selected-window)))
8623 (when (window-live-p org-agenda-show-window)
8624 (select-window org-agenda-show-window)
8625 (ignore-errors (scroll-down))
8626 (select-window win))))
8628 (defun org-agenda-show-1 (&optional more)
8629 "Display the Org-mode file which contains the item at point.
8630 The prefix arg selects the amount of information to display:
8632 0 hide the subtree
8633 1 just show the entry according to defaults.
8634 2 show the children view
8635 3 show the subtree view
8636 4 show the entire subtree and any LOGBOOK drawers
8637 5 show the entire subtree and any drawers
8638 With prefix argument FULL-ENTRY, make the entire entry visible
8639 if it was hidden in the outline."
8640 (interactive "p")
8641 (let ((win (selected-window)))
8642 (org-agenda-goto t)
8643 (org-back-to-heading)
8644 (set-window-start (selected-window) (point-at-bol))
8645 (cond
8646 ((= more 0)
8647 (hide-subtree)
8648 (save-excursion
8649 (org-back-to-heading)
8650 (run-hook-with-args 'org-cycle-hook 'folded))
8651 (message "Remote: FOLDED"))
8652 ((and (org-called-interactively-p 'any) (= more 1))
8653 (message "Remote: show with default settings"))
8654 ((= more 2)
8655 (show-entry)
8656 (show-children)
8657 (save-excursion
8658 (org-back-to-heading)
8659 (run-hook-with-args 'org-cycle-hook 'children))
8660 (message "Remote: CHILDREN"))
8661 ((= more 3)
8662 (show-subtree)
8663 (save-excursion
8664 (org-back-to-heading)
8665 (run-hook-with-args 'org-cycle-hook 'subtree))
8666 (message "Remote: SUBTREE"))
8667 ((= more 4)
8668 (show-subtree)
8669 (save-excursion
8670 (org-back-to-heading)
8671 (org-cycle-hide-drawers 'subtree '("LOGBOOK")))
8672 (message "Remote: SUBTREE AND LOGBOOK"))
8673 ((> more 4)
8674 (show-subtree)
8675 (message "Remote: SUBTREE AND ALL DRAWERS")))
8676 (select-window win)))
8678 (defvar org-agenda-cycle-counter nil)
8679 (defun org-agenda-cycle-show (&optional n)
8680 "Show the current entry in another window, with default settings.
8681 Default settings are taken from `org-show-hierarchy-above' and siblings.
8682 When use repeatedly in immediate succession, the remote entry will cycle
8683 through visibility
8685 children -> subtree -> folded
8687 When called with a numeric prefix arg, that arg will be passed through to
8688 `org-agenda-show-1'. For the interpretation of that argument, see the
8689 docstring of `org-agenda-show-1'."
8690 (interactive "P")
8691 (if (integerp n)
8692 (setq org-agenda-cycle-counter n)
8693 (if (not (eq last-command this-command))
8694 (setq org-agenda-cycle-counter 1)
8695 (if (equal org-agenda-cycle-counter 0)
8696 (setq org-agenda-cycle-counter 2)
8697 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8698 (if (> org-agenda-cycle-counter 3)
8699 (setq org-agenda-cycle-counter 0)))))
8700 (org-agenda-show-1 org-agenda-cycle-counter))
8702 (defun org-agenda-recenter (arg)
8703 "Display the Org-mode file which contains the item at point and recenter."
8704 (interactive "P")
8705 (let ((win (selected-window)))
8706 (org-agenda-goto t)
8707 (recenter arg)
8708 (select-window win)))
8710 (defun org-agenda-show-mouse (ev)
8711 "Display the Org-mode file which contains the item at the mouse click."
8712 (interactive "e")
8713 (mouse-set-point ev)
8714 (org-agenda-show))
8716 (defun org-agenda-check-no-diary ()
8717 "Check if the entry is a diary link and abort if yes."
8718 (if (org-get-at-bol 'org-agenda-diary-link)
8719 (org-agenda-error)))
8721 (defun org-agenda-error ()
8722 "Throw an error when a command is not allowed in the agenda."
8723 (user-error "Command not allowed in this line"))
8725 (defun org-agenda-tree-to-indirect-buffer (arg)
8726 "Show the subtree corresponding to the current entry in an indirect buffer.
8727 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8729 With a numerical prefix ARG, go up to this level and then take that tree.
8730 With a negative numeric ARG, go up by this number of levels.
8731 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8732 use the dedicated frame)."
8733 (interactive "P")
8734 (if current-prefix-arg
8735 (org-agenda-do-tree-to-indirect-buffer arg)
8736 (let ((agenda-buffer (buffer-name))
8737 (agenda-window (selected-window))
8738 (indirect-window
8739 (and org-last-indirect-buffer
8740 (get-buffer-window org-last-indirect-buffer))))
8741 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8742 (unless (or (eq org-indirect-buffer-display 'new-frame)
8743 (eq org-indirect-buffer-display 'dedicated-frame))
8744 (unwind-protect
8745 (unless (and indirect-window (window-live-p indirect-window))
8746 (setq indirect-window (split-window agenda-window)))
8747 (and indirect-window (select-window indirect-window))
8748 (switch-to-buffer org-last-indirect-buffer :norecord)
8749 (fit-window-to-buffer indirect-window)))
8750 (select-window (get-buffer-window agenda-buffer)))))
8752 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8753 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8754 (org-agenda-check-no-diary)
8755 (let* ((marker (or (org-get-at-bol 'org-marker)
8756 (org-agenda-error)))
8757 (buffer (marker-buffer marker))
8758 (pos (marker-position marker)))
8759 (with-current-buffer buffer
8760 (save-excursion
8761 (goto-char pos)
8762 (funcall 'org-tree-to-indirect-buffer arg)))))
8764 (defvar org-last-heading-marker (make-marker)
8765 "Marker pointing to the headline that last changed its TODO state
8766 by a remote command from the agenda.")
8768 (defun org-agenda-todo-nextset ()
8769 "Switch TODO entry to next sequence."
8770 (interactive)
8771 (org-agenda-todo 'nextset))
8773 (defun org-agenda-todo-previousset ()
8774 "Switch TODO entry to previous sequence."
8775 (interactive)
8776 (org-agenda-todo 'previousset))
8778 (defun org-agenda-todo (&optional arg)
8779 "Cycle TODO state of line at point, also in Org-mode file.
8780 This changes the line at point, all other lines in the agenda referring to
8781 the same tree node, and the headline of the tree node in the Org-mode file."
8782 (interactive "P")
8783 (org-agenda-check-no-diary)
8784 (let* ((col (current-column))
8785 (marker (or (org-get-at-bol 'org-marker)
8786 (org-agenda-error)))
8787 (buffer (marker-buffer marker))
8788 (pos (marker-position marker))
8789 (hdmarker (org-get-at-bol 'org-hd-marker))
8790 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8791 (inhibit-read-only t)
8792 org-agenda-headline-snapshot-before-repeat newhead just-one)
8793 (org-with-remote-undo buffer
8794 (with-current-buffer buffer
8795 (widen)
8796 (goto-char pos)
8797 (org-show-context 'agenda)
8798 (save-excursion
8799 (and (outline-next-heading)
8800 (org-flag-heading nil))) ; show the next heading
8801 (let ((current-prefix-arg arg))
8802 (call-interactively 'org-todo))
8803 (and (bolp) (forward-char 1))
8804 (setq newhead (org-get-heading))
8805 (when (and (org-bound-and-true-p
8806 org-agenda-headline-snapshot-before-repeat)
8807 (not (equal org-agenda-headline-snapshot-before-repeat
8808 newhead))
8809 todayp)
8810 (setq newhead org-agenda-headline-snapshot-before-repeat
8811 just-one t))
8812 (save-excursion
8813 (org-back-to-heading)
8814 (move-marker org-last-heading-marker (point))))
8815 (beginning-of-line 1)
8816 (save-window-excursion
8817 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8818 (when (org-bound-and-true-p org-clock-out-when-done)
8819 (string-match (concat "^" (regexp-opt org-done-keywords-for-agenda))
8820 newhead)
8821 (org-agenda-unmark-clocking-task))
8822 (org-move-to-column col)
8823 (org-agenda-mark-clocking-task))))
8825 (defun org-agenda-add-note (&optional arg)
8826 "Add a time-stamped note to the entry at point."
8827 (interactive "P")
8828 (org-agenda-check-no-diary)
8829 (let* ((marker (or (org-get-at-bol 'org-marker)
8830 (org-agenda-error)))
8831 (buffer (marker-buffer marker))
8832 (pos (marker-position marker))
8833 (hdmarker (org-get-at-bol 'org-hd-marker))
8834 (inhibit-read-only t))
8835 (with-current-buffer buffer
8836 (widen)
8837 (goto-char pos)
8838 (org-show-context 'agenda)
8839 (save-excursion
8840 (and (outline-next-heading)
8841 (org-flag-heading nil))) ; show the next heading
8842 (org-add-note))))
8844 (defun org-agenda-change-all-lines (newhead hdmarker
8845 &optional fixface just-this)
8846 "Change all lines in the agenda buffer which match HDMARKER.
8847 The new content of the line will be NEWHEAD (as modified by
8848 `org-agenda-format-item'). HDMARKER is checked with
8849 `equal' against all `org-hd-marker' text properties in the file.
8850 If FIXFACE is non-nil, the face of each item is modified according to
8851 the new TODO state.
8852 If JUST-THIS is non-nil, change just the current line, not all.
8853 If FORCE-TAGS is non nil, the car of it returns the new tags."
8854 (let* ((inhibit-read-only t)
8855 (line (org-current-line))
8856 (org-agenda-buffer (current-buffer))
8857 (thetags (with-current-buffer (marker-buffer hdmarker)
8858 (save-excursion (save-restriction (widen)
8859 (goto-char hdmarker)
8860 (org-get-tags-at)))))
8861 props m pl undone-face done-face finish new dotime level cat tags)
8862 (save-excursion
8863 (goto-char (point-max))
8864 (beginning-of-line 1)
8865 (while (not finish)
8866 (setq finish (bobp))
8867 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8868 (or (not just-this) (= (org-current-line) line))
8869 (equal m hdmarker))
8870 (setq props (text-properties-at (point))
8871 dotime (org-get-at-bol 'dotime)
8872 cat (org-get-at-eol 'org-category 1)
8873 level (org-get-at-bol 'level)
8874 tags thetags
8876 (let ((org-prefix-format-compiled
8877 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8878 org-prefix-format-compiled))
8879 (extra (org-get-at-bol 'extra)))
8880 (with-current-buffer (marker-buffer hdmarker)
8881 (save-excursion
8882 (save-restriction
8883 (widen)
8884 (org-agenda-format-item extra newhead level cat tags dotime)))))
8885 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8886 undone-face (org-get-at-bol 'undone-face)
8887 done-face (org-get-at-bol 'done-face))
8888 (beginning-of-line 1)
8889 (cond
8890 ((equal new "")
8891 (and (looking-at ".*\n?") (replace-match "")))
8892 ((looking-at ".*")
8893 (replace-match new t t)
8894 (beginning-of-line 1)
8895 (add-text-properties (point-at-bol) (point-at-eol) props)
8896 (when fixface
8897 (add-text-properties
8898 (point-at-bol) (point-at-eol)
8899 (list 'face
8900 (if org-last-todo-state-is-todo
8901 undone-face done-face))))
8902 (org-agenda-highlight-todo 'line)
8903 (beginning-of-line 1))
8904 (t (error "Line update did not work")))
8905 (save-restriction
8906 (narrow-to-region (point-at-bol) (point-at-eol))
8907 (org-agenda-finalize)))
8908 (beginning-of-line 0)))))
8910 (defun org-agenda-align-tags (&optional line)
8911 "Align all tags in agenda items to `org-agenda-tags-column'."
8912 (let ((inhibit-read-only t) l c)
8913 (save-excursion
8914 (goto-char (if line (point-at-bol) (point-min)))
8915 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8916 (if line (point-at-eol) nil) t)
8917 (add-text-properties
8918 (match-beginning 2) (match-end 2)
8919 (list 'face (delq nil (let ((prop (get-text-property
8920 (match-beginning 2) 'face)))
8921 (or (listp prop) (setq prop (list prop)))
8922 (if (memq 'org-tag prop)
8923 prop
8924 (cons 'org-tag prop))))))
8925 (setq l (- (match-end 2) (match-beginning 2))
8926 c (if (< org-agenda-tags-column 0)
8927 (- (abs org-agenda-tags-column) l)
8928 org-agenda-tags-column))
8929 (delete-region (match-beginning 1) (match-end 1))
8930 (goto-char (match-beginning 1))
8931 (insert (org-add-props
8932 (make-string (max 1 (- c (current-column))) ?\ )
8933 (plist-put (copy-sequence (text-properties-at (point)))
8934 'face nil))))
8935 (goto-char (point-min))
8936 (org-font-lock-add-tag-faces (point-max)))))
8938 (defun org-agenda-priority-up ()
8939 "Increase the priority of line at point, also in Org-mode file."
8940 (interactive)
8941 (org-agenda-priority 'up))
8943 (defun org-agenda-priority-down ()
8944 "Decrease the priority of line at point, also in Org-mode file."
8945 (interactive)
8946 (org-agenda-priority 'down))
8948 (defun org-agenda-priority (&optional force-direction)
8949 "Set the priority of line at point, also in Org-mode file.
8950 This changes the line at point, all other lines in the agenda referring to
8951 the same tree node, and the headline of the tree node in the Org-mode file.
8952 Called with a universal prefix arg, show the priority instead of setting it."
8953 (interactive "P")
8954 (if (equal force-direction '(4))
8955 (org-show-priority)
8956 (unless org-enable-priority-commands
8957 (error "Priority commands are disabled"))
8958 (org-agenda-check-no-diary)
8959 (let* ((col (current-column))
8960 (marker (or (org-get-at-bol 'org-marker)
8961 (org-agenda-error)))
8962 (hdmarker (org-get-at-bol 'org-hd-marker))
8963 (buffer (marker-buffer hdmarker))
8964 (pos (marker-position hdmarker))
8965 (inhibit-read-only t)
8966 newhead)
8967 (org-with-remote-undo buffer
8968 (with-current-buffer buffer
8969 (widen)
8970 (goto-char pos)
8971 (org-show-context 'agenda)
8972 (save-excursion
8973 (and (outline-next-heading)
8974 (org-flag-heading nil))) ; show the next heading
8975 (funcall 'org-priority force-direction)
8976 (end-of-line 1)
8977 (setq newhead (org-get-heading)))
8978 (org-agenda-change-all-lines newhead hdmarker)
8979 (org-move-to-column col)))))
8981 ;; FIXME: should fix the tags property of the agenda line.
8982 (defun org-agenda-set-tags (&optional tag onoff)
8983 "Set tags for the current headline."
8984 (interactive)
8985 (org-agenda-check-no-diary)
8986 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8987 (call-interactively 'org-change-tag-in-region)
8988 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8989 (org-agenda-error)))
8990 (buffer (marker-buffer hdmarker))
8991 (pos (marker-position hdmarker))
8992 (inhibit-read-only t)
8993 newhead)
8994 (org-with-remote-undo buffer
8995 (with-current-buffer buffer
8996 (widen)
8997 (goto-char pos)
8998 (save-excursion
8999 (org-show-context 'agenda))
9000 (save-excursion
9001 (and (outline-next-heading)
9002 (org-flag-heading nil))) ; show the next heading
9003 (goto-char pos)
9004 (if tag
9005 (org-toggle-tag tag onoff)
9006 (call-interactively 'org-set-tags))
9007 (end-of-line 1)
9008 (setq newhead (org-get-heading)))
9009 (org-agenda-change-all-lines newhead hdmarker)
9010 (beginning-of-line 1)))))
9012 (defun org-agenda-set-property ()
9013 "Set a property for the current headline."
9014 (interactive)
9015 (org-agenda-check-no-diary)
9016 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9017 (org-agenda-error)))
9018 (buffer (marker-buffer hdmarker))
9019 (pos (marker-position hdmarker))
9020 (inhibit-read-only t)
9021 newhead)
9022 (org-with-remote-undo buffer
9023 (with-current-buffer buffer
9024 (widen)
9025 (goto-char pos)
9026 (save-excursion
9027 (org-show-context 'agenda))
9028 (save-excursion
9029 (and (outline-next-heading)
9030 (org-flag-heading nil))) ; show the next heading
9031 (goto-char pos)
9032 (call-interactively 'org-set-property)))))
9034 (defun org-agenda-set-effort ()
9035 "Set the effort property for the current headline."
9036 (interactive)
9037 (org-agenda-check-no-diary)
9038 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9039 (org-agenda-error)))
9040 (buffer (marker-buffer hdmarker))
9041 (pos (marker-position hdmarker))
9042 (inhibit-read-only t)
9043 newhead)
9044 (org-with-remote-undo buffer
9045 (with-current-buffer buffer
9046 (widen)
9047 (goto-char pos)
9048 (save-excursion
9049 (org-show-context 'agenda))
9050 (save-excursion
9051 (and (outline-next-heading)
9052 (org-flag-heading nil))) ; show the next heading
9053 (goto-char pos)
9054 (call-interactively 'org-set-effort)
9055 (end-of-line 1)
9056 (setq newhead (org-get-heading)))
9057 (org-agenda-change-all-lines newhead hdmarker))))
9059 (defun org-agenda-toggle-archive-tag ()
9060 "Toggle the archive tag for the current entry."
9061 (interactive)
9062 (org-agenda-check-no-diary)
9063 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9064 (org-agenda-error)))
9065 (buffer (marker-buffer hdmarker))
9066 (pos (marker-position hdmarker))
9067 (inhibit-read-only t)
9068 newhead)
9069 (org-with-remote-undo buffer
9070 (with-current-buffer buffer
9071 (widen)
9072 (goto-char pos)
9073 (org-show-context 'agenda)
9074 (save-excursion
9075 (and (outline-next-heading)
9076 (org-flag-heading nil))) ; show the next heading
9077 (call-interactively 'org-toggle-archive-tag)
9078 (end-of-line 1)
9079 (setq newhead (org-get-heading)))
9080 (org-agenda-change-all-lines newhead hdmarker)
9081 (beginning-of-line 1))))
9083 (defun org-agenda-do-date-later (arg)
9084 (interactive "P")
9085 (cond
9086 ((or (equal arg '(16))
9087 (memq last-command
9088 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9089 (setq this-command 'org-agenda-date-later-minutes)
9090 (org-agenda-date-later-minutes 1))
9091 ((or (equal arg '(4))
9092 (memq last-command
9093 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9094 (setq this-command 'org-agenda-date-later-hours)
9095 (org-agenda-date-later-hours 1))
9097 (org-agenda-date-later (prefix-numeric-value arg)))))
9099 (defun org-agenda-do-date-earlier (arg)
9100 (interactive "P")
9101 (cond
9102 ((or (equal arg '(16))
9103 (memq last-command
9104 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9105 (setq this-command 'org-agenda-date-earlier-minutes)
9106 (org-agenda-date-earlier-minutes 1))
9107 ((or (equal arg '(4))
9108 (memq last-command
9109 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9110 (setq this-command 'org-agenda-date-earlier-hours)
9111 (org-agenda-date-earlier-hours 1))
9113 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9115 (defun org-agenda-date-later (arg &optional what)
9116 "Change the date of this item to ARG day(s) later."
9117 (interactive "p")
9118 (org-agenda-check-type t 'agenda 'timeline)
9119 (org-agenda-check-no-diary)
9120 (let* ((marker (or (org-get-at-bol 'org-marker)
9121 (org-agenda-error)))
9122 (buffer (marker-buffer marker))
9123 (pos (marker-position marker))
9124 cdate today)
9125 (org-with-remote-undo buffer
9126 (with-current-buffer buffer
9127 (widen)
9128 (goto-char pos)
9129 (if (not (org-at-timestamp-p))
9130 (error "Cannot find time stamp"))
9131 (when (and org-agenda-move-date-from-past-immediately-to-today
9132 (equal arg 1)
9133 (or (not what) (eq what 'day))
9134 (not (save-match-data (org-at-date-range-p))))
9135 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9136 cdate (calendar-absolute-from-gregorian
9137 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9138 today (org-today))
9139 (if (> today cdate)
9140 ;; immediately shift to today
9141 (setq arg (- today cdate))))
9142 (org-timestamp-change arg (or what 'day))
9143 (when (and (org-at-date-range-p)
9144 (re-search-backward org-tr-regexp-both (point-at-bol)))
9145 (let ((end org-last-changed-timestamp))
9146 (org-timestamp-change arg (or what 'day))
9147 (setq org-last-changed-timestamp
9148 (concat org-last-changed-timestamp "--" end)))))
9149 (org-agenda-show-new-time marker org-last-changed-timestamp))
9150 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9152 (defun org-agenda-date-earlier (arg &optional what)
9153 "Change the date of this item to ARG day(s) earlier."
9154 (interactive "p")
9155 (org-agenda-date-later (- arg) what))
9157 (defun org-agenda-date-later-minutes (arg)
9158 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9159 (interactive "p")
9160 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9161 (org-agenda-date-later arg 'minute))
9163 (defun org-agenda-date-earlier-minutes (arg)
9164 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9165 (interactive "p")
9166 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9167 (org-agenda-date-earlier arg 'minute))
9169 (defun org-agenda-date-later-hours (arg)
9170 "Change the time of this item, in hour steps."
9171 (interactive "p")
9172 (org-agenda-date-later arg 'hour))
9174 (defun org-agenda-date-earlier-hours (arg)
9175 "Change the time of this item, in hour steps."
9176 (interactive "p")
9177 (org-agenda-date-earlier arg 'hour))
9179 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9180 "Show new date stamp via text properties."
9181 ;; We use text properties to make this undoable
9182 (let ((inhibit-read-only t))
9183 (setq stamp (concat prefix " => " stamp " "))
9184 (save-excursion
9185 (goto-char (point-max))
9186 (while (not (bobp))
9187 (when (equal marker (org-get-at-bol 'org-marker))
9188 (remove-text-properties (point-at-bol) (point-at-eol) '(display))
9189 (org-move-to-column (- (window-width) (length stamp)) t)
9190 (if (featurep 'xemacs)
9191 ;; Use `duplicable' property to trigger undo recording
9192 (let ((ex (make-extent nil nil))
9193 (gl (make-glyph stamp)))
9194 (set-glyph-face gl 'secondary-selection)
9195 (set-extent-properties
9196 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9197 (insert-extent ex (1- (point)) (point-at-eol)))
9198 (add-text-properties
9199 (1- (point)) (point-at-eol)
9200 (list 'display (org-add-props stamp nil
9201 'face '(secondary-selection default)))))
9202 (beginning-of-line 1))
9203 (beginning-of-line 0)))))
9205 (defun org-agenda-date-prompt (arg)
9206 "Change the date of this item. Date is prompted for, with default today.
9207 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9208 be used to request time specification in the time stamp."
9209 (interactive "P")
9210 (org-agenda-check-type t 'agenda 'timeline)
9211 (org-agenda-check-no-diary)
9212 (let* ((marker (or (org-get-at-bol 'org-marker)
9213 (org-agenda-error)))
9214 (buffer (marker-buffer marker))
9215 (pos (marker-position marker)))
9216 (org-with-remote-undo buffer
9217 (with-current-buffer buffer
9218 (widen)
9219 (goto-char pos)
9220 (if (not (org-at-timestamp-p t))
9221 (error "Cannot find time stamp"))
9222 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9223 (org-agenda-show-new-time marker org-last-changed-timestamp))
9224 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9226 (defun org-agenda-schedule (arg &optional time)
9227 "Schedule the item at point.
9228 ARG is passed through to `org-schedule'."
9229 (interactive "P")
9230 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9231 (org-agenda-check-no-diary)
9232 (let* ((marker (or (org-get-at-bol 'org-marker)
9233 (org-agenda-error)))
9234 (type (marker-insertion-type marker))
9235 (buffer (marker-buffer marker))
9236 (pos (marker-position marker))
9237 (org-insert-labeled-timestamps-at-point nil)
9239 (set-marker-insertion-type marker t)
9240 (org-with-remote-undo buffer
9241 (with-current-buffer buffer
9242 (widen)
9243 (goto-char pos)
9244 (setq ts (org-schedule arg time)))
9245 (org-agenda-show-new-time marker ts " S"))
9246 (message "%s" ts)))
9248 (defun org-agenda-deadline (arg &optional time)
9249 "Schedule the item at point.
9250 ARG is passed through to `org-deadline'."
9251 (interactive "P")
9252 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9253 (org-agenda-check-no-diary)
9254 (let* ((marker (or (org-get-at-bol 'org-marker)
9255 (org-agenda-error)))
9256 (buffer (marker-buffer marker))
9257 (pos (marker-position marker))
9258 (org-insert-labeled-timestamps-at-point nil)
9260 (org-with-remote-undo buffer
9261 (with-current-buffer buffer
9262 (widen)
9263 (goto-char pos)
9264 (setq ts (org-deadline arg time)))
9265 (org-agenda-show-new-time marker ts " D"))
9266 (message "%s" ts)))
9268 (defun org-agenda-clock-in (&optional arg)
9269 "Start the clock on the currently selected item."
9270 (interactive "P")
9271 (org-agenda-check-no-diary)
9272 (if (equal arg '(4))
9273 (org-clock-in arg)
9274 (let* ((marker (or (org-get-at-bol 'org-marker)
9275 (org-agenda-error)))
9276 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9277 (pos (marker-position marker))
9278 (col (current-column))
9279 newhead)
9280 (org-with-remote-undo (marker-buffer marker)
9281 (with-current-buffer (marker-buffer marker)
9282 (widen)
9283 (goto-char pos)
9284 (org-show-context 'agenda)
9285 (org-show-entry)
9286 (org-cycle-hide-drawers 'children)
9287 (org-clock-in arg)
9288 (setq newhead (org-get-heading)))
9289 (org-agenda-change-all-lines newhead hdmarker))
9290 (org-move-to-column col))))
9292 (defun org-agenda-clock-out ()
9293 "Stop the currently running clock."
9294 (interactive)
9295 (unless (marker-buffer org-clock-marker)
9296 (error "No running clock"))
9297 (let ((marker (make-marker)) (col (current-column)) newhead)
9298 (org-with-remote-undo (marker-buffer org-clock-marker)
9299 (with-current-buffer (marker-buffer org-clock-marker)
9300 (save-excursion
9301 (save-restriction
9302 (widen)
9303 (goto-char org-clock-marker)
9304 (org-back-to-heading t)
9305 (move-marker marker (point))
9306 (org-clock-out)
9307 (setq newhead (org-get-heading))))))
9308 (org-agenda-change-all-lines newhead marker)
9309 (move-marker marker nil)
9310 (org-move-to-column col)
9311 (org-agenda-unmark-clocking-task)))
9313 (defun org-agenda-clock-cancel (&optional arg)
9314 "Cancel the currently running clock."
9315 (interactive "P")
9316 (unless (marker-buffer org-clock-marker)
9317 (user-error "No running clock"))
9318 (org-with-remote-undo (marker-buffer org-clock-marker)
9319 (org-clock-cancel)))
9321 (defun org-agenda-clock-goto ()
9322 "Jump to the currently clocked in task within the agenda.
9323 If the currently clocked in task is not listed in the agenda
9324 buffer, display it in another window."
9325 (interactive)
9326 (let (pos)
9327 (mapc (lambda (o)
9328 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9329 (setq pos (overlay-start o))))
9330 (overlays-in (point-min) (point-max)))
9331 (cond (pos (goto-char pos))
9332 ;; If the currently clocked entry is not in the agenda
9333 ;; buffer, we visit it in another window:
9334 (org-clock-current-task
9335 (org-switch-to-buffer-other-window (org-clock-goto)))
9336 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9338 (defun org-agenda-diary-entry-in-org-file ()
9339 "Make a diary entry in the file `org-agenda-diary-file'."
9340 (let (d1 d2 char (text "") dp1 dp2)
9341 (if (equal (buffer-name) "*Calendar*")
9342 (setq d1 (calendar-cursor-to-date t)
9343 d2 (car calendar-mark-ring))
9344 (setq dp1 (get-text-property (point-at-bol) 'day))
9345 (unless dp1 (user-error "No date defined in current line"))
9346 (setq d1 (calendar-gregorian-from-absolute dp1)
9347 d2 (and (ignore-errors (mark))
9348 (save-excursion
9349 (goto-char (mark))
9350 (setq dp2 (get-text-property (point-at-bol) 'day)))
9351 (calendar-gregorian-from-absolute dp2))))
9352 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9353 (setq char (read-char-exclusive))
9354 (cond
9355 ((equal char ?d)
9356 (setq text (read-string "Day entry: "))
9357 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9358 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9359 ((equal char ?a)
9360 (setq d1 (list (car d1) (nth 1 d1)
9361 (read-number (format "Reference year [%d]: " (nth 2 d1))
9362 (nth 2 d1))))
9363 (setq text (read-string "Anniversary (use %d to show years): "))
9364 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9365 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9366 ((equal char ?b)
9367 (setq text (read-string "Block entry: "))
9368 (unless (and d1 d2 (not (equal d1 d2)))
9369 (user-error "No block of days selected"))
9370 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9371 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9372 ((equal char ?j)
9373 (org-switch-to-buffer-other-window
9374 (find-file-noselect org-agenda-diary-file))
9375 (require 'org-datetree)
9376 (org-datetree-find-date-create d1)
9377 (org-reveal t))
9378 (t (user-error "Invalid selection character `%c'" char)))))
9380 (defcustom org-agenda-insert-diary-strategy 'date-tree
9381 "Where in `org-agenda-diary-file' should new entries be added?
9382 Valid values:
9384 date-tree in the date tree, as child of the date
9385 top-level as top-level entries at the end of the file."
9386 :group 'org-agenda
9387 :type '(choice
9388 (const :tag "in a date tree" date-tree)
9389 (const :tag "as top level at end of file" top-level)))
9391 (defcustom org-agenda-insert-diary-extract-time nil
9392 "Non-nil means extract any time specification from the diary entry."
9393 :group 'org-agenda
9394 :version "24.1"
9395 :type 'boolean)
9397 (defcustom org-agenda-bulk-mark-char ">"
9398 "A single-character string to be used as the bulk mark."
9399 :group 'org-agenda
9400 :version "24.1"
9401 :type 'string)
9403 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9404 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9405 If TEXT is not empty, it will become the headline of the new entry, and
9406 the resulting entry will not be shown. When TEXT is empty, switch to
9407 `org-agenda-diary-file' and let the user finish the entry there."
9408 (let ((cw (current-window-configuration)))
9409 (org-switch-to-buffer-other-window
9410 (find-file-noselect org-agenda-diary-file))
9411 (widen)
9412 (goto-char (point-min))
9413 (cond
9414 ((eq type 'anniversary)
9415 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9416 (progn
9417 (or (org-at-heading-p t)
9418 (progn
9419 (outline-next-heading)
9420 (insert "* Anniversaries\n\n")
9421 (beginning-of-line -1)))))
9422 (outline-next-heading)
9423 (org-back-over-empty-lines)
9424 (backward-char 1)
9425 (insert "\n")
9426 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9427 (nth 2 d1) (car d1) (nth 1 d1) text)))
9428 ((eq type 'day)
9429 (let ((org-prefix-has-time t)
9430 (org-agenda-time-leading-zero t)
9431 fmt time time2)
9432 (if org-agenda-insert-diary-extract-time
9433 ;; Use org-agenda-format-item to parse text for a time-range and
9434 ;; remove it. FIXME: This is a hack, we should refactor
9435 ;; that function to make time extraction available separately
9436 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9437 time (get-text-property 0 'time fmt)
9438 time2 (if (> (length time) 0)
9439 ;; split-string removes trailing ...... if
9440 ;; no end time given. First space
9441 ;; separates time from date.
9442 (concat " " (car (split-string time "\\.")))
9443 nil)
9444 text (get-text-property 0 'txt fmt)))
9445 (if (eq org-agenda-insert-diary-strategy 'top-level)
9446 (org-agenda-insert-diary-as-top-level text)
9447 (require 'org-datetree)
9448 (org-datetree-find-date-create d1)
9449 (org-agenda-insert-diary-make-new-entry text))
9450 (org-insert-time-stamp (org-time-from-absolute
9451 (calendar-absolute-from-gregorian d1))
9452 nil nil nil nil time2))
9453 (end-of-line 0))
9454 ((eq type 'block)
9455 (if (> (calendar-absolute-from-gregorian d1)
9456 (calendar-absolute-from-gregorian d2))
9457 (setq d1 (prog1 d2 (setq d2 d1))))
9458 (if (eq org-agenda-insert-diary-strategy 'top-level)
9459 (org-agenda-insert-diary-as-top-level text)
9460 (require 'org-datetree)
9461 (org-datetree-find-date-create d1)
9462 (org-agenda-insert-diary-make-new-entry text))
9463 (org-insert-time-stamp (org-time-from-absolute
9464 (calendar-absolute-from-gregorian d1)))
9465 (insert "--")
9466 (org-insert-time-stamp (org-time-from-absolute
9467 (calendar-absolute-from-gregorian d2)))
9468 (end-of-line 0)))
9469 (if (string-match "\\S-" text)
9470 (progn
9471 (set-window-configuration cw)
9472 (message "%s entry added to %s"
9473 (capitalize (symbol-name type))
9474 (abbreviate-file-name org-agenda-diary-file)))
9475 (org-reveal t)
9476 (message "Please finish entry here"))))
9478 (defun org-agenda-insert-diary-as-top-level (text)
9479 "Make new entry as a top-level entry at the end of the file.
9480 Add TEXT as headline, and position the cursor in the second line so that
9481 a timestamp can be added there."
9482 (widen)
9483 (goto-char (point-max))
9484 (or (bolp) (insert "\n"))
9485 (insert "* " text "\n")
9486 (if org-adapt-indentation (org-indent-to-column 2)))
9488 (defun org-agenda-insert-diary-make-new-entry (text)
9489 "Make a new entry with TEXT as the first child of the current subtree.
9490 Position the point in the line right after the new heading so
9491 that a timestamp can be added there."
9492 (let ((org-show-following-heading t)
9493 (org-show-siblings t)
9494 (org-show-hierarchy-above t)
9495 (org-show-entry-below t)
9496 col)
9497 (outline-next-heading)
9498 (org-back-over-empty-lines)
9499 (or (looking-at "[ \t]*$")
9500 (progn (insert "\n") (backward-char 1)))
9501 (org-insert-heading nil t)
9502 (org-do-demote)
9503 (setq col (current-column))
9504 (insert text "\n")
9505 (if org-adapt-indentation (org-indent-to-column col))
9506 (let ((org-show-following-heading t)
9507 (org-show-siblings t)
9508 (org-show-hierarchy-above t)
9509 (org-show-entry-below t))
9510 (org-show-context))))
9512 (defun org-agenda-diary-entry ()
9513 "Make a diary entry, like the `i' command from the calendar.
9514 All the standard commands work: block, weekly etc.
9515 When `org-agenda-diary-file' points to a file,
9516 `org-agenda-diary-entry-in-org-file' is called instead to create
9517 entries in that Org-mode file."
9518 (interactive)
9519 (if (not (eq org-agenda-diary-file 'diary-file))
9520 (org-agenda-diary-entry-in-org-file)
9521 (require 'diary-lib)
9522 (let* ((char (progn
9523 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9524 (read-char-exclusive)))
9525 (cmd (cdr (assoc char
9526 '((?d . insert-diary-entry)
9527 (?w . insert-weekly-diary-entry)
9528 (?m . insert-monthly-diary-entry)
9529 (?y . insert-yearly-diary-entry)
9530 (?a . insert-anniversary-diary-entry)
9531 (?b . insert-block-diary-entry)
9532 (?c . insert-cyclic-diary-entry)))))
9533 (oldf (symbol-function 'calendar-cursor-to-date))
9534 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9535 (point (point))
9536 (mark (or (mark t) (point))))
9537 (unless cmd
9538 (user-error "No command associated with <%c>" char))
9539 (unless (and (get-text-property point 'day)
9540 (or (not (equal ?b char))
9541 (get-text-property mark 'day)))
9542 (user-error "Don't know which date to use for diary entry"))
9543 ;; We implement this by hacking the `calendar-cursor-to-date' function
9544 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9545 (let ((calendar-mark-ring
9546 (list (calendar-gregorian-from-absolute
9547 (or (get-text-property mark 'day)
9548 (get-text-property point 'day))))))
9549 (unwind-protect
9550 (progn
9551 (fset 'calendar-cursor-to-date
9552 (lambda (&optional error dummy)
9553 (calendar-gregorian-from-absolute
9554 (get-text-property point 'day))))
9555 (call-interactively cmd))
9556 (fset 'calendar-cursor-to-date oldf))))))
9558 (defun org-agenda-execute-calendar-command (cmd)
9559 "Execute a calendar command from the agenda with date from cursor."
9560 (org-agenda-check-type t 'agenda 'timeline)
9561 (require 'diary-lib)
9562 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9563 (user-error "Don't know which date to use for the calendar command"))
9564 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9565 (point (point))
9566 (date (calendar-gregorian-from-absolute
9567 (get-text-property point 'day)))
9568 ;; the following 2 vars are needed in the calendar
9569 (displayed-month (car date))
9570 (displayed-year (nth 2 date)))
9571 (unwind-protect
9572 (progn
9573 (fset 'calendar-cursor-to-date
9574 (lambda (&optional error dummy)
9575 (calendar-gregorian-from-absolute
9576 (get-text-property point 'day))))
9577 (call-interactively cmd))
9578 (fset 'calendar-cursor-to-date oldf))))
9580 (defun org-agenda-phases-of-moon ()
9581 "Display the phases of the moon for the 3 months around the cursor date."
9582 (interactive)
9583 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9585 (defun org-agenda-holidays ()
9586 "Display the holidays for the 3 months around the cursor date."
9587 (interactive)
9588 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9590 (defvar calendar-longitude) ; defined in calendar.el
9591 (defvar calendar-latitude) ; defined in calendar.el
9592 (defvar calendar-location-name) ; defined in calendar.el
9594 (defun org-agenda-sunrise-sunset (arg)
9595 "Display sunrise and sunset for the cursor date.
9596 Latitude and longitude can be specified with the variables
9597 `calendar-latitude' and `calendar-longitude'. When called with prefix
9598 argument, latitude and longitude will be prompted for."
9599 (interactive "P")
9600 (require 'solar)
9601 (let ((calendar-longitude (if arg nil calendar-longitude))
9602 (calendar-latitude (if arg nil calendar-latitude))
9603 (calendar-location-name
9604 (if arg "the given coordinates" calendar-location-name)))
9605 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9607 (defun org-agenda-goto-calendar ()
9608 "Open the Emacs calendar with the date at the cursor."
9609 (interactive)
9610 (org-agenda-check-type t 'agenda 'timeline)
9611 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9612 (user-error "Don't know which date to open in calendar")))
9613 (date (calendar-gregorian-from-absolute day))
9614 (calendar-move-hook nil)
9615 (calendar-view-holidays-initially-flag nil)
9616 (calendar-view-diary-initially-flag nil))
9617 (calendar)
9618 (calendar-goto-date date)))
9620 ;;;###autoload
9621 (defun org-calendar-goto-agenda ()
9622 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9623 This is a command that has to be installed in `calendar-mode-map'."
9624 (interactive)
9625 (org-agenda-list nil (calendar-absolute-from-gregorian
9626 (calendar-cursor-to-date))
9627 nil))
9629 (defun org-agenda-convert-date ()
9630 (interactive)
9631 (org-agenda-check-type t 'agenda 'timeline)
9632 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9633 date s)
9634 (unless day
9635 (user-error "Don't know which date to convert"))
9636 (setq date (calendar-gregorian-from-absolute day))
9637 (setq s (concat
9638 "Gregorian: " (calendar-date-string date) "\n"
9639 "ISO: " (calendar-iso-date-string date) "\n"
9640 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9641 "Julian: " (calendar-julian-date-string date) "\n"
9642 "Astron. JD: " (calendar-astro-date-string date)
9643 " (Julian date number at noon UTC)\n"
9644 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9645 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9646 "French: " (calendar-french-date-string date) "\n"
9647 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9648 "Mayan: " (calendar-mayan-date-string date) "\n"
9649 "Coptic: " (calendar-coptic-date-string date) "\n"
9650 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9651 "Persian: " (calendar-persian-date-string date) "\n"
9652 "Chinese: " (calendar-chinese-date-string date) "\n"))
9653 (with-output-to-temp-buffer "*Dates*"
9654 (princ s))
9655 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9657 ;;; Bulk commands
9659 (defun org-agenda-bulk-marked-p ()
9660 (eq (get-char-property (point-at-bol) 'type)
9661 'org-marked-entry-overlay))
9663 (defun org-agenda-bulk-mark (&optional arg)
9664 "Mark the entry at point for future bulk action."
9665 (interactive "p")
9666 (dotimes (i (or arg 1))
9667 (unless (org-get-at-bol 'org-agenda-diary-link)
9668 (let* ((m (org-get-at-bol 'org-hd-marker))
9670 (unless (org-agenda-bulk-marked-p)
9671 (unless m (user-error "Nothing to mark at point"))
9672 (push m org-agenda-bulk-marked-entries)
9673 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9674 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9675 (org-get-todo-face "TODO")
9676 'evaporate)
9677 (overlay-put ov 'type 'org-marked-entry-overlay))
9678 (end-of-line 1)
9679 (or (ignore-errors
9680 (goto-char (next-single-property-change (point) 'txt)))
9681 (beginning-of-line 2))
9682 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9683 (beginning-of-line 2))
9684 (message "%d entries marked for bulk action"
9685 (length org-agenda-bulk-marked-entries))))))
9687 (defun org-agenda-bulk-mark-all ()
9688 "Mark all entries for future agenda bulk action."
9689 (interactive)
9690 (org-agenda-bulk-mark-regexp "."))
9692 (defun org-agenda-bulk-mark-regexp (regexp)
9693 "Mark entries matching REGEXP for future agenda bulk action."
9694 (interactive "sMark entries matching regexp: ")
9695 (let ((entries-marked 0) txt-at-point)
9696 (save-excursion
9697 (goto-char (point-min))
9698 (goto-char (next-single-property-change (point) 'txt))
9699 (while (and (re-search-forward regexp nil t)
9700 (setq txt-at-point (get-text-property (point) 'txt)))
9701 (when (string-match regexp txt-at-point)
9702 (setq entries-marked (1+ entries-marked))
9703 (call-interactively 'org-agenda-bulk-mark))))
9704 (if (not entries-marked)
9705 (message "No entry matching this regexp."))))
9707 (defun org-agenda-bulk-unmark (&optional arg)
9708 "Unmark the entry at point for future bulk action."
9709 (interactive "P")
9710 (if arg
9711 (org-agenda-bulk-unmark-all)
9712 (cond ((org-agenda-bulk-marked-p)
9713 (org-agenda-bulk-remove-overlays
9714 (point-at-bol) (+ 2 (point-at-bol)))
9715 (setq org-agenda-bulk-marked-entries
9716 (delete (org-get-at-bol 'org-hd-marker)
9717 org-agenda-bulk-marked-entries))
9718 (end-of-line 1)
9719 (or (ignore-errors
9720 (goto-char (next-single-property-change (point) 'txt)))
9721 (beginning-of-line 2))
9722 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9723 (beginning-of-line 2))
9724 (message "%d entries left marked for bulk action"
9725 (length org-agenda-bulk-marked-entries)))
9726 (t (message "No entry to unmark here")))))
9728 (defun org-agenda-bulk-toggle-all ()
9729 "Toggle all marks for bulk action."
9730 (interactive)
9731 (save-excursion
9732 (goto-char (point-min))
9733 (while (ignore-errors
9734 (goto-char (next-single-property-change (point) 'txt)))
9735 (org-agenda-bulk-toggle))))
9737 (defun org-agenda-bulk-toggle ()
9738 "Toggle the mark at point for bulk action."
9739 (interactive)
9740 (if (org-agenda-bulk-marked-p)
9741 (org-agenda-bulk-unmark)
9742 (org-agenda-bulk-mark)))
9744 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9745 "Remove the mark overlays between BEG and END in the agenda buffer.
9746 BEG and END default to the buffer limits.
9748 This only removes the overlays, it does not remove the markers
9749 from the list in `org-agenda-bulk-marked-entries'."
9750 (interactive)
9751 (mapc (lambda (ov)
9752 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9753 (delete-overlay ov)))
9754 (overlays-in (or beg (point-min)) (or end (point-max)))))
9756 (defun org-agenda-bulk-unmark-all ()
9757 "Remove all marks in the agenda buffer.
9758 This will remove the markers and the overlays."
9759 (interactive)
9760 (if (null org-agenda-bulk-marked-entries)
9761 (message "No entry to unmark")
9762 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9763 (setq org-agenda-bulk-marked-entries nil)
9764 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9766 (defcustom org-agenda-persistent-marks nil
9767 "Non-nil means marked items will stay marked after a bulk action.
9768 You can toggle this interactively by typing `p' when prompted for a
9769 bulk action."
9770 :group 'org-agenda
9771 :version "24.1"
9772 :type 'boolean)
9774 (defun org-agenda-bulk-action (&optional arg)
9775 "Execute an remote-editing action on all marked entries.
9776 The prefix arg is passed through to the command if possible."
9777 (interactive "P")
9778 ;; Make sure we have markers, and only valid ones
9779 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9780 (mapc
9781 (lambda (m)
9782 (unless (and (markerp m)
9783 (marker-buffer m)
9784 (buffer-live-p (marker-buffer m))
9785 (marker-position m))
9786 (user-error "Marker %s for bulk command is invalid" m)))
9787 org-agenda-bulk-marked-entries)
9789 ;; Prompt for the bulk command
9790 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9791 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9792 "[S]catter [f]unction "
9793 (when org-agenda-bulk-custom-functions
9794 (concat " Custom: ["
9795 (mapconcat (lambda(f) (char-to-string (car f)))
9796 org-agenda-bulk-custom-functions "")
9797 "]"))))
9798 (catch 'exit
9799 (let* ((action (read-char-exclusive))
9800 (org-log-refile (if org-log-refile 'time nil))
9801 (entries (reverse org-agenda-bulk-marked-entries))
9802 (org-overriding-default-time
9803 (if (get-text-property (point) 'org-agenda-date-header)
9804 (org-get-cursor-date)))
9805 redo-at-end
9806 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9807 (cond
9808 ((equal action ?p)
9809 (let ((org-agenda-persistent-marks
9810 (not org-agenda-persistent-marks)))
9811 (org-agenda-bulk-action)
9812 (throw 'exit nil)))
9814 ((equal action ?$)
9815 (setq cmd '(org-agenda-archive)))
9817 ((equal action ?A)
9818 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9820 ((member action '(?r ?w))
9821 (setq rfloc (org-refile-get-location
9822 "Refile to"
9823 (marker-buffer (car entries))
9824 org-refile-allow-creating-parent-nodes))
9825 (if (nth 3 rfloc)
9826 (setcar (nthcdr 3 rfloc)
9827 (move-marker (make-marker) (nth 3 rfloc)
9828 (or (get-file-buffer (nth 1 rfloc))
9829 (find-buffer-visiting (nth 1 rfloc))
9830 (error "This should not happen")))))
9832 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9833 redo-at-end t))
9835 ((equal action ?t)
9836 (setq state (org-icompleting-read
9837 "Todo state: "
9838 (with-current-buffer (marker-buffer (car entries))
9839 (mapcar 'list org-todo-keywords-1))))
9840 (setq cmd `(let ((org-inhibit-blocking t)
9841 (org-inhibit-logging 'note))
9842 (org-agenda-todo ,state))))
9844 ((memq action '(?- ?+))
9845 (setq tag (org-icompleting-read
9846 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9847 (with-current-buffer (marker-buffer (car entries))
9848 (delq nil
9849 (mapcar (lambda (x)
9850 (if (stringp (car x)) x)) org-tag-alist)))))
9851 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9853 ((memq action '(?s ?d))
9854 (let* ((time
9855 (unless arg
9856 (org-read-date
9857 nil nil nil
9858 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9859 org-overriding-default-time)))
9860 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9861 (setq cmd `(eval '(,c1 arg ,time)))))
9863 ((equal action ?S)
9864 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9865 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9866 (let ((days (read-number
9867 (format "Scatter tasks across how many %sdays: "
9868 (if arg "week" "")) 7)))
9869 (setq cmd
9870 `(let ((distance (1+ (random ,days))))
9871 (if arg
9872 (let ((dist distance)
9873 (day-of-week
9874 (calendar-day-of-week
9875 (calendar-gregorian-from-absolute (org-today)))))
9876 (dotimes (i (1+ dist))
9877 (while (member day-of-week org-agenda-weekend-days)
9878 (incf distance)
9879 (incf day-of-week)
9880 (if (= day-of-week 7)
9881 (setq day-of-week 0)))
9882 (incf day-of-week)
9883 (if (= day-of-week 7)
9884 (setq day-of-week 0)))))
9885 ;; silently fail when try to replan a sexp entry
9886 (condition-case nil
9887 (let* ((date (calendar-gregorian-from-absolute
9888 (+ (org-today) distance)))
9889 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9890 (nth 2 date))))
9891 (org-agenda-schedule nil time))
9892 (error nil)))))))
9894 ((assoc action org-agenda-bulk-custom-functions)
9895 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9896 redo-at-end t))
9898 ((equal action ?f)
9899 (setq cmd (list (intern
9900 (org-icompleting-read "Function: "
9901 obarray 'fboundp t nil nil)))))
9903 (t (user-error "Invalid bulk action")))
9905 ;; Sort the markers, to make sure that parents are handled before children
9906 (setq entries (sort entries
9907 (lambda (a b)
9908 (cond
9909 ((equal (marker-buffer a) (marker-buffer b))
9910 (< (marker-position a) (marker-position b)))
9912 (string< (buffer-name (marker-buffer a))
9913 (buffer-name (marker-buffer b))))))))
9915 ;; Now loop over all markers and apply cmd
9916 (while (setq e (pop entries))
9917 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9918 (if (not pos)
9919 (progn (message "Skipping removed entry at %s" e)
9920 (setq cntskip (1+ cntskip)))
9921 (goto-char pos)
9922 (let (org-loop-over-headlines-in-active-region)
9923 (eval cmd))
9924 (setq cnt (1+ cnt))))
9925 (when redo-at-end (org-agenda-redo))
9926 (unless org-agenda-persistent-marks
9927 (org-agenda-bulk-unmark-all))
9928 (message "Acted on %d entries%s%s"
9930 (if (= cntskip 0)
9932 (format ", skipped %d (disappeared before their turn)"
9933 cntskip))
9934 (if (not org-agenda-persistent-marks)
9935 "" " (kept marked)"))))))
9937 (defun org-agenda-capture (&optional with-time)
9938 "Call `org-capture' with the date at point.
9939 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9940 current HH:MM time."
9941 (interactive "P")
9942 (if (not (eq major-mode 'org-agenda-mode))
9943 (user-error "You cannot do this outside of agenda buffers")
9944 (let ((org-overriding-default-time
9945 (org-get-cursor-date (equal with-time 1))))
9946 (call-interactively 'org-capture))))
9948 ;;; Dragging agenda lines forward/backward
9950 (defun org-agenda-reapply-filters ()
9951 "Re-apply all agenda filters."
9952 (mapcar
9953 (lambda(f) (when (car f) (org-agenda-filter-apply (car f) (cadr f))))
9954 `((,org-agenda-tag-filter tag)
9955 (,org-agenda-category-filter category)
9956 (,org-agenda-regexp-filter regexp)
9957 (,org-agenda-effort-filter effort)
9958 (,(get 'org-agenda-tag-filter :preset-filter) tag)
9959 (,(get 'org-agenda-category-filter :preset-filter) category)
9960 (,(get 'org-agenda-effort-filter :preset-filter) effort)
9961 (,(get 'org-agenda-regexp-filter :preset-filter) regexp))))
9963 (defun org-agenda-drag-line-forward (arg &optional backward)
9964 "Drag an agenda line forward by ARG lines.
9965 When the optional argument `backward' is non-nil, move backward."
9966 (interactive "p")
9967 (let ((inhibit-read-only t) lst line)
9968 (if (or (not (get-text-property (point) 'txt))
9969 (save-excursion
9970 (dotimes (n arg)
9971 (move-beginning-of-line (if backward 0 2))
9972 (push (not (get-text-property (point) 'txt)) lst))
9973 (delq nil lst)))
9974 (message "Cannot move line forward")
9975 (let ((end (save-excursion (move-beginning-of-line 2) (point))))
9976 (move-beginning-of-line 1)
9977 (setq line (buffer-substring (point) end))
9978 (delete-region (point) end)
9979 (move-beginning-of-line (funcall (if backward '1- '1+) arg))
9980 (insert line)
9981 (org-agenda-reapply-filters)
9982 (org-agenda-mark-clocking-task)
9983 (move-beginning-of-line 0)))))
9985 (defun org-agenda-drag-line-backward (arg)
9986 "Drag an agenda line backward by ARG lines."
9987 (interactive "p")
9988 (org-agenda-drag-line-forward arg t))
9990 ;;; Flagging notes
9992 (defun org-agenda-show-the-flagging-note ()
9993 "Display the flagging note in the other window.
9994 When called a second time in direct sequence, offer to remove the FLAGGING
9995 tag and (if present) the flagging note."
9996 (interactive)
9997 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9998 (win (selected-window))
9999 note heading newhead)
10000 (unless hdmarker
10001 (user-error "No linked entry at point"))
10002 (if (and (eq this-command last-command)
10003 (y-or-n-p "Unflag and remove any flagging note? "))
10004 (progn
10005 (org-agenda-remove-flag hdmarker)
10006 (let ((win (get-buffer-window "*Flagging Note*")))
10007 (and win (delete-window win)))
10008 (message "Entry unflagged"))
10009 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
10010 (unless note
10011 (user-error "No flagging note"))
10012 (org-kill-new note)
10013 (org-switch-to-buffer-other-window "*Flagging Note*")
10014 (erase-buffer)
10015 (insert note)
10016 (goto-char (point-min))
10017 (while (re-search-forward "\\\\n" nil t)
10018 (replace-match "\n" t t))
10019 (goto-char (point-min))
10020 (select-window win)
10021 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
10023 (defun org-agenda-remove-flag (marker)
10024 "Remove the FLAGGED tag and any flagging note in the entry."
10025 (let (newhead)
10026 (org-with-point-at marker
10027 (org-toggle-tag "FLAGGED" 'off)
10028 (org-entry-delete nil "THEFLAGGINGNOTE")
10029 (setq newhead (org-get-heading)))
10030 (org-agenda-change-all-lines newhead marker)
10031 (message "Entry unflagged")))
10033 (defun org-agenda-get-any-marker (&optional pos)
10034 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
10035 (get-text-property (or pos (point-at-bol)) 'org-marker)))
10037 ;;; Appointment reminders
10039 (defvar appt-time-msg-list) ; defined in appt.el
10041 ;;;###autoload
10042 (defun org-agenda-to-appt (&optional refresh filter &rest args)
10043 "Activate appointments found in `org-agenda-files'.
10044 With a \\[universal-argument] prefix, refresh the list of
10045 appointments.
10047 If FILTER is t, interactively prompt the user for a regular
10048 expression, and filter out entries that don't match it.
10050 If FILTER is a string, use this string as a regular expression
10051 for filtering entries out.
10053 If FILTER is a function, filter out entries against which
10054 calling the function returns nil. This function takes one
10055 argument: an entry from `org-agenda-get-day-entries'.
10057 FILTER can also be an alist with the car of each cell being
10058 either 'headline or 'category. For example:
10060 '((headline \"IMPORTANT\")
10061 (category \"Work\"))
10063 will only add headlines containing IMPORTANT or headlines
10064 belonging to the \"Work\" category.
10066 ARGS are symbols indicating what kind of entries to consider.
10067 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
10068 \(i.e., deadlines and scheduled items with a hh:mm specification)
10069 and :timestamp entries. See the docstring of `org-diary' for
10070 details and examples.
10072 If an entry has a APPT_WARNTIME property, its value will be used
10073 to override `appt-message-warning-time'."
10074 (interactive "P")
10075 (if refresh (setq appt-time-msg-list nil))
10076 (if (eq filter t)
10077 (setq filter (read-from-minibuffer "Regexp filter: ")))
10078 (let* ((cnt 0) ; count added events
10079 (scope (or args '(:deadline* :scheduled* :timestamp)))
10080 (org-agenda-new-buffers nil)
10081 (org-deadline-warning-days 0)
10082 ;; Do not use `org-today' here because appt only takes
10083 ;; time and without date as argument, so it may pass wrong
10084 ;; information otherwise
10085 (today (org-date-to-gregorian
10086 (time-to-days (current-time))))
10087 (org-agenda-restrict nil)
10088 (files (org-agenda-files 'unrestricted)) entries file
10089 (org-agenda-buffer nil))
10090 ;; Get all entries which may contain an appt
10091 (org-agenda-prepare-buffers files)
10092 (while (setq file (pop files))
10093 (setq entries
10094 (delq nil
10095 (append entries
10096 (apply 'org-agenda-get-day-entries
10097 file today scope)))))
10098 ;; Map thru entries and find if we should filter them out
10099 (mapc
10100 (lambda(x)
10101 (let* ((evt (org-trim
10102 (replace-regexp-in-string
10103 org-bracket-link-regexp "\\3"
10104 (or (get-text-property 1 'txt x) ""))))
10105 (cat (get-text-property (1- (length x)) 'org-category x))
10106 (tod (get-text-property 1 'time-of-day x))
10107 (ok (or (null filter)
10108 (and (stringp filter) (string-match filter evt))
10109 (and (functionp filter) (funcall filter x))
10110 (and (listp filter)
10111 (let ((cat-filter (cadr (assoc 'category filter)))
10112 (evt-filter (cadr (assoc 'headline filter))))
10113 (or (and (stringp cat-filter)
10114 (string-match cat-filter cat))
10115 (and (stringp evt-filter)
10116 (string-match evt-filter evt)))))))
10117 (wrn (get-text-property 1 'warntime x)))
10118 ;; FIXME: Shall we remove text-properties for the appt text?
10119 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10120 (when (and ok tod)
10121 (setq tod (concat "00" (number-to-string tod))
10122 tod (when (string-match
10123 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10124 (concat (match-string 1 tod) ":"
10125 (match-string 2 tod))))
10126 (if (version< emacs-version "23.3")
10127 (appt-add tod evt)
10128 (appt-add tod evt wrn))
10129 (setq cnt (1+ cnt))))) entries)
10130 (org-release-buffers org-agenda-new-buffers)
10131 (if (eq cnt 0)
10132 (message "No event to add")
10133 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10135 (defun org-agenda-todayp (date)
10136 "Does DATE mean today, when considering `org-extend-today-until'?"
10137 (let ((today (org-today))
10138 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10139 date)))
10140 (eq date today)))
10142 (defun org-agenda-todo-yesterday (&optional arg)
10143 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10144 (interactive "P")
10145 (let* ((hour (third (decode-time
10146 (org-current-time))))
10147 (org-extend-today-until (1+ hour)))
10148 (org-agenda-todo arg)))
10150 (provide 'org-agenda)
10152 ;;; org-agenda.el ends here