org-clock.el (org-clock-insert-selection-line): Don't display the clockout time
[org-mode.git] / lisp / org-agenda.el
blobe72d7e94f1cc5baef6b2f95fbcc6c9f6ff50739f
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the code for creating and using the Agenda for Org-mode.
29 ;; The functions `org-batch-agenda', `org-batch-agenda-csv', and
30 ;; `org-batch-store-agenda-views' are implemented as macros to provide
31 ;; a convenient way for extracting agenda information from the command
32 ;; line. The Lisp does not evaluate parameters of a macro call; thus
33 ;; it is not necessary to quote the parameters passed to one of those
34 ;; functions. E.g. you can write:
36 ;; emacs -batch -l ~/.emacs -eval '(org-batch-agenda "a" org-agenda-span 7)'
38 ;; To export an agenda spanning 7 days. If `org-batch-agenda' would
39 ;; have been implemented as a regular function you'd have to quote the
40 ;; symbol org-agenda-span. Moreover: To use a symbol as parameter
41 ;; value you would have to double quote the symbol.
43 ;; This is a hack, but it works even when running Org byte-compiled.
46 ;;; Code:
48 (require 'org)
49 (require 'org-macs)
50 (eval-when-compile
51 (require 'cl))
53 (declare-function diary-add-to-list "diary-lib"
54 (date string specifier &optional marker globcolor literal))
55 (declare-function calendar-absolute-from-iso "cal-iso" (date))
56 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
57 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
58 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
59 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
60 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
61 (declare-function calendar-french-date-string "cal-french" (&optional date))
62 (declare-function calendar-goto-date "cal-move" (date))
63 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
64 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
65 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
66 (declare-function calendar-iso-from-absolute "cal-iso" (date))
67 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
68 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
69 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
70 (declare-function calendar-check-holidays "holidays" (date))
72 (declare-function org-datetree-find-date-create "org-datetree"
73 (date &optional keep-restriction))
74 (declare-function org-columns-quit "org-colview" ())
75 (declare-function diary-date-display-form "diary-lib" (&optional type))
76 (declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
77 (declare-function org-habit-insert-consistency-graphs
78 "org-habit" (&optional line))
79 (declare-function org-is-habit-p "org-habit" (&optional pom))
80 (declare-function org-habit-parse-todo "org-habit" (&optional pom))
81 (declare-function org-habit-get-priority "org-habit" (habit &optional moment))
82 (declare-function org-pop-to-buffer-same-window "org-compat"
83 (&optional buffer-or-name norecord label))
84 (declare-function org-agenda-columns "org-colview" ())
85 (declare-function org-add-archive-files "org-archive" (files))
86 (declare-function org-capture "org-capture" (&optional goto keys))
88 (defvar calendar-mode-map) ; defined in calendar.el
89 (defvar org-clock-current-task nil) ; defined in org-clock.el
90 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
91 (defvar org-habit-show-habits) ; defined in org-habit.el
92 (defvar org-habit-show-habits-only-for-today)
93 (defvar org-habit-show-all-today)
95 ;; Defined somewhere in this file, but used before definition.
96 (defvar org-agenda-buffer-name "*Org Agenda*")
97 (defvar org-agenda-overriding-header nil)
98 (defvar org-agenda-title-append nil)
99 (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
100 (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
101 (defvar original-date) ; dynamically scoped, calendar.el does scope this
103 (defvar org-agenda-undo-list nil
104 "List of undoable operations in the agenda since last refresh.")
105 (defvar org-agenda-pending-undo-list nil
106 "In a series of undo commands, this is the list of remaining undo items.")
108 (defcustom org-agenda-confirm-kill 1
109 "When set, remote killing from the agenda buffer needs confirmation.
110 When t, a confirmation is always needed. When a number N, confirmation is
111 only needed when the text to be killed contains more than N non-white lines."
112 :group 'org-agenda
113 :type '(choice
114 (const :tag "Never" nil)
115 (const :tag "Always" t)
116 (integer :tag "When more than N lines")))
118 (defcustom org-agenda-compact-blocks nil
119 "Non-nil means make the block agenda more compact.
120 This is done globally by leaving out lines like the agenda span
121 name and week number or the separator lines."
122 :group 'org-agenda
123 :type 'boolean)
125 (defcustom org-agenda-block-separator ?=
126 "The separator between blocks in the agenda.
127 If this is a string, it will be used as the separator, with a newline added.
128 If it is a character, it will be repeated to fill the window width.
129 If nil the separator is disabled. In `org-agenda-custom-commands' this
130 addresses the separator between the current and the previous block."
131 :group 'org-agenda
132 :type '(choice
133 (const :tag "Disabled" nil)
134 (character)
135 (string)))
137 (defgroup org-agenda-export nil
138 "Options concerning exporting agenda views in Org-mode."
139 :tag "Org Agenda Export"
140 :group 'org-agenda)
142 (defcustom org-agenda-with-colors t
143 "Non-nil means use colors in agenda views."
144 :group 'org-agenda-export
145 :type 'boolean)
147 (defcustom org-agenda-exporter-settings nil
148 "Alist of variable/value pairs that should be active during agenda export.
149 This is a good place to set options for ps-print and for htmlize.
150 Note that the way this is implemented, the values will be evaluated
151 before assigned to the variables. So make sure to quote values you do
152 *not* want evaluated, for example
154 (setq org-agenda-exporter-settings
155 '((ps-print-color-p 'black-white)))"
156 :group 'org-agenda-export
157 :type '(repeat
158 (list
159 (variable)
160 (sexp :tag "Value"))))
162 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
163 "Hook run in a temporary buffer before writing the agenda to an export file.
164 A useful function for this hook is `org-agenda-add-entry-text'."
165 :group 'org-agenda-export
166 :type 'hook
167 :options '(org-agenda-add-entry-text))
169 (defcustom org-agenda-add-entry-text-maxlines 0
170 "Maximum number of entry text lines to be added to agenda.
171 This is only relevant when `org-agenda-add-entry-text' is part of
172 `org-agenda-before-write-hook', which is the default.
173 When this is 0, nothing will happen. When it is greater than 0, it
174 specifies the maximum number of lines that will be added for each entry
175 that is listed in the agenda view.
177 Note that this variable is not used during display, only when exporting
178 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
179 and `org-agenda-entry-text-maxlines'."
180 :group 'org-agenda
181 :type 'integer)
183 (defcustom org-agenda-add-entry-text-descriptive-links t
184 "Non-nil means export org-links as descriptive links in agenda added text.
185 This variable applies to the text added to the agenda when
186 `org-agenda-add-entry-text-maxlines' is larger than 0.
187 When this variable nil, the URL will (also) be shown."
188 :group 'org-agenda
189 :type 'boolean)
191 (defcustom org-agenda-export-html-style nil
192 "The style specification for exported HTML Agenda files.
193 If this variable contains a string, it will replace the default <style>
194 section as produced by `htmlize'.
195 Since there are different ways of setting style information, this variable
196 needs to contain the full HTML structure to provide a style, including the
197 surrounding HTML tags. The style specifications should include definitions
198 the fonts used by the agenda, here is an example:
200 <style type=\"text/css\">
201 p { font-weight: normal; color: gray; }
202 .org-agenda-structure {
203 font-size: 110%;
204 color: #003399;
205 font-weight: 600;
207 .org-todo {
208 color: #cc6666;
209 font-weight: bold;
211 .org-agenda-done {
212 color: #339933;
214 .org-done {
215 color: #339933;
217 .title { text-align: center; }
218 .todo, .deadline { color: red; }
219 .done { color: green; }
220 </style>
222 or, if you want to keep the style in a file,
224 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
226 As the value of this option simply gets inserted into the HTML <head> header,
227 you can \"misuse\" it to also add other text to the header."
228 :group 'org-agenda-export
229 :group 'org-export-html
230 :type 'string)
232 (defcustom org-agenda-persistent-filter nil
233 "When set, keep filters from one agenda view to the next."
234 :group 'org-agenda
235 :type 'boolean)
237 (defgroup org-agenda-custom-commands nil
238 "Options concerning agenda views in Org-mode."
239 :tag "Org Agenda Custom Commands"
240 :group 'org-agenda)
242 (defconst org-sorting-choice
243 '(choice
244 (const time-up) (const time-down)
245 (const timestamp-up) (const timestamp-down)
246 (const scheduled-up) (const scheduled-down)
247 (const deadline-up) (const deadline-down)
248 (const ts-up) (const ts-down)
249 (const tsia-up) (const tsia-down)
250 (const category-keep) (const category-up) (const category-down)
251 (const tag-down) (const tag-up)
252 (const priority-up) (const priority-down)
253 (const todo-state-up) (const todo-state-down)
254 (const effort-up) (const effort-down)
255 (const habit-up) (const habit-down)
256 (const alpha-up) (const alpha-down)
257 (const user-defined-up) (const user-defined-down))
258 "Sorting choices.")
260 ;; Keep custom values for `org-agenda-filter-preset' compatible with
261 ;; the new variable `org-agenda-tag-filter-preset'.
262 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
263 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
265 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
266 "List of types searched for when creating the daily/weekly agenda.
267 This variable is a list of symbols that controls the types of
268 items that appear in the daily/weekly agenda. Allowed symbols in this
269 list are are
271 :timestamp List items containing a date stamp or date range matching
272 the selected date. This includes sexp entries in angular
273 brackets.
275 :sexp List entries resulting from plain diary-like sexps.
277 :deadline List deadline due on that date. When the date is today,
278 also list any deadlines past due, or due within
279 `org-deadline-warning-days'. `:deadline' must appear before
280 `:scheduled' if the setting of
281 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
282 any effect.
284 :deadline* Same as above, but only include the deadline if it has an
285 hour specification as [h]h:mm.
287 :scheduled List all items which are scheduled for the given date.
288 The diary for *today* also contains items which were
289 scheduled earlier and are not yet marked DONE.
291 :scheduled* Same as above, but only include the scheduled item if it
292 has an hour specification as [h]h:mm.
294 By default, all four non-starred types are turned on.
296 When :scheduled* or :deadline* are included, :schedule or :deadline
297 will be ignored.
299 Never set this variable globally using `setq', because then it
300 will apply to all future agenda commands. Instead, bind it with
301 `let' to scope it dynamically into the agenda-constructing
302 command. A good way to set it is through options in
303 `org-agenda-custom-commands'. For a more flexible (though
304 somewhat less efficient) way of determining what is included in
305 the daily/weekly agenda, see `org-agenda-skip-function'.")
307 (defconst org-agenda-custom-commands-local-options
308 `(repeat :tag "Local settings for this command. Remember to quote values"
309 (choice :tag "Setting"
310 (list :tag "Heading for this block"
311 (const org-agenda-overriding-header)
312 (string :tag "Headline"))
313 (list :tag "Files to be searched"
314 (const org-agenda-files)
315 (list
316 (const :format "" quote)
317 (repeat (file))))
318 (list :tag "Sorting strategy"
319 (const org-agenda-sorting-strategy)
320 (list
321 (const :format "" quote)
322 (repeat
323 ,org-sorting-choice)))
324 (list :tag "Prefix format"
325 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
326 (string))
327 (list :tag "Number of days in agenda"
328 (const org-agenda-span)
329 (choice (const :tag "Day" 'day)
330 (const :tag "Week" 'week)
331 (const :tag "Month" 'month)
332 (const :tag "Year" 'year)
333 (integer :tag "Custom")))
334 (list :tag "Fixed starting date"
335 (const org-agenda-start-day)
336 (string :value "2007-11-01"))
337 (list :tag "Start on day of week"
338 (const org-agenda-start-on-weekday)
339 (choice :value 1
340 (const :tag "Today" nil)
341 (integer :tag "Weekday No.")))
342 (list :tag "Include data from diary"
343 (const org-agenda-include-diary)
344 (boolean))
345 (list :tag "Deadline Warning days"
346 (const org-deadline-warning-days)
347 (integer :value 1))
348 (list :tag "Category filter preset"
349 (const org-agenda-category-filter-preset)
350 (list
351 (const :format "" quote)
352 (repeat
353 (string :tag "+category or -category"))))
354 (list :tag "Tags filter preset"
355 (const org-agenda-tag-filter-preset)
356 (list
357 (const :format "" quote)
358 (repeat
359 (string :tag "+tag or -tag"))))
360 (list :tag "Regexp filter preset"
361 (const org-agenda-regexp-filter-preset)
362 (list
363 (const :format "" quote)
364 (repeat
365 (string :tag "+regexp or -regexp"))))
366 (list :tag "Set daily/weekly entry types"
367 (const org-agenda-entry-types)
368 (list
369 (const :format "" quote)
370 (set :greedy t :value ,org-agenda-entry-types
371 (const :deadline)
372 (const :scheduled)
373 (const :deadline*)
374 (const :scheduled*)
375 (const :timestamp)
376 (const :sexp))))
377 (list :tag "Standard skipping condition"
378 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
379 (const org-agenda-skip-function)
380 (list
381 (const :format "" quote)
382 (list
383 (choice
384 :tag "Skipping range"
385 (const :tag "Skip entry" org-agenda-skip-entry-if)
386 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
387 (repeat :inline t :tag "Conditions for skipping"
388 (choice
389 :tag "Condition type"
390 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
391 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
392 (list :tag "TODO state is" :inline t
393 (const 'todo)
394 (choice
395 (const :tag "any not-done state" 'todo)
396 (const :tag "any done state" 'done)
397 (const :tag "any state" 'any)
398 (list :tag "Keyword list"
399 (const :format "" quote)
400 (repeat (string :tag "Keyword")))))
401 (list :tag "TODO state is not" :inline t
402 (const 'nottodo)
403 (choice
404 (const :tag "any not-done state" 'todo)
405 (const :tag "any done state" 'done)
406 (const :tag "any state" 'any)
407 (list :tag "Keyword list"
408 (const :format "" quote)
409 (repeat (string :tag "Keyword")))))
410 (const :tag "scheduled" 'scheduled)
411 (const :tag "not scheduled" 'notscheduled)
412 (const :tag "deadline" 'deadline)
413 (const :tag "no deadline" 'notdeadline)
414 (const :tag "timestamp" 'timestamp)
415 (const :tag "no timestamp" 'nottimestamp))))))
416 (list :tag "Non-standard skipping condition"
417 :value (org-agenda-skip-function)
418 (const org-agenda-skip-function)
419 (sexp :tag "Function or form (quoted!)"))
420 (list :tag "Any variable"
421 (variable :tag "Variable")
422 (sexp :tag "Value (sexp)"))))
423 "Selection of examples for agenda command settings.
424 This will be spliced into the custom type of
425 `org-agenda-custom-commands'.")
428 (defcustom org-agenda-custom-commands
429 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
430 "Custom commands for the agenda.
431 These commands will be offered on the splash screen displayed by the
432 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
434 (key desc type match settings files)
436 key The key (one or more characters as a string) to be associated
437 with the command.
438 desc A description of the command, when omitted or nil, a default
439 description is built using MATCH.
440 type The command type, any of the following symbols:
441 agenda The daily/weekly agenda.
442 todo Entries with a specific TODO keyword, in all agenda files.
443 search Entries containing search words entry or headline.
444 tags Tags/Property/TODO match in all agenda files.
445 tags-todo Tags/P/T match in all agenda files, TODO entries only.
446 todo-tree Sparse tree of specific TODO keyword in *current* file.
447 tags-tree Sparse tree with all tags matches in *current* file.
448 occur-tree Occur sparse tree for *current* file.
449 ... A user-defined function.
450 match What to search for:
451 - a single keyword for TODO keyword searches
452 - a tags match expression for tags searches
453 - a word search expression for text searches.
454 - a regular expression for occur searches
455 For all other commands, this should be the empty string.
456 settings A list of option settings, similar to that in a let form, so like
457 this: ((opt1 val1) (opt2 val2) ...). The values will be
458 evaluated at the moment of execution, so quote them when needed.
459 files A list of files file to write the produced agenda buffer to
460 with the command `org-store-agenda-views'.
461 If a file name ends in \".html\", an HTML version of the buffer
462 is written out. If it ends in \".ps\", a postscript version is
463 produced. Otherwise, only the plain text is written to the file.
465 You can also define a set of commands, to create a composite agenda buffer.
466 In this case, an entry looks like this:
468 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
470 where
472 desc A description string to be displayed in the dispatcher menu.
473 cmd An agenda command, similar to the above. However, tree commands
474 are not allowed, but instead you can get agenda and global todo list.
475 So valid commands for a set are:
476 (agenda \"\" settings)
477 (alltodo \"\" settings)
478 (stuck \"\" settings)
479 (todo \"match\" settings files)
480 (search \"match\" settings files)
481 (tags \"match\" settings files)
482 (tags-todo \"match\" settings files)
484 Each command can carry a list of options, and another set of options can be
485 given for the whole set of commands. Individual command options take
486 precedence over the general options.
488 When using several characters as key to a command, the first characters
489 are prefix commands. For the dispatcher to display useful information, you
490 should provide a description for the prefix, like
492 (setq org-agenda-custom-commands
493 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
494 (\"hl\" tags \"+HOME+Lisa\")
495 (\"hp\" tags \"+HOME+Peter\")
496 (\"hk\" tags \"+HOME+Kim\")))"
497 :group 'org-agenda-custom-commands
498 :type `(repeat
499 (choice :value ("x" "Describe command here" tags "" nil)
500 (list :tag "Single command"
501 (string :tag "Access Key(s) ")
502 (option (string :tag "Description"))
503 (choice
504 (const :tag "Agenda" agenda)
505 (const :tag "TODO list" alltodo)
506 (const :tag "Search words" search)
507 (const :tag "Stuck projects" stuck)
508 (const :tag "Tags/Property match (all agenda files)" tags)
509 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
510 (const :tag "TODO keyword search (all agenda files)" todo)
511 (const :tag "Tags sparse tree (current buffer)" tags-tree)
512 (const :tag "TODO keyword tree (current buffer)" todo-tree)
513 (const :tag "Occur tree (current buffer)" occur-tree)
514 (sexp :tag "Other, user-defined function"))
515 (string :tag "Match (only for some commands)")
516 ,org-agenda-custom-commands-local-options
517 (option (repeat :tag "Export" (file :tag "Export to"))))
518 (list :tag "Command series, all agenda files"
519 (string :tag "Access Key(s)")
520 (string :tag "Description ")
521 (repeat :tag "Component"
522 (choice
523 (list :tag "Agenda"
524 (const :format "" agenda)
525 (const :tag "" :format "" "")
526 ,org-agenda-custom-commands-local-options)
527 (list :tag "TODO list (all keywords)"
528 (const :format "" alltodo)
529 (const :tag "" :format "" "")
530 ,org-agenda-custom-commands-local-options)
531 (list :tag "Search words"
532 (const :format "" search)
533 (string :tag "Match")
534 ,org-agenda-custom-commands-local-options)
535 (list :tag "Stuck projects"
536 (const :format "" stuck)
537 (const :tag "" :format "" "")
538 ,org-agenda-custom-commands-local-options)
539 (list :tag "Tags search"
540 (const :format "" tags)
541 (string :tag "Match")
542 ,org-agenda-custom-commands-local-options)
543 (list :tag "Tags search, TODO entries only"
544 (const :format "" tags-todo)
545 (string :tag "Match")
546 ,org-agenda-custom-commands-local-options)
547 (list :tag "TODO keyword search"
548 (const :format "" todo)
549 (string :tag "Match")
550 ,org-agenda-custom-commands-local-options)
551 (list :tag "Other, user-defined function"
552 (symbol :tag "function")
553 (string :tag "Match")
554 ,org-agenda-custom-commands-local-options)))
556 (repeat :tag "Settings for entire command set"
557 (list (variable :tag "Any variable")
558 (sexp :tag "Value")))
559 (option (repeat :tag "Export" (file :tag "Export to"))))
560 (cons :tag "Prefix key documentation"
561 (string :tag "Access Key(s)")
562 (string :tag "Description ")))))
564 (defcustom org-agenda-query-register ?o
565 "The register holding the current query string.
566 The purpose of this is that if you construct a query string interactively,
567 you can then use it to define a custom command."
568 :group 'org-agenda-custom-commands
569 :type 'character)
571 (defcustom org-stuck-projects
572 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
573 "How to identify stuck projects.
574 This is a list of four items:
575 1. A tags/todo/property matcher string that is used to identify a project.
576 See the manual for a description of tag and property searches.
577 The entire tree below a headline matched by this is considered one project.
578 2. A list of TODO keywords identifying non-stuck projects.
579 If the project subtree contains any headline with one of these todo
580 keywords, the project is considered to be not stuck. If you specify
581 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
582 3. A list of tags identifying non-stuck projects.
583 If the project subtree contains any headline with one of these tags,
584 the project is considered to be not stuck. If you specify \"*\" as
585 a tag, any tag will mark the project unstuck. Note that this is about
586 the explicit presence of a tag somewhere in the subtree, inherited
587 tags do not count here. If inherited tags make a project not stuck,
588 use \"-TAG\" in the tags part of the matcher under (1.) above.
589 4. An arbitrary regular expression matching non-stuck projects.
591 If the project turns out to be not stuck, search continues also in the
592 subtree to see if any of the subtasks have project status.
594 See also the variable `org-tags-match-list-sublevels' which applies
595 to projects matched by this search as well.
597 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
598 or `C-c a #' to produce the list."
599 :group 'org-agenda-custom-commands
600 :type '(list
601 (string :tag "Tags/TODO match to identify a project")
602 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
603 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
604 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
606 (defcustom org-agenda-filter-effort-default-operator "<"
607 "The default operator for effort estimate filtering.
608 If you select an effort estimate limit without first pressing an operator,
609 this one will be used."
610 :group 'org-agenda-custom-commands
611 :type '(choice (const :tag "less or equal" "<")
612 (const :tag "greater or equal"">")
613 (const :tag "equal" "=")))
615 (defgroup org-agenda-skip nil
616 "Options concerning skipping parts of agenda files."
617 :tag "Org Agenda Skip"
618 :group 'org-agenda)
620 (defcustom org-agenda-skip-function-global nil
621 "Function to be called at each match during agenda construction.
622 If this function returns nil, the current match should not be skipped.
623 If the function decided to skip an agenda match, is must return the
624 buffer position from which the search should be continued.
625 This may also be a Lisp form, which will be evaluated.
627 This variable will be applied to every agenda match, including
628 tags/property searches and TODO lists. So try to make the test function
629 do its checking as efficiently as possible. To implement a skipping
630 condition just for specific agenda commands, use the variable
631 `org-agenda-skip-function' which can be set in the options section
632 of custom agenda commands."
633 :group 'org-agenda-skip
634 :type 'sexp)
636 (defgroup org-agenda-daily/weekly nil
637 "Options concerning the daily/weekly agenda."
638 :tag "Org Agenda Daily/Weekly"
639 :group 'org-agenda)
640 (defgroup org-agenda-todo-list nil
641 "Options concerning the global todo list agenda view."
642 :tag "Org Agenda Todo List"
643 :group 'org-agenda)
644 (defgroup org-agenda-match-view nil
645 "Options concerning the general tags/property/todo match agenda view."
646 :tag "Org Agenda Match View"
647 :group 'org-agenda)
648 (defgroup org-agenda-search-view nil
649 "Options concerning the general tags/property/todo match agenda view."
650 :tag "Org Agenda Match View"
651 :group 'org-agenda)
653 (defvar org-agenda-archives-mode nil
654 "Non-nil means the agenda will include archived items.
655 If this is the symbol `trees', trees in the selected agenda scope
656 that are marked with the ARCHIVE tag will be included anyway. When this is
657 t, also all archive files associated with the current selection of agenda
658 files will be included.")
660 (defcustom org-agenda-restriction-lock-highlight-subtree t
661 "Non-nil means highlight the whole subtree when restriction is active.
662 Otherwise only highlight the headline. Highlighting the whole subtree is
663 useful to ensure no edits happen beyond the restricted region."
664 :group 'org-agenda
665 :type 'boolean)
667 (defcustom org-agenda-skip-comment-trees t
668 "Non-nil means skip trees that start with the COMMENT keyword.
669 When nil, these trees are also scanned by agenda commands."
670 :group 'org-agenda-skip
671 :type 'boolean)
673 (defcustom org-agenda-todo-list-sublevels t
674 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
675 When nil, the sublevels of a TODO entry are not checked, resulting in
676 potentially much shorter TODO lists."
677 :group 'org-agenda-skip
678 :group 'org-agenda-todo-list
679 :type 'boolean)
681 (defcustom org-agenda-todo-ignore-with-date nil
682 "Non-nil means don't show entries with a date in the global todo list.
683 You can use this if you prefer to mark mere appointments with a TODO keyword,
684 but don't want them to show up in the TODO list.
685 When this is set, it also covers deadlines and scheduled items, the settings
686 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
687 will be ignored.
688 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
689 :group 'org-agenda-skip
690 :group 'org-agenda-todo-list
691 :type 'boolean)
693 (defcustom org-agenda-todo-ignore-timestamp nil
694 "Non-nil means don't show entries with a timestamp.
695 This applies when creating the global todo list.
696 Valid values are:
698 past Don't show entries for today or in the past.
700 future Don't show entries with a timestamp in the future.
701 The idea behind this is that if it has a future
702 timestamp, you don't want to think about it until the
703 date.
705 all Don't show any entries with a timestamp in the global todo list.
706 The idea behind this is that by setting a timestamp, you
707 have already \"taken care\" of this item.
709 This variable can also have an integer as a value. If positive (N),
710 todos with a timestamp N or more days in the future will be ignored. If
711 negative (-N), todos with a timestamp N or more days in the past will be
712 ignored. If 0, todos with a timestamp either today or in the future will
713 be ignored. For example, a value of -1 will exclude todos with a
714 timestamp in the past (yesterday or earlier), while a value of 7 will
715 exclude todos with a timestamp a week or more in the future.
717 See also `org-agenda-todo-ignore-with-date'.
718 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
719 to make his option also apply to the tags-todo list."
720 :group 'org-agenda-skip
721 :group 'org-agenda-todo-list
722 :version "24.1"
723 :type '(choice
724 (const :tag "Ignore future timestamp todos" future)
725 (const :tag "Ignore past or present timestamp todos" past)
726 (const :tag "Ignore all timestamp todos" all)
727 (const :tag "Show timestamp todos" nil)
728 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
730 (defcustom org-agenda-todo-ignore-scheduled nil
731 "Non-nil means, ignore some scheduled TODO items when making TODO list.
732 This applies when creating the global todo list.
733 Valid values are:
735 past Don't show entries scheduled today or in the past.
737 future Don't show entries scheduled in the future.
738 The idea behind this is that by scheduling it, you don't want to
739 think about it until the scheduled date.
741 all Don't show any scheduled entries in the global todo list.
742 The idea behind this is that by scheduling it, you have already
743 \"taken care\" of this item.
745 t Same as `all', for backward compatibility.
747 This variable can also have an integer as a value. See
748 `org-agenda-todo-ignore-timestamp' for more details.
750 See also `org-agenda-todo-ignore-with-date'.
751 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
752 to make his option also apply to the tags-todo list."
753 :group 'org-agenda-skip
754 :group 'org-agenda-todo-list
755 :type '(choice
756 (const :tag "Ignore future-scheduled todos" future)
757 (const :tag "Ignore past- or present-scheduled todos" past)
758 (const :tag "Ignore all scheduled todos" all)
759 (const :tag "Ignore all scheduled todos (compatibility)" t)
760 (const :tag "Show scheduled todos" nil)
761 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
763 (defcustom org-agenda-todo-ignore-deadlines nil
764 "Non-nil means ignore some deadlined TODO items when making TODO list.
765 There are different motivations for using different values, please think
766 carefully when configuring this variable.
768 This applies when creating the global todo list.
769 Valid values are:
771 near Don't show near deadline entries. A deadline is near when it is
772 closer than `org-deadline-warning-days' days. The idea behind this
773 is that such items will appear in the agenda anyway.
775 far Don't show TODO entries where a deadline has been defined, but
776 the deadline is not near. This is useful if you don't want to
777 use the todo list to figure out what to do now.
779 past Don't show entries with a deadline timestamp for today or in the past.
781 future Don't show entries with a deadline timestamp in the future, not even
782 when they become `near' ones. Use it with caution.
784 all Ignore all TODO entries that do have a deadline.
786 t Same as `near', for backward compatibility.
788 This variable can also have an integer as a value. See
789 `org-agenda-todo-ignore-timestamp' for more details.
791 See also `org-agenda-todo-ignore-with-date'.
792 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
793 to make his option also apply to the tags-todo list."
794 :group 'org-agenda-skip
795 :group 'org-agenda-todo-list
796 :type '(choice
797 (const :tag "Ignore near deadlines" near)
798 (const :tag "Ignore near deadlines (compatibility)" t)
799 (const :tag "Ignore far deadlines" far)
800 (const :tag "Ignore all TODOs with a deadlines" all)
801 (const :tag "Show all TODOs, even if they have a deadline" nil)
802 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
804 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
805 "Time unit to use when possibly ignoring an agenda item.
807 See the docstring of various `org-agenda-todo-ignore-*' options.
808 The default is to compare time stamps using days. An item is thus
809 considered to be in the future if it is at least one day after today.
810 Non-nil means to compare time stamps using seconds. An item is then
811 considered future if it has a time value later than current time."
812 :group 'org-agenda-skip
813 :group 'org-agenda-todo-list
814 :version "24.4"
815 :package-version '(Org . "8.0")
816 :type '(choice
817 (const :tag "Compare time with days" nil)
818 (const :tag "Compare time with seconds" t)))
820 (defcustom org-agenda-tags-todo-honor-ignore-options nil
821 "Non-nil means honor todo-list ignores options also in tags-todo search.
822 The variables
823 `org-agenda-todo-ignore-with-date',
824 `org-agenda-todo-ignore-timestamp',
825 `org-agenda-todo-ignore-scheduled',
826 `org-agenda-todo-ignore-deadlines'
827 make the global TODO list skip entries that have time stamps of certain
828 kinds. If this option is set, the same options will also apply for the
829 tags-todo search, which is the general tags/property matcher
830 restricted to unfinished TODO entries only."
831 :group 'org-agenda-skip
832 :group 'org-agenda-todo-list
833 :group 'org-agenda-match-view
834 :type 'boolean)
836 (defcustom org-agenda-skip-scheduled-if-done nil
837 "Non-nil means don't show scheduled items in agenda when they are done.
838 This is relevant for the daily/weekly agenda, not for the TODO list. And
839 it applies only to the actual date of the scheduling. Warnings about
840 an item with a past scheduling dates are always turned off when the item
841 is DONE."
842 :group 'org-agenda-skip
843 :group 'org-agenda-daily/weekly
844 :type 'boolean)
846 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
847 "Non-nil means skip scheduling line if same entry shows because of deadline.
849 In the agenda of today, an entry can show up multiple times
850 because it is both scheduled and has a nearby deadline, and maybe
851 a plain time stamp as well.
853 When this variable is nil, the entry will be shown several times.
855 When set to t, then only the deadline is shown and the fact that
856 the entry is scheduled today or was scheduled previously is not
857 shown.
859 When set to the symbol `not-today', skip scheduled previously,
860 but not scheduled today.
862 When set to the symbol `repeated-after-deadline', skip scheduled
863 items if they are repeated beyond the current dealine."
864 :group 'org-agenda-skip
865 :group 'org-agenda-daily/weekly
866 :type '(choice
867 (const :tag "Never" nil)
868 (const :tag "Always" t)
869 (const :tag "Not when scheduled today" not-today)
870 (const :tag "When repeated past deadline" repeated-after-deadline)))
872 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
873 "Non-nil means skip timestamp line if same entry shows because of deadline.
874 In the agenda of today, an entry can show up multiple times
875 because it has both a plain timestamp and has a nearby deadline.
876 When this variable is t, then only the deadline is shown and the
877 fact that the entry has a timestamp for or including today is not
878 shown. When this variable is nil, the entry will be shown
879 several times."
880 :group 'org-agenda-skip
881 :group 'org-agenda-daily/weekly
882 :version "24.1"
883 :type '(choice
884 (const :tag "Never" nil)
885 (const :tag "Always" t)))
887 (defcustom org-agenda-skip-deadline-if-done nil
888 "Non-nil means don't show deadlines when the corresponding item is done.
889 When nil, the deadline is still shown and should give you a happy feeling.
890 This is relevant for the daily/weekly agenda. And it applied only to the
891 actually date of the deadline. Warnings about approaching and past-due
892 deadlines are always turned off when the item is DONE."
893 :group 'org-agenda-skip
894 :group 'org-agenda-daily/weekly
895 :type 'boolean)
897 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
898 "Non-nil means skip deadline prewarning when entry is also scheduled.
899 This will apply on all days where a prewarning for the deadline would
900 be shown, but not at the day when the entry is actually due. On that day,
901 the deadline will be shown anyway.
902 This variable may be set to nil, t, the symbol `pre-scheduled',
903 or a number which will then give the number of days before the actual
904 deadline when the prewarnings should resume. The symbol `pre-scheduled'
905 eliminates the deadline prewarning only prior to the scheduled date.
906 This can be used in a workflow where the first showing of the deadline will
907 trigger you to schedule it, and then you don't want to be reminded of it
908 because you will take care of it on the day when scheduled."
909 :group 'org-agenda-skip
910 :group 'org-agenda-daily/weekly
911 :version "24.1"
912 :type '(choice
913 (const :tag "Always show prewarning" nil)
914 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
915 (const :tag "Remove prewarning if entry is scheduled" t)
916 (integer :tag "Restart prewarning N days before deadline")))
918 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
919 "Non-nil means skip scheduled delay when entry also has a deadline.
920 This variable may be set to nil, t, the symbol `post-deadline',
921 or a number which will then give the number of days after the actual
922 scheduled date when the delay should expire. The symbol `post-deadline'
923 eliminates the schedule delay when the date is posterior to the deadline."
924 :group 'org-agenda-skip
925 :group 'org-agenda-daily/weekly
926 :version "24.4"
927 :package-version '(Org . "8.0")
928 :type '(choice
929 (const :tag "Always honor delay" nil)
930 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
931 (const :tag "Ignore delay if entry has a deadline" t)
932 (integer :tag "Honor delay up until N days after the scheduled date")))
934 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
935 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
936 When non-nil, after the search for timestamps has matched once in an
937 entry, the rest of the entry will not be searched."
938 :group 'org-agenda-skip
939 :type 'boolean)
941 (defcustom org-agenda-skip-timestamp-if-done nil
942 "Non-nil means don't select item by timestamp or -range if it is DONE."
943 :group 'org-agenda-skip
944 :group 'org-agenda-daily/weekly
945 :type 'boolean)
947 (defcustom org-agenda-dim-blocked-tasks t
948 "Non-nil means dim blocked tasks in the agenda display.
949 This causes some overhead during agenda construction, but if you
950 have turned on `org-enforce-todo-dependencies',
951 `org-enforce-todo-checkbox-dependencies', or any other blocking
952 mechanism, this will create useful feedback in the agenda.
954 Instead of t, this variable can also have the value `invisible'.
955 Then blocked tasks will be invisible and only become visible when
956 they become unblocked. An exemption to this behavior is when a task is
957 blocked because of unchecked checkboxes below it. Since checkboxes do
958 not show up in the agenda views, making this task invisible you remove any
959 trace from agenda views that there is something to do. Therefore, a task
960 that is blocked because of checkboxes will never be made invisible, it
961 will only be dimmed."
962 :group 'org-agenda-daily/weekly
963 :group 'org-agenda-todo-list
964 :version "24.3"
965 :type '(choice
966 (const :tag "Do not dim" nil)
967 (const :tag "Dim to a gray face" t)
968 (const :tag "Make invisible" invisible)))
970 (defcustom org-timeline-show-empty-dates 3
971 "Non-nil means `org-timeline' also shows dates without an entry.
972 When nil, only the days which actually have entries are shown.
973 When t, all days between the first and the last date are shown.
974 When an integer, show also empty dates, but if there is a gap of more than
975 N days, just insert a special line indicating the size of the gap."
976 :group 'org-agenda-skip
977 :type '(choice
978 (const :tag "None" nil)
979 (const :tag "All" t)
980 (integer :tag "at most")))
982 (defgroup org-agenda-startup nil
983 "Options concerning initial settings in the Agenda in Org Mode."
984 :tag "Org Agenda Startup"
985 :group 'org-agenda)
987 (defcustom org-agenda-menu-show-matcher t
988 "Non-nil means show the match string in the agenda dispatcher menu.
989 When nil, the matcher string is not shown, but is put into the help-echo
990 property so than moving the mouse over the command shows it.
991 Setting it to nil is good if matcher strings are very long and/or if
992 you want to use two-columns display (see `org-agenda-menu-two-columns')."
993 :group 'org-agenda
994 :version "24.1"
995 :type 'boolean)
997 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
999 (defcustom org-agenda-menu-two-columns nil
1000 "Non-nil means, use two columns to show custom commands in the dispatcher.
1001 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1002 to nil."
1003 :group 'org-agenda
1004 :version "24.1"
1005 :type 'boolean)
1007 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1008 (defcustom org-agenda-finalize-hook nil
1009 "Hook run just before displaying an agenda buffer.
1010 The buffer is still writable when the hook is called.
1012 You can modify some of the buffer substrings but you should be
1013 extra careful not to modify the text properties of the agenda
1014 headlines as the agenda display heavily relies on them."
1015 :group 'org-agenda-startup
1016 :type 'hook)
1018 (defcustom org-agenda-mouse-1-follows-link nil
1019 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1020 A longer mouse click will still set point. Does not work on XEmacs.
1021 Needs to be set before org.el is loaded."
1022 :group 'org-agenda-startup
1023 :type 'boolean)
1025 (defcustom org-agenda-start-with-follow-mode nil
1026 "The initial value of follow mode in a newly created agenda window."
1027 :group 'org-agenda-startup
1028 :type 'boolean)
1030 (defcustom org-agenda-follow-indirect nil
1031 "Non-nil means `org-agenda-follow-mode' displays only the
1032 current item's tree, in an indirect buffer."
1033 :group 'org-agenda
1034 :version "24.1"
1035 :type 'boolean)
1037 (defcustom org-agenda-show-outline-path t
1038 "Non-nil means show outline path in echo area after line motion."
1039 :group 'org-agenda-startup
1040 :type 'boolean)
1042 (defcustom org-agenda-start-with-entry-text-mode nil
1043 "The initial value of entry-text-mode in a newly created agenda window."
1044 :group 'org-agenda-startup
1045 :type 'boolean)
1047 (defcustom org-agenda-entry-text-maxlines 5
1048 "Number of text lines to be added when `E' is pressed in the agenda.
1050 Note that this variable only used during agenda display. Add add entry text
1051 when exporting the agenda, configure the variable
1052 `org-agenda-add-entry-ext-maxlines'."
1053 :group 'org-agenda
1054 :type 'integer)
1056 (defcustom org-agenda-entry-text-exclude-regexps nil
1057 "List of regular expressions to clean up entry text.
1058 The complete matches of all regular expressions in this list will be
1059 removed from entry text before it is shown in the agenda."
1060 :group 'org-agenda
1061 :type '(repeat (regexp)))
1063 (defcustom org-agenda-entry-text-leaders " > "
1064 "Text prepended to the entry text in agenda buffers."
1065 :version "24.4"
1066 :package-version '(Org . "8.0")
1067 :group 'org-agenda
1068 :type 'string)
1070 (defvar org-agenda-entry-text-cleanup-hook nil
1071 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1072 This cleanup is done in a temporary buffer, so the function may inspect and
1073 change the entire buffer.
1074 Some default stuff like drawers and scheduling/deadline dates will already
1075 have been removed when this is called, as will any matches for regular
1076 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1078 (defvar org-agenda-include-inactive-timestamps nil
1079 "Non-nil means include inactive time stamps in agenda and timeline.
1080 Dynamically scoped.")
1082 (defgroup org-agenda-windows nil
1083 "Options concerning the windows used by the Agenda in Org Mode."
1084 :tag "Org Agenda Windows"
1085 :group 'org-agenda)
1087 (defcustom org-agenda-window-setup 'reorganize-frame
1088 "How the agenda buffer should be displayed.
1089 Possible values for this option are:
1091 current-window Show agenda in the current window, keeping all other windows.
1092 other-window Use `switch-to-buffer-other-window' to display agenda.
1093 reorganize-frame Show only two windows on the current frame, the current
1094 window and the agenda.
1095 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1096 Also, when exiting the agenda, kill that frame.
1097 See also the variable `org-agenda-restore-windows-after-quit'."
1098 :group 'org-agenda-windows
1099 :type '(choice
1100 (const current-window)
1101 (const other-frame)
1102 (const other-window)
1103 (const reorganize-frame)))
1105 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1106 "The min and max height of the agenda window as a fraction of frame height.
1107 The value of the variable is a cons cell with two numbers between 0 and 1.
1108 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1109 :group 'org-agenda-windows
1110 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1112 (defcustom org-agenda-restore-windows-after-quit nil
1113 "Non-nil means restore window configuration upon exiting agenda.
1114 Before the window configuration is changed for displaying the agenda,
1115 the current status is recorded. When the agenda is exited with
1116 `q' or `x' and this option is set, the old state is restored. If
1117 `org-agenda-window-setup' is `other-frame', the value of this
1118 option will be ignored."
1119 :group 'org-agenda-windows
1120 :type 'boolean)
1122 (defcustom org-agenda-ndays nil
1123 "Number of days to include in overview display.
1124 Should be 1 or 7.
1125 Obsolete, see `org-agenda-span'."
1126 :group 'org-agenda-daily/weekly
1127 :type 'integer)
1129 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1131 (defcustom org-agenda-span 'week
1132 "Number of days to include in overview display.
1133 Can be day, week, month, year, or any number of days.
1134 Custom commands can set this variable in the options section."
1135 :group 'org-agenda-daily/weekly
1136 :type '(choice (const :tag "Day" day)
1137 (const :tag "Week" week)
1138 (const :tag "Month" month)
1139 (const :tag "Year" year)
1140 (integer :tag "Custom")))
1142 (defcustom org-agenda-start-on-weekday 1
1143 "Non-nil means start the overview always on the specified weekday.
1144 0 denotes Sunday, 1 denotes Monday, etc.
1145 When nil, always start on the current day.
1146 Custom commands can set this variable in the options section."
1147 :group 'org-agenda-daily/weekly
1148 :type '(choice (const :tag "Today" nil)
1149 (integer :tag "Weekday No.")))
1151 (defcustom org-agenda-show-all-dates t
1152 "Non-nil means `org-agenda' shows every day in the selected range.
1153 When nil, only the days which actually have entries are shown."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1157 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1158 "Format string for displaying dates in the agenda.
1159 Used by the daily/weekly agenda and by the timeline. This should be
1160 a format string understood by `format-time-string', or a function returning
1161 the formatted date as a string. The function must take a single argument,
1162 a calendar-style date list like (month day year)."
1163 :group 'org-agenda-daily/weekly
1164 :type '(choice
1165 (string :tag "Format string")
1166 (function :tag "Function")))
1168 (defun org-agenda-format-date-aligned (date)
1169 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1170 This function makes sure that dates are aligned for easy reading."
1171 (require 'cal-iso)
1172 (let* ((dayname (calendar-day-name date))
1173 (day (cadr date))
1174 (day-of-week (calendar-day-of-week date))
1175 (month (car date))
1176 (monthname (calendar-month-name month))
1177 (year (nth 2 date))
1178 (iso-week (org-days-to-iso-week
1179 (calendar-absolute-from-gregorian date)))
1180 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1181 (1- year))
1182 ((and (= month 12) (<= iso-week 1))
1183 (1+ year))
1184 (t year)))
1185 (weekstring (if (= day-of-week 1)
1186 (format " W%02d" iso-week)
1187 "")))
1188 (format "%-10s %2d %s %4d%s"
1189 dayname day monthname year weekstring)))
1191 (defcustom org-agenda-time-leading-zero nil
1192 "Non-nil means use leading zero for military times in agenda.
1193 For example, 9:30am would become 09:30 rather than 9:30."
1194 :group 'org-agenda-daily/weekly
1195 :version "24.1"
1196 :type 'boolean)
1198 (defcustom org-agenda-timegrid-use-ampm nil
1199 "When set, show AM/PM style timestamps on the timegrid."
1200 :group 'org-agenda
1201 :version "24.1"
1202 :type 'boolean)
1204 (defun org-agenda-time-of-day-to-ampm (time)
1205 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1206 (let* ((hour-number (string-to-number (substring time 0 -3)))
1207 (minute (substring time -2))
1208 (ampm "am"))
1209 (cond
1210 ((equal hour-number 12)
1211 (setq ampm "pm"))
1212 ((> hour-number 12)
1213 (setq ampm "pm")
1214 (setq hour-number (- hour-number 12))))
1215 (concat
1216 (if org-agenda-time-leading-zero
1217 (format "%02d" hour-number)
1218 (format "%02s" (number-to-string hour-number)))
1219 ":" minute ampm)))
1221 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1222 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1223 (if org-agenda-timegrid-use-ampm
1224 (org-agenda-time-of-day-to-ampm time)
1225 time))
1227 (defcustom org-agenda-weekend-days '(6 0)
1228 "Which days are weekend?
1229 These days get the special face `org-agenda-date-weekend' in the agenda
1230 and timeline buffers."
1231 :group 'org-agenda-daily/weekly
1232 :type '(set :greedy t
1233 (const :tag "Monday" 1)
1234 (const :tag "Tuesday" 2)
1235 (const :tag "Wednesday" 3)
1236 (const :tag "Thursday" 4)
1237 (const :tag "Friday" 5)
1238 (const :tag "Saturday" 6)
1239 (const :tag "Sunday" 0)))
1241 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1242 "Non-nil means jump to today when moving a past date forward in time.
1243 When using S-right in the agenda to move a a date forward, and the date
1244 stamp currently points to the past, the first key press will move it
1245 to today. WHen nil, just move one day forward even if the date stays
1246 in the past."
1247 :group 'org-agenda-daily/weekly
1248 :version "24.1"
1249 :type 'boolean)
1251 (defcustom org-agenda-include-diary nil
1252 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1253 Custom commands can set this variable in the options section."
1254 :group 'org-agenda-daily/weekly
1255 :type 'boolean)
1257 (defcustom org-agenda-include-deadlines t
1258 "If non-nil, include entries within their deadline warning period.
1259 Custom commands can set this variable in the options section."
1260 :group 'org-agenda-daily/weekly
1261 :version "24.1"
1262 :type 'boolean)
1264 (defcustom org-agenda-repeating-timestamp-show-all t
1265 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1266 When set to a list of strings, only show occurrences of repeating
1267 stamps for these TODO keywords. When nil, only one occurrence is
1268 shown, either today or the nearest into the future."
1269 :group 'org-agenda-daily/weekly
1270 :type '(choice
1271 (const :tag "Show repeating stamps" t)
1272 (repeat :tag "Show repeating stamps for these TODO keywords"
1273 (string :tag "TODO Keyword"))
1274 (const :tag "Don't show repeating stamps" nil)))
1276 (defcustom org-scheduled-past-days 10000
1277 "Number of days to continue listing scheduled items not marked DONE.
1278 When an item is scheduled on a date, it shows up in the agenda on this
1279 day and will be listed until it is marked done for the number of days
1280 given here."
1281 :group 'org-agenda-daily/weekly
1282 :type 'integer)
1284 (defcustom org-agenda-log-mode-items '(closed clock)
1285 "List of items that should be shown in agenda log mode.
1286 This list may contain the following symbols:
1288 closed Show entries that have been closed on that day.
1289 clock Show entries that have received clocked time on that day.
1290 state Show all logged state changes.
1291 Note that instead of changing this variable, you can also press `C-u l' in
1292 the agenda to display all available LOG items temporarily."
1293 :group 'org-agenda-daily/weekly
1294 :type '(set :greedy t (const closed) (const clock) (const state)))
1296 (defcustom org-agenda-clock-consistency-checks
1297 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1298 :gap-ok-around ("4:00")
1299 :default-face ((:background "DarkRed") (:foreground "white"))
1300 :overlap-face nil :gap-face nil :no-end-time-face nil
1301 :long-face nil :short-face nil)
1302 "This is a property list, with the following keys:
1304 :max-duration Mark clocking chunks that are longer than this time.
1305 This is a time string like \"HH:MM\", or the number
1306 of minutes as an integer.
1308 :min-duration Mark clocking chunks that are shorter that this.
1309 This is a time string like \"HH:MM\", or the number
1310 of minutes as an integer.
1312 :max-gap Mark gaps between clocking chunks that are longer than
1313 this duration. A number of minutes, or a string
1314 like \"HH:MM\".
1316 :gap-ok-around List of times during the day which are usually not working
1317 times. When a gap is detected, but the gap contains any
1318 of these times, the gap is *not* reported. For example,
1319 if this is (\"4:00\" \"13:00\") then gaps that contain
1320 4:00 in the morning (i.e. the night) and 13:00
1321 (i.e. a typical lunch time) do not cause a warning.
1322 You should have at least one time during the night in this
1323 list, or otherwise the first task each morning will trigger
1324 a warning because it follows a long gap.
1326 Furthermore, the following properties can be used to define faces for
1327 issue display.
1329 :default-face the default face, if the specific face is undefined
1330 :overlap-face face for overlapping clocks
1331 :gap-face face for gaps between clocks
1332 :no-end-time-face face for incomplete clocks
1333 :long-face face for clock intervals that are too long
1334 :short-face face for clock intervals that are too short"
1335 :group 'org-agenda-daily/weekly
1336 :group 'org-clock
1337 :version "24.1"
1338 :type 'plist)
1340 (defcustom org-agenda-log-mode-add-notes t
1341 "Non-nil means add first line of notes to log entries in agenda views.
1342 If a log item like a state change or a clock entry is associated with
1343 notes, the first line of these notes will be added to the entry in the
1344 agenda display."
1345 :group 'org-agenda-daily/weekly
1346 :type 'boolean)
1348 (defcustom org-agenda-start-with-log-mode nil
1349 "The initial value of log-mode in a newly created agenda window.
1350 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1351 explanations on the possible values."
1352 :group 'org-agenda-startup
1353 :group 'org-agenda-daily/weekly
1354 :type '(choice (const :tag "Don't show log items" nil)
1355 (const :tag "Show only log items" 'only)
1356 (const :tag "Show all possible log items" 'clockcheck)
1357 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1358 (choice (const :tag "Show closed log items" 'closed)
1359 (const :tag "Show clocked log items" 'clock)
1360 (const :tag "Show all logged state changes" 'state)))))
1362 (defcustom org-agenda-start-with-clockreport-mode nil
1363 "The initial value of clockreport-mode in a newly created agenda window."
1364 :group 'org-agenda-startup
1365 :group 'org-agenda-daily/weekly
1366 :type 'boolean)
1368 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1369 "Property list with parameters for the clocktable in clockreport mode.
1370 This is the display mode that shows a clock table in the daily/weekly
1371 agenda, the properties for this dynamic block can be set here.
1372 The usual clocktable parameters are allowed here, but you cannot set
1373 the properties :name, :tstart, :tend, :block, and :scope - these will
1374 be overwritten to make sure the content accurately reflects the
1375 current display in the agenda."
1376 :group 'org-agenda-daily/weekly
1377 :type 'plist)
1379 (defcustom org-agenda-search-view-always-boolean nil
1380 "Non-nil means the search string is interpreted as individual parts.
1382 The search string for search view can either be interpreted as a phrase,
1383 or as a list of snippets that define a boolean search for a number of
1384 strings.
1386 When this is non-nil, the string will be split on whitespace, and each
1387 snippet will be searched individually, and all must match in order to
1388 select an entry. A snippet is then a single string of non-white
1389 characters, or a string in double quotes, or a regexp in {} braces.
1390 If a snippet is preceded by \"-\", the snippet must *not* match.
1391 \"+\" is syntactic sugar for positive selection. Each snippet may
1392 be found as a full word or a partial word, but see the variable
1393 `org-agenda-search-view-force-full-words'.
1395 When this is nil, search will look for the entire search phrase as one,
1396 with each space character matching any amount of whitespace, including
1397 line breaks.
1399 Even when this is nil, you can still switch to Boolean search dynamically
1400 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1401 is a regexp marked with braces like \"{abc}\", this will also switch to
1402 boolean search."
1403 :group 'org-agenda-search-view
1404 :version "24.1"
1405 :type 'boolean)
1407 (org-defvaralias 'org-agenda-search-view-search-words-only
1408 'org-agenda-search-view-always-boolean)
1410 (defcustom org-agenda-search-view-force-full-words nil
1411 "Non-nil means, search words must be matches as complete words.
1412 When nil, they may also match part of a word."
1413 :group 'org-agenda-search-view
1414 :version "24.1"
1415 :type 'boolean)
1417 (defcustom org-agenda-search-view-max-outline-level nil
1418 "Maximum outline level to display in search view.
1419 E.g. when this is set to 1, the search view will only
1420 show headlines of level 1."
1421 :group 'org-agenda-search-view
1422 :version "24.4"
1423 :package-version '(Org . "8.0")
1424 :type 'integer)
1426 (defgroup org-agenda-time-grid nil
1427 "Options concerning the time grid in the Org-mode Agenda."
1428 :tag "Org Agenda Time Grid"
1429 :group 'org-agenda)
1431 (defcustom org-agenda-search-headline-for-time t
1432 "Non-nil means search headline for a time-of-day.
1433 If the headline contains a time-of-day in one format or another, it will
1434 be used to sort the entry into the time sequence of items for a day.
1435 Some people have time stamps in the headline that refer to the creation
1436 time or so, and then this produces an unwanted side effect. If this is
1437 the case for your, use this variable to turn off searching the headline
1438 for a time."
1439 :group 'org-agenda-time-grid
1440 :type 'boolean)
1442 (defcustom org-agenda-use-time-grid t
1443 "Non-nil means show a time grid in the agenda schedule.
1444 A time grid is a set of lines for specific times (like every two hours between
1445 8:00 and 20:00). The items scheduled for a day at specific times are
1446 sorted in between these lines.
1447 For details about when the grid will be shown, and what it will look like, see
1448 the variable `org-agenda-time-grid'."
1449 :group 'org-agenda-time-grid
1450 :type 'boolean)
1452 (defcustom org-agenda-time-grid
1453 '((daily today require-timed)
1454 "----------------"
1455 (800 1000 1200 1400 1600 1800 2000))
1457 "The settings for time grid for agenda display.
1458 This is a list of three items. The first item is again a list. It contains
1459 symbols specifying conditions when the grid should be displayed:
1461 daily if the agenda shows a single day
1462 weekly if the agenda shows an entire week
1463 today show grid on current date, independent of daily/weekly display
1464 require-timed show grid only if at least one item has a time specification
1466 The second item is a string which will be placed behind the grid time.
1468 The third item is a list of integers, indicating the times that should have
1469 a grid line."
1470 :group 'org-agenda-time-grid
1471 :type
1472 '(list
1473 (set :greedy t :tag "Grid Display Options"
1474 (const :tag "Show grid in single day agenda display" daily)
1475 (const :tag "Show grid in weekly agenda display" weekly)
1476 (const :tag "Always show grid for today" today)
1477 (const :tag "Show grid only if any timed entries are present"
1478 require-timed)
1479 (const :tag "Skip grid times already present in an entry"
1480 remove-match))
1481 (string :tag "Grid String")
1482 (repeat :tag "Grid Times" (integer :tag "Time"))))
1484 (defcustom org-agenda-show-current-time-in-grid t
1485 "Non-nil means show the current time in the time grid."
1486 :group 'org-agenda-time-grid
1487 :version "24.1"
1488 :type 'boolean)
1490 (defcustom org-agenda-current-time-string
1491 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1492 "The string for the current time marker in the agenda."
1493 :group 'org-agenda-time-grid
1494 :version "24.1"
1495 :type 'string)
1497 (defgroup org-agenda-sorting nil
1498 "Options concerning sorting in the Org-mode Agenda."
1499 :tag "Org Agenda Sorting"
1500 :group 'org-agenda)
1502 (defcustom org-agenda-sorting-strategy
1503 '((agenda habit-down time-up priority-down category-keep)
1504 (todo priority-down category-keep)
1505 (tags priority-down category-keep)
1506 (search category-keep))
1507 "Sorting structure for the agenda items of a single day.
1508 This is a list of symbols which will be used in sequence to determine
1509 if an entry should be listed before another entry. The following
1510 symbols are recognized:
1512 time-up Put entries with time-of-day indications first, early first
1513 time-down Put entries with time-of-day indications first, late first
1514 timestamp-up Sort by any timestamp, early first
1515 timestamp-down Sort by any timestamp, late first
1516 scheduled-up Sort by scheduled timestamp, early first
1517 scheduled-down Sort by scheduled timestamp, late first
1518 deadline-up Sort by deadline timestamp, early first
1519 deadline-down Sort by deadline timestamp, late first
1520 ts-up Sort by active timestamp, early first
1521 ts-down Sort by active timestamp, late first
1522 tsia-up Sort by inactive timestamp, early first
1523 tsia-down Sort by inactive timestamp, late first
1524 category-keep Keep the default order of categories, corresponding to the
1525 sequence in `org-agenda-files'.
1526 category-up Sort alphabetically by category, A-Z.
1527 category-down Sort alphabetically by category, Z-A.
1528 tag-up Sort alphabetically by last tag, A-Z.
1529 tag-down Sort alphabetically by last tag, Z-A.
1530 priority-up Sort numerically by priority, high priority last.
1531 priority-down Sort numerically by priority, high priority first.
1532 todo-state-up Sort by todo state, tasks that are done last.
1533 todo-state-down Sort by todo state, tasks that are done first.
1534 effort-up Sort numerically by estimated effort, high effort last.
1535 effort-down Sort numerically by estimated effort, high effort first.
1536 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1537 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1538 habit-up Put entries that are habits first
1539 habit-down Put entries that are habits last
1540 alpha-up Sort headlines alphabetically
1541 alpha-down Sort headlines alphabetically, reversed
1543 The different possibilities will be tried in sequence, and testing stops
1544 if one comparison returns a \"not-equal\". For example, the default
1545 '(time-up category-keep priority-down)
1546 means: Pull out all entries having a specified time of day and sort them,
1547 in order to make a time schedule for the current day the first thing in the
1548 agenda listing for the day. Of the entries without a time indication, keep
1549 the grouped in categories, don't sort the categories, but keep them in
1550 the sequence given in `org-agenda-files'. Within each category sort by
1551 priority.
1553 Leaving out `category-keep' would mean that items will be sorted across
1554 categories by priority.
1556 Instead of a single list, this can also be a set of list for specific
1557 contents, with a context symbol in the car of the list, any of
1558 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1560 Custom commands can bind this variable in the options section."
1561 :group 'org-agenda-sorting
1562 :type `(choice
1563 (repeat :tag "General" ,org-sorting-choice)
1564 (list :tag "Individually"
1565 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1566 (repeat ,org-sorting-choice))
1567 (cons (const :tag "Strategy for TODO lists" todo)
1568 (repeat ,org-sorting-choice))
1569 (cons (const :tag "Strategy for Tags matches" tags)
1570 (repeat ,org-sorting-choice))
1571 (cons (const :tag "Strategy for search matches" search)
1572 (repeat ,org-sorting-choice)))))
1574 (defcustom org-agenda-cmp-user-defined nil
1575 "A function to define the comparison `user-defined'.
1576 This function must receive two arguments, agenda entry a and b.
1577 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1578 the user comparison, return nil.
1579 When this is defined, you can make `user-defined-up' and `user-defined-down'
1580 part of an agenda sorting strategy."
1581 :group 'org-agenda-sorting
1582 :type 'symbol)
1584 (defcustom org-sort-agenda-notime-is-late t
1585 "Non-nil means items without time are considered late.
1586 This is only relevant for sorting. When t, items which have no explicit
1587 time like 15:30 will be considered as 99:01, i.e. later than any items which
1588 do have a time. When nil, the default time is before 0:00. You can use this
1589 option to decide if the schedule for today should come before or after timeless
1590 agenda entries."
1591 :group 'org-agenda-sorting
1592 :type 'boolean)
1594 (defcustom org-sort-agenda-noeffort-is-high t
1595 "Non-nil means items without effort estimate are sorted as high effort.
1596 This also applies when filtering an agenda view with respect to the
1597 < or > effort operator. Then, tasks with no effort defined will be treated
1598 as tasks with high effort.
1599 When nil, such items are sorted as 0 minutes effort."
1600 :group 'org-agenda-sorting
1601 :type 'boolean)
1603 (defgroup org-agenda-line-format nil
1604 "Options concerning the entry prefix in the Org-mode agenda display."
1605 :tag "Org Agenda Line Format"
1606 :group 'org-agenda)
1608 (defcustom org-agenda-prefix-format
1609 '((agenda . " %i %-12:c%?-12t% s")
1610 (timeline . " % s")
1611 (todo . " %i %-12:c")
1612 (tags . " %i %-12:c")
1613 (search . " %i %-12:c"))
1614 "Format specifications for the prefix of items in the agenda views.
1615 An alist with five entries, each for the different agenda types. The
1616 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1617 The values are format strings.
1619 This format works similar to a printf format, with the following meaning:
1621 %c the category of the item, \"Diary\" for entries from the diary,
1622 or as given by the CATEGORY keyword or derived from the file name
1623 %e the effort required by the item
1624 %l the level of the item (insert X space(s) if item is of level X)
1625 %i the icon category of the item, see `org-agenda-category-icon-alist'
1626 %T the last tag of the item (ignore inherited tags, which come first)
1627 %t the HH:MM time-of-day specification if one applies to the entry
1628 %s Scheduling/Deadline information, a short string
1629 %b show breadcrumbs, i.e., the names of the higher levels
1630 %(expression) Eval EXPRESSION and replace the control string
1631 by the result
1633 All specifiers work basically like the standard `%s' of printf, but may
1634 contain two additional characters: a question mark just after the `%'
1635 and a whitespace/punctuation character just before the final letter.
1637 If the first character after `%' is a question mark, the entire field
1638 will only be included if the corresponding value applies to the current
1639 entry. This is useful for fields which should have fixed width when
1640 present, but zero width when absent. For example, \"%?-12t\" will
1641 result in a 12 character time field if a time of the day is specified,
1642 but will completely disappear in entries which do not contain a time.
1644 If there is punctuation or whitespace character just before the
1645 final format letter, this character will be appended to the field
1646 value if the value is not empty. For example, the format
1647 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1648 the category is empty, no additional colon is inserted.
1650 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1651 which means:
1653 - Indent the line with two space characters
1654 - Give the category a 12 chars wide field, padded with whitespace on
1655 the right (because of `-'). Append a colon if there is a category
1656 (because of `:').
1657 - If there is a time-of-day, put it into a 12 chars wide field. If no
1658 time, don't put in an empty field, just skip it (because of '?').
1659 - Finally, put the scheduling information.
1661 See also the variables `org-agenda-remove-times-when-in-prefix' and
1662 `org-agenda-remove-tags'.
1664 Custom commands can set this variable in the options section."
1665 :type '(choice
1666 (string :tag "General format")
1667 (list :greedy t :tag "View dependent"
1668 (cons (const agenda) (string :tag "Format"))
1669 (cons (const timeline) (string :tag "Format"))
1670 (cons (const todo) (string :tag "Format"))
1671 (cons (const tags) (string :tag "Format"))
1672 (cons (const search) (string :tag "Format"))))
1673 :group 'org-agenda-line-format)
1675 (defvar org-prefix-format-compiled nil
1676 "The compiled prefix format and associated variables.
1677 This is a list where first element is a list of variable bindings, and second
1678 element is the compiled format expression. See the variable
1679 `org-agenda-prefix-format'.")
1681 (defcustom org-agenda-todo-keyword-format "%-1s"
1682 "Format for the TODO keyword in agenda lines.
1683 Set this to something like \"%-12s\" if you want all TODO keywords
1684 to occupy a fixed space in the agenda display."
1685 :group 'org-agenda-line-format
1686 :type 'string)
1688 (defcustom org-agenda-diary-sexp-prefix nil
1689 "A regexp that matches part of a diary sexp entry
1690 which should be treated as scheduling/deadline information in
1691 `org-agenda'.
1693 For example, you can use this to extract the `diary-remind-message' from
1694 `diary-remind' entries."
1695 :group 'org-agenda-line-format
1696 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1698 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1699 "Text preceding timerange entries in the agenda view.
1700 This is a list with two strings. The first applies when the range
1701 is entirely on one day. The second applies if the range spans several days.
1702 The strings may have two \"%d\" format specifiers which will be filled
1703 with the sequence number of the days, and the total number of days in the
1704 range, respectively."
1705 :group 'org-agenda-line-format
1706 :type '(list
1707 (string :tag "Deadline today ")
1708 (choice :tag "Deadline relative"
1709 (string :tag "Format string")
1710 (function))))
1712 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1713 "Text preceding scheduled items in the agenda view.
1714 This is a list with two strings. The first applies when the item is
1715 scheduled on the current day. The second applies when it has been scheduled
1716 previously, it may contain a %d indicating that this is the nth time that
1717 this item is scheduled, due to automatic rescheduling of unfinished items
1718 for the following day. So this number is one larger than the number of days
1719 that passed since this item was scheduled first."
1720 :group 'org-agenda-line-format
1721 :version "24.4"
1722 :package-version '(Org . "8.0")
1723 :type '(list
1724 (string :tag "Scheduled today ")
1725 (string :tag "Scheduled previously")))
1727 (defcustom org-agenda-inactive-leader "["
1728 "Text preceding item pulled into the agenda by inactive time stamps.
1729 These entries are added to the agenda when pressing \"[\"."
1730 :group 'org-agenda-line-format
1731 :version "24.1"
1732 :type '(list
1733 (string :tag "Scheduled today ")
1734 (string :tag "Scheduled previously")))
1736 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1737 "Text preceding deadline items in the agenda view.
1738 This is a list with three strings. The first applies when the item has its
1739 deadline on the current day. The second applies when the deadline is in the
1740 future, the third one when it is in the past. The strings may contain %d
1741 to capture the number of days."
1742 :group 'org-agenda-line-format
1743 :version "24.4"
1744 :package-version '(Org . "8.0")
1745 :type '(list
1746 (string :tag "Deadline today ")
1747 (choice :tag "Deadline relative"
1748 (string :tag "Format string")
1749 (function))))
1751 (defcustom org-agenda-remove-times-when-in-prefix t
1752 "Non-nil means remove duplicate time specifications in agenda items.
1753 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1754 time-of-day specification in a headline or diary entry is extracted and
1755 placed into the prefix. If this option is non-nil, the original specification
1756 \(a timestamp or -range, or just a plain time(range) specification like
1757 11:30-4pm) will be removed for agenda display. This makes the agenda less
1758 cluttered.
1759 The option can be t or nil. It may also be the symbol `beg', indicating
1760 that the time should only be removed when it is located at the beginning of
1761 the headline/diary entry."
1762 :group 'org-agenda-line-format
1763 :type '(choice
1764 (const :tag "Always" t)
1765 (const :tag "Never" nil)
1766 (const :tag "When at beginning of entry" beg)))
1768 (defcustom org-agenda-remove-timeranges-from-blocks nil
1769 "Non-nil means remove time ranges specifications in agenda
1770 items that span on several days."
1771 :group 'org-agenda-line-format
1772 :version "24.1"
1773 :type 'boolean)
1775 (defcustom org-agenda-default-appointment-duration nil
1776 "Default duration for appointments that only have a starting time.
1777 When nil, no duration is specified in such cases.
1778 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1779 :group 'org-agenda-line-format
1780 :type '(choice
1781 (integer :tag "Minutes")
1782 (const :tag "No default duration")))
1784 (defcustom org-agenda-show-inherited-tags t
1785 "Non-nil means show inherited tags in each agenda line.
1787 When this option is set to 'always, it take precedences over
1788 `org-agenda-use-tag-inheritance' and inherited tags are shown
1789 in every agenda.
1791 When this option is set to t (the default), inherited tags are
1792 shown when they are available, i.e. when the value of
1793 `org-agenda-use-tag-inheritance' has been taken into account.
1795 This can be set to a list of agenda types in which the agenda
1796 must display the inherited tags. Available types are 'todo,
1797 'agenda, 'search and 'timeline.
1799 When set to nil, never show inherited tags in agenda lines."
1800 :group 'org-agenda-line-format
1801 :group 'org-agenda
1802 :version "24.3"
1803 :type '(choice
1804 (const :tag "Show inherited tags when available" t)
1805 (const :tag "Always show inherited tags" 'always)
1806 (repeat :tag "Show inherited tags only in selected agenda types"
1807 (symbol :tag "Agenda type"))))
1809 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1810 "List of agenda view types where to use tag inheritance.
1812 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1813 controlled by `org-use-tag-inheritance'. In other agenda types,
1814 `org-use-tag-inheritance' is not used for the selection of the
1815 agenda entries. Still, you may want the agenda to be aware of
1816 the inherited tags anyway, e.g. for later tag filtering.
1818 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1820 This variable has no effect if `org-agenda-show-inherited-tags'
1821 is set to 'always. In that case, the agenda is aware of those
1822 tags.
1824 The default value sets tags in every agenda type. Setting this
1825 option to nil will speed up non-tags agenda view a lot."
1826 :group 'org-agenda
1827 :version "24.3"
1828 :type '(choice
1829 (const :tag "Use tag inheritance in all agenda types" t)
1830 (repeat :tag "Use tag inheritance in selected agenda types"
1831 (symbol :tag "Agenda type"))))
1833 (defcustom org-agenda-hide-tags-regexp nil
1834 "Regular expression used to filter away specific tags in agenda views.
1835 This means that these tags will be present, but not be shown in the agenda
1836 line. Secondary filtering will still work on the hidden tags.
1837 Nil means don't hide any tags."
1838 :group 'org-agenda-line-format
1839 :type '(choice
1840 (const :tag "Hide none" nil)
1841 (string :tag "Regexp ")))
1843 (defcustom org-agenda-remove-tags nil
1844 "Non-nil means remove the tags from the headline copy in the agenda.
1845 When this is the symbol `prefix', only remove tags when
1846 `org-agenda-prefix-format' contains a `%T' specifier."
1847 :group 'org-agenda-line-format
1848 :type '(choice
1849 (const :tag "Always" t)
1850 (const :tag "Never" nil)
1851 (const :tag "When prefix format contains %T" prefix)))
1853 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1854 'org-agenda-remove-tags)
1856 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1857 "Shift tags in agenda items to this column.
1858 If this number is positive, it specifies the column. If it is negative,
1859 it means that the tags should be flushright to that column. For example,
1860 -80 works well for a normal 80 character screen."
1861 :group 'org-agenda-line-format
1862 :type 'integer)
1864 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1866 (defcustom org-agenda-fontify-priorities 'cookies
1867 "Non-nil means highlight low and high priorities in agenda.
1868 When t, the highest priority entries are bold, lowest priority italic.
1869 However, settings in `org-priority-faces' will overrule these faces.
1870 When this variable is the symbol `cookies', only fontify the
1871 cookies, not the entire task.
1872 This may also be an association list of priority faces, whose
1873 keys are the character values of `org-highest-priority',
1874 `org-default-priority', and `org-lowest-priority' (the default values
1875 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1876 color as a string, or a list like `(:background \"Red\")'.
1877 If it is a color, the variable `org-faces-easy-properties'
1878 determines if it is a foreground or a background color."
1879 :group 'org-agenda-line-format
1880 :type '(choice
1881 (const :tag "Never" nil)
1882 (const :tag "Defaults" t)
1883 (const :tag "Cookies only" cookies)
1884 (repeat :tag "Specify"
1885 (list (character :tag "Priority" :value ?A)
1886 (choice :tag "Face "
1887 (string :tag "Color")
1888 (sexp :tag "Face"))))))
1890 (defcustom org-agenda-day-face-function nil
1891 "Function called to determine what face should be used to display a day.
1892 The only argument passed to that function is the day. It should
1893 returns a face, or nil if does not want to specify a face and let
1894 the normal rules apply."
1895 :group 'org-agenda-line-format
1896 :version "24.1"
1897 :type 'function)
1899 (defcustom org-agenda-category-icon-alist nil
1900 "Alist of category icon to be displayed in agenda views.
1902 Each entry should have the following format:
1904 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1906 Where CATEGORY-REGEXP is a regexp matching the categories where
1907 the icon should be displayed.
1908 FILE-OR-DATA either a file path or a string containing image data.
1910 The other fields can be omitted safely if not needed:
1911 TYPE indicates the image type.
1912 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1913 image data.
1914 PROPS are additional image attributes to assign to the image,
1915 like, e.g. `:ascent center'.
1917 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1919 If you want to set the display properties yourself, just put a
1920 list as second element:
1922 (CATEGORY-REGEXP (MY PROPERTY LIST))
1924 For example, to display a 16px horizontal space for Emacs
1925 category, you can use:
1927 (\"Emacs\" '(space . (:width (16))))"
1928 :group 'org-agenda-line-format
1929 :version "24.1"
1930 :type '(alist :key-type (string :tag "Regexp matching category")
1931 :value-type (choice (list :tag "Icon"
1932 (string :tag "File or data")
1933 (symbol :tag "Type")
1934 (boolean :tag "Data?")
1935 (repeat :tag "Extra image properties" :inline t symbol))
1936 (list :tag "Display properties" sexp))))
1938 (defgroup org-agenda-column-view nil
1939 "Options concerning column view in the agenda."
1940 :tag "Org Agenda Column View"
1941 :group 'org-agenda)
1943 (defcustom org-agenda-columns-show-summaries t
1944 "Non-nil means show summaries for columns displayed in the agenda view."
1945 :group 'org-agenda-column-view
1946 :type 'boolean)
1948 (defcustom org-agenda-columns-compute-summary-properties t
1949 "Non-nil means recompute all summary properties before column view.
1950 When column view in the agenda is listing properties that have a summary
1951 operator, it can go to all relevant buffers and recompute the summaries
1952 there. This can mean overhead for the agenda column view, but is necessary
1953 to have thing up to date.
1954 As a special case, a CLOCKSUM property also makes sure that the clock
1955 computations are current."
1956 :group 'org-agenda-column-view
1957 :type 'boolean)
1959 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1960 "Non-nil means the duration of an appointment will add to day effort.
1961 The property to which appointment durations will be added is the one given
1962 in the option `org-effort-property'. If an appointment does not have
1963 an end time, `org-agenda-default-appointment-duration' will be used. If that
1964 is not set, an appointment without end time will not contribute to the time
1965 estimate."
1966 :group 'org-agenda-column-view
1967 :type 'boolean)
1969 (defcustom org-agenda-auto-exclude-function nil
1970 "A function called with a tag to decide if it is filtered on '/ RET'.
1971 The sole argument to the function, which is called once for each
1972 possible tag, is a string giving the name of the tag. The
1973 function should return either nil if the tag should be included
1974 as normal, or \"-<TAG>\" to exclude the tag.
1975 Note that for the purpose of tag filtering, only the lower-case version of
1976 all tags will be considered, so that this function will only ever see
1977 the lower-case version of all tags."
1978 :group 'org-agenda
1979 :type 'function)
1981 (defcustom org-agenda-bulk-custom-functions nil
1982 "Alist of characters and custom functions for bulk actions.
1983 For example, this value makes those two functions available:
1985 '((?R set-category)
1986 (?C bulk-cut))
1988 With selected entries in an agenda buffer, `B R' will call
1989 the custom function `set-category' on the selected entries.
1990 Note that functions in this alist don't need to be quoted."
1991 :type 'alist
1992 :version "24.1"
1993 :group 'org-agenda)
1995 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1996 "Execute BODY with point at location given by `org-hd-marker' property.
1997 If STRING is non-nil, the text property will be fetched from position 0
1998 in that string. If STRING is nil, it will be fetched from the beginning
1999 of the current line."
2000 (org-with-gensyms (marker)
2001 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2002 'org-hd-marker ,string)))
2003 (with-current-buffer (marker-buffer ,marker)
2004 (save-excursion
2005 (goto-char ,marker)
2006 ,@body)))))
2007 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2009 (defun org-add-agenda-custom-command (entry)
2010 "Replace or add a command in `org-agenda-custom-commands'.
2011 This is mostly for hacking and trying a new command - once the command
2012 works you probably want to add it to `org-agenda-custom-commands' for good."
2013 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2014 (if ass
2015 (setcdr ass (cdr entry))
2016 (push entry org-agenda-custom-commands))))
2018 ;;; Define the org-agenda-mode
2020 (defvar org-agenda-mode-map (make-sparse-keymap)
2021 "Keymap for `org-agenda-mode'.")
2022 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2024 (defvar org-agenda-menu) ; defined later in this file.
2025 (defvar org-agenda-restrict nil) ; defined later in this file.
2026 (defvar org-agenda-follow-mode nil)
2027 (defvar org-agenda-entry-text-mode nil)
2028 (defvar org-agenda-clockreport-mode nil)
2029 (defvar org-agenda-show-log nil)
2030 (defvar org-agenda-redo-command nil)
2031 (defvar org-agenda-query-string nil)
2032 (defvar org-agenda-mode-hook nil
2033 "Hook run after `org-agenda-mode' is turned on.
2034 The buffer is still writable when this hook is called.")
2035 (defvar org-agenda-type nil)
2036 (defvar org-agenda-force-single-file nil)
2037 (defvar org-agenda-bulk-marked-entries nil
2038 "List of markers that refer to marked entries in the agenda.")
2040 ;;; Multiple agenda buffers support
2042 (defcustom org-agenda-sticky nil
2043 "Non-nil means agenda q key will bury agenda buffers.
2044 Agenda commands will then show existing buffer instead of generating new ones.
2045 When nil, `q' will kill the single agenda buffer."
2046 :group 'org-agenda
2047 :version "24.3"
2048 :type 'boolean)
2051 ;;;###autoload
2052 (defun org-toggle-sticky-agenda (&optional arg)
2053 "Toggle `org-agenda-sticky'."
2054 (interactive "P")
2055 (let ((new-value (if arg
2056 (> (prefix-numeric-value arg) 0)
2057 (not org-agenda-sticky))))
2058 (if (equal new-value org-agenda-sticky)
2059 (and (org-called-interactively-p 'interactive)
2060 (message "Sticky agenda was already %s"
2061 (if org-agenda-sticky "enabled" "disabled")))
2062 (setq org-agenda-sticky new-value)
2063 (org-agenda-kill-all-agenda-buffers)
2064 (and (org-called-interactively-p 'interactive)
2065 (message "Sticky agenda was %s"
2066 (if org-agenda-sticky "enabled" "disabled"))))))
2068 (defvar org-agenda-buffer nil
2069 "Agenda buffer currently being generated.")
2071 (defvar org-agenda-last-prefix-arg nil)
2072 (defvar org-agenda-this-buffer-name nil)
2073 (defvar org-agenda-doing-sticky-redo nil)
2074 (defvar org-agenda-this-buffer-is-sticky nil)
2076 (defconst org-agenda-local-vars
2077 '(org-agenda-this-buffer-name
2078 org-agenda-undo-list
2079 org-agenda-pending-undo-list
2080 org-agenda-follow-mode
2081 org-agenda-entry-text-mode
2082 org-agenda-clockreport-mode
2083 org-agenda-show-log
2084 org-agenda-redo-command
2085 org-agenda-query-string
2086 org-agenda-type
2087 org-agenda-bulk-marked-entries
2088 org-agenda-undo-has-started-in
2089 org-agenda-info
2090 org-agenda-pre-window-conf
2091 org-agenda-columns-active
2092 org-agenda-tag-filter-overlays
2093 org-agenda-tag-filter
2094 org-agenda-cat-filter-overlays
2095 org-agenda-category-filter
2096 org-agenda-re-filter-overlays
2097 org-agenda-regexp-filter
2098 org-agenda-markers
2099 org-agenda-last-search-view-search-was-boolean
2100 org-agenda-filtered-by-category
2101 org-agenda-filter-form
2102 org-agenda-cycle-counter
2103 org-agenda-last-prefix-arg)
2104 "Variables that must be local in agenda buffers to allow multiple buffers.")
2106 (defun org-agenda-mode ()
2107 "Mode for time-sorted view on action items in Org-mode files.
2109 The following commands are available:
2111 \\{org-agenda-mode-map}"
2112 (interactive)
2113 (cond (org-agenda-doing-sticky-redo
2114 ;; Refreshing sticky agenda-buffer
2116 ;; Preserve the value of `org-agenda-local-vars' variables,
2117 ;; while letting `kill-all-local-variables' kill the rest
2118 (let ((save (buffer-local-variables)))
2119 (kill-all-local-variables)
2120 (mapc 'make-local-variable org-agenda-local-vars)
2121 (dolist (elem save)
2122 (let ((var (car elem))
2123 (val (cdr elem)))
2124 (when (and val
2125 (member var org-agenda-local-vars))
2126 (set var val)))))
2127 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2128 (org-agenda-sticky
2129 ;; Creating a sticky Agenda buffer for the first time
2130 (kill-all-local-variables)
2131 (mapc 'make-local-variable org-agenda-local-vars)
2132 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2134 ;; Creating a non-sticky agenda buffer
2135 (kill-all-local-variables)
2136 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2137 (setq org-agenda-undo-list nil
2138 org-agenda-pending-undo-list nil
2139 org-agenda-bulk-marked-entries nil)
2140 (setq major-mode 'org-agenda-mode)
2141 ;; Keep global-font-lock-mode from turning on font-lock-mode
2142 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2143 (setq mode-name "Org-Agenda")
2144 (use-local-map org-agenda-mode-map)
2145 (easy-menu-add org-agenda-menu)
2146 (if org-startup-truncated (setq truncate-lines t))
2147 (org-set-local 'line-move-visual nil)
2148 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2149 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2150 ;; Make sure properties are removed when copying text
2151 (make-local-variable 'filter-buffer-substring-functions)
2152 (add-hook 'filter-buffer-substring-functions
2153 (lambda (fun start end delete)
2154 (substring-no-properties (funcall fun start end delete))))
2155 (unless org-agenda-keep-modes
2156 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2157 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2158 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2159 org-agenda-show-log org-agenda-start-with-log-mode))
2161 (easy-menu-change
2162 '("Agenda") "Agenda Files"
2163 (append
2164 (list
2165 (vector
2166 (if (get 'org-agenda-files 'org-restrict)
2167 "Restricted to single file"
2168 "Edit File List")
2169 '(org-edit-agenda-file-list)
2170 (not (get 'org-agenda-files 'org-restrict)))
2171 "--")
2172 (mapcar 'org-file-menu-entry (org-agenda-files))))
2173 (org-agenda-set-mode-name)
2174 (apply
2175 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2176 (list 'org-agenda-mode-hook)))
2178 (substitute-key-definition 'undo 'org-agenda-undo
2179 org-agenda-mode-map global-map)
2180 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2181 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2182 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2183 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2184 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2185 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2186 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2187 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2188 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2189 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2190 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2191 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2192 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2193 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2194 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2195 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2196 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2197 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2198 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2199 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2200 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2201 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2202 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2203 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2204 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2205 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2206 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2207 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2208 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2209 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2210 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2211 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2212 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2213 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2214 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2215 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2216 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2217 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2218 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2219 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2220 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2221 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2222 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2223 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2224 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2225 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2227 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2228 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2229 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2230 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2231 (while l (org-defkey org-agenda-mode-map
2232 (int-to-string (pop l)) 'digit-argument)))
2234 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2235 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2236 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2237 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2238 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2239 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2240 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2241 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2242 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2243 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2244 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2245 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2246 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2247 'org-clock-modify-effort-estimate)
2248 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2249 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2250 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2251 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2252 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2253 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2254 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2255 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2256 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2257 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2258 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2259 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2260 (substitute-key-definition 'next-line 'org-agenda-next-line
2261 org-agenda-mode-map global-map)
2262 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2263 org-agenda-mode-map global-map)
2264 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2265 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2266 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2267 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2268 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2269 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2270 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2271 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2272 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2273 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2274 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2275 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2276 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2277 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2278 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2279 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2280 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2281 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2282 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2283 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2284 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2285 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2286 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2287 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2288 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2289 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2290 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2291 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2292 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2293 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2295 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2296 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2297 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2298 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2299 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2300 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2301 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2302 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2303 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2304 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2305 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2306 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2307 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2308 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2309 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2311 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2312 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2313 (when org-agenda-mouse-1-follows-link
2314 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2315 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2316 '("Agenda"
2317 ("Agenda Files")
2318 "--"
2319 ("Agenda Dates"
2320 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2321 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2322 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2323 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2324 "--"
2325 ("View"
2326 ["Day View" org-agenda-day-view
2327 :active (org-agenda-check-type nil 'agenda)
2328 :style radio :selected (eq org-agenda-current-span 'day)
2329 :keys "v d (or just d)"]
2330 ["Week View" org-agenda-week-view
2331 :active (org-agenda-check-type nil 'agenda)
2332 :style radio :selected (eq org-agenda-current-span 'week)
2333 :keys "v w (or just w)"]
2334 ["Month View" org-agenda-month-view
2335 :active (org-agenda-check-type nil 'agenda)
2336 :style radio :selected (eq org-agenda-current-span 'month)
2337 :keys "v m"]
2338 ["Year View" org-agenda-year-view
2339 :active (org-agenda-check-type nil 'agenda)
2340 :style radio :selected (eq org-agenda-current-span 'year)
2341 :keys "v y"]
2342 "--"
2343 ["Include Diary" org-agenda-toggle-diary
2344 :style toggle :selected org-agenda-include-diary
2345 :active (org-agenda-check-type nil 'agenda)]
2346 ["Include Deadlines" org-agenda-toggle-deadlines
2347 :style toggle :selected org-agenda-include-deadlines
2348 :active (org-agenda-check-type nil 'agenda)]
2349 ["Use Time Grid" org-agenda-toggle-time-grid
2350 :style toggle :selected org-agenda-use-time-grid
2351 :active (org-agenda-check-type nil 'agenda)]
2352 "--"
2353 ["Show clock report" org-agenda-clockreport-mode
2354 :style toggle :selected org-agenda-clockreport-mode
2355 :active (org-agenda-check-type nil 'agenda)]
2356 ["Show some entry text" org-agenda-entry-text-mode
2357 :style toggle :selected org-agenda-entry-text-mode
2358 :active t]
2359 "--"
2360 ["Show Logbook entries" org-agenda-log-mode
2361 :style toggle :selected org-agenda-show-log
2362 :active (org-agenda-check-type nil 'agenda 'timeline)
2363 :keys "v l (or just l)"]
2364 ["Include archived trees" org-agenda-archives-mode
2365 :style toggle :selected org-agenda-archives-mode :active t
2366 :keys "v a"]
2367 ["Include archive files" (org-agenda-archives-mode t)
2368 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2369 :keys "v A"]
2370 "--"
2371 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2372 ["Write view to file" org-agenda-write t]
2373 ["Rebuild buffer" org-agenda-redo t]
2374 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2375 "--"
2376 ["Show original entry" org-agenda-show t]
2377 ["Go To (other window)" org-agenda-goto t]
2378 ["Go To (this window)" org-agenda-switch-to t]
2379 ["Capture with cursor date" org-agenda-capture t]
2380 ["Follow Mode" org-agenda-follow-mode
2381 :style toggle :selected org-agenda-follow-mode :active t]
2382 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2383 "--"
2384 ("TODO"
2385 ["Cycle TODO" org-agenda-todo t]
2386 ["Next TODO set" org-agenda-todo-nextset t]
2387 ["Previous TODO set" org-agenda-todo-previousset t]
2388 ["Add note" org-agenda-add-note t])
2389 ("Archive/Refile/Delete"
2390 ["Archive default" org-agenda-archive-default t]
2391 ["Archive default" org-agenda-archive-default-with-confirmation t]
2392 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2393 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2394 ["Archive subtree" org-agenda-archive t]
2395 "--"
2396 ["Refile" org-agenda-refile t]
2397 "--"
2398 ["Delete subtree" org-agenda-kill t])
2399 ("Bulk action"
2400 ["Mark entry" org-agenda-bulk-mark t]
2401 ["Mark all" org-agenda-bulk-mark-all t]
2402 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2403 ["Unmark entry" org-agenda-bulk-unmark t]
2404 ["Unmark all entries" org-agenda-bulk-unmark-all :active t :keys "U"])
2405 ["Act on all marked" org-agenda-bulk-action t]
2406 "--"
2407 ("Tags and Properties"
2408 ["Show all Tags" org-agenda-show-tags t]
2409 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2410 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2411 "--"
2412 ["Column View" org-columns t])
2413 ("Deadline/Schedule"
2414 ["Schedule" org-agenda-schedule t]
2415 ["Set Deadline" org-agenda-deadline t]
2416 "--"
2417 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2418 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2419 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2420 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2421 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2422 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2423 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2424 ("Clock and Effort"
2425 ["Clock in" org-agenda-clock-in t]
2426 ["Clock out" org-agenda-clock-out t]
2427 ["Clock cancel" org-agenda-clock-cancel t]
2428 ["Goto running clock" org-clock-goto t]
2429 "--"
2430 ["Set Effort" org-agenda-set-effort t]
2431 ["Change clocked effort" org-clock-modify-effort-estimate
2432 (org-clock-is-active)])
2433 ("Priority"
2434 ["Set Priority" org-agenda-priority t]
2435 ["Increase Priority" org-agenda-priority-up t]
2436 ["Decrease Priority" org-agenda-priority-down t]
2437 ["Show Priority" org-show-priority t])
2438 ("Calendar/Diary"
2439 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2440 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2441 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2442 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2443 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2444 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2445 "--"
2446 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2447 "--"
2448 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2449 "--"
2450 ("MobileOrg"
2451 ["Push Files and Views" org-mobile-push t]
2452 ["Get Captured and Flagged" org-mobile-pull t]
2453 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2454 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2455 "--"
2456 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2457 "--"
2458 ["Quit" org-agenda-quit t]
2459 ["Exit and Release Buffers" org-agenda-exit t]
2462 ;;; Agenda undo
2464 (defvar org-agenda-allow-remote-undo t
2465 "Non-nil means allow remote undo from the agenda buffer.")
2466 (defvar org-agenda-undo-has-started-in nil
2467 "Buffers that have already seen `undo-start' in the current undo sequence.")
2469 (defun org-agenda-undo ()
2470 "Undo a remote editing step in the agenda.
2471 This undoes changes both in the agenda buffer and in the remote buffer
2472 that have been changed along."
2473 (interactive)
2474 (or org-agenda-allow-remote-undo
2475 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2476 (if (not (eq this-command last-command))
2477 (setq org-agenda-undo-has-started-in nil
2478 org-agenda-pending-undo-list org-agenda-undo-list))
2479 (if (not org-agenda-pending-undo-list)
2480 (user-error "No further undo information"))
2481 (let* ((entry (pop org-agenda-pending-undo-list))
2482 buf line cmd rembuf)
2483 (setq cmd (pop entry) line (pop entry))
2484 (setq rembuf (nth 2 entry))
2485 (org-with-remote-undo rembuf
2486 (while (bufferp (setq buf (pop entry)))
2487 (if (pop entry)
2488 (with-current-buffer buf
2489 (let ((last-undo-buffer buf)
2490 (inhibit-read-only t))
2491 (unless (memq buf org-agenda-undo-has-started-in)
2492 (push buf org-agenda-undo-has-started-in)
2493 (make-local-variable 'pending-undo-list)
2494 (undo-start))
2495 (while (and pending-undo-list
2496 (listp pending-undo-list)
2497 (not (car pending-undo-list)))
2498 (pop pending-undo-list))
2499 (undo-more 1))))))
2500 (org-goto-line line)
2501 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2503 (defun org-verify-change-for-undo (l1 l2)
2504 "Verify that a real change occurred between the undo lists L1 and L2."
2505 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2506 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2507 (not (eq l1 l2)))
2509 ;;; Agenda dispatch
2511 (defvar org-agenda-restrict-begin (make-marker))
2512 (defvar org-agenda-restrict-end (make-marker))
2513 (defvar org-agenda-last-dispatch-buffer nil)
2514 (defvar org-agenda-overriding-restriction nil)
2516 (defcustom org-agenda-custom-commands-contexts nil
2517 "Alist of custom agenda keys and contextual rules.
2519 For example, if you have a custom agenda command \"p\" and you
2520 want this command to be accessible only from plain text files,
2521 use this:
2523 '((\"p\" ((in-file . \"\\.txt\"))))
2525 Here are the available contexts definitions:
2527 in-file: command displayed only in matching files
2528 in-mode: command displayed only in matching modes
2529 not-in-file: command not displayed in matching files
2530 not-in-mode: command not displayed in matching modes
2531 in-buffer: command displayed only in matching buffers
2532 not-in-buffer: command not displayed in matching buffers
2533 [function]: a custom function taking no argument
2535 If you define several checks, the agenda command will be
2536 accessible if there is at least one valid check.
2538 You can also bind a key to another agenda custom command
2539 depending on contextual rules.
2541 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2543 Here it means: in .txt files, use \"p\" as the key for the
2544 agenda command otherwise associated with \"q\". (The command
2545 originally associated with \"q\" is not displayed to avoid
2546 duplicates.)"
2547 :version "24.3"
2548 :group 'org-agenda-custom-commands
2549 :type '(repeat (list :tag "Rule"
2550 (string :tag " Agenda key")
2551 (string :tag "Replace by command")
2552 (repeat :tag "Available when"
2553 (choice
2554 (cons :tag "Condition"
2555 (choice
2556 (const :tag "In file" in-file)
2557 (const :tag "Not in file" not-in-file)
2558 (const :tag "In buffer" in-buffer)
2559 (const :tag "Not in buffer" not-in-buffer)
2560 (const :tag "In mode" in-mode)
2561 (const :tag "Not in mode" not-in-mode))
2562 (regexp))
2563 (function :tag "Custom function"))))))
2565 (defcustom org-agenda-max-entries nil
2566 "Maximum number of entries to display in an agenda.
2567 This can be nil (no limit) or an integer or an alist of agenda
2568 types with an associated number of entries to display in this
2569 type."
2570 :version "24.4"
2571 :package-version '(Org . "8.0")
2572 :group 'org-agenda-custom-commands
2573 :type '(choice (symbol :tag "No limit" nil)
2574 (integer :tag "Max number of entries")
2575 (repeat
2576 (cons (choice :tag "Agenda type"
2577 (const agenda)
2578 (const todo)
2579 (const tags)
2580 (const search)
2581 (const timeline))
2582 (integer :tag "Max number of entries")))))
2584 (defcustom org-agenda-max-todos nil
2585 "Maximum number of TODOs to display in an agenda.
2586 This can be nil (no limit) or an integer or an alist of agenda
2587 types with an associated number of entries to display in this
2588 type."
2589 :version "24.4"
2590 :package-version '(Org . "8.0")
2591 :group 'org-agenda-custom-commands
2592 :type '(choice (symbol :tag "No limit" nil)
2593 (integer :tag "Max number of entries")
2594 (repeat
2595 (cons (choice :tag "Agenda type"
2596 (const agenda)
2597 (const todo)
2598 (const tags)
2599 (const search)
2600 (const timeline))
2601 (integer :tag "Max number of entries")))))
2603 (defcustom org-agenda-max-tags nil
2604 "Maximum number of tagged entries to display in an agenda.
2605 This can be nil (no limit) or an integer or an alist of agenda
2606 types with an associated number of entries to display in this
2607 type."
2608 :version "24.4"
2609 :package-version '(Org . "8.0")
2610 :group 'org-agenda-custom-commands
2611 :type '(choice (symbol :tag "No limit" nil)
2612 (integer :tag "Max number of entries")
2613 (repeat
2614 (cons (choice :tag "Agenda type"
2615 (const agenda)
2616 (const todo)
2617 (const tags)
2618 (const search)
2619 (const timeline))
2620 (integer :tag "Max number of entries")))))
2622 (defcustom org-agenda-max-effort nil
2623 "Maximum cumulated effort duration for the agenda.
2624 This can be nil (no limit) or a number of minutes (as an integer)
2625 or an alist of agenda types with an associated number of minutes
2626 to limit entries to in this type."
2627 :version "24.4"
2628 :package-version '(Org . "8.0")
2629 :group 'org-agenda-custom-commands
2630 :type '(choice (symbol :tag "No limit" nil)
2631 (integer :tag "Max number of entries")
2632 (repeat
2633 (cons (choice :tag "Agenda type"
2634 (const agenda)
2635 (const todo)
2636 (const tags)
2637 (const search)
2638 (const timeline))
2639 (integer :tag "Max number of entries")))))
2641 (defvar org-keys nil)
2642 (defvar org-match nil)
2643 ;;;###autoload
2644 (defun org-agenda (&optional arg org-keys restriction)
2645 "Dispatch agenda commands to collect entries to the agenda buffer.
2646 Prompts for a command to execute. Any prefix arg will be passed
2647 on to the selected command. The default selections are:
2649 a Call `org-agenda-list' to display the agenda for current day or week.
2650 t Call `org-todo-list' to display the global todo list.
2651 T Call `org-todo-list' to display the global todo list, select only
2652 entries with a specific TODO keyword (the user gets a prompt).
2653 m Call `org-tags-view' to display headlines with tags matching
2654 a condition (the user is prompted for the condition).
2655 M Like `m', but select only TODO entries, no ordinary headlines.
2656 L Create a timeline for the current buffer.
2657 e Export views to associated files.
2658 s Search entries for keywords.
2659 S Search entries for keywords, only with TODO keywords.
2660 / Multi occur across all agenda files and also files listed
2661 in `org-agenda-text-search-extra-files'.
2662 < Restrict agenda commands to buffer, subtree, or region.
2663 Press several times to get the desired effect.
2664 > Remove a previous restriction.
2665 # List \"stuck\" projects.
2666 ! Configure what \"stuck\" means.
2667 C Configure custom agenda commands.
2669 More commands can be added by configuring the variable
2670 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2671 searches can be pre-defined in this way.
2673 If the current buffer is in Org-mode and visiting a file, you can also
2674 first press `<' once to indicate that the agenda should be temporarily
2675 \(until the next use of \\[org-agenda]) restricted to the current file.
2676 Pressing `<' twice means to restrict to the current subtree or region
2677 \(if active)."
2678 (interactive "P")
2679 (catch 'exit
2680 (let* ((prefix-descriptions nil)
2681 (org-agenda-buffer-name org-agenda-buffer-name)
2682 (org-agenda-window-setup (if (equal (buffer-name)
2683 org-agenda-buffer-name)
2684 'current-window
2685 org-agenda-window-setup))
2686 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2687 (org-agenda-custom-commands
2688 ;; normalize different versions
2689 (delq nil
2690 (mapcar
2691 (lambda (x)
2692 (cond ((stringp (cdr x))
2693 (push x prefix-descriptions)
2694 nil)
2695 ((stringp (nth 1 x)) x)
2696 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2697 (t (cons (car x) (cons "" (cdr x))))))
2698 org-agenda-custom-commands)))
2699 (org-agenda-custom-commands
2700 (org-contextualize-keys
2701 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2702 (buf (current-buffer))
2703 (bfn (buffer-file-name (buffer-base-buffer)))
2704 entry key type org-match lprops ans)
2705 ;; Turn off restriction unless there is an overriding one,
2706 (unless org-agenda-overriding-restriction
2707 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2708 ;; There is a request to keep the file list in place
2709 (put 'org-agenda-files 'org-restrict nil))
2710 (setq org-agenda-restrict nil)
2711 (move-marker org-agenda-restrict-begin nil)
2712 (move-marker org-agenda-restrict-end nil))
2713 ;; Delete old local properties
2714 (put 'org-agenda-redo-command 'org-lprops nil)
2715 ;; Delete previously set last-arguments
2716 (put 'org-agenda-redo-command 'last-args nil)
2717 ;; Remember where this call originated
2718 (setq org-agenda-last-dispatch-buffer (current-buffer))
2719 (unless org-keys
2720 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2721 org-keys (car ans)
2722 restriction (cdr ans)))
2723 ;; If we have sticky agenda buffers, set a name for the buffer,
2724 ;; depending on the invoking keys. The user may still set this
2725 ;; as a command option, which will overwrite what we do here.
2726 (if org-agenda-sticky
2727 (setq org-agenda-buffer-name
2728 (format "*Org Agenda(%s)*" org-keys)))
2729 ;; Establish the restriction, if any
2730 (when (and (not org-agenda-overriding-restriction) restriction)
2731 (put 'org-agenda-files 'org-restrict (list bfn))
2732 (cond
2733 ((eq restriction 'region)
2734 (setq org-agenda-restrict t)
2735 (move-marker org-agenda-restrict-begin (region-beginning))
2736 (move-marker org-agenda-restrict-end (region-end)))
2737 ((eq restriction 'subtree)
2738 (save-excursion
2739 (setq org-agenda-restrict t)
2740 (org-back-to-heading t)
2741 (move-marker org-agenda-restrict-begin (point))
2742 (move-marker org-agenda-restrict-end
2743 (progn (org-end-of-subtree t)))))))
2745 ;; For example the todo list should not need it (but does...)
2746 (cond
2747 ((setq entry (assoc org-keys org-agenda-custom-commands))
2748 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2749 (progn
2750 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2751 lprops (nth 4 entry))
2752 (if org-agenda-sticky
2753 (setq org-agenda-buffer-name
2754 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2755 (format "*Org Agenda(%s)*" org-keys))))
2756 (put 'org-agenda-redo-command 'org-lprops lprops)
2757 (cond
2758 ((eq type 'agenda)
2759 (org-let lprops '(org-agenda-list current-prefix-arg)))
2760 ((eq type 'agenda*)
2761 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2762 ((eq type 'alltodo)
2763 (org-let lprops '(org-todo-list current-prefix-arg)))
2764 ((eq type 'search)
2765 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2766 ((eq type 'stuck)
2767 (org-let lprops '(org-agenda-list-stuck-projects
2768 current-prefix-arg)))
2769 ((eq type 'tags)
2770 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2771 ((eq type 'tags-todo)
2772 (org-let lprops '(org-tags-view '(4) org-match)))
2773 ((eq type 'todo)
2774 (org-let lprops '(org-todo-list org-match)))
2775 ((eq type 'tags-tree)
2776 (org-check-for-org-mode)
2777 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2778 ((eq type 'todo-tree)
2779 (org-check-for-org-mode)
2780 (org-let lprops
2781 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2782 (regexp-quote org-match) "\\>"))))
2783 ((eq type 'occur-tree)
2784 (org-check-for-org-mode)
2785 (org-let lprops '(org-occur org-match)))
2786 ((functionp type)
2787 (org-let lprops '(funcall type org-match)))
2788 ((fboundp type)
2789 (org-let lprops '(funcall type org-match)))
2790 (t (user-error "Invalid custom agenda command type %s" type))))
2791 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2792 ((equal org-keys "C")
2793 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2794 (customize-variable 'org-agenda-custom-commands))
2795 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2796 ((equal org-keys "s") (call-interactively 'org-search-view))
2797 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2798 ((equal org-keys "t") (call-interactively 'org-todo-list))
2799 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2800 ((equal org-keys "m") (call-interactively 'org-tags-view))
2801 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2802 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2803 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2804 (org-add-hook
2805 'post-command-hook
2806 (lambda ()
2807 (unless (current-message)
2808 (let* ((m (org-agenda-get-any-marker))
2809 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2810 (when note
2811 (message (concat
2812 "FLAGGING-NOTE ([?] for more info): "
2813 (org-add-props
2814 (replace-regexp-in-string
2815 "\\\\n" "//"
2816 (copy-sequence note))
2817 nil 'face 'org-warning)))))))
2818 t t))
2819 ((equal org-keys "L")
2820 (unless (derived-mode-p 'org-mode)
2821 (user-error "This is not an Org-mode file"))
2822 (unless restriction
2823 (put 'org-agenda-files 'org-restrict (list bfn))
2824 (org-call-with-arg 'org-timeline arg)))
2825 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2826 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2827 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2828 (t (user-error "Invalid agenda key"))))))
2830 (defun org-agenda-append-agenda ()
2831 "Append another agenda view to the current one.
2832 This function allows interactive building of block agendas.
2833 Agenda views are separated by `org-agenda-block-separator'."
2834 (interactive)
2835 (unless (derived-mode-p 'org-agenda-mode)
2836 (user-error "Can only append from within agenda buffer"))
2837 (let ((org-agenda-multi t))
2838 (org-agenda)
2839 (widen)
2840 (org-agenda-finalize)
2841 (setq buffer-read-only t)
2842 (org-agenda-fit-window-to-buffer)))
2844 (defun org-agenda-normalize-custom-commands (cmds)
2845 "Normalize custom commands CMDS."
2846 (delq nil
2847 (mapcar
2848 (lambda (x)
2849 (cond ((stringp (cdr x)) nil)
2850 ((stringp (nth 1 x)) x)
2851 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2852 (t (cons (car x) (cons "" (cdr x))))))
2853 cmds)))
2855 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2856 "The user interface for selecting an agenda command."
2857 (catch 'exit
2858 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2859 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2860 (region-p (org-region-active-p))
2861 (custom org-agenda-custom-commands)
2862 (selstring "")
2863 restriction second-time
2864 c entry key type match prefixes rmheader header-end custom1 desc
2865 line lines left right n n1)
2866 (save-window-excursion
2867 (delete-other-windows)
2868 (org-switch-to-buffer-other-window " *Agenda Commands*")
2869 (erase-buffer)
2870 (insert (eval-when-compile
2871 (let ((header
2872 "Press key for an agenda command: < Buffer, subtree/region restriction
2873 -------------------------------- > Remove restriction
2874 a Agenda for current week or day e Export agenda views
2875 t List of all TODO entries T Entries with special TODO kwd
2876 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2877 s Search for keywords S Like s, but only TODO entries
2878 L Timeline for current buffer # List stuck projects (!=configure)
2879 / Multi-occur C Configure custom agenda commands
2880 ? Find :FLAGGED: entries * Toggle sticky agenda views
2882 (start 0))
2883 (while (string-match
2884 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2885 header start)
2886 (setq start (match-end 0))
2887 (add-text-properties (match-beginning 2) (match-end 2)
2888 '(face bold) header))
2889 header)))
2890 (setq header-end (point-marker))
2891 (while t
2892 (setq custom1 custom)
2893 (when (eq rmheader t)
2894 (org-goto-line 1)
2895 (re-search-forward ":" nil t)
2896 (delete-region (match-end 0) (point-at-eol))
2897 (forward-char 1)
2898 (looking-at "-+")
2899 (delete-region (match-end 0) (point-at-eol))
2900 (move-marker header-end (match-end 0)))
2901 (goto-char header-end)
2902 (delete-region (point) (point-max))
2904 ;; Produce all the lines that describe custom commands and prefixes
2905 (setq lines nil)
2906 (while (setq entry (pop custom1))
2907 (setq key (car entry) desc (nth 1 entry)
2908 type (nth 2 entry)
2909 match (nth 3 entry))
2910 (if (> (length key) 1)
2911 (add-to-list 'prefixes (string-to-char key))
2912 (setq line
2913 (format
2914 "%-4s%-14s"
2915 (org-add-props (copy-sequence key)
2916 '(face bold))
2917 (cond
2918 ((string-match "\\S-" desc) desc)
2919 ((eq type 'agenda) "Agenda for current week or day")
2920 ((eq type 'agenda*) "Appointments for current week or day")
2921 ((eq type 'alltodo) "List of all TODO entries")
2922 ((eq type 'search) "Word search")
2923 ((eq type 'stuck) "List of stuck projects")
2924 ((eq type 'todo) "TODO keyword")
2925 ((eq type 'tags) "Tags query")
2926 ((eq type 'tags-todo) "Tags (TODO)")
2927 ((eq type 'tags-tree) "Tags tree")
2928 ((eq type 'todo-tree) "TODO kwd tree")
2929 ((eq type 'occur-tree) "Occur tree")
2930 ((functionp type) (if (symbolp type)
2931 (symbol-name type)
2932 "Lambda expression"))
2933 (t "???"))))
2934 (if org-agenda-menu-show-matcher
2935 (setq line
2936 (concat line ": "
2937 (cond
2938 ((stringp match)
2939 (setq match (copy-sequence match))
2940 (org-add-props match nil 'face 'org-warning))
2941 ((listp type)
2942 (format "set of %d commands" (length type))))))
2943 (if (org-string-nw-p match)
2944 (add-text-properties
2945 0 (length line) (list 'help-echo
2946 (concat "Matcher: " match)) line)))
2947 (push line lines)))
2948 (setq lines (nreverse lines))
2949 (when prefixes
2950 (mapc (lambda (x)
2951 (push
2952 (format "%s %s"
2953 (org-add-props (char-to-string x)
2954 nil 'face 'bold)
2955 (or (cdr (assoc (concat selstring
2956 (char-to-string x))
2957 prefix-descriptions))
2958 "Prefix key"))
2959 lines))
2960 prefixes))
2962 ;; Check if we should display in two columns
2963 (if org-agenda-menu-two-columns
2964 (progn
2965 (setq n (length lines)
2966 n1 (+ (/ n 2) (mod n 2))
2967 right (nthcdr n1 lines)
2968 left (copy-sequence lines))
2969 (setcdr (nthcdr (1- n1) left) nil))
2970 (setq left lines right nil))
2971 (while left
2972 (insert "\n" (pop left))
2973 (when right
2974 (if (< (current-column) 40)
2975 (move-to-column 40 t)
2976 (insert " "))
2977 (insert (pop right))))
2979 ;; Make the window the right size
2980 (goto-char (point-min))
2981 (if second-time
2982 (if (not (pos-visible-in-window-p (point-max)))
2983 (org-fit-window-to-buffer))
2984 (setq second-time t)
2985 (org-fit-window-to-buffer))
2987 ;; Ask for selection
2988 (message "Press key for agenda command%s:"
2989 (if (or restrict-ok org-agenda-overriding-restriction)
2990 (if org-agenda-overriding-restriction
2991 " (restriction lock active)"
2992 (if restriction
2993 (format " (restricted to %s)" restriction)
2994 " (unrestricted)"))
2995 ""))
2996 (setq c (read-char-exclusive))
2997 (message "")
2998 (cond
2999 ((assoc (char-to-string c) custom)
3000 (setq selstring (concat selstring (char-to-string c)))
3001 (throw 'exit (cons selstring restriction)))
3002 ((memq c prefixes)
3003 (setq selstring (concat selstring (char-to-string c))
3004 prefixes nil
3005 rmheader (or rmheader t)
3006 custom (delq nil (mapcar
3007 (lambda (x)
3008 (if (or (= (length (car x)) 1)
3009 (/= (string-to-char (car x)) c))
3011 (cons (substring (car x) 1) (cdr x))))
3012 custom))))
3013 ((eq c ?*)
3014 (call-interactively 'org-toggle-sticky-agenda)
3015 (sit-for 2))
3016 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3017 (message "Restriction is only possible in Org-mode buffers")
3018 (ding) (sit-for 1))
3019 ((eq c ?1)
3020 (org-agenda-remove-restriction-lock 'noupdate)
3021 (setq restriction 'buffer))
3022 ((eq c ?0)
3023 (org-agenda-remove-restriction-lock 'noupdate)
3024 (setq restriction (if region-p 'region 'subtree)))
3025 ((eq c ?<)
3026 (org-agenda-remove-restriction-lock 'noupdate)
3027 (setq restriction
3028 (cond
3029 ((eq restriction 'buffer)
3030 (if region-p 'region 'subtree))
3031 ((memq restriction '(subtree region))
3032 nil)
3033 (t 'buffer))))
3034 ((eq c ?>)
3035 (org-agenda-remove-restriction-lock 'noupdate)
3036 (setq restriction nil))
3037 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3038 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3039 ((and (> (length selstring) 0) (eq c ?\d))
3040 (delete-window)
3041 (org-agenda-get-restriction-and-command prefix-descriptions))
3043 ((equal c ?q) (error "Abort"))
3044 (t (user-error "Invalid key %c" c))))))))
3046 (defun org-agenda-fit-window-to-buffer ()
3047 "Fit the window to the buffer size."
3048 (and (memq org-agenda-window-setup '(reorganize-frame))
3049 (fboundp 'fit-window-to-buffer)
3050 (org-fit-window-to-buffer
3052 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3053 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3055 (defvar org-cmd nil)
3056 (defvar org-agenda-overriding-cmd nil)
3057 (defvar org-agenda-overriding-arguments nil)
3058 (defvar org-agenda-overriding-cmd-arguments nil)
3059 (defun org-agenda-run-series (name series)
3060 "Run agenda NAME as a SERIES of agenda commands."
3061 (org-let (nth 1 series) '(org-agenda-prepare name))
3062 ;; We need to reset agenda markers here, because when constructing a
3063 ;; block agenda, the individual blocks do not do that.
3064 (org-agenda-reset-markers)
3065 (let* ((org-agenda-multi t)
3066 (redo (list 'org-agenda-run-series name (list 'quote series)))
3067 (cmds (car series))
3068 (gprops (nth 1 series))
3069 match ;; The byte compiler incorrectly complains about this. Keep it!
3070 org-cmd type lprops)
3071 (while (setq org-cmd (pop cmds))
3072 (setq type (car org-cmd)
3073 match (eval (nth 1 org-cmd))
3074 lprops (nth 2 org-cmd))
3075 (let ((org-agenda-overriding-arguments
3076 (if (eq org-agenda-overriding-cmd org-cmd)
3077 (or org-agenda-overriding-arguments
3078 org-agenda-overriding-cmd-arguments))))
3079 (cond
3080 ((eq type 'agenda)
3081 (org-let2 gprops lprops
3082 '(call-interactively 'org-agenda-list)))
3083 ((eq type 'agenda*)
3084 (org-let2 gprops lprops
3085 '(funcall 'org-agenda-list nil nil t)))
3086 ((eq type 'alltodo)
3087 (org-let2 gprops lprops
3088 '(call-interactively 'org-todo-list)))
3089 ((eq type 'search)
3090 (org-let2 gprops lprops
3091 '(org-search-view current-prefix-arg match nil)))
3092 ((eq type 'stuck)
3093 (org-let2 gprops lprops
3094 '(call-interactively 'org-agenda-list-stuck-projects)))
3095 ((eq type 'tags)
3096 (org-let2 gprops lprops
3097 '(org-tags-view current-prefix-arg match)))
3098 ((eq type 'tags-todo)
3099 (org-let2 gprops lprops
3100 '(org-tags-view '(4) match)))
3101 ((eq type 'todo)
3102 (org-let2 gprops lprops
3103 '(org-todo-list match)))
3104 ((fboundp type)
3105 (org-let2 gprops lprops
3106 '(funcall type match)))
3107 (t (error "Invalid type in command series")))))
3108 (widen)
3109 (let ((inhibit-read-only t))
3110 (add-text-properties (point-min) (point-max)
3111 `(org-series t org-series-redo-cmd ,redo)))
3112 (setq org-agenda-redo-command redo)
3113 (goto-char (point-min)))
3114 (org-agenda-fit-window-to-buffer)
3115 (org-let (nth 1 series) '(org-agenda-finalize)))
3117 ;;;###autoload
3118 (defmacro org-batch-agenda (cmd-key &rest parameters)
3119 "Run an agenda command in batch mode and send the result to STDOUT.
3120 If CMD-KEY is a string of length 1, it is used as a key in
3121 `org-agenda-custom-commands' and triggers this command. If it is a
3122 longer string it is used as a tags/todo match string.
3123 Parameters are alternating variable names and values that will be bound
3124 before running the agenda command."
3125 (org-eval-in-environment (org-make-parameter-alist parameters)
3126 (if (> (length cmd-key) 2)
3127 (org-tags-view nil cmd-key)
3128 (org-agenda nil cmd-key)))
3129 (set-buffer org-agenda-buffer-name)
3130 (princ (buffer-string)))
3132 (defvar org-agenda-info nil)
3134 ;;;###autoload
3135 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3136 "Run an agenda command in batch mode and send the result to STDOUT.
3137 If CMD-KEY is a string of length 1, it is used as a key in
3138 `org-agenda-custom-commands' and triggers this command. If it is a
3139 longer string it is used as a tags/todo match string.
3140 Parameters are alternating variable names and values that will be bound
3141 before running the agenda command.
3143 The output gives a line for each selected agenda item. Each
3144 item is a list of comma-separated values, like this:
3146 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3148 category The category of the item
3149 head The headline, without TODO kwd, TAGS and PRIORITY
3150 type The type of the agenda entry, can be
3151 todo selected in TODO match
3152 tagsmatch selected in tags match
3153 diary imported from diary
3154 deadline a deadline on given date
3155 scheduled scheduled on given date
3156 timestamp entry has timestamp on given date
3157 closed entry was closed on given date
3158 upcoming-deadline warning about deadline
3159 past-scheduled forwarded scheduled item
3160 block entry has date block including g. date
3161 todo The todo keyword, if any
3162 tags All tags including inherited ones, separated by colons
3163 date The relevant date, like 2007-2-14
3164 time The time, like 15:00-16:50
3165 extra Sting with extra planning info
3166 priority-l The priority letter if any was given
3167 priority-n The computed numerical priority
3168 agenda-day The day in the agenda where this is listed"
3169 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3170 (org-make-parameter-alist parameters))
3171 (if (> (length cmd-key) 2)
3172 (org-tags-view nil cmd-key)
3173 (org-agenda nil cmd-key)))
3174 (set-buffer org-agenda-buffer-name)
3175 (let* ((lines (org-split-string (buffer-string) "\n"))
3176 line)
3177 (while (setq line (pop lines))
3178 (catch 'next
3179 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3180 (setq org-agenda-info
3181 (org-fix-agenda-info (text-properties-at 0 line)))
3182 (princ
3183 (mapconcat 'org-agenda-export-csv-mapper
3184 '(org-category txt type todo tags date time extra
3185 priority-letter priority agenda-day)
3186 ","))
3187 (princ "\n")))))
3189 (defun org-fix-agenda-info (props)
3190 "Make sure all properties on an agenda item have a canonical form.
3191 This ensures the export commands can easily use it."
3192 (let (tmp re)
3193 (when (setq tmp (plist-get props 'tags))
3194 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3195 (when (setq tmp (plist-get props 'date))
3196 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3197 (let ((calendar-date-display-form '(year "-" month "-" day)))
3198 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3200 (setq tmp (calendar-date-string tmp)))
3201 (setq props (plist-put props 'date tmp)))
3202 (when (setq tmp (plist-get props 'day))
3203 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3204 (let ((calendar-date-display-form '(year "-" month "-" day)))
3205 (setq tmp (calendar-date-string tmp)))
3206 (setq props (plist-put props 'day tmp))
3207 (setq props (plist-put props 'agenda-day tmp)))
3208 (when (setq tmp (plist-get props 'txt))
3209 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3210 (plist-put props 'priority-letter (match-string 1 tmp))
3211 (setq tmp (replace-match "" t t tmp)))
3212 (when (and (setq re (plist-get props 'org-todo-regexp))
3213 (setq re (concat "\\`\\.*" re " ?"))
3214 (string-match re tmp))
3215 (plist-put props 'todo (match-string 1 tmp))
3216 (setq tmp (replace-match "" t t tmp)))
3217 (plist-put props 'txt tmp)))
3218 props)
3220 (defun org-agenda-export-csv-mapper (prop)
3221 (let ((res (plist-get org-agenda-info prop)))
3222 (setq res
3223 (cond
3224 ((not res) "")
3225 ((stringp res) res)
3226 (t (prin1-to-string res))))
3227 (while (string-match "," res)
3228 (setq res (replace-match ";" t t res)))
3229 (org-trim res)))
3231 ;;;###autoload
3232 (defun org-store-agenda-views (&rest parameters)
3233 "Store agenda views."
3234 (interactive)
3235 (eval (list 'org-batch-store-agenda-views)))
3237 ;;;###autoload
3238 (defmacro org-batch-store-agenda-views (&rest parameters)
3239 "Run all custom agenda commands that have a file argument."
3240 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3241 (pop-up-frames nil)
3242 (dir default-directory)
3243 (pars (org-make-parameter-alist parameters))
3244 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3245 (save-window-excursion
3246 (while cmds
3247 (setq cmd (pop cmds)
3248 thiscmdkey (car cmd)
3249 thiscmdcmd (cdr cmd)
3250 match (nth 2 thiscmdcmd)
3251 bufname (if org-agenda-sticky
3252 (or (and (stringp match)
3253 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3254 (format "*Org Agenda(%s)*" thiscmdkey))
3255 org-agenda-buffer-name)
3256 cmd-or-set (nth 2 cmd)
3257 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3258 files (nth (if (listp cmd-or-set) 4 5) cmd))
3259 (if (stringp files) (setq files (list files)))
3260 (when files
3261 (org-eval-in-environment (append org-agenda-exporter-settings
3262 opts pars)
3263 (org-agenda nil thiscmdkey))
3264 (set-buffer bufname)
3265 (while files
3266 (org-eval-in-environment (append org-agenda-exporter-settings
3267 opts pars)
3268 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3269 (and (get-buffer bufname)
3270 (kill-buffer bufname)))))))
3272 (defvar org-agenda-current-span nil
3273 "The current span used in the agenda view.") ; local variable in the agenda buffer
3274 (defun org-agenda-mark-header-line (pos)
3275 "Mark the line at POS as an agenda structure header."
3276 (save-excursion
3277 (goto-char pos)
3278 (put-text-property (point-at-bol) (point-at-eol)
3279 'org-agenda-structural-header t)
3280 (when org-agenda-title-append
3281 (put-text-property (point-at-bol) (point-at-eol)
3282 'org-agenda-title-append org-agenda-title-append))))
3284 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3285 (defvar org-agenda-write-buffer-name "Agenda View")
3286 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3287 "Write the current buffer (an agenda view) as a file.
3288 Depending on the extension of the file name, plain text (.txt),
3289 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3290 If the extension is .ics, run icalendar export over all files used
3291 to construct the agenda and limit the export to entries listed in the
3292 agenda now.
3293 If the extension is .org, collect all subtrees corresponding to the
3294 agenda entries and add them in an .org file.
3295 With prefix argument OPEN, open the new file immediately.
3296 If NOSETTINGS is given, do not scope the settings of
3297 `org-agenda-exporter-settings' into the export commands. This is used when
3298 the settings have already been scoped and we do not wish to overrule other,
3299 higher priority settings.
3300 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3301 (interactive "FWrite agenda to file: \nP")
3302 (if (or (not (file-writable-p file))
3303 (and (file-exists-p file)
3304 (if (called-interactively-p 'any)
3305 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3306 (user-error "Cannot write agenda to file %s" file))
3307 (org-let (if nosettings nil org-agenda-exporter-settings)
3308 '(save-excursion
3309 (save-window-excursion
3310 (org-agenda-mark-filtered-text)
3311 (let ((bs (copy-sequence (buffer-string))) beg content)
3312 (org-agenda-unmark-filtered-text)
3313 (with-temp-buffer
3314 (rename-buffer org-agenda-write-buffer-name t)
3315 (set-buffer-modified-p nil)
3316 (insert bs)
3317 (org-agenda-remove-marked-text 'org-filtered)
3318 (while (setq beg (text-property-any (point-min) (point-max)
3319 'org-filtered t))
3320 (delete-region
3321 beg (or (next-single-property-change beg 'org-filtered)
3322 (point-max))))
3323 (run-hooks 'org-agenda-before-write-hook)
3324 (cond
3325 ((org-bound-and-true-p org-mobile-creating-agendas)
3326 (org-mobile-write-agenda-for-mobile file))
3327 ((string-match "\\.org\\'" file)
3328 (let (content p m message-log-max)
3329 (goto-char (point-min))
3330 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3331 (goto-char p)
3332 (setq m (get-text-property (point) 'org-hd-marker))
3333 (when m
3334 (push (save-excursion
3335 (set-buffer (marker-buffer m))
3336 (goto-char m)
3337 (org-copy-subtree 1 nil t t)
3338 org-subtree-clip)
3339 content)))
3340 (find-file file)
3341 (erase-buffer)
3342 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3343 (write-file file)
3344 (kill-buffer (current-buffer))
3345 (message "Org file written to %s" file)))
3346 ((string-match "\\.html?\\'" file)
3347 (require 'htmlize)
3348 (set-buffer (htmlize-buffer (current-buffer)))
3349 (when org-agenda-export-html-style
3350 ;; replace <style> section with org-agenda-export-html-style
3351 (goto-char (point-min))
3352 (kill-region (- (search-forward "<style") 6)
3353 (search-forward "</style>"))
3354 (insert org-agenda-export-html-style))
3355 (write-file file)
3356 (kill-buffer (current-buffer))
3357 (message "HTML written to %s" file))
3358 ((string-match "\\.ps\\'" file)
3359 (require 'ps-print)
3360 (ps-print-buffer-with-faces file)
3361 (message "Postscript written to %s" file))
3362 ((string-match "\\.pdf\\'" file)
3363 (require 'ps-print)
3364 (ps-print-buffer-with-faces
3365 (concat (file-name-sans-extension file) ".ps"))
3366 (call-process "ps2pdf" nil nil nil
3367 (expand-file-name
3368 (concat (file-name-sans-extension file) ".ps"))
3369 (expand-file-name file))
3370 (delete-file (concat (file-name-sans-extension file) ".ps"))
3371 (message "PDF written to %s" file))
3372 ((string-match "\\.ics\\'" file)
3373 (require 'ox-icalendar)
3374 (org-icalendar-export-current-agenda (expand-file-name file)))
3376 (let ((bs (buffer-string)))
3377 (find-file file)
3378 (erase-buffer)
3379 (insert bs)
3380 (save-buffer 0)
3381 (kill-buffer (current-buffer))
3382 (message "Plain text written to %s" file))))))))
3383 (set-buffer (or agenda-bufname
3384 (and (org-called-interactively-p 'any) (buffer-name))
3385 org-agenda-buffer-name)))
3386 (when open (org-open-file file)))
3388 (defvar org-agenda-tag-filter-overlays nil)
3389 (defvar org-agenda-cat-filter-overlays nil)
3390 (defvar org-agenda-re-filter-overlays nil)
3392 (defun org-agenda-mark-filtered-text ()
3393 "Mark all text hidden by filtering with a text property."
3394 (let ((inhibit-read-only t))
3395 (mapc
3396 (lambda (o)
3397 (when (equal (overlay-buffer o) (current-buffer))
3398 (put-text-property
3399 (overlay-start o) (overlay-end o)
3400 'org-filtered t)))
3401 (append org-agenda-tag-filter-overlays
3402 org-agenda-cat-filter-overlays
3403 org-agenda-re-filter-overlays))))
3405 (defun org-agenda-unmark-filtered-text ()
3406 "Remove the filtering text property."
3407 (let ((inhibit-read-only t))
3408 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3410 (defun org-agenda-remove-marked-text (property &optional value)
3411 "Delete all text marked with VALUE of PROPERTY.
3412 VALUE defaults to t."
3413 (let (beg)
3414 (setq value (or value t))
3415 (while (setq beg (text-property-any (point-min) (point-max)
3416 property value))
3417 (delete-region
3418 beg (or (next-single-property-change beg 'org-filtered)
3419 (point-max))))))
3421 (defun org-agenda-add-entry-text ()
3422 "Add entry text to agenda lines.
3423 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3424 entry text following headings shown in the agenda.
3425 Drawers will be excluded, also the line with scheduling/deadline info."
3426 (when (and (> org-agenda-add-entry-text-maxlines 0)
3427 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3428 (let (m txt)
3429 (goto-char (point-min))
3430 (while (not (eobp))
3431 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3432 (beginning-of-line 2)
3433 (setq txt (org-agenda-get-some-entry-text
3434 m org-agenda-add-entry-text-maxlines " > "))
3435 (end-of-line 1)
3436 (if (string-match "\\S-" txt)
3437 (insert "\n" txt)
3438 (or (eobp) (forward-char 1))))))))
3440 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3441 &rest keep)
3442 "Extract entry text from MARKER, at most N-LINES lines.
3443 This will ignore drawers etc, just get the text.
3444 If INDENT is given, prefix every line with this string. If KEEP is
3445 given, it is a list of symbols, defining stuff that should not be
3446 removed from the entry content. Currently only `planning' is allowed here."
3447 (let (txt drawer-re kwd-time-re ind)
3448 (save-excursion
3449 (with-current-buffer (marker-buffer marker)
3450 (if (not (derived-mode-p 'org-mode))
3451 (setq txt "")
3452 (save-excursion
3453 (save-restriction
3454 (widen)
3455 (goto-char marker)
3456 (end-of-line 1)
3457 (setq txt (buffer-substring
3458 (min (1+ (point)) (point-max))
3459 (progn (outline-next-heading) (point)))
3460 drawer-re org-drawer-regexp
3461 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3462 ".*\n?"))
3463 (with-temp-buffer
3464 (insert txt)
3465 (when org-agenda-add-entry-text-descriptive-links
3466 (goto-char (point-min))
3467 (while (org-activate-bracket-links (point-max))
3468 (add-text-properties (match-beginning 0) (match-end 0)
3469 '(face org-link))))
3470 (goto-char (point-min))
3471 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3472 (set-text-properties (match-beginning 0) (match-end 0)
3473 nil))
3474 (goto-char (point-min))
3475 (while (re-search-forward drawer-re nil t)
3476 (delete-region
3477 (match-beginning 0)
3478 (progn (re-search-forward
3479 "^[ \t]*:END:.*\n?" nil 'move)
3480 (point))))
3481 (unless (member 'planning keep)
3482 (goto-char (point-min))
3483 (while (re-search-forward kwd-time-re nil t)
3484 (replace-match "")))
3485 (goto-char (point-min))
3486 (when org-agenda-entry-text-exclude-regexps
3487 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3488 (while (setq re (pop re-list))
3489 (goto-char (point-min))
3490 (while (re-search-forward re nil t)
3491 (replace-match "")))))
3492 (goto-char (point-max))
3493 (skip-chars-backward " \t\n")
3494 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3496 ;; find and remove min common indentation
3497 (goto-char (point-min))
3498 (untabify (point-min) (point-max))
3499 (setq ind (org-get-indentation))
3500 (while (not (eobp))
3501 (unless (looking-at "[ \t]*$")
3502 (setq ind (min ind (org-get-indentation))))
3503 (beginning-of-line 2))
3504 (goto-char (point-min))
3505 (while (not (eobp))
3506 (unless (looking-at "[ \t]*$")
3507 (move-to-column ind)
3508 (delete-region (point-at-bol) (point)))
3509 (beginning-of-line 2))
3511 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3513 (goto-char (point-min))
3514 (when indent
3515 (while (and (not (eobp)) (re-search-forward "^" nil t))
3516 (replace-match indent t t)))
3517 (goto-char (point-min))
3518 (while (looking-at "[ \t]*\n") (replace-match ""))
3519 (goto-char (point-max))
3520 (when (> (org-current-line)
3521 n-lines)
3522 (org-goto-line (1+ n-lines))
3523 (backward-char 1))
3524 (setq txt (buffer-substring (point-min) (point)))))))))
3525 txt))
3527 (defun org-check-for-org-mode ()
3528 "Make sure current buffer is in org-mode. Error if not."
3529 (or (derived-mode-p 'org-mode)
3530 (error "Cannot execute org-mode agenda command on buffer in %s"
3531 major-mode)))
3533 ;;; Agenda prepare and finalize
3535 (defvar org-agenda-multi nil) ; dynamically scoped
3536 (defvar org-agenda-pre-window-conf nil)
3537 (defvar org-agenda-columns-active nil)
3538 (defvar org-agenda-name nil)
3539 (defvar org-agenda-tag-filter nil)
3540 (defvar org-agenda-category-filter nil)
3541 (defvar org-agenda-regexp-filter nil)
3542 (defvar org-agenda-top-headline-filter nil)
3543 (defvar org-agenda-tag-filter-while-redo nil)
3544 (defvar org-agenda-tag-filter-preset nil
3545 "A preset of the tags filter used for secondary agenda filtering.
3546 This must be a list of strings, each string must be a single tag preceded
3547 by \"+\" or \"-\".
3548 This variable should not be set directly, but agenda custom commands can
3549 bind it in the options section. The preset filter is a global property of
3550 the entire agenda view. In a block agenda, it will not work reliably to
3551 define a filter for one of the individual blocks. You need to set it in
3552 the global options and expect it to be applied to the entire view.")
3554 (defvar org-agenda-category-filter-preset nil
3555 "A preset of the category filter used for secondary agenda filtering.
3556 This must be a list of strings, each string must be a single category
3557 preceded by \"+\" or \"-\".
3558 This variable should not be set directly, but agenda custom commands can
3559 bind it in the options section. The preset filter is a global property of
3560 the entire agenda view. In a block agenda, it will not work reliably to
3561 define a filter for one of the individual blocks. You need to set it in
3562 the global options and expect it to be applied to the entire view.")
3564 (defvar org-agenda-regexp-filter-preset nil
3565 "A preset of the regexp filter used for secondary agenda filtering.
3566 This must be a list of strings, each string must be a single category
3567 preceded by \"+\" or \"-\".
3568 This variable should not be set directly, but agenda custom commands can
3569 bind it in the options section. The preset filter is a global property of
3570 the entire agenda view. In a block agenda, it will not work reliably to
3571 define a filter for one of the individual blocks. You need to set it in
3572 the global options and expect it to be applied to the entire view.")
3574 (defun org-agenda-use-sticky-p ()
3575 "Return non-nil if an agenda buffer named
3576 `org-agenda-buffer-name' exists and should be shown instead of
3577 generating a new one."
3578 (and
3579 ;; turned off by user
3580 org-agenda-sticky
3581 ;; For multi-agenda buffer already exists
3582 (not org-agenda-multi)
3583 ;; buffer found
3584 (get-buffer org-agenda-buffer-name)
3585 ;; C-u parameter is same as last call
3586 (with-current-buffer (get-buffer org-agenda-buffer-name)
3587 (and
3588 (equal current-prefix-arg
3589 org-agenda-last-prefix-arg)
3590 ;; In case user turned stickiness on, while having existing
3591 ;; Agenda buffer active, don't reuse that buffer, because it
3592 ;; does not have org variables local
3593 org-agenda-this-buffer-is-sticky))))
3595 (defun org-agenda-prepare-window (abuf)
3596 "Setup agenda buffer in the window."
3597 (let* ((awin (get-buffer-window abuf))
3598 wconf)
3599 (cond
3600 ((equal (current-buffer) abuf) nil)
3601 (awin (select-window awin))
3602 ((not (setq wconf (current-window-configuration))))
3603 ((equal org-agenda-window-setup 'current-window)
3604 (org-pop-to-buffer-same-window abuf))
3605 ((equal org-agenda-window-setup 'other-window)
3606 (org-switch-to-buffer-other-window abuf))
3607 ((equal org-agenda-window-setup 'other-frame)
3608 (switch-to-buffer-other-frame abuf))
3609 ((equal org-agenda-window-setup 'reorganize-frame)
3610 (delete-other-windows)
3611 (org-switch-to-buffer-other-window abuf)))
3612 ;; additional test in case agenda is invoked from within agenda
3613 ;; buffer via elisp link
3614 (unless (equal (current-buffer) abuf)
3615 (org-pop-to-buffer-same-window abuf))
3616 (setq org-agenda-pre-window-conf
3617 (or org-agenda-pre-window-conf wconf))))
3619 (defun org-agenda-prepare (&optional name)
3620 (if (org-agenda-use-sticky-p)
3621 (progn
3622 ;; Popup existing buffer
3623 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3624 (message "Sticky Agenda buffer, use `r' to refresh")
3625 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3626 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3627 (setq org-todo-keywords-for-agenda nil)
3628 (setq org-drawers-for-agenda nil)
3629 (unless org-agenda-persistent-filter
3630 (setq org-agenda-tag-filter nil
3631 org-agenda-category-filter nil
3632 org-agenda-regexp-filter nil))
3633 (put 'org-agenda-tag-filter :preset-filter
3634 org-agenda-tag-filter-preset)
3635 (put 'org-agenda-category-filter :preset-filter
3636 org-agenda-category-filter-preset)
3637 (put 'org-agenda-regexp-filter :preset-filter
3638 org-agenda-regexp-filter-preset)
3639 (if org-agenda-multi
3640 (progn
3641 (setq buffer-read-only nil)
3642 (goto-char (point-max))
3643 (unless (or (bobp) org-agenda-compact-blocks
3644 (not org-agenda-block-separator))
3645 (insert "\n"
3646 (if (stringp org-agenda-block-separator)
3647 org-agenda-block-separator
3648 (make-string (window-width) org-agenda-block-separator))
3649 "\n"))
3650 (narrow-to-region (point) (point-max)))
3651 (setq org-done-keywords-for-agenda nil)
3653 ;; Setting any org variables that are in org-agenda-local-vars
3654 ;; list need to be done after the prepare call
3655 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3656 (setq buffer-read-only nil)
3657 (org-agenda-reset-markers)
3658 (let ((inhibit-read-only t)) (erase-buffer))
3659 (org-agenda-mode)
3660 (setq org-agenda-buffer (current-buffer))
3661 (setq org-agenda-contributing-files nil)
3662 (setq org-agenda-columns-active nil)
3663 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3664 (setq org-todo-keywords-for-agenda
3665 (org-uniquify org-todo-keywords-for-agenda))
3666 (setq org-done-keywords-for-agenda
3667 (org-uniquify org-done-keywords-for-agenda))
3668 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3669 (setq org-agenda-last-prefix-arg current-prefix-arg)
3670 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3671 (and name (not org-agenda-name)
3672 (org-set-local 'org-agenda-name name)))
3673 (setq buffer-read-only nil)))
3675 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3676 (defun org-agenda-finalize ()
3677 "Finishing touch for the agenda buffer, called just before displaying it."
3678 (unless org-agenda-multi
3679 (save-excursion
3680 (let ((inhibit-read-only t))
3681 (goto-char (point-min))
3682 (save-excursion
3683 (while (org-activate-bracket-links (point-max))
3684 (add-text-properties (match-beginning 0) (match-end 0)
3685 '(face org-link))))
3686 (save-excursion
3687 (while (org-activate-plain-links (point-max))
3688 (add-text-properties (match-beginning 0) (match-end 0)
3689 '(face org-link))))
3690 (unless (eq org-agenda-remove-tags t)
3691 (org-agenda-align-tags))
3692 (unless org-agenda-with-colors
3693 (remove-text-properties (point-min) (point-max) '(face nil)))
3694 (if (and (boundp 'org-agenda-overriding-columns-format)
3695 org-agenda-overriding-columns-format)
3696 (org-set-local 'org-agenda-overriding-columns-format
3697 org-agenda-overriding-columns-format))
3698 (if (and (boundp 'org-agenda-view-columns-initially)
3699 org-agenda-view-columns-initially)
3700 (org-agenda-columns))
3701 (when org-agenda-fontify-priorities
3702 (org-agenda-fontify-priorities))
3703 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3704 (org-agenda-dim-blocked-tasks))
3705 ;; We need to widen when `org-agenda-finalize' is called from
3706 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3707 (when org-clock-current-task
3708 (save-restriction
3709 (widen)
3710 (org-agenda-mark-clocking-task)))
3711 (when org-agenda-entry-text-mode
3712 (org-agenda-entry-text-hide)
3713 (org-agenda-entry-text-show))
3714 (if (and (functionp 'org-habit-insert-consistency-graphs)
3715 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3716 (org-habit-insert-consistency-graphs))
3717 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3718 (unless (or (eq org-agenda-show-inherited-tags 'always)
3719 (and (listp org-agenda-show-inherited-tags)
3720 (memq org-agenda-type org-agenda-show-inherited-tags))
3721 (and (eq org-agenda-show-inherited-tags t)
3722 (or (eq org-agenda-use-tag-inheritance t)
3723 (and (listp org-agenda-use-tag-inheritance)
3724 (not (memq org-agenda-type
3725 org-agenda-use-tag-inheritance))))))
3726 (let (mrk)
3727 (save-excursion
3728 (goto-char (point-min))
3729 (while (equal (forward-line) 0)
3730 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3731 (get-text-property (point) 'org-hd-marker)))
3732 (put-text-property (point-at-bol) (point-at-eol)
3733 'tags (org-with-point-at mrk
3734 (delete-dups
3735 (mapcar 'downcase (org-get-tags-at))))))))))
3736 (run-hooks 'org-agenda-finalize-hook)
3737 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3738 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3739 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3740 (org-agenda-filter-apply org-agenda-category-filter 'category))
3741 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3742 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3743 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3745 (defun org-agenda-mark-clocking-task ()
3746 "Mark the current clock entry in the agenda if it is present."
3747 (org-agenda-unmark-clocking-task)
3748 (when (marker-buffer org-clock-hd-marker)
3749 (save-excursion
3750 (goto-char (point-min))
3751 (let (s ov)
3752 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3753 (goto-char s)
3754 (when (equal (org-get-at-bol 'org-hd-marker)
3755 org-clock-hd-marker)
3756 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3757 (overlay-put ov 'type 'org-agenda-clocking)
3758 (overlay-put ov 'face 'org-agenda-clocking)
3759 (overlay-put ov 'help-echo
3760 "The clock is running in this item")))))))
3762 (defun org-agenda-unmark-clocking-task ()
3763 "Unmark the current clocking task."
3764 (mapc (lambda (o)
3765 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3766 (delete-overlay o)))
3767 (overlays-in (point-min) (point-max))))
3769 (defun org-agenda-fontify-priorities ()
3770 "Make highest priority lines bold, and lowest italic."
3771 (interactive)
3772 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3773 (delete-overlay o)))
3774 (overlays-in (point-min) (point-max)))
3775 (save-excursion
3776 (let (b e p ov h l)
3777 (goto-char (point-min))
3778 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3779 (setq h (or (get-char-property (point) 'org-highest-priority)
3780 org-highest-priority)
3781 l (or (get-char-property (point) 'org-lowest-priority)
3782 org-lowest-priority)
3783 p (string-to-char (match-string 1))
3784 b (match-beginning 0)
3785 e (if (eq org-agenda-fontify-priorities 'cookies)
3786 (match-end 0)
3787 (point-at-eol))
3788 ov (make-overlay b e))
3789 (overlay-put
3790 ov 'face
3791 (cons (cond ((org-face-from-face-or-color
3792 'priority nil
3793 (cdr (assoc p org-priority-faces))))
3794 ((and (listp org-agenda-fontify-priorities)
3795 (org-face-from-face-or-color
3796 'priority nil
3797 (cdr (assoc p org-agenda-fontify-priorities)))))
3798 ((equal p l) 'italic)
3799 ((equal p h) 'bold))
3800 'org-priority))
3801 (overlay-put ov 'org-type 'org-priority)))))
3803 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3804 "Dim currently blocked TODO's in the agenda display.
3805 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3806 dimming them."
3807 (interactive "P")
3808 (when (org-called-interactively-p 'interactive)
3809 (message "Dim or hide blocked tasks..."))
3810 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3811 (delete-overlay o)))
3812 (overlays-in (point-min) (point-max)))
3813 (save-excursion
3814 (let ((inhibit-read-only t)
3815 (org-depend-tag-blocked nil)
3816 (invis (or (not (null invisible))
3817 (eq org-agenda-dim-blocked-tasks 'invisible)))
3818 org-blocked-by-checkboxes
3819 invis1 b e p ov h l)
3820 (goto-char (point-min))
3821 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3822 (and pos (goto-char (1+ pos))))
3823 (setq org-blocked-by-checkboxes nil invis1 invis)
3824 (let ((marker (org-get-at-bol 'org-hd-marker)))
3825 (when (and marker
3826 (with-current-buffer (marker-buffer marker)
3827 (save-excursion (goto-char marker)
3828 (org-entry-blocked-p))))
3829 (if org-blocked-by-checkboxes (setq invis1 nil))
3830 (setq b (if invis1
3831 (max (point-min) (1- (point-at-bol)))
3832 (point-at-bol))
3833 e (point-at-eol)
3834 ov (make-overlay b e))
3835 (if invis1
3836 (overlay-put ov 'invisible t)
3837 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3838 (overlay-put ov 'org-type 'org-blocked-todo))))))
3839 (when (org-called-interactively-p 'interactive)
3840 (message "Dim or hide blocked tasks...done")))
3842 (defvar org-agenda-skip-function nil
3843 "Function to be called at each match during agenda construction.
3844 If this function returns nil, the current match should not be skipped.
3845 Otherwise, the function must return a position from where the search
3846 should be continued.
3847 This may also be a Lisp form, it will be evaluated.
3848 Never set this variable using `setq' or so, because then it will apply
3849 to all future agenda commands. If you do want a global skipping condition,
3850 use the option `org-agenda-skip-function-global' instead.
3851 The correct usage for `org-agenda-skip-function' is to bind it with
3852 `let' to scope it dynamically into the agenda-constructing command.
3853 A good way to set it is through options in `org-agenda-custom-commands'.")
3855 (defun org-agenda-skip ()
3856 "Throw to `:skip' in places that should be skipped.
3857 Also moves point to the end of the skipped region, so that search can
3858 continue from there."
3859 (let ((p (point-at-bol)) to)
3860 (when (or
3861 (save-excursion (goto-char p) (looking-at comment-start-skip))
3862 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3863 (get-text-property p :org-archived)
3864 (org-end-of-subtree t))
3865 (and org-agenda-skip-comment-trees
3866 (get-text-property p :org-comment)
3867 (org-end-of-subtree t))
3868 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3869 (org-agenda-skip-eval org-agenda-skip-function)))
3870 (goto-char to))
3871 (org-in-src-block-p t))
3872 (throw :skip t))))
3874 (defun org-agenda-skip-eval (form)
3875 "If FORM is a function or a list, call (or eval) is and return result.
3876 `save-excursion' and `save-match-data' are wrapped around the call, so point
3877 and match data are returned to the previous state no matter what these
3878 functions do."
3879 (let (fp)
3880 (and form
3881 (or (setq fp (functionp form))
3882 (consp form))
3883 (save-excursion
3884 (save-match-data
3885 (if fp
3886 (funcall form)
3887 (eval form)))))))
3889 (defvar org-agenda-markers nil
3890 "List of all currently active markers created by `org-agenda'.")
3891 (defvar org-agenda-last-marker-time (org-float-time)
3892 "Creation time of the last agenda marker.")
3894 (defun org-agenda-new-marker (&optional pos)
3895 "Return a new agenda marker.
3896 Org-mode keeps a list of these markers and resets them when they are
3897 no longer in use."
3898 (let ((m (copy-marker (or pos (point)))))
3899 (setq org-agenda-last-marker-time (org-float-time))
3900 (if org-agenda-buffer
3901 (with-current-buffer org-agenda-buffer
3902 (push m org-agenda-markers))
3903 (push m org-agenda-markers))
3906 (defun org-agenda-reset-markers ()
3907 "Reset markers created by `org-agenda'."
3908 (while org-agenda-markers
3909 (move-marker (pop org-agenda-markers) nil)))
3911 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3912 "Save relative positions of markers in region.
3913 This check for agenda markers in all agenda buffers currently active."
3914 (dolist (buf (buffer-list))
3915 (with-current-buffer buf
3916 (when (eq major-mode 'org-agenda-mode)
3917 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3918 org-agenda-markers)))))
3920 ;;; Entry text mode
3922 (defun org-agenda-entry-text-show-here ()
3923 "Add some text from the entry as context to the current line."
3924 (let (m txt o)
3925 (setq m (org-get-at-bol 'org-hd-marker))
3926 (unless (marker-buffer m)
3927 (error "No marker points to an entry here"))
3928 (setq txt (concat "\n" (org-no-properties
3929 (org-agenda-get-some-entry-text
3930 m org-agenda-entry-text-maxlines
3931 org-agenda-entry-text-leaders))))
3932 (when (string-match "\\S-" txt)
3933 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3934 (overlay-put o 'evaporate t)
3935 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3936 (overlay-put o 'after-string txt))))
3938 (defun org-agenda-entry-text-show ()
3939 "Add entry context for all agenda lines."
3940 (interactive)
3941 (save-excursion
3942 (goto-char (point-max))
3943 (beginning-of-line 1)
3944 (while (not (bobp))
3945 (when (org-get-at-bol 'org-hd-marker)
3946 (org-agenda-entry-text-show-here))
3947 (beginning-of-line 0))))
3949 (defun org-agenda-entry-text-hide ()
3950 "Remove any shown entry context."
3951 (delq nil
3952 (mapcar (lambda (o)
3953 (if (eq (overlay-get o 'org-overlay-type)
3954 'agenda-entry-content)
3955 (progn (delete-overlay o) t)))
3956 (overlays-in (point-min) (point-max)))))
3958 (defun org-agenda-get-day-face (date)
3959 "Return the face DATE should be displayed with."
3960 (or (and (functionp org-agenda-day-face-function)
3961 (funcall org-agenda-day-face-function date))
3962 (cond ((org-agenda-todayp date)
3963 'org-agenda-date-today)
3964 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3965 'org-agenda-date-weekend)
3966 (t 'org-agenda-date))))
3968 ;;; Agenda timeline
3970 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3972 (defun org-timeline (&optional dotodo)
3973 "Show a time-sorted view of the entries in the current org file.
3974 Only entries with a time stamp of today or later will be listed. With
3975 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3976 under the current date.
3977 If the buffer contains an active region, only check the region for
3978 dates."
3979 (interactive "P")
3980 (let* ((dopast t)
3981 (org-agenda-show-log-scoped org-agenda-show-log)
3982 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3983 (current-buffer))))
3984 (date (calendar-current-date))
3985 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3986 (end (if (org-region-active-p) (region-end) (point-max)))
3987 (day-numbers (org-get-all-dates
3988 beg end 'no-ranges
3989 t org-agenda-show-log-scoped ; always include today
3990 org-timeline-show-empty-dates))
3991 (org-deadline-warning-days 0)
3992 (org-agenda-only-exact-dates t)
3993 (today (org-today))
3994 (past t)
3995 args
3996 s e rtn d emptyp)
3997 (setq org-agenda-redo-command
3998 (list 'progn
3999 (list 'org-switch-to-buffer-other-window (current-buffer))
4000 (list 'org-timeline (list 'quote dotodo))))
4001 (if (not dopast)
4002 ;; Remove past dates from the list of dates.
4003 (setq day-numbers (delq nil (mapcar (lambda(x)
4004 (if (>= x today) x nil))
4005 day-numbers))))
4006 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4007 (org-compile-prefix-format 'timeline)
4008 (org-set-sorting-strategy 'timeline)
4009 (if org-agenda-show-log-scoped (push :closed args))
4010 (push :timestamp args)
4011 (push :deadline args)
4012 (push :scheduled args)
4013 (push :sexp args)
4014 (if dotodo (push :todo args))
4015 (insert "Timeline of file " entry "\n")
4016 (add-text-properties (point-min) (point)
4017 (list 'face 'org-agenda-structure))
4018 (org-agenda-mark-header-line (point-min))
4019 (while (setq d (pop day-numbers))
4020 (if (and (listp d) (eq (car d) :omitted))
4021 (progn
4022 (setq s (point))
4023 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4024 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4025 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4026 (if (and (>= d today)
4027 dopast
4028 past)
4029 (progn
4030 (setq past nil)
4031 (insert (make-string 79 ?-) "\n")))
4032 (setq date (calendar-gregorian-from-absolute d))
4033 (setq s (point))
4034 (setq rtn (and (not emptyp)
4035 (apply 'org-agenda-get-day-entries entry
4036 date args)))
4037 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4038 (progn
4039 (insert
4040 (if (stringp org-agenda-format-date)
4041 (format-time-string org-agenda-format-date
4042 (org-time-from-absolute date))
4043 (funcall org-agenda-format-date date))
4044 "\n")
4045 (put-text-property s (1- (point)) 'face
4046 (org-agenda-get-day-face date))
4047 (put-text-property s (1- (point)) 'org-date-line t)
4048 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4049 (if (equal d today)
4050 (put-text-property s (1- (point)) 'org-today t))
4051 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4052 (put-text-property s (1- (point)) 'day d)))))
4053 (goto-char (point-min))
4054 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4055 (point-min)))
4056 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
4057 (org-agenda-finalize)
4058 (setq buffer-read-only t)))
4060 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4061 "Return a list of all relevant day numbers from BEG to END buffer positions.
4062 If NO-RANGES is non-nil, include only the start and end dates of a range,
4063 not every single day in the range. If FORCE-TODAY is non-nil, make
4064 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4065 inactive time stamps (those in square brackets) are included.
4066 When EMPTY is non-nil, also include days without any entries."
4067 (let ((re (concat
4068 (if pre-re pre-re "")
4069 (if inactive org-ts-regexp-both org-ts-regexp)))
4070 dates dates1 date day day1 day2 ts1 ts2 pos)
4071 (if force-today
4072 (setq dates (list (org-today))))
4073 (save-excursion
4074 (goto-char beg)
4075 (while (re-search-forward re end t)
4076 (setq day (time-to-days (org-time-string-to-time
4077 (substring (match-string 1) 0 10)
4078 (current-buffer) (match-beginning 0))))
4079 (or (memq day dates) (push day dates)))
4080 (unless no-ranges
4081 (goto-char beg)
4082 (while (re-search-forward org-tr-regexp end t)
4083 (setq pos (match-beginning 0))
4084 (setq ts1 (substring (match-string 1) 0 10)
4085 ts2 (substring (match-string 2) 0 10)
4086 day1 (time-to-days (org-time-string-to-time
4087 ts1 (current-buffer) pos))
4088 day2 (time-to-days (org-time-string-to-time
4089 ts2 (current-buffer) pos)))
4090 (while (< (setq day1 (1+ day1)) day2)
4091 (or (memq day1 dates) (push day1 dates)))))
4092 (setq dates (sort dates '<))
4093 (when empty
4094 (while (setq day (pop dates))
4095 (setq day2 (car dates))
4096 (push day dates1)
4097 (when (and day2 empty)
4098 (if (or (eq empty t)
4099 (and (numberp empty) (<= (- day2 day) empty)))
4100 (while (< (setq day (1+ day)) day2)
4101 (push (list day) dates1))
4102 (push (cons :omitted (- day2 day)) dates1))))
4103 (setq dates (nreverse dates1)))
4104 dates)))
4106 ;;; Agenda Daily/Weekly
4108 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4109 "Start day for the agenda view.
4110 Custom commands can set this variable in the options section.
4111 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4112 input allowed when reading a date through the Org calendar.
4113 See the docstring of `org-read-date' for details.")
4114 (defvar org-starting-day nil) ; local variable in the agenda buffer
4115 (defvar org-arg-loc nil) ; local variable
4117 (defvar org-agenda-buffer-tmp-name nil)
4118 ;;;###autoload
4119 (defun org-agenda-list (&optional arg start-day span with-hour)
4120 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4121 The view will be for the current day or week, but from the overview buffer
4122 you will be able to go to other days/weeks.
4124 With a numeric prefix argument in an interactive call, the agenda will
4125 span ARG days. Lisp programs should instead specify SPAN to change
4126 the number of days. SPAN defaults to `org-agenda-span'.
4128 START-DAY defaults to TODAY, or to the most recent match for the weekday
4129 given in `org-agenda-start-on-weekday'.
4131 When WITH-HOUR is non-nil, only include scheduled and deadline
4132 items if they have an hour specification like [h]h:mm."
4133 (interactive "P")
4134 (if org-agenda-overriding-arguments
4135 (setq arg (car org-agenda-overriding-arguments)
4136 start-day (nth 1 org-agenda-overriding-arguments)
4137 span (nth 2 org-agenda-overriding-arguments)))
4138 (if (and (integerp arg) (> arg 0))
4139 (setq span arg arg nil))
4140 (catch 'exit
4141 (setq org-agenda-buffer-name
4142 (or org-agenda-buffer-tmp-name
4143 (if org-agenda-sticky
4144 (cond ((and org-keys (stringp org-match))
4145 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4146 (org-keys
4147 (format "*Org Agenda(%s)*" org-keys))
4148 (t "*Org Agenda(a)*")))
4149 org-agenda-buffer-name))
4150 (org-agenda-prepare "Day/Week")
4151 (setq start-day (or start-day org-agenda-start-day))
4152 (if (stringp start-day)
4153 ;; Convert to an absolute day number
4154 (setq start-day (time-to-days (org-read-date nil t start-day))))
4155 (org-compile-prefix-format 'agenda)
4156 (org-set-sorting-strategy 'agenda)
4157 (let* ((span (org-agenda-ndays-to-span
4158 (or span org-agenda-ndays org-agenda-span)))
4159 (today (org-today))
4160 (sd (or start-day today))
4161 (ndays (org-agenda-span-to-ndays span sd))
4162 (org-agenda-start-on-weekday
4163 (if (eq ndays 7)
4164 org-agenda-start-on-weekday))
4165 (thefiles (org-agenda-files nil 'ifmode))
4166 (files thefiles)
4167 (start (if (or (null org-agenda-start-on-weekday)
4168 (< ndays 7))
4170 (let* ((nt (calendar-day-of-week
4171 (calendar-gregorian-from-absolute sd)))
4172 (n1 org-agenda-start-on-weekday)
4173 (d (- nt n1)))
4174 (- sd (+ (if (< d 0) 7 0) d)))))
4175 (day-numbers (list start))
4176 (day-cnt 0)
4177 (inhibit-redisplay (not debug-on-error))
4178 (org-agenda-show-log-scoped org-agenda-show-log)
4179 s e rtn rtnall file date d start-pos end-pos todayp
4180 clocktable-start clocktable-end filter)
4181 (setq org-agenda-redo-command
4182 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4183 (dotimes (n (1- ndays))
4184 (push (1+ (car day-numbers)) day-numbers))
4185 (setq day-numbers (nreverse day-numbers))
4186 (setq clocktable-start (car day-numbers)
4187 clocktable-end (1+ (or (org-last day-numbers) 0)))
4188 (org-set-local 'org-starting-day (car day-numbers))
4189 (org-set-local 'org-arg-loc arg)
4190 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4191 (unless org-agenda-compact-blocks
4192 (let* ((d1 (car day-numbers))
4193 (d2 (org-last day-numbers))
4194 (w1 (org-days-to-iso-week d1))
4195 (w2 (org-days-to-iso-week d2)))
4196 (setq s (point))
4197 (if org-agenda-overriding-header
4198 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4199 nil 'face 'org-agenda-structure) "\n")
4200 (insert (org-agenda-span-name span)
4201 "-agenda"
4202 (if (< (- d2 d1) 350)
4203 (if (= w1 w2)
4204 (format " (W%02d)" w1)
4205 (format " (W%02d-W%02d)" w1 w2))
4207 ":\n")))
4208 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4209 'org-date-line t))
4210 (org-agenda-mark-header-line s))
4211 (while (setq d (pop day-numbers))
4212 (setq date (calendar-gregorian-from-absolute d)
4213 s (point))
4214 (if (or (setq todayp (= d today))
4215 (and (not start-pos) (= d sd)))
4216 (setq start-pos (point))
4217 (if (and start-pos (not end-pos))
4218 (setq end-pos (point))))
4219 (setq files thefiles
4220 rtnall nil)
4221 (while (setq file (pop files))
4222 (catch 'nextfile
4223 (org-check-agenda-file file)
4224 (let ((org-agenda-entry-types org-agenda-entry-types))
4225 ;; Starred types override non-starred equivalents
4226 (when (member :deadline* org-agenda-entry-types)
4227 (setq org-agenda-entry-types
4228 (delq :deadline org-agenda-entry-types)))
4229 (when (member :scheduled* org-agenda-entry-types)
4230 (setq org-agenda-entry-types
4231 (delq :scheduled org-agenda-entry-types)))
4232 ;; Honor with-hour
4233 (when with-hour
4234 (when (member :deadline org-agenda-entry-types)
4235 (setq org-agenda-entry-types
4236 (delq :deadline org-agenda-entry-types))
4237 (push :deadline* org-agenda-entry-types))
4238 (when (member :scheduled org-agenda-entry-types)
4239 (setq org-agenda-entry-types
4240 (delq :scheduled org-agenda-entry-types))
4241 (push :scheduled* org-agenda-entry-types)))
4242 (unless org-agenda-include-deadlines
4243 (setq org-agenda-entry-types
4244 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4245 (cond
4246 ((memq org-agenda-show-log-scoped '(only clockcheck))
4247 (setq rtn (org-agenda-get-day-entries
4248 file date :closed)))
4249 (org-agenda-show-log-scoped
4250 (setq rtn (apply 'org-agenda-get-day-entries
4251 file date
4252 (append '(:closed) org-agenda-entry-types))))
4254 (setq rtn (apply 'org-agenda-get-day-entries
4255 file date
4256 org-agenda-entry-types)))))
4257 (setq rtnall (append rtnall rtn)))) ;; all entries
4258 (if org-agenda-include-diary
4259 (let ((org-agenda-search-headline-for-time t))
4260 (require 'diary-lib)
4261 (setq rtn (org-get-entries-from-diary date))
4262 (setq rtnall (append rtnall rtn))))
4263 (if (or rtnall org-agenda-show-all-dates)
4264 (progn
4265 (setq day-cnt (1+ day-cnt))
4266 (insert
4267 (if (stringp org-agenda-format-date)
4268 (format-time-string org-agenda-format-date
4269 (org-time-from-absolute date))
4270 (funcall org-agenda-format-date date))
4271 "\n")
4272 (put-text-property s (1- (point)) 'face
4273 (org-agenda-get-day-face date))
4274 (put-text-property s (1- (point)) 'org-date-line t)
4275 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4276 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4277 (when todayp
4278 (put-text-property s (1- (point)) 'org-today t))
4279 (setq rtnall
4280 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4281 (if rtnall (insert ;; all entries
4282 (org-agenda-finalize-entries rtnall 'agenda)
4283 "\n"))
4284 (put-text-property s (1- (point)) 'day d)
4285 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4286 (when (and org-agenda-clockreport-mode clocktable-start)
4287 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4288 ;; the above line is to ensure the restricted range!
4289 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4290 tbl)
4291 (setq p (org-plist-delete p :block))
4292 (setq p (plist-put p :tstart clocktable-start))
4293 (setq p (plist-put p :tend clocktable-end))
4294 (setq p (plist-put p :scope 'agenda))
4295 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4296 (setq filter (or org-agenda-tag-filter-while-redo
4297 (get 'org-agenda-tag-filter :preset-filter))))
4298 (setq p (plist-put p :tags (mapconcat (lambda (x)
4299 (if (string-match "[<>=]" x)
4302 filter ""))))
4303 (setq tbl (apply 'org-clock-get-clocktable p))
4304 (insert tbl)))
4305 (goto-char (point-min))
4306 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4307 (unless (and (pos-visible-in-window-p (point-min))
4308 (pos-visible-in-window-p (point-max)))
4309 (goto-char (1- (point-max)))
4310 (recenter -1)
4311 (if (not (pos-visible-in-window-p (or start-pos 1)))
4312 (progn
4313 (goto-char (or start-pos 1))
4314 (recenter 1))))
4315 (goto-char (or start-pos 1))
4316 (add-text-properties (point-min) (point-max)
4317 `(org-agenda-type agenda
4318 org-last-args (,arg ,start-day ,span)
4319 org-redo-cmd ,org-agenda-redo-command
4320 org-series-cmd ,org-cmd))
4321 (if (eq org-agenda-show-log-scoped 'clockcheck)
4322 (org-agenda-show-clocking-issues))
4323 (org-agenda-finalize)
4324 (setq buffer-read-only t)
4325 (message ""))))
4327 (defun org-agenda-ndays-to-span (n)
4328 "Return a span symbol for a span of N days, or N if none matches."
4329 (cond ((symbolp n) n)
4330 ((= n 1) 'day)
4331 ((= n 7) 'week)
4332 (t n)))
4334 (defun org-agenda-span-to-ndays (span &optional start-day)
4335 "Return ndays from SPAN, possibly starting at START-DAY.
4336 START-DAY is an absolute time value."
4337 (cond ((numberp span) span)
4338 ((eq span 'day) 1)
4339 ((eq span 'week) 7)
4340 ((eq span 'month)
4341 (let ((date (calendar-gregorian-from-absolute start-day)))
4342 (calendar-last-day-of-month (car date) (caddr date))))
4343 ((eq span 'year)
4344 (let ((date (calendar-gregorian-from-absolute start-day)))
4345 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4347 (defun org-agenda-span-name (span)
4348 "Return a SPAN name."
4349 (if (null span)
4351 (if (symbolp span)
4352 (capitalize (symbol-name span))
4353 (format "%d days" span))))
4355 ;;; Agenda word search
4357 (defvar org-agenda-search-history nil)
4359 (defvar org-search-syntax-table nil
4360 "Special syntax table for org-mode search.
4361 In this table, we have single quotes not as word constituents, to
4362 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4364 (defvar org-mode-syntax-table) ; From org.el
4365 (defun org-search-syntax-table ()
4366 (unless org-search-syntax-table
4367 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4368 (modify-syntax-entry ?' "." org-search-syntax-table)
4369 (modify-syntax-entry ?` "." org-search-syntax-table))
4370 org-search-syntax-table)
4372 (defvar org-agenda-last-search-view-search-was-boolean nil)
4374 ;;;###autoload
4375 (defun org-search-view (&optional todo-only string edit-at)
4376 "Show all entries that contain a phrase or words or regular expressions.
4378 With optional prefix argument TODO-ONLY, only consider entries that are
4379 TODO entries. The argument STRING can be used to pass a default search
4380 string into this function. If EDIT-AT is non-nil, it means that the
4381 user should get a chance to edit this string, with cursor at position
4382 EDIT-AT.
4384 The search string can be viewed either as a phrase that should be found as
4385 is, or it can be broken into a number of snippets, each of which must match
4386 in a Boolean way to select an entry. The default depends on the variable
4387 `org-agenda-search-view-always-boolean'.
4388 Even if this is turned off (the default) you can always switch to
4389 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4391 The default is a direct search of the whole phrase, where each space in
4392 the search string can expand to an arbitrary amount of whitespace,
4393 including newlines.
4395 If using a Boolean search, the search string is split on whitespace and
4396 each snippet is searched separately, with logical AND to select an entry.
4397 Words prefixed with a minus must *not* occur in the entry. Words without
4398 a prefix or prefixed with a plus must occur in the entry. Matching is
4399 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4400 match whole words, not parts of a word) if
4401 `org-agenda-search-view-force-full-words' is set (default is nil).
4403 Boolean search snippets enclosed by curly braces are interpreted as
4404 regular expressions that must or (when preceded with \"-\") must not
4405 match in the entry. Snippets enclosed into double quotes will be taken
4406 as a whole, to include whitespace.
4408 - If the search string starts with an asterisk, search only in headlines.
4409 - If (possibly after the leading star) the search string starts with an
4410 exclamation mark, this also means to look at TODO entries only, an effect
4411 that can also be achieved with a prefix argument.
4412 - If (possibly after star and exclamation mark) the search string starts
4413 with a colon, this will mean that the (non-regexp) snippets of the
4414 Boolean search must match as full words.
4416 This command searches the agenda files, and in addition the files listed
4417 in `org-agenda-text-search-extra-files'."
4418 (interactive "P")
4419 (if org-agenda-overriding-arguments
4420 (setq todo-only (car org-agenda-overriding-arguments)
4421 string (nth 1 org-agenda-overriding-arguments)
4422 edit-at (nth 2 org-agenda-overriding-arguments)))
4423 (let* ((props (list 'face nil
4424 'done-face 'org-agenda-done
4425 'org-not-done-regexp org-not-done-regexp
4426 'org-todo-regexp org-todo-regexp
4427 'org-complex-heading-regexp org-complex-heading-regexp
4428 'mouse-face 'highlight
4429 'help-echo (format "mouse-2 or RET jump to location")))
4430 (full-words org-agenda-search-view-force-full-words)
4431 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4432 regexp rtn rtnall files file pos inherited-tags
4433 marker category category-pos level tags c neg re boolean
4434 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4435 (unless (and (not edit-at)
4436 (stringp string)
4437 (string-match "\\S-" string))
4438 (setq string (read-string
4439 (if org-agenda-search-view-always-boolean
4440 "[+-]Word/{Regexp} ...: "
4441 "Phrase or [+-]Word/{Regexp} ...: ")
4442 (cond
4443 ((integerp edit-at) (cons string edit-at))
4444 (edit-at string))
4445 'org-agenda-search-history)))
4446 (catch 'exit
4447 (if org-agenda-sticky
4448 (setq org-agenda-buffer-name
4449 (if (stringp string)
4450 (format "*Org Agenda(%s:%s)*"
4451 (or org-keys (or (and todo-only "S") "s")) string)
4452 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4453 (org-agenda-prepare "SEARCH")
4454 (org-compile-prefix-format 'search)
4455 (org-set-sorting-strategy 'search)
4456 (setq org-agenda-redo-command
4457 (list 'org-search-view (if todo-only t nil)
4458 (list 'if 'current-prefix-arg nil string)))
4459 (setq org-agenda-query-string string)
4460 (if (equal (string-to-char string) ?*)
4461 (setq hdl-only t
4462 words (substring string 1))
4463 (setq words string))
4464 (when (equal (string-to-char words) ?!)
4465 (setq todo-only t
4466 words (substring words 1)))
4467 (when (equal (string-to-char words) ?:)
4468 (setq full-words t
4469 words (substring words 1)))
4470 (if (or org-agenda-search-view-always-boolean
4471 (member (string-to-char words) '(?- ?+ ?\{)))
4472 (setq boolean t))
4473 (setq words (org-split-string words))
4474 (let (www w)
4475 (while (setq w (pop words))
4476 (while (and (string-match "\\\\\\'" w) words)
4477 (setq w (concat (substring w 0 -1) " " (pop words))))
4478 (push w www))
4479 (setq words (nreverse www) www nil)
4480 (while (setq w (pop words))
4481 (when (and (string-match "\\`[-+]?{" w)
4482 (not (string-match "}\\'" w)))
4483 (while (and words (not (string-match "}\\'" (car words))))
4484 (setq w (concat w " " (pop words))))
4485 (setq w (concat w " " (pop words))))
4486 (push w www))
4487 (setq words (nreverse www)))
4488 (setq org-agenda-last-search-view-search-was-boolean boolean)
4489 (when boolean
4490 (let (wds w)
4491 (while (setq w (pop words))
4492 (if (or (equal (substring w 0 1) "\"")
4493 (and (> (length w) 1)
4494 (member (substring w 0 1) '("+" "-"))
4495 (equal (substring w 1 2) "\"")))
4496 (while (and words (not (equal (substring w -1) "\"")))
4497 (setq w (concat w " " (pop words)))))
4498 (and (string-match "\\`\\([-+]?\\)\"" w)
4499 (setq w (replace-match "\\1" nil nil w)))
4500 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4501 (push w wds))
4502 (setq words (nreverse wds))))
4503 (if boolean
4504 (mapc (lambda (w)
4505 (setq c (string-to-char w))
4506 (if (equal c ?-)
4507 (setq neg t w (substring w 1))
4508 (if (equal c ?+)
4509 (setq neg nil w (substring w 1))
4510 (setq neg nil)))
4511 (if (string-match "\\`{.*}\\'" w)
4512 (setq re (substring w 1 -1))
4513 (if full-words
4514 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4515 (setq re (regexp-quote (downcase w)))))
4516 (if neg (push re regexps-) (push re regexps+)))
4517 words)
4518 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4519 regexps+))
4520 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4521 (if (not regexps+)
4522 (setq regexp org-outline-regexp-bol)
4523 (setq regexp (pop regexps+))
4524 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4525 regexp))))
4526 (setq files (org-agenda-files nil 'ifmode))
4527 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4528 (pop org-agenda-text-search-extra-files)
4529 (setq files (org-add-archive-files files)))
4530 (setq files (append files org-agenda-text-search-extra-files)
4531 rtnall nil)
4532 (while (setq file (pop files))
4533 (setq ee nil)
4534 (catch 'nextfile
4535 (org-check-agenda-file file)
4536 (setq buffer (if (file-exists-p file)
4537 (org-get-agenda-file-buffer file)
4538 (error "No such file %s" file)))
4539 (if (not buffer)
4540 ;; If file does not exist, make sure an error message is sent
4541 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4542 file))))
4543 (with-current-buffer buffer
4544 (with-syntax-table (org-search-syntax-table)
4545 (unless (derived-mode-p 'org-mode)
4546 (error "Agenda file %s is not in `org-mode'" file))
4547 (let ((case-fold-search t))
4548 (save-excursion
4549 (save-restriction
4550 (if org-agenda-restrict
4551 (narrow-to-region org-agenda-restrict-begin
4552 org-agenda-restrict-end)
4553 (widen))
4554 (goto-char (point-min))
4555 (unless (or (org-at-heading-p)
4556 (outline-next-heading))
4557 (throw 'nextfile t))
4558 (goto-char (max (point-min) (1- (point))))
4559 (while (re-search-forward regexp nil t)
4560 (org-back-to-heading t)
4561 (while (and org-agenda-search-view-max-outline-level
4562 (> (org-reduced-level (org-outline-level))
4563 org-agenda-search-view-max-outline-level)
4564 (forward-line -1)
4565 (outline-back-to-heading t)))
4566 (skip-chars-forward "* ")
4567 (setq beg (point-at-bol)
4568 beg1 (point)
4569 end (progn
4570 (outline-next-heading)
4571 (while (and org-agenda-search-view-max-outline-level
4572 (> (org-reduced-level (org-outline-level))
4573 org-agenda-search-view-max-outline-level)
4574 (forward-line 1)
4575 (outline-next-heading)))
4576 (point)))
4578 (catch :skip
4579 (goto-char beg)
4580 (org-agenda-skip)
4581 (setq str (buffer-substring-no-properties
4582 (point-at-bol)
4583 (if hdl-only (point-at-eol) end)))
4584 (mapc (lambda (wr) (when (string-match wr str)
4585 (goto-char (1- end))
4586 (throw :skip t)))
4587 regexps-)
4588 (mapc (lambda (wr) (unless (string-match wr str)
4589 (goto-char (1- end))
4590 (throw :skip t)))
4591 (if todo-only
4592 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4593 regexps+)
4594 regexps+))
4595 (goto-char beg)
4596 (setq marker (org-agenda-new-marker (point))
4597 category (org-get-category)
4598 level (make-string (org-reduced-level (org-outline-level)) ? )
4599 category-pos (get-text-property (point) 'org-category-position)
4600 inherited-tags
4601 (or (eq org-agenda-show-inherited-tags 'always)
4602 (and (listp org-agenda-show-inherited-tags)
4603 (memq 'todo org-agenda-show-inherited-tags))
4604 (and (eq org-agenda-show-inherited-tags t)
4605 (or (eq org-agenda-use-tag-inheritance t)
4606 (memq 'todo org-agenda-use-tag-inheritance))))
4607 tags (org-get-tags-at nil (not inherited-tags))
4608 txt (org-agenda-format-item
4610 (buffer-substring-no-properties
4611 beg1 (point-at-eol))
4612 level category tags t))
4613 (org-add-props txt props
4614 'org-marker marker 'org-hd-marker marker
4615 'org-todo-regexp org-todo-regexp
4616 'level level
4617 'org-complex-heading-regexp org-complex-heading-regexp
4618 'priority 1000 'org-category category
4619 'org-category-position category-pos
4620 'type "search")
4621 (push txt ee)
4622 (goto-char (1- end))))))))))
4623 (setq rtn (nreverse ee))
4624 (setq rtnall (append rtnall rtn)))
4625 (if org-agenda-overriding-header
4626 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4627 nil 'face 'org-agenda-structure) "\n")
4628 (insert "Search words: ")
4629 (add-text-properties (point-min) (1- (point))
4630 (list 'face 'org-agenda-structure))
4631 (setq pos (point))
4632 (insert string "\n")
4633 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4634 (setq pos (point))
4635 (unless org-agenda-multi
4636 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4637 (add-text-properties pos (1- (point))
4638 (list 'face 'org-agenda-structure))))
4639 (org-agenda-mark-header-line (point-min))
4640 (when rtnall
4641 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4642 (goto-char (point-min))
4643 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4644 (add-text-properties (point-min) (point-max)
4645 `(org-agenda-type search
4646 org-last-args (,todo-only ,string ,edit-at)
4647 org-redo-cmd ,org-agenda-redo-command
4648 org-series-cmd ,org-cmd))
4649 (org-agenda-finalize)
4650 (setq buffer-read-only t))))
4652 ;;; Agenda TODO list
4654 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4655 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4656 (concat
4657 (if (or (equal keywords "ALL") (not keywords))
4658 (propertize "ALL" 'face 'warning)
4659 (mapconcat
4660 (lambda (kw)
4661 (propertize kw 'face (org-get-todo-face kw)))
4662 (org-split-string keywords "|")
4663 "|"))
4664 "\n"))
4666 (defvar org-select-this-todo-keyword nil)
4667 (defvar org-last-arg nil)
4669 ;;;###autoload
4670 (defun org-todo-list (&optional arg)
4671 "Show all (not done) TODO entries from all agenda file in a single list.
4672 The prefix arg can be used to select a specific TODO keyword and limit
4673 the list to these. When using \\[universal-argument], you will be prompted
4674 for a keyword. A numeric prefix directly selects the Nth keyword in
4675 `org-todo-keywords-1'."
4676 (interactive "P")
4677 (if org-agenda-overriding-arguments
4678 (setq arg org-agenda-overriding-arguments))
4679 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4680 (let* ((today (org-today))
4681 (date (calendar-gregorian-from-absolute today))
4682 (kwds org-todo-keywords-for-agenda)
4683 (completion-ignore-case t)
4684 (org-select-this-todo-keyword
4685 (if (stringp arg) arg
4686 (and arg (integerp arg) (> arg 0)
4687 (nth (1- arg) kwds))))
4688 rtn rtnall files file pos)
4689 (when (equal arg '(4))
4690 (setq org-select-this-todo-keyword
4691 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4692 (mapcar 'list kwds) nil nil)))
4693 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4694 (catch 'exit
4695 (if org-agenda-sticky
4696 (setq org-agenda-buffer-name
4697 (if (stringp org-select-this-todo-keyword)
4698 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4699 org-select-this-todo-keyword)
4700 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4701 (org-agenda-prepare "TODO")
4702 (org-compile-prefix-format 'todo)
4703 (org-set-sorting-strategy 'todo)
4704 (setq org-agenda-redo-command
4705 `(org-todo-list (or (and (numberp current-prefix-arg)
4706 current-prefix-arg)
4707 ,org-select-this-todo-keyword
4708 current-prefix-arg ,arg)))
4709 (setq files (org-agenda-files nil 'ifmode)
4710 rtnall nil)
4711 (while (setq file (pop files))
4712 (catch 'nextfile
4713 (org-check-agenda-file file)
4714 (setq rtn (org-agenda-get-day-entries file date :todo))
4715 (setq rtnall (append rtnall rtn))))
4716 (if org-agenda-overriding-header
4717 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4718 nil 'face 'org-agenda-structure) "\n")
4719 (insert "Global list of TODO items of type: ")
4720 (add-text-properties (point-min) (1- (point))
4721 (list 'face 'org-agenda-structure
4722 'short-heading
4723 (concat "ToDo: "
4724 (or org-select-this-todo-keyword "ALL"))))
4725 (org-agenda-mark-header-line (point-min))
4726 (insert (org-agenda-propertize-selected-todo-keywords
4727 org-select-this-todo-keyword))
4728 (setq pos (point))
4729 (unless org-agenda-multi
4730 (insert "Available with `N r': (0)[ALL]")
4731 (let ((n 0) s)
4732 (mapc (lambda (x)
4733 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4734 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4735 (insert "\n "))
4736 (insert " " s))
4737 kwds))
4738 (insert "\n"))
4739 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4740 (org-agenda-mark-header-line (point-min))
4741 (when rtnall
4742 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4743 (goto-char (point-min))
4744 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4745 (add-text-properties (point-min) (point-max)
4746 `(org-agenda-type todo
4747 org-last-args ,arg
4748 org-redo-cmd ,org-agenda-redo-command
4749 org-series-cmd ,org-cmd))
4750 (org-agenda-finalize)
4751 (setq buffer-read-only t))))
4753 ;;; Agenda tags match
4755 ;;;###autoload
4756 (defun org-tags-view (&optional todo-only match)
4757 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4758 The prefix arg TODO-ONLY limits the search to TODO entries."
4759 (interactive "P")
4760 (if org-agenda-overriding-arguments
4761 (setq todo-only (car org-agenda-overriding-arguments)
4762 match (nth 1 org-agenda-overriding-arguments)))
4763 (let* ((org-tags-match-list-sublevels
4764 org-tags-match-list-sublevels)
4765 (completion-ignore-case t)
4766 rtn rtnall files file pos matcher
4767 buffer)
4768 (when (and (stringp match) (not (string-match "\\S-" match)))
4769 (setq match nil))
4770 (catch 'exit
4771 (if org-agenda-sticky
4772 (setq org-agenda-buffer-name
4773 (if (stringp match)
4774 (format "*Org Agenda(%s:%s)*"
4775 (or org-keys (or (and todo-only "M") "m")) match)
4776 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4777 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4778 ;; expanding tags within `org-make-tags-matcher'
4779 (org-agenda-prepare (concat "TAGS " match))
4780 (setq matcher (org-make-tags-matcher match)
4781 match (car matcher) matcher (cdr matcher))
4782 (org-compile-prefix-format 'tags)
4783 (org-set-sorting-strategy 'tags)
4784 (setq org-agenda-query-string match)
4785 (setq org-agenda-redo-command
4786 (list 'org-tags-view `(quote ,todo-only)
4787 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4788 (setq files (org-agenda-files nil 'ifmode)
4789 rtnall nil)
4790 (while (setq file (pop files))
4791 (catch 'nextfile
4792 (org-check-agenda-file file)
4793 (setq buffer (if (file-exists-p file)
4794 (org-get-agenda-file-buffer file)
4795 (error "No such file %s" file)))
4796 (if (not buffer)
4797 ;; If file does not exist, error message to agenda
4798 (setq rtn (list
4799 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4800 rtnall (append rtnall rtn))
4801 (with-current-buffer buffer
4802 (unless (derived-mode-p 'org-mode)
4803 (error "Agenda file %s is not in `org-mode'" file))
4804 (save-excursion
4805 (save-restriction
4806 (if org-agenda-restrict
4807 (narrow-to-region org-agenda-restrict-begin
4808 org-agenda-restrict-end)
4809 (widen))
4810 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4811 (setq rtnall (append rtnall rtn))))))))
4812 (if org-agenda-overriding-header
4813 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4814 nil 'face 'org-agenda-structure) "\n")
4815 (insert "Headlines with TAGS match: ")
4816 (add-text-properties (point-min) (1- (point))
4817 (list 'face 'org-agenda-structure
4818 'short-heading
4819 (concat "Match: " match)))
4820 (setq pos (point))
4821 (insert match "\n")
4822 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4823 (setq pos (point))
4824 (unless org-agenda-multi
4825 (insert "Press `C-u r' to search again with new search string\n"))
4826 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4827 (org-agenda-mark-header-line (point-min))
4828 (when rtnall
4829 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4830 (goto-char (point-min))
4831 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4832 (add-text-properties (point-min) (point-max)
4833 `(org-agenda-type tags
4834 org-last-args (,todo-only ,match)
4835 org-redo-cmd ,org-agenda-redo-command
4836 org-series-cmd ,org-cmd))
4837 (org-agenda-finalize)
4838 (setq buffer-read-only t))))
4840 ;;; Agenda Finding stuck projects
4842 (defvar org-agenda-skip-regexp nil
4843 "Regular expression used in skipping subtrees for the agenda.
4844 This is basically a temporary global variable that can be set and then
4845 used by user-defined selections using `org-agenda-skip-function'.")
4847 (defvar org-agenda-overriding-header nil
4848 "When set during agenda, todo and tags searches it replaces the header.
4849 This variable should not be set directly, but custom commands can bind it
4850 in the options section.")
4852 (defun org-agenda-skip-entry-when-regexp-matches ()
4853 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4854 If yes, it returns the end position of this entry, causing agenda commands
4855 to skip the entry but continuing the search in the subtree. This is a
4856 function that can be put into `org-agenda-skip-function' for the duration
4857 of a command."
4858 (let ((end (save-excursion (org-end-of-subtree t)))
4859 skip)
4860 (save-excursion
4861 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4862 (and skip end)))
4864 (defun org-agenda-skip-subtree-when-regexp-matches ()
4865 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4866 If yes, it returns the end position of this tree, causing agenda commands
4867 to skip this subtree. This is a function that can be put into
4868 `org-agenda-skip-function' for the duration of a command."
4869 (let ((end (save-excursion (org-end-of-subtree t)))
4870 skip)
4871 (save-excursion
4872 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4873 (and skip end)))
4875 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4876 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4877 If yes, it returns the end position of the current entry (NOT the tree),
4878 causing agenda commands to skip the entry but continuing the search in
4879 the subtree. This is a function that can be put into
4880 `org-agenda-skip-function' for the duration of a command. An important
4881 use of this function is for the stuck project list."
4882 (let ((end (save-excursion (org-end-of-subtree t)))
4883 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4884 skip)
4885 (save-excursion
4886 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4887 (and skip entry-end)))
4889 (defun org-agenda-skip-entry-if (&rest conditions)
4890 "Skip entry if any of CONDITIONS is true.
4891 See `org-agenda-skip-if' for details."
4892 (org-agenda-skip-if nil conditions))
4894 (defun org-agenda-skip-subtree-if (&rest conditions)
4895 "Skip entry if any of CONDITIONS is true.
4896 See `org-agenda-skip-if' for details."
4897 (org-agenda-skip-if t conditions))
4899 (defun org-agenda-skip-if (subtree conditions)
4900 "Checks current entity for CONDITIONS.
4901 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4902 the entry (i.e. the text before the next heading) is checked.
4904 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4905 from different tests. Valid conditions are:
4907 scheduled Check if there is a scheduled cookie
4908 notscheduled Check if there is no scheduled cookie
4909 deadline Check if there is a deadline
4910 notdeadline Check if there is no deadline
4911 timestamp Check if there is a timestamp (also deadline or scheduled)
4912 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4913 regexp Check if regexp matches
4914 notregexp Check if regexp does not match.
4915 todo Check if TODO keyword matches
4916 nottodo Check if TODO keyword does not match
4918 The regexp is taken from the conditions list, it must come right after
4919 the `regexp' or `notregexp' element.
4921 `todo' and `nottodo' accept as an argument a list of todo
4922 keywords, which may include \"*\" to match any todo keyword.
4924 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4926 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4928 Instead of a list, a keyword class may be given. For example:
4930 (org-agenda-skip-entry-if 'nottodo 'done)
4932 would skip entries that haven't been marked with any of \"DONE\"
4933 keywords. Possible classes are: `todo', `done', `any'.
4935 If any of these conditions is met, this function returns the end point of
4936 the entity, causing the search to continue from there. This is a function
4937 that can be put into `org-agenda-skip-function' for the duration of a command."
4938 (let (beg end m)
4939 (org-back-to-heading t)
4940 (setq beg (point)
4941 end (if subtree
4942 (progn (org-end-of-subtree t) (point))
4943 (progn (outline-next-heading) (1- (point)))))
4944 (goto-char beg)
4945 (and
4947 (and (memq 'scheduled conditions)
4948 (re-search-forward org-scheduled-time-regexp end t))
4949 (and (memq 'notscheduled conditions)
4950 (not (re-search-forward org-scheduled-time-regexp end t)))
4951 (and (memq 'deadline conditions)
4952 (re-search-forward org-deadline-time-regexp end t))
4953 (and (memq 'notdeadline conditions)
4954 (not (re-search-forward org-deadline-time-regexp end t)))
4955 (and (memq 'timestamp conditions)
4956 (re-search-forward org-ts-regexp end t))
4957 (and (memq 'nottimestamp conditions)
4958 (not (re-search-forward org-ts-regexp end t)))
4959 (and (setq m (memq 'regexp conditions))
4960 (stringp (nth 1 m))
4961 (re-search-forward (nth 1 m) end t))
4962 (and (setq m (memq 'notregexp conditions))
4963 (stringp (nth 1 m))
4964 (not (re-search-forward (nth 1 m) end t)))
4965 (and (or
4966 (setq m (memq 'nottodo conditions))
4967 (setq m (memq 'todo-unblocked conditions))
4968 (setq m (memq 'nottodo-unblocked conditions))
4969 (setq m (memq 'todo conditions)))
4970 (org-agenda-skip-if-todo m end)))
4971 end)))
4973 (defun org-agenda-skip-if-todo (args end)
4974 "Helper function for `org-agenda-skip-if', do not use it directly.
4975 ARGS is a list with first element either `todo', `nottodo',
4976 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4977 a list of TODO keywords, or a state symbol `todo' or `done' or
4978 `any'."
4979 (let ((kw (car args))
4980 (arg (cadr args))
4981 todo-wds todo-re)
4982 (setq todo-wds
4983 (org-uniquify
4984 (cond
4985 ((listp arg) ;; list of keywords
4986 (if (member "*" arg)
4987 (mapcar 'substring-no-properties org-todo-keywords-1)
4988 arg))
4989 ((symbolp arg) ;; keyword class name
4990 (cond
4991 ((eq arg 'todo)
4992 (org-delete-all org-done-keywords
4993 (mapcar 'substring-no-properties
4994 org-todo-keywords-1)))
4995 ((eq arg 'done) org-done-keywords)
4996 ((eq arg 'any)
4997 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4998 (setq todo-re
4999 (concat "^\\*+[ \t]+\\<\\("
5000 (mapconcat 'identity todo-wds "\\|")
5001 "\\)\\>"))
5002 (cond
5003 ((eq kw 'todo) (re-search-forward todo-re end t))
5004 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5005 ((eq kw 'todo-unblocked)
5006 (catch 'unblocked
5007 (while (re-search-forward todo-re end t)
5008 (or (org-entry-blocked-p) (throw 'unblocked t)))
5009 nil))
5010 ((eq kw 'nottodo-unblocked)
5011 (catch 'unblocked
5012 (while (re-search-forward todo-re end t)
5013 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5017 ;;;###autoload
5018 (defun org-agenda-list-stuck-projects (&rest ignore)
5019 "Create agenda view for projects that are stuck.
5020 Stuck projects are project that have no next actions. For the definitions
5021 of what a project is and how to check if it stuck, customize the variable
5022 `org-stuck-projects'."
5023 (interactive)
5024 (let* ((org-agenda-skip-function
5025 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5026 ;; We could have used org-agenda-skip-if here.
5027 (org-agenda-overriding-header
5028 (or org-agenda-overriding-header "List of stuck projects: "))
5029 (matcher (nth 0 org-stuck-projects))
5030 (todo (nth 1 org-stuck-projects))
5031 (todo-wds (if (member "*" todo)
5032 (progn
5033 (org-agenda-prepare-buffers (org-agenda-files
5034 nil 'ifmode))
5035 (org-delete-all
5036 org-done-keywords-for-agenda
5037 (copy-sequence org-todo-keywords-for-agenda)))
5038 todo))
5039 (todo-re (concat "^\\*+[ \t]+\\("
5040 (mapconcat 'identity todo-wds "\\|")
5041 "\\)\\>"))
5042 (tags (nth 2 org-stuck-projects))
5043 (tags-re (if (member "*" tags)
5044 (concat org-outline-regexp-bol
5045 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5046 (if tags
5047 (concat org-outline-regexp-bol
5048 ".*:\\("
5049 (mapconcat 'identity tags "\\|")
5050 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5051 (gen-re (nth 3 org-stuck-projects))
5052 (re-list
5053 (delq nil
5054 (list
5055 (if todo todo-re)
5056 (if tags tags-re)
5057 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5058 gen-re)))))
5059 (setq org-agenda-skip-regexp
5060 (if re-list
5061 (mapconcat 'identity re-list "\\|")
5062 (error "No information how to identify unstuck projects")))
5063 (org-tags-view nil matcher)
5064 (with-current-buffer org-agenda-buffer-name
5065 (setq org-agenda-redo-command
5066 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5068 ;;; Diary integration
5070 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5071 (defvar diary-list-entries-hook)
5072 (defvar diary-time-regexp)
5073 (defun org-get-entries-from-diary (date)
5074 "Get the (Emacs Calendar) diary entries for DATE."
5075 (require 'diary-lib)
5076 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5077 (diary-display-hook '(fancy-diary-display))
5078 (diary-display-function 'fancy-diary-display)
5079 (pop-up-frames nil)
5080 (diary-list-entries-hook
5081 (cons 'org-diary-default-entry diary-list-entries-hook))
5082 (diary-file-name-prefix nil) ; turn this feature off
5083 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5084 entries
5085 (org-disable-agenda-to-diary t))
5086 (save-excursion
5087 (save-window-excursion
5088 (funcall (if (fboundp 'diary-list-entries)
5089 'diary-list-entries 'list-diary-entries)
5090 date 1)))
5091 (if (not (get-buffer diary-fancy-buffer))
5092 (setq entries nil)
5093 (with-current-buffer diary-fancy-buffer
5094 (setq buffer-read-only nil)
5095 (if (zerop (buffer-size))
5096 ;; No entries
5097 (setq entries nil)
5098 ;; Omit the date and other unnecessary stuff
5099 (org-agenda-cleanup-fancy-diary)
5100 ;; Add prefix to each line and extend the text properties
5101 (if (zerop (buffer-size))
5102 (setq entries nil)
5103 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5104 (setq entries
5105 (with-temp-buffer
5106 (insert entries) (goto-char (point-min))
5107 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5108 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5109 (replace-match (concat "; " (match-string 1)))))
5110 (buffer-string)))))
5111 (set-buffer-modified-p nil)
5112 (kill-buffer diary-fancy-buffer)))
5113 (when entries
5114 (setq entries (org-split-string entries "\n"))
5115 (setq entries
5116 (mapcar
5117 (lambda (x)
5118 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5119 ;; Extend the text properties to the beginning of the line
5120 (org-add-props x (text-properties-at (1- (length x)) x)
5121 'type "diary" 'date date 'face 'org-agenda-diary))
5122 entries)))))
5124 (defvar org-agenda-cleanup-fancy-diary-hook nil
5125 "Hook run when the fancy diary buffer is cleaned up.")
5127 (defun org-agenda-cleanup-fancy-diary ()
5128 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5129 This gets rid of the date, the underline under the date, and
5130 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5131 date. It also removes lines that contain only whitespace."
5132 (goto-char (point-min))
5133 (if (looking-at ".*?:[ \t]*")
5134 (progn
5135 (replace-match "")
5136 (re-search-forward "\n=+$" nil t)
5137 (replace-match "")
5138 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5139 (re-search-forward "\n=+$" nil t)
5140 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5141 (goto-char (point-min))
5142 (while (re-search-forward "^ +\n" nil t)
5143 (replace-match ""))
5144 (goto-char (point-min))
5145 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5146 (replace-match ""))
5147 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5149 ;; Make sure entries from the diary have the right text properties.
5150 (eval-after-load "diary-lib"
5151 '(if (boundp 'diary-modify-entry-list-string-function)
5152 ;; We can rely on the hook, nothing to do
5154 ;; Hook not available, must use advice to make this work
5155 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5156 "Make the position visible."
5157 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5158 (stringp string)
5159 buffer-file-name)
5160 (setq string (org-modify-diary-entry-string string))))))
5162 (defun org-modify-diary-entry-string (string)
5163 "Add text properties to string, allowing org-mode to act on it."
5164 (org-add-props string nil
5165 'mouse-face 'highlight
5166 'help-echo (if buffer-file-name
5167 (format "mouse-2 or RET jump to diary file %s"
5168 (abbreviate-file-name buffer-file-name))
5170 'org-agenda-diary-link t
5171 'org-marker (org-agenda-new-marker (point-at-bol))))
5173 (defun org-diary-default-entry ()
5174 "Add a dummy entry to the diary.
5175 Needed to avoid empty dates which mess up holiday display."
5176 ;; Catch the error if dealing with the new add-to-diary-alist
5177 (when org-disable-agenda-to-diary
5178 (condition-case nil
5179 (org-add-to-diary-list original-date "Org-mode dummy" "")
5180 (error
5181 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5183 (defun org-add-to-diary-list (&rest args)
5184 (if (fboundp 'diary-add-to-list)
5185 (apply 'diary-add-to-list args)
5186 (apply 'add-to-diary-list args)))
5188 (defvar org-diary-last-run-time nil)
5190 ;;;###autoload
5191 (defun org-diary (&rest args)
5192 "Return diary information from org files.
5193 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5194 It accesses org files and extracts information from those files to be
5195 listed in the diary. The function accepts arguments specifying what
5196 items should be listed. For a list of arguments allowed here, see the
5197 variable `org-agenda-entry-types'.
5199 The call in the diary file should look like this:
5201 &%%(org-diary) ~/path/to/some/orgfile.org
5203 Use a separate line for each org file to check. Or, if you omit the file name,
5204 all files listed in `org-agenda-files' will be checked automatically:
5206 &%%(org-diary)
5208 If you don't give any arguments (as in the example above), the default value
5209 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5210 So the example above may also be written as
5212 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5214 The function expects the lisp variables `entry' and `date' to be provided
5215 by the caller, because this is how the calendar works. Don't use this
5216 function from a program - use `org-agenda-get-day-entries' instead."
5217 (when (> (- (org-float-time)
5218 org-agenda-last-marker-time)
5220 ;; I am not sure if this works with sticky agendas, because the marker
5221 ;; list is then no longer a global variable.
5222 (org-agenda-reset-markers))
5223 (org-compile-prefix-format 'agenda)
5224 (org-set-sorting-strategy 'agenda)
5225 (setq args (or args org-agenda-entry-types))
5226 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5227 (list entry)
5228 (org-agenda-files t)))
5229 (time (org-float-time))
5230 file rtn results)
5231 (when (or (not org-diary-last-run-time)
5232 (> (- time
5233 org-diary-last-run-time)
5235 (org-agenda-prepare-buffers files))
5236 (setq org-diary-last-run-time time)
5237 ;; If this is called during org-agenda, don't return any entries to
5238 ;; the calendar. Org Agenda will list these entries itself.
5239 (if org-disable-agenda-to-diary (setq files nil))
5240 (while (setq file (pop files))
5241 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5242 (setq results (append results rtn)))
5243 (when results
5244 (setq results
5245 (mapcar (lambda (i) (replace-regexp-in-string
5246 org-bracket-link-regexp "\\3" i)) results))
5247 (concat (org-agenda-finalize-entries results) "\n"))))
5249 ;;; Agenda entry finders
5251 (defun org-agenda-get-day-entries (file date &rest args)
5252 "Does the work for `org-diary' and `org-agenda'.
5253 FILE is the path to a file to be checked for entries. DATE is date like
5254 the one returned by `calendar-current-date'. ARGS are symbols indicating
5255 which kind of entries should be extracted. For details about these, see
5256 the documentation of `org-diary'."
5257 (setq args (or args org-agenda-entry-types))
5258 (let* ((org-startup-folded nil)
5259 (org-startup-align-all-tables nil)
5260 (buffer (if (file-exists-p file)
5261 (org-get-agenda-file-buffer file)
5262 (error "No such file %s" file)))
5263 arg results rtn deadline-results)
5264 (if (not buffer)
5265 ;; If file does not exist, make sure an error message ends up in diary
5266 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5267 (with-current-buffer buffer
5268 (unless (derived-mode-p 'org-mode)
5269 (error "Agenda file %s is not in `org-mode'" file))
5270 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5271 (let ((case-fold-search nil))
5272 (save-excursion
5273 (save-restriction
5274 (if org-agenda-restrict
5275 (narrow-to-region org-agenda-restrict-begin
5276 org-agenda-restrict-end)
5277 (widen))
5278 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5279 (while (setq arg (pop args))
5280 (cond
5281 ((and (eq arg :todo)
5282 (equal date (calendar-gregorian-from-absolute
5283 (org-today))))
5284 (setq rtn (org-agenda-get-todos))
5285 (setq results (append results rtn)))
5286 ((eq arg :timestamp)
5287 (setq rtn (org-agenda-get-blocks))
5288 (setq results (append results rtn))
5289 (setq rtn (org-agenda-get-timestamps deadline-results))
5290 (setq results (append results rtn)))
5291 ((eq arg :sexp)
5292 (setq rtn (org-agenda-get-sexps))
5293 (setq results (append results rtn)))
5294 ((eq arg :scheduled)
5295 (setq rtn (org-agenda-get-scheduled deadline-results))
5296 (setq results (append results rtn)))
5297 ((eq arg :scheduled*)
5298 (setq rtn (org-agenda-get-scheduled deadline-results t))
5299 (setq results (append results rtn)))
5300 ((eq arg :closed)
5301 (setq rtn (org-agenda-get-progress))
5302 (setq results (append results rtn)))
5303 ((eq arg :deadline)
5304 (setq rtn (org-agenda-get-deadlines))
5305 (setq deadline-results (copy-sequence rtn))
5306 (setq results (append results rtn)))
5307 ((eq arg :deadline*)
5308 (setq rtn (org-agenda-get-deadlines t))
5309 (setq deadline-results (copy-sequence rtn))
5310 (setq results (append results rtn))))))))
5311 results))))
5313 (defsubst org-em (x y list)
5314 "Is X or Y a member of LIST?"
5315 (or (memq x list) (memq y list)))
5317 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5318 (defvar org-agenda-sorting-strategy-selected nil)
5320 (defun org-agenda-get-todos ()
5321 "Return the TODO information for agenda display."
5322 (let* ((props (list 'face nil
5323 'done-face 'org-agenda-done
5324 'org-not-done-regexp org-not-done-regexp
5325 'org-todo-regexp org-todo-regexp
5326 'org-complex-heading-regexp org-complex-heading-regexp
5327 'mouse-face 'highlight
5328 'help-echo
5329 (format "mouse-2 or RET jump to org file %s"
5330 (abbreviate-file-name buffer-file-name))))
5331 (regexp (format org-heading-keyword-regexp-format
5332 (cond
5333 ((and org-select-this-todo-keyword
5334 (equal org-select-this-todo-keyword "*"))
5335 org-todo-regexp)
5336 (org-select-this-todo-keyword
5337 (concat "\\("
5338 (mapconcat 'identity
5339 (org-split-string
5340 org-select-this-todo-keyword
5341 "|")
5342 "\\|") "\\)"))
5343 (t org-not-done-regexp))))
5344 marker priority category category-pos level tags todo-state ts-date ts-date-type
5345 ee txt beg end inherited-tags todo-state-end-pos)
5346 (goto-char (point-min))
5347 (while (re-search-forward regexp nil t)
5348 (catch :skip
5349 (save-match-data
5350 (beginning-of-line)
5351 (org-agenda-skip)
5352 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5353 (unless (and (setq todo-state (org-get-todo-state))
5354 (setq todo-state-end-pos (match-end 2)))
5355 (goto-char end)
5356 (throw :skip nil))
5357 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5358 (goto-char (1+ beg))
5359 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5360 (throw :skip nil)))
5361 (goto-char (match-beginning 2))
5362 (setq marker (org-agenda-new-marker (match-beginning 0))
5363 category (org-get-category)
5364 ts-date (let (ts)
5365 (save-match-data
5366 (cond ((org-em 'scheduled-up 'scheduled-down
5367 org-agenda-sorting-strategy-selected)
5368 (setq ts (org-entry-get (point) "SCHEDULED")
5369 ts-date-type " scheduled"))
5370 ((org-em 'deadline-up 'deadline-down
5371 org-agenda-sorting-strategy-selected)
5372 (setq ts (org-entry-get (point) "DEADLINE")
5373 ts-date-type " deadline"))
5374 ((org-em 'ts-up 'ts-down
5375 org-agenda-sorting-strategy-selected)
5376 (setq ts (org-entry-get (point) "TIMESTAMP")
5377 ts-date-type " timestamp"))
5378 ((org-em 'tsia-up 'tsia-down
5379 org-agenda-sorting-strategy-selected)
5380 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5381 ts-date-type " timestamp_ia"))
5382 ((org-em 'timestamp-up 'timestamp-down
5383 org-agenda-sorting-strategy-selected)
5384 (setq ts (or (org-entry-get (point) "SCHEDULED")
5385 (org-entry-get (point) "DEADLINE")
5386 (org-entry-get (point) "TIMESTAMP")
5387 (org-entry-get (point) "TIMESTAMP_IA"))
5388 ts-date-type ""))
5389 (t (setq ts-date-type "")))
5390 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5391 category-pos (get-text-property (point) 'org-category-position)
5392 txt (org-trim
5393 (buffer-substring (match-beginning 2) (match-end 0)))
5394 inherited-tags
5395 (or (eq org-agenda-show-inherited-tags 'always)
5396 (and (listp org-agenda-show-inherited-tags)
5397 (memq 'todo org-agenda-show-inherited-tags))
5398 (and (eq org-agenda-show-inherited-tags t)
5399 (or (eq org-agenda-use-tag-inheritance t)
5400 (memq 'todo org-agenda-use-tag-inheritance))))
5401 tags (org-get-tags-at nil (not inherited-tags))
5402 level (make-string (org-reduced-level (org-outline-level)) ? )
5403 txt (org-agenda-format-item "" txt level category tags t)
5404 priority (1+ (org-get-priority txt)))
5405 (org-add-props txt props
5406 'org-marker marker 'org-hd-marker marker
5407 'priority priority 'org-category category
5408 'level level
5409 'ts-date ts-date
5410 'org-category-position category-pos
5411 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5412 (push txt ee)
5413 (if org-agenda-todo-list-sublevels
5414 (goto-char todo-state-end-pos)
5415 (org-end-of-subtree 'invisible))))
5416 (nreverse ee)))
5418 (defun org-agenda-todo-custom-ignore-p (time n)
5419 "Check whether timestamp is farther away than n number of days.
5420 This function is invoked if `org-agenda-todo-ignore-deadlines',
5421 `org-agenda-todo-ignore-scheduled' or
5422 `org-agenda-todo-ignore-timestamp' is set to an integer."
5423 (let ((days (org-time-stamp-to-now
5424 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5425 (if (>= n 0)
5426 (>= days n)
5427 (<= days n))))
5429 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5430 (&optional end)
5431 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5432 (when (or org-agenda-todo-ignore-with-date
5433 org-agenda-todo-ignore-scheduled
5434 org-agenda-todo-ignore-deadlines
5435 org-agenda-todo-ignore-timestamp)
5436 (setq end (or end (save-excursion (outline-next-heading) (point))))
5437 (save-excursion
5438 (or (and org-agenda-todo-ignore-with-date
5439 (re-search-forward org-ts-regexp end t))
5440 (and org-agenda-todo-ignore-scheduled
5441 (re-search-forward org-scheduled-time-regexp end t)
5442 (cond
5443 ((eq org-agenda-todo-ignore-scheduled 'future)
5444 (> (org-time-stamp-to-now
5445 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5446 ((eq org-agenda-todo-ignore-scheduled 'past)
5447 (<= (org-time-stamp-to-now
5448 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5449 ((numberp org-agenda-todo-ignore-scheduled)
5450 (org-agenda-todo-custom-ignore-p
5451 (match-string 1) org-agenda-todo-ignore-scheduled))
5452 (t)))
5453 (and org-agenda-todo-ignore-deadlines
5454 (re-search-forward org-deadline-time-regexp end t)
5455 (cond
5456 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5457 ((eq org-agenda-todo-ignore-deadlines 'far)
5458 (not (org-deadline-close (match-string 1))))
5459 ((eq org-agenda-todo-ignore-deadlines 'future)
5460 (> (org-time-stamp-to-now
5461 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5462 ((eq org-agenda-todo-ignore-deadlines 'past)
5463 (<= (org-time-stamp-to-now
5464 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5465 ((numberp org-agenda-todo-ignore-deadlines)
5466 (org-agenda-todo-custom-ignore-p
5467 (match-string 1) org-agenda-todo-ignore-deadlines))
5468 (t (org-deadline-close (match-string 1)))))
5469 (and org-agenda-todo-ignore-timestamp
5470 (let ((buffer (current-buffer))
5471 (regexp
5472 (concat
5473 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5474 (start (point)))
5475 ;; Copy current buffer into a temporary one
5476 (with-temp-buffer
5477 (insert-buffer-substring buffer start end)
5478 (goto-char (point-min))
5479 ;; Delete SCHEDULED and DEADLINE items
5480 (while (re-search-forward regexp end t)
5481 (delete-region (match-beginning 0) (match-end 0)))
5482 (goto-char (point-min))
5483 ;; No search for timestamp left
5484 (when (re-search-forward org-ts-regexp nil t)
5485 (cond
5486 ((eq org-agenda-todo-ignore-timestamp 'future)
5487 (> (org-time-stamp-to-now
5488 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5489 ((eq org-agenda-todo-ignore-timestamp 'past)
5490 (<= (org-time-stamp-to-now
5491 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5492 ((numberp org-agenda-todo-ignore-timestamp)
5493 (org-agenda-todo-custom-ignore-p
5494 (match-string 1) org-agenda-todo-ignore-timestamp))
5495 (t))))))))))
5497 (defun org-agenda-get-timestamps (&optional deadline-results)
5498 "Return the date stamp information for agenda display."
5499 (let* ((props (list 'face 'org-agenda-calendar-event
5500 'org-not-done-regexp org-not-done-regexp
5501 'org-todo-regexp org-todo-regexp
5502 'org-complex-heading-regexp org-complex-heading-regexp
5503 'mouse-face 'highlight
5504 'help-echo
5505 (format "mouse-2 or RET jump to org file %s"
5506 (abbreviate-file-name buffer-file-name))))
5507 (d1 (calendar-absolute-from-gregorian date))
5509 (deadline-position-alist
5510 (mapcar (lambda (a) (and (setq mm (get-text-property
5511 0 'org-hd-marker a))
5512 (cons (marker-position mm) a)))
5513 deadline-results))
5514 (remove-re org-ts-regexp)
5515 (regexp
5516 (concat
5517 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5518 (regexp-quote
5519 (substring
5520 (format-time-string
5521 (car org-time-stamp-formats)
5522 (apply 'encode-time ; DATE bound by calendar
5523 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5524 1 11))
5525 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5526 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5527 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5528 donep tmp priority category category-pos level ee txt timestr tags
5529 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5530 inherited-tags ts-date)
5531 (goto-char (point-min))
5532 (while (setq end-of-match (re-search-forward regexp nil t))
5533 (setq b0 (match-beginning 0)
5534 b3 (match-beginning 3) e3 (match-end 3)
5535 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5536 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5537 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5538 (member todo-state
5539 org-agenda-repeating-timestamp-show-all)))
5540 (catch :skip
5541 (and (org-at-date-range-p) (throw :skip nil))
5542 (org-agenda-skip)
5543 (if (and (match-end 1)
5544 (not (= d1 (org-time-string-to-absolute
5545 (match-string 1) d1 nil show-all
5546 (current-buffer) b0))))
5547 (throw :skip nil))
5548 (if (and e3
5549 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5550 (throw :skip nil))
5551 (setq tmp (buffer-substring (max (point-min)
5552 (- b0 org-ds-keyword-length))
5554 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5555 inactivep (= (char-after b0) ?\[)
5556 deadlinep (string-match org-deadline-regexp tmp)
5557 scheduledp (string-match org-scheduled-regexp tmp)
5558 closedp (and org-agenda-include-inactive-timestamps
5559 (string-match org-closed-string tmp))
5560 clockp (and org-agenda-include-inactive-timestamps
5561 (or (string-match org-clock-string tmp)
5562 (string-match "]-+\\'" tmp)))
5563 warntime (get-text-property (point) 'org-appt-warntime)
5564 donep (member todo-state org-done-keywords))
5565 (if (or scheduledp deadlinep closedp clockp
5566 (and donep org-agenda-skip-timestamp-if-done))
5567 (throw :skip t))
5568 (if (string-match ">" timestr)
5569 ;; substring should only run to end of time stamp
5570 (setq timestr (substring timestr 0 (match-end 0))))
5571 (setq marker (org-agenda-new-marker b0)
5572 category (org-get-category b0)
5573 category-pos (get-text-property b0 'org-category-position))
5574 (save-excursion
5575 (if (not (re-search-backward org-outline-regexp-bol nil t))
5576 (throw :skip nil)
5577 (goto-char (match-beginning 0))
5578 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5579 (assoc (point) deadline-position-alist))
5580 (throw :skip nil))
5581 (setq hdmarker (org-agenda-new-marker)
5582 inherited-tags
5583 (or (eq org-agenda-show-inherited-tags 'always)
5584 (and (listp org-agenda-show-inherited-tags)
5585 (memq 'agenda org-agenda-show-inherited-tags))
5586 (and (eq org-agenda-show-inherited-tags t)
5587 (or (eq org-agenda-use-tag-inheritance t)
5588 (memq 'agenda org-agenda-use-tag-inheritance))))
5589 tags (org-get-tags-at nil (not inherited-tags))
5590 level (make-string (org-reduced-level (org-outline-level)) ? ))
5591 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5592 (setq head (or (match-string 1) ""))
5593 (setq txt (org-agenda-format-item
5594 (if inactivep org-agenda-inactive-leader nil)
5595 head level category tags timestr
5596 remove-re habitp)))
5597 (setq priority (org-get-priority txt))
5598 (org-add-props txt props 'priority priority
5599 'org-marker marker 'org-hd-marker hdmarker
5600 'org-category category 'date date
5601 'level level
5602 'ts-date
5603 (ignore-errors (org-time-string-to-absolute timestr))
5604 'org-category-position category-pos
5605 'todo-state todo-state
5606 'warntime warntime
5607 'type "timestamp")
5608 (push txt ee))
5609 (if org-agenda-skip-additional-timestamps-same-entry
5610 (outline-next-heading)
5611 (goto-char end-of-match))))
5612 (nreverse ee)))
5614 (defun org-agenda-get-sexps ()
5615 "Return the sexp information for agenda display."
5616 (require 'diary-lib)
5617 (let* ((props (list 'face 'org-agenda-calendar-sexp
5618 'mouse-face 'highlight
5619 'help-echo
5620 (format "mouse-2 or RET jump to org file %s"
5621 (abbreviate-file-name buffer-file-name))))
5622 (regexp "^&?%%(")
5623 marker category extra category-pos level ee txt tags entry
5624 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5625 (goto-char (point-min))
5626 (while (re-search-forward regexp nil t)
5627 (catch :skip
5628 (org-agenda-skip)
5629 (setq beg (match-beginning 0))
5630 (goto-char (1- (match-end 0)))
5631 (setq b (point))
5632 (forward-sexp 1)
5633 (setq sexp (buffer-substring b (point)))
5634 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5635 (org-trim (match-string 1))
5636 ""))
5637 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5638 (when result
5639 (setq marker (org-agenda-new-marker beg)
5640 level (make-string (org-reduced-level (org-outline-level)) ? )
5641 category (org-get-category beg)
5642 category-pos (get-text-property beg 'org-category-position)
5643 inherited-tags
5644 (or (eq org-agenda-show-inherited-tags 'always)
5645 (and (listp org-agenda-show-inherited-tags)
5646 (memq 'agenda org-agenda-show-inherited-tags))
5647 (and (eq org-agenda-show-inherited-tags t)
5648 (or (eq org-agenda-use-tag-inheritance t)
5649 (memq 'agenda org-agenda-use-tag-inheritance))))
5650 tags (org-get-tags-at nil (not inherited-tags))
5651 todo-state (org-get-todo-state)
5652 warntime (get-text-property (point) 'org-appt-warntime)
5653 extra nil)
5655 (dolist (r (if (stringp result)
5656 (list result)
5657 result)) ;; we expect a list here
5658 (when (and org-agenda-diary-sexp-prefix
5659 (string-match org-agenda-diary-sexp-prefix r))
5660 (setq extra (match-string 0 r)
5661 r (replace-match "" nil nil r)))
5662 (if (string-match "\\S-" r)
5663 (setq txt r)
5664 (setq txt "SEXP entry returned empty string"))
5665 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5666 (org-add-props txt props 'org-marker marker
5667 'org-category category 'date date 'todo-state todo-state
5668 'org-category-position category-pos 'tags tags
5669 'level level
5670 'type "sexp" 'warntime warntime)
5671 (push txt ee)))))
5672 (nreverse ee)))
5674 ;; Calendar sanity: define some functions that are independent of
5675 ;; `calendar-date-style'.
5676 ;; Normally I would like to use ISO format when calling the diary functions,
5677 ;; but to make sure we still have Emacs 22 compatibility we bind
5678 ;; also `european-calendar-style' and use european format
5679 (defun org-anniversary (year month day &optional mark)
5680 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5681 (org-no-warnings
5682 (let ((calendar-date-style 'european) (european-calendar-style t))
5683 (diary-anniversary day month year mark))))
5684 (defun org-cyclic (N year month day &optional mark)
5685 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5686 (org-no-warnings
5687 (let ((calendar-date-style 'european) (european-calendar-style t))
5688 (diary-cyclic N day month year mark))))
5689 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5690 "Like `diary-block', but with fixed (ISO) order of arguments."
5691 (org-no-warnings
5692 (let ((calendar-date-style 'european) (european-calendar-style t))
5693 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5694 (defun org-date (year month day &optional mark)
5695 "Like `diary-date', but with fixed (ISO) order of arguments."
5696 (org-no-warnings
5697 (let ((calendar-date-style 'european) (european-calendar-style t))
5698 (diary-date day month year mark))))
5700 ;; Define the` org-class' function
5701 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5702 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5703 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5704 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5705 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5706 `holidays', then any date that is known by the Emacs calendar to be a
5707 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5708 then those holidays will be skipped."
5709 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5710 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5711 (d (calendar-absolute-from-gregorian date))
5712 (h (when skip-weeks (calendar-check-holidays date))))
5713 (and
5714 (<= date1 d)
5715 (<= d date2)
5716 (= (calendar-day-of-week date) dayname)
5717 (or (not skip-weeks)
5718 (progn
5719 (require 'cal-iso)
5720 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5721 (not (or (and h (memq 'holidays skip-weeks))
5722 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5723 entry)))
5725 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5726 "Like `org-class', but honor `calendar-date-style'.
5727 The order of the first 2 times 3 arguments depends on the variable
5728 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5729 So for American calendars, give this as MONTH DAY YEAR, for European as
5730 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5731 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5732 is any number of ISO weeks in the block period for which the item should
5733 be skipped.
5735 This function is here only for backward compatibility and it is deprecated,
5736 please use `org-class' instead."
5737 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5738 (date2 (org-order-calendar-date-args m2 d2 y2)))
5739 (org-class
5740 (nth 2 date1) (car date1) (nth 1 date1)
5741 (nth 2 date2) (car date2) (nth 1 date2)
5742 dayname skip-weeks)))
5743 (make-obsolete 'org-diary-class 'org-class "")
5745 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5746 (defalias 'org-get-closed 'org-agenda-get-progress)
5747 (defun org-agenda-get-progress ()
5748 "Return the logged TODO entries for agenda display."
5749 (let* ((props (list 'mouse-face 'highlight
5750 'org-not-done-regexp org-not-done-regexp
5751 'org-todo-regexp org-todo-regexp
5752 'org-complex-heading-regexp org-complex-heading-regexp
5753 'help-echo
5754 (format "mouse-2 or RET jump to org file %s"
5755 (abbreviate-file-name buffer-file-name))))
5756 (items (if (consp org-agenda-show-log-scoped)
5757 org-agenda-show-log-scoped
5758 (if (eq org-agenda-show-log-scoped 'clockcheck)
5759 '(clock)
5760 org-agenda-log-mode-items)))
5761 (parts
5762 (delq nil
5763 (list
5764 (if (memq 'closed items) (concat "\\<" org-closed-string))
5765 (if (memq 'clock items) (concat "\\<" org-clock-string))
5766 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5767 (parts-re (if parts (mapconcat 'identity parts "\\|")
5768 (error "`org-agenda-log-mode-items' is empty")))
5769 (regexp (concat
5770 "\\(" parts-re "\\)"
5771 " *\\["
5772 (regexp-quote
5773 (substring
5774 (format-time-string
5775 (car org-time-stamp-formats)
5776 (apply 'encode-time ; DATE bound by calendar
5777 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5778 1 11))))
5779 (org-agenda-search-headline-for-time nil)
5780 marker hdmarker priority category category-pos level tags closedp
5781 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5782 (goto-char (point-min))
5783 (while (re-search-forward regexp nil t)
5784 (catch :skip
5785 (org-agenda-skip)
5786 (setq marker (org-agenda-new-marker (match-beginning 0))
5787 closedp (equal (match-string 1) org-closed-string)
5788 statep (equal (string-to-char (match-string 1)) ?-)
5789 clockp (not (or closedp statep))
5790 state (and statep (match-string 2))
5791 category (org-get-category (match-beginning 0))
5792 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5793 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5794 (when (string-match "\\]" timestr)
5795 ;; substring should only run to end of time stamp
5796 (setq rest (substring timestr (match-end 0))
5797 timestr (substring timestr 0 (match-end 0)))
5798 (if (and (not closedp) (not statep)
5799 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5800 rest))
5801 (progn (setq timestr (concat (substring timestr 0 -1)
5802 "-" (match-string 1 rest) "]"))
5803 (setq clocked (match-string 2 rest)))
5804 (setq clocked "-")))
5805 (save-excursion
5806 (setq extra
5807 (cond
5808 ((not org-agenda-log-mode-add-notes) nil)
5809 (statep
5810 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5811 (match-string 1)))
5812 (clockp
5813 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5814 (match-string 1)))))
5815 (if (not (re-search-backward org-outline-regexp-bol nil t))
5816 (throw :skip nil)
5817 (goto-char (match-beginning 0))
5818 (setq hdmarker (org-agenda-new-marker)
5819 inherited-tags
5820 (or (eq org-agenda-show-inherited-tags 'always)
5821 (and (listp org-agenda-show-inherited-tags)
5822 (memq 'todo org-agenda-show-inherited-tags))
5823 (and (eq org-agenda-show-inherited-tags t)
5824 (or (eq org-agenda-use-tag-inheritance t)
5825 (memq 'todo org-agenda-use-tag-inheritance))))
5826 tags (org-get-tags-at nil (not inherited-tags))
5827 level (make-string (org-reduced-level (org-outline-level)) ? ))
5828 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5829 (setq txt (match-string 1))
5830 (when extra
5831 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5832 (setq txt (concat (substring txt 0 (match-beginning 1))
5833 " - " extra " " (match-string 2 txt)))
5834 (setq txt (concat txt " - " extra))))
5835 (setq txt (org-agenda-format-item
5836 (cond
5837 (closedp "Closed: ")
5838 (statep (concat "State: (" state ")"))
5839 (t (concat "Clocked: (" clocked ")")))
5840 txt level category tags timestr)))
5841 (setq priority 100000)
5842 (org-add-props txt props
5843 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5844 'priority priority 'org-category category
5845 'org-category-position category-pos
5846 'level level
5847 'type "closed" 'date date
5848 'undone-face 'org-warning 'done-face 'org-agenda-done)
5849 (push txt ee))
5850 (goto-char (point-at-eol))))
5851 (nreverse ee)))
5853 (defun org-agenda-show-clocking-issues ()
5854 "Add overlays, showing issues with clocking.
5855 See also the user option `org-agenda-clock-consistency-checks'."
5856 (interactive)
5857 (let* ((org-time-clocksum-use-effort-durations nil)
5858 (pl org-agenda-clock-consistency-checks)
5859 (re (concat "^[ \t]*"
5860 org-clock-string
5861 "[ \t]+"
5862 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5863 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5864 (tlstart 0.)
5865 (tlend 0.)
5866 (maxtime (org-hh:mm-string-to-minutes
5867 (or (plist-get pl :max-duration) "24:00")))
5868 (mintime (org-hh:mm-string-to-minutes
5869 (or (plist-get pl :min-duration) 0)))
5870 (maxgap (org-hh:mm-string-to-minutes
5871 ;; default 30:00 means never complain
5872 (or (plist-get pl :max-gap) "30:00")))
5873 (gapok (mapcar 'org-hh:mm-string-to-minutes
5874 (plist-get pl :gap-ok-around)))
5875 (def-face (or (plist-get pl :default-face)
5876 '((:background "DarkRed") (:foreground "white"))))
5877 issue face m te ts dt ov)
5878 (goto-char (point-min))
5879 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5880 (setq issue nil face def-face)
5881 (catch 'next
5882 (setq m (org-get-at-bol 'org-marker)
5883 te nil ts nil)
5884 (unless (and m (markerp m))
5885 (setq issue "No valid clock line") (throw 'next t))
5886 (org-with-point-at m
5887 (save-excursion
5888 (goto-char (point-at-bol))
5889 (unless (looking-at re)
5890 (error "No valid Clock line")
5891 (throw 'next t))
5892 (unless (match-end 3)
5893 (setq issue "No end time"
5894 face (or (plist-get pl :no-end-time-face) face))
5895 (throw 'next t))
5896 (setq ts (match-string 1)
5897 te (match-string 3)
5898 ts (org-float-time
5899 (apply 'encode-time (org-parse-time-string ts)))
5900 te (org-float-time
5901 (apply 'encode-time (org-parse-time-string te)))
5902 dt (- te ts))))
5903 (cond
5904 ((> dt (* 60 maxtime))
5905 ;; a very long clocking chunk
5906 (setq issue (format "Clocking interval is very long: %s"
5907 (org-minutes-to-clocksum-string
5908 (floor (/ (float dt) 60.))))
5909 face (or (plist-get pl :long-face) face)))
5910 ((< dt (* 60 mintime))
5911 ;; a very short clocking chunk
5912 (setq issue (format "Clocking interval is very short: %s"
5913 (org-minutes-to-clocksum-string
5914 (floor (/ (float dt) 60.))))
5915 face (or (plist-get pl :short-face) face)))
5916 ((and (> tlend 0) (< ts tlend))
5917 ;; Two clock entries are overlapping
5918 (setq issue (format "Clocking overlap: %d minutes"
5919 (/ (- tlend ts) 60))
5920 face (or (plist-get pl :overlap-face) face)))
5921 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5922 ;; There is a gap, lets see if we need to report it
5923 (unless (org-agenda-check-clock-gap tlend ts gapok)
5924 (setq issue (format "Clocking gap: %d minutes"
5925 (/ (- ts tlend) 60))
5926 face (or (plist-get pl :gap-face) face))))
5927 (t nil)))
5928 (setq tlend (or te tlend) tlstart (or ts tlstart))
5929 (when issue
5930 ;; OK, there was some issue, add an overlay to show the issue
5931 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5932 (overlay-put ov 'before-string
5933 (concat
5934 (org-add-props
5935 (format "%-43s" (concat " " issue))
5937 'face face)
5938 "\n"))
5939 (overlay-put ov 'evaporate t)))))
5941 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5942 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5943 (catch 'exit
5944 (unless ok-list
5945 ;; there are no OK times for gaps...
5946 (throw 'exit nil))
5947 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5948 ;; This is more than 24 hours, so it is OK.
5949 ;; because we have at least one OK time, that must be in the
5950 ;; 24 hour interval.
5951 (throw 'exit t))
5952 ;; We have a shorter gap.
5953 ;; Now we have to get the minute of the day when these times are
5954 (let* ((t1dec (decode-time (seconds-to-time t1)))
5955 (t2dec (decode-time (seconds-to-time t2)))
5956 ;; compute the minute on the day
5957 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5958 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5959 (when (< min2 min1)
5960 ;; if min2 is smaller than min1, this means it is on the next day.
5961 ;; Wrap it to after midnight.
5962 (setq min2 (+ min2 1440)))
5963 ;; Now check if any of the OK times is in the gap
5964 (mapc (lambda (x)
5965 ;; Wrap the time to after midnight if necessary
5966 (if (< x min1) (setq x (+ x 1440)))
5967 ;; Check if in interval
5968 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5969 ok-list)
5970 ;; Nope, this gap is not OK
5971 nil)))
5973 (defun org-agenda-get-deadlines (&optional with-hour)
5974 "Return the deadline information for agenda display.
5975 When WITH-HOUR is non-nil, only return deadlines with an hour
5976 specification like [h]h:mm."
5977 (let* ((props (list 'mouse-face 'highlight
5978 'org-not-done-regexp org-not-done-regexp
5979 'org-todo-regexp org-todo-regexp
5980 'org-complex-heading-regexp org-complex-heading-regexp
5981 'help-echo
5982 (format "mouse-2 or RET jump to org file %s"
5983 (abbreviate-file-name buffer-file-name))))
5984 (regexp (if with-hour
5985 org-deadline-time-hour-regexp
5986 org-deadline-time-regexp))
5987 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5988 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5989 (dl0 (car org-agenda-deadline-leaders))
5990 (dl1 (nth 1 org-agenda-deadline-leaders))
5991 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
5992 d2 diff dfrac wdays pos pos1 category category-pos level
5993 tags suppress-prewarning ee txt head face s todo-state
5994 show-all upcomingp donep timestr warntime inherited-tags ts-date)
5995 (goto-char (point-min))
5996 (while (re-search-forward regexp nil t)
5997 (catch :skip
5998 (org-agenda-skip)
5999 (setq s (match-string 1)
6000 txt nil
6001 pos (1- (match-beginning 1))
6002 todo-state (save-match-data (org-get-todo-state))
6003 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6004 (member todo-state
6005 org-agenda-repeating-timestamp-show-all))
6006 d2 (org-time-string-to-absolute
6007 s d1 'past show-all (current-buffer) pos)
6008 diff (- d2 d1))
6009 (setq suppress-prewarning
6010 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6011 (let ((item (buffer-substring (point-at-bol)
6012 (point-at-eol))))
6013 (save-match-data
6014 (and (string-match
6015 org-scheduled-time-regexp item)
6016 (match-string 1 item)))))))
6017 (cond
6018 ((not ds) nil)
6019 ;; The current item has a scheduled date (in ds), so
6020 ;; evaluate its prewarning lead time.
6021 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6022 ;; Use global prewarning-restart lead time.
6023 org-agenda-skip-deadline-prewarning-if-scheduled)
6024 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6025 'pre-scheduled)
6026 ;; Set prewarning to no earlier than scheduled.
6027 (min (- d2 (org-time-string-to-absolute
6028 ds d1 'past show-all (current-buffer) pos))
6029 org-deadline-warning-days))
6030 ;; Set prewarning to deadline.
6031 (t 0))))
6032 (setq wdays (if suppress-prewarning
6033 (let ((org-deadline-warning-days suppress-prewarning))
6034 (org-get-wdays s))
6035 (org-get-wdays s))
6036 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6037 upcomingp (and todayp (> diff 0)))
6038 ;; When to show a deadline in the calendar:
6039 ;; If the expiration is within wdays warning time.
6040 ;; Past-due deadlines are only shown on the current date
6041 (if (and (or (and (<= diff wdays)
6042 (and todayp (not org-agenda-only-exact-dates)))
6043 (= diff 0)))
6044 (save-excursion
6045 ;; (setq todo-state (org-get-todo-state))
6046 (setq donep (member todo-state org-done-keywords))
6047 (if (and donep
6048 (or org-agenda-skip-deadline-if-done
6049 (not (= diff 0))))
6050 (setq txt nil)
6051 (setq category (org-get-category)
6052 warntime (get-text-property (point) 'org-appt-warntime)
6053 category-pos (get-text-property (point) 'org-category-position))
6054 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6055 (throw :skip nil)
6056 (goto-char (match-end 0))
6057 (setq pos1 (match-beginning 0))
6058 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6059 (setq inherited-tags
6060 (or (eq org-agenda-show-inherited-tags 'always)
6061 (and (listp org-agenda-show-inherited-tags)
6062 (memq 'agenda org-agenda-show-inherited-tags))
6063 (and (eq org-agenda-show-inherited-tags t)
6064 (or (eq org-agenda-use-tag-inheritance t)
6065 (memq 'agenda org-agenda-use-tag-inheritance))))
6066 tags (org-get-tags-at pos1 (not inherited-tags)))
6067 (setq head (buffer-substring
6068 (point)
6069 (progn (skip-chars-forward "^\r\n")
6070 (point))))
6071 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6072 (setq timestr
6073 (concat (substring s (match-beginning 1)) " "))
6074 (setq timestr 'time))
6075 (setq txt (org-agenda-format-item
6076 (cond ((= diff 0) dl0)
6077 ((> diff 0)
6078 (if (functionp dl1)
6079 (funcall dl1 diff date)
6080 (format dl1 diff)))
6082 (if (functionp dl2)
6083 (funcall dl2 diff date)
6084 (format dl2 (if (string= dl2 dl1)
6085 diff (abs diff))))))
6086 head level category tags
6087 (if (not (= diff 0)) nil timestr)))))
6088 (when txt
6089 (setq face (org-agenda-deadline-face dfrac))
6090 (org-add-props txt props
6091 'org-marker (org-agenda-new-marker pos)
6092 'warntime warntime
6093 'level level
6094 'ts-date d2
6095 'org-hd-marker (org-agenda-new-marker pos1)
6096 'priority (+ (- diff)
6097 (org-get-priority txt))
6098 'org-category category
6099 'org-category-position category-pos
6100 'todo-state todo-state
6101 'type (if upcomingp "upcoming-deadline" "deadline")
6102 'date (if upcomingp date d2)
6103 'face (if donep 'org-agenda-done face)
6104 'undone-face face 'done-face 'org-agenda-done)
6105 (push txt ee))))))
6106 (nreverse ee)))
6108 (defun org-agenda-deadline-face (fraction)
6109 "Return the face to displaying a deadline item.
6110 FRACTION is what fraction of the head-warning time has passed."
6111 (let ((faces org-agenda-deadline-faces) f)
6112 (catch 'exit
6113 (while (setq f (pop faces))
6114 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6116 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6117 "Return the scheduled information for agenda display.
6118 When WITH-HOUR is non-nil, only return scheduled items with
6119 an hour specification like [h]h:mm."
6120 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6121 'org-todo-regexp org-todo-regexp
6122 'org-complex-heading-regexp org-complex-heading-regexp
6123 'done-face 'org-agenda-done
6124 'mouse-face 'highlight
6125 'help-echo
6126 (format "mouse-2 or RET jump to org file %s"
6127 (abbreviate-file-name buffer-file-name))))
6128 (regexp (if with-hour
6129 org-scheduled-time-hour-regexp
6130 org-scheduled-time-regexp))
6131 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6132 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6134 (deadline-position-alist
6135 (mapcar (lambda (a) (and (setq mm (get-text-property
6136 0 'org-hd-marker a))
6137 (cons (marker-position mm) a)))
6138 deadline-results))
6139 d2 diff pos pos1 category category-pos level tags donep
6140 ee txt head pastschedp todo-state face timestr s habitp show-all
6141 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6142 ddays)
6143 (goto-char (point-min))
6144 (while (re-search-forward regexp nil t)
6145 (catch :skip
6146 (org-agenda-skip)
6147 (setq s (match-string 1)
6148 txt nil
6149 pos (1- (match-beginning 1))
6150 todo-state (save-match-data (org-get-todo-state))
6151 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6152 (member todo-state
6153 org-agenda-repeating-timestamp-show-all))
6154 d2 (org-time-string-to-absolute
6155 s d1 'past show-all (current-buffer) pos)
6156 diff (- d2 d1)
6157 warntime (get-text-property (point) 'org-appt-warntime))
6158 (setq pastschedp (and todayp (< diff 0)))
6159 (setq did-habit-check-p nil)
6160 (setq suppress-delay
6161 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6162 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6163 (save-match-data
6164 (and (string-match
6165 org-deadline-time-regexp item)
6166 (match-string 1 item)))))))
6167 (cond
6168 ((not ds) nil)
6169 ;; The current item has a deadline date (in ds), so
6170 ;; evaluate its delay time.
6171 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6172 ;; Use global delay time.
6173 (- org-agenda-skip-scheduled-delay-if-deadline))
6174 ((eq org-agenda-skip-scheduled-delay-if-deadline
6175 'post-deadline)
6176 ;; Set delay to no later than deadline.
6177 (min (- d2 (org-time-string-to-absolute
6178 ds d1 'past show-all (current-buffer) pos))
6179 org-scheduled-delay-days))
6180 (t 0))))
6181 (setq ddays (if suppress-delay
6182 (let ((org-scheduled-delay-days suppress-delay))
6183 (org-get-wdays s t t))
6184 (org-get-wdays s t)))
6185 ;; Use a delay of 0 when there is a repeater and the delay is
6186 ;; of the form --3d
6187 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6188 (< (org-time-string-to-absolute s)
6189 (org-time-string-to-absolute
6190 s d2 'past nil (current-buffer) pos)))
6191 (setq ddays 0))
6192 ;; When to show a scheduled item in the calendar:
6193 ;; If it is on or past the date.
6194 (when (or (and (> ddays 0) (= diff (- ddays)))
6195 (and (zerop ddays) (= diff 0))
6196 (and (< (+ diff ddays) 0)
6197 (< (abs diff) org-scheduled-past-days)
6198 (and todayp (not org-agenda-only-exact-dates)))
6199 ;; org-is-habit-p uses org-entry-get, which is expansive
6200 ;; so we go extra mile to only call it once
6201 (and todayp
6202 (boundp 'org-habit-show-all-today)
6203 org-habit-show-all-today
6204 (setq did-habit-check-p t)
6205 (setq habitp (and (functionp 'org-is-habit-p)
6206 (org-is-habit-p)))))
6207 (save-excursion
6208 (setq donep (member todo-state org-done-keywords))
6209 (if (and donep
6210 (or org-agenda-skip-scheduled-if-done
6211 (not (= diff 0))
6212 (and (functionp 'org-is-habit-p)
6213 (org-is-habit-p))))
6214 (setq txt nil)
6215 (setq habitp (if did-habit-check-p habitp
6216 (and (functionp 'org-is-habit-p)
6217 (org-is-habit-p))))
6218 (setq category (org-get-category)
6219 category-pos (get-text-property (point) 'org-category-position))
6220 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6221 'repeated-after-deadline)
6222 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6223 (throw :skip nil))
6224 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6225 (throw :skip nil)
6226 (goto-char (match-end 0))
6227 (setq pos1 (match-beginning 0))
6228 (if habitp
6229 (if (or (not org-habit-show-habits)
6230 (and (not todayp)
6231 (boundp 'org-habit-show-habits-only-for-today)
6232 org-habit-show-habits-only-for-today))
6233 (throw :skip nil))
6234 (if (and
6235 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6236 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6237 pastschedp))
6238 (setq mm (assoc pos1 deadline-position-alist)))
6239 (throw :skip nil)))
6240 (setq inherited-tags
6241 (or (eq org-agenda-show-inherited-tags 'always)
6242 (and (listp org-agenda-show-inherited-tags)
6243 (memq 'agenda org-agenda-show-inherited-tags))
6244 (and (eq org-agenda-show-inherited-tags t)
6245 (or (eq org-agenda-use-tag-inheritance t)
6246 (memq 'agenda org-agenda-use-tag-inheritance))))
6248 tags (org-get-tags-at nil (not inherited-tags)))
6249 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6250 (setq head (buffer-substring
6251 (point)
6252 (progn (skip-chars-forward "^\r\n") (point))))
6253 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6254 (setq timestr
6255 (concat (substring s (match-beginning 1)) " "))
6256 (setq timestr 'time))
6257 (setq txt (org-agenda-format-item
6258 (if (= diff 0)
6259 (car org-agenda-scheduled-leaders)
6260 (format (nth 1 org-agenda-scheduled-leaders)
6261 (- 1 diff)))
6262 head level category tags
6263 (if (not (= diff 0)) nil timestr)
6264 nil habitp))))
6265 (when txt
6266 (setq face
6267 (cond
6268 ((and (not habitp) pastschedp)
6269 'org-scheduled-previously)
6270 (todayp 'org-scheduled-today)
6271 (t 'org-scheduled))
6272 habitp (and habitp (org-habit-parse-todo)))
6273 (org-add-props txt props
6274 'undone-face face
6275 'face (if donep 'org-agenda-done face)
6276 'org-marker (org-agenda-new-marker pos)
6277 'org-hd-marker (org-agenda-new-marker pos1)
6278 'type (if pastschedp "past-scheduled" "scheduled")
6279 'date (if pastschedp d2 date)
6280 'ts-date d2
6281 'warntime warntime
6282 'level level
6283 'priority (if habitp
6284 (org-habit-get-priority habitp)
6285 (+ 94 (- 5 diff) (org-get-priority txt)))
6286 'org-category category
6287 'category-position category-pos
6288 'org-habit-p habitp
6289 'todo-state todo-state)
6290 (push txt ee))))))
6291 (nreverse ee)))
6293 (defun org-agenda-get-blocks ()
6294 "Return the date-range information for agenda display."
6295 (let* ((props (list 'face nil
6296 'org-not-done-regexp org-not-done-regexp
6297 'org-todo-regexp org-todo-regexp
6298 'org-complex-heading-regexp org-complex-heading-regexp
6299 'mouse-face 'highlight
6300 'help-echo
6301 (format "mouse-2 or RET jump to org file %s"
6302 (abbreviate-file-name buffer-file-name))))
6303 (regexp org-tr-regexp)
6304 (d0 (calendar-absolute-from-gregorian date))
6305 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6306 level todo-state tags pos head donep inherited-tags)
6307 (goto-char (point-min))
6308 (while (re-search-forward regexp nil t)
6309 (catch :skip
6310 (org-agenda-skip)
6311 (setq pos (point))
6312 (let ((start-time (match-string 1))
6313 (end-time (match-string 2)))
6314 (setq s1 (match-string 1)
6315 s2 (match-string 2)
6316 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6317 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6318 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6319 ;; Only allow days between the limits, because the normal
6320 ;; date stamps will catch the limits.
6321 (save-excursion
6322 (setq todo-state (org-get-todo-state))
6323 (setq donep (member todo-state org-done-keywords))
6324 (if (and donep org-agenda-skip-timestamp-if-done)
6325 (throw :skip t))
6326 (setq marker (org-agenda-new-marker (point)))
6327 (setq category (org-get-category)
6328 category-pos (get-text-property (point) 'org-category-position))
6329 (if (not (re-search-backward org-outline-regexp-bol nil t))
6330 (throw :skip nil)
6331 (goto-char (match-beginning 0))
6332 (setq hdmarker (org-agenda-new-marker (point))
6333 inherited-tags
6334 (or (eq org-agenda-show-inherited-tags 'always)
6335 (and (listp org-agenda-show-inherited-tags)
6336 (memq 'agenda org-agenda-show-inherited-tags))
6337 (and (eq org-agenda-show-inherited-tags t)
6338 (or (eq org-agenda-use-tag-inheritance t)
6339 (memq 'agenda org-agenda-use-tag-inheritance))))
6341 tags (org-get-tags-at nil (not inherited-tags)))
6342 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6343 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6344 (setq head (match-string 1))
6345 (let ((remove-re
6346 (if org-agenda-remove-timeranges-from-blocks
6347 (concat
6348 "<" (regexp-quote s1) ".*?>"
6349 "--"
6350 "<" (regexp-quote s2) ".*?>")
6351 nil)))
6352 (setq txt (org-agenda-format-item
6353 (format
6354 (nth (if (= d1 d2) 0 1)
6355 org-agenda-timerange-leaders)
6356 (1+ (- d0 d1)) (1+ (- d2 d1)))
6357 head level category tags
6358 (cond ((and (= d1 d0) (= d2 d0))
6359 (concat "<" start-time ">--<" end-time ">"))
6360 ((= d1 d0)
6361 (concat "<" start-time ">"))
6362 ((= d2 d0)
6363 (concat "<" end-time ">")))
6364 remove-re))))
6365 (org-add-props txt props
6366 'org-marker marker 'org-hd-marker hdmarker
6367 'type "block" 'date date
6368 'level level
6369 'todo-state todo-state
6370 'priority (org-get-priority txt) 'org-category category
6371 'org-category-position category-pos)
6372 (push txt ee))))
6373 (goto-char pos)))
6374 ;; Sort the entries by expiration date.
6375 (nreverse ee)))
6377 ;;; Agenda presentation and sorting
6379 (defvar org-prefix-has-time nil
6380 "A flag, set by `org-compile-prefix-format'.
6381 The flag is set if the currently compiled format contains a `%t'.")
6382 (defvar org-prefix-has-tag nil
6383 "A flag, set by `org-compile-prefix-format'.
6384 The flag is set if the currently compiled format contains a `%T'.")
6385 (defvar org-prefix-has-effort nil
6386 "A flag, set by `org-compile-prefix-format'.
6387 The flag is set if the currently compiled format contains a `%e'.")
6388 (defvar org-prefix-has-breadcrumbs nil
6389 "A flag, set by `org-compile-prefix-format'.
6390 The flag is set if the currently compiled format contains a `%b'.")
6391 (defvar org-prefix-category-length nil
6392 "Used by `org-compile-prefix-format' to remember the category field width.")
6393 (defvar org-prefix-category-max-length nil
6394 "Used by `org-compile-prefix-format' to remember the category field width.")
6396 (defun org-agenda-get-category-icon (category)
6397 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6398 (dolist (entry org-agenda-category-icon-alist)
6399 (when (org-string-match-p (car entry) category)
6400 (if (listp (cadr entry))
6401 (return (cadr entry))
6402 (return (apply 'create-image (cdr entry)))))))
6404 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6405 remove-re habitp)
6406 "Format TXT to be inserted into the agenda buffer.
6407 In particular, add the prefix and corresponding text properties.
6409 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6410 LEVEL may be a string to replace the `%l' specifier.
6411 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6412 category taken from local variable or file name. It will replace the `%c'
6413 specifier in the format.
6414 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6415 TXT for sorting of this entry, and for the `%t' specifier in the format.
6416 When DOTIME is a string, this string is searched for a time before TXT is.
6417 TAGS can be the tags of the headline.
6418 Any match of REMOVE-RE will be removed from TXT."
6419 ;; We keep the org-prefix-* variable values along with a compiled
6420 ;; formatter, so that multiple agendas existing at the same time do
6421 ;; not step on each other toes.
6423 ;; It was inconvenient to make these variables buffer local in
6424 ;; Agenda buffers, because this function expects to be called with
6425 ;; the buffer where item comes from being current, and not agenda
6426 ;; buffer
6427 (let* ((bindings (car org-prefix-format-compiled))
6428 (formatter (cadr org-prefix-format-compiled)))
6429 (loop for (var value) in bindings
6430 do (set var value))
6431 (save-match-data
6432 ;; Diary entries sometimes have extra whitespace at the beginning
6433 (setq txt (org-trim txt))
6435 ;; Fix the tags part in txt
6436 (setq txt (org-agenda-fix-displayed-tags
6437 txt tags
6438 org-agenda-show-inherited-tags
6439 org-agenda-hide-tags-regexp))
6441 (let* ((category (or category
6442 (if (stringp org-category)
6443 org-category
6444 (and org-category (symbol-name org-category)))
6445 (if buffer-file-name
6446 (file-name-sans-extension
6447 (file-name-nondirectory buffer-file-name))
6448 "")))
6449 (category-icon (org-agenda-get-category-icon category))
6450 (category-icon (if category-icon
6451 (propertize " " 'display category-icon)
6452 ""))
6453 ;; time, tag, effort are needed for the eval of the prefix format
6454 (tag (if tags (nth (1- (length tags)) tags) ""))
6455 time effort neffort
6456 (ts (if dotime (concat
6457 (if (stringp dotime) dotime "")
6458 (and org-agenda-search-headline-for-time txt))))
6459 (time-of-day (and dotime (org-get-time-of-day ts)))
6460 stamp plain s0 s1 s2 rtn srp l
6461 duration thecategory breadcrumbs)
6462 (and (derived-mode-p 'org-mode) buffer-file-name
6463 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6464 (when (and dotime time-of-day)
6465 ;; Extract starting and ending time and move them to prefix
6466 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6467 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6468 (setq s0 (match-string 0 ts)
6469 srp (and stamp (match-end 3))
6470 s1 (match-string (if plain 1 2) ts)
6471 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6473 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6474 ;; them, we might want to remove them there to avoid duplication.
6475 ;; The user can turn this off with a variable.
6476 (if (and org-prefix-has-time
6477 org-agenda-remove-times-when-in-prefix (or stamp plain)
6478 (string-match (concat (regexp-quote s0) " *") txt)
6479 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6480 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6481 (= (match-beginning 0) 0)
6483 (setq txt (replace-match "" nil nil txt))))
6484 ;; Normalize the time(s) to 24 hour
6485 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6486 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6488 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6489 (let (org-time-clocksum-use-effort-durations)
6490 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6491 (setq s2
6492 (org-minutes-to-clocksum-string
6493 (+ (org-hh:mm-string-to-minutes s1)
6494 org-agenda-default-appointment-duration)))))
6496 ;; Compute the duration
6497 (when s2
6498 (setq duration (- (org-hh:mm-string-to-minutes s2)
6499 (org-hh:mm-string-to-minutes s1)))))
6501 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6502 txt)
6503 ;; Tags are in the string
6504 (if (or (eq org-agenda-remove-tags t)
6505 (and org-agenda-remove-tags
6506 org-prefix-has-tag))
6507 (setq txt (replace-match "" t t txt))
6508 (setq txt (replace-match
6509 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6510 (match-string 2 txt))
6511 t t txt))))
6512 (when (derived-mode-p 'org-mode)
6513 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6515 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6516 ;; current buffer, so move this check outside of above
6517 (if effort
6518 (setq neffort (org-duration-string-to-minutes effort)
6519 effort (setq effort (concat "[" effort "]")))
6520 ;; prevent erroring out with %e format when there is no effort
6521 (setq effort ""))
6523 (when remove-re
6524 (while (string-match remove-re txt)
6525 (setq txt (replace-match "" t t txt))))
6527 ;; Set org-heading property on `txt' to mark the start of the
6528 ;; heading.
6529 (add-text-properties 0 (length txt) '(org-heading t) txt)
6531 ;; Prepare the variables needed in the eval of the compiled format
6532 (if org-prefix-has-breadcrumbs
6533 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6534 (let ((s (org-display-outline-path nil nil "->" t)))
6535 (if (eq "" s) "" (concat s "->"))))))
6536 (setq time (cond (s2 (concat
6537 (org-agenda-time-of-day-to-ampm-maybe s1)
6538 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6539 (if org-agenda-timegrid-use-ampm " ")))
6540 (s1 (concat
6541 (org-agenda-time-of-day-to-ampm-maybe s1)
6542 (if org-agenda-timegrid-use-ampm
6543 "........ "
6544 "......")))
6545 (t ""))
6546 extra (or (and (not habitp) extra) "")
6547 category (if (symbolp category) (symbol-name category) category)
6548 thecategory (copy-sequence category)
6549 level (or level ""))
6550 (if (string-match org-bracket-link-regexp category)
6551 (progn
6552 (setq l (if (match-end 3)
6553 (- (match-end 3) (match-beginning 3))
6554 (- (match-end 1) (match-beginning 1))))
6555 (when (< l (or org-prefix-category-length 0))
6556 (setq category (copy-sequence category))
6557 (org-add-props category nil
6558 'extra-space (make-string
6559 (- org-prefix-category-length l 1) ?\ ))))
6560 (if (and org-prefix-category-max-length
6561 (>= (length category) org-prefix-category-max-length))
6562 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6563 ;; Evaluate the compiled format
6564 (setq rtn (concat (eval formatter) txt))
6566 ;; And finally add the text properties
6567 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6568 (org-add-props rtn nil
6569 'org-category (if thecategory (downcase thecategory) category)
6570 'tags (mapcar 'org-downcase-keep-props tags)
6571 'org-highest-priority org-highest-priority
6572 'org-lowest-priority org-lowest-priority
6573 'time-of-day time-of-day
6574 'duration duration
6575 'effort effort
6576 'effort-minutes neffort
6577 'breadcrumbs breadcrumbs
6578 'txt txt
6579 'level level
6580 'time time
6581 'extra extra
6582 'format org-prefix-format-compiled
6583 'dotime dotime)))))
6585 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6586 "Remove tags string from TXT, and add a modified list of tags.
6587 The modified list may contain inherited tags, and tags matched by
6588 `org-agenda-hide-tags-regexp' will be removed."
6589 (when (or add-inherited hide-re)
6590 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6591 (setq txt (substring txt 0 (match-beginning 0))))
6592 (setq tags
6593 (delq nil
6594 (mapcar (lambda (tg)
6595 (if (or (and hide-re (string-match hide-re tg))
6596 (and (not add-inherited)
6597 (get-text-property 0 'inherited tg)))
6599 tg))
6600 tags)))
6601 (when tags
6602 (let ((have-i (get-text-property 0 'inherited (car tags)))
6604 (setq txt (concat txt " :"
6605 (mapconcat
6606 (lambda (x)
6607 (setq i (get-text-property 0 'inherited x))
6608 (if (and have-i (not i))
6609 (progn
6610 (setq have-i nil)
6611 (concat ":" x))
6613 tags ":")
6614 (if have-i "::" ":"))))))
6615 txt)
6617 (defun org-downcase-keep-props (s)
6618 (let ((props (text-properties-at 0 s)))
6619 (setq s (downcase s))
6620 (add-text-properties 0 (length s) props s)
6623 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6625 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6626 "Add a time-grid for agenda items which need it.
6628 LIST is the list of agenda items formatted by `org-agenda-list'.
6629 NDAYS is the span of the current agenda view.
6630 TODAYP is `t' when the current agenda view is on today."
6631 (catch 'exit
6632 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6633 ((and todayp (member 'today (car org-agenda-time-grid))))
6634 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6635 ((member 'weekly (car org-agenda-time-grid)))
6636 (t (throw 'exit list)))
6637 (let* ((have (delq nil (mapcar
6638 (lambda (x) (get-text-property 1 'time-of-day x))
6639 list)))
6640 (string (nth 1 org-agenda-time-grid))
6641 (gridtimes (nth 2 org-agenda-time-grid))
6642 (req (car org-agenda-time-grid))
6643 (remove (member 'remove-match req))
6644 new time)
6645 (if (and (member 'require-timed req) (not have))
6646 ;; don't show empty grid
6647 (throw 'exit list))
6648 (while (setq time (pop gridtimes))
6649 (unless (and remove (member time have))
6650 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6651 (push (org-agenda-format-item
6652 nil string nil "" nil
6653 (concat (substring time 0 -2) ":" (substring time -2)))
6654 new)
6655 (put-text-property
6656 2 (length (car new)) 'face 'org-time-grid (car new))))
6657 (when (and todayp org-agenda-show-current-time-in-grid)
6658 (push (org-agenda-format-item
6659 nil org-agenda-current-time-string nil "" nil
6660 (format-time-string "%H:%M "))
6661 new)
6662 (put-text-property
6663 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6665 (if (member 'time-up org-agenda-sorting-strategy-selected)
6666 (append new list)
6667 (append list new)))))
6669 (defun org-compile-prefix-format (key)
6670 "Compile the prefix format into a Lisp form that can be evaluated.
6671 The resulting form and associated variable bindings is returned
6672 and stored in the variable `org-prefix-format-compiled'."
6673 (setq org-prefix-has-time nil
6674 org-prefix-has-tag nil
6675 org-prefix-category-length nil
6676 org-prefix-has-effort nil
6677 org-prefix-has-breadcrumbs nil)
6678 (let ((s (cond
6679 ((stringp org-agenda-prefix-format)
6680 org-agenda-prefix-format)
6681 ((assq key org-agenda-prefix-format)
6682 (cdr (assq key org-agenda-prefix-format)))
6683 (t " %-12:c%?-12t% s")))
6684 (start 0)
6685 varform vars var e c f opt)
6686 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6687 s start)
6688 (setq var (or (cdr (assoc (match-string 4 s)
6689 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6690 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6691 'eval)
6692 c (or (match-string 3 s) "")
6693 opt (match-beginning 1)
6694 start (1+ (match-beginning 0)))
6695 (if (equal var 'time) (setq org-prefix-has-time t))
6696 (if (equal var 'tag) (setq org-prefix-has-tag t))
6697 (if (equal var 'effort) (setq org-prefix-has-effort t))
6698 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6699 (setq f (concat "%" (match-string 2 s) "s"))
6700 (when (equal var 'category)
6701 (setq org-prefix-category-length
6702 (floor (abs (string-to-number (match-string 2 s)))))
6703 (setq org-prefix-category-max-length
6704 (let ((x (match-string 2 s)))
6705 (save-match-data
6706 (if (string-match "\\.[0-9]+" x)
6707 (string-to-number (substring (match-string 0 x) 1)))))))
6708 (if (eq var 'eval)
6709 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6710 (if opt
6711 (setq varform
6712 `(if (equal "" ,var)
6714 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6715 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6716 (setq s (replace-match "%s" t nil s))
6717 (push varform vars))
6718 (setq vars (nreverse vars))
6719 (with-current-buffer (or org-agenda-buffer (current-buffer))
6720 (setq org-prefix-format-compiled
6721 (list
6722 `((org-prefix-has-time ,org-prefix-has-time)
6723 (org-prefix-has-tag ,org-prefix-has-tag)
6724 (org-prefix-category-length ,org-prefix-category-length)
6725 (org-prefix-has-effort ,org-prefix-has-effort)
6726 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6727 `(format ,s ,@vars))))))
6729 (defun org-set-sorting-strategy (key)
6730 (if (symbolp (car org-agenda-sorting-strategy))
6731 ;; the old format
6732 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6733 (setq org-agenda-sorting-strategy-selected
6734 (or (cdr (assq key org-agenda-sorting-strategy))
6735 (cdr (assq 'agenda org-agenda-sorting-strategy))
6736 '(time-up category-keep priority-down)))))
6738 (defun org-get-time-of-day (s &optional string mod24)
6739 "Check string S for a time of day.
6740 If found, return it as a military time number between 0 and 2400.
6741 If not found, return nil.
6742 The optional STRING argument forces conversion into a 5 character wide string
6743 HH:MM."
6744 (save-match-data
6745 (when
6746 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6747 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6748 (let* ((h (string-to-number (match-string 1 s)))
6749 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6750 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6751 (am-p (equal ampm "am"))
6752 (h1 (cond ((not ampm) h)
6753 ((= h 12) (if am-p 0 12))
6754 (t (+ h (if am-p 0 12)))))
6755 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6756 (mod h1 24) h1))
6757 (t0 (+ (* 100 h2) m))
6758 (t1 (concat (if (>= h1 24) "+" " ")
6759 (if (and org-agenda-time-leading-zero
6760 (< t0 1000)) "0" "")
6761 (if (< t0 100) "0" "")
6762 (if (< t0 10) "0" "")
6763 (int-to-string t0))))
6764 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6766 (defvar org-agenda-before-sorting-filter-function nil
6767 "Function to be applied to agenda items prior to sorting.
6768 Prior to sorting also means just before they are inserted into the agenda.
6770 To aid sorting, you may revisit the original entries and add more text
6771 properties which will later be used by the sorting functions.
6773 The function should take a string argument, an agenda line.
6774 It has access to the text properties in that line, which contain among
6775 other things, the property `org-hd-marker' that points to the entry
6776 where the line comes from. Note that not all lines going into the agenda
6777 have this property, only most.
6779 The function should return the modified string. It is probably best
6780 to ONLY change text properties.
6782 You can also use this function as a filter, by returning nil for lines
6783 you don't want to have in the agenda at all. For this application, you
6784 could bind the variable in the options section of a custom command.")
6786 (defun org-agenda-finalize-entries (list &optional type)
6787 "Sort, limit and concatenate the LIST of agenda items.
6788 The optional argument TYPE tells the agenda type."
6789 (let ((max-effort (cond ((listp org-agenda-max-effort)
6790 (cdr (assoc type org-agenda-max-effort)))
6791 (t org-agenda-max-effort)))
6792 (max-todo (cond ((listp org-agenda-max-todos)
6793 (cdr (assoc type org-agenda-max-todos)))
6794 (t org-agenda-max-todos)))
6795 (max-tags (cond ((listp org-agenda-max-tags)
6796 (cdr (assoc type org-agenda-max-tags)))
6797 (t org-agenda-max-tags)))
6798 (max-entries (cond ((listp org-agenda-max-entries)
6799 (cdr (assoc type org-agenda-max-entries)))
6800 (t org-agenda-max-entries))) l)
6801 (when org-agenda-before-sorting-filter-function
6802 (setq list
6803 (delq nil
6804 (mapcar
6805 org-agenda-before-sorting-filter-function list))))
6806 (setq list (mapcar 'org-agenda-highlight-todo list)
6807 list (mapcar 'identity (sort list 'org-entries-lessp)))
6808 (when max-effort
6809 (setq list (org-agenda-limit-entries
6810 list 'effort-minutes max-effort 'identity)))
6811 (when max-todo
6812 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6813 (when max-tags
6814 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6815 (when max-entries
6816 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6817 (mapconcat 'identity list "\n")))
6819 (defun org-agenda-limit-entries (list prop limit &optional fn)
6820 "Limit the number of agenda entries."
6821 (let ((include (and limit (< limit 0))))
6822 (if limit
6823 (let ((fun (or fn (lambda (p) (if p 1))))
6824 (lim 0))
6825 (delq nil
6826 (mapcar
6827 (lambda (e)
6828 (let ((pval (funcall fun (get-text-property 1 prop e))))
6829 (if pval (setq lim (+ lim pval)))
6830 (cond ((and pval (<= lim (abs limit))) e)
6831 ((and include (not pval)) e))))
6832 list)))
6833 list)))
6835 (defun org-agenda-limit-interactively ()
6836 "In agenda, interactively limit entries to various maximums."
6837 (interactive)
6838 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6839 (num (string-to-number (read-from-minibuffer "How many? "))))
6840 (cond ((equal max ?e)
6841 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6842 ((equal max ?t)
6843 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6844 ((equal max ?T)
6845 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6846 ((equal max ?E)
6847 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6848 (org-agenda-fit-window-to-buffer))
6850 (defun org-agenda-highlight-todo (x)
6851 (let ((org-done-keywords org-done-keywords-for-agenda)
6852 (case-fold-search nil)
6854 (if (eq x 'line)
6855 (save-excursion
6856 (beginning-of-line 1)
6857 (setq re (org-get-at-bol 'org-todo-regexp))
6858 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6859 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6860 (add-text-properties (match-beginning 0) (match-end 1)
6861 (list 'face (org-get-todo-face 1)))
6862 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6863 (delete-region (match-beginning 1) (1- (match-end 0)))
6864 (goto-char (match-beginning 1))
6865 (insert (format org-agenda-todo-keyword-format s)))))
6866 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6867 (setq re (get-text-property 0 'org-todo-regexp x))
6868 (when (and re
6869 ;; Test `pl' because if there's no heading content,
6870 ;; there's no point matching to highlight. Note
6871 ;; that if we didn't test `pl' first, and there
6872 ;; happened to be no keyword from `org-todo-regexp'
6873 ;; on this heading line, then the `equal' comparison
6874 ;; afterwards would spuriously succeed in the case
6875 ;; where `pl' is nil -- causing an args-out-of-range
6876 ;; error when we try to add text properties to text
6877 ;; that isn't there.
6879 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6880 x pl) pl))
6881 (add-text-properties
6882 (or (match-end 1) (match-end 0)) (match-end 0)
6883 (list 'face (org-get-todo-face (match-string 2 x)))
6885 (when (match-end 1)
6886 (setq x (concat (substring x 0 (match-end 1))
6887 (format org-agenda-todo-keyword-format
6888 (match-string 2 x))
6889 (org-add-props " " (text-properties-at 0 x))
6890 (substring x (match-end 3)))))))
6891 x)))
6893 (defsubst org-cmp-priority (a b)
6894 "Compare the priorities of string A and B."
6895 (let ((pa (or (get-text-property 1 'priority a) 0))
6896 (pb (or (get-text-property 1 'priority b) 0)))
6897 (cond ((> pa pb) +1)
6898 ((< pa pb) -1))))
6900 (defsubst org-cmp-effort (a b)
6901 "Compare the effort values of string A and B."
6902 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6903 (ea (or (get-text-property 1 'effort-minutes a) def))
6904 (eb (or (get-text-property 1 'effort-minutes b) def)))
6905 (cond ((> ea eb) +1)
6906 ((< ea eb) -1))))
6908 (defsubst org-cmp-category (a b)
6909 "Compare the string values of categories of strings A and B."
6910 (let ((ca (or (get-text-property 1 'org-category a) ""))
6911 (cb (or (get-text-property 1 'org-category b) "")))
6912 (cond ((string-lessp ca cb) -1)
6913 ((string-lessp cb ca) +1))))
6915 (defsubst org-cmp-todo-state (a b)
6916 "Compare the todo states of strings A and B."
6917 (let* ((ma (or (get-text-property 1 'org-marker a)
6918 (get-text-property 1 'org-hd-marker a)))
6919 (mb (or (get-text-property 1 'org-marker b)
6920 (get-text-property 1 'org-hd-marker b)))
6921 (fa (and ma (marker-buffer ma)))
6922 (fb (and mb (marker-buffer mb)))
6923 (todo-kwds
6924 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6925 (and fb (with-current-buffer fb org-todo-keywords-1))))
6926 (ta (or (get-text-property 1 'todo-state a) ""))
6927 (tb (or (get-text-property 1 'todo-state b) ""))
6928 (la (- (length (member ta todo-kwds))))
6929 (lb (- (length (member tb todo-kwds))))
6930 (donepa (member ta org-done-keywords-for-agenda))
6931 (donepb (member tb org-done-keywords-for-agenda)))
6932 (cond ((and donepa (not donepb)) -1)
6933 ((and (not donepa) donepb) +1)
6934 ((< la lb) -1)
6935 ((< lb la) +1))))
6937 (defsubst org-cmp-alpha (a b)
6938 "Compare the headlines, alphabetically."
6939 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6940 (plb (text-property-any 0 (length b) 'org-heading t b))
6941 (ta (and pla (substring a pla)))
6942 (tb (and plb (substring b plb))))
6943 (when pla
6944 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6945 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6946 (setq ta (substring ta (match-end 0))))
6947 (setq ta (downcase ta)))
6948 (when plb
6949 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6950 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6951 (setq tb (substring tb (match-end 0))))
6952 (setq tb (downcase tb)))
6953 (cond ((not ta) +1)
6954 ((not tb) -1)
6955 ((string-lessp ta tb) -1)
6956 ((string-lessp tb ta) +1))))
6958 (defsubst org-cmp-tag (a b)
6959 "Compare the string values of the first tags of A and B."
6960 (let ((ta (car (last (get-text-property 1 'tags a))))
6961 (tb (car (last (get-text-property 1 'tags b)))))
6962 (cond ((not ta) +1)
6963 ((not tb) -1)
6964 ((string-lessp ta tb) -1)
6965 ((string-lessp tb ta) +1))))
6967 (defsubst org-cmp-time (a b)
6968 "Compare the time-of-day values of strings A and B."
6969 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6970 (ta (or (get-text-property 1 'time-of-day a) def))
6971 (tb (or (get-text-property 1 'time-of-day b) def)))
6972 (cond ((< ta tb) -1)
6973 ((< tb ta) +1))))
6975 (defsubst org-cmp-ts (a b &optional type)
6976 "Compare the timestamps values of entries A and B.
6977 When TYPE is \"scheduled\", \"deadline\", \"timestamp\"
6978 or \"timestamp_ia\", compare within each of these type.
6979 When TYPE is the empty string, compare all timestamps
6980 without respect of their type."
6981 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6982 (ta (or (and (string-match type (get-text-property 1 'type a))
6983 (get-text-property 1 'ts-date a)) def))
6984 (tb (or (and (string-match type (get-text-property 1 'type b))
6985 (get-text-property 1 'ts-date b)) def)))
6986 (cond ((< ta tb) -1)
6987 ((< tb ta) +1))))
6989 (defsubst org-cmp-habit-p (a b)
6990 "Compare the todo states of strings A and B."
6991 (let ((ha (get-text-property 1 'org-habit-p a))
6992 (hb (get-text-property 1 'org-habit-p b)))
6993 (cond ((and ha (not hb)) -1)
6994 ((and (not ha) hb) +1))))
6996 (defun org-entries-lessp (a b)
6997 "Predicate for sorting agenda entries."
6998 ;; The following variables will be used when the form is evaluated.
6999 ;; So even though the compiler complains, keep them.
7000 (let* ((ss org-agenda-sorting-strategy-selected)
7001 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7002 (org-cmp-ts a b "")))
7003 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7004 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7005 (org-cmp-ts a b "scheduled")))
7006 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7007 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7008 (org-cmp-ts a b "deadline")))
7009 (deadline-down (if deadline-up (- deadline-up) nil))
7010 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7011 (org-cmp-ts a b "iatimestamp_ia")))
7012 (tsia-down (if tsia-up (- tsia-up) nil))
7013 (ts-up (and (org-em 'ts-up 'ts-down ss)
7014 (org-cmp-ts a b "timestamp")))
7015 (ts-down (if ts-up (- ts-up) nil))
7016 (time-up (and (org-em 'time-up 'time-down ss)
7017 (org-cmp-time a b)))
7018 (time-down (if time-up (- time-up) nil))
7019 (priority-up (and (org-em 'priority-up 'priority-down ss)
7020 (org-cmp-priority a b)))
7021 (priority-down (if priority-up (- priority-up) nil))
7022 (effort-up (and (org-em 'effort-up 'effort-down ss)
7023 (org-cmp-effort a b)))
7024 (effort-down (if effort-up (- effort-up) nil))
7025 (category-up (and (or (org-em 'category-up 'category-down ss)
7026 (memq 'category-keep ss))
7027 (org-cmp-category a b)))
7028 (category-down (if category-up (- category-up) nil))
7029 (category-keep (if category-up +1 nil))
7030 (tag-up (and (org-em 'tag-up 'tag-down ss)
7031 (org-cmp-tag a b)))
7032 (tag-down (if tag-up (- tag-up) nil))
7033 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7034 (org-cmp-todo-state a b)))
7035 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7036 (habit-up (and (org-em 'habit-up 'habit-down ss)
7037 (org-cmp-habit-p a b)))
7038 (habit-down (if habit-up (- habit-up) nil))
7039 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7040 (org-cmp-alpha a b)))
7041 (alpha-down (if alpha-up (- alpha-up) nil))
7042 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7043 user-defined-up user-defined-down)
7044 (if (and need-user-cmp org-agenda-cmp-user-defined
7045 (functionp org-agenda-cmp-user-defined))
7046 (setq user-defined-up
7047 (funcall org-agenda-cmp-user-defined a b)
7048 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7049 (cdr (assoc
7050 (eval (cons 'or org-agenda-sorting-strategy-selected))
7051 '((-1 . t) (1 . nil) (nil . nil))))))
7053 ;;; Agenda restriction lock
7055 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7056 "Overlay to mark the headline to which agenda commands are restricted.")
7057 (overlay-put org-agenda-restriction-lock-overlay
7058 'face 'org-agenda-restriction-lock)
7059 (overlay-put org-agenda-restriction-lock-overlay
7060 'help-echo "Agendas are currently limited to this subtree.")
7061 (org-detach-overlay org-agenda-restriction-lock-overlay)
7063 (defun org-agenda-set-restriction-lock (&optional type)
7064 "Set restriction lock for agenda, to current subtree or file.
7065 Restriction will be the file if TYPE is `file', or if type is the
7066 universal prefix '(4), or if the cursor is before the first headline
7067 in the file. Otherwise, restriction will be to the current subtree."
7068 (interactive "P")
7069 (and (equal type '(4)) (setq type 'file))
7070 (setq type (cond
7071 (type type)
7072 ((org-at-heading-p) 'subtree)
7073 ((condition-case nil (org-back-to-heading t) (error nil))
7074 'subtree)
7075 (t 'file)))
7076 (if (eq type 'subtree)
7077 (progn
7078 (setq org-agenda-restrict t)
7079 (setq org-agenda-overriding-restriction 'subtree)
7080 (put 'org-agenda-files 'org-restrict
7081 (list (buffer-file-name (buffer-base-buffer))))
7082 (org-back-to-heading t)
7083 (move-overlay org-agenda-restriction-lock-overlay
7084 (point)
7085 (if org-agenda-restriction-lock-highlight-subtree
7086 (save-excursion (org-end-of-subtree t t) (point))
7087 (point-at-eol)))
7088 (move-marker org-agenda-restrict-begin (point))
7089 (move-marker org-agenda-restrict-end
7090 (save-excursion (org-end-of-subtree t t)))
7091 (message "Locking agenda restriction to subtree"))
7092 (put 'org-agenda-files 'org-restrict
7093 (list (buffer-file-name (buffer-base-buffer))))
7094 (setq org-agenda-restrict nil)
7095 (setq org-agenda-overriding-restriction 'file)
7096 (move-marker org-agenda-restrict-begin nil)
7097 (move-marker org-agenda-restrict-end nil)
7098 (message "Locking agenda restriction to file"))
7099 (setq current-prefix-arg nil)
7100 (org-agenda-maybe-redo))
7102 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7103 "Remove the agenda restriction lock."
7104 (interactive "P")
7105 (org-detach-overlay org-agenda-restriction-lock-overlay)
7106 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7107 (setq org-agenda-overriding-restriction nil)
7108 (setq org-agenda-restrict nil)
7109 (put 'org-agenda-files 'org-restrict nil)
7110 (move-marker org-agenda-restrict-begin nil)
7111 (move-marker org-agenda-restrict-end nil)
7112 (setq current-prefix-arg nil)
7113 (message "Agenda restriction lock removed")
7114 (or noupdate (org-agenda-maybe-redo)))
7116 (defun org-agenda-maybe-redo ()
7117 "If there is any window showing the agenda view, update it."
7118 (let ((w (get-buffer-window org-agenda-buffer-name t))
7119 (w0 (selected-window)))
7120 (when w
7121 (select-window w)
7122 (org-agenda-redo)
7123 (select-window w0)
7124 (if org-agenda-overriding-restriction
7125 (message "Agenda view shifted to new %s restriction"
7126 org-agenda-overriding-restriction)
7127 (message "Agenda restriction lock removed")))))
7129 ;;; Agenda commands
7131 (defun org-agenda-check-type (error &rest types)
7132 "Check if agenda buffer is of allowed type.
7133 If ERROR is non-nil, throw an error, otherwise just return nil.
7134 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7135 (if (not org-agenda-type)
7136 (error "No Org agenda currently displayed")
7137 (if (memq org-agenda-type types)
7139 (if error
7140 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7141 nil))))
7143 (defun org-agenda-Quit ()
7144 "Exit the agenda and kill buffers loaded by `org-agenda'.
7145 Also restore the window configuration."
7146 (interactive)
7147 (if org-agenda-columns-active
7148 (org-columns-quit)
7149 (let ((buf (current-buffer)))
7150 (if (eq org-agenda-window-setup 'other-frame)
7151 (progn
7152 (org-agenda-reset-markers)
7153 (kill-buffer buf)
7154 (org-columns-remove-overlays)
7155 (setq org-agenda-archives-mode nil)
7156 (delete-frame))
7157 (and (not (eq org-agenda-window-setup 'current-window))
7158 (not (one-window-p))
7159 (delete-window))
7160 (org-agenda-reset-markers)
7161 (kill-buffer buf)
7162 (org-columns-remove-overlays)
7163 (setq org-agenda-archives-mode nil)))
7164 (setq org-agenda-buffer nil)
7165 ;; Maybe restore the pre-agenda window configuration.
7166 (and org-agenda-restore-windows-after-quit
7167 (not (eq org-agenda-window-setup 'other-frame))
7168 org-agenda-pre-window-conf
7169 (set-window-configuration org-agenda-pre-window-conf)
7170 (setq org-agenda-pre-window-conf nil))))
7172 (defun org-agenda-quit ()
7173 "Exit the agenda and restore the window configuration.
7174 When `org-agenda-sticky' is non-nil, only bury the agenda."
7175 (interactive)
7176 (if (and (eq org-indirect-buffer-display 'other-window)
7177 org-last-indirect-buffer)
7178 (let ((org-last-indirect-window
7179 (get-buffer-window org-last-indirect-buffer)))
7180 (if org-last-indirect-window
7181 (delete-window org-last-indirect-window))))
7182 (if org-agenda-columns-active
7183 (org-columns-quit)
7184 (if org-agenda-sticky
7185 (let ((buf (current-buffer)))
7186 (if (eq org-agenda-window-setup 'other-frame)
7187 (progn
7188 (delete-frame))
7189 (and (not (eq org-agenda-window-setup 'current-window))
7190 (not (one-window-p))
7191 (delete-window)))
7192 (with-current-buffer buf
7193 (bury-buffer)
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))))
7200 (org-agenda-Quit))))
7202 (defun org-agenda-exit ()
7203 "Exit the agenda and restore the window configuration.
7204 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7205 buffers visited directly by the user will not be touched."
7206 (interactive)
7207 (org-release-buffers org-agenda-new-buffers)
7208 (setq org-agenda-new-buffers nil)
7209 (org-agenda-Quit))
7211 (defun org-agenda-kill-all-agenda-buffers ()
7212 "Kill all buffers in `org-agenda-mode'.
7213 This is used when toggling sticky agendas.
7214 You can also explicitly invoke it with `C-c a C-k'."
7215 (interactive)
7216 (let (blist)
7217 (dolist (buf (buffer-list))
7218 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7219 (push buf blist)))
7220 (mapc 'kill-buffer blist)))
7222 (defun org-agenda-execute (arg)
7223 "Execute another agenda command, keeping same window.
7224 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7225 in the agenda."
7226 (interactive "P")
7227 (let ((org-agenda-window-setup 'current-window))
7228 (org-agenda arg)))
7230 (defun org-agenda-redo (&optional all)
7231 "Rebuild possibly ALL agenda view(s) in the current buffer."
7232 (interactive "P")
7233 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7234 (cpa (unless (eq all t) current-prefix-arg))
7235 (org-agenda-doing-sticky-redo org-agenda-sticky)
7236 (org-agenda-sticky nil)
7237 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7238 org-agenda-buffer-name))
7239 (org-agenda-keep-modes t)
7240 (tag-filter org-agenda-tag-filter)
7241 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7242 (top-hl-filter org-agenda-top-headline-filter)
7243 (cat-filter org-agenda-category-filter)
7244 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7245 (re-filter org-agenda-regexp-filter)
7246 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7247 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7248 (cols org-agenda-columns-active)
7249 (line (org-current-line))
7250 (window-line (- line (org-current-line (window-start))))
7251 (lprops (get 'org-agenda-redo-command 'org-lprops))
7252 (redo-cmd (get-text-property p 'org-redo-cmd))
7253 (last-args (get-text-property p 'org-last-args))
7254 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7255 (org-agenda-overriding-cmd-arguments
7256 (unless (eq all t)
7257 (cond ((listp last-args)
7258 (cons (or cpa (car last-args)) (cdr last-args)))
7259 ((stringp last-args)
7260 last-args))))
7261 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7262 (put 'org-agenda-tag-filter :preset-filter nil)
7263 (put 'org-agenda-category-filter :preset-filter nil)
7264 (put 'org-agenda-regexp-filter :preset-filter nil)
7265 (and cols (org-columns-quit))
7266 (message "Rebuilding agenda buffer...")
7267 (if series-redo-cmd
7268 (eval series-redo-cmd)
7269 (org-let lprops redo-cmd))
7270 (setq org-agenda-undo-list nil
7271 org-agenda-pending-undo-list nil
7272 org-agenda-tag-filter tag-filter
7273 org-agenda-category-filter cat-filter
7274 org-agenda-regexp-filter re-filter
7275 org-agenda-top-headline-filter top-hl-filter)
7276 (message "Rebuilding agenda buffer...done")
7277 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7278 (put 'org-agenda-category-filter :preset-filter cat-preset)
7279 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7280 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7281 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7282 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7283 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7284 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7285 (org-goto-line line)
7286 (recenter window-line)))
7288 (defvar org-global-tags-completion-table nil)
7289 (defvar org-agenda-filter-form nil)
7290 (defvar org-agenda-filtered-by-category nil)
7292 (defun org-agenda-filter-by-category (strip)
7293 "Keep only those lines in the agenda buffer that have a specific category.
7294 The category is that of the current line."
7295 (interactive "P")
7296 (if (and org-agenda-filtered-by-category
7297 org-agenda-category-filter)
7298 (org-agenda-filter-show-all-cat)
7299 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7300 (if (and cat (not (string= "" cat)))
7301 (org-agenda-filter-apply
7302 (setq org-agenda-category-filter
7303 (list (concat (if strip "-" "+") cat)))
7304 'category)
7305 (error "No category at point")))))
7307 (defun org-find-top-headline (&optional pos)
7308 "Find the topmost parent headline and return it."
7309 (save-excursion
7310 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7311 (if pos (goto-char pos))
7312 ;; Skip up to the topmost parent
7313 (while (ignore-errors (outline-up-heading 1) t))
7314 (ignore-errors
7315 (nth 4 (org-heading-components))))))
7317 (defvar org-agenda-filtered-by-top-headline nil)
7318 (defun org-agenda-filter-by-top-headline (strip)
7319 "Keep only those lines that are descendants from the same top headline.
7320 The top headline is that of the current line."
7321 (interactive "P")
7322 (if org-agenda-filtered-by-top-headline
7323 (progn
7324 (setq org-agenda-filtered-by-top-headline nil
7325 org-agenda-top-headline-filter nil)
7326 (org-agenda-filter-show-all-cat))
7327 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7328 (if cat (org-agenda-filter-top-headline-apply cat strip)
7329 (error "No top-level category at point")))))
7331 (defvar org-agenda-regexp-filter nil)
7332 (defun org-agenda-filter-by-regexp (strip)
7333 "Filter agenda entries by a regular expression.
7334 Regexp filters are cumulative.
7335 With no prefix argument, keep entries matching the regexp.
7336 With one prefix argument, filter out entries matching the regexp.
7337 With two prefix arguments, remove the regexp filters."
7338 (interactive "P")
7339 (if (not (equal strip '(16)))
7340 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7341 (read-from-minibuffer
7342 (if (equal strip '(4))
7343 "Filter out entries matching regexp: "
7344 "Narrow to entries matching regexp: ")))))
7345 (push flt org-agenda-regexp-filter)
7346 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7347 (org-agenda-filter-show-all-re)
7348 (message "Regexp filter removed")))
7350 (defun org-agenda-filter-remove-all ()
7351 "Remove all filters from the current agenda buffer."
7352 (interactive)
7353 (when org-agenda-tag-filter
7354 (org-agenda-filter-show-all-tag))
7355 (when org-agenda-category-filter
7356 (org-agenda-filter-show-all-cat))
7357 (when org-agenda-regexp-filter
7358 (org-agenda-filter-show-all-re)))
7360 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7361 "Keep only those lines in the agenda buffer that have a specific tag.
7362 The tag is selected with its fast selection letter, as configured.
7363 With prefix argument STRIP, remove all lines that do have the tag.
7364 A lisp caller can specify CHAR. NARROW means that the new tag should be
7365 used to narrow the search - the interactive user can also press `-' or `+'
7366 to switch to narrowing."
7367 (interactive "P")
7368 (let* ((alist org-tag-alist-for-agenda)
7369 (tag-chars (mapconcat
7370 (lambda (x) (if (and (not (symbolp (car x)))
7371 (cdr x))
7372 (char-to-string (cdr x))
7373 ""))
7374 alist ""))
7375 (efforts (org-split-string
7376 (or (cdr (assoc (concat org-effort-property "_ALL")
7377 org-global-properties))
7378 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7379 "")))
7380 (effort-op org-agenda-filter-effort-default-operator)
7381 (effort-prompt "")
7382 (inhibit-read-only t)
7383 (current org-agenda-tag-filter)
7384 maybe-refresh a n tag)
7385 (unless char
7386 (message
7387 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7388 (if narrow "Narrow" "Filter") tag-chars
7389 (if org-agenda-auto-exclude-function "[RET], " ""))
7390 (setq char (read-char-exclusive)))
7391 (when (member char '(?+ ?-))
7392 ;; Narrowing down
7393 (cond ((equal char ?-) (setq strip t narrow t))
7394 ((equal char ?+) (setq strip nil narrow t)))
7395 (message
7396 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7397 (setq char (read-char-exclusive)))
7398 (when (member char '(?< ?> ?= ??))
7399 ;; An effort operator
7400 (setq effort-op (char-to-string char))
7401 (setq alist nil) ; to make sure it will be interpreted as effort.
7402 (unless (equal char ??)
7403 (loop for i from 0 to 9 do
7404 (setq effort-prompt
7405 (concat
7406 effort-prompt " ["
7407 (if (= i 9) "0" (int-to-string (1+ i)))
7408 "]" (nth i efforts))))
7409 (message "Effort%s: %s " effort-op effort-prompt)
7410 (setq char (read-char-exclusive))
7411 (when (or (< char ?0) (> char ?9))
7412 (error "Need 1-9,0 to select effort"))))
7413 (when (equal char ?\t)
7414 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7415 (org-set-local 'org-global-tags-completion-table
7416 (org-global-tags-completion-table)))
7417 (let ((completion-ignore-case t))
7418 (setq tag (org-icompleting-read
7419 "Tag: " org-global-tags-completion-table))))
7420 (cond
7421 ((equal char ?\r)
7422 (org-agenda-filter-show-all-tag)
7423 (when org-agenda-auto-exclude-function
7424 (setq org-agenda-tag-filter nil)
7425 (dolist (tag (org-agenda-get-represented-tags))
7426 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7427 (if modifier
7428 (push modifier org-agenda-tag-filter))))
7429 (if (not (null org-agenda-tag-filter))
7430 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7431 (setq maybe-refresh t))
7432 ((equal char ?/)
7433 (org-agenda-filter-show-all-tag)
7434 (when (get 'org-agenda-tag-filter :preset-filter)
7435 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7436 (setq maybe-refresh t))
7437 ((equal char ?. )
7438 (setq org-agenda-tag-filter
7439 (mapcar (lambda(tag) (concat "+" tag))
7440 (org-get-at-bol 'tags)))
7441 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7442 (setq maybe-refresh t))
7443 ((or (equal char ?\ )
7444 (setq a (rassoc char alist))
7445 (and (>= char ?0) (<= char ?9)
7446 (setq n (if (= char ?0) 9 (- char ?0 1))
7447 tag (concat effort-op (nth n efforts))
7448 a (cons tag nil)))
7449 (and (= char ??)
7450 (setq tag "?eff")
7451 a (cons tag nil))
7452 (and tag (setq a (cons tag nil))))
7453 (org-agenda-filter-show-all-tag)
7454 (setq tag (car a))
7455 (setq org-agenda-tag-filter
7456 (cons (concat (if strip "-" "+") tag)
7457 (if narrow current nil)))
7458 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7459 (setq maybe-refresh t))
7460 (t (error "Invalid tag selection character %c" char)))
7461 (when (and maybe-refresh
7462 (eq org-agenda-clockreport-mode 'with-filter))
7463 (org-agenda-redo))))
7465 (defun org-agenda-get-represented-tags ()
7466 "Get a list of all tags currently represented in the agenda."
7467 (let (p tags)
7468 (save-excursion
7469 (goto-char (point-min))
7470 (while (setq p (next-single-property-change (point) 'tags))
7471 (goto-char p)
7472 (mapc (lambda (x) (add-to-list 'tags x))
7473 (get-text-property (point) 'tags))))
7474 tags))
7476 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7477 "Refine the current filter. See `org-agenda-filter-by-tag'."
7478 (interactive "P")
7479 (org-agenda-filter-by-tag strip char 'refine))
7481 (defun org-agenda-filter-make-matcher (filter type)
7482 "Create the form that tests a line for agenda filter."
7483 (let (f f1)
7484 (cond
7485 ;; Tag filter
7486 ((eq type 'tag)
7487 (setq filter
7488 (delete-dups
7489 (append (get 'org-agenda-tag-filter :preset-filter)
7490 filter)))
7491 (dolist (x filter)
7492 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7493 (ffunc
7494 (lambda (nf0 nf01 fltr notgroup op)
7495 (dolist (x fltr)
7496 (if (member x '("-" "+"))
7497 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7498 (if (string-match "[<=>?]" x)
7499 (setq nf01 (org-agenda-filter-effort-form x))
7500 (setq nf01 (list 'member (downcase (substring x 1))
7501 'tags)))
7502 (when (equal (string-to-char x) ?-)
7503 (setq nf01 (list 'not nf01))
7504 (when (not notgroup) (setq op 'and))))
7505 (push nf01 nf0))
7506 (if notgroup
7507 (push (cons 'and nf0) f)
7508 (push (cons (or op 'or) nf0) f)))))
7509 (if (equal nfilter filter)
7510 (funcall ffunc f1 f filter t nil)
7511 (funcall ffunc nf1 nf nfilter nil nil)))))
7512 ;; Category filter
7513 ((eq type 'category)
7514 (setq filter
7515 (delete-dups
7516 (append (get 'org-agenda-category-filter :preset-filter)
7517 filter)))
7518 (dolist (x filter)
7519 (if (equal "-" (substring x 0 1))
7520 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7521 (setq f1 (list 'equal (substring x 1) 'cat)))
7522 (push f1 f)))
7523 ;; Regexp filter
7524 ((eq type 'regexp)
7525 (setq filter
7526 (delete-dups
7527 (append (get 'org-agenda-regexp-filter :preset-filter)
7528 filter)))
7529 (dolist (x filter)
7530 (if (equal "-" (substring x 0 1))
7531 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7532 (setq f1 (list 'string-match (substring x 1) 'txt)))
7533 (push f1 f))))
7534 (cons 'and (nreverse f))))
7536 (defun org-agenda-filter-effort-form (e)
7537 "Return the form to compare the effort of the current line with what E says.
7538 E looks like \"+<2:25\"."
7539 (let (op)
7540 (setq e (substring e 1))
7541 (setq op (string-to-char e) e (substring e 1))
7542 (setq op (cond ((equal op ?<) '<=)
7543 ((equal op ?>) '>=)
7544 ((equal op ??) op)
7545 (t '=)))
7546 (list 'org-agenda-compare-effort (list 'quote op)
7547 (org-duration-string-to-minutes e))))
7549 (defun org-agenda-compare-effort (op value)
7550 "Compare the effort of the current line with VALUE, using OP.
7551 If the line does not have an effort defined, return nil."
7552 (let ((eff (org-get-at-bol 'effort-minutes)))
7553 (if (equal op ??)
7554 (not eff)
7555 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7556 value))))
7558 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7559 "Expand group tags in FILTER for the agenda.
7560 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7561 (if org-group-tags
7562 (let ((case-fold-search t) rtn)
7563 (mapc
7564 (lambda (f)
7565 (let (f0 dir)
7566 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7567 (setq dir (match-string 1 f) f0 (match-string 2 f))
7568 (setq dir (if no-operator "" "+") f0 f))
7569 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7570 (org-tags-expand f0 t t))
7571 rtn))))
7572 filter)
7573 (reverse rtn))
7574 filter))
7576 (defun org-agenda-filter-apply (filter type)
7577 "Set FILTER as the new agenda filter and apply it."
7578 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7579 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7580 (let (tags cat txt)
7581 (setq org-agenda-filter-form
7582 (org-agenda-filter-make-matcher filter type))
7583 (if (and (eq type 'category)
7584 (not (equal (substring (car filter) 0 1) "-")))
7585 ;; Only set `org-agenda-filtered-by-category' to t
7586 ;; when a unique category is used as the filter
7587 (setq org-agenda-filtered-by-category t))
7588 (org-agenda-set-mode-name)
7589 (save-excursion
7590 (goto-char (point-min))
7591 (while (not (eobp))
7592 (if (org-get-at-bol 'org-marker)
7593 (progn
7594 (setq tags ; used in eval
7595 (apply 'append
7596 (mapcar (lambda (f)
7597 (org-agenda-filter-expand-tags (list f) t))
7598 (org-get-at-bol 'tags)))
7599 cat (get-text-property (point) 'org-category)
7600 txt (get-text-property (point) 'txt))
7601 (if (not (eval org-agenda-filter-form))
7602 (org-agenda-filter-hide-line type))
7603 (beginning-of-line 2))
7604 (beginning-of-line 2))))
7605 (if (get-char-property (point) 'invisible)
7606 (ignore-errors (org-agenda-previous-line)))))
7608 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7609 "Filter by top headline HL."
7610 (org-agenda-set-mode-name)
7611 (save-excursion
7612 (goto-char (point-min))
7613 (while (not (eobp))
7614 (let* ((pos (org-get-at-bol 'org-hd-marker))
7615 (tophl (and pos (org-find-top-headline pos))))
7616 (if (and tophl (funcall (if negative 'identity 'not)
7617 (string= hl tophl)))
7618 (org-agenda-filter-hide-line 'category)))
7619 (beginning-of-line 2)))
7620 (if (get-char-property (point) 'invisible)
7621 (org-agenda-previous-line))
7622 (setq org-agenda-top-headline-filter hl
7623 org-agenda-filtered-by-top-headline t))
7625 (defun org-agenda-filter-hide-line (type)
7626 "Hide lines with TYPE in the agenda buffer."
7627 (let (ov)
7628 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7629 (point-at-eol)))
7630 (overlay-put ov 'invisible t)
7631 (overlay-put ov 'type type)
7632 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7633 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7634 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7636 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7637 (setq pos (or pos (point)))
7638 (save-excursion
7639 (dolist (ov (overlays-at pos))
7640 (when (and (overlay-get ov 'invisible)
7641 (eq (overlay-get ov 'type) 'tag))
7642 (goto-char pos)
7643 (if (< (overlay-start ov) (point-at-eol))
7644 (move-overlay ov (point-at-eol)
7645 (overlay-end ov)))))))
7647 (defun org-agenda-filter-show-all-tag nil
7648 "Remove tag filter overlays from the agenda buffer."
7649 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7650 (setq org-agenda-tag-filter-overlays nil
7651 org-agenda-tag-filter nil
7652 org-agenda-filter-form nil)
7653 (org-agenda-set-mode-name))
7655 (defun org-agenda-filter-show-all-re nil
7656 "Remove regexp filter overlays from the agenda buffer."
7657 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7658 (setq org-agenda-re-filter-overlays nil
7659 org-agenda-regexp-filter nil
7660 org-agenda-filter-form nil)
7661 (org-agenda-set-mode-name))
7663 (defun org-agenda-filter-show-all-cat nil
7664 "Remove category filter overlays from the agenda buffer."
7665 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7666 (setq org-agenda-cat-filter-overlays nil
7667 org-agenda-filtered-by-category nil
7668 org-agenda-category-filter nil
7669 org-agenda-filter-form nil)
7670 (org-agenda-set-mode-name))
7672 (defun org-agenda-manipulate-query-add ()
7673 "Manipulate the query by adding a search term with positive selection.
7674 Positive selection means the term must be matched for selection of an entry."
7675 (interactive)
7676 (org-agenda-manipulate-query ?\[))
7677 (defun org-agenda-manipulate-query-subtract ()
7678 "Manipulate the query by adding a search term with negative selection.
7679 Negative selection means term must not be matched for selection of an entry."
7680 (interactive)
7681 (org-agenda-manipulate-query ?\]))
7682 (defun org-agenda-manipulate-query-add-re ()
7683 "Manipulate the query by adding a search regexp with positive selection.
7684 Positive selection means the regexp must match for selection of an entry."
7685 (interactive)
7686 (org-agenda-manipulate-query ?\{))
7687 (defun org-agenda-manipulate-query-subtract-re ()
7688 "Manipulate the query by adding a search regexp with negative selection.
7689 Negative selection means regexp must not match for selection of an entry."
7690 (interactive)
7691 (org-agenda-manipulate-query ?\}))
7692 (defun org-agenda-manipulate-query (char)
7693 (cond
7694 ((memq org-agenda-type '(timeline agenda))
7695 (let ((org-agenda-include-inactive-timestamps t))
7696 (org-agenda-redo))
7697 (message "Display now includes inactive timestamps as well"))
7698 ((eq org-agenda-type 'search)
7699 (org-add-to-string
7700 'org-agenda-query-string
7701 (if org-agenda-last-search-view-search-was-boolean
7702 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7703 (?\{ . " +{}") (?\} . " -{}"))))
7704 " "))
7705 (setq org-agenda-redo-command
7706 (list 'org-search-view
7707 (car (get-text-property (min (1- (point-max)) (point))
7708 'org-last-args))
7709 org-agenda-query-string
7710 (+ (length org-agenda-query-string)
7711 (if (member char '(?\{ ?\})) 0 1))))
7712 (set-register org-agenda-query-register org-agenda-query-string)
7713 (let ((org-agenda-overriding-arguments
7714 (cdr org-agenda-redo-command)))
7715 (org-agenda-redo)))
7716 (t (error "Cannot manipulate query for %s-type agenda buffers"
7717 org-agenda-type))))
7719 (defun org-add-to-string (var string)
7720 (set var (concat (symbol-value var) string)))
7722 (defun org-agenda-goto-date (span)
7723 "Jump to DATE in agenda."
7724 (interactive "P")
7725 (let* ((org-read-date-prefer-future
7726 (eval org-agenda-jump-prefer-future))
7727 (date (org-read-date))
7728 (day (time-to-days (org-time-string-to-time date)))
7729 (org-agenda-sticky-orig org-agenda-sticky)
7730 (org-agenda-buffer-tmp-name (buffer-name))
7731 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7732 (0-arg (or current-prefix-arg (car args)))
7733 (2-arg (nth 2 args))
7734 (with-hour-p (nth 4 org-agenda-redo-command))
7735 (newcmd (list 'org-agenda-list 0-arg date
7736 (org-agenda-span-to-ndays
7737 2-arg (org-time-string-to-absolute date))
7738 with-hour-p))
7739 (newargs (cdr newcmd))
7740 (inhibit-read-only t)
7741 org-agenda-sticky)
7742 (if (not (org-agenda-check-type t 'agenda))
7743 (error "Not available in non-agenda views")
7744 (add-text-properties (point-min) (point-max)
7745 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7746 (org-agenda-redo)
7747 (goto-char (point-min))
7748 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7749 (save-excursion (move-beginning-of-line 2) (eobp))))
7750 (move-beginning-of-line 2))
7751 (setq org-agenda-sticky org-agenda-sticky-orig
7752 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7754 (defun org-agenda-goto-today ()
7755 "Go to today."
7756 (interactive)
7757 (org-agenda-check-type t 'timeline 'agenda)
7758 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7759 (curspan (nth 2 args))
7760 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7761 (cond
7762 (tdpos (goto-char tdpos))
7763 ((eq org-agenda-type 'agenda)
7764 (let* ((sd (org-agenda-compute-starting-span
7765 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7766 (org-agenda-overriding-arguments args))
7767 (setf (nth 1 org-agenda-overriding-arguments) sd)
7768 (org-agenda-redo)
7769 (org-agenda-find-same-or-today-or-agenda)))
7770 (t (error "Cannot find today")))))
7772 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7773 (goto-char
7774 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7775 (text-property-any (point-min) (point-max) 'org-today t)
7776 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7777 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7778 (org-agenda-goto-block-beginning))
7779 (point-min))))
7781 (defun org-agenda-goto-block-beginning ()
7782 "Go the agenda block beginning."
7783 (interactive)
7784 (if (not (derived-mode-p 'org-agenda-mode))
7785 (error "Cannot execute this command outside of org-agenda-mode buffers")
7786 (let (dest)
7787 (save-excursion
7788 (unless (looking-at "\\'")
7789 (forward-char))
7790 (let* ((prop 'org-agenda-structural-header)
7791 (p (previous-single-property-change (point) prop))
7792 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7793 (1- (point))) prop)))
7794 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7795 (if (not dest)
7796 (error "Cannot find the beginning of the blog")
7797 (goto-char dest)
7798 (move-beginning-of-line 1)))))
7800 (defun org-agenda-later (arg)
7801 "Go forward in time by the current span.
7802 With prefix ARG, go forward that many times the current span."
7803 (interactive "p")
7804 (org-agenda-check-type t 'agenda)
7805 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7806 (span (or (nth 2 args) org-agenda-current-span))
7807 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7808 (greg (calendar-gregorian-from-absolute sd))
7809 (cnt (org-get-at-bol 'org-day-cnt))
7810 greg2)
7811 (cond
7812 ((numberp span)
7813 (setq sd (+ (* span arg) sd)))
7814 ((eq span 'day)
7815 (setq sd (+ arg sd)))
7816 ((eq span 'week)
7817 (setq sd (+ (* 7 arg) sd)))
7818 ((eq span 'month)
7819 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7820 sd (calendar-absolute-from-gregorian greg2))
7821 (setcar greg2 (1+ (car greg2))))
7822 ((eq span 'year)
7823 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7824 sd (calendar-absolute-from-gregorian greg2))
7825 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7827 (setq sd (+ (* span arg) sd))))
7828 (let ((org-agenda-overriding-cmd
7829 ;; `cmd' may have been set by `org-agenda-run-series' which
7830 ;; uses `org-agenda-overriding-cmd' to decide whether
7831 ;; overriding is allowed for `cmd'
7832 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7833 (org-agenda-overriding-arguments
7834 (list (car args) sd span)))
7835 (org-agenda-redo)
7836 (org-agenda-find-same-or-today-or-agenda cnt))))
7838 (defun org-agenda-earlier (arg)
7839 "Go backward in time by the current span.
7840 With prefix ARG, go backward that many times the current span."
7841 (interactive "p")
7842 (org-agenda-later (- arg)))
7844 (defun org-agenda-view-mode-dispatch ()
7845 "Call one of the view mode commands."
7846 (interactive)
7847 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7848 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7849 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7850 (let ((a (read-char-exclusive)))
7851 (case a
7852 (?\ (call-interactively 'org-agenda-reset-view))
7853 (?d (call-interactively 'org-agenda-day-view))
7854 (?w (call-interactively 'org-agenda-week-view))
7855 (?m (call-interactively 'org-agenda-month-view))
7856 (?y (call-interactively 'org-agenda-year-view))
7857 (?l (call-interactively 'org-agenda-log-mode))
7858 (?L (org-agenda-log-mode '(4)))
7859 (?c (org-agenda-log-mode 'clockcheck))
7860 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7861 (?a (call-interactively 'org-agenda-archives-mode))
7862 (?A (org-agenda-archives-mode 'files))
7863 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7864 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7865 (?G (call-interactively 'org-agenda-toggle-time-grid))
7866 (?D (call-interactively 'org-agenda-toggle-diary))
7867 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7868 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7869 (org-agenda-check-type t 'timeline 'agenda)
7870 (org-agenda-redo))
7871 (message "Display now includes inactive timestamps as well"))
7872 (?q (message "Abort"))
7873 (otherwise (error "Invalid key" )))))
7875 (defun org-agenda-reset-view ()
7876 "Switch to default view for agenda."
7877 (interactive)
7878 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7879 (defun org-agenda-day-view (&optional day-of-month)
7880 "Switch to daily view for agenda.
7881 With argument DAY-OF-MONTH, switch to that day of the month."
7882 (interactive "P")
7883 (org-agenda-change-time-span 'day day-of-month))
7884 (defun org-agenda-week-view (&optional iso-week)
7885 "Switch to daily view for agenda.
7886 With argument ISO-WEEK, switch to the corresponding ISO week.
7887 If ISO-WEEK has more then 2 digits, only the last two encode the
7888 week. Any digits before this encode a year. So 200712 means
7889 week 12 of year 2007. Years in the range 1938-2037 can also be
7890 written as 2-digit years."
7891 (interactive "P")
7892 (org-agenda-change-time-span 'week iso-week))
7893 (defun org-agenda-month-view (&optional month)
7894 "Switch to monthly view for agenda.
7895 With argument MONTH, switch to that month."
7896 (interactive "P")
7897 (org-agenda-change-time-span 'month month))
7898 (defun org-agenda-year-view (&optional year)
7899 "Switch to yearly view for agenda.
7900 With argument YEAR, switch to that year.
7901 If MONTH has more then 2 digits, only the last two encode the
7902 month. Any digits before this encode a year. So 200712 means
7903 December year 2007. Years in the range 1938-2037 can also be
7904 written as 2-digit years."
7905 (interactive "P")
7906 (when year
7907 (setq year (org-small-year-to-year year)))
7908 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7909 (org-agenda-change-time-span 'year year)
7910 (error "Abort")))
7912 (defun org-agenda-change-time-span (span &optional n)
7913 "Change the agenda view to SPAN.
7914 SPAN may be `day', `week', `month', `year'."
7915 (org-agenda-check-type t 'agenda)
7916 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7917 (curspan (nth 2 args)))
7918 (if (and (not n) (equal curspan span))
7919 (error "Viewing span is already \"%s\"" span))
7920 (let* ((sd (or (org-get-at-bol 'day)
7921 (nth 1 args)
7922 org-starting-day))
7923 (sd (org-agenda-compute-starting-span sd span n))
7924 (org-agenda-overriding-cmd
7925 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7926 (org-agenda-overriding-arguments
7927 (list (car args) sd span)))
7928 (org-agenda-redo)
7929 (org-agenda-find-same-or-today-or-agenda))
7930 (org-agenda-set-mode-name)
7931 (message "Switched to %s view" span)))
7933 (defun org-agenda-compute-starting-span (sd span &optional n)
7934 "Compute starting date for agenda.
7935 SPAN may be `day', `week', `month', `year'. The return value
7936 is a cons cell with the starting date and the number of days,
7937 so that the date SD will be in that range."
7938 (let* ((greg (calendar-gregorian-from-absolute sd))
7939 (dg (nth 1 greg))
7940 (mg (car greg))
7941 (yg (nth 2 greg)))
7942 (cond
7943 ((eq span 'day)
7944 (when n
7945 (setq sd (+ (calendar-absolute-from-gregorian
7946 (list mg 1 yg))
7947 n -1))))
7948 ((eq span 'week)
7949 (let* ((nt (calendar-day-of-week
7950 (calendar-gregorian-from-absolute sd)))
7951 (d (if org-agenda-start-on-weekday
7952 (- nt org-agenda-start-on-weekday)
7955 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7956 (when n
7957 (require 'cal-iso)
7958 (when (> n 99)
7959 (setq y1 (org-small-year-to-year (/ n 100))
7960 n (mod n 100)))
7961 (setq sd
7962 (calendar-absolute-from-iso
7963 (list n 1
7964 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7965 ((eq span 'month)
7966 (let (y1)
7967 (when (and n (> n 99))
7968 (setq y1 (org-small-year-to-year (/ n 100))
7969 n (mod n 100)))
7970 (setq sd (calendar-absolute-from-gregorian
7971 (list (or n mg) 1 (or y1 yg))))))
7972 ((eq span 'year)
7973 (setq sd (calendar-absolute-from-gregorian
7974 (list 1 1 (or n yg))))))
7975 sd))
7977 (defun org-agenda-next-date-line (&optional arg)
7978 "Jump to the next line indicating a date in agenda buffer."
7979 (interactive "p")
7980 (org-agenda-check-type t 'agenda 'timeline)
7981 (beginning-of-line 1)
7982 ;; This does not work if user makes date format that starts with a blank
7983 (if (looking-at "^\\S-") (forward-char 1))
7984 (if (not (re-search-forward "^\\S-" nil t arg))
7985 (progn
7986 (backward-char 1)
7987 (error "No next date after this line in this buffer")))
7988 (goto-char (match-beginning 0)))
7990 (defun org-agenda-previous-date-line (&optional arg)
7991 "Jump to the previous line indicating a date in agenda buffer."
7992 (interactive "p")
7993 (org-agenda-check-type t 'agenda 'timeline)
7994 (beginning-of-line 1)
7995 (if (not (re-search-backward "^\\S-" nil t arg))
7996 (error "No previous date before this line in this buffer")))
7998 ;; Initialize the highlight
7999 (defvar org-hl (make-overlay 1 1))
8000 (overlay-put org-hl 'face 'highlight)
8002 (defun org-highlight (begin end &optional buffer)
8003 "Highlight a region with overlay."
8004 (move-overlay org-hl begin end (or buffer (current-buffer))))
8006 (defun org-unhighlight ()
8007 "Detach overlay INDEX."
8008 (org-detach-overlay org-hl))
8010 (defun org-unhighlight-once ()
8011 "Remove the highlight from its position, and this function from the hook."
8012 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8013 (org-unhighlight))
8015 (defvar org-agenda-pre-follow-window-conf nil)
8016 (defun org-agenda-follow-mode ()
8017 "Toggle follow mode in an agenda buffer."
8018 (interactive)
8019 (unless org-agenda-follow-mode
8020 (setq org-agenda-pre-follow-window-conf
8021 (current-window-configuration)))
8022 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8023 (unless org-agenda-follow-mode
8024 (set-window-configuration org-agenda-pre-follow-window-conf))
8025 (org-agenda-set-mode-name)
8026 (org-agenda-do-context-action)
8027 (message "Follow mode is %s"
8028 (if org-agenda-follow-mode "on" "off")))
8030 (defun org-agenda-entry-text-mode (&optional arg)
8031 "Toggle entry text mode in an agenda buffer."
8032 (interactive "P")
8033 (if (or org-agenda-tag-filter
8034 org-agenda-category-filter
8035 org-agenda-top-headline-filter)
8036 (user-error "Can't show entry text in filtered views")
8037 (setq org-agenda-entry-text-mode (or (integerp arg)
8038 (not org-agenda-entry-text-mode)))
8039 (org-agenda-entry-text-hide)
8040 (and org-agenda-entry-text-mode
8041 (let ((org-agenda-entry-text-maxlines
8042 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8043 (org-agenda-entry-text-show)))
8044 (org-agenda-set-mode-name)
8045 (message "Entry text mode is %s%s"
8046 (if org-agenda-entry-text-mode "on" "off")
8047 (if (not org-agenda-entry-text-mode) ""
8048 (format " (maximum number of lines is %d)"
8049 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8051 (defun org-agenda-clockreport-mode (&optional with-filter)
8052 "Toggle clocktable mode in an agenda buffer.
8053 With prefix arg WITH-FILTER, make the clocktable respect the current
8054 agenda filter."
8055 (interactive "P")
8056 (org-agenda-check-type t 'agenda)
8057 (if with-filter
8058 (setq org-agenda-clockreport-mode 'with-filter)
8059 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8060 (org-agenda-set-mode-name)
8061 (org-agenda-redo)
8062 (message "Clocktable mode is %s"
8063 (if org-agenda-clockreport-mode "on" "off")))
8065 (defun org-agenda-log-mode (&optional special)
8066 "Toggle log mode in an agenda buffer.
8067 With argument SPECIAL, show all possible log items, not only the ones
8068 configured in `org-agenda-log-mode-items'.
8069 With a double `C-u' prefix arg, show *only* log items, nothing else."
8070 (interactive "P")
8071 (org-agenda-check-type t 'agenda 'timeline)
8072 (setq org-agenda-show-log
8073 (cond
8074 ((equal special '(16)) 'only)
8075 ((eq special 'clockcheck)
8076 (if (eq org-agenda-show-log 'clockcheck)
8077 nil 'clockcheck))
8078 (special '(closed clock state))
8079 (t (not org-agenda-show-log))))
8080 (org-agenda-set-mode-name)
8081 (org-agenda-redo)
8082 (message "Log mode is %s"
8083 (if org-agenda-show-log "on" "off")))
8085 (defun org-agenda-archives-mode (&optional with-files)
8086 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8087 When called with a prefix argument, include all archive files as well."
8088 (interactive "P")
8089 (setq org-agenda-archives-mode
8090 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8091 (org-agenda-set-mode-name)
8092 (org-agenda-redo)
8093 (message
8094 "%s"
8095 (cond
8096 ((eq org-agenda-archives-mode nil)
8097 "No archives are included")
8098 ((eq org-agenda-archives-mode 'trees)
8099 (format "Trees with :%s: tag are included" org-archive-tag))
8100 ((eq org-agenda-archives-mode t)
8101 (format "Trees with :%s: tag and all active archive files are included"
8102 org-archive-tag)))))
8104 (defun org-agenda-toggle-diary ()
8105 "Toggle diary inclusion in an agenda buffer."
8106 (interactive)
8107 (org-agenda-check-type t 'agenda)
8108 (setq org-agenda-include-diary (not org-agenda-include-diary))
8109 (org-agenda-redo)
8110 (org-agenda-set-mode-name)
8111 (message "Diary inclusion turned %s"
8112 (if org-agenda-include-diary "on" "off")))
8114 (defun org-agenda-toggle-deadlines ()
8115 "Toggle inclusion of entries with a deadline in an agenda buffer."
8116 (interactive)
8117 (org-agenda-check-type t 'agenda)
8118 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8119 (org-agenda-redo)
8120 (org-agenda-set-mode-name)
8121 (message "Deadlines inclusion turned %s"
8122 (if org-agenda-include-deadlines "on" "off")))
8124 (defun org-agenda-toggle-time-grid ()
8125 "Toggle time grid in an agenda buffer."
8126 (interactive)
8127 (org-agenda-check-type t 'agenda)
8128 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8129 (org-agenda-redo)
8130 (org-agenda-set-mode-name)
8131 (message "Time-grid turned %s"
8132 (if org-agenda-use-time-grid "on" "off")))
8134 (defun org-agenda-set-mode-name ()
8135 "Set the mode name to indicate all the small mode settings."
8136 (setq mode-name
8137 (list "Org-Agenda"
8138 (if (get 'org-agenda-files 'org-restrict) " []" "")
8140 '(:eval (org-agenda-span-name org-agenda-current-span))
8141 (if org-agenda-follow-mode " Follow" "")
8142 (if org-agenda-entry-text-mode " ETxt" "")
8143 (if org-agenda-include-diary " Diary" "")
8144 (if org-agenda-include-deadlines " Ddl" "")
8145 (if org-agenda-use-time-grid " Grid" "")
8146 (if (and (boundp 'org-habit-show-habits)
8147 org-habit-show-habits) " Habit" "")
8148 (cond
8149 ((consp org-agenda-show-log) " LogAll")
8150 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8151 (org-agenda-show-log " Log")
8152 (t ""))
8153 (if (or org-agenda-category-filter
8154 (get 'org-agenda-category-filter :preset-filter))
8155 '(:eval (org-propertize
8156 (concat " <"
8157 (mapconcat
8158 'identity
8159 (append
8160 (get 'org-agenda-category-filter :preset-filter)
8161 org-agenda-category-filter)
8163 ">")
8164 'face 'org-agenda-filter-category
8165 'help-echo "Category used in filtering")) "")
8166 (if (or org-agenda-tag-filter
8167 (get 'org-agenda-tag-filter :preset-filter))
8168 '(:eval (org-propertize
8169 (concat " {"
8170 (mapconcat
8171 'identity
8172 (append
8173 (get 'org-agenda-tag-filter :preset-filter)
8174 org-agenda-tag-filter)
8176 "}")
8177 'face 'org-agenda-filter-tags
8178 'help-echo "Tags used in filtering")) "")
8179 (if (or org-agenda-regexp-filter
8180 (get 'org-agenda-regexp-filter :preset-filter))
8181 '(:eval (org-propertize
8182 (concat " ["
8183 (mapconcat
8184 'identity
8185 (append
8186 (get 'org-agenda-regexp-filter :preset-filter)
8187 org-agenda-regexp-filter)
8189 "]")
8190 'face 'org-agenda-filter-regexp
8191 'help-echo "Regexp used in filtering")) "")
8192 (if org-agenda-archives-mode
8193 (if (eq org-agenda-archives-mode t)
8194 " Archives"
8195 (format " :%s:" org-archive-tag))
8197 (if org-agenda-clockreport-mode
8198 (if (eq org-agenda-clockreport-mode 'with-filter)
8199 " Clock{}" " Clock")
8200 "")))
8201 (force-mode-line-update))
8203 (define-obsolete-function-alias
8204 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8206 (defun org-agenda-update-agenda-type ()
8207 "Update the agenda type after each command."
8208 (setq org-agenda-type
8209 (or (get-text-property (point) 'org-agenda-type)
8210 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8212 (defun org-agenda-next-line ()
8213 "Move cursor to the next line, and show if follow mode is active."
8214 (interactive)
8215 (call-interactively 'next-line)
8216 (org-agenda-do-context-action))
8218 (defun org-agenda-previous-line ()
8219 "Move cursor to the previous line, and show if follow-mode is active."
8220 (interactive)
8221 (call-interactively 'previous-line)
8222 (org-agenda-do-context-action))
8224 (defun org-agenda-next-item (n)
8225 "Move cursor to next agenda item."
8226 (interactive "p")
8227 (let ((col (current-column)))
8228 (dotimes (c n)
8229 (when (next-single-property-change (point-at-eol) 'org-marker)
8230 (move-end-of-line 1)
8231 (goto-char (next-single-property-change (point) 'org-marker))))
8232 (org-move-to-column col))
8233 (org-agenda-do-context-action))
8235 (defun org-agenda-previous-item (n)
8236 "Move cursor to next agenda item."
8237 (interactive "p")
8238 (dotimes (c n)
8239 (let ((col (current-column))
8240 (goto (save-excursion
8241 (move-end-of-line 0)
8242 (previous-single-property-change (point) 'org-marker))))
8243 (if goto (goto-char goto))
8244 (org-move-to-column col)))
8245 (org-agenda-do-context-action))
8247 (defun org-agenda-do-context-action ()
8248 "Show outline path and, maybe, follow mode window."
8249 (let ((m (org-get-at-bol 'org-marker)))
8250 (when (and (markerp m) (marker-buffer m))
8251 (and org-agenda-follow-mode
8252 (if org-agenda-follow-indirect
8253 (org-agenda-tree-to-indirect-buffer nil)
8254 (org-agenda-show)))
8255 (and org-agenda-show-outline-path
8256 (org-with-point-at m (org-display-outline-path t))))))
8258 (defun org-agenda-show-tags ()
8259 "Show the tags applicable to the current item."
8260 (interactive)
8261 (let* ((tags (org-get-at-bol 'tags)))
8262 (if tags
8263 (message "Tags are :%s:"
8264 (org-no-properties (mapconcat 'identity tags ":")))
8265 (message "No tags associated with this line"))))
8267 (defun org-agenda-goto (&optional highlight)
8268 "Go to the Org-mode file which contains the item at point."
8269 (interactive)
8270 (let* ((marker (or (org-get-at-bol 'org-marker)
8271 (org-agenda-error)))
8272 (buffer (marker-buffer marker))
8273 (pos (marker-position marker)))
8274 (switch-to-buffer-other-window buffer)
8275 (widen)
8276 (push-mark)
8277 (goto-char pos)
8278 (when (derived-mode-p 'org-mode)
8279 (org-show-context 'agenda)
8280 (save-excursion
8281 (and (outline-next-heading)
8282 (org-flag-heading nil)))) ; show the next heading
8283 (when (outline-invisible-p)
8284 (show-entry)) ; display invisible text
8285 (recenter (/ (window-height) 2))
8286 (run-hooks 'org-agenda-after-show-hook)
8287 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8289 (defvar org-agenda-after-show-hook nil
8290 "Normal hook run after an item has been shown from the agenda.
8291 Point is in the buffer where the item originated.")
8293 (defun org-agenda-kill ()
8294 "Kill the entry or subtree belonging to the current agenda entry."
8295 (interactive)
8296 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8297 (let* ((bufname-orig (buffer-name))
8298 (marker (or (org-get-at-bol 'org-marker)
8299 (org-agenda-error)))
8300 (buffer (marker-buffer marker))
8301 (pos (marker-position marker))
8302 (type (org-get-at-bol 'type))
8303 dbeg dend (n 0) conf)
8304 (org-with-remote-undo buffer
8305 (with-current-buffer buffer
8306 (save-excursion
8307 (goto-char pos)
8308 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8309 (setq dbeg (progn (org-back-to-heading t) (point))
8310 dend (org-end-of-subtree t t))
8311 (setq dbeg (point-at-bol)
8312 dend (min (point-max) (1+ (point-at-eol)))))
8313 (goto-char dbeg)
8314 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8315 (setq conf (or (eq t org-agenda-confirm-kill)
8316 (and (numberp org-agenda-confirm-kill)
8317 (> n org-agenda-confirm-kill))))
8318 (and conf
8319 (not (y-or-n-p
8320 (format "Delete entry with %d lines in buffer \"%s\"? "
8321 n (buffer-name buffer))))
8322 (error "Abort"))
8323 (let ((org-agenda-buffer-name bufname-orig))
8324 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8325 (with-current-buffer buffer (delete-region dbeg dend))
8326 (message "Agenda item and source killed"))))
8328 (defvar org-archive-default-command) ; defined in org-archive.el
8329 (defun org-agenda-archive-default ()
8330 "Archive the entry or subtree belonging to the current agenda entry."
8331 (interactive)
8332 (require 'org-archive)
8333 (org-agenda-archive-with org-archive-default-command))
8335 (defun org-agenda-archive-default-with-confirmation ()
8336 "Archive the entry or subtree belonging to the current agenda entry."
8337 (interactive)
8338 (require 'org-archive)
8339 (org-agenda-archive-with org-archive-default-command 'confirm))
8341 (defun org-agenda-archive ()
8342 "Archive the entry or subtree belonging to the current agenda entry."
8343 (interactive)
8344 (org-agenda-archive-with 'org-archive-subtree))
8346 (defun org-agenda-archive-to-archive-sibling ()
8347 "Move the entry to the archive sibling."
8348 (interactive)
8349 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8351 (defun org-agenda-archive-with (cmd &optional confirm)
8352 "Move the entry to the archive sibling."
8353 (interactive)
8354 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8355 (let* ((bufname-orig (buffer-name))
8356 (marker (or (org-get-at-bol 'org-marker)
8357 (org-agenda-error)))
8358 (buffer (marker-buffer marker))
8359 (pos (marker-position marker)))
8360 (org-with-remote-undo buffer
8361 (with-current-buffer buffer
8362 (if (derived-mode-p 'org-mode)
8363 (if (and confirm
8364 (not (y-or-n-p "Archive this subtree or entry? ")))
8365 (error "Abort")
8366 (save-excursion
8367 (goto-char pos)
8368 (let ((org-agenda-buffer-name bufname-orig))
8369 (org-remove-subtree-entries-from-agenda))
8370 (org-back-to-heading t)
8371 (funcall cmd)))
8372 (error "Archiving works only in Org-mode files"))))))
8374 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8375 "Remove all lines in the agenda that correspond to a given subtree.
8376 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8377 If this information is not given, the function uses the tree at point."
8378 (let ((buf (or buf (current-buffer))) m p)
8379 (save-excursion
8380 (unless (and beg end)
8381 (org-back-to-heading t)
8382 (setq beg (point))
8383 (org-end-of-subtree t)
8384 (setq end (point)))
8385 (set-buffer (get-buffer org-agenda-buffer-name))
8386 (save-excursion
8387 (goto-char (point-max))
8388 (beginning-of-line 1)
8389 (while (not (bobp))
8390 (when (and (setq m (org-get-at-bol 'org-marker))
8391 (equal buf (marker-buffer m))
8392 (setq p (marker-position m))
8393 (>= p beg)
8394 (< p end))
8395 (let ((inhibit-read-only t))
8396 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8397 (beginning-of-line 0))))))
8399 (defun org-agenda-refile (&optional goto rfloc no-update)
8400 "Refile the item at point.
8402 When GOTO is 0 or '(64), clear the refile cache.
8403 When GOTO is '(16), go to the location of the last refiled item.
8404 RFLOC can be a refile location obtained in a different way.
8405 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8406 (interactive "P")
8407 (cond
8408 ((member goto '(0 (64)))
8409 (org-refile-cache-clear))
8410 ((equal goto '(16))
8411 (org-refile-goto-last-stored))
8413 (let* ((buffer-orig (buffer-name))
8414 (marker (or (org-get-at-bol 'org-hd-marker)
8415 (org-agenda-error)))
8416 (buffer (marker-buffer marker))
8417 (pos (marker-position marker))
8418 (rfloc (or rfloc
8419 (org-refile-get-location
8420 (if goto "Goto" "Refile to") buffer
8421 org-refile-allow-creating-parent-nodes))))
8422 (with-current-buffer buffer
8423 (save-excursion
8424 (save-restriction
8425 (widen)
8426 (goto-char marker)
8427 (let ((org-agenda-buffer-name buffer-orig))
8428 (org-remove-subtree-entries-from-agenda))
8429 (org-refile goto buffer rfloc)))))
8430 (unless no-update (org-agenda-redo)))))
8432 (defun org-agenda-open-link (&optional arg)
8433 "Open the link(s) in the current entry, if any.
8434 This looks for a link in the displayed line in the agenda.
8435 It also looks at the text of the entry itself."
8436 (interactive "P")
8437 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8438 (org-get-at-bol 'org-marker)))
8439 (buffer (and marker (marker-buffer marker)))
8440 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8441 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8442 (lk0 (car lkall))
8443 (lk (if (stringp lk0) (list lk0) lk0))
8444 (lkend (cdr lkall))
8445 trg)
8446 (cond
8447 ((and buffer lk)
8448 (mapcar (lambda(l)
8449 (with-current-buffer buffer
8450 (setq trg (and (string-match org-bracket-link-regexp l)
8451 (match-string 1 l)))
8452 (if (or (not trg) (string-match org-any-link-re trg))
8453 (save-excursion
8454 (save-restriction
8455 (widen)
8456 (goto-char marker)
8457 (when (search-forward l nil lkend)
8458 (goto-char (match-beginning 0))
8459 (org-open-at-point))))
8460 ;; This is an internal link, widen the buffer
8461 (switch-to-buffer-other-window buffer)
8462 (widen)
8463 (goto-char marker)
8464 (when (search-forward l nil lkend)
8465 (goto-char (match-beginning 0))
8466 (org-open-at-point)))))
8467 lk))
8468 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8469 (save-excursion
8470 (beginning-of-line 1)
8471 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8472 (org-open-link-from-string (match-string 1)))
8473 (t (message "No link to open here")))))
8475 (defun org-agenda-copy-local-variable (var)
8476 "Get a variable from a referenced buffer and install it here."
8477 (let ((m (org-get-at-bol 'org-marker)))
8478 (when (and m (buffer-live-p (marker-buffer m)))
8479 (org-set-local var (with-current-buffer (marker-buffer m)
8480 (symbol-value var))))))
8482 (defun org-agenda-switch-to (&optional delete-other-windows)
8483 "Go to the Org-mode file which contains the item at point."
8484 (interactive)
8485 (if (and org-return-follows-link
8486 (not (org-get-at-bol 'org-marker))
8487 (org-in-regexp org-bracket-link-regexp))
8488 (org-open-link-from-string (match-string 0))
8489 (let* ((marker (or (org-get-at-bol 'org-marker)
8490 (org-agenda-error)))
8491 (buffer (marker-buffer marker))
8492 (pos (marker-position marker)))
8493 (org-pop-to-buffer-same-window buffer)
8494 (and delete-other-windows (delete-other-windows))
8495 (widen)
8496 (goto-char pos)
8497 (when (derived-mode-p 'org-mode)
8498 (org-show-context 'agenda)
8499 (save-excursion
8500 (and (outline-next-heading)
8501 (org-flag-heading nil))) ; show the next heading
8502 (when (outline-invisible-p)
8503 (show-entry)) ; display invisible text
8504 (run-hooks 'org-agenda-after-show-hook)))))
8506 (defun org-agenda-goto-mouse (ev)
8507 "Go to the Org-mode file which contains the item at the mouse click."
8508 (interactive "e")
8509 (mouse-set-point ev)
8510 (org-agenda-goto))
8512 (defun org-agenda-show (&optional full-entry)
8513 "Display the Org-mode file which contains the item at point.
8514 With prefix argument FULL-ENTRY, make the entire entry visible
8515 if it was hidden in the outline."
8516 (interactive "P")
8517 (let ((win (selected-window)))
8518 (if full-entry
8519 (let ((org-show-entry-below t))
8520 (org-agenda-goto t))
8521 (org-agenda-goto t))
8522 (select-window win)))
8524 (defvar org-agenda-show-window nil)
8525 (defun org-agenda-show-and-scroll-up (&optional arg)
8526 "Display the Org-mode file which contains the item at point.
8527 When called repeatedly, scroll the window that is displaying the buffer.
8528 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8529 `show-subtree' to display the item, so that drawers and logbooks stay
8530 folded."
8531 (interactive "P")
8532 (let ((win (selected-window)))
8533 (if (and (window-live-p org-agenda-show-window)
8534 (eq this-command last-command))
8535 (progn
8536 (select-window org-agenda-show-window)
8537 (ignore-errors (scroll-up)))
8538 (org-agenda-goto t)
8539 (if arg (org-show-entry) (show-subtree))
8540 (setq org-agenda-show-window (selected-window)))
8541 (select-window win)))
8543 (defun org-agenda-show-scroll-down ()
8544 "Scroll down the window showing the agenda."
8545 (interactive)
8546 (let ((win (selected-window)))
8547 (when (window-live-p org-agenda-show-window)
8548 (select-window org-agenda-show-window)
8549 (ignore-errors (scroll-down))
8550 (select-window win))))
8552 (defun org-agenda-show-1 (&optional more)
8553 "Display the Org-mode file which contains the item at point.
8554 The prefix arg selects the amount of information to display:
8556 0 hide the subtree
8557 1 just show the entry according to defaults.
8558 2 show the children view
8559 3 show the subtree view
8560 4 show the entire subtree and any LOGBOOK drawers
8561 5 show the entire subtree and any drawers
8562 With prefix argument FULL-ENTRY, make the entire entry visible
8563 if it was hidden in the outline."
8564 (interactive "p")
8565 (let ((win (selected-window)))
8566 (org-agenda-goto t)
8567 (org-recenter-heading 1)
8568 (cond
8569 ((= more 0)
8570 (hide-subtree)
8571 (save-excursion
8572 (org-back-to-heading)
8573 (run-hook-with-args 'org-cycle-hook 'folded))
8574 (message "Remote: FOLDED"))
8575 ((and (org-called-interactively-p 'any) (= more 1))
8576 (message "Remote: show with default settings"))
8577 ((= more 2)
8578 (show-entry)
8579 (show-children)
8580 (save-excursion
8581 (org-back-to-heading)
8582 (run-hook-with-args 'org-cycle-hook 'children))
8583 (message "Remote: CHILDREN"))
8584 ((= more 3)
8585 (show-subtree)
8586 (save-excursion
8587 (org-back-to-heading)
8588 (run-hook-with-args 'org-cycle-hook 'subtree))
8589 (message "Remote: SUBTREE"))
8590 ((= more 4)
8591 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8592 (org-drawer-regexp
8593 (concat "^[ \t]*:\\("
8594 (mapconcat 'regexp-quote org-drawers "\\|")
8595 "\\):[ \t]*$")))
8596 (show-subtree)
8597 (save-excursion
8598 (org-back-to-heading)
8599 (org-cycle-hide-drawers 'subtree)))
8600 (message "Remote: SUBTREE AND LOGBOOK"))
8601 ((> more 4)
8602 (show-subtree)
8603 (message "Remote: SUBTREE AND ALL DRAWERS")))
8604 (select-window win)))
8606 (defun org-recenter-heading (n)
8607 (save-excursion
8608 (org-back-to-heading)
8609 (recenter n)))
8611 (defvar org-agenda-cycle-counter nil)
8612 (defun org-agenda-cycle-show (&optional n)
8613 "Show the current entry in another window, with default settings.
8614 Default settings are taken from `org-show-hierarchy-above' and siblings.
8615 When use repeatedly in immediate succession, the remote entry will cycle
8616 through visibility
8618 children -> subtree -> folded
8620 When called with a numeric prefix arg, that arg will be passed through to
8621 `org-agenda-show-1'. For the interpretation of that argument, see the
8622 docstring of `org-agenda-show-1'."
8623 (interactive "P")
8624 (if (integerp n)
8625 (setq org-agenda-cycle-counter n)
8626 (if (not (eq last-command this-command))
8627 (setq org-agenda-cycle-counter 1)
8628 (if (equal org-agenda-cycle-counter 0)
8629 (setq org-agenda-cycle-counter 2)
8630 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8631 (if (> org-agenda-cycle-counter 3)
8632 (setq org-agenda-cycle-counter 0)))))
8633 (org-agenda-show-1 org-agenda-cycle-counter))
8635 (defun org-agenda-recenter (arg)
8636 "Display the Org-mode file which contains the item at point and recenter."
8637 (interactive "P")
8638 (let ((win (selected-window)))
8639 (org-agenda-goto t)
8640 (recenter arg)
8641 (select-window win)))
8643 (defun org-agenda-show-mouse (ev)
8644 "Display the Org-mode file which contains the item at the mouse click."
8645 (interactive "e")
8646 (mouse-set-point ev)
8647 (org-agenda-show))
8649 (defun org-agenda-check-no-diary ()
8650 "Check if the entry is a diary link and abort if yes."
8651 (if (org-get-at-bol 'org-agenda-diary-link)
8652 (org-agenda-error)))
8654 (defun org-agenda-error ()
8655 (error "Command not allowed in this line"))
8657 (defun org-agenda-tree-to-indirect-buffer (arg)
8658 "Show the subtree corresponding to the current entry in an indirect buffer.
8659 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8661 With a numerical prefix ARG, go up to this level and then take that tree.
8662 With a negative numeric ARG, go up by this number of levels.
8663 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8664 use the dedicated frame)."
8665 (interactive "P")
8666 (if current-prefix-arg
8667 (org-agenda-do-tree-to-indirect-buffer arg)
8668 (let ((agenda-buffer (buffer-name))
8669 (agenda-window (selected-window))
8670 (indirect-window
8671 (and org-last-indirect-buffer
8672 (get-buffer-window org-last-indirect-buffer))))
8673 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8674 (unless (or (eq org-indirect-buffer-display 'new-frame)
8675 (eq org-indirect-buffer-display 'dedicated-frame))
8676 (unwind-protect
8677 (unless (and indirect-window (window-live-p indirect-window))
8678 (setq indirect-window (split-window agenda-window)))
8679 (and indirect-window (select-window indirect-window))
8680 (switch-to-buffer org-last-indirect-buffer :norecord)
8681 (fit-window-to-buffer indirect-window)))
8682 (select-window (get-buffer-window agenda-buffer)))))
8684 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8685 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8686 (org-agenda-check-no-diary)
8687 (let* ((marker (or (org-get-at-bol 'org-marker)
8688 (org-agenda-error)))
8689 (buffer (marker-buffer marker))
8690 (pos (marker-position marker)))
8691 (with-current-buffer buffer
8692 (save-excursion
8693 (goto-char pos)
8694 (funcall 'org-tree-to-indirect-buffer arg)))))
8696 (defvar org-last-heading-marker (make-marker)
8697 "Marker pointing to the headline that last changed its TODO state
8698 by a remote command from the agenda.")
8700 (defun org-agenda-todo-nextset ()
8701 "Switch TODO entry to next sequence."
8702 (interactive)
8703 (org-agenda-todo 'nextset))
8705 (defun org-agenda-todo-previousset ()
8706 "Switch TODO entry to previous sequence."
8707 (interactive)
8708 (org-agenda-todo 'previousset))
8710 (defun org-agenda-todo (&optional arg)
8711 "Cycle TODO state of line at point, also in Org-mode file.
8712 This changes the line at point, all other lines in the agenda referring to
8713 the same tree node, and the headline of the tree node in the Org-mode file."
8714 (interactive "P")
8715 (org-agenda-check-no-diary)
8716 (let* ((col (current-column))
8717 (marker (or (org-get-at-bol 'org-marker)
8718 (org-agenda-error)))
8719 (buffer (marker-buffer marker))
8720 (pos (marker-position marker))
8721 (hdmarker (org-get-at-bol 'org-hd-marker))
8722 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8723 (inhibit-read-only t)
8724 org-agenda-headline-snapshot-before-repeat newhead just-one)
8725 (org-with-remote-undo buffer
8726 (with-current-buffer buffer
8727 (widen)
8728 (goto-char pos)
8729 (org-show-context 'agenda)
8730 (save-excursion
8731 (and (outline-next-heading)
8732 (org-flag-heading nil))) ; show the next heading
8733 (let ((current-prefix-arg arg))
8734 (call-interactively 'org-todo))
8735 (and (bolp) (forward-char 1))
8736 (setq newhead (org-get-heading))
8737 (when (and (org-bound-and-true-p
8738 org-agenda-headline-snapshot-before-repeat)
8739 (not (equal org-agenda-headline-snapshot-before-repeat
8740 newhead))
8741 todayp)
8742 (setq newhead org-agenda-headline-snapshot-before-repeat
8743 just-one t))
8744 (save-excursion
8745 (org-back-to-heading)
8746 (move-marker org-last-heading-marker (point))))
8747 (beginning-of-line 1)
8748 (save-excursion
8749 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8750 (org-move-to-column col))))
8752 (defun org-agenda-add-note (&optional arg)
8753 "Add a time-stamped note to the entry at point."
8754 (interactive "P")
8755 (org-agenda-check-no-diary)
8756 (let* ((marker (or (org-get-at-bol 'org-marker)
8757 (org-agenda-error)))
8758 (buffer (marker-buffer marker))
8759 (pos (marker-position marker))
8760 (hdmarker (org-get-at-bol 'org-hd-marker))
8761 (inhibit-read-only t))
8762 (with-current-buffer buffer
8763 (widen)
8764 (goto-char pos)
8765 (org-show-context 'agenda)
8766 (save-excursion
8767 (and (outline-next-heading)
8768 (org-flag-heading nil))) ; show the next heading
8769 (org-add-note))))
8771 (defun org-agenda-change-all-lines (newhead hdmarker
8772 &optional fixface just-this)
8773 "Change all lines in the agenda buffer which match HDMARKER.
8774 The new content of the line will be NEWHEAD (as modified by
8775 `org-agenda-format-item'). HDMARKER is checked with
8776 `equal' against all `org-hd-marker' text properties in the file.
8777 If FIXFACE is non-nil, the face of each item is modified according to
8778 the new TODO state.
8779 If JUST-THIS is non-nil, change just the current line, not all.
8780 If FORCE-TAGS is non nil, the car of it returns the new tags."
8781 (let* ((inhibit-read-only t)
8782 (line (org-current-line))
8783 (org-agenda-buffer (current-buffer))
8784 (thetags (with-current-buffer (marker-buffer hdmarker)
8785 (save-excursion (save-restriction (widen)
8786 (goto-char hdmarker)
8787 (org-get-tags-at)))))
8788 props m pl undone-face done-face finish new dotime level cat tags)
8789 (save-excursion
8790 (goto-char (point-max))
8791 (beginning-of-line 1)
8792 (while (not finish)
8793 (setq finish (bobp))
8794 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8795 (or (not just-this) (= (org-current-line) line))
8796 (equal m hdmarker))
8797 (setq props (text-properties-at (point))
8798 dotime (org-get-at-bol 'dotime)
8799 cat (org-get-at-bol 'org-category)
8800 level (org-get-at-bol 'level)
8801 tags thetags
8803 (let ((org-prefix-format-compiled
8804 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8805 org-prefix-format-compiled))
8806 (extra (org-get-at-bol 'extra)))
8807 (with-current-buffer (marker-buffer hdmarker)
8808 (save-excursion
8809 (save-restriction
8810 (widen)
8811 (org-agenda-format-item extra newhead level cat tags dotime)))))
8812 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8813 undone-face (org-get-at-bol 'undone-face)
8814 done-face (org-get-at-bol 'done-face))
8815 (beginning-of-line 1)
8816 (cond
8817 ((equal new "")
8818 (and (looking-at ".*\n?") (replace-match "")))
8819 ((looking-at ".*")
8820 (replace-match new t t)
8821 (beginning-of-line 1)
8822 (add-text-properties (point-at-bol) (point-at-eol) props)
8823 (when fixface
8824 (add-text-properties
8825 (point-at-bol) (point-at-eol)
8826 (list 'face
8827 (if org-last-todo-state-is-todo
8828 undone-face done-face))))
8829 (org-agenda-highlight-todo 'line)
8830 (beginning-of-line 1))
8831 (t (error "Line update did not work")))
8832 (save-restriction
8833 (narrow-to-region (point-at-bol) (point-at-eol))
8834 (org-agenda-finalize)))
8835 (beginning-of-line 0)))))
8837 (defun org-agenda-align-tags (&optional line)
8838 "Align all tags in agenda items to `org-agenda-tags-column'."
8839 (let ((inhibit-read-only t) l c)
8840 (save-excursion
8841 (goto-char (if line (point-at-bol) (point-min)))
8842 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8843 (if line (point-at-eol) nil) t)
8844 (add-text-properties
8845 (match-beginning 2) (match-end 2)
8846 (list 'face (delq nil (let ((prop (get-text-property
8847 (match-beginning 2) 'face)))
8848 (or (listp prop) (setq prop (list prop)))
8849 (if (memq 'org-tag prop)
8850 prop
8851 (cons 'org-tag prop))))))
8852 (setq l (- (match-end 2) (match-beginning 2))
8853 c (if (< org-agenda-tags-column 0)
8854 (- (abs org-agenda-tags-column) l)
8855 org-agenda-tags-column))
8856 (delete-region (match-beginning 1) (match-end 1))
8857 (goto-char (match-beginning 1))
8858 (insert (org-add-props
8859 (make-string (max 1 (- c (current-column))) ?\ )
8860 (plist-put (copy-sequence (text-properties-at (point)))
8861 'face nil))))
8862 (goto-char (point-min))
8863 (org-font-lock-add-tag-faces (point-max)))))
8865 (defun org-agenda-priority-up ()
8866 "Increase the priority of line at point, also in Org-mode file."
8867 (interactive)
8868 (org-agenda-priority 'up))
8870 (defun org-agenda-priority-down ()
8871 "Decrease the priority of line at point, also in Org-mode file."
8872 (interactive)
8873 (org-agenda-priority 'down))
8875 (defun org-agenda-priority (&optional force-direction)
8876 "Set the priority of line at point, also in Org-mode file.
8877 This changes the line at point, all other lines in the agenda referring to
8878 the same tree node, and the headline of the tree node in the Org-mode file.
8879 Called with a universal prefix arg, show the priority instead of setting it."
8880 (interactive "P")
8881 (if (equal force-direction '(4))
8882 (org-show-priority)
8883 (unless org-enable-priority-commands
8884 (error "Priority commands are disabled"))
8885 (org-agenda-check-no-diary)
8886 (let* ((marker (or (org-get-at-bol 'org-marker)
8887 (org-agenda-error)))
8888 (hdmarker (org-get-at-bol 'org-hd-marker))
8889 (buffer (marker-buffer hdmarker))
8890 (pos (marker-position hdmarker))
8891 (inhibit-read-only t)
8892 newhead)
8893 (org-with-remote-undo buffer
8894 (with-current-buffer buffer
8895 (widen)
8896 (goto-char pos)
8897 (org-show-context 'agenda)
8898 (save-excursion
8899 (and (outline-next-heading)
8900 (org-flag-heading nil))) ; show the next heading
8901 (funcall 'org-priority force-direction)
8902 (end-of-line 1)
8903 (setq newhead (org-get-heading)))
8904 (org-agenda-change-all-lines newhead hdmarker)
8905 (beginning-of-line 1)))))
8907 ;; FIXME: should fix the tags property of the agenda line.
8908 (defun org-agenda-set-tags (&optional tag onoff)
8909 "Set tags for the current headline."
8910 (interactive)
8911 (org-agenda-check-no-diary)
8912 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8913 (call-interactively 'org-change-tag-in-region)
8914 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8915 (org-agenda-error)))
8916 (buffer (marker-buffer hdmarker))
8917 (pos (marker-position hdmarker))
8918 (inhibit-read-only t)
8919 newhead)
8920 (org-with-remote-undo buffer
8921 (with-current-buffer buffer
8922 (widen)
8923 (goto-char pos)
8924 (save-excursion
8925 (org-show-context 'agenda))
8926 (save-excursion
8927 (and (outline-next-heading)
8928 (org-flag-heading nil))) ; show the next heading
8929 (goto-char pos)
8930 (if tag
8931 (org-toggle-tag tag onoff)
8932 (call-interactively 'org-set-tags))
8933 (end-of-line 1)
8934 (setq newhead (org-get-heading)))
8935 (org-agenda-change-all-lines newhead hdmarker)
8936 (beginning-of-line 1)))))
8938 (defun org-agenda-set-property ()
8939 "Set a property for the current headline."
8940 (interactive)
8941 (org-agenda-check-no-diary)
8942 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8943 (org-agenda-error)))
8944 (buffer (marker-buffer hdmarker))
8945 (pos (marker-position hdmarker))
8946 (inhibit-read-only t)
8947 newhead)
8948 (org-with-remote-undo buffer
8949 (with-current-buffer buffer
8950 (widen)
8951 (goto-char pos)
8952 (save-excursion
8953 (org-show-context 'agenda))
8954 (save-excursion
8955 (and (outline-next-heading)
8956 (org-flag-heading nil))) ; show the next heading
8957 (goto-char pos)
8958 (call-interactively 'org-set-property)))))
8960 (defun org-agenda-set-effort ()
8961 "Set the effort property for the current headline."
8962 (interactive)
8963 (org-agenda-check-no-diary)
8964 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8965 (org-agenda-error)))
8966 (buffer (marker-buffer hdmarker))
8967 (pos (marker-position hdmarker))
8968 (inhibit-read-only t)
8969 newhead)
8970 (org-with-remote-undo buffer
8971 (with-current-buffer buffer
8972 (widen)
8973 (goto-char pos)
8974 (save-excursion
8975 (org-show-context 'agenda))
8976 (save-excursion
8977 (and (outline-next-heading)
8978 (org-flag-heading nil))) ; show the next heading
8979 (goto-char pos)
8980 (call-interactively 'org-set-effort)
8981 (end-of-line 1)
8982 (setq newhead (org-get-heading)))
8983 (org-agenda-change-all-lines newhead hdmarker))))
8985 (defun org-agenda-toggle-archive-tag ()
8986 "Toggle the archive tag for the current entry."
8987 (interactive)
8988 (org-agenda-check-no-diary)
8989 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8990 (org-agenda-error)))
8991 (buffer (marker-buffer hdmarker))
8992 (pos (marker-position hdmarker))
8993 (inhibit-read-only t)
8994 newhead)
8995 (org-with-remote-undo buffer
8996 (with-current-buffer buffer
8997 (widen)
8998 (goto-char pos)
8999 (org-show-context 'agenda)
9000 (save-excursion
9001 (and (outline-next-heading)
9002 (org-flag-heading nil))) ; show the next heading
9003 (call-interactively 'org-toggle-archive-tag)
9004 (end-of-line 1)
9005 (setq newhead (org-get-heading)))
9006 (org-agenda-change-all-lines newhead hdmarker)
9007 (beginning-of-line 1))))
9009 (defun org-agenda-do-date-later (arg)
9010 (interactive "P")
9011 (cond
9012 ((or (equal arg '(16))
9013 (memq last-command
9014 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9015 (setq this-command 'org-agenda-date-later-minutes)
9016 (org-agenda-date-later-minutes 1))
9017 ((or (equal arg '(4))
9018 (memq last-command
9019 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9020 (setq this-command 'org-agenda-date-later-hours)
9021 (org-agenda-date-later-hours 1))
9023 (org-agenda-date-later (prefix-numeric-value arg)))))
9025 (defun org-agenda-do-date-earlier (arg)
9026 (interactive "P")
9027 (cond
9028 ((or (equal arg '(16))
9029 (memq last-command
9030 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9031 (setq this-command 'org-agenda-date-earlier-minutes)
9032 (org-agenda-date-earlier-minutes 1))
9033 ((or (equal arg '(4))
9034 (memq last-command
9035 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9036 (setq this-command 'org-agenda-date-earlier-hours)
9037 (org-agenda-date-earlier-hours 1))
9039 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9041 (defun org-agenda-date-later (arg &optional what)
9042 "Change the date of this item to ARG day(s) later."
9043 (interactive "p")
9044 (org-agenda-check-type t 'agenda 'timeline)
9045 (org-agenda-check-no-diary)
9046 (let* ((marker (or (org-get-at-bol 'org-marker)
9047 (org-agenda-error)))
9048 (buffer (marker-buffer marker))
9049 (pos (marker-position marker))
9050 cdate today)
9051 (org-with-remote-undo buffer
9052 (with-current-buffer buffer
9053 (widen)
9054 (goto-char pos)
9055 (if (not (org-at-timestamp-p))
9056 (error "Cannot find time stamp"))
9057 (when (and org-agenda-move-date-from-past-immediately-to-today
9058 (equal arg 1)
9059 (or (not what) (eq what 'day))
9060 (not (save-match-data (org-at-date-range-p))))
9061 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9062 cdate (calendar-absolute-from-gregorian
9063 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9064 today (org-today))
9065 (if (> today cdate)
9066 ;; immediately shift to today
9067 (setq arg (- today cdate))))
9068 (org-timestamp-change arg (or what 'day))
9069 (when (and (org-at-date-range-p)
9070 (re-search-backward org-tr-regexp-both (point-at-bol)))
9071 (let ((end org-last-changed-timestamp))
9072 (org-timestamp-change arg (or what 'day))
9073 (setq org-last-changed-timestamp
9074 (concat org-last-changed-timestamp "--" end)))))
9075 (org-agenda-show-new-time marker org-last-changed-timestamp))
9076 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9078 (defun org-agenda-date-earlier (arg &optional what)
9079 "Change the date of this item to ARG day(s) earlier."
9080 (interactive "p")
9081 (org-agenda-date-later (- arg) what))
9083 (defun org-agenda-date-later-minutes (arg)
9084 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9085 (interactive "p")
9086 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9087 (org-agenda-date-later arg 'minute))
9089 (defun org-agenda-date-earlier-minutes (arg)
9090 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9091 (interactive "p")
9092 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9093 (org-agenda-date-earlier arg 'minute))
9095 (defun org-agenda-date-later-hours (arg)
9096 "Change the time of this item, in hour steps."
9097 (interactive "p")
9098 (org-agenda-date-later arg 'hour))
9100 (defun org-agenda-date-earlier-hours (arg)
9101 "Change the time of this item, in hour steps."
9102 (interactive "p")
9103 (org-agenda-date-earlier arg 'hour))
9105 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9106 "Show new date stamp via text properties."
9107 ;; We use text properties to make this undoable
9108 (let ((inhibit-read-only t))
9109 (setq stamp (concat prefix " => " stamp " "))
9110 (save-excursion
9111 (goto-char (point-max))
9112 (while (not (bobp))
9113 (when (equal marker (org-get-at-bol 'org-marker))
9114 (org-move-to-column (- (window-width) (length stamp)) t)
9115 (org-agenda-fix-tags-filter-overlays-at (point))
9116 (if (featurep 'xemacs)
9117 ;; Use `duplicable' property to trigger undo recording
9118 (let ((ex (make-extent nil nil))
9119 (gl (make-glyph stamp)))
9120 (set-glyph-face gl 'secondary-selection)
9121 (set-extent-properties
9122 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9123 (insert-extent ex (1- (point)) (point-at-eol)))
9124 (add-text-properties
9125 (1- (point)) (point-at-eol)
9126 (list 'display (org-add-props stamp nil
9127 'face 'secondary-selection))))
9128 (beginning-of-line 1))
9129 (beginning-of-line 0)))))
9131 (defun org-agenda-date-prompt (arg)
9132 "Change the date of this item. Date is prompted for, with default today.
9133 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9134 be used to request time specification in the time stamp."
9135 (interactive "P")
9136 (org-agenda-check-type t 'agenda 'timeline)
9137 (org-agenda-check-no-diary)
9138 (let* ((marker (or (org-get-at-bol 'org-marker)
9139 (org-agenda-error)))
9140 (buffer (marker-buffer marker))
9141 (pos (marker-position marker)))
9142 (org-with-remote-undo buffer
9143 (with-current-buffer buffer
9144 (widen)
9145 (goto-char pos)
9146 (if (not (org-at-timestamp-p t))
9147 (error "Cannot find time stamp"))
9148 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
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-schedule (arg &optional time)
9153 "Schedule the item at point.
9154 ARG is passed through to `org-schedule'."
9155 (interactive "P")
9156 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9157 (org-agenda-check-no-diary)
9158 (let* ((marker (or (org-get-at-bol 'org-marker)
9159 (org-agenda-error)))
9160 (type (marker-insertion-type marker))
9161 (buffer (marker-buffer marker))
9162 (pos (marker-position marker))
9163 (org-insert-labeled-timestamps-at-point nil)
9165 (set-marker-insertion-type marker t)
9166 (org-with-remote-undo buffer
9167 (with-current-buffer buffer
9168 (widen)
9169 (goto-char pos)
9170 (setq ts (org-schedule arg time)))
9171 (org-agenda-show-new-time marker ts " S"))
9172 (message "%s" ts)))
9174 (defun org-agenda-deadline (arg &optional time)
9175 "Schedule the item at point.
9176 ARG is passed through to `org-deadline'."
9177 (interactive "P")
9178 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9179 (org-agenda-check-no-diary)
9180 (let* ((marker (or (org-get-at-bol 'org-marker)
9181 (org-agenda-error)))
9182 (buffer (marker-buffer marker))
9183 (pos (marker-position marker))
9184 (org-insert-labeled-timestamps-at-point nil)
9186 (org-with-remote-undo buffer
9187 (with-current-buffer buffer
9188 (widen)
9189 (goto-char pos)
9190 (setq ts (org-deadline arg time)))
9191 (org-agenda-show-new-time marker ts " D"))
9192 (message "%s" ts)))
9194 (defun org-agenda-clock-in (&optional arg)
9195 "Start the clock on the currently selected item."
9196 (interactive "P")
9197 (org-agenda-check-no-diary)
9198 (if (equal arg '(4))
9199 (org-clock-in arg)
9200 (let* ((marker (or (org-get-at-bol 'org-marker)
9201 (org-agenda-error)))
9202 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9203 (pos (marker-position marker))
9204 (col (current-column))
9205 newhead)
9206 (org-with-remote-undo (marker-buffer marker)
9207 (with-current-buffer (marker-buffer marker)
9208 (widen)
9209 (goto-char pos)
9210 (org-show-context 'agenda)
9211 (org-show-entry)
9212 (org-cycle-hide-drawers 'children)
9213 (org-clock-in arg)
9214 (setq newhead (org-get-heading)))
9215 (org-agenda-change-all-lines newhead hdmarker))
9216 (org-move-to-column col))))
9218 (defun org-agenda-clock-out ()
9219 "Stop the currently running clock."
9220 (interactive)
9221 (unless (marker-buffer org-clock-marker)
9222 (error "No running clock"))
9223 (let ((marker (make-marker)) (col (current-column)) newhead)
9224 (org-with-remote-undo (marker-buffer org-clock-marker)
9225 (with-current-buffer (marker-buffer org-clock-marker)
9226 (save-excursion
9227 (save-restriction
9228 (widen)
9229 (goto-char org-clock-marker)
9230 (org-back-to-heading t)
9231 (move-marker marker (point))
9232 (org-clock-out)
9233 (setq newhead (org-get-heading))))))
9234 (org-agenda-change-all-lines newhead marker)
9235 (move-marker marker nil)
9236 (org-move-to-column col)
9237 (org-agenda-unmark-clocking-task)))
9239 (defun org-agenda-clock-cancel (&optional arg)
9240 "Cancel the currently running clock."
9241 (interactive "P")
9242 (unless (marker-buffer org-clock-marker)
9243 (user-error "No running clock"))
9244 (org-with-remote-undo (marker-buffer org-clock-marker)
9245 (org-clock-cancel)))
9247 (defun org-agenda-clock-goto ()
9248 "Jump to the currently clocked in task within the agenda.
9249 If the currently clocked in task is not listed in the agenda
9250 buffer, display it in another window."
9251 (interactive)
9252 (let (pos)
9253 (mapc (lambda (o)
9254 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9255 (setq pos (overlay-start o))))
9256 (overlays-in (point-min) (point-max)))
9257 (cond (pos (goto-char pos))
9258 ;; If the currently clocked entry is not in the agenda
9259 ;; buffer, we visit it in another window:
9260 (org-clock-current-task
9261 (org-switch-to-buffer-other-window (org-clock-goto)))
9262 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9264 (defun org-agenda-diary-entry-in-org-file ()
9265 "Make a diary entry in the file `org-agenda-diary-file'."
9266 (let (d1 d2 char (text "") dp1 dp2)
9267 (if (equal (buffer-name) "*Calendar*")
9268 (setq d1 (calendar-cursor-to-date t)
9269 d2 (car calendar-mark-ring))
9270 (setq dp1 (get-text-property (point-at-bol) 'day))
9271 (unless dp1 (user-error "No date defined in current line"))
9272 (setq d1 (calendar-gregorian-from-absolute dp1)
9273 d2 (and (ignore-errors (mark))
9274 (save-excursion
9275 (goto-char (mark))
9276 (setq dp2 (get-text-property (point-at-bol) 'day)))
9277 (calendar-gregorian-from-absolute dp2))))
9278 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9279 (setq char (read-char-exclusive))
9280 (cond
9281 ((equal char ?d)
9282 (setq text (read-string "Day entry: "))
9283 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9284 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9285 ((equal char ?a)
9286 (setq d1 (list (car d1) (nth 1 d1)
9287 (read-number (format "Reference year [%d]: " (nth 2 d1))
9288 (nth 2 d1))))
9289 (setq text (read-string "Anniversary (use %d to show years): "))
9290 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9291 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9292 ((equal char ?b)
9293 (setq text (read-string "Block entry: "))
9294 (unless (and d1 d2 (not (equal d1 d2)))
9295 (user-error "No block of days selected"))
9296 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9297 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9298 ((equal char ?j)
9299 (org-switch-to-buffer-other-window
9300 (find-file-noselect org-agenda-diary-file))
9301 (require 'org-datetree)
9302 (org-datetree-find-date-create d1)
9303 (org-reveal t))
9304 (t (user-error "Invalid selection character `%c'" char)))))
9306 (defcustom org-agenda-insert-diary-strategy 'date-tree
9307 "Where in `org-agenda-diary-file' should new entries be added?
9308 Valid values:
9310 date-tree in the date tree, as child of the date
9311 top-level as top-level entries at the end of the file."
9312 :group 'org-agenda
9313 :type '(choice
9314 (const :tag "in a date tree" date-tree)
9315 (const :tag "as top level at end of file" top-level)))
9317 (defcustom org-agenda-insert-diary-extract-time nil
9318 "Non-nil means extract any time specification from the diary entry."
9319 :group 'org-agenda
9320 :version "24.1"
9321 :type 'boolean)
9323 (defcustom org-agenda-bulk-mark-char ">"
9324 "A single-character string to be used as the bulk mark."
9325 :group 'org-agenda
9326 :version "24.1"
9327 :type 'string)
9329 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9330 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9331 If TEXT is not empty, it will become the headline of the new entry, and
9332 the resulting entry will not be shown. When TEXT is empty, switch to
9333 `org-agenda-diary-file' and let the user finish the entry there."
9334 (let ((cw (current-window-configuration)))
9335 (org-switch-to-buffer-other-window
9336 (find-file-noselect org-agenda-diary-file))
9337 (widen)
9338 (goto-char (point-min))
9339 (cond
9340 ((eq type 'anniversary)
9341 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9342 (progn
9343 (or (org-at-heading-p t)
9344 (progn
9345 (outline-next-heading)
9346 (insert "* Anniversaries\n\n")
9347 (beginning-of-line -1)))))
9348 (outline-next-heading)
9349 (org-back-over-empty-lines)
9350 (backward-char 1)
9351 (insert "\n")
9352 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9353 (nth 2 d1) (car d1) (nth 1 d1) text)))
9354 ((eq type 'day)
9355 (let ((org-prefix-has-time t)
9356 (org-agenda-time-leading-zero t)
9357 fmt time time2)
9358 (if org-agenda-insert-diary-extract-time
9359 ;; Use org-agenda-format-item to parse text for a time-range and
9360 ;; remove it. FIXME: This is a hack, we should refactor
9361 ;; that function to make time extraction available separately
9362 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9363 time (get-text-property 0 'time fmt)
9364 time2 (if (> (length time) 0)
9365 ;; split-string removes trailing ...... if
9366 ;; no end time given. First space
9367 ;; separates time from date.
9368 (concat " " (car (split-string time "\\.")))
9369 nil)
9370 text (get-text-property 0 'txt fmt)))
9371 (if (eq org-agenda-insert-diary-strategy 'top-level)
9372 (org-agenda-insert-diary-as-top-level text)
9373 (require 'org-datetree)
9374 (org-datetree-find-date-create d1)
9375 (org-agenda-insert-diary-make-new-entry text))
9376 (org-insert-time-stamp (org-time-from-absolute
9377 (calendar-absolute-from-gregorian d1))
9378 nil nil nil nil time2))
9379 (end-of-line 0))
9380 ((eq type 'block)
9381 (if (> (calendar-absolute-from-gregorian d1)
9382 (calendar-absolute-from-gregorian d2))
9383 (setq d1 (prog1 d2 (setq d2 d1))))
9384 (if (eq org-agenda-insert-diary-strategy 'top-level)
9385 (org-agenda-insert-diary-as-top-level text)
9386 (require 'org-datetree)
9387 (org-datetree-find-date-create d1)
9388 (org-agenda-insert-diary-make-new-entry text))
9389 (org-insert-time-stamp (org-time-from-absolute
9390 (calendar-absolute-from-gregorian d1)))
9391 (insert "--")
9392 (org-insert-time-stamp (org-time-from-absolute
9393 (calendar-absolute-from-gregorian d2)))
9394 (end-of-line 0)))
9395 (if (string-match "\\S-" text)
9396 (progn
9397 (set-window-configuration cw)
9398 (message "%s entry added to %s"
9399 (capitalize (symbol-name type))
9400 (abbreviate-file-name org-agenda-diary-file)))
9401 (org-reveal t)
9402 (message "Please finish entry here"))))
9404 (defun org-agenda-insert-diary-as-top-level (text)
9405 "Make new entry as a top-level entry at the end of the file.
9406 Add TEXT as headline, and position the cursor in the second line so that
9407 a timestamp can be added there."
9408 (widen)
9409 (goto-char (point-max))
9410 (or (bolp) (insert "\n"))
9411 (insert "* " text "\n")
9412 (if org-adapt-indentation (org-indent-to-column 2)))
9414 (defun org-agenda-insert-diary-make-new-entry (text)
9415 "Make new entry as last child of current entry.
9416 Add TEXT as headline, and position the cursor in the second line so that
9417 a timestamp can be added there."
9418 (let ((org-show-following-heading t)
9419 (org-show-siblings t)
9420 (org-show-hierarchy-above t)
9421 (org-show-entry-below t)
9422 col)
9423 (outline-next-heading)
9424 (org-back-over-empty-lines)
9425 (or (looking-at "[ \t]*$")
9426 (progn (insert "\n") (backward-char 1)))
9427 (org-insert-heading nil t)
9428 (org-do-demote)
9429 (setq col (current-column))
9430 (insert text "\n")
9431 (if org-adapt-indentation (org-indent-to-column col))
9432 (let ((org-show-following-heading t)
9433 (org-show-siblings t)
9434 (org-show-hierarchy-above t)
9435 (org-show-entry-below t))
9436 (org-show-context))))
9438 (defun org-agenda-diary-entry ()
9439 "Make a diary entry, like the `i' command from the calendar.
9440 All the standard commands work: block, weekly etc.
9441 When `org-agenda-diary-file' points to a file,
9442 `org-agenda-diary-entry-in-org-file' is called instead to create
9443 entries in that Org-mode file."
9444 (interactive)
9445 (if (not (eq org-agenda-diary-file 'diary-file))
9446 (org-agenda-diary-entry-in-org-file)
9447 (require 'diary-lib)
9448 (let* ((char (progn
9449 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9450 (read-char-exclusive)))
9451 (cmd (cdr (assoc char
9452 '((?d . insert-diary-entry)
9453 (?w . insert-weekly-diary-entry)
9454 (?m . insert-monthly-diary-entry)
9455 (?y . insert-yearly-diary-entry)
9456 (?a . insert-anniversary-diary-entry)
9457 (?b . insert-block-diary-entry)
9458 (?c . insert-cyclic-diary-entry)))))
9459 (oldf (symbol-function 'calendar-cursor-to-date))
9460 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9461 (point (point))
9462 (mark (or (mark t) (point))))
9463 (unless cmd
9464 (user-error "No command associated with <%c>" char))
9465 (unless (and (get-text-property point 'day)
9466 (or (not (equal ?b char))
9467 (get-text-property mark 'day)))
9468 (user-error "Don't know which date to use for diary entry"))
9469 ;; We implement this by hacking the `calendar-cursor-to-date' function
9470 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9471 (let ((calendar-mark-ring
9472 (list (calendar-gregorian-from-absolute
9473 (or (get-text-property mark 'day)
9474 (get-text-property point 'day))))))
9475 (unwind-protect
9476 (progn
9477 (fset 'calendar-cursor-to-date
9478 (lambda (&optional error dummy)
9479 (calendar-gregorian-from-absolute
9480 (get-text-property point 'day))))
9481 (call-interactively cmd))
9482 (fset 'calendar-cursor-to-date oldf))))))
9484 (defun org-agenda-execute-calendar-command (cmd)
9485 "Execute a calendar command from the agenda with date from cursor."
9486 (org-agenda-check-type t 'agenda 'timeline)
9487 (require 'diary-lib)
9488 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9489 (user-error "Don't know which date to use for the calendar command"))
9490 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9491 (point (point))
9492 (date (calendar-gregorian-from-absolute
9493 (get-text-property point 'day)))
9494 ;; the following 2 vars are needed in the calendar
9495 (displayed-month (car date))
9496 (displayed-year (nth 2 date)))
9497 (unwind-protect
9498 (progn
9499 (fset 'calendar-cursor-to-date
9500 (lambda (&optional error dummy)
9501 (calendar-gregorian-from-absolute
9502 (get-text-property point 'day))))
9503 (call-interactively cmd))
9504 (fset 'calendar-cursor-to-date oldf))))
9506 (defun org-agenda-phases-of-moon ()
9507 "Display the phases of the moon for the 3 months around the cursor date."
9508 (interactive)
9509 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9511 (defun org-agenda-holidays ()
9512 "Display the holidays for the 3 months around the cursor date."
9513 (interactive)
9514 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9516 (defvar calendar-longitude) ; defined in calendar.el
9517 (defvar calendar-latitude) ; defined in calendar.el
9518 (defvar calendar-location-name) ; defined in calendar.el
9520 (defun org-agenda-sunrise-sunset (arg)
9521 "Display sunrise and sunset for the cursor date.
9522 Latitude and longitude can be specified with the variables
9523 `calendar-latitude' and `calendar-longitude'. When called with prefix
9524 argument, latitude and longitude will be prompted for."
9525 (interactive "P")
9526 (require 'solar)
9527 (let ((calendar-longitude (if arg nil calendar-longitude))
9528 (calendar-latitude (if arg nil calendar-latitude))
9529 (calendar-location-name
9530 (if arg "the given coordinates" calendar-location-name)))
9531 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9533 (defun org-agenda-goto-calendar ()
9534 "Open the Emacs calendar with the date at the cursor."
9535 (interactive)
9536 (org-agenda-check-type t 'agenda 'timeline)
9537 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9538 (user-error "Don't know which date to open in calendar")))
9539 (date (calendar-gregorian-from-absolute day))
9540 (calendar-move-hook nil)
9541 (calendar-view-holidays-initially-flag nil)
9542 (calendar-view-diary-initially-flag nil))
9543 (calendar)
9544 (calendar-goto-date date)))
9546 ;;;###autoload
9547 (defun org-calendar-goto-agenda ()
9548 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9549 This is a command that has to be installed in `calendar-mode-map'."
9550 (interactive)
9551 (org-agenda-list nil (calendar-absolute-from-gregorian
9552 (calendar-cursor-to-date))
9553 nil))
9555 (defun org-agenda-convert-date ()
9556 (interactive)
9557 (org-agenda-check-type t 'agenda 'timeline)
9558 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9559 date s)
9560 (unless day
9561 (user-error "Don't know which date to convert"))
9562 (setq date (calendar-gregorian-from-absolute day))
9563 (setq s (concat
9564 "Gregorian: " (calendar-date-string date) "\n"
9565 "ISO: " (calendar-iso-date-string date) "\n"
9566 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9567 "Julian: " (calendar-julian-date-string date) "\n"
9568 "Astron. JD: " (calendar-astro-date-string date)
9569 " (Julian date number at noon UTC)\n"
9570 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9571 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9572 "French: " (calendar-french-date-string date) "\n"
9573 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9574 "Mayan: " (calendar-mayan-date-string date) "\n"
9575 "Coptic: " (calendar-coptic-date-string date) "\n"
9576 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9577 "Persian: " (calendar-persian-date-string date) "\n"
9578 "Chinese: " (calendar-chinese-date-string date) "\n"))
9579 (with-output-to-temp-buffer "*Dates*"
9580 (princ s))
9581 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9583 ;;; Bulk commands
9585 (defun org-agenda-bulk-marked-p ()
9586 (eq (get-char-property (point-at-bol) 'type)
9587 'org-marked-entry-overlay))
9589 (defun org-agenda-bulk-mark (&optional arg)
9590 "Mark the entry at point for future bulk action."
9591 (interactive "p")
9592 (dotimes (i (or arg 1))
9593 (unless (org-get-at-bol 'org-agenda-diary-link)
9594 (let* ((m (org-get-at-bol 'org-hd-marker))
9596 (unless (org-agenda-bulk-marked-p)
9597 (unless m (user-error "Nothing to mark at point"))
9598 (push m org-agenda-bulk-marked-entries)
9599 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9600 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9601 (org-get-todo-face "TODO")
9602 'evaporate)
9603 (overlay-put ov 'type 'org-marked-entry-overlay))
9604 (beginning-of-line 2)
9605 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9606 (beginning-of-line 2))
9607 (message "%d entries marked for bulk action"
9608 (length org-agenda-bulk-marked-entries))))))
9610 (defun org-agenda-bulk-mark-all ()
9611 "Mark all entries for future agenda bulk action."
9612 (interactive)
9613 (org-agenda-bulk-mark-regexp "."))
9615 (defun org-agenda-bulk-mark-regexp (regexp)
9616 "Mark entries matching REGEXP for future agenda bulk action."
9617 (interactive "sMark entries matching regexp: ")
9618 (let ((entries-marked 0))
9619 (save-excursion
9620 (goto-char (point-min))
9621 (goto-char (next-single-property-change (point) 'txt))
9622 (while (re-search-forward regexp nil t)
9623 (when (string-match regexp (get-text-property (point) 'txt))
9624 (setq entries-marked (1+ entries-marked))
9625 (call-interactively 'org-agenda-bulk-mark))))
9626 (if (not entries-marked)
9627 (message "No entry matching this regexp."))))
9629 (defun org-agenda-bulk-unmark (&optional arg)
9630 "Unmark the entry at point for future bulk action."
9631 (interactive "P")
9632 (if arg
9633 (org-agenda-bulk-unmark-all)
9634 (cond ((org-agenda-bulk-marked-p)
9635 (org-agenda-bulk-remove-overlays
9636 (point-at-bol) (+ 2 (point-at-bol)))
9637 (setq org-agenda-bulk-marked-entries
9638 (delete (org-get-at-bol 'org-hd-marker)
9639 org-agenda-bulk-marked-entries))
9640 (beginning-of-line 2)
9641 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9642 (beginning-of-line 2))
9643 (message "%d entries left marked for bulk action"
9644 (length org-agenda-bulk-marked-entries)))
9645 (t (message "No entry to unmark here")))))
9647 (defun org-agenda-bulk-toggle ()
9648 "Toggle marking the entry at point for bulk action."
9649 (interactive)
9650 (if (org-agenda-bulk-marked-p)
9651 (org-agenda-bulk-unmark)
9652 (org-agenda-bulk-mark)))
9654 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9655 "Remove the mark overlays between BEG and END in the agenda buffer.
9656 BEG and END default to the buffer limits.
9658 This only removes the overlays, it does not remove the markers
9659 from the list in `org-agenda-bulk-marked-entries'."
9660 (interactive)
9661 (mapc (lambda (ov)
9662 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9663 (delete-overlay ov)))
9664 (overlays-in (or beg (point-min)) (or end (point-max)))))
9666 (defun org-agenda-bulk-unmark-all ()
9667 "Remove all marks in the agenda buffer.
9668 This will remove the markers and the overlays."
9669 (interactive)
9670 (if (null org-agenda-bulk-marked-entries)
9671 (message "No entry to unmark")
9672 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9673 (setq org-agenda-bulk-marked-entries nil)
9674 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9676 (defcustom org-agenda-persistent-marks nil
9677 "Non-nil means marked items will stay marked after a bulk action.
9678 You can toggle this interactively by typing `p' when prompted for a
9679 bulk action."
9680 :group 'org-agenda
9681 :version "24.1"
9682 :type 'boolean)
9684 (defun org-agenda-bulk-action (&optional arg)
9685 "Execute an remote-editing action on all marked entries.
9686 The prefix arg is passed through to the command if possible."
9687 (interactive "P")
9688 ;; Make sure we have markers, and only valid ones
9689 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9690 (mapc
9691 (lambda (m)
9692 (unless (and (markerp m)
9693 (marker-buffer m)
9694 (buffer-live-p (marker-buffer m))
9695 (marker-position m))
9696 (user-error "Marker %s for bulk command is invalid" m)))
9697 org-agenda-bulk-marked-entries)
9699 ;; Prompt for the bulk command
9700 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9701 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9702 "[S]catter [f]unction "
9703 (when org-agenda-bulk-custom-functions
9704 (concat " Custom: ["
9705 (mapconcat (lambda(f) (char-to-string (car f)))
9706 org-agenda-bulk-custom-functions "")
9707 "]"))))
9708 (catch 'exit
9709 (let* ((action (read-char-exclusive))
9710 (org-log-refile (if org-log-refile 'time nil))
9711 (entries (reverse org-agenda-bulk-marked-entries))
9712 (org-overriding-default-time
9713 (if (get-text-property (point) 'org-agenda-date-header)
9714 (org-get-cursor-date)))
9715 redo-at-end
9716 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9717 (cond
9718 ((equal action ?p)
9719 (let ((org-agenda-persistent-marks
9720 (not org-agenda-persistent-marks)))
9721 (org-agenda-bulk-action)
9722 (throw 'exit nil)))
9724 ((equal action ?$)
9725 (setq cmd '(org-agenda-archive)))
9727 ((equal action ?A)
9728 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9730 ((member action '(?r ?w))
9731 (setq rfloc (org-refile-get-location
9732 "Refile to"
9733 (marker-buffer (car entries))
9734 org-refile-allow-creating-parent-nodes))
9735 (if (nth 3 rfloc)
9736 (setcar (nthcdr 3 rfloc)
9737 (move-marker (make-marker) (nth 3 rfloc)
9738 (or (get-file-buffer (nth 1 rfloc))
9739 (find-buffer-visiting (nth 1 rfloc))
9740 (error "This should not happen")))))
9742 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9743 redo-at-end t))
9745 ((equal action ?t)
9746 (setq state (org-icompleting-read
9747 "Todo state: "
9748 (with-current-buffer (marker-buffer (car entries))
9749 (mapcar 'list org-todo-keywords-1))))
9750 (setq cmd `(let ((org-inhibit-blocking t)
9751 (org-inhibit-logging 'note))
9752 (org-agenda-todo ,state))))
9754 ((memq action '(?- ?+))
9755 (setq tag (org-icompleting-read
9756 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9757 (with-current-buffer (marker-buffer (car entries))
9758 (delq nil
9759 (mapcar (lambda (x)
9760 (if (stringp (car x)) x)) org-tag-alist)))))
9761 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9763 ((memq action '(?s ?d))
9764 (let* ((time
9765 (unless arg
9766 (org-read-date
9767 nil nil nil
9768 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9769 org-overriding-default-time)))
9770 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9771 (setq cmd `(eval '(,c1 arg ,time)))))
9773 ((equal action ?S)
9774 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9775 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9776 (let ((days (read-number
9777 (format "Scatter tasks across how many %sdays: "
9778 (if arg "week" "")) 7)))
9779 (setq cmd
9780 `(let ((distance (1+ (random ,days))))
9781 (if arg
9782 (let ((dist distance)
9783 (day-of-week
9784 (calendar-day-of-week
9785 (calendar-gregorian-from-absolute (org-today)))))
9786 (dotimes (i (1+ dist))
9787 (while (member day-of-week org-agenda-weekend-days)
9788 (incf distance)
9789 (incf day-of-week)
9790 (if (= day-of-week 7)
9791 (setq day-of-week 0)))
9792 (incf day-of-week)
9793 (if (= day-of-week 7)
9794 (setq day-of-week 0)))))
9795 ;; silently fail when try to replan a sexp entry
9796 (condition-case nil
9797 (let* ((date (calendar-gregorian-from-absolute
9798 (+ (org-today) distance)))
9799 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9800 (nth 2 date))))
9801 (org-agenda-schedule nil time))
9802 (error nil)))))))
9804 ((assoc action org-agenda-bulk-custom-functions)
9805 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9806 redo-at-end t))
9808 ((equal action ?f)
9809 (setq cmd (list (intern
9810 (org-icompleting-read "Function: "
9811 obarray 'fboundp t nil nil)))))
9813 (t (user-error "Invalid bulk action")))
9815 ;; Sort the markers, to make sure that parents are handled before children
9816 (setq entries (sort entries
9817 (lambda (a b)
9818 (cond
9819 ((equal (marker-buffer a) (marker-buffer b))
9820 (< (marker-position a) (marker-position b)))
9822 (string< (buffer-name (marker-buffer a))
9823 (buffer-name (marker-buffer b))))))))
9825 ;; Now loop over all markers and apply cmd
9826 (while (setq e (pop entries))
9827 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9828 (if (not pos)
9829 (progn (message "Skipping removed entry at %s" e)
9830 (setq cntskip (1+ cntskip)))
9831 (goto-char pos)
9832 (let (org-loop-over-headlines-in-active-region)
9833 (eval cmd))
9834 (setq cnt (1+ cnt))))
9835 (when redo-at-end (org-agenda-redo))
9836 (unless org-agenda-persistent-marks
9837 (org-agenda-bulk-unmark-all))
9838 (message "Acted on %d entries%s%s"
9840 (if (= cntskip 0)
9842 (format ", skipped %d (disappeared before their turn)"
9843 cntskip))
9844 (if (not org-agenda-persistent-marks)
9845 "" " (kept marked)"))))))
9847 (defun org-agenda-capture (&optional with-time)
9848 "Call `org-capture' with the date at point.
9849 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9850 current HH:MM time."
9851 (interactive "P")
9852 (if (not (eq major-mode 'org-agenda-mode))
9853 (user-error "You cannot do this outside of agenda buffers")
9854 (let ((org-overriding-default-time
9855 (org-get-cursor-date (equal with-time 1))))
9856 (call-interactively 'org-capture))))
9858 ;;; Flagging notes
9860 (defun org-agenda-show-the-flagging-note ()
9861 "Display the flagging note in the other window.
9862 When called a second time in direct sequence, offer to remove the FLAGGING
9863 tag and (if present) the flagging note."
9864 (interactive)
9865 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9866 (win (selected-window))
9867 note heading newhead)
9868 (unless hdmarker
9869 (user-error "No linked entry at point"))
9870 (if (and (eq this-command last-command)
9871 (y-or-n-p "Unflag and remove any flagging note? "))
9872 (progn
9873 (org-agenda-remove-flag hdmarker)
9874 (let ((win (get-buffer-window "*Flagging Note*")))
9875 (and win (delete-window win)))
9876 (message "Entry unflagged"))
9877 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9878 (unless note
9879 (user-error "No flagging note"))
9880 (org-kill-new note)
9881 (org-switch-to-buffer-other-window "*Flagging Note*")
9882 (erase-buffer)
9883 (insert note)
9884 (goto-char (point-min))
9885 (while (re-search-forward "\\\\n" nil t)
9886 (replace-match "\n" t t))
9887 (goto-char (point-min))
9888 (select-window win)
9889 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9891 (defun org-agenda-remove-flag (marker)
9892 "Remove the FLAGGED tag and any flagging note in the entry."
9893 (let (newhead)
9894 (org-with-point-at marker
9895 (org-toggle-tag "FLAGGED" 'off)
9896 (org-entry-delete nil "THEFLAGGINGNOTE")
9897 (setq newhead (org-get-heading)))
9898 (org-agenda-change-all-lines newhead marker)
9899 (message "Entry unflagged")))
9901 (defun org-agenda-get-any-marker (&optional pos)
9902 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9903 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9905 ;;; Appointment reminders
9907 (defvar appt-time-msg-list) ; defined in appt.el
9909 ;;;###autoload
9910 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9911 "Activate appointments found in `org-agenda-files'.
9912 With a \\[universal-argument] prefix, refresh the list of
9913 appointments.
9915 If FILTER is t, interactively prompt the user for a regular
9916 expression, and filter out entries that don't match it.
9918 If FILTER is a string, use this string as a regular expression
9919 for filtering entries out.
9921 If FILTER is a function, filter out entries against which
9922 calling the function returns nil. This function takes one
9923 argument: an entry from `org-agenda-get-day-entries'.
9925 FILTER can also be an alist with the car of each cell being
9926 either 'headline or 'category. For example:
9928 '((headline \"IMPORTANT\")
9929 (category \"Work\"))
9931 will only add headlines containing IMPORTANT or headlines
9932 belonging to the \"Work\" category.
9934 ARGS are symbols indicating what kind of entries to consider.
9935 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9936 \(i.e., deadlines and scheduled items with a hh:mm specification)
9937 and :timestamp entries. See the docstring of `org-diary' for
9938 details and examples.
9940 If an entry has a APPT_WARNTIME property, its value will be used
9941 to override `appt-message-warning-time'."
9942 (interactive "P")
9943 (if refresh (setq appt-time-msg-list nil))
9944 (if (eq filter t)
9945 (setq filter (read-from-minibuffer "Regexp filter: ")))
9946 (let* ((cnt 0) ; count added events
9947 (scope (or args '(:deadline* :scheduled* :timestamp)))
9948 (org-agenda-new-buffers nil)
9949 (org-deadline-warning-days 0)
9950 ;; Do not use `org-today' here because appt only takes
9951 ;; time and without date as argument, so it may pass wrong
9952 ;; information otherwise
9953 (today (org-date-to-gregorian
9954 (time-to-days (current-time))))
9955 (org-agenda-restrict nil)
9956 (files (org-agenda-files 'unrestricted)) entries file
9957 (org-agenda-buffer nil))
9958 ;; Get all entries which may contain an appt
9959 (org-agenda-prepare-buffers files)
9960 (while (setq file (pop files))
9961 (setq entries
9962 (delq nil
9963 (append entries
9964 (apply 'org-agenda-get-day-entries
9965 file today scope)))))
9966 ;; Map thru entries and find if we should filter them out
9967 (mapc
9968 (lambda(x)
9969 (let* ((evt (org-trim
9970 (replace-regexp-in-string
9971 org-bracket-link-regexp "\\3"
9972 (or (get-text-property 1 'txt x) ""))))
9973 (cat (get-text-property 1 'org-category x))
9974 (tod (get-text-property 1 'time-of-day x))
9975 (ok (or (null filter)
9976 (and (stringp filter) (string-match filter evt))
9977 (and (functionp filter) (funcall filter x))
9978 (and (listp filter)
9979 (let ((cat-filter (cadr (assoc 'category filter)))
9980 (evt-filter (cadr (assoc 'headline filter))))
9981 (or (and (stringp cat-filter)
9982 (string-match cat-filter cat))
9983 (and (stringp evt-filter)
9984 (string-match evt-filter evt)))))))
9985 (wrn (get-text-property 1 'warntime x)))
9986 ;; FIXME: Shall we remove text-properties for the appt text?
9987 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9988 (when (and ok tod)
9989 (setq tod (concat "00" (number-to-string tod))
9990 tod (when (string-match
9991 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9992 (concat (match-string 1 tod) ":"
9993 (match-string 2 tod))))
9994 (if (version< emacs-version "23.3")
9995 (appt-add tod evt)
9996 (appt-add tod evt wrn))
9997 (setq cnt (1+ cnt))))) entries)
9998 (org-release-buffers org-agenda-new-buffers)
9999 (if (eq cnt 0)
10000 (message "No event to add")
10001 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10003 (defun org-agenda-todayp (date)
10004 "Does DATE mean today, when considering `org-extend-today-until'?"
10005 (let ((today (org-today))
10006 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10007 date)))
10008 (eq date today)))
10010 (defun org-agenda-todo-yesterday (&optional arg)
10011 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10012 (interactive "P")
10013 (let* ((hour (third (decode-time
10014 (org-current-time))))
10015 (org-extend-today-until (1+ hour)))
10016 (org-agenda-todo arg)))
10018 (provide 'org-agenda)
10020 ;;; org-agenda.el ends here