org-agenda.el (org-agenda-entry-text-mode): Also check against regexp filters
[org-mode.git] / lisp / org-agenda.el
blobd0842220cff518de817d0de0f21490553a185aa2
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 (org-agenda-show-log org-agenda-show-log-scoped)
3983 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3984 (current-buffer))))
3985 (date (calendar-current-date))
3986 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3987 (end (if (org-region-active-p) (region-end) (point-max)))
3988 (day-numbers (org-get-all-dates
3989 beg end 'no-ranges
3990 t org-agenda-show-log-scoped ; always include today
3991 org-timeline-show-empty-dates))
3992 (org-deadline-warning-days 0)
3993 (org-agenda-only-exact-dates t)
3994 (today (org-today))
3995 (past t)
3996 args
3997 s e rtn d emptyp)
3998 (setq org-agenda-redo-command
3999 (list 'let
4000 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4001 (list 'org-switch-to-buffer-other-window (current-buffer))
4002 (list 'org-timeline (list 'quote dotodo))))
4003 (put 'org-agenda-redo-command 'org-lprops nil)
4004 (if (not dopast)
4005 ;; Remove past dates from the list of dates.
4006 (setq day-numbers (delq nil (mapcar (lambda(x)
4007 (if (>= x today) x nil))
4008 day-numbers))))
4009 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4010 (org-compile-prefix-format 'timeline)
4011 (org-set-sorting-strategy 'timeline)
4012 (if org-agenda-show-log-scoped (push :closed args))
4013 (push :timestamp args)
4014 (push :deadline args)
4015 (push :scheduled args)
4016 (push :sexp args)
4017 (if dotodo (push :todo args))
4018 (insert "Timeline of file " entry "\n")
4019 (add-text-properties (point-min) (point)
4020 (list 'face 'org-agenda-structure))
4021 (org-agenda-mark-header-line (point-min))
4022 (while (setq d (pop day-numbers))
4023 (if (and (listp d) (eq (car d) :omitted))
4024 (progn
4025 (setq s (point))
4026 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4027 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4028 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4029 (if (and (>= d today)
4030 dopast
4031 past)
4032 (progn
4033 (setq past nil)
4034 (insert (make-string 79 ?-) "\n")))
4035 (setq date (calendar-gregorian-from-absolute d))
4036 (setq s (point))
4037 (setq rtn (and (not emptyp)
4038 (apply 'org-agenda-get-day-entries entry
4039 date args)))
4040 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4041 (progn
4042 (insert
4043 (if (stringp org-agenda-format-date)
4044 (format-time-string org-agenda-format-date
4045 (org-time-from-absolute date))
4046 (funcall org-agenda-format-date date))
4047 "\n")
4048 (put-text-property s (1- (point)) 'face
4049 (org-agenda-get-day-face date))
4050 (put-text-property s (1- (point)) 'org-date-line t)
4051 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4052 (if (equal d today)
4053 (put-text-property s (1- (point)) 'org-today t))
4054 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4055 (put-text-property s (1- (point)) 'day d)))))
4056 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4057 (point-min)))
4058 (add-text-properties
4059 (point-min) (point-max)
4060 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4061 (org-agenda-finalize)
4062 (setq buffer-read-only t)))
4064 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4065 "Return a list of all relevant day numbers from BEG to END buffer positions.
4066 If NO-RANGES is non-nil, include only the start and end dates of a range,
4067 not every single day in the range. If FORCE-TODAY is non-nil, make
4068 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4069 inactive time stamps (those in square brackets) are included.
4070 When EMPTY is non-nil, also include days without any entries."
4071 (let ((re (concat
4072 (if pre-re pre-re "")
4073 (if inactive org-ts-regexp-both org-ts-regexp)))
4074 dates dates1 date day day1 day2 ts1 ts2 pos)
4075 (if force-today
4076 (setq dates (list (org-today))))
4077 (save-excursion
4078 (goto-char beg)
4079 (while (re-search-forward re end t)
4080 (setq day (time-to-days (org-time-string-to-time
4081 (substring (match-string 1) 0 10)
4082 (current-buffer) (match-beginning 0))))
4083 (or (memq day dates) (push day dates)))
4084 (unless no-ranges
4085 (goto-char beg)
4086 (while (re-search-forward org-tr-regexp end t)
4087 (setq pos (match-beginning 0))
4088 (setq ts1 (substring (match-string 1) 0 10)
4089 ts2 (substring (match-string 2) 0 10)
4090 day1 (time-to-days (org-time-string-to-time
4091 ts1 (current-buffer) pos))
4092 day2 (time-to-days (org-time-string-to-time
4093 ts2 (current-buffer) pos)))
4094 (while (< (setq day1 (1+ day1)) day2)
4095 (or (memq day1 dates) (push day1 dates)))))
4096 (setq dates (sort dates '<))
4097 (when empty
4098 (while (setq day (pop dates))
4099 (setq day2 (car dates))
4100 (push day dates1)
4101 (when (and day2 empty)
4102 (if (or (eq empty t)
4103 (and (numberp empty) (<= (- day2 day) empty)))
4104 (while (< (setq day (1+ day)) day2)
4105 (push (list day) dates1))
4106 (push (cons :omitted (- day2 day)) dates1))))
4107 (setq dates (nreverse dates1)))
4108 dates)))
4110 ;;; Agenda Daily/Weekly
4112 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4113 "Start day for the agenda view.
4114 Custom commands can set this variable in the options section.
4115 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4116 input allowed when reading a date through the Org calendar.
4117 See the docstring of `org-read-date' for details.")
4118 (defvar org-starting-day nil) ; local variable in the agenda buffer
4119 (defvar org-arg-loc nil) ; local variable
4121 (defvar org-agenda-buffer-tmp-name nil)
4122 ;;;###autoload
4123 (defun org-agenda-list (&optional arg start-day span with-hour)
4124 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4125 The view will be for the current day or week, but from the overview buffer
4126 you will be able to go to other days/weeks.
4128 With a numeric prefix argument in an interactive call, the agenda will
4129 span ARG days. Lisp programs should instead specify SPAN to change
4130 the number of days. SPAN defaults to `org-agenda-span'.
4132 START-DAY defaults to TODAY, or to the most recent match for the weekday
4133 given in `org-agenda-start-on-weekday'.
4135 When WITH-HOUR is non-nil, only include scheduled and deadline
4136 items if they have an hour specification like [h]h:mm."
4137 (interactive "P")
4138 (if org-agenda-overriding-arguments
4139 (setq arg (car org-agenda-overriding-arguments)
4140 start-day (nth 1 org-agenda-overriding-arguments)
4141 span (nth 2 org-agenda-overriding-arguments)))
4142 (if (and (integerp arg) (> arg 0))
4143 (setq span arg arg nil))
4144 (catch 'exit
4145 (setq org-agenda-buffer-name
4146 (or org-agenda-buffer-tmp-name
4147 (if org-agenda-sticky
4148 (cond ((and org-keys (stringp org-match))
4149 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4150 (org-keys
4151 (format "*Org Agenda(%s)*" org-keys))
4152 (t "*Org Agenda(a)*")))
4153 org-agenda-buffer-name))
4154 (org-agenda-prepare "Day/Week")
4155 (setq start-day (or start-day org-agenda-start-day))
4156 (if (stringp start-day)
4157 ;; Convert to an absolute day number
4158 (setq start-day (time-to-days (org-read-date nil t start-day))))
4159 (org-compile-prefix-format 'agenda)
4160 (org-set-sorting-strategy 'agenda)
4161 (let* ((span (org-agenda-ndays-to-span
4162 (or span org-agenda-ndays org-agenda-span)))
4163 (today (org-today))
4164 (sd (or start-day today))
4165 (ndays (org-agenda-span-to-ndays span sd))
4166 (org-agenda-start-on-weekday
4167 (if (eq ndays 7)
4168 org-agenda-start-on-weekday))
4169 (thefiles (org-agenda-files nil 'ifmode))
4170 (files thefiles)
4171 (start (if (or (null org-agenda-start-on-weekday)
4172 (< ndays 7))
4174 (let* ((nt (calendar-day-of-week
4175 (calendar-gregorian-from-absolute sd)))
4176 (n1 org-agenda-start-on-weekday)
4177 (d (- nt n1)))
4178 (- sd (+ (if (< d 0) 7 0) d)))))
4179 (day-numbers (list start))
4180 (day-cnt 0)
4181 (inhibit-redisplay (not debug-on-error))
4182 (org-agenda-show-log-scoped org-agenda-show-log)
4183 s e rtn rtnall file date d start-pos end-pos todayp
4184 clocktable-start clocktable-end filter)
4185 (setq org-agenda-redo-command
4186 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4187 (dotimes (n (1- ndays))
4188 (push (1+ (car day-numbers)) day-numbers))
4189 (setq day-numbers (nreverse day-numbers))
4190 (setq clocktable-start (car day-numbers)
4191 clocktable-end (1+ (or (org-last day-numbers) 0)))
4192 (org-set-local 'org-starting-day (car day-numbers))
4193 (org-set-local 'org-arg-loc arg)
4194 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4195 (unless org-agenda-compact-blocks
4196 (let* ((d1 (car day-numbers))
4197 (d2 (org-last day-numbers))
4198 (w1 (org-days-to-iso-week d1))
4199 (w2 (org-days-to-iso-week d2)))
4200 (setq s (point))
4201 (if org-agenda-overriding-header
4202 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4203 nil 'face 'org-agenda-structure) "\n")
4204 (insert (org-agenda-span-name span)
4205 "-agenda"
4206 (if (< (- d2 d1) 350)
4207 (if (= w1 w2)
4208 (format " (W%02d)" w1)
4209 (format " (W%02d-W%02d)" w1 w2))
4211 ":\n")))
4212 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4213 'org-date-line t))
4214 (org-agenda-mark-header-line s))
4215 (while (setq d (pop day-numbers))
4216 (setq date (calendar-gregorian-from-absolute d)
4217 s (point))
4218 (if (or (setq todayp (= d today))
4219 (and (not start-pos) (= d sd)))
4220 (setq start-pos (point))
4221 (if (and start-pos (not end-pos))
4222 (setq end-pos (point))))
4223 (setq files thefiles
4224 rtnall nil)
4225 (while (setq file (pop files))
4226 (catch 'nextfile
4227 (org-check-agenda-file file)
4228 (let ((org-agenda-entry-types org-agenda-entry-types))
4229 ;; Starred types override non-starred equivalents
4230 (when (member :deadline* org-agenda-entry-types)
4231 (setq org-agenda-entry-types
4232 (delq :deadline org-agenda-entry-types)))
4233 (when (member :scheduled* org-agenda-entry-types)
4234 (setq org-agenda-entry-types
4235 (delq :scheduled org-agenda-entry-types)))
4236 ;; Honor with-hour
4237 (when with-hour
4238 (when (member :deadline org-agenda-entry-types)
4239 (setq org-agenda-entry-types
4240 (delq :deadline org-agenda-entry-types))
4241 (push :deadline* org-agenda-entry-types))
4242 (when (member :scheduled org-agenda-entry-types)
4243 (setq org-agenda-entry-types
4244 (delq :scheduled org-agenda-entry-types))
4245 (push :scheduled* org-agenda-entry-types)))
4246 (unless org-agenda-include-deadlines
4247 (setq org-agenda-entry-types
4248 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4249 (cond
4250 ((memq org-agenda-show-log-scoped '(only clockcheck))
4251 (setq rtn (org-agenda-get-day-entries
4252 file date :closed)))
4253 (org-agenda-show-log-scoped
4254 (setq rtn (apply 'org-agenda-get-day-entries
4255 file date
4256 (append '(:closed) org-agenda-entry-types))))
4258 (setq rtn (apply 'org-agenda-get-day-entries
4259 file date
4260 org-agenda-entry-types)))))
4261 (setq rtnall (append rtnall rtn)))) ;; all entries
4262 (if org-agenda-include-diary
4263 (let ((org-agenda-search-headline-for-time t))
4264 (require 'diary-lib)
4265 (setq rtn (org-get-entries-from-diary date))
4266 (setq rtnall (append rtnall rtn))))
4267 (if (or rtnall org-agenda-show-all-dates)
4268 (progn
4269 (setq day-cnt (1+ day-cnt))
4270 (insert
4271 (if (stringp org-agenda-format-date)
4272 (format-time-string org-agenda-format-date
4273 (org-time-from-absolute date))
4274 (funcall org-agenda-format-date date))
4275 "\n")
4276 (put-text-property s (1- (point)) 'face
4277 (org-agenda-get-day-face date))
4278 (put-text-property s (1- (point)) 'org-date-line t)
4279 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4280 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4281 (when todayp
4282 (put-text-property s (1- (point)) 'org-today t))
4283 (setq rtnall
4284 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4285 (if rtnall (insert ;; all entries
4286 (org-agenda-finalize-entries rtnall 'agenda)
4287 "\n"))
4288 (put-text-property s (1- (point)) 'day d)
4289 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4290 (when (and org-agenda-clockreport-mode clocktable-start)
4291 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4292 ;; the above line is to ensure the restricted range!
4293 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4294 tbl)
4295 (setq p (org-plist-delete p :block))
4296 (setq p (plist-put p :tstart clocktable-start))
4297 (setq p (plist-put p :tend clocktable-end))
4298 (setq p (plist-put p :scope 'agenda))
4299 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4300 (setq filter (or org-agenda-tag-filter-while-redo
4301 (get 'org-agenda-tag-filter :preset-filter))))
4302 (setq p (plist-put p :tags (mapconcat (lambda (x)
4303 (if (string-match "[<>=]" x)
4306 filter ""))))
4307 (setq tbl (apply 'org-clock-get-clocktable p))
4308 (insert tbl)))
4309 (goto-char (point-min))
4310 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4311 (unless (and (pos-visible-in-window-p (point-min))
4312 (pos-visible-in-window-p (point-max)))
4313 (goto-char (1- (point-max)))
4314 (recenter -1)
4315 (if (not (pos-visible-in-window-p (or start-pos 1)))
4316 (progn
4317 (goto-char (or start-pos 1))
4318 (recenter 1))))
4319 (goto-char (or start-pos 1))
4320 (add-text-properties (point-min) (point-max)
4321 `(org-agenda-type agenda
4322 org-last-args (,arg ,start-day ,span)
4323 org-redo-cmd ,org-agenda-redo-command
4324 org-series-cmd ,org-cmd))
4325 (if (eq org-agenda-show-log-scoped 'clockcheck)
4326 (org-agenda-show-clocking-issues))
4327 (org-agenda-finalize)
4328 (setq buffer-read-only t)
4329 (message ""))))
4331 (defun org-agenda-ndays-to-span (n)
4332 "Return a span symbol for a span of N days, or N if none matches."
4333 (cond ((symbolp n) n)
4334 ((= n 1) 'day)
4335 ((= n 7) 'week)
4336 (t n)))
4338 (defun org-agenda-span-to-ndays (span &optional start-day)
4339 "Return ndays from SPAN, possibly starting at START-DAY.
4340 START-DAY is an absolute time value."
4341 (cond ((numberp span) span)
4342 ((eq span 'day) 1)
4343 ((eq span 'week) 7)
4344 ((eq span 'month)
4345 (let ((date (calendar-gregorian-from-absolute start-day)))
4346 (calendar-last-day-of-month (car date) (caddr date))))
4347 ((eq span 'year)
4348 (let ((date (calendar-gregorian-from-absolute start-day)))
4349 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4351 (defun org-agenda-span-name (span)
4352 "Return a SPAN name."
4353 (if (null span)
4355 (if (symbolp span)
4356 (capitalize (symbol-name span))
4357 (format "%d days" span))))
4359 ;;; Agenda word search
4361 (defvar org-agenda-search-history nil)
4363 (defvar org-search-syntax-table nil
4364 "Special syntax table for org-mode search.
4365 In this table, we have single quotes not as word constituents, to
4366 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4368 (defvar org-mode-syntax-table) ; From org.el
4369 (defun org-search-syntax-table ()
4370 (unless org-search-syntax-table
4371 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4372 (modify-syntax-entry ?' "." org-search-syntax-table)
4373 (modify-syntax-entry ?` "." org-search-syntax-table))
4374 org-search-syntax-table)
4376 (defvar org-agenda-last-search-view-search-was-boolean nil)
4378 ;;;###autoload
4379 (defun org-search-view (&optional todo-only string edit-at)
4380 "Show all entries that contain a phrase or words or regular expressions.
4382 With optional prefix argument TODO-ONLY, only consider entries that are
4383 TODO entries. The argument STRING can be used to pass a default search
4384 string into this function. If EDIT-AT is non-nil, it means that the
4385 user should get a chance to edit this string, with cursor at position
4386 EDIT-AT.
4388 The search string can be viewed either as a phrase that should be found as
4389 is, or it can be broken into a number of snippets, each of which must match
4390 in a Boolean way to select an entry. The default depends on the variable
4391 `org-agenda-search-view-always-boolean'.
4392 Even if this is turned off (the default) you can always switch to
4393 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4395 The default is a direct search of the whole phrase, where each space in
4396 the search string can expand to an arbitrary amount of whitespace,
4397 including newlines.
4399 If using a Boolean search, the search string is split on whitespace and
4400 each snippet is searched separately, with logical AND to select an entry.
4401 Words prefixed with a minus must *not* occur in the entry. Words without
4402 a prefix or prefixed with a plus must occur in the entry. Matching is
4403 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4404 match whole words, not parts of a word) if
4405 `org-agenda-search-view-force-full-words' is set (default is nil).
4407 Boolean search snippets enclosed by curly braces are interpreted as
4408 regular expressions that must or (when preceded with \"-\") must not
4409 match in the entry. Snippets enclosed into double quotes will be taken
4410 as a whole, to include whitespace.
4412 - If the search string starts with an asterisk, search only in headlines.
4413 - If (possibly after the leading star) the search string starts with an
4414 exclamation mark, this also means to look at TODO entries only, an effect
4415 that can also be achieved with a prefix argument.
4416 - If (possibly after star and exclamation mark) the search string starts
4417 with a colon, this will mean that the (non-regexp) snippets of the
4418 Boolean search must match as full words.
4420 This command searches the agenda files, and in addition the files listed
4421 in `org-agenda-text-search-extra-files'."
4422 (interactive "P")
4423 (if org-agenda-overriding-arguments
4424 (setq todo-only (car org-agenda-overriding-arguments)
4425 string (nth 1 org-agenda-overriding-arguments)
4426 edit-at (nth 2 org-agenda-overriding-arguments)))
4427 (let* ((props (list 'face nil
4428 'done-face 'org-agenda-done
4429 'org-not-done-regexp org-not-done-regexp
4430 'org-todo-regexp org-todo-regexp
4431 'org-complex-heading-regexp org-complex-heading-regexp
4432 'mouse-face 'highlight
4433 'help-echo (format "mouse-2 or RET jump to location")))
4434 (full-words org-agenda-search-view-force-full-words)
4435 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4436 regexp rtn rtnall files file pos inherited-tags
4437 marker category category-pos level tags c neg re boolean
4438 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4439 (unless (and (not edit-at)
4440 (stringp string)
4441 (string-match "\\S-" string))
4442 (setq string (read-string
4443 (if org-agenda-search-view-always-boolean
4444 "[+-]Word/{Regexp} ...: "
4445 "Phrase or [+-]Word/{Regexp} ...: ")
4446 (cond
4447 ((integerp edit-at) (cons string edit-at))
4448 (edit-at string))
4449 'org-agenda-search-history)))
4450 (catch 'exit
4451 (if org-agenda-sticky
4452 (setq org-agenda-buffer-name
4453 (if (stringp string)
4454 (format "*Org Agenda(%s:%s)*"
4455 (or org-keys (or (and todo-only "S") "s")) string)
4456 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4457 (org-agenda-prepare "SEARCH")
4458 (org-compile-prefix-format 'search)
4459 (org-set-sorting-strategy 'search)
4460 (setq org-agenda-redo-command
4461 (list 'org-search-view (if todo-only t nil)
4462 (list 'if 'current-prefix-arg nil string)))
4463 (setq org-agenda-query-string string)
4464 (if (equal (string-to-char string) ?*)
4465 (setq hdl-only t
4466 words (substring string 1))
4467 (setq words string))
4468 (when (equal (string-to-char words) ?!)
4469 (setq todo-only t
4470 words (substring words 1)))
4471 (when (equal (string-to-char words) ?:)
4472 (setq full-words t
4473 words (substring words 1)))
4474 (if (or org-agenda-search-view-always-boolean
4475 (member (string-to-char words) '(?- ?+ ?\{)))
4476 (setq boolean t))
4477 (setq words (org-split-string words))
4478 (let (www w)
4479 (while (setq w (pop words))
4480 (while (and (string-match "\\\\\\'" w) words)
4481 (setq w (concat (substring w 0 -1) " " (pop words))))
4482 (push w www))
4483 (setq words (nreverse www) www nil)
4484 (while (setq w (pop words))
4485 (when (and (string-match "\\`[-+]?{" w)
4486 (not (string-match "}\\'" w)))
4487 (while (and words (not (string-match "}\\'" (car words))))
4488 (setq w (concat w " " (pop words))))
4489 (setq w (concat w " " (pop words))))
4490 (push w www))
4491 (setq words (nreverse www)))
4492 (setq org-agenda-last-search-view-search-was-boolean boolean)
4493 (when boolean
4494 (let (wds w)
4495 (while (setq w (pop words))
4496 (if (or (equal (substring w 0 1) "\"")
4497 (and (> (length w) 1)
4498 (member (substring w 0 1) '("+" "-"))
4499 (equal (substring w 1 2) "\"")))
4500 (while (and words (not (equal (substring w -1) "\"")))
4501 (setq w (concat w " " (pop words)))))
4502 (and (string-match "\\`\\([-+]?\\)\"" w)
4503 (setq w (replace-match "\\1" nil nil w)))
4504 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4505 (push w wds))
4506 (setq words (nreverse wds))))
4507 (if boolean
4508 (mapc (lambda (w)
4509 (setq c (string-to-char w))
4510 (if (equal c ?-)
4511 (setq neg t w (substring w 1))
4512 (if (equal c ?+)
4513 (setq neg nil w (substring w 1))
4514 (setq neg nil)))
4515 (if (string-match "\\`{.*}\\'" w)
4516 (setq re (substring w 1 -1))
4517 (if full-words
4518 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4519 (setq re (regexp-quote (downcase w)))))
4520 (if neg (push re regexps-) (push re regexps+)))
4521 words)
4522 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4523 regexps+))
4524 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4525 (if (not regexps+)
4526 (setq regexp org-outline-regexp-bol)
4527 (setq regexp (pop regexps+))
4528 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4529 regexp))))
4530 (setq files (org-agenda-files nil 'ifmode))
4531 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4532 (pop org-agenda-text-search-extra-files)
4533 (setq files (org-add-archive-files files)))
4534 (setq files (append files org-agenda-text-search-extra-files)
4535 rtnall nil)
4536 (while (setq file (pop files))
4537 (setq ee nil)
4538 (catch 'nextfile
4539 (org-check-agenda-file file)
4540 (setq buffer (if (file-exists-p file)
4541 (org-get-agenda-file-buffer file)
4542 (error "No such file %s" file)))
4543 (if (not buffer)
4544 ;; If file does not exist, make sure an error message is sent
4545 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4546 file))))
4547 (with-current-buffer buffer
4548 (with-syntax-table (org-search-syntax-table)
4549 (unless (derived-mode-p 'org-mode)
4550 (error "Agenda file %s is not in `org-mode'" file))
4551 (let ((case-fold-search t))
4552 (save-excursion
4553 (save-restriction
4554 (if org-agenda-restrict
4555 (narrow-to-region org-agenda-restrict-begin
4556 org-agenda-restrict-end)
4557 (widen))
4558 (goto-char (point-min))
4559 (unless (or (org-at-heading-p)
4560 (outline-next-heading))
4561 (throw 'nextfile t))
4562 (goto-char (max (point-min) (1- (point))))
4563 (while (re-search-forward regexp nil t)
4564 (org-back-to-heading t)
4565 (while (and org-agenda-search-view-max-outline-level
4566 (> (org-reduced-level (org-outline-level))
4567 org-agenda-search-view-max-outline-level)
4568 (forward-line -1)
4569 (outline-back-to-heading t)))
4570 (skip-chars-forward "* ")
4571 (setq beg (point-at-bol)
4572 beg1 (point)
4573 end (progn
4574 (outline-next-heading)
4575 (while (and org-agenda-search-view-max-outline-level
4576 (> (org-reduced-level (org-outline-level))
4577 org-agenda-search-view-max-outline-level)
4578 (forward-line 1)
4579 (outline-next-heading)))
4580 (point)))
4582 (catch :skip
4583 (goto-char beg)
4584 (org-agenda-skip)
4585 (setq str (buffer-substring-no-properties
4586 (point-at-bol)
4587 (if hdl-only (point-at-eol) end)))
4588 (mapc (lambda (wr) (when (string-match wr str)
4589 (goto-char (1- end))
4590 (throw :skip t)))
4591 regexps-)
4592 (mapc (lambda (wr) (unless (string-match wr str)
4593 (goto-char (1- end))
4594 (throw :skip t)))
4595 (if todo-only
4596 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4597 regexps+)
4598 regexps+))
4599 (goto-char beg)
4600 (setq marker (org-agenda-new-marker (point))
4601 category (org-get-category)
4602 level (make-string (org-reduced-level (org-outline-level)) ? )
4603 category-pos (get-text-property (point) 'org-category-position)
4604 inherited-tags
4605 (or (eq org-agenda-show-inherited-tags 'always)
4606 (and (listp org-agenda-show-inherited-tags)
4607 (memq 'todo org-agenda-show-inherited-tags))
4608 (and (eq org-agenda-show-inherited-tags t)
4609 (or (eq org-agenda-use-tag-inheritance t)
4610 (memq 'todo org-agenda-use-tag-inheritance))))
4611 tags (org-get-tags-at nil (not inherited-tags))
4612 txt (org-agenda-format-item
4614 (buffer-substring-no-properties
4615 beg1 (point-at-eol))
4616 level category tags t))
4617 (org-add-props txt props
4618 'org-marker marker 'org-hd-marker marker
4619 'org-todo-regexp org-todo-regexp
4620 'level level
4621 'org-complex-heading-regexp org-complex-heading-regexp
4622 'priority 1000 'org-category category
4623 'org-category-position category-pos
4624 'type "search")
4625 (push txt ee)
4626 (goto-char (1- end))))))))))
4627 (setq rtn (nreverse ee))
4628 (setq rtnall (append rtnall rtn)))
4629 (if org-agenda-overriding-header
4630 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4631 nil 'face 'org-agenda-structure) "\n")
4632 (insert "Search words: ")
4633 (add-text-properties (point-min) (1- (point))
4634 (list 'face 'org-agenda-structure))
4635 (setq pos (point))
4636 (insert string "\n")
4637 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4638 (setq pos (point))
4639 (unless org-agenda-multi
4640 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4641 (add-text-properties pos (1- (point))
4642 (list 'face 'org-agenda-structure))))
4643 (org-agenda-mark-header-line (point-min))
4644 (when rtnall
4645 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4646 (goto-char (point-min))
4647 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4648 (add-text-properties (point-min) (point-max)
4649 `(org-agenda-type search
4650 org-last-args (,todo-only ,string ,edit-at)
4651 org-redo-cmd ,org-agenda-redo-command
4652 org-series-cmd ,org-cmd))
4653 (org-agenda-finalize)
4654 (setq buffer-read-only t))))
4656 ;;; Agenda TODO list
4658 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4659 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4660 (concat
4661 (if (or (equal keywords "ALL") (not keywords))
4662 (propertize "ALL" 'face 'warning)
4663 (mapconcat
4664 (lambda (kw)
4665 (propertize kw 'face (org-get-todo-face kw)))
4666 (org-split-string keywords "|")
4667 "|"))
4668 "\n"))
4670 (defvar org-select-this-todo-keyword nil)
4671 (defvar org-last-arg nil)
4673 ;;;###autoload
4674 (defun org-todo-list (&optional arg)
4675 "Show all (not done) TODO entries from all agenda file in a single list.
4676 The prefix arg can be used to select a specific TODO keyword and limit
4677 the list to these. When using \\[universal-argument], you will be prompted
4678 for a keyword. A numeric prefix directly selects the Nth keyword in
4679 `org-todo-keywords-1'."
4680 (interactive "P")
4681 (if org-agenda-overriding-arguments
4682 (setq arg org-agenda-overriding-arguments))
4683 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4684 (let* ((today (org-today))
4685 (date (calendar-gregorian-from-absolute today))
4686 (kwds org-todo-keywords-for-agenda)
4687 (completion-ignore-case t)
4688 (org-select-this-todo-keyword
4689 (if (stringp arg) arg
4690 (and arg (integerp arg) (> arg 0)
4691 (nth (1- arg) kwds))))
4692 rtn rtnall files file pos)
4693 (when (equal arg '(4))
4694 (setq org-select-this-todo-keyword
4695 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4696 (mapcar 'list kwds) nil nil)))
4697 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4698 (catch 'exit
4699 (if org-agenda-sticky
4700 (setq org-agenda-buffer-name
4701 (if (stringp org-select-this-todo-keyword)
4702 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4703 org-select-this-todo-keyword)
4704 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4705 (org-agenda-prepare "TODO")
4706 (org-compile-prefix-format 'todo)
4707 (org-set-sorting-strategy 'todo)
4708 (setq org-agenda-redo-command
4709 `(org-todo-list (or (and (numberp current-prefix-arg)
4710 current-prefix-arg)
4711 ,org-select-this-todo-keyword
4712 current-prefix-arg ,arg)))
4713 (setq files (org-agenda-files nil 'ifmode)
4714 rtnall nil)
4715 (while (setq file (pop files))
4716 (catch 'nextfile
4717 (org-check-agenda-file file)
4718 (setq rtn (org-agenda-get-day-entries file date :todo))
4719 (setq rtnall (append rtnall rtn))))
4720 (if org-agenda-overriding-header
4721 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4722 nil 'face 'org-agenda-structure) "\n")
4723 (insert "Global list of TODO items of type: ")
4724 (add-text-properties (point-min) (1- (point))
4725 (list 'face 'org-agenda-structure
4726 'short-heading
4727 (concat "ToDo: "
4728 (or org-select-this-todo-keyword "ALL"))))
4729 (org-agenda-mark-header-line (point-min))
4730 (insert (org-agenda-propertize-selected-todo-keywords
4731 org-select-this-todo-keyword))
4732 (setq pos (point))
4733 (unless org-agenda-multi
4734 (insert "Available with `N r': (0)[ALL]")
4735 (let ((n 0) s)
4736 (mapc (lambda (x)
4737 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4738 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4739 (insert "\n "))
4740 (insert " " s))
4741 kwds))
4742 (insert "\n"))
4743 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4744 (org-agenda-mark-header-line (point-min))
4745 (when rtnall
4746 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4747 (goto-char (point-min))
4748 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4749 (add-text-properties (point-min) (point-max)
4750 `(org-agenda-type todo
4751 org-last-args ,arg
4752 org-redo-cmd ,org-agenda-redo-command
4753 org-series-cmd ,org-cmd))
4754 (org-agenda-finalize)
4755 (setq buffer-read-only t))))
4757 ;;; Agenda tags match
4759 ;;;###autoload
4760 (defun org-tags-view (&optional todo-only match)
4761 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4762 The prefix arg TODO-ONLY limits the search to TODO entries."
4763 (interactive "P")
4764 (if org-agenda-overriding-arguments
4765 (setq todo-only (car org-agenda-overriding-arguments)
4766 match (nth 1 org-agenda-overriding-arguments)))
4767 (let* ((org-tags-match-list-sublevels
4768 org-tags-match-list-sublevels)
4769 (completion-ignore-case t)
4770 rtn rtnall files file pos matcher
4771 buffer)
4772 (when (and (stringp match) (not (string-match "\\S-" match)))
4773 (setq match nil))
4774 (catch 'exit
4775 (if org-agenda-sticky
4776 (setq org-agenda-buffer-name
4777 (if (stringp match)
4778 (format "*Org Agenda(%s:%s)*"
4779 (or org-keys (or (and todo-only "M") "m")) match)
4780 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4781 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4782 ;; expanding tags within `org-make-tags-matcher'
4783 (org-agenda-prepare (concat "TAGS " match))
4784 (setq matcher (org-make-tags-matcher match)
4785 match (car matcher) matcher (cdr matcher))
4786 (org-compile-prefix-format 'tags)
4787 (org-set-sorting-strategy 'tags)
4788 (setq org-agenda-query-string match)
4789 (setq org-agenda-redo-command
4790 (list 'org-tags-view `(quote ,todo-only)
4791 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4792 (setq files (org-agenda-files nil 'ifmode)
4793 rtnall nil)
4794 (while (setq file (pop files))
4795 (catch 'nextfile
4796 (org-check-agenda-file file)
4797 (setq buffer (if (file-exists-p file)
4798 (org-get-agenda-file-buffer file)
4799 (error "No such file %s" file)))
4800 (if (not buffer)
4801 ;; If file does not exist, error message to agenda
4802 (setq rtn (list
4803 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4804 rtnall (append rtnall rtn))
4805 (with-current-buffer buffer
4806 (unless (derived-mode-p 'org-mode)
4807 (error "Agenda file %s is not in `org-mode'" file))
4808 (save-excursion
4809 (save-restriction
4810 (if org-agenda-restrict
4811 (narrow-to-region org-agenda-restrict-begin
4812 org-agenda-restrict-end)
4813 (widen))
4814 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4815 (setq rtnall (append rtnall rtn))))))))
4816 (if org-agenda-overriding-header
4817 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4818 nil 'face 'org-agenda-structure) "\n")
4819 (insert "Headlines with TAGS match: ")
4820 (add-text-properties (point-min) (1- (point))
4821 (list 'face 'org-agenda-structure
4822 'short-heading
4823 (concat "Match: " match)))
4824 (setq pos (point))
4825 (insert match "\n")
4826 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4827 (setq pos (point))
4828 (unless org-agenda-multi
4829 (insert "Press `C-u r' to search again with new search string\n"))
4830 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4831 (org-agenda-mark-header-line (point-min))
4832 (when rtnall
4833 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4834 (goto-char (point-min))
4835 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4836 (add-text-properties (point-min) (point-max)
4837 `(org-agenda-type tags
4838 org-last-args (,todo-only ,match)
4839 org-redo-cmd ,org-agenda-redo-command
4840 org-series-cmd ,org-cmd))
4841 (org-agenda-finalize)
4842 (setq buffer-read-only t))))
4844 ;;; Agenda Finding stuck projects
4846 (defvar org-agenda-skip-regexp nil
4847 "Regular expression used in skipping subtrees for the agenda.
4848 This is basically a temporary global variable that can be set and then
4849 used by user-defined selections using `org-agenda-skip-function'.")
4851 (defvar org-agenda-overriding-header nil
4852 "When set during agenda, todo and tags searches it replaces the header.
4853 This variable should not be set directly, but custom commands can bind it
4854 in the options section.")
4856 (defun org-agenda-skip-entry-when-regexp-matches ()
4857 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4858 If yes, it returns the end position of this entry, causing agenda commands
4859 to skip the entry but continuing the search in the subtree. This is a
4860 function that can be put into `org-agenda-skip-function' for the duration
4861 of a command."
4862 (let ((end (save-excursion (org-end-of-subtree t)))
4863 skip)
4864 (save-excursion
4865 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4866 (and skip end)))
4868 (defun org-agenda-skip-subtree-when-regexp-matches ()
4869 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4870 If yes, it returns the end position of this tree, causing agenda commands
4871 to skip this subtree. This is a function that can be put into
4872 `org-agenda-skip-function' for the duration of a command."
4873 (let ((end (save-excursion (org-end-of-subtree t)))
4874 skip)
4875 (save-excursion
4876 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4877 (and skip end)))
4879 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4880 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4881 If yes, it returns the end position of the current entry (NOT the tree),
4882 causing agenda commands to skip the entry but continuing the search in
4883 the subtree. This is a function that can be put into
4884 `org-agenda-skip-function' for the duration of a command. An important
4885 use of this function is for the stuck project list."
4886 (let ((end (save-excursion (org-end-of-subtree t)))
4887 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4888 skip)
4889 (save-excursion
4890 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4891 (and skip entry-end)))
4893 (defun org-agenda-skip-entry-if (&rest conditions)
4894 "Skip entry if any of CONDITIONS is true.
4895 See `org-agenda-skip-if' for details."
4896 (org-agenda-skip-if nil conditions))
4898 (defun org-agenda-skip-subtree-if (&rest conditions)
4899 "Skip entry if any of CONDITIONS is true.
4900 See `org-agenda-skip-if' for details."
4901 (org-agenda-skip-if t conditions))
4903 (defun org-agenda-skip-if (subtree conditions)
4904 "Checks current entity for CONDITIONS.
4905 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4906 the entry (i.e. the text before the next heading) is checked.
4908 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4909 from different tests. Valid conditions are:
4911 scheduled Check if there is a scheduled cookie
4912 notscheduled Check if there is no scheduled cookie
4913 deadline Check if there is a deadline
4914 notdeadline Check if there is no deadline
4915 timestamp Check if there is a timestamp (also deadline or scheduled)
4916 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4917 regexp Check if regexp matches
4918 notregexp Check if regexp does not match.
4919 todo Check if TODO keyword matches
4920 nottodo Check if TODO keyword does not match
4922 The regexp is taken from the conditions list, it must come right after
4923 the `regexp' or `notregexp' element.
4925 `todo' and `nottodo' accept as an argument a list of todo
4926 keywords, which may include \"*\" to match any todo keyword.
4928 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4930 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4932 Instead of a list, a keyword class may be given. For example:
4934 (org-agenda-skip-entry-if 'nottodo 'done)
4936 would skip entries that haven't been marked with any of \"DONE\"
4937 keywords. Possible classes are: `todo', `done', `any'.
4939 If any of these conditions is met, this function returns the end point of
4940 the entity, causing the search to continue from there. This is a function
4941 that can be put into `org-agenda-skip-function' for the duration of a command."
4942 (let (beg end m)
4943 (org-back-to-heading t)
4944 (setq beg (point)
4945 end (if subtree
4946 (progn (org-end-of-subtree t) (point))
4947 (progn (outline-next-heading) (1- (point)))))
4948 (goto-char beg)
4949 (and
4951 (and (memq 'scheduled conditions)
4952 (re-search-forward org-scheduled-time-regexp end t))
4953 (and (memq 'notscheduled conditions)
4954 (not (re-search-forward org-scheduled-time-regexp end t)))
4955 (and (memq 'deadline conditions)
4956 (re-search-forward org-deadline-time-regexp end t))
4957 (and (memq 'notdeadline conditions)
4958 (not (re-search-forward org-deadline-time-regexp end t)))
4959 (and (memq 'timestamp conditions)
4960 (re-search-forward org-ts-regexp end t))
4961 (and (memq 'nottimestamp conditions)
4962 (not (re-search-forward org-ts-regexp end t)))
4963 (and (setq m (memq 'regexp conditions))
4964 (stringp (nth 1 m))
4965 (re-search-forward (nth 1 m) end t))
4966 (and (setq m (memq 'notregexp conditions))
4967 (stringp (nth 1 m))
4968 (not (re-search-forward (nth 1 m) end t)))
4969 (and (or
4970 (setq m (memq 'nottodo conditions))
4971 (setq m (memq 'todo-unblocked conditions))
4972 (setq m (memq 'nottodo-unblocked conditions))
4973 (setq m (memq 'todo conditions)))
4974 (org-agenda-skip-if-todo m end)))
4975 end)))
4977 (defun org-agenda-skip-if-todo (args end)
4978 "Helper function for `org-agenda-skip-if', do not use it directly.
4979 ARGS is a list with first element either `todo', `nottodo',
4980 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4981 a list of TODO keywords, or a state symbol `todo' or `done' or
4982 `any'."
4983 (let ((kw (car args))
4984 (arg (cadr args))
4985 todo-wds todo-re)
4986 (setq todo-wds
4987 (org-uniquify
4988 (cond
4989 ((listp arg) ;; list of keywords
4990 (if (member "*" arg)
4991 (mapcar 'substring-no-properties org-todo-keywords-1)
4992 arg))
4993 ((symbolp arg) ;; keyword class name
4994 (cond
4995 ((eq arg 'todo)
4996 (org-delete-all org-done-keywords
4997 (mapcar 'substring-no-properties
4998 org-todo-keywords-1)))
4999 ((eq arg 'done) org-done-keywords)
5000 ((eq arg 'any)
5001 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5002 (setq todo-re
5003 (concat "^\\*+[ \t]+\\<\\("
5004 (mapconcat 'identity todo-wds "\\|")
5005 "\\)\\>"))
5006 (cond
5007 ((eq kw 'todo) (re-search-forward todo-re end t))
5008 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5009 ((eq kw 'todo-unblocked)
5010 (catch 'unblocked
5011 (while (re-search-forward todo-re end t)
5012 (or (org-entry-blocked-p) (throw 'unblocked t)))
5013 nil))
5014 ((eq kw 'nottodo-unblocked)
5015 (catch 'unblocked
5016 (while (re-search-forward todo-re end t)
5017 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5021 ;;;###autoload
5022 (defun org-agenda-list-stuck-projects (&rest ignore)
5023 "Create agenda view for projects that are stuck.
5024 Stuck projects are project that have no next actions. For the definitions
5025 of what a project is and how to check if it stuck, customize the variable
5026 `org-stuck-projects'."
5027 (interactive)
5028 (let* ((org-agenda-skip-function
5029 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5030 ;; We could have used org-agenda-skip-if here.
5031 (org-agenda-overriding-header
5032 (or org-agenda-overriding-header "List of stuck projects: "))
5033 (matcher (nth 0 org-stuck-projects))
5034 (todo (nth 1 org-stuck-projects))
5035 (todo-wds (if (member "*" todo)
5036 (progn
5037 (org-agenda-prepare-buffers (org-agenda-files
5038 nil 'ifmode))
5039 (org-delete-all
5040 org-done-keywords-for-agenda
5041 (copy-sequence org-todo-keywords-for-agenda)))
5042 todo))
5043 (todo-re (concat "^\\*+[ \t]+\\("
5044 (mapconcat 'identity todo-wds "\\|")
5045 "\\)\\>"))
5046 (tags (nth 2 org-stuck-projects))
5047 (tags-re (if (member "*" tags)
5048 (concat org-outline-regexp-bol
5049 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5050 (if tags
5051 (concat org-outline-regexp-bol
5052 ".*:\\("
5053 (mapconcat 'identity tags "\\|")
5054 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5055 (gen-re (nth 3 org-stuck-projects))
5056 (re-list
5057 (delq nil
5058 (list
5059 (if todo todo-re)
5060 (if tags tags-re)
5061 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5062 gen-re)))))
5063 (setq org-agenda-skip-regexp
5064 (if re-list
5065 (mapconcat 'identity re-list "\\|")
5066 (error "No information how to identify unstuck projects")))
5067 (org-tags-view nil matcher)
5068 (with-current-buffer org-agenda-buffer-name
5069 (setq org-agenda-redo-command
5070 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5072 ;;; Diary integration
5074 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5075 (defvar diary-list-entries-hook)
5076 (defvar diary-time-regexp)
5077 (defun org-get-entries-from-diary (date)
5078 "Get the (Emacs Calendar) diary entries for DATE."
5079 (require 'diary-lib)
5080 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5081 (diary-display-hook '(fancy-diary-display))
5082 (diary-display-function 'fancy-diary-display)
5083 (pop-up-frames nil)
5084 (diary-list-entries-hook
5085 (cons 'org-diary-default-entry diary-list-entries-hook))
5086 (diary-file-name-prefix nil) ; turn this feature off
5087 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5088 entries
5089 (org-disable-agenda-to-diary t))
5090 (save-excursion
5091 (save-window-excursion
5092 (funcall (if (fboundp 'diary-list-entries)
5093 'diary-list-entries 'list-diary-entries)
5094 date 1)))
5095 (if (not (get-buffer diary-fancy-buffer))
5096 (setq entries nil)
5097 (with-current-buffer diary-fancy-buffer
5098 (setq buffer-read-only nil)
5099 (if (zerop (buffer-size))
5100 ;; No entries
5101 (setq entries nil)
5102 ;; Omit the date and other unnecessary stuff
5103 (org-agenda-cleanup-fancy-diary)
5104 ;; Add prefix to each line and extend the text properties
5105 (if (zerop (buffer-size))
5106 (setq entries nil)
5107 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5108 (setq entries
5109 (with-temp-buffer
5110 (insert entries) (goto-char (point-min))
5111 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5112 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5113 (replace-match (concat "; " (match-string 1)))))
5114 (buffer-string)))))
5115 (set-buffer-modified-p nil)
5116 (kill-buffer diary-fancy-buffer)))
5117 (when entries
5118 (setq entries (org-split-string entries "\n"))
5119 (setq entries
5120 (mapcar
5121 (lambda (x)
5122 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5123 ;; Extend the text properties to the beginning of the line
5124 (org-add-props x (text-properties-at (1- (length x)) x)
5125 'type "diary" 'date date 'face 'org-agenda-diary))
5126 entries)))))
5128 (defvar org-agenda-cleanup-fancy-diary-hook nil
5129 "Hook run when the fancy diary buffer is cleaned up.")
5131 (defun org-agenda-cleanup-fancy-diary ()
5132 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5133 This gets rid of the date, the underline under the date, and
5134 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5135 date. It also removes lines that contain only whitespace."
5136 (goto-char (point-min))
5137 (if (looking-at ".*?:[ \t]*")
5138 (progn
5139 (replace-match "")
5140 (re-search-forward "\n=+$" nil t)
5141 (replace-match "")
5142 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5143 (re-search-forward "\n=+$" nil t)
5144 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5145 (goto-char (point-min))
5146 (while (re-search-forward "^ +\n" nil t)
5147 (replace-match ""))
5148 (goto-char (point-min))
5149 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5150 (replace-match ""))
5151 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5153 ;; Make sure entries from the diary have the right text properties.
5154 (eval-after-load "diary-lib"
5155 '(if (boundp 'diary-modify-entry-list-string-function)
5156 ;; We can rely on the hook, nothing to do
5158 ;; Hook not available, must use advice to make this work
5159 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5160 "Make the position visible."
5161 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5162 (stringp string)
5163 buffer-file-name)
5164 (setq string (org-modify-diary-entry-string string))))))
5166 (defun org-modify-diary-entry-string (string)
5167 "Add text properties to string, allowing org-mode to act on it."
5168 (org-add-props string nil
5169 'mouse-face 'highlight
5170 'help-echo (if buffer-file-name
5171 (format "mouse-2 or RET jump to diary file %s"
5172 (abbreviate-file-name buffer-file-name))
5174 'org-agenda-diary-link t
5175 'org-marker (org-agenda-new-marker (point-at-bol))))
5177 (defun org-diary-default-entry ()
5178 "Add a dummy entry to the diary.
5179 Needed to avoid empty dates which mess up holiday display."
5180 ;; Catch the error if dealing with the new add-to-diary-alist
5181 (when org-disable-agenda-to-diary
5182 (condition-case nil
5183 (org-add-to-diary-list original-date "Org-mode dummy" "")
5184 (error
5185 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5187 (defun org-add-to-diary-list (&rest args)
5188 (if (fboundp 'diary-add-to-list)
5189 (apply 'diary-add-to-list args)
5190 (apply 'add-to-diary-list args)))
5192 (defvar org-diary-last-run-time nil)
5194 ;;;###autoload
5195 (defun org-diary (&rest args)
5196 "Return diary information from org files.
5197 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5198 It accesses org files and extracts information from those files to be
5199 listed in the diary. The function accepts arguments specifying what
5200 items should be listed. For a list of arguments allowed here, see the
5201 variable `org-agenda-entry-types'.
5203 The call in the diary file should look like this:
5205 &%%(org-diary) ~/path/to/some/orgfile.org
5207 Use a separate line for each org file to check. Or, if you omit the file name,
5208 all files listed in `org-agenda-files' will be checked automatically:
5210 &%%(org-diary)
5212 If you don't give any arguments (as in the example above), the default value
5213 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5214 So the example above may also be written as
5216 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5218 The function expects the lisp variables `entry' and `date' to be provided
5219 by the caller, because this is how the calendar works. Don't use this
5220 function from a program - use `org-agenda-get-day-entries' instead."
5221 (when (> (- (org-float-time)
5222 org-agenda-last-marker-time)
5224 ;; I am not sure if this works with sticky agendas, because the marker
5225 ;; list is then no longer a global variable.
5226 (org-agenda-reset-markers))
5227 (org-compile-prefix-format 'agenda)
5228 (org-set-sorting-strategy 'agenda)
5229 (setq args (or args org-agenda-entry-types))
5230 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5231 (list entry)
5232 (org-agenda-files t)))
5233 (time (org-float-time))
5234 file rtn results)
5235 (when (or (not org-diary-last-run-time)
5236 (> (- time
5237 org-diary-last-run-time)
5239 (org-agenda-prepare-buffers files))
5240 (setq org-diary-last-run-time time)
5241 ;; If this is called during org-agenda, don't return any entries to
5242 ;; the calendar. Org Agenda will list these entries itself.
5243 (if org-disable-agenda-to-diary (setq files nil))
5244 (while (setq file (pop files))
5245 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5246 (setq results (append results rtn)))
5247 (when results
5248 (setq results
5249 (mapcar (lambda (i) (replace-regexp-in-string
5250 org-bracket-link-regexp "\\3" i)) results))
5251 (concat (org-agenda-finalize-entries results) "\n"))))
5253 ;;; Agenda entry finders
5255 (defun org-agenda-get-day-entries (file date &rest args)
5256 "Does the work for `org-diary' and `org-agenda'.
5257 FILE is the path to a file to be checked for entries. DATE is date like
5258 the one returned by `calendar-current-date'. ARGS are symbols indicating
5259 which kind of entries should be extracted. For details about these, see
5260 the documentation of `org-diary'."
5261 (setq args (or args org-agenda-entry-types))
5262 (let* ((org-startup-folded nil)
5263 (org-startup-align-all-tables nil)
5264 (buffer (if (file-exists-p file)
5265 (org-get-agenda-file-buffer file)
5266 (error "No such file %s" file)))
5267 arg results rtn deadline-results)
5268 (if (not buffer)
5269 ;; If file does not exist, make sure an error message ends up in diary
5270 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5271 (with-current-buffer buffer
5272 (unless (derived-mode-p 'org-mode)
5273 (error "Agenda file %s is not in `org-mode'" file))
5274 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5275 (let ((case-fold-search nil))
5276 (save-excursion
5277 (save-restriction
5278 (if org-agenda-restrict
5279 (narrow-to-region org-agenda-restrict-begin
5280 org-agenda-restrict-end)
5281 (widen))
5282 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5283 (while (setq arg (pop args))
5284 (cond
5285 ((and (eq arg :todo)
5286 (equal date (calendar-gregorian-from-absolute
5287 (org-today))))
5288 (setq rtn (org-agenda-get-todos))
5289 (setq results (append results rtn)))
5290 ((eq arg :timestamp)
5291 (setq rtn (org-agenda-get-blocks))
5292 (setq results (append results rtn))
5293 (setq rtn (org-agenda-get-timestamps deadline-results))
5294 (setq results (append results rtn)))
5295 ((eq arg :sexp)
5296 (setq rtn (org-agenda-get-sexps))
5297 (setq results (append results rtn)))
5298 ((eq arg :scheduled)
5299 (setq rtn (org-agenda-get-scheduled deadline-results))
5300 (setq results (append results rtn)))
5301 ((eq arg :scheduled*)
5302 (setq rtn (org-agenda-get-scheduled deadline-results t))
5303 (setq results (append results rtn)))
5304 ((eq arg :closed)
5305 (setq rtn (org-agenda-get-progress))
5306 (setq results (append results rtn)))
5307 ((eq arg :deadline)
5308 (setq rtn (org-agenda-get-deadlines))
5309 (setq deadline-results (copy-sequence rtn))
5310 (setq results (append results rtn)))
5311 ((eq arg :deadline*)
5312 (setq rtn (org-agenda-get-deadlines t))
5313 (setq deadline-results (copy-sequence rtn))
5314 (setq results (append results rtn))))))))
5315 results))))
5317 (defsubst org-em (x y list)
5318 "Is X or Y a member of LIST?"
5319 (or (memq x list) (memq y list)))
5321 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5322 (defvar org-agenda-sorting-strategy-selected nil)
5324 (defun org-agenda-get-todos ()
5325 "Return the TODO information for agenda display."
5326 (let* ((props (list 'face nil
5327 'done-face 'org-agenda-done
5328 'org-not-done-regexp org-not-done-regexp
5329 'org-todo-regexp org-todo-regexp
5330 'org-complex-heading-regexp org-complex-heading-regexp
5331 'mouse-face 'highlight
5332 'help-echo
5333 (format "mouse-2 or RET jump to org file %s"
5334 (abbreviate-file-name buffer-file-name))))
5335 (regexp (format org-heading-keyword-regexp-format
5336 (cond
5337 ((and org-select-this-todo-keyword
5338 (equal org-select-this-todo-keyword "*"))
5339 org-todo-regexp)
5340 (org-select-this-todo-keyword
5341 (concat "\\("
5342 (mapconcat 'identity
5343 (org-split-string
5344 org-select-this-todo-keyword
5345 "|")
5346 "\\|") "\\)"))
5347 (t org-not-done-regexp))))
5348 marker priority category category-pos level tags todo-state ts-date ts-date-type
5349 ee txt beg end inherited-tags todo-state-end-pos)
5350 (goto-char (point-min))
5351 (while (re-search-forward regexp nil t)
5352 (catch :skip
5353 (save-match-data
5354 (beginning-of-line)
5355 (org-agenda-skip)
5356 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5357 (unless (and (setq todo-state (org-get-todo-state))
5358 (setq todo-state-end-pos (match-end 2)))
5359 (goto-char end)
5360 (throw :skip nil))
5361 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5362 (goto-char (1+ beg))
5363 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5364 (throw :skip nil)))
5365 (goto-char (match-beginning 2))
5366 (setq marker (org-agenda-new-marker (match-beginning 0))
5367 category (org-get-category)
5368 ts-date (let (ts)
5369 (save-match-data
5370 (cond ((org-em 'scheduled-up 'scheduled-down
5371 org-agenda-sorting-strategy-selected)
5372 (setq ts (org-entry-get (point) "SCHEDULED")
5373 ts-date-type " scheduled"))
5374 ((org-em 'deadline-up 'deadline-down
5375 org-agenda-sorting-strategy-selected)
5376 (setq ts (org-entry-get (point) "DEADLINE")
5377 ts-date-type " deadline"))
5378 ((org-em 'ts-up 'ts-down
5379 org-agenda-sorting-strategy-selected)
5380 (setq ts (org-entry-get (point) "TIMESTAMP")
5381 ts-date-type " timestamp"))
5382 ((org-em 'tsia-up 'tsia-down
5383 org-agenda-sorting-strategy-selected)
5384 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5385 ts-date-type " timestamp_ia"))
5386 ((org-em 'timestamp-up 'timestamp-down
5387 org-agenda-sorting-strategy-selected)
5388 (setq ts (or (org-entry-get (point) "SCHEDULED")
5389 (org-entry-get (point) "DEADLINE")
5390 (org-entry-get (point) "TIMESTAMP")
5391 (org-entry-get (point) "TIMESTAMP_IA"))
5392 ts-date-type ""))
5393 (t (setq ts-date-type "")))
5394 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5395 category-pos (get-text-property (point) 'org-category-position)
5396 txt (org-trim
5397 (buffer-substring (match-beginning 2) (match-end 0)))
5398 inherited-tags
5399 (or (eq org-agenda-show-inherited-tags 'always)
5400 (and (listp org-agenda-show-inherited-tags)
5401 (memq 'todo org-agenda-show-inherited-tags))
5402 (and (eq org-agenda-show-inherited-tags t)
5403 (or (eq org-agenda-use-tag-inheritance t)
5404 (memq 'todo org-agenda-use-tag-inheritance))))
5405 tags (org-get-tags-at nil (not inherited-tags))
5406 level (make-string (org-reduced-level (org-outline-level)) ? )
5407 txt (org-agenda-format-item "" txt level category tags t)
5408 priority (1+ (org-get-priority txt)))
5409 (org-add-props txt props
5410 'org-marker marker 'org-hd-marker marker
5411 'priority priority 'org-category category
5412 'level level
5413 'ts-date ts-date
5414 'org-category-position category-pos
5415 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5416 (push txt ee)
5417 (if org-agenda-todo-list-sublevels
5418 (goto-char todo-state-end-pos)
5419 (org-end-of-subtree 'invisible))))
5420 (nreverse ee)))
5422 (defun org-agenda-todo-custom-ignore-p (time n)
5423 "Check whether timestamp is farther away than n number of days.
5424 This function is invoked if `org-agenda-todo-ignore-deadlines',
5425 `org-agenda-todo-ignore-scheduled' or
5426 `org-agenda-todo-ignore-timestamp' is set to an integer."
5427 (let ((days (org-time-stamp-to-now
5428 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5429 (if (>= n 0)
5430 (>= days n)
5431 (<= days n))))
5433 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5434 (&optional end)
5435 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5436 (when (or org-agenda-todo-ignore-with-date
5437 org-agenda-todo-ignore-scheduled
5438 org-agenda-todo-ignore-deadlines
5439 org-agenda-todo-ignore-timestamp)
5440 (setq end (or end (save-excursion (outline-next-heading) (point))))
5441 (save-excursion
5442 (or (and org-agenda-todo-ignore-with-date
5443 (re-search-forward org-ts-regexp end t))
5444 (and org-agenda-todo-ignore-scheduled
5445 (re-search-forward org-scheduled-time-regexp end t)
5446 (cond
5447 ((eq org-agenda-todo-ignore-scheduled 'future)
5448 (> (org-time-stamp-to-now
5449 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5450 ((eq org-agenda-todo-ignore-scheduled 'past)
5451 (<= (org-time-stamp-to-now
5452 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5453 ((numberp org-agenda-todo-ignore-scheduled)
5454 (org-agenda-todo-custom-ignore-p
5455 (match-string 1) org-agenda-todo-ignore-scheduled))
5456 (t)))
5457 (and org-agenda-todo-ignore-deadlines
5458 (re-search-forward org-deadline-time-regexp end t)
5459 (cond
5460 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5461 ((eq org-agenda-todo-ignore-deadlines 'far)
5462 (not (org-deadline-close (match-string 1))))
5463 ((eq org-agenda-todo-ignore-deadlines 'future)
5464 (> (org-time-stamp-to-now
5465 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5466 ((eq org-agenda-todo-ignore-deadlines 'past)
5467 (<= (org-time-stamp-to-now
5468 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5469 ((numberp org-agenda-todo-ignore-deadlines)
5470 (org-agenda-todo-custom-ignore-p
5471 (match-string 1) org-agenda-todo-ignore-deadlines))
5472 (t (org-deadline-close (match-string 1)))))
5473 (and org-agenda-todo-ignore-timestamp
5474 (let ((buffer (current-buffer))
5475 (regexp
5476 (concat
5477 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5478 (start (point)))
5479 ;; Copy current buffer into a temporary one
5480 (with-temp-buffer
5481 (insert-buffer-substring buffer start end)
5482 (goto-char (point-min))
5483 ;; Delete SCHEDULED and DEADLINE items
5484 (while (re-search-forward regexp end t)
5485 (delete-region (match-beginning 0) (match-end 0)))
5486 (goto-char (point-min))
5487 ;; No search for timestamp left
5488 (when (re-search-forward org-ts-regexp nil t)
5489 (cond
5490 ((eq org-agenda-todo-ignore-timestamp 'future)
5491 (> (org-time-stamp-to-now
5492 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5493 ((eq org-agenda-todo-ignore-timestamp 'past)
5494 (<= (org-time-stamp-to-now
5495 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5496 ((numberp org-agenda-todo-ignore-timestamp)
5497 (org-agenda-todo-custom-ignore-p
5498 (match-string 1) org-agenda-todo-ignore-timestamp))
5499 (t))))))))))
5501 (defun org-agenda-get-timestamps (&optional deadline-results)
5502 "Return the date stamp information for agenda display."
5503 (let* ((props (list 'face 'org-agenda-calendar-event
5504 'org-not-done-regexp org-not-done-regexp
5505 'org-todo-regexp org-todo-regexp
5506 'org-complex-heading-regexp org-complex-heading-regexp
5507 'mouse-face 'highlight
5508 'help-echo
5509 (format "mouse-2 or RET jump to org file %s"
5510 (abbreviate-file-name buffer-file-name))))
5511 (d1 (calendar-absolute-from-gregorian date))
5513 (deadline-position-alist
5514 (mapcar (lambda (a) (and (setq mm (get-text-property
5515 0 'org-hd-marker a))
5516 (cons (marker-position mm) a)))
5517 deadline-results))
5518 (remove-re org-ts-regexp)
5519 (regexp
5520 (concat
5521 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5522 (regexp-quote
5523 (substring
5524 (format-time-string
5525 (car org-time-stamp-formats)
5526 (apply 'encode-time ; DATE bound by calendar
5527 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5528 1 11))
5529 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5530 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5531 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5532 donep tmp priority category category-pos level ee txt timestr tags
5533 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5534 inherited-tags ts-date)
5535 (goto-char (point-min))
5536 (while (setq end-of-match (re-search-forward regexp nil t))
5537 (setq b0 (match-beginning 0)
5538 b3 (match-beginning 3) e3 (match-end 3)
5539 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5540 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5541 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5542 (member todo-state
5543 org-agenda-repeating-timestamp-show-all)))
5544 (catch :skip
5545 (and (org-at-date-range-p) (throw :skip nil))
5546 (org-agenda-skip)
5547 (if (and (match-end 1)
5548 (not (= d1 (org-time-string-to-absolute
5549 (match-string 1) d1 nil show-all
5550 (current-buffer) b0))))
5551 (throw :skip nil))
5552 (if (and e3
5553 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5554 (throw :skip nil))
5555 (setq tmp (buffer-substring (max (point-min)
5556 (- b0 org-ds-keyword-length))
5558 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5559 inactivep (= (char-after b0) ?\[)
5560 deadlinep (string-match org-deadline-regexp tmp)
5561 scheduledp (string-match org-scheduled-regexp tmp)
5562 closedp (and org-agenda-include-inactive-timestamps
5563 (string-match org-closed-string tmp))
5564 clockp (and org-agenda-include-inactive-timestamps
5565 (or (string-match org-clock-string tmp)
5566 (string-match "]-+\\'" tmp)))
5567 warntime (get-text-property (point) 'org-appt-warntime)
5568 donep (member todo-state org-done-keywords))
5569 (if (or scheduledp deadlinep closedp clockp
5570 (and donep org-agenda-skip-timestamp-if-done))
5571 (throw :skip t))
5572 (if (string-match ">" timestr)
5573 ;; substring should only run to end of time stamp
5574 (setq timestr (substring timestr 0 (match-end 0))))
5575 (setq marker (org-agenda-new-marker b0)
5576 category (org-get-category b0)
5577 category-pos (get-text-property b0 'org-category-position))
5578 (save-excursion
5579 (if (not (re-search-backward org-outline-regexp-bol nil t))
5580 (throw :skip nil)
5581 (goto-char (match-beginning 0))
5582 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5583 (assoc (point) deadline-position-alist))
5584 (throw :skip nil))
5585 (setq hdmarker (org-agenda-new-marker)
5586 inherited-tags
5587 (or (eq org-agenda-show-inherited-tags 'always)
5588 (and (listp org-agenda-show-inherited-tags)
5589 (memq 'agenda org-agenda-show-inherited-tags))
5590 (and (eq org-agenda-show-inherited-tags t)
5591 (or (eq org-agenda-use-tag-inheritance t)
5592 (memq 'agenda org-agenda-use-tag-inheritance))))
5593 tags (org-get-tags-at nil (not inherited-tags))
5594 level (make-string (org-reduced-level (org-outline-level)) ? ))
5595 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5596 (setq head (or (match-string 1) ""))
5597 (setq txt (org-agenda-format-item
5598 (if inactivep org-agenda-inactive-leader nil)
5599 head level category tags timestr
5600 remove-re habitp)))
5601 (setq priority (org-get-priority txt))
5602 (org-add-props txt props 'priority priority
5603 'org-marker marker 'org-hd-marker hdmarker
5604 'org-category category 'date date
5605 'level level
5606 'ts-date
5607 (ignore-errors (org-time-string-to-absolute timestr))
5608 'org-category-position category-pos
5609 'todo-state todo-state
5610 'warntime warntime
5611 'type "timestamp")
5612 (push txt ee))
5613 (if org-agenda-skip-additional-timestamps-same-entry
5614 (outline-next-heading)
5615 (goto-char end-of-match))))
5616 (nreverse ee)))
5618 (defun org-agenda-get-sexps ()
5619 "Return the sexp information for agenda display."
5620 (require 'diary-lib)
5621 (let* ((props (list 'face 'org-agenda-calendar-sexp
5622 'mouse-face 'highlight
5623 'help-echo
5624 (format "mouse-2 or RET jump to org file %s"
5625 (abbreviate-file-name buffer-file-name))))
5626 (regexp "^&?%%(")
5627 marker category extra category-pos level ee txt tags entry
5628 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5629 (goto-char (point-min))
5630 (while (re-search-forward regexp nil t)
5631 (catch :skip
5632 (org-agenda-skip)
5633 (setq beg (match-beginning 0))
5634 (goto-char (1- (match-end 0)))
5635 (setq b (point))
5636 (forward-sexp 1)
5637 (setq sexp (buffer-substring b (point)))
5638 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5639 (org-trim (match-string 1))
5640 ""))
5641 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5642 (when result
5643 (setq marker (org-agenda-new-marker beg)
5644 level (make-string (org-reduced-level (org-outline-level)) ? )
5645 category (org-get-category beg)
5646 category-pos (get-text-property beg 'org-category-position)
5647 inherited-tags
5648 (or (eq org-agenda-show-inherited-tags 'always)
5649 (and (listp org-agenda-show-inherited-tags)
5650 (memq 'agenda org-agenda-show-inherited-tags))
5651 (and (eq org-agenda-show-inherited-tags t)
5652 (or (eq org-agenda-use-tag-inheritance t)
5653 (memq 'agenda org-agenda-use-tag-inheritance))))
5654 tags (org-get-tags-at nil (not inherited-tags))
5655 todo-state (org-get-todo-state)
5656 warntime (get-text-property (point) 'org-appt-warntime)
5657 extra nil)
5659 (dolist (r (if (stringp result)
5660 (list result)
5661 result)) ;; we expect a list here
5662 (when (and org-agenda-diary-sexp-prefix
5663 (string-match org-agenda-diary-sexp-prefix r))
5664 (setq extra (match-string 0 r)
5665 r (replace-match "" nil nil r)))
5666 (if (string-match "\\S-" r)
5667 (setq txt r)
5668 (setq txt "SEXP entry returned empty string"))
5669 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5670 (org-add-props txt props 'org-marker marker
5671 'org-category category 'date date 'todo-state todo-state
5672 'org-category-position category-pos 'tags tags
5673 'level level
5674 'type "sexp" 'warntime warntime)
5675 (push txt ee)))))
5676 (nreverse ee)))
5678 ;; Calendar sanity: define some functions that are independent of
5679 ;; `calendar-date-style'.
5680 ;; Normally I would like to use ISO format when calling the diary functions,
5681 ;; but to make sure we still have Emacs 22 compatibility we bind
5682 ;; also `european-calendar-style' and use european format
5683 (defun org-anniversary (year month day &optional mark)
5684 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5685 (org-no-warnings
5686 (let ((calendar-date-style 'european) (european-calendar-style t))
5687 (diary-anniversary day month year mark))))
5688 (defun org-cyclic (N year month day &optional mark)
5689 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5690 (org-no-warnings
5691 (let ((calendar-date-style 'european) (european-calendar-style t))
5692 (diary-cyclic N day month year mark))))
5693 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5694 "Like `diary-block', but with fixed (ISO) order of arguments."
5695 (org-no-warnings
5696 (let ((calendar-date-style 'european) (european-calendar-style t))
5697 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5698 (defun org-date (year month day &optional mark)
5699 "Like `diary-date', but with fixed (ISO) order of arguments."
5700 (org-no-warnings
5701 (let ((calendar-date-style 'european) (european-calendar-style t))
5702 (diary-date day month year mark))))
5704 ;; Define the` org-class' function
5705 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5706 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5707 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5708 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5709 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5710 `holidays', then any date that is known by the Emacs calendar to be a
5711 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5712 then those holidays will be skipped."
5713 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5714 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5715 (d (calendar-absolute-from-gregorian date))
5716 (h (when skip-weeks (calendar-check-holidays date))))
5717 (and
5718 (<= date1 d)
5719 (<= d date2)
5720 (= (calendar-day-of-week date) dayname)
5721 (or (not skip-weeks)
5722 (progn
5723 (require 'cal-iso)
5724 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5725 (not (or (and h (memq 'holidays skip-weeks))
5726 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5727 entry)))
5729 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5730 "Like `org-class', but honor `calendar-date-style'.
5731 The order of the first 2 times 3 arguments depends on the variable
5732 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5733 So for American calendars, give this as MONTH DAY YEAR, for European as
5734 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5735 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5736 is any number of ISO weeks in the block period for which the item should
5737 be skipped.
5739 This function is here only for backward compatibility and it is deprecated,
5740 please use `org-class' instead."
5741 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5742 (date2 (org-order-calendar-date-args m2 d2 y2)))
5743 (org-class
5744 (nth 2 date1) (car date1) (nth 1 date1)
5745 (nth 2 date2) (car date2) (nth 1 date2)
5746 dayname skip-weeks)))
5747 (make-obsolete 'org-diary-class 'org-class "")
5749 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5750 (defalias 'org-get-closed 'org-agenda-get-progress)
5751 (defun org-agenda-get-progress ()
5752 "Return the logged TODO entries for agenda display."
5753 (let* ((props (list 'mouse-face 'highlight
5754 'org-not-done-regexp org-not-done-regexp
5755 'org-todo-regexp org-todo-regexp
5756 'org-complex-heading-regexp org-complex-heading-regexp
5757 'help-echo
5758 (format "mouse-2 or RET jump to org file %s"
5759 (abbreviate-file-name buffer-file-name))))
5760 (items (if (consp org-agenda-show-log-scoped)
5761 org-agenda-show-log-scoped
5762 (if (eq org-agenda-show-log-scoped 'clockcheck)
5763 '(clock)
5764 org-agenda-log-mode-items)))
5765 (parts
5766 (delq nil
5767 (list
5768 (if (memq 'closed items) (concat "\\<" org-closed-string))
5769 (if (memq 'clock items) (concat "\\<" org-clock-string))
5770 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5771 (parts-re (if parts (mapconcat 'identity parts "\\|")
5772 (error "`org-agenda-log-mode-items' is empty")))
5773 (regexp (concat
5774 "\\(" parts-re "\\)"
5775 " *\\["
5776 (regexp-quote
5777 (substring
5778 (format-time-string
5779 (car org-time-stamp-formats)
5780 (apply 'encode-time ; DATE bound by calendar
5781 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5782 1 11))))
5783 (org-agenda-search-headline-for-time nil)
5784 marker hdmarker priority category category-pos level tags closedp
5785 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5786 (goto-char (point-min))
5787 (while (re-search-forward regexp nil t)
5788 (catch :skip
5789 (org-agenda-skip)
5790 (setq marker (org-agenda-new-marker (match-beginning 0))
5791 closedp (equal (match-string 1) org-closed-string)
5792 statep (equal (string-to-char (match-string 1)) ?-)
5793 clockp (not (or closedp statep))
5794 state (and statep (match-string 2))
5795 category (org-get-category (match-beginning 0))
5796 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5797 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5798 (when (string-match "\\]" timestr)
5799 ;; substring should only run to end of time stamp
5800 (setq rest (substring timestr (match-end 0))
5801 timestr (substring timestr 0 (match-end 0)))
5802 (if (and (not closedp) (not statep)
5803 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5804 rest))
5805 (progn (setq timestr (concat (substring timestr 0 -1)
5806 "-" (match-string 1 rest) "]"))
5807 (setq clocked (match-string 2 rest)))
5808 (setq clocked "-")))
5809 (save-excursion
5810 (setq extra
5811 (cond
5812 ((not org-agenda-log-mode-add-notes) nil)
5813 (statep
5814 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5815 (match-string 1)))
5816 (clockp
5817 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5818 (match-string 1)))))
5819 (if (not (re-search-backward org-outline-regexp-bol nil t))
5820 (throw :skip nil)
5821 (goto-char (match-beginning 0))
5822 (setq hdmarker (org-agenda-new-marker)
5823 inherited-tags
5824 (or (eq org-agenda-show-inherited-tags 'always)
5825 (and (listp org-agenda-show-inherited-tags)
5826 (memq 'todo org-agenda-show-inherited-tags))
5827 (and (eq org-agenda-show-inherited-tags t)
5828 (or (eq org-agenda-use-tag-inheritance t)
5829 (memq 'todo org-agenda-use-tag-inheritance))))
5830 tags (org-get-tags-at nil (not inherited-tags))
5831 level (make-string (org-reduced-level (org-outline-level)) ? ))
5832 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5833 (setq txt (match-string 1))
5834 (when extra
5835 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5836 (setq txt (concat (substring txt 0 (match-beginning 1))
5837 " - " extra " " (match-string 2 txt)))
5838 (setq txt (concat txt " - " extra))))
5839 (setq txt (org-agenda-format-item
5840 (cond
5841 (closedp "Closed: ")
5842 (statep (concat "State: (" state ")"))
5843 (t (concat "Clocked: (" clocked ")")))
5844 txt level category tags timestr)))
5845 (setq priority 100000)
5846 (org-add-props txt props
5847 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5848 'priority priority 'org-category category
5849 'org-category-position category-pos
5850 'level level
5851 'type "closed" 'date date
5852 'undone-face 'org-warning 'done-face 'org-agenda-done)
5853 (push txt ee))
5854 (goto-char (point-at-eol))))
5855 (nreverse ee)))
5857 (defun org-agenda-show-clocking-issues ()
5858 "Add overlays, showing issues with clocking.
5859 See also the user option `org-agenda-clock-consistency-checks'."
5860 (interactive)
5861 (let* ((org-time-clocksum-use-effort-durations nil)
5862 (pl org-agenda-clock-consistency-checks)
5863 (re (concat "^[ \t]*"
5864 org-clock-string
5865 "[ \t]+"
5866 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5867 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5868 (tlstart 0.)
5869 (tlend 0.)
5870 (maxtime (org-hh:mm-string-to-minutes
5871 (or (plist-get pl :max-duration) "24:00")))
5872 (mintime (org-hh:mm-string-to-minutes
5873 (or (plist-get pl :min-duration) 0)))
5874 (maxgap (org-hh:mm-string-to-minutes
5875 ;; default 30:00 means never complain
5876 (or (plist-get pl :max-gap) "30:00")))
5877 (gapok (mapcar 'org-hh:mm-string-to-minutes
5878 (plist-get pl :gap-ok-around)))
5879 (def-face (or (plist-get pl :default-face)
5880 '((:background "DarkRed") (:foreground "white"))))
5881 issue face m te ts dt ov)
5882 (goto-char (point-min))
5883 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5884 (setq issue nil face def-face)
5885 (catch 'next
5886 (setq m (org-get-at-bol 'org-marker)
5887 te nil ts nil)
5888 (unless (and m (markerp m))
5889 (setq issue "No valid clock line") (throw 'next t))
5890 (org-with-point-at m
5891 (save-excursion
5892 (goto-char (point-at-bol))
5893 (unless (looking-at re)
5894 (error "No valid Clock line")
5895 (throw 'next t))
5896 (unless (match-end 3)
5897 (setq issue "No end time"
5898 face (or (plist-get pl :no-end-time-face) face))
5899 (throw 'next t))
5900 (setq ts (match-string 1)
5901 te (match-string 3)
5902 ts (org-float-time
5903 (apply 'encode-time (org-parse-time-string ts)))
5904 te (org-float-time
5905 (apply 'encode-time (org-parse-time-string te)))
5906 dt (- te ts))))
5907 (cond
5908 ((> dt (* 60 maxtime))
5909 ;; a very long clocking chunk
5910 (setq issue (format "Clocking interval is very long: %s"
5911 (org-minutes-to-clocksum-string
5912 (floor (/ (float dt) 60.))))
5913 face (or (plist-get pl :long-face) face)))
5914 ((< dt (* 60 mintime))
5915 ;; a very short clocking chunk
5916 (setq issue (format "Clocking interval is very short: %s"
5917 (org-minutes-to-clocksum-string
5918 (floor (/ (float dt) 60.))))
5919 face (or (plist-get pl :short-face) face)))
5920 ((and (> tlend 0) (< ts tlend))
5921 ;; Two clock entries are overlapping
5922 (setq issue (format "Clocking overlap: %d minutes"
5923 (/ (- tlend ts) 60))
5924 face (or (plist-get pl :overlap-face) face)))
5925 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5926 ;; There is a gap, lets see if we need to report it
5927 (unless (org-agenda-check-clock-gap tlend ts gapok)
5928 (setq issue (format "Clocking gap: %d minutes"
5929 (/ (- ts tlend) 60))
5930 face (or (plist-get pl :gap-face) face))))
5931 (t nil)))
5932 (setq tlend (or te tlend) tlstart (or ts tlstart))
5933 (when issue
5934 ;; OK, there was some issue, add an overlay to show the issue
5935 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5936 (overlay-put ov 'before-string
5937 (concat
5938 (org-add-props
5939 (format "%-43s" (concat " " issue))
5941 'face face)
5942 "\n"))
5943 (overlay-put ov 'evaporate t)))))
5945 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5946 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5947 (catch 'exit
5948 (unless ok-list
5949 ;; there are no OK times for gaps...
5950 (throw 'exit nil))
5951 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5952 ;; This is more than 24 hours, so it is OK.
5953 ;; because we have at least one OK time, that must be in the
5954 ;; 24 hour interval.
5955 (throw 'exit t))
5956 ;; We have a shorter gap.
5957 ;; Now we have to get the minute of the day when these times are
5958 (let* ((t1dec (decode-time (seconds-to-time t1)))
5959 (t2dec (decode-time (seconds-to-time t2)))
5960 ;; compute the minute on the day
5961 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5962 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5963 (when (< min2 min1)
5964 ;; if min2 is smaller than min1, this means it is on the next day.
5965 ;; Wrap it to after midnight.
5966 (setq min2 (+ min2 1440)))
5967 ;; Now check if any of the OK times is in the gap
5968 (mapc (lambda (x)
5969 ;; Wrap the time to after midnight if necessary
5970 (if (< x min1) (setq x (+ x 1440)))
5971 ;; Check if in interval
5972 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5973 ok-list)
5974 ;; Nope, this gap is not OK
5975 nil)))
5977 (defun org-agenda-get-deadlines (&optional with-hour)
5978 "Return the deadline information for agenda display.
5979 When WITH-HOUR is non-nil, only return deadlines with an hour
5980 specification like [h]h:mm."
5981 (let* ((props (list 'mouse-face 'highlight
5982 'org-not-done-regexp org-not-done-regexp
5983 'org-todo-regexp org-todo-regexp
5984 'org-complex-heading-regexp org-complex-heading-regexp
5985 'help-echo
5986 (format "mouse-2 or RET jump to org file %s"
5987 (abbreviate-file-name buffer-file-name))))
5988 (regexp (if with-hour
5989 org-deadline-time-hour-regexp
5990 org-deadline-time-regexp))
5991 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5992 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5993 (dl0 (car org-agenda-deadline-leaders))
5994 (dl1 (nth 1 org-agenda-deadline-leaders))
5995 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
5996 d2 diff dfrac wdays pos pos1 category category-pos level
5997 tags suppress-prewarning ee txt head face s todo-state
5998 show-all upcomingp donep timestr warntime inherited-tags ts-date)
5999 (goto-char (point-min))
6000 (while (re-search-forward regexp nil t)
6001 (catch :skip
6002 (org-agenda-skip)
6003 (setq s (match-string 1)
6004 txt nil
6005 pos (1- (match-beginning 1))
6006 todo-state (save-match-data (org-get-todo-state))
6007 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6008 (member todo-state
6009 org-agenda-repeating-timestamp-show-all))
6010 d2 (org-time-string-to-absolute
6011 s d1 'past show-all (current-buffer) pos)
6012 diff (- d2 d1))
6013 (setq suppress-prewarning
6014 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6015 (let ((item (buffer-substring (point-at-bol)
6016 (point-at-eol))))
6017 (save-match-data
6018 (and (string-match
6019 org-scheduled-time-regexp item)
6020 (match-string 1 item)))))))
6021 (cond
6022 ((not ds) nil)
6023 ;; The current item has a scheduled date (in ds), so
6024 ;; evaluate its prewarning lead time.
6025 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6026 ;; Use global prewarning-restart lead time.
6027 org-agenda-skip-deadline-prewarning-if-scheduled)
6028 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6029 'pre-scheduled)
6030 ;; Set prewarning to no earlier than scheduled.
6031 (min (- d2 (org-time-string-to-absolute
6032 ds d1 'past show-all (current-buffer) pos))
6033 org-deadline-warning-days))
6034 ;; Set prewarning to deadline.
6035 (t 0))))
6036 (setq wdays (if suppress-prewarning
6037 (let ((org-deadline-warning-days suppress-prewarning))
6038 (org-get-wdays s))
6039 (org-get-wdays s))
6040 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6041 upcomingp (and todayp (> diff 0)))
6042 ;; When to show a deadline in the calendar:
6043 ;; If the expiration is within wdays warning time.
6044 ;; Past-due deadlines are only shown on the current date
6045 (if (and (or (and (<= diff wdays)
6046 (and todayp (not org-agenda-only-exact-dates)))
6047 (= diff 0)))
6048 (save-excursion
6049 ;; (setq todo-state (org-get-todo-state))
6050 (setq donep (member todo-state org-done-keywords))
6051 (if (and donep
6052 (or org-agenda-skip-deadline-if-done
6053 (not (= diff 0))))
6054 (setq txt nil)
6055 (setq category (org-get-category)
6056 warntime (get-text-property (point) 'org-appt-warntime)
6057 category-pos (get-text-property (point) 'org-category-position))
6058 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6059 (throw :skip nil)
6060 (goto-char (match-end 0))
6061 (setq pos1 (match-beginning 0))
6062 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6063 (setq inherited-tags
6064 (or (eq org-agenda-show-inherited-tags 'always)
6065 (and (listp org-agenda-show-inherited-tags)
6066 (memq 'agenda org-agenda-show-inherited-tags))
6067 (and (eq org-agenda-show-inherited-tags t)
6068 (or (eq org-agenda-use-tag-inheritance t)
6069 (memq 'agenda org-agenda-use-tag-inheritance))))
6070 tags (org-get-tags-at pos1 (not inherited-tags)))
6071 (setq head (buffer-substring
6072 (point)
6073 (progn (skip-chars-forward "^\r\n")
6074 (point))))
6075 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6076 (setq timestr
6077 (concat (substring s (match-beginning 1)) " "))
6078 (setq timestr 'time))
6079 (setq txt (org-agenda-format-item
6080 (cond ((= diff 0) dl0)
6081 ((> diff 0)
6082 (if (functionp dl1)
6083 (funcall dl1 diff date)
6084 (format dl1 diff)))
6086 (if (functionp dl2)
6087 (funcall dl2 diff date)
6088 (format dl2 (if (string= dl2 dl1)
6089 diff (abs diff))))))
6090 head level category tags
6091 (if (not (= diff 0)) nil timestr)))))
6092 (when txt
6093 (setq face (org-agenda-deadline-face dfrac))
6094 (org-add-props txt props
6095 'org-marker (org-agenda-new-marker pos)
6096 'warntime warntime
6097 'level level
6098 'ts-date d2
6099 'org-hd-marker (org-agenda-new-marker pos1)
6100 'priority (+ (- diff)
6101 (org-get-priority txt))
6102 'org-category category
6103 'org-category-position category-pos
6104 'todo-state todo-state
6105 'type (if upcomingp "upcoming-deadline" "deadline")
6106 'date (if upcomingp date d2)
6107 'face (if donep 'org-agenda-done face)
6108 'undone-face face 'done-face 'org-agenda-done)
6109 (push txt ee))))))
6110 (nreverse ee)))
6112 (defun org-agenda-deadline-face (fraction)
6113 "Return the face to displaying a deadline item.
6114 FRACTION is what fraction of the head-warning time has passed."
6115 (let ((faces org-agenda-deadline-faces) f)
6116 (catch 'exit
6117 (while (setq f (pop faces))
6118 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6120 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6121 "Return the scheduled information for agenda display.
6122 When WITH-HOUR is non-nil, only return scheduled items with
6123 an hour specification like [h]h:mm."
6124 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6125 'org-todo-regexp org-todo-regexp
6126 'org-complex-heading-regexp org-complex-heading-regexp
6127 'done-face 'org-agenda-done
6128 'mouse-face 'highlight
6129 'help-echo
6130 (format "mouse-2 or RET jump to org file %s"
6131 (abbreviate-file-name buffer-file-name))))
6132 (regexp (if with-hour
6133 org-scheduled-time-hour-regexp
6134 org-scheduled-time-regexp))
6135 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6136 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6138 (deadline-position-alist
6139 (mapcar (lambda (a) (and (setq mm (get-text-property
6140 0 'org-hd-marker a))
6141 (cons (marker-position mm) a)))
6142 deadline-results))
6143 d2 diff pos pos1 category category-pos level tags donep
6144 ee txt head pastschedp todo-state face timestr s habitp show-all
6145 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6146 ddays)
6147 (goto-char (point-min))
6148 (while (re-search-forward regexp nil t)
6149 (catch :skip
6150 (org-agenda-skip)
6151 (setq s (match-string 1)
6152 txt nil
6153 pos (1- (match-beginning 1))
6154 todo-state (save-match-data (org-get-todo-state))
6155 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6156 (member todo-state
6157 org-agenda-repeating-timestamp-show-all))
6158 d2 (org-time-string-to-absolute
6159 s d1 'past show-all (current-buffer) pos)
6160 diff (- d2 d1)
6161 warntime (get-text-property (point) 'org-appt-warntime))
6162 (setq pastschedp (and todayp (< diff 0)))
6163 (setq did-habit-check-p nil)
6164 (setq suppress-delay
6165 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6166 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6167 (save-match-data
6168 (and (string-match
6169 org-deadline-time-regexp item)
6170 (match-string 1 item)))))))
6171 (cond
6172 ((not ds) nil)
6173 ;; The current item has a deadline date (in ds), so
6174 ;; evaluate its delay time.
6175 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6176 ;; Use global delay time.
6177 (- org-agenda-skip-scheduled-delay-if-deadline))
6178 ((eq org-agenda-skip-scheduled-delay-if-deadline
6179 'post-deadline)
6180 ;; Set delay to no later than deadline.
6181 (min (- d2 (org-time-string-to-absolute
6182 ds d1 'past show-all (current-buffer) pos))
6183 org-scheduled-delay-days))
6184 (t 0))))
6185 (setq ddays (if suppress-delay
6186 (let ((org-scheduled-delay-days suppress-delay))
6187 (org-get-wdays s t t))
6188 (org-get-wdays s t)))
6189 ;; Use a delay of 0 when there is a repeater and the delay is
6190 ;; of the form --3d
6191 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6192 (< (org-time-string-to-absolute s)
6193 (org-time-string-to-absolute
6194 s d2 'past nil (current-buffer) pos)))
6195 (setq ddays 0))
6196 ;; When to show a scheduled item in the calendar:
6197 ;; If it is on or past the date.
6198 (when (or (and (> ddays 0) (= diff (- ddays)))
6199 (and (zerop ddays) (= diff 0))
6200 (and (< (+ diff ddays) 0)
6201 (< (abs diff) org-scheduled-past-days)
6202 (and todayp (not org-agenda-only-exact-dates)))
6203 ;; org-is-habit-p uses org-entry-get, which is expansive
6204 ;; so we go extra mile to only call it once
6205 (and todayp
6206 (boundp 'org-habit-show-all-today)
6207 org-habit-show-all-today
6208 (setq did-habit-check-p t)
6209 (setq habitp (and (functionp 'org-is-habit-p)
6210 (org-is-habit-p)))))
6211 (save-excursion
6212 (setq donep (member todo-state org-done-keywords))
6213 (if (and donep
6214 (or org-agenda-skip-scheduled-if-done
6215 (not (= diff 0))
6216 (and (functionp 'org-is-habit-p)
6217 (org-is-habit-p))))
6218 (setq txt nil)
6219 (setq habitp (if did-habit-check-p habitp
6220 (and (functionp 'org-is-habit-p)
6221 (org-is-habit-p))))
6222 (setq category (org-get-category)
6223 category-pos (get-text-property (point) 'org-category-position))
6224 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6225 'repeated-after-deadline)
6226 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6227 (throw :skip nil))
6228 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6229 (throw :skip nil)
6230 (goto-char (match-end 0))
6231 (setq pos1 (match-beginning 0))
6232 (if habitp
6233 (if (or (not org-habit-show-habits)
6234 (and (not todayp)
6235 (boundp 'org-habit-show-habits-only-for-today)
6236 org-habit-show-habits-only-for-today))
6237 (throw :skip nil))
6238 (if (and
6239 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6240 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6241 pastschedp))
6242 (setq mm (assoc pos1 deadline-position-alist)))
6243 (throw :skip nil)))
6244 (setq inherited-tags
6245 (or (eq org-agenda-show-inherited-tags 'always)
6246 (and (listp org-agenda-show-inherited-tags)
6247 (memq 'agenda org-agenda-show-inherited-tags))
6248 (and (eq org-agenda-show-inherited-tags t)
6249 (or (eq org-agenda-use-tag-inheritance t)
6250 (memq 'agenda org-agenda-use-tag-inheritance))))
6252 tags (org-get-tags-at nil (not inherited-tags)))
6253 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6254 (setq head (buffer-substring
6255 (point)
6256 (progn (skip-chars-forward "^\r\n") (point))))
6257 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6258 (setq timestr
6259 (concat (substring s (match-beginning 1)) " "))
6260 (setq timestr 'time))
6261 (setq txt (org-agenda-format-item
6262 (if (= diff 0)
6263 (car org-agenda-scheduled-leaders)
6264 (format (nth 1 org-agenda-scheduled-leaders)
6265 (- 1 diff)))
6266 head level category tags
6267 (if (not (= diff 0)) nil timestr)
6268 nil habitp))))
6269 (when txt
6270 (setq face
6271 (cond
6272 ((and (not habitp) pastschedp)
6273 'org-scheduled-previously)
6274 (todayp 'org-scheduled-today)
6275 (t 'org-scheduled))
6276 habitp (and habitp (org-habit-parse-todo)))
6277 (org-add-props txt props
6278 'undone-face face
6279 'face (if donep 'org-agenda-done face)
6280 'org-marker (org-agenda-new-marker pos)
6281 'org-hd-marker (org-agenda-new-marker pos1)
6282 'type (if pastschedp "past-scheduled" "scheduled")
6283 'date (if pastschedp d2 date)
6284 'ts-date d2
6285 'warntime warntime
6286 'level level
6287 'priority (if habitp
6288 (org-habit-get-priority habitp)
6289 (+ 94 (- 5 diff) (org-get-priority txt)))
6290 'org-category category
6291 'category-position category-pos
6292 'org-habit-p habitp
6293 'todo-state todo-state)
6294 (push txt ee))))))
6295 (nreverse ee)))
6297 (defun org-agenda-get-blocks ()
6298 "Return the date-range information for agenda display."
6299 (let* ((props (list 'face nil
6300 'org-not-done-regexp org-not-done-regexp
6301 'org-todo-regexp org-todo-regexp
6302 'org-complex-heading-regexp org-complex-heading-regexp
6303 'mouse-face 'highlight
6304 'help-echo
6305 (format "mouse-2 or RET jump to org file %s"
6306 (abbreviate-file-name buffer-file-name))))
6307 (regexp org-tr-regexp)
6308 (d0 (calendar-absolute-from-gregorian date))
6309 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6310 level todo-state tags pos head donep inherited-tags)
6311 (goto-char (point-min))
6312 (while (re-search-forward regexp nil t)
6313 (catch :skip
6314 (org-agenda-skip)
6315 (setq pos (point))
6316 (let ((start-time (match-string 1))
6317 (end-time (match-string 2)))
6318 (setq s1 (match-string 1)
6319 s2 (match-string 2)
6320 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6321 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6322 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6323 ;; Only allow days between the limits, because the normal
6324 ;; date stamps will catch the limits.
6325 (save-excursion
6326 (setq todo-state (org-get-todo-state))
6327 (setq donep (member todo-state org-done-keywords))
6328 (if (and donep org-agenda-skip-timestamp-if-done)
6329 (throw :skip t))
6330 (setq marker (org-agenda-new-marker (point)))
6331 (setq category (org-get-category)
6332 category-pos (get-text-property (point) 'org-category-position))
6333 (if (not (re-search-backward org-outline-regexp-bol nil t))
6334 (throw :skip nil)
6335 (goto-char (match-beginning 0))
6336 (setq hdmarker (org-agenda-new-marker (point))
6337 inherited-tags
6338 (or (eq org-agenda-show-inherited-tags 'always)
6339 (and (listp org-agenda-show-inherited-tags)
6340 (memq 'agenda org-agenda-show-inherited-tags))
6341 (and (eq org-agenda-show-inherited-tags t)
6342 (or (eq org-agenda-use-tag-inheritance t)
6343 (memq 'agenda org-agenda-use-tag-inheritance))))
6345 tags (org-get-tags-at nil (not inherited-tags)))
6346 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6347 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6348 (setq head (match-string 1))
6349 (let ((remove-re
6350 (if org-agenda-remove-timeranges-from-blocks
6351 (concat
6352 "<" (regexp-quote s1) ".*?>"
6353 "--"
6354 "<" (regexp-quote s2) ".*?>")
6355 nil)))
6356 (setq txt (org-agenda-format-item
6357 (format
6358 (nth (if (= d1 d2) 0 1)
6359 org-agenda-timerange-leaders)
6360 (1+ (- d0 d1)) (1+ (- d2 d1)))
6361 head level category tags
6362 (cond ((and (= d1 d0) (= d2 d0))
6363 (concat "<" start-time ">--<" end-time ">"))
6364 ((= d1 d0)
6365 (concat "<" start-time ">"))
6366 ((= d2 d0)
6367 (concat "<" end-time ">")))
6368 remove-re))))
6369 (org-add-props txt props
6370 'org-marker marker 'org-hd-marker hdmarker
6371 'type "block" 'date date
6372 'level level
6373 'todo-state todo-state
6374 'priority (org-get-priority txt) 'org-category category
6375 'org-category-position category-pos)
6376 (push txt ee))))
6377 (goto-char pos)))
6378 ;; Sort the entries by expiration date.
6379 (nreverse ee)))
6381 ;;; Agenda presentation and sorting
6383 (defvar org-prefix-has-time nil
6384 "A flag, set by `org-compile-prefix-format'.
6385 The flag is set if the currently compiled format contains a `%t'.")
6386 (defvar org-prefix-has-tag nil
6387 "A flag, set by `org-compile-prefix-format'.
6388 The flag is set if the currently compiled format contains a `%T'.")
6389 (defvar org-prefix-has-effort nil
6390 "A flag, set by `org-compile-prefix-format'.
6391 The flag is set if the currently compiled format contains a `%e'.")
6392 (defvar org-prefix-has-breadcrumbs nil
6393 "A flag, set by `org-compile-prefix-format'.
6394 The flag is set if the currently compiled format contains a `%b'.")
6395 (defvar org-prefix-category-length nil
6396 "Used by `org-compile-prefix-format' to remember the category field width.")
6397 (defvar org-prefix-category-max-length nil
6398 "Used by `org-compile-prefix-format' to remember the category field width.")
6400 (defun org-agenda-get-category-icon (category)
6401 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6402 (dolist (entry org-agenda-category-icon-alist)
6403 (when (org-string-match-p (car entry) category)
6404 (if (listp (cadr entry))
6405 (return (cadr entry))
6406 (return (apply 'create-image (cdr entry)))))))
6408 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6409 remove-re habitp)
6410 "Format TXT to be inserted into the agenda buffer.
6411 In particular, add the prefix and corresponding text properties.
6413 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6414 LEVEL may be a string to replace the `%l' specifier.
6415 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6416 category taken from local variable or file name. It will replace the `%c'
6417 specifier in the format.
6418 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6419 TXT for sorting of this entry, and for the `%t' specifier in the format.
6420 When DOTIME is a string, this string is searched for a time before TXT is.
6421 TAGS can be the tags of the headline.
6422 Any match of REMOVE-RE will be removed from TXT."
6423 ;; We keep the org-prefix-* variable values along with a compiled
6424 ;; formatter, so that multiple agendas existing at the same time do
6425 ;; not step on each other toes.
6427 ;; It was inconvenient to make these variables buffer local in
6428 ;; Agenda buffers, because this function expects to be called with
6429 ;; the buffer where item comes from being current, and not agenda
6430 ;; buffer
6431 (let* ((bindings (car org-prefix-format-compiled))
6432 (formatter (cadr org-prefix-format-compiled)))
6433 (loop for (var value) in bindings
6434 do (set var value))
6435 (save-match-data
6436 ;; Diary entries sometimes have extra whitespace at the beginning
6437 (setq txt (org-trim txt))
6439 ;; Fix the tags part in txt
6440 (setq txt (org-agenda-fix-displayed-tags
6441 txt tags
6442 org-agenda-show-inherited-tags
6443 org-agenda-hide-tags-regexp))
6445 (let* ((category (or category
6446 (if (stringp org-category)
6447 org-category
6448 (and org-category (symbol-name org-category)))
6449 (if buffer-file-name
6450 (file-name-sans-extension
6451 (file-name-nondirectory buffer-file-name))
6452 "")))
6453 (category-icon (org-agenda-get-category-icon category))
6454 (category-icon (if category-icon
6455 (propertize " " 'display category-icon)
6456 ""))
6457 ;; time, tag, effort are needed for the eval of the prefix format
6458 (tag (if tags (nth (1- (length tags)) tags) ""))
6459 time effort neffort
6460 (ts (if dotime (concat
6461 (if (stringp dotime) dotime "")
6462 (and org-agenda-search-headline-for-time txt))))
6463 (time-of-day (and dotime (org-get-time-of-day ts)))
6464 stamp plain s0 s1 s2 rtn srp l
6465 duration thecategory breadcrumbs)
6466 (and (derived-mode-p 'org-mode) buffer-file-name
6467 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6468 (when (and dotime time-of-day)
6469 ;; Extract starting and ending time and move them to prefix
6470 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6471 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6472 (setq s0 (match-string 0 ts)
6473 srp (and stamp (match-end 3))
6474 s1 (match-string (if plain 1 2) ts)
6475 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6477 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6478 ;; them, we might want to remove them there to avoid duplication.
6479 ;; The user can turn this off with a variable.
6480 (if (and org-prefix-has-time
6481 org-agenda-remove-times-when-in-prefix (or stamp plain)
6482 (string-match (concat (regexp-quote s0) " *") txt)
6483 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6484 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6485 (= (match-beginning 0) 0)
6487 (setq txt (replace-match "" nil nil txt))))
6488 ;; Normalize the time(s) to 24 hour
6489 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6490 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6492 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6493 (let (org-time-clocksum-use-effort-durations)
6494 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6495 (setq s2
6496 (org-minutes-to-clocksum-string
6497 (+ (org-hh:mm-string-to-minutes s1)
6498 org-agenda-default-appointment-duration)))))
6500 ;; Compute the duration
6501 (when s2
6502 (setq duration (- (org-hh:mm-string-to-minutes s2)
6503 (org-hh:mm-string-to-minutes s1)))))
6505 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6506 txt)
6507 ;; Tags are in the string
6508 (if (or (eq org-agenda-remove-tags t)
6509 (and org-agenda-remove-tags
6510 org-prefix-has-tag))
6511 (setq txt (replace-match "" t t txt))
6512 (setq txt (replace-match
6513 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6514 (match-string 2 txt))
6515 t t txt))))
6516 (when (derived-mode-p 'org-mode)
6517 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6519 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6520 ;; current buffer, so move this check outside of above
6521 (if effort
6522 (setq neffort (org-duration-string-to-minutes effort)
6523 effort (setq effort (concat "[" effort "]")))
6524 ;; prevent erroring out with %e format when there is no effort
6525 (setq effort ""))
6527 (when remove-re
6528 (while (string-match remove-re txt)
6529 (setq txt (replace-match "" t t txt))))
6531 ;; Set org-heading property on `txt' to mark the start of the
6532 ;; heading.
6533 (add-text-properties 0 (length txt) '(org-heading t) txt)
6535 ;; Prepare the variables needed in the eval of the compiled format
6536 (if org-prefix-has-breadcrumbs
6537 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6538 (let ((s (org-display-outline-path nil nil "->" t)))
6539 (if (eq "" s) "" (concat s "->"))))))
6540 (setq time (cond (s2 (concat
6541 (org-agenda-time-of-day-to-ampm-maybe s1)
6542 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6543 (if org-agenda-timegrid-use-ampm " ")))
6544 (s1 (concat
6545 (org-agenda-time-of-day-to-ampm-maybe s1)
6546 (if org-agenda-timegrid-use-ampm
6547 "........ "
6548 "......")))
6549 (t ""))
6550 extra (or (and (not habitp) extra) "")
6551 category (if (symbolp category) (symbol-name category) category)
6552 thecategory (copy-sequence category)
6553 level (or level ""))
6554 (if (string-match org-bracket-link-regexp category)
6555 (progn
6556 (setq l (if (match-end 3)
6557 (- (match-end 3) (match-beginning 3))
6558 (- (match-end 1) (match-beginning 1))))
6559 (when (< l (or org-prefix-category-length 0))
6560 (setq category (copy-sequence category))
6561 (org-add-props category nil
6562 'extra-space (make-string
6563 (- org-prefix-category-length l 1) ?\ ))))
6564 (if (and org-prefix-category-max-length
6565 (>= (length category) org-prefix-category-max-length))
6566 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6567 ;; Evaluate the compiled format
6568 (setq rtn (concat (eval formatter) txt))
6570 ;; And finally add the text properties
6571 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6572 (org-add-props rtn nil
6573 'org-category (if thecategory (downcase thecategory) category)
6574 'tags (mapcar 'org-downcase-keep-props tags)
6575 'org-highest-priority org-highest-priority
6576 'org-lowest-priority org-lowest-priority
6577 'time-of-day time-of-day
6578 'duration duration
6579 'effort effort
6580 'effort-minutes neffort
6581 'breadcrumbs breadcrumbs
6582 'txt txt
6583 'level level
6584 'time time
6585 'extra extra
6586 'format org-prefix-format-compiled
6587 'dotime dotime)))))
6589 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6590 "Remove tags string from TXT, and add a modified list of tags.
6591 The modified list may contain inherited tags, and tags matched by
6592 `org-agenda-hide-tags-regexp' will be removed."
6593 (when (or add-inherited hide-re)
6594 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6595 (setq txt (substring txt 0 (match-beginning 0))))
6596 (setq tags
6597 (delq nil
6598 (mapcar (lambda (tg)
6599 (if (or (and hide-re (string-match hide-re tg))
6600 (and (not add-inherited)
6601 (get-text-property 0 'inherited tg)))
6603 tg))
6604 tags)))
6605 (when tags
6606 (let ((have-i (get-text-property 0 'inherited (car tags)))
6608 (setq txt (concat txt " :"
6609 (mapconcat
6610 (lambda (x)
6611 (setq i (get-text-property 0 'inherited x))
6612 (if (and have-i (not i))
6613 (progn
6614 (setq have-i nil)
6615 (concat ":" x))
6617 tags ":")
6618 (if have-i "::" ":"))))))
6619 txt)
6621 (defun org-downcase-keep-props (s)
6622 (let ((props (text-properties-at 0 s)))
6623 (setq s (downcase s))
6624 (add-text-properties 0 (length s) props s)
6627 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6629 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6630 "Add a time-grid for agenda items which need it.
6632 LIST is the list of agenda items formatted by `org-agenda-list'.
6633 NDAYS is the span of the current agenda view.
6634 TODAYP is `t' when the current agenda view is on today."
6635 (catch 'exit
6636 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6637 ((and todayp (member 'today (car org-agenda-time-grid))))
6638 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6639 ((member 'weekly (car org-agenda-time-grid)))
6640 (t (throw 'exit list)))
6641 (let* ((have (delq nil (mapcar
6642 (lambda (x) (get-text-property 1 'time-of-day x))
6643 list)))
6644 (string (nth 1 org-agenda-time-grid))
6645 (gridtimes (nth 2 org-agenda-time-grid))
6646 (req (car org-agenda-time-grid))
6647 (remove (member 'remove-match req))
6648 new time)
6649 (if (and (member 'require-timed req) (not have))
6650 ;; don't show empty grid
6651 (throw 'exit list))
6652 (while (setq time (pop gridtimes))
6653 (unless (and remove (member time have))
6654 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6655 (push (org-agenda-format-item
6656 nil string nil "" nil
6657 (concat (substring time 0 -2) ":" (substring time -2)))
6658 new)
6659 (put-text-property
6660 2 (length (car new)) 'face 'org-time-grid (car new))))
6661 (when (and todayp org-agenda-show-current-time-in-grid)
6662 (push (org-agenda-format-item
6663 nil org-agenda-current-time-string nil "" nil
6664 (format-time-string "%H:%M "))
6665 new)
6666 (put-text-property
6667 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6669 (if (member 'time-up org-agenda-sorting-strategy-selected)
6670 (append new list)
6671 (append list new)))))
6673 (defun org-compile-prefix-format (key)
6674 "Compile the prefix format into a Lisp form that can be evaluated.
6675 The resulting form and associated variable bindings is returned
6676 and stored in the variable `org-prefix-format-compiled'."
6677 (setq org-prefix-has-time nil
6678 org-prefix-has-tag nil
6679 org-prefix-category-length nil
6680 org-prefix-has-effort nil
6681 org-prefix-has-breadcrumbs nil)
6682 (let ((s (cond
6683 ((stringp org-agenda-prefix-format)
6684 org-agenda-prefix-format)
6685 ((assq key org-agenda-prefix-format)
6686 (cdr (assq key org-agenda-prefix-format)))
6687 (t " %-12:c%?-12t% s")))
6688 (start 0)
6689 varform vars var e c f opt)
6690 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6691 s start)
6692 (setq var (or (cdr (assoc (match-string 4 s)
6693 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6694 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6695 'eval)
6696 c (or (match-string 3 s) "")
6697 opt (match-beginning 1)
6698 start (1+ (match-beginning 0)))
6699 (if (equal var 'time) (setq org-prefix-has-time t))
6700 (if (equal var 'tag) (setq org-prefix-has-tag t))
6701 (if (equal var 'effort) (setq org-prefix-has-effort t))
6702 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6703 (setq f (concat "%" (match-string 2 s) "s"))
6704 (when (equal var 'category)
6705 (setq org-prefix-category-length
6706 (floor (abs (string-to-number (match-string 2 s)))))
6707 (setq org-prefix-category-max-length
6708 (let ((x (match-string 2 s)))
6709 (save-match-data
6710 (if (string-match "\\.[0-9]+" x)
6711 (string-to-number (substring (match-string 0 x) 1)))))))
6712 (if (eq var 'eval)
6713 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6714 (if opt
6715 (setq varform
6716 `(if (equal "" ,var)
6718 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6719 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6720 (setq s (replace-match "%s" t nil s))
6721 (push varform vars))
6722 (setq vars (nreverse vars))
6723 (with-current-buffer (or org-agenda-buffer (current-buffer))
6724 (setq org-prefix-format-compiled
6725 (list
6726 `((org-prefix-has-time ,org-prefix-has-time)
6727 (org-prefix-has-tag ,org-prefix-has-tag)
6728 (org-prefix-category-length ,org-prefix-category-length)
6729 (org-prefix-has-effort ,org-prefix-has-effort)
6730 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6731 `(format ,s ,@vars))))))
6733 (defun org-set-sorting-strategy (key)
6734 (if (symbolp (car org-agenda-sorting-strategy))
6735 ;; the old format
6736 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6737 (setq org-agenda-sorting-strategy-selected
6738 (or (cdr (assq key org-agenda-sorting-strategy))
6739 (cdr (assq 'agenda org-agenda-sorting-strategy))
6740 '(time-up category-keep priority-down)))))
6742 (defun org-get-time-of-day (s &optional string mod24)
6743 "Check string S for a time of day.
6744 If found, return it as a military time number between 0 and 2400.
6745 If not found, return nil.
6746 The optional STRING argument forces conversion into a 5 character wide string
6747 HH:MM."
6748 (save-match-data
6749 (when
6750 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6751 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6752 (let* ((h (string-to-number (match-string 1 s)))
6753 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6754 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6755 (am-p (equal ampm "am"))
6756 (h1 (cond ((not ampm) h)
6757 ((= h 12) (if am-p 0 12))
6758 (t (+ h (if am-p 0 12)))))
6759 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6760 (mod h1 24) h1))
6761 (t0 (+ (* 100 h2) m))
6762 (t1 (concat (if (>= h1 24) "+" " ")
6763 (if (and org-agenda-time-leading-zero
6764 (< t0 1000)) "0" "")
6765 (if (< t0 100) "0" "")
6766 (if (< t0 10) "0" "")
6767 (int-to-string t0))))
6768 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6770 (defvar org-agenda-before-sorting-filter-function nil
6771 "Function to be applied to agenda items prior to sorting.
6772 Prior to sorting also means just before they are inserted into the agenda.
6774 To aid sorting, you may revisit the original entries and add more text
6775 properties which will later be used by the sorting functions.
6777 The function should take a string argument, an agenda line.
6778 It has access to the text properties in that line, which contain among
6779 other things, the property `org-hd-marker' that points to the entry
6780 where the line comes from. Note that not all lines going into the agenda
6781 have this property, only most.
6783 The function should return the modified string. It is probably best
6784 to ONLY change text properties.
6786 You can also use this function as a filter, by returning nil for lines
6787 you don't want to have in the agenda at all. For this application, you
6788 could bind the variable in the options section of a custom command.")
6790 (defun org-agenda-finalize-entries (list &optional type)
6791 "Sort, limit and concatenate the LIST of agenda items.
6792 The optional argument TYPE tells the agenda type."
6793 (let ((max-effort (cond ((listp org-agenda-max-effort)
6794 (cdr (assoc type org-agenda-max-effort)))
6795 (t org-agenda-max-effort)))
6796 (max-todo (cond ((listp org-agenda-max-todos)
6797 (cdr (assoc type org-agenda-max-todos)))
6798 (t org-agenda-max-todos)))
6799 (max-tags (cond ((listp org-agenda-max-tags)
6800 (cdr (assoc type org-agenda-max-tags)))
6801 (t org-agenda-max-tags)))
6802 (max-entries (cond ((listp org-agenda-max-entries)
6803 (cdr (assoc type org-agenda-max-entries)))
6804 (t org-agenda-max-entries))) l)
6805 (when org-agenda-before-sorting-filter-function
6806 (setq list
6807 (delq nil
6808 (mapcar
6809 org-agenda-before-sorting-filter-function list))))
6810 (setq list (mapcar 'org-agenda-highlight-todo list)
6811 list (mapcar 'identity (sort list 'org-entries-lessp)))
6812 (when max-effort
6813 (setq list (org-agenda-limit-entries
6814 list 'effort-minutes max-effort 'identity)))
6815 (when max-todo
6816 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6817 (when max-tags
6818 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6819 (when max-entries
6820 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6821 (mapconcat 'identity list "\n")))
6823 (defun org-agenda-limit-entries (list prop limit &optional fn)
6824 "Limit the number of agenda entries."
6825 (let ((include (and limit (< limit 0))))
6826 (if limit
6827 (let ((fun (or fn (lambda (p) (if p 1))))
6828 (lim 0))
6829 (delq nil
6830 (mapcar
6831 (lambda (e)
6832 (let ((pval (funcall fun (get-text-property 1 prop e))))
6833 (if pval (setq lim (+ lim pval)))
6834 (cond ((and pval (<= lim (abs limit))) e)
6835 ((and include (not pval)) e))))
6836 list)))
6837 list)))
6839 (defun org-agenda-limit-interactively ()
6840 "In agenda, interactively limit entries to various maximums."
6841 (interactive)
6842 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6843 (num (string-to-number (read-from-minibuffer "How many? "))))
6844 (cond ((equal max ?e)
6845 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6846 ((equal max ?t)
6847 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6848 ((equal max ?T)
6849 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6850 ((equal max ?E)
6851 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6852 (org-agenda-fit-window-to-buffer))
6854 (defun org-agenda-highlight-todo (x)
6855 (let ((org-done-keywords org-done-keywords-for-agenda)
6856 (case-fold-search nil)
6858 (if (eq x 'line)
6859 (save-excursion
6860 (beginning-of-line 1)
6861 (setq re (org-get-at-bol 'org-todo-regexp))
6862 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6863 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6864 (add-text-properties (match-beginning 0) (match-end 1)
6865 (list 'face (org-get-todo-face 1)))
6866 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6867 (delete-region (match-beginning 1) (1- (match-end 0)))
6868 (goto-char (match-beginning 1))
6869 (insert (format org-agenda-todo-keyword-format s)))))
6870 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6871 (setq re (get-text-property 0 'org-todo-regexp x))
6872 (when (and re
6873 ;; Test `pl' because if there's no heading content,
6874 ;; there's no point matching to highlight. Note
6875 ;; that if we didn't test `pl' first, and there
6876 ;; happened to be no keyword from `org-todo-regexp'
6877 ;; on this heading line, then the `equal' comparison
6878 ;; afterwards would spuriously succeed in the case
6879 ;; where `pl' is nil -- causing an args-out-of-range
6880 ;; error when we try to add text properties to text
6881 ;; that isn't there.
6883 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6884 x pl) pl))
6885 (add-text-properties
6886 (or (match-end 1) (match-end 0)) (match-end 0)
6887 (list 'face (org-get-todo-face (match-string 2 x)))
6889 (when (match-end 1)
6890 (setq x (concat (substring x 0 (match-end 1))
6891 (format org-agenda-todo-keyword-format
6892 (match-string 2 x))
6893 (org-add-props " " (text-properties-at 0 x))
6894 (substring x (match-end 3)))))))
6895 x)))
6897 (defsubst org-cmp-priority (a b)
6898 "Compare the priorities of string A and B."
6899 (let ((pa (or (get-text-property 1 'priority a) 0))
6900 (pb (or (get-text-property 1 'priority b) 0)))
6901 (cond ((> pa pb) +1)
6902 ((< pa pb) -1))))
6904 (defsubst org-cmp-effort (a b)
6905 "Compare the effort values of string A and B."
6906 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6907 (ea (or (get-text-property 1 'effort-minutes a) def))
6908 (eb (or (get-text-property 1 'effort-minutes b) def)))
6909 (cond ((> ea eb) +1)
6910 ((< ea eb) -1))))
6912 (defsubst org-cmp-category (a b)
6913 "Compare the string values of categories of strings A and B."
6914 (let ((ca (or (get-text-property 1 'org-category a) ""))
6915 (cb (or (get-text-property 1 'org-category b) "")))
6916 (cond ((string-lessp ca cb) -1)
6917 ((string-lessp cb ca) +1))))
6919 (defsubst org-cmp-todo-state (a b)
6920 "Compare the todo states of strings A and B."
6921 (let* ((ma (or (get-text-property 1 'org-marker a)
6922 (get-text-property 1 'org-hd-marker a)))
6923 (mb (or (get-text-property 1 'org-marker b)
6924 (get-text-property 1 'org-hd-marker b)))
6925 (fa (and ma (marker-buffer ma)))
6926 (fb (and mb (marker-buffer mb)))
6927 (todo-kwds
6928 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6929 (and fb (with-current-buffer fb org-todo-keywords-1))))
6930 (ta (or (get-text-property 1 'todo-state a) ""))
6931 (tb (or (get-text-property 1 'todo-state b) ""))
6932 (la (- (length (member ta todo-kwds))))
6933 (lb (- (length (member tb todo-kwds))))
6934 (donepa (member ta org-done-keywords-for-agenda))
6935 (donepb (member tb org-done-keywords-for-agenda)))
6936 (cond ((and donepa (not donepb)) -1)
6937 ((and (not donepa) donepb) +1)
6938 ((< la lb) -1)
6939 ((< lb la) +1))))
6941 (defsubst org-cmp-alpha (a b)
6942 "Compare the headlines, alphabetically."
6943 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6944 (plb (text-property-any 0 (length b) 'org-heading t b))
6945 (ta (and pla (substring a pla)))
6946 (tb (and plb (substring b plb))))
6947 (when pla
6948 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6949 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6950 (setq ta (substring ta (match-end 0))))
6951 (setq ta (downcase ta)))
6952 (when plb
6953 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6954 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6955 (setq tb (substring tb (match-end 0))))
6956 (setq tb (downcase tb)))
6957 (cond ((not ta) +1)
6958 ((not tb) -1)
6959 ((string-lessp ta tb) -1)
6960 ((string-lessp tb ta) +1))))
6962 (defsubst org-cmp-tag (a b)
6963 "Compare the string values of the first tags of A and B."
6964 (let ((ta (car (last (get-text-property 1 'tags a))))
6965 (tb (car (last (get-text-property 1 'tags b)))))
6966 (cond ((not ta) +1)
6967 ((not tb) -1)
6968 ((string-lessp ta tb) -1)
6969 ((string-lessp tb ta) +1))))
6971 (defsubst org-cmp-time (a b)
6972 "Compare the time-of-day values of strings A and B."
6973 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6974 (ta (or (get-text-property 1 'time-of-day a) def))
6975 (tb (or (get-text-property 1 'time-of-day b) def)))
6976 (cond ((< ta tb) -1)
6977 ((< tb ta) +1))))
6979 (defsubst org-cmp-ts (a b &optional type)
6980 "Compare the timestamps values of entries A and B.
6981 When TYPE is \"scheduled\", \"deadline\", \"timestamp\"
6982 or \"timestamp_ia\", compare within each of these type.
6983 When TYPE is the empty string, compare all timestamps
6984 without respect of their type."
6985 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6986 (ta (or (and (string-match type (get-text-property 1 'type a))
6987 (get-text-property 1 'ts-date a)) def))
6988 (tb (or (and (string-match type (get-text-property 1 'type b))
6989 (get-text-property 1 'ts-date b)) def)))
6990 (cond ((< ta tb) -1)
6991 ((< tb ta) +1))))
6993 (defsubst org-cmp-habit-p (a b)
6994 "Compare the todo states of strings A and B."
6995 (let ((ha (get-text-property 1 'org-habit-p a))
6996 (hb (get-text-property 1 'org-habit-p b)))
6997 (cond ((and ha (not hb)) -1)
6998 ((and (not ha) hb) +1))))
7000 (defun org-entries-lessp (a b)
7001 "Predicate for sorting agenda entries."
7002 ;; The following variables will be used when the form is evaluated.
7003 ;; So even though the compiler complains, keep them.
7004 (let* ((ss org-agenda-sorting-strategy-selected)
7005 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7006 (org-cmp-ts a b "")))
7007 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7008 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7009 (org-cmp-ts a b "scheduled")))
7010 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7011 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7012 (org-cmp-ts a b "deadline")))
7013 (deadline-down (if deadline-up (- deadline-up) nil))
7014 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7015 (org-cmp-ts a b "iatimestamp_ia")))
7016 (tsia-down (if tsia-up (- tsia-up) nil))
7017 (ts-up (and (org-em 'ts-up 'ts-down ss)
7018 (org-cmp-ts a b "timestamp")))
7019 (ts-down (if ts-up (- ts-up) nil))
7020 (time-up (and (org-em 'time-up 'time-down ss)
7021 (org-cmp-time a b)))
7022 (time-down (if time-up (- time-up) nil))
7023 (priority-up (and (org-em 'priority-up 'priority-down ss)
7024 (org-cmp-priority a b)))
7025 (priority-down (if priority-up (- priority-up) nil))
7026 (effort-up (and (org-em 'effort-up 'effort-down ss)
7027 (org-cmp-effort a b)))
7028 (effort-down (if effort-up (- effort-up) nil))
7029 (category-up (and (or (org-em 'category-up 'category-down ss)
7030 (memq 'category-keep ss))
7031 (org-cmp-category a b)))
7032 (category-down (if category-up (- category-up) nil))
7033 (category-keep (if category-up +1 nil))
7034 (tag-up (and (org-em 'tag-up 'tag-down ss)
7035 (org-cmp-tag a b)))
7036 (tag-down (if tag-up (- tag-up) nil))
7037 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7038 (org-cmp-todo-state a b)))
7039 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7040 (habit-up (and (org-em 'habit-up 'habit-down ss)
7041 (org-cmp-habit-p a b)))
7042 (habit-down (if habit-up (- habit-up) nil))
7043 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7044 (org-cmp-alpha a b)))
7045 (alpha-down (if alpha-up (- alpha-up) nil))
7046 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7047 user-defined-up user-defined-down)
7048 (if (and need-user-cmp org-agenda-cmp-user-defined
7049 (functionp org-agenda-cmp-user-defined))
7050 (setq user-defined-up
7051 (funcall org-agenda-cmp-user-defined a b)
7052 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7053 (cdr (assoc
7054 (eval (cons 'or org-agenda-sorting-strategy-selected))
7055 '((-1 . t) (1 . nil) (nil . nil))))))
7057 ;;; Agenda restriction lock
7059 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7060 "Overlay to mark the headline to which agenda commands are restricted.")
7061 (overlay-put org-agenda-restriction-lock-overlay
7062 'face 'org-agenda-restriction-lock)
7063 (overlay-put org-agenda-restriction-lock-overlay
7064 'help-echo "Agendas are currently limited to this subtree.")
7065 (org-detach-overlay org-agenda-restriction-lock-overlay)
7067 (defun org-agenda-set-restriction-lock (&optional type)
7068 "Set restriction lock for agenda, to current subtree or file.
7069 Restriction will be the file if TYPE is `file', or if type is the
7070 universal prefix '(4), or if the cursor is before the first headline
7071 in the file. Otherwise, restriction will be to the current subtree."
7072 (interactive "P")
7073 (and (equal type '(4)) (setq type 'file))
7074 (setq type (cond
7075 (type type)
7076 ((org-at-heading-p) 'subtree)
7077 ((condition-case nil (org-back-to-heading t) (error nil))
7078 'subtree)
7079 (t 'file)))
7080 (if (eq type 'subtree)
7081 (progn
7082 (setq org-agenda-restrict t)
7083 (setq org-agenda-overriding-restriction 'subtree)
7084 (put 'org-agenda-files 'org-restrict
7085 (list (buffer-file-name (buffer-base-buffer))))
7086 (org-back-to-heading t)
7087 (move-overlay org-agenda-restriction-lock-overlay
7088 (point)
7089 (if org-agenda-restriction-lock-highlight-subtree
7090 (save-excursion (org-end-of-subtree t t) (point))
7091 (point-at-eol)))
7092 (move-marker org-agenda-restrict-begin (point))
7093 (move-marker org-agenda-restrict-end
7094 (save-excursion (org-end-of-subtree t t)))
7095 (message "Locking agenda restriction to subtree"))
7096 (put 'org-agenda-files 'org-restrict
7097 (list (buffer-file-name (buffer-base-buffer))))
7098 (setq org-agenda-restrict nil)
7099 (setq org-agenda-overriding-restriction 'file)
7100 (move-marker org-agenda-restrict-begin nil)
7101 (move-marker org-agenda-restrict-end nil)
7102 (message "Locking agenda restriction to file"))
7103 (setq current-prefix-arg nil)
7104 (org-agenda-maybe-redo))
7106 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7107 "Remove the agenda restriction lock."
7108 (interactive "P")
7109 (org-detach-overlay org-agenda-restriction-lock-overlay)
7110 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7111 (setq org-agenda-overriding-restriction nil)
7112 (setq org-agenda-restrict nil)
7113 (put 'org-agenda-files 'org-restrict nil)
7114 (move-marker org-agenda-restrict-begin nil)
7115 (move-marker org-agenda-restrict-end nil)
7116 (setq current-prefix-arg nil)
7117 (message "Agenda restriction lock removed")
7118 (or noupdate (org-agenda-maybe-redo)))
7120 (defun org-agenda-maybe-redo ()
7121 "If there is any window showing the agenda view, update it."
7122 (let ((w (get-buffer-window org-agenda-buffer-name t))
7123 (w0 (selected-window)))
7124 (when w
7125 (select-window w)
7126 (org-agenda-redo)
7127 (select-window w0)
7128 (if org-agenda-overriding-restriction
7129 (message "Agenda view shifted to new %s restriction"
7130 org-agenda-overriding-restriction)
7131 (message "Agenda restriction lock removed")))))
7133 ;;; Agenda commands
7135 (defun org-agenda-check-type (error &rest types)
7136 "Check if agenda buffer is of allowed type.
7137 If ERROR is non-nil, throw an error, otherwise just return nil.
7138 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7139 (if (not org-agenda-type)
7140 (error "No Org agenda currently displayed")
7141 (if (memq org-agenda-type types)
7143 (if error
7144 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7145 nil))))
7147 (defun org-agenda-Quit ()
7148 "Exit the agenda and kill buffers loaded by `org-agenda'.
7149 Also restore the window configuration."
7150 (interactive)
7151 (if org-agenda-columns-active
7152 (org-columns-quit)
7153 (let ((buf (current-buffer)))
7154 (if (eq org-agenda-window-setup 'other-frame)
7155 (progn
7156 (org-agenda-reset-markers)
7157 (kill-buffer buf)
7158 (org-columns-remove-overlays)
7159 (setq org-agenda-archives-mode nil)
7160 (delete-frame))
7161 (and (not (eq org-agenda-window-setup 'current-window))
7162 (not (one-window-p))
7163 (delete-window))
7164 (org-agenda-reset-markers)
7165 (kill-buffer buf)
7166 (org-columns-remove-overlays)
7167 (setq org-agenda-archives-mode nil)))
7168 (setq org-agenda-buffer nil)
7169 ;; Maybe restore the pre-agenda window configuration.
7170 (and org-agenda-restore-windows-after-quit
7171 (not (eq org-agenda-window-setup 'other-frame))
7172 org-agenda-pre-window-conf
7173 (set-window-configuration org-agenda-pre-window-conf)
7174 (setq org-agenda-pre-window-conf nil))))
7176 (defun org-agenda-quit ()
7177 "Exit the agenda and restore the window configuration.
7178 When `org-agenda-sticky' is non-nil, only bury the agenda."
7179 (interactive)
7180 (if (and (eq org-indirect-buffer-display 'other-window)
7181 org-last-indirect-buffer)
7182 (let ((org-last-indirect-window
7183 (get-buffer-window org-last-indirect-buffer)))
7184 (if org-last-indirect-window
7185 (delete-window org-last-indirect-window))))
7186 (if org-agenda-columns-active
7187 (org-columns-quit)
7188 (if org-agenda-sticky
7189 (let ((buf (current-buffer)))
7190 (if (eq org-agenda-window-setup 'other-frame)
7191 (progn
7192 (delete-frame))
7193 (and (not (eq org-agenda-window-setup 'current-window))
7194 (not (one-window-p))
7195 (delete-window)))
7196 (with-current-buffer buf
7197 (bury-buffer)
7198 ;; Maybe restore the pre-agenda window configuration.
7199 (and org-agenda-restore-windows-after-quit
7200 (not (eq org-agenda-window-setup 'other-frame))
7201 org-agenda-pre-window-conf
7202 (set-window-configuration org-agenda-pre-window-conf)
7203 (setq org-agenda-pre-window-conf nil))))
7204 (org-agenda-Quit))))
7206 (defun org-agenda-exit ()
7207 "Exit the agenda and restore the window configuration.
7208 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7209 buffers visited directly by the user will not be touched."
7210 (interactive)
7211 (org-release-buffers org-agenda-new-buffers)
7212 (setq org-agenda-new-buffers nil)
7213 (org-agenda-Quit))
7215 (defun org-agenda-kill-all-agenda-buffers ()
7216 "Kill all buffers in `org-agenda-mode'.
7217 This is used when toggling sticky agendas.
7218 You can also explicitly invoke it with `C-c a C-k'."
7219 (interactive)
7220 (let (blist)
7221 (dolist (buf (buffer-list))
7222 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7223 (push buf blist)))
7224 (mapc 'kill-buffer blist)))
7226 (defun org-agenda-execute (arg)
7227 "Execute another agenda command, keeping same window.
7228 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7229 in the agenda."
7230 (interactive "P")
7231 (let ((org-agenda-window-setup 'current-window))
7232 (org-agenda arg)))
7234 (defun org-agenda-redo (&optional all)
7235 "Rebuild possibly ALL agenda view(s) in the current buffer."
7236 (interactive "P")
7237 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7238 (cpa (unless (eq all t) current-prefix-arg))
7239 (org-agenda-doing-sticky-redo org-agenda-sticky)
7240 (org-agenda-sticky nil)
7241 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7242 org-agenda-buffer-name))
7243 (org-agenda-keep-modes t)
7244 (tag-filter org-agenda-tag-filter)
7245 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7246 (top-hl-filter org-agenda-top-headline-filter)
7247 (cat-filter org-agenda-category-filter)
7248 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7249 (re-filter org-agenda-regexp-filter)
7250 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7251 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7252 (cols org-agenda-columns-active)
7253 (line (org-current-line))
7254 (window-line (- line (org-current-line (window-start))))
7255 (lprops (get 'org-agenda-redo-command 'org-lprops))
7256 (redo-cmd (get-text-property p 'org-redo-cmd))
7257 (last-args (get-text-property p 'org-last-args))
7258 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7259 (org-agenda-overriding-cmd-arguments
7260 (unless (eq all t)
7261 (cond ((listp last-args)
7262 (cons (or cpa (car last-args)) (cdr last-args)))
7263 ((stringp last-args)
7264 last-args))))
7265 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7266 (put 'org-agenda-tag-filter :preset-filter nil)
7267 (put 'org-agenda-category-filter :preset-filter nil)
7268 (put 'org-agenda-regexp-filter :preset-filter nil)
7269 (and cols (org-columns-quit))
7270 (message "Rebuilding agenda buffer...")
7271 (if series-redo-cmd
7272 (eval series-redo-cmd)
7273 (org-let lprops redo-cmd))
7274 (setq org-agenda-undo-list nil
7275 org-agenda-pending-undo-list nil
7276 org-agenda-tag-filter tag-filter
7277 org-agenda-category-filter cat-filter
7278 org-agenda-regexp-filter re-filter
7279 org-agenda-top-headline-filter top-hl-filter)
7280 (message "Rebuilding agenda buffer...done")
7281 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7282 (put 'org-agenda-category-filter :preset-filter cat-preset)
7283 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7284 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7285 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7286 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7287 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7288 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7289 (org-goto-line line)
7290 (recenter window-line)))
7292 (defvar org-global-tags-completion-table nil)
7293 (defvar org-agenda-filter-form nil)
7294 (defvar org-agenda-filtered-by-category nil)
7296 (defun org-agenda-filter-by-category (strip)
7297 "Keep only those lines in the agenda buffer that have a specific category.
7298 The category is that of the current line."
7299 (interactive "P")
7300 (if (and org-agenda-filtered-by-category
7301 org-agenda-category-filter)
7302 (org-agenda-filter-show-all-cat)
7303 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7304 (if (and cat (not (string= "" cat)))
7305 (org-agenda-filter-apply
7306 (setq org-agenda-category-filter
7307 (list (concat (if strip "-" "+") cat)))
7308 'category)
7309 (error "No category at point")))))
7311 (defun org-find-top-headline (&optional pos)
7312 "Find the topmost parent headline and return it."
7313 (save-excursion
7314 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7315 (if pos (goto-char pos))
7316 ;; Skip up to the topmost parent
7317 (while (ignore-errors (outline-up-heading 1) t))
7318 (ignore-errors
7319 (nth 4 (org-heading-components))))))
7321 (defvar org-agenda-filtered-by-top-headline nil)
7322 (defun org-agenda-filter-by-top-headline (strip)
7323 "Keep only those lines that are descendants from the same top headline.
7324 The top headline is that of the current line."
7325 (interactive "P")
7326 (if org-agenda-filtered-by-top-headline
7327 (progn
7328 (setq org-agenda-filtered-by-top-headline nil
7329 org-agenda-top-headline-filter nil)
7330 (org-agenda-filter-show-all-cat))
7331 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7332 (if cat (org-agenda-filter-top-headline-apply cat strip)
7333 (error "No top-level category at point")))))
7335 (defvar org-agenda-regexp-filter nil)
7336 (defun org-agenda-filter-by-regexp (strip)
7337 "Filter agenda entries by a regular expression.
7338 Regexp filters are cumulative.
7339 With no prefix argument, keep entries matching the regexp.
7340 With one prefix argument, filter out entries matching the regexp.
7341 With two prefix arguments, remove the regexp filters."
7342 (interactive "P")
7343 (if (not (equal strip '(16)))
7344 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7345 (read-from-minibuffer
7346 (if (equal strip '(4))
7347 "Filter out entries matching regexp: "
7348 "Narrow to entries matching regexp: ")))))
7349 (push flt org-agenda-regexp-filter)
7350 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7351 (org-agenda-filter-show-all-re)
7352 (message "Regexp filter removed")))
7354 (defun org-agenda-filter-remove-all ()
7355 "Remove all filters from the current agenda buffer."
7356 (interactive)
7357 (when org-agenda-tag-filter
7358 (org-agenda-filter-show-all-tag))
7359 (when org-agenda-category-filter
7360 (org-agenda-filter-show-all-cat))
7361 (when org-agenda-regexp-filter
7362 (org-agenda-filter-show-all-re)))
7364 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7365 "Keep only those lines in the agenda buffer that have a specific tag.
7366 The tag is selected with its fast selection letter, as configured.
7367 With prefix argument STRIP, remove all lines that do have the tag.
7368 A lisp caller can specify CHAR. NARROW means that the new tag should be
7369 used to narrow the search - the interactive user can also press `-' or `+'
7370 to switch to narrowing."
7371 (interactive "P")
7372 (let* ((alist org-tag-alist-for-agenda)
7373 (tag-chars (mapconcat
7374 (lambda (x) (if (and (not (symbolp (car x)))
7375 (cdr x))
7376 (char-to-string (cdr x))
7377 ""))
7378 alist ""))
7379 (efforts (org-split-string
7380 (or (cdr (assoc (concat org-effort-property "_ALL")
7381 org-global-properties))
7382 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7383 "")))
7384 (effort-op org-agenda-filter-effort-default-operator)
7385 (effort-prompt "")
7386 (inhibit-read-only t)
7387 (current org-agenda-tag-filter)
7388 maybe-refresh a n tag)
7389 (unless char
7390 (message
7391 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7392 (if narrow "Narrow" "Filter") tag-chars
7393 (if org-agenda-auto-exclude-function "[RET], " ""))
7394 (setq char (read-char-exclusive)))
7395 (when (member char '(?+ ?-))
7396 ;; Narrowing down
7397 (cond ((equal char ?-) (setq strip t narrow t))
7398 ((equal char ?+) (setq strip nil narrow t)))
7399 (message
7400 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7401 (setq char (read-char-exclusive)))
7402 (when (member char '(?< ?> ?= ??))
7403 ;; An effort operator
7404 (setq effort-op (char-to-string char))
7405 (setq alist nil) ; to make sure it will be interpreted as effort.
7406 (unless (equal char ??)
7407 (loop for i from 0 to 9 do
7408 (setq effort-prompt
7409 (concat
7410 effort-prompt " ["
7411 (if (= i 9) "0" (int-to-string (1+ i)))
7412 "]" (nth i efforts))))
7413 (message "Effort%s: %s " effort-op effort-prompt)
7414 (setq char (read-char-exclusive))
7415 (when (or (< char ?0) (> char ?9))
7416 (error "Need 1-9,0 to select effort"))))
7417 (when (equal char ?\t)
7418 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7419 (org-set-local 'org-global-tags-completion-table
7420 (org-global-tags-completion-table)))
7421 (let ((completion-ignore-case t))
7422 (setq tag (org-icompleting-read
7423 "Tag: " org-global-tags-completion-table))))
7424 (cond
7425 ((equal char ?\r)
7426 (org-agenda-filter-show-all-tag)
7427 (when org-agenda-auto-exclude-function
7428 (setq org-agenda-tag-filter nil)
7429 (dolist (tag (org-agenda-get-represented-tags))
7430 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7431 (if modifier
7432 (push modifier org-agenda-tag-filter))))
7433 (if (not (null org-agenda-tag-filter))
7434 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7435 (setq maybe-refresh t))
7436 ((equal char ?/)
7437 (org-agenda-filter-show-all-tag)
7438 (when (get 'org-agenda-tag-filter :preset-filter)
7439 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7440 (setq maybe-refresh t))
7441 ((equal char ?. )
7442 (setq org-agenda-tag-filter
7443 (mapcar (lambda(tag) (concat "+" tag))
7444 (org-get-at-bol 'tags)))
7445 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7446 (setq maybe-refresh t))
7447 ((or (equal char ?\ )
7448 (setq a (rassoc char alist))
7449 (and (>= char ?0) (<= char ?9)
7450 (setq n (if (= char ?0) 9 (- char ?0 1))
7451 tag (concat effort-op (nth n efforts))
7452 a (cons tag nil)))
7453 (and (= char ??)
7454 (setq tag "?eff")
7455 a (cons tag nil))
7456 (and tag (setq a (cons tag nil))))
7457 (org-agenda-filter-show-all-tag)
7458 (setq tag (car a))
7459 (setq org-agenda-tag-filter
7460 (cons (concat (if strip "-" "+") tag)
7461 (if narrow current nil)))
7462 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7463 (setq maybe-refresh t))
7464 (t (error "Invalid tag selection character %c" char)))
7465 (when (and maybe-refresh
7466 (eq org-agenda-clockreport-mode 'with-filter))
7467 (org-agenda-redo))))
7469 (defun org-agenda-get-represented-tags ()
7470 "Get a list of all tags currently represented in the agenda."
7471 (let (p tags)
7472 (save-excursion
7473 (goto-char (point-min))
7474 (while (setq p (next-single-property-change (point) 'tags))
7475 (goto-char p)
7476 (mapc (lambda (x) (add-to-list 'tags x))
7477 (get-text-property (point) 'tags))))
7478 tags))
7480 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7481 "Refine the current filter. See `org-agenda-filter-by-tag'."
7482 (interactive "P")
7483 (org-agenda-filter-by-tag strip char 'refine))
7485 (defun org-agenda-filter-make-matcher (filter type)
7486 "Create the form that tests a line for agenda filter."
7487 (let (f f1)
7488 (cond
7489 ;; Tag filter
7490 ((eq type 'tag)
7491 (setq filter
7492 (delete-dups
7493 (append (get 'org-agenda-tag-filter :preset-filter)
7494 filter)))
7495 (dolist (x filter)
7496 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7497 (ffunc
7498 (lambda (nf0 nf01 fltr notgroup op)
7499 (dolist (x fltr)
7500 (if (member x '("-" "+"))
7501 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7502 (if (string-match "[<=>?]" x)
7503 (setq nf01 (org-agenda-filter-effort-form x))
7504 (setq nf01 (list 'member (downcase (substring x 1))
7505 'tags)))
7506 (when (equal (string-to-char x) ?-)
7507 (setq nf01 (list 'not nf01))
7508 (when (not notgroup) (setq op 'and))))
7509 (push nf01 nf0))
7510 (if notgroup
7511 (push (cons 'and nf0) f)
7512 (push (cons (or op 'or) nf0) f)))))
7513 (if (equal nfilter filter)
7514 (funcall ffunc f1 f filter t nil)
7515 (funcall ffunc nf1 nf nfilter nil nil)))))
7516 ;; Category filter
7517 ((eq type 'category)
7518 (setq filter
7519 (delete-dups
7520 (append (get 'org-agenda-category-filter :preset-filter)
7521 filter)))
7522 (dolist (x filter)
7523 (if (equal "-" (substring x 0 1))
7524 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7525 (setq f1 (list 'equal (substring x 1) 'cat)))
7526 (push f1 f)))
7527 ;; Regexp filter
7528 ((eq type 'regexp)
7529 (setq filter
7530 (delete-dups
7531 (append (get 'org-agenda-regexp-filter :preset-filter)
7532 filter)))
7533 (dolist (x filter)
7534 (if (equal "-" (substring x 0 1))
7535 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7536 (setq f1 (list 'string-match (substring x 1) 'txt)))
7537 (push f1 f))))
7538 (cons 'and (nreverse f))))
7540 (defun org-agenda-filter-effort-form (e)
7541 "Return the form to compare the effort of the current line with what E says.
7542 E looks like \"+<2:25\"."
7543 (let (op)
7544 (setq e (substring e 1))
7545 (setq op (string-to-char e) e (substring e 1))
7546 (setq op (cond ((equal op ?<) '<=)
7547 ((equal op ?>) '>=)
7548 ((equal op ??) op)
7549 (t '=)))
7550 (list 'org-agenda-compare-effort (list 'quote op)
7551 (org-duration-string-to-minutes e))))
7553 (defun org-agenda-compare-effort (op value)
7554 "Compare the effort of the current line with VALUE, using OP.
7555 If the line does not have an effort defined, return nil."
7556 (let ((eff (org-get-at-bol 'effort-minutes)))
7557 (if (equal op ??)
7558 (not eff)
7559 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7560 value))))
7562 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7563 "Expand group tags in FILTER for the agenda.
7564 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7565 (if org-group-tags
7566 (let ((case-fold-search t) rtn)
7567 (mapc
7568 (lambda (f)
7569 (let (f0 dir)
7570 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7571 (setq dir (match-string 1 f) f0 (match-string 2 f))
7572 (setq dir (if no-operator "" "+") f0 f))
7573 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7574 (org-tags-expand f0 t t))
7575 rtn))))
7576 filter)
7577 (reverse rtn))
7578 filter))
7580 (defun org-agenda-filter-apply (filter type)
7581 "Set FILTER as the new agenda filter and apply it."
7582 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7583 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7584 (let (tags cat txt)
7585 (setq org-agenda-filter-form
7586 (org-agenda-filter-make-matcher filter type))
7587 (if (and (eq type 'category)
7588 (not (equal (substring (car filter) 0 1) "-")))
7589 ;; Only set `org-agenda-filtered-by-category' to t
7590 ;; when a unique category is used as the filter
7591 (setq org-agenda-filtered-by-category t))
7592 (org-agenda-set-mode-name)
7593 (save-excursion
7594 (goto-char (point-min))
7595 (while (not (eobp))
7596 (if (org-get-at-bol 'org-marker)
7597 (progn
7598 (setq tags ; used in eval
7599 (apply 'append
7600 (mapcar (lambda (f)
7601 (org-agenda-filter-expand-tags (list f) t))
7602 (org-get-at-bol 'tags)))
7603 cat (get-text-property (point) 'org-category)
7604 txt (get-text-property (point) 'txt))
7605 (if (not (eval org-agenda-filter-form))
7606 (org-agenda-filter-hide-line type))
7607 (beginning-of-line 2))
7608 (beginning-of-line 2))))
7609 (if (get-char-property (point) 'invisible)
7610 (ignore-errors (org-agenda-previous-line)))))
7612 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7613 "Filter by top headline HL."
7614 (org-agenda-set-mode-name)
7615 (save-excursion
7616 (goto-char (point-min))
7617 (while (not (eobp))
7618 (let* ((pos (org-get-at-bol 'org-hd-marker))
7619 (tophl (and pos (org-find-top-headline pos))))
7620 (if (and tophl (funcall (if negative 'identity 'not)
7621 (string= hl tophl)))
7622 (org-agenda-filter-hide-line 'category)))
7623 (beginning-of-line 2)))
7624 (if (get-char-property (point) 'invisible)
7625 (org-agenda-previous-line))
7626 (setq org-agenda-top-headline-filter hl
7627 org-agenda-filtered-by-top-headline t))
7629 (defun org-agenda-filter-hide-line (type)
7630 "Hide lines with TYPE in the agenda buffer."
7631 (let (ov)
7632 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7633 (point-at-eol)))
7634 (overlay-put ov 'invisible t)
7635 (overlay-put ov 'type type)
7636 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7637 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7638 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7640 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7641 (setq pos (or pos (point)))
7642 (save-excursion
7643 (dolist (ov (overlays-at pos))
7644 (when (and (overlay-get ov 'invisible)
7645 (eq (overlay-get ov 'type) 'tag))
7646 (goto-char pos)
7647 (if (< (overlay-start ov) (point-at-eol))
7648 (move-overlay ov (point-at-eol)
7649 (overlay-end ov)))))))
7651 (defun org-agenda-filter-show-all-tag nil
7652 "Remove tag filter overlays from the agenda buffer."
7653 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7654 (setq org-agenda-tag-filter-overlays nil
7655 org-agenda-tag-filter nil
7656 org-agenda-filter-form nil)
7657 (org-agenda-set-mode-name))
7659 (defun org-agenda-filter-show-all-re nil
7660 "Remove regexp filter overlays from the agenda buffer."
7661 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7662 (setq org-agenda-re-filter-overlays nil
7663 org-agenda-regexp-filter nil
7664 org-agenda-filter-form nil)
7665 (org-agenda-set-mode-name))
7667 (defun org-agenda-filter-show-all-cat nil
7668 "Remove category filter overlays from the agenda buffer."
7669 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7670 (setq org-agenda-cat-filter-overlays nil
7671 org-agenda-filtered-by-category nil
7672 org-agenda-category-filter nil
7673 org-agenda-filter-form nil)
7674 (org-agenda-set-mode-name))
7676 (defun org-agenda-manipulate-query-add ()
7677 "Manipulate the query by adding a search term with positive selection.
7678 Positive selection means the term must be matched for selection of an entry."
7679 (interactive)
7680 (org-agenda-manipulate-query ?\[))
7681 (defun org-agenda-manipulate-query-subtract ()
7682 "Manipulate the query by adding a search term with negative selection.
7683 Negative selection means term must not be matched for selection of an entry."
7684 (interactive)
7685 (org-agenda-manipulate-query ?\]))
7686 (defun org-agenda-manipulate-query-add-re ()
7687 "Manipulate the query by adding a search regexp with positive selection.
7688 Positive selection means the regexp must match for selection of an entry."
7689 (interactive)
7690 (org-agenda-manipulate-query ?\{))
7691 (defun org-agenda-manipulate-query-subtract-re ()
7692 "Manipulate the query by adding a search regexp with negative selection.
7693 Negative selection means regexp must not match for selection of an entry."
7694 (interactive)
7695 (org-agenda-manipulate-query ?\}))
7696 (defun org-agenda-manipulate-query (char)
7697 (cond
7698 ((memq org-agenda-type '(timeline agenda))
7699 (let ((org-agenda-include-inactive-timestamps t))
7700 (org-agenda-redo))
7701 (message "Display now includes inactive timestamps as well"))
7702 ((eq org-agenda-type 'search)
7703 (org-add-to-string
7704 'org-agenda-query-string
7705 (if org-agenda-last-search-view-search-was-boolean
7706 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7707 (?\{ . " +{}") (?\} . " -{}"))))
7708 " "))
7709 (setq org-agenda-redo-command
7710 (list 'org-search-view
7711 (car (get-text-property (min (1- (point-max)) (point))
7712 'org-last-args))
7713 org-agenda-query-string
7714 (+ (length org-agenda-query-string)
7715 (if (member char '(?\{ ?\})) 0 1))))
7716 (set-register org-agenda-query-register org-agenda-query-string)
7717 (let ((org-agenda-overriding-arguments
7718 (cdr org-agenda-redo-command)))
7719 (org-agenda-redo)))
7720 (t (error "Cannot manipulate query for %s-type agenda buffers"
7721 org-agenda-type))))
7723 (defun org-add-to-string (var string)
7724 (set var (concat (symbol-value var) string)))
7726 (defun org-agenda-goto-date (span)
7727 "Jump to DATE in agenda."
7728 (interactive "P")
7729 (let* ((org-read-date-prefer-future
7730 (eval org-agenda-jump-prefer-future))
7731 (date (org-read-date))
7732 (day (time-to-days (org-time-string-to-time date)))
7733 (org-agenda-sticky-orig org-agenda-sticky)
7734 (org-agenda-buffer-tmp-name (buffer-name))
7735 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7736 (0-arg (or current-prefix-arg (car args)))
7737 (2-arg (nth 2 args))
7738 (with-hour-p (nth 4 org-agenda-redo-command))
7739 (newcmd (list 'org-agenda-list 0-arg date
7740 (org-agenda-span-to-ndays
7741 2-arg (org-time-string-to-absolute date))
7742 with-hour-p))
7743 (newargs (cdr newcmd))
7744 (inhibit-read-only t)
7745 org-agenda-sticky)
7746 (if (not (org-agenda-check-type t 'agenda))
7747 (error "Not available in non-agenda views")
7748 (add-text-properties (point-min) (point-max)
7749 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7750 (org-agenda-redo)
7751 (goto-char (point-min))
7752 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7753 (save-excursion (move-beginning-of-line 2) (eobp))))
7754 (move-beginning-of-line 2))
7755 (setq org-agenda-sticky org-agenda-sticky-orig
7756 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7758 (defun org-agenda-goto-today ()
7759 "Go to today."
7760 (interactive)
7761 (org-agenda-check-type t 'timeline 'agenda)
7762 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7763 (curspan (nth 2 args))
7764 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7765 (cond
7766 (tdpos (goto-char tdpos))
7767 ((eq org-agenda-type 'agenda)
7768 (let* ((sd (org-agenda-compute-starting-span
7769 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7770 (org-agenda-overriding-arguments args))
7771 (setf (nth 1 org-agenda-overriding-arguments) sd)
7772 (org-agenda-redo)
7773 (org-agenda-find-same-or-today-or-agenda)))
7774 (t (error "Cannot find today")))))
7776 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7777 (goto-char
7778 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7779 (text-property-any (point-min) (point-max) 'org-today t)
7780 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7781 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7782 (org-agenda-goto-block-beginning))
7783 (point-min))))
7785 (defun org-agenda-goto-block-beginning ()
7786 "Go the agenda block beginning."
7787 (interactive)
7788 (if (not (derived-mode-p 'org-agenda-mode))
7789 (error "Cannot execute this command outside of org-agenda-mode buffers")
7790 (let (dest)
7791 (save-excursion
7792 (unless (looking-at "\\'")
7793 (forward-char))
7794 (let* ((prop 'org-agenda-structural-header)
7795 (p (previous-single-property-change (point) prop))
7796 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7797 (1- (point))) prop)))
7798 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7799 (if (not dest)
7800 (error "Cannot find the beginning of the blog")
7801 (goto-char dest)
7802 (move-beginning-of-line 1)))))
7804 (defun org-agenda-later (arg)
7805 "Go forward in time by the current span.
7806 With prefix ARG, go forward that many times the current span."
7807 (interactive "p")
7808 (org-agenda-check-type t 'agenda)
7809 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7810 (span (or (nth 2 args) org-agenda-current-span))
7811 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7812 (greg (calendar-gregorian-from-absolute sd))
7813 (cnt (org-get-at-bol 'org-day-cnt))
7814 greg2)
7815 (cond
7816 ((numberp span)
7817 (setq sd (+ (* span arg) sd)))
7818 ((eq span 'day)
7819 (setq sd (+ arg sd)))
7820 ((eq span 'week)
7821 (setq sd (+ (* 7 arg) sd)))
7822 ((eq span 'month)
7823 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7824 sd (calendar-absolute-from-gregorian greg2))
7825 (setcar greg2 (1+ (car greg2))))
7826 ((eq span 'year)
7827 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7828 sd (calendar-absolute-from-gregorian greg2))
7829 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7831 (setq sd (+ (* span arg) sd))))
7832 (let ((org-agenda-overriding-cmd
7833 ;; `cmd' may have been set by `org-agenda-run-series' which
7834 ;; uses `org-agenda-overriding-cmd' to decide whether
7835 ;; overriding is allowed for `cmd'
7836 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7837 (org-agenda-overriding-arguments
7838 (list (car args) sd span)))
7839 (org-agenda-redo)
7840 (org-agenda-find-same-or-today-or-agenda cnt))))
7842 (defun org-agenda-earlier (arg)
7843 "Go backward in time by the current span.
7844 With prefix ARG, go backward that many times the current span."
7845 (interactive "p")
7846 (org-agenda-later (- arg)))
7848 (defun org-agenda-view-mode-dispatch ()
7849 "Call one of the view mode commands."
7850 (interactive)
7851 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7852 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7853 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7854 (let ((a (read-char-exclusive)))
7855 (case a
7856 (?\ (call-interactively 'org-agenda-reset-view))
7857 (?d (call-interactively 'org-agenda-day-view))
7858 (?w (call-interactively 'org-agenda-week-view))
7859 (?m (call-interactively 'org-agenda-month-view))
7860 (?y (call-interactively 'org-agenda-year-view))
7861 (?l (call-interactively 'org-agenda-log-mode))
7862 (?L (org-agenda-log-mode '(4)))
7863 (?c (org-agenda-log-mode 'clockcheck))
7864 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7865 (?a (call-interactively 'org-agenda-archives-mode))
7866 (?A (org-agenda-archives-mode 'files))
7867 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7868 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7869 (?G (call-interactively 'org-agenda-toggle-time-grid))
7870 (?D (call-interactively 'org-agenda-toggle-diary))
7871 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7872 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7873 (org-agenda-check-type t 'timeline 'agenda)
7874 (org-agenda-redo))
7875 (message "Display now includes inactive timestamps as well"))
7876 (?q (message "Abort"))
7877 (otherwise (error "Invalid key" )))))
7879 (defun org-agenda-reset-view ()
7880 "Switch to default view for agenda."
7881 (interactive)
7882 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7883 (defun org-agenda-day-view (&optional day-of-month)
7884 "Switch to daily view for agenda.
7885 With argument DAY-OF-MONTH, switch to that day of the month."
7886 (interactive "P")
7887 (org-agenda-change-time-span 'day day-of-month))
7888 (defun org-agenda-week-view (&optional iso-week)
7889 "Switch to daily view for agenda.
7890 With argument ISO-WEEK, switch to the corresponding ISO week.
7891 If ISO-WEEK has more then 2 digits, only the last two encode the
7892 week. Any digits before this encode a year. So 200712 means
7893 week 12 of year 2007. Years in the range 1938-2037 can also be
7894 written as 2-digit years."
7895 (interactive "P")
7896 (org-agenda-change-time-span 'week iso-week))
7897 (defun org-agenda-month-view (&optional month)
7898 "Switch to monthly view for agenda.
7899 With argument MONTH, switch to that month."
7900 (interactive "P")
7901 (org-agenda-change-time-span 'month month))
7902 (defun org-agenda-year-view (&optional year)
7903 "Switch to yearly view for agenda.
7904 With argument YEAR, switch to that year.
7905 If MONTH has more then 2 digits, only the last two encode the
7906 month. Any digits before this encode a year. So 200712 means
7907 December year 2007. Years in the range 1938-2037 can also be
7908 written as 2-digit years."
7909 (interactive "P")
7910 (when year
7911 (setq year (org-small-year-to-year year)))
7912 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7913 (org-agenda-change-time-span 'year year)
7914 (error "Abort")))
7916 (defun org-agenda-change-time-span (span &optional n)
7917 "Change the agenda view to SPAN.
7918 SPAN may be `day', `week', `month', `year'."
7919 (org-agenda-check-type t 'agenda)
7920 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7921 (curspan (nth 2 args)))
7922 (if (and (not n) (equal curspan span))
7923 (error "Viewing span is already \"%s\"" span))
7924 (let* ((sd (or (org-get-at-bol 'day)
7925 (nth 1 args)
7926 org-starting-day))
7927 (sd (org-agenda-compute-starting-span sd span n))
7928 (org-agenda-overriding-cmd
7929 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7930 (org-agenda-overriding-arguments
7931 (list (car args) sd span)))
7932 (org-agenda-redo)
7933 (org-agenda-find-same-or-today-or-agenda))
7934 (org-agenda-set-mode-name)
7935 (message "Switched to %s view" span)))
7937 (defun org-agenda-compute-starting-span (sd span &optional n)
7938 "Compute starting date for agenda.
7939 SPAN may be `day', `week', `month', `year'. The return value
7940 is a cons cell with the starting date and the number of days,
7941 so that the date SD will be in that range."
7942 (let* ((greg (calendar-gregorian-from-absolute sd))
7943 (dg (nth 1 greg))
7944 (mg (car greg))
7945 (yg (nth 2 greg)))
7946 (cond
7947 ((eq span 'day)
7948 (when n
7949 (setq sd (+ (calendar-absolute-from-gregorian
7950 (list mg 1 yg))
7951 n -1))))
7952 ((eq span 'week)
7953 (let* ((nt (calendar-day-of-week
7954 (calendar-gregorian-from-absolute sd)))
7955 (d (if org-agenda-start-on-weekday
7956 (- nt org-agenda-start-on-weekday)
7959 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7960 (when n
7961 (require 'cal-iso)
7962 (when (> n 99)
7963 (setq y1 (org-small-year-to-year (/ n 100))
7964 n (mod n 100)))
7965 (setq sd
7966 (calendar-absolute-from-iso
7967 (list n 1
7968 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7969 ((eq span 'month)
7970 (let (y1)
7971 (when (and n (> n 99))
7972 (setq y1 (org-small-year-to-year (/ n 100))
7973 n (mod n 100)))
7974 (setq sd (calendar-absolute-from-gregorian
7975 (list (or n mg) 1 (or y1 yg))))))
7976 ((eq span 'year)
7977 (setq sd (calendar-absolute-from-gregorian
7978 (list 1 1 (or n yg))))))
7979 sd))
7981 (defun org-agenda-next-date-line (&optional arg)
7982 "Jump to the next line indicating a date in agenda buffer."
7983 (interactive "p")
7984 (org-agenda-check-type t 'agenda 'timeline)
7985 (beginning-of-line 1)
7986 ;; This does not work if user makes date format that starts with a blank
7987 (if (looking-at "^\\S-") (forward-char 1))
7988 (if (not (re-search-forward "^\\S-" nil t arg))
7989 (progn
7990 (backward-char 1)
7991 (error "No next date after this line in this buffer")))
7992 (goto-char (match-beginning 0)))
7994 (defun org-agenda-previous-date-line (&optional arg)
7995 "Jump to the previous line indicating a date in agenda buffer."
7996 (interactive "p")
7997 (org-agenda-check-type t 'agenda 'timeline)
7998 (beginning-of-line 1)
7999 (if (not (re-search-backward "^\\S-" nil t arg))
8000 (error "No previous date before this line in this buffer")))
8002 ;; Initialize the highlight
8003 (defvar org-hl (make-overlay 1 1))
8004 (overlay-put org-hl 'face 'highlight)
8006 (defun org-highlight (begin end &optional buffer)
8007 "Highlight a region with overlay."
8008 (move-overlay org-hl begin end (or buffer (current-buffer))))
8010 (defun org-unhighlight ()
8011 "Detach overlay INDEX."
8012 (org-detach-overlay org-hl))
8014 (defun org-unhighlight-once ()
8015 "Remove the highlight from its position, and this function from the hook."
8016 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8017 (org-unhighlight))
8019 (defvar org-agenda-pre-follow-window-conf nil)
8020 (defun org-agenda-follow-mode ()
8021 "Toggle follow mode in an agenda buffer."
8022 (interactive)
8023 (unless org-agenda-follow-mode
8024 (setq org-agenda-pre-follow-window-conf
8025 (current-window-configuration)))
8026 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8027 (unless org-agenda-follow-mode
8028 (set-window-configuration org-agenda-pre-follow-window-conf))
8029 (org-agenda-set-mode-name)
8030 (org-agenda-do-context-action)
8031 (message "Follow mode is %s"
8032 (if org-agenda-follow-mode "on" "off")))
8034 (defun org-agenda-entry-text-mode (&optional arg)
8035 "Toggle entry text mode in an agenda buffer."
8036 (interactive "P")
8037 (if (or org-agenda-tag-filter
8038 org-agenda-category-filter
8039 org-agenda-regexp-filter
8040 org-agenda-top-headline-filter)
8041 (user-error "Can't show entry text in filtered views")
8042 (setq org-agenda-entry-text-mode (or (integerp arg)
8043 (not org-agenda-entry-text-mode)))
8044 (org-agenda-entry-text-hide)
8045 (and org-agenda-entry-text-mode
8046 (let ((org-agenda-entry-text-maxlines
8047 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8048 (org-agenda-entry-text-show)))
8049 (org-agenda-set-mode-name)
8050 (message "Entry text mode is %s%s"
8051 (if org-agenda-entry-text-mode "on" "off")
8052 (if (not org-agenda-entry-text-mode) ""
8053 (format " (maximum number of lines is %d)"
8054 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8056 (defun org-agenda-clockreport-mode (&optional with-filter)
8057 "Toggle clocktable mode in an agenda buffer.
8058 With prefix arg WITH-FILTER, make the clocktable respect the current
8059 agenda filter."
8060 (interactive "P")
8061 (org-agenda-check-type t 'agenda)
8062 (if with-filter
8063 (setq org-agenda-clockreport-mode 'with-filter)
8064 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8065 (org-agenda-set-mode-name)
8066 (org-agenda-redo)
8067 (message "Clocktable mode is %s"
8068 (if org-agenda-clockreport-mode "on" "off")))
8070 (defun org-agenda-log-mode (&optional special)
8071 "Toggle log mode in an agenda buffer.
8072 With argument SPECIAL, show all possible log items, not only the ones
8073 configured in `org-agenda-log-mode-items'.
8074 With a double `C-u' prefix arg, show *only* log items, nothing else."
8075 (interactive "P")
8076 (org-agenda-check-type t 'agenda 'timeline)
8077 (setq org-agenda-show-log
8078 (cond
8079 ((equal special '(16)) 'only)
8080 ((eq special 'clockcheck)
8081 (if (eq org-agenda-show-log 'clockcheck)
8082 nil 'clockcheck))
8083 (special '(closed clock state))
8084 (t (not org-agenda-show-log))))
8085 (org-agenda-set-mode-name)
8086 (org-agenda-redo)
8087 (message "Log mode is %s"
8088 (if org-agenda-show-log "on" "off")))
8090 (defun org-agenda-archives-mode (&optional with-files)
8091 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8092 When called with a prefix argument, include all archive files as well."
8093 (interactive "P")
8094 (setq org-agenda-archives-mode
8095 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8096 (org-agenda-set-mode-name)
8097 (org-agenda-redo)
8098 (message
8099 "%s"
8100 (cond
8101 ((eq org-agenda-archives-mode nil)
8102 "No archives are included")
8103 ((eq org-agenda-archives-mode 'trees)
8104 (format "Trees with :%s: tag are included" org-archive-tag))
8105 ((eq org-agenda-archives-mode t)
8106 (format "Trees with :%s: tag and all active archive files are included"
8107 org-archive-tag)))))
8109 (defun org-agenda-toggle-diary ()
8110 "Toggle diary inclusion in an agenda buffer."
8111 (interactive)
8112 (org-agenda-check-type t 'agenda)
8113 (setq org-agenda-include-diary (not org-agenda-include-diary))
8114 (org-agenda-redo)
8115 (org-agenda-set-mode-name)
8116 (message "Diary inclusion turned %s"
8117 (if org-agenda-include-diary "on" "off")))
8119 (defun org-agenda-toggle-deadlines ()
8120 "Toggle inclusion of entries with a deadline in an agenda buffer."
8121 (interactive)
8122 (org-agenda-check-type t 'agenda)
8123 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8124 (org-agenda-redo)
8125 (org-agenda-set-mode-name)
8126 (message "Deadlines inclusion turned %s"
8127 (if org-agenda-include-deadlines "on" "off")))
8129 (defun org-agenda-toggle-time-grid ()
8130 "Toggle time grid in an agenda buffer."
8131 (interactive)
8132 (org-agenda-check-type t 'agenda)
8133 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8134 (org-agenda-redo)
8135 (org-agenda-set-mode-name)
8136 (message "Time-grid turned %s"
8137 (if org-agenda-use-time-grid "on" "off")))
8139 (defun org-agenda-set-mode-name ()
8140 "Set the mode name to indicate all the small mode settings."
8141 (setq mode-name
8142 (list "Org-Agenda"
8143 (if (get 'org-agenda-files 'org-restrict) " []" "")
8145 '(:eval (org-agenda-span-name org-agenda-current-span))
8146 (if org-agenda-follow-mode " Follow" "")
8147 (if org-agenda-entry-text-mode " ETxt" "")
8148 (if org-agenda-include-diary " Diary" "")
8149 (if org-agenda-include-deadlines " Ddl" "")
8150 (if org-agenda-use-time-grid " Grid" "")
8151 (if (and (boundp 'org-habit-show-habits)
8152 org-habit-show-habits) " Habit" "")
8153 (cond
8154 ((consp org-agenda-show-log) " LogAll")
8155 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8156 (org-agenda-show-log " Log")
8157 (t ""))
8158 (if (or org-agenda-category-filter
8159 (get 'org-agenda-category-filter :preset-filter))
8160 '(:eval (org-propertize
8161 (concat " <"
8162 (mapconcat
8163 'identity
8164 (append
8165 (get 'org-agenda-category-filter :preset-filter)
8166 org-agenda-category-filter)
8168 ">")
8169 'face 'org-agenda-filter-category
8170 'help-echo "Category used in filtering")) "")
8171 (if (or org-agenda-tag-filter
8172 (get 'org-agenda-tag-filter :preset-filter))
8173 '(:eval (org-propertize
8174 (concat " {"
8175 (mapconcat
8176 'identity
8177 (append
8178 (get 'org-agenda-tag-filter :preset-filter)
8179 org-agenda-tag-filter)
8181 "}")
8182 'face 'org-agenda-filter-tags
8183 'help-echo "Tags used in filtering")) "")
8184 (if (or org-agenda-regexp-filter
8185 (get 'org-agenda-regexp-filter :preset-filter))
8186 '(:eval (org-propertize
8187 (concat " ["
8188 (mapconcat
8189 'identity
8190 (append
8191 (get 'org-agenda-regexp-filter :preset-filter)
8192 org-agenda-regexp-filter)
8194 "]")
8195 'face 'org-agenda-filter-regexp
8196 'help-echo "Regexp used in filtering")) "")
8197 (if org-agenda-archives-mode
8198 (if (eq org-agenda-archives-mode t)
8199 " Archives"
8200 (format " :%s:" org-archive-tag))
8202 (if org-agenda-clockreport-mode
8203 (if (eq org-agenda-clockreport-mode 'with-filter)
8204 " Clock{}" " Clock")
8205 "")))
8206 (force-mode-line-update))
8208 (define-obsolete-function-alias
8209 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8211 (defun org-agenda-update-agenda-type ()
8212 "Update the agenda type after each command."
8213 (setq org-agenda-type
8214 (or (get-text-property (point) 'org-agenda-type)
8215 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8217 (defun org-agenda-next-line ()
8218 "Move cursor to the next line, and show if follow mode is active."
8219 (interactive)
8220 (call-interactively 'next-line)
8221 (org-agenda-do-context-action))
8223 (defun org-agenda-previous-line ()
8224 "Move cursor to the previous line, and show if follow-mode is active."
8225 (interactive)
8226 (call-interactively 'previous-line)
8227 (org-agenda-do-context-action))
8229 (defun org-agenda-next-item (n)
8230 "Move cursor to next agenda item."
8231 (interactive "p")
8232 (let ((col (current-column)))
8233 (dotimes (c n)
8234 (when (next-single-property-change (point-at-eol) 'org-marker)
8235 (move-end-of-line 1)
8236 (goto-char (next-single-property-change (point) 'org-marker))))
8237 (org-move-to-column col))
8238 (org-agenda-do-context-action))
8240 (defun org-agenda-previous-item (n)
8241 "Move cursor to next agenda item."
8242 (interactive "p")
8243 (dotimes (c n)
8244 (let ((col (current-column))
8245 (goto (save-excursion
8246 (move-end-of-line 0)
8247 (previous-single-property-change (point) 'org-marker))))
8248 (if goto (goto-char goto))
8249 (org-move-to-column col)))
8250 (org-agenda-do-context-action))
8252 (defun org-agenda-do-context-action ()
8253 "Show outline path and, maybe, follow mode window."
8254 (let ((m (org-get-at-bol 'org-marker)))
8255 (when (and (markerp m) (marker-buffer m))
8256 (and org-agenda-follow-mode
8257 (if org-agenda-follow-indirect
8258 (org-agenda-tree-to-indirect-buffer nil)
8259 (org-agenda-show)))
8260 (and org-agenda-show-outline-path
8261 (org-with-point-at m (org-display-outline-path t))))))
8263 (defun org-agenda-show-tags ()
8264 "Show the tags applicable to the current item."
8265 (interactive)
8266 (let* ((tags (org-get-at-bol 'tags)))
8267 (if tags
8268 (message "Tags are :%s:"
8269 (org-no-properties (mapconcat 'identity tags ":")))
8270 (message "No tags associated with this line"))))
8272 (defun org-agenda-goto (&optional highlight)
8273 "Go to the Org-mode file which contains the item at point."
8274 (interactive)
8275 (let* ((marker (or (org-get-at-bol 'org-marker)
8276 (org-agenda-error)))
8277 (buffer (marker-buffer marker))
8278 (pos (marker-position marker)))
8279 (switch-to-buffer-other-window buffer)
8280 (widen)
8281 (push-mark)
8282 (goto-char pos)
8283 (when (derived-mode-p 'org-mode)
8284 (org-show-context 'agenda)
8285 (save-excursion
8286 (and (outline-next-heading)
8287 (org-flag-heading nil)))) ; show the next heading
8288 (when (outline-invisible-p)
8289 (show-entry)) ; display invisible text
8290 (recenter (/ (window-height) 2))
8291 (run-hooks 'org-agenda-after-show-hook)
8292 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8294 (defvar org-agenda-after-show-hook nil
8295 "Normal hook run after an item has been shown from the agenda.
8296 Point is in the buffer where the item originated.")
8298 (defun org-agenda-kill ()
8299 "Kill the entry or subtree belonging to the current agenda entry."
8300 (interactive)
8301 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8302 (let* ((bufname-orig (buffer-name))
8303 (marker (or (org-get-at-bol 'org-marker)
8304 (org-agenda-error)))
8305 (buffer (marker-buffer marker))
8306 (pos (marker-position marker))
8307 (type (org-get-at-bol 'type))
8308 dbeg dend (n 0) conf)
8309 (org-with-remote-undo buffer
8310 (with-current-buffer buffer
8311 (save-excursion
8312 (goto-char pos)
8313 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8314 (setq dbeg (progn (org-back-to-heading t) (point))
8315 dend (org-end-of-subtree t t))
8316 (setq dbeg (point-at-bol)
8317 dend (min (point-max) (1+ (point-at-eol)))))
8318 (goto-char dbeg)
8319 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8320 (setq conf (or (eq t org-agenda-confirm-kill)
8321 (and (numberp org-agenda-confirm-kill)
8322 (> n org-agenda-confirm-kill))))
8323 (and conf
8324 (not (y-or-n-p
8325 (format "Delete entry with %d lines in buffer \"%s\"? "
8326 n (buffer-name buffer))))
8327 (error "Abort"))
8328 (let ((org-agenda-buffer-name bufname-orig))
8329 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8330 (with-current-buffer buffer (delete-region dbeg dend))
8331 (message "Agenda item and source killed"))))
8333 (defvar org-archive-default-command) ; defined in org-archive.el
8334 (defun org-agenda-archive-default ()
8335 "Archive the entry or subtree belonging to the current agenda entry."
8336 (interactive)
8337 (require 'org-archive)
8338 (org-agenda-archive-with org-archive-default-command))
8340 (defun org-agenda-archive-default-with-confirmation ()
8341 "Archive the entry or subtree belonging to the current agenda entry."
8342 (interactive)
8343 (require 'org-archive)
8344 (org-agenda-archive-with org-archive-default-command 'confirm))
8346 (defun org-agenda-archive ()
8347 "Archive the entry or subtree belonging to the current agenda entry."
8348 (interactive)
8349 (org-agenda-archive-with 'org-archive-subtree))
8351 (defun org-agenda-archive-to-archive-sibling ()
8352 "Move the entry to the archive sibling."
8353 (interactive)
8354 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8356 (defun org-agenda-archive-with (cmd &optional confirm)
8357 "Move the entry to the archive sibling."
8358 (interactive)
8359 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8360 (let* ((bufname-orig (buffer-name))
8361 (marker (or (org-get-at-bol 'org-marker)
8362 (org-agenda-error)))
8363 (buffer (marker-buffer marker))
8364 (pos (marker-position marker)))
8365 (org-with-remote-undo buffer
8366 (with-current-buffer buffer
8367 (if (derived-mode-p 'org-mode)
8368 (if (and confirm
8369 (not (y-or-n-p "Archive this subtree or entry? ")))
8370 (error "Abort")
8371 (save-excursion
8372 (goto-char pos)
8373 (let ((org-agenda-buffer-name bufname-orig))
8374 (org-remove-subtree-entries-from-agenda))
8375 (org-back-to-heading t)
8376 (funcall cmd)))
8377 (error "Archiving works only in Org-mode files"))))))
8379 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8380 "Remove all lines in the agenda that correspond to a given subtree.
8381 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8382 If this information is not given, the function uses the tree at point."
8383 (let ((buf (or buf (current-buffer))) m p)
8384 (save-excursion
8385 (unless (and beg end)
8386 (org-back-to-heading t)
8387 (setq beg (point))
8388 (org-end-of-subtree t)
8389 (setq end (point)))
8390 (set-buffer (get-buffer org-agenda-buffer-name))
8391 (save-excursion
8392 (goto-char (point-max))
8393 (beginning-of-line 1)
8394 (while (not (bobp))
8395 (when (and (setq m (org-get-at-bol 'org-marker))
8396 (equal buf (marker-buffer m))
8397 (setq p (marker-position m))
8398 (>= p beg)
8399 (< p end))
8400 (let ((inhibit-read-only t))
8401 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8402 (beginning-of-line 0))))))
8404 (defun org-agenda-refile (&optional goto rfloc no-update)
8405 "Refile the item at point.
8407 When GOTO is 0 or '(64), clear the refile cache.
8408 When GOTO is '(16), go to the location of the last refiled item.
8409 RFLOC can be a refile location obtained in a different way.
8410 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8411 (interactive "P")
8412 (cond
8413 ((member goto '(0 (64)))
8414 (org-refile-cache-clear))
8415 ((equal goto '(16))
8416 (org-refile-goto-last-stored))
8418 (let* ((buffer-orig (buffer-name))
8419 (marker (or (org-get-at-bol 'org-hd-marker)
8420 (org-agenda-error)))
8421 (buffer (marker-buffer marker))
8422 (pos (marker-position marker))
8423 (rfloc (or rfloc
8424 (org-refile-get-location
8425 (if goto "Goto" "Refile to") buffer
8426 org-refile-allow-creating-parent-nodes))))
8427 (with-current-buffer buffer
8428 (save-excursion
8429 (save-restriction
8430 (widen)
8431 (goto-char marker)
8432 (let ((org-agenda-buffer-name buffer-orig))
8433 (org-remove-subtree-entries-from-agenda))
8434 (org-refile goto buffer rfloc)))))
8435 (unless no-update (org-agenda-redo)))))
8437 (defun org-agenda-open-link (&optional arg)
8438 "Open the link(s) in the current entry, if any.
8439 This looks for a link in the displayed line in the agenda.
8440 It also looks at the text of the entry itself."
8441 (interactive "P")
8442 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8443 (org-get-at-bol 'org-marker)))
8444 (buffer (and marker (marker-buffer marker)))
8445 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8446 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8447 (lk0 (car lkall))
8448 (lk (if (stringp lk0) (list lk0) lk0))
8449 (lkend (cdr lkall))
8450 trg)
8451 (cond
8452 ((and buffer lk)
8453 (mapcar (lambda(l)
8454 (with-current-buffer buffer
8455 (setq trg (and (string-match org-bracket-link-regexp l)
8456 (match-string 1 l)))
8457 (if (or (not trg) (string-match org-any-link-re trg))
8458 (save-excursion
8459 (save-restriction
8460 (widen)
8461 (goto-char marker)
8462 (when (search-forward l nil lkend)
8463 (goto-char (match-beginning 0))
8464 (org-open-at-point))))
8465 ;; This is an internal link, widen the buffer
8466 (switch-to-buffer-other-window buffer)
8467 (widen)
8468 (goto-char marker)
8469 (when (search-forward l nil lkend)
8470 (goto-char (match-beginning 0))
8471 (org-open-at-point)))))
8472 lk))
8473 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8474 (save-excursion
8475 (beginning-of-line 1)
8476 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8477 (org-open-link-from-string (match-string 1)))
8478 (t (message "No link to open here")))))
8480 (defun org-agenda-copy-local-variable (var)
8481 "Get a variable from a referenced buffer and install it here."
8482 (let ((m (org-get-at-bol 'org-marker)))
8483 (when (and m (buffer-live-p (marker-buffer m)))
8484 (org-set-local var (with-current-buffer (marker-buffer m)
8485 (symbol-value var))))))
8487 (defun org-agenda-switch-to (&optional delete-other-windows)
8488 "Go to the Org-mode file which contains the item at point."
8489 (interactive)
8490 (if (and org-return-follows-link
8491 (not (org-get-at-bol 'org-marker))
8492 (org-in-regexp org-bracket-link-regexp))
8493 (org-open-link-from-string (match-string 0))
8494 (let* ((marker (or (org-get-at-bol 'org-marker)
8495 (org-agenda-error)))
8496 (buffer (marker-buffer marker))
8497 (pos (marker-position marker)))
8498 (org-pop-to-buffer-same-window buffer)
8499 (and delete-other-windows (delete-other-windows))
8500 (widen)
8501 (goto-char pos)
8502 (when (derived-mode-p 'org-mode)
8503 (org-show-context 'agenda)
8504 (save-excursion
8505 (and (outline-next-heading)
8506 (org-flag-heading nil))) ; show the next heading
8507 (when (outline-invisible-p)
8508 (show-entry)) ; display invisible text
8509 (run-hooks 'org-agenda-after-show-hook)))))
8511 (defun org-agenda-goto-mouse (ev)
8512 "Go to the Org-mode file which contains the item at the mouse click."
8513 (interactive "e")
8514 (mouse-set-point ev)
8515 (org-agenda-goto))
8517 (defun org-agenda-show (&optional full-entry)
8518 "Display the Org-mode file which contains the item at point.
8519 With prefix argument FULL-ENTRY, make the entire entry visible
8520 if it was hidden in the outline."
8521 (interactive "P")
8522 (let ((win (selected-window)))
8523 (if full-entry
8524 (let ((org-show-entry-below t))
8525 (org-agenda-goto t))
8526 (org-agenda-goto t))
8527 (select-window win)))
8529 (defvar org-agenda-show-window nil)
8530 (defun org-agenda-show-and-scroll-up (&optional arg)
8531 "Display the Org-mode file which contains the item at point.
8532 When called repeatedly, scroll the window that is displaying the buffer.
8533 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8534 `show-subtree' to display the item, so that drawers and logbooks stay
8535 folded."
8536 (interactive "P")
8537 (let ((win (selected-window)))
8538 (if (and (window-live-p org-agenda-show-window)
8539 (eq this-command last-command))
8540 (progn
8541 (select-window org-agenda-show-window)
8542 (ignore-errors (scroll-up)))
8543 (org-agenda-goto t)
8544 (if arg (org-show-entry) (show-subtree))
8545 (setq org-agenda-show-window (selected-window)))
8546 (select-window win)))
8548 (defun org-agenda-show-scroll-down ()
8549 "Scroll down the window showing the agenda."
8550 (interactive)
8551 (let ((win (selected-window)))
8552 (when (window-live-p org-agenda-show-window)
8553 (select-window org-agenda-show-window)
8554 (ignore-errors (scroll-down))
8555 (select-window win))))
8557 (defun org-agenda-show-1 (&optional more)
8558 "Display the Org-mode file which contains the item at point.
8559 The prefix arg selects the amount of information to display:
8561 0 hide the subtree
8562 1 just show the entry according to defaults.
8563 2 show the children view
8564 3 show the subtree view
8565 4 show the entire subtree and any LOGBOOK drawers
8566 5 show the entire subtree and any drawers
8567 With prefix argument FULL-ENTRY, make the entire entry visible
8568 if it was hidden in the outline."
8569 (interactive "p")
8570 (let ((win (selected-window)))
8571 (org-agenda-goto t)
8572 (org-recenter-heading 1)
8573 (cond
8574 ((= more 0)
8575 (hide-subtree)
8576 (save-excursion
8577 (org-back-to-heading)
8578 (run-hook-with-args 'org-cycle-hook 'folded))
8579 (message "Remote: FOLDED"))
8580 ((and (org-called-interactively-p 'any) (= more 1))
8581 (message "Remote: show with default settings"))
8582 ((= more 2)
8583 (show-entry)
8584 (show-children)
8585 (save-excursion
8586 (org-back-to-heading)
8587 (run-hook-with-args 'org-cycle-hook 'children))
8588 (message "Remote: CHILDREN"))
8589 ((= more 3)
8590 (show-subtree)
8591 (save-excursion
8592 (org-back-to-heading)
8593 (run-hook-with-args 'org-cycle-hook 'subtree))
8594 (message "Remote: SUBTREE"))
8595 ((= more 4)
8596 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8597 (org-drawer-regexp
8598 (concat "^[ \t]*:\\("
8599 (mapconcat 'regexp-quote org-drawers "\\|")
8600 "\\):[ \t]*$")))
8601 (show-subtree)
8602 (save-excursion
8603 (org-back-to-heading)
8604 (org-cycle-hide-drawers 'subtree)))
8605 (message "Remote: SUBTREE AND LOGBOOK"))
8606 ((> more 4)
8607 (show-subtree)
8608 (message "Remote: SUBTREE AND ALL DRAWERS")))
8609 (select-window win)))
8611 (defun org-recenter-heading (n)
8612 (save-excursion
8613 (org-back-to-heading)
8614 (recenter n)))
8616 (defvar org-agenda-cycle-counter nil)
8617 (defun org-agenda-cycle-show (&optional n)
8618 "Show the current entry in another window, with default settings.
8619 Default settings are taken from `org-show-hierarchy-above' and siblings.
8620 When use repeatedly in immediate succession, the remote entry will cycle
8621 through visibility
8623 children -> subtree -> folded
8625 When called with a numeric prefix arg, that arg will be passed through to
8626 `org-agenda-show-1'. For the interpretation of that argument, see the
8627 docstring of `org-agenda-show-1'."
8628 (interactive "P")
8629 (if (integerp n)
8630 (setq org-agenda-cycle-counter n)
8631 (if (not (eq last-command this-command))
8632 (setq org-agenda-cycle-counter 1)
8633 (if (equal org-agenda-cycle-counter 0)
8634 (setq org-agenda-cycle-counter 2)
8635 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8636 (if (> org-agenda-cycle-counter 3)
8637 (setq org-agenda-cycle-counter 0)))))
8638 (org-agenda-show-1 org-agenda-cycle-counter))
8640 (defun org-agenda-recenter (arg)
8641 "Display the Org-mode file which contains the item at point and recenter."
8642 (interactive "P")
8643 (let ((win (selected-window)))
8644 (org-agenda-goto t)
8645 (recenter arg)
8646 (select-window win)))
8648 (defun org-agenda-show-mouse (ev)
8649 "Display the Org-mode file which contains the item at the mouse click."
8650 (interactive "e")
8651 (mouse-set-point ev)
8652 (org-agenda-show))
8654 (defun org-agenda-check-no-diary ()
8655 "Check if the entry is a diary link and abort if yes."
8656 (if (org-get-at-bol 'org-agenda-diary-link)
8657 (org-agenda-error)))
8659 (defun org-agenda-error ()
8660 (error "Command not allowed in this line"))
8662 (defun org-agenda-tree-to-indirect-buffer (arg)
8663 "Show the subtree corresponding to the current entry in an indirect buffer.
8664 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8666 With a numerical prefix ARG, go up to this level and then take that tree.
8667 With a negative numeric ARG, go up by this number of levels.
8668 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8669 use the dedicated frame)."
8670 (interactive "P")
8671 (if current-prefix-arg
8672 (org-agenda-do-tree-to-indirect-buffer arg)
8673 (let ((agenda-buffer (buffer-name))
8674 (agenda-window (selected-window))
8675 (indirect-window
8676 (and org-last-indirect-buffer
8677 (get-buffer-window org-last-indirect-buffer))))
8678 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8679 (unless (or (eq org-indirect-buffer-display 'new-frame)
8680 (eq org-indirect-buffer-display 'dedicated-frame))
8681 (unwind-protect
8682 (unless (and indirect-window (window-live-p indirect-window))
8683 (setq indirect-window (split-window agenda-window)))
8684 (and indirect-window (select-window indirect-window))
8685 (switch-to-buffer org-last-indirect-buffer :norecord)
8686 (fit-window-to-buffer indirect-window)))
8687 (select-window (get-buffer-window agenda-buffer)))))
8689 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8690 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8691 (org-agenda-check-no-diary)
8692 (let* ((marker (or (org-get-at-bol 'org-marker)
8693 (org-agenda-error)))
8694 (buffer (marker-buffer marker))
8695 (pos (marker-position marker)))
8696 (with-current-buffer buffer
8697 (save-excursion
8698 (goto-char pos)
8699 (funcall 'org-tree-to-indirect-buffer arg)))))
8701 (defvar org-last-heading-marker (make-marker)
8702 "Marker pointing to the headline that last changed its TODO state
8703 by a remote command from the agenda.")
8705 (defun org-agenda-todo-nextset ()
8706 "Switch TODO entry to next sequence."
8707 (interactive)
8708 (org-agenda-todo 'nextset))
8710 (defun org-agenda-todo-previousset ()
8711 "Switch TODO entry to previous sequence."
8712 (interactive)
8713 (org-agenda-todo 'previousset))
8715 (defun org-agenda-todo (&optional arg)
8716 "Cycle TODO state of line at point, also in Org-mode file.
8717 This changes the line at point, all other lines in the agenda referring to
8718 the same tree node, and the headline of the tree node in the Org-mode file."
8719 (interactive "P")
8720 (org-agenda-check-no-diary)
8721 (let* ((col (current-column))
8722 (marker (or (org-get-at-bol 'org-marker)
8723 (org-agenda-error)))
8724 (buffer (marker-buffer marker))
8725 (pos (marker-position marker))
8726 (hdmarker (org-get-at-bol 'org-hd-marker))
8727 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8728 (inhibit-read-only t)
8729 org-agenda-headline-snapshot-before-repeat newhead just-one)
8730 (org-with-remote-undo buffer
8731 (with-current-buffer buffer
8732 (widen)
8733 (goto-char pos)
8734 (org-show-context 'agenda)
8735 (save-excursion
8736 (and (outline-next-heading)
8737 (org-flag-heading nil))) ; show the next heading
8738 (let ((current-prefix-arg arg))
8739 (call-interactively 'org-todo))
8740 (and (bolp) (forward-char 1))
8741 (setq newhead (org-get-heading))
8742 (when (and (org-bound-and-true-p
8743 org-agenda-headline-snapshot-before-repeat)
8744 (not (equal org-agenda-headline-snapshot-before-repeat
8745 newhead))
8746 todayp)
8747 (setq newhead org-agenda-headline-snapshot-before-repeat
8748 just-one t))
8749 (save-excursion
8750 (org-back-to-heading)
8751 (move-marker org-last-heading-marker (point))))
8752 (beginning-of-line 1)
8753 (save-excursion
8754 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8755 (org-move-to-column col))))
8757 (defun org-agenda-add-note (&optional arg)
8758 "Add a time-stamped note to the entry at point."
8759 (interactive "P")
8760 (org-agenda-check-no-diary)
8761 (let* ((marker (or (org-get-at-bol 'org-marker)
8762 (org-agenda-error)))
8763 (buffer (marker-buffer marker))
8764 (pos (marker-position marker))
8765 (hdmarker (org-get-at-bol 'org-hd-marker))
8766 (inhibit-read-only t))
8767 (with-current-buffer buffer
8768 (widen)
8769 (goto-char pos)
8770 (org-show-context 'agenda)
8771 (save-excursion
8772 (and (outline-next-heading)
8773 (org-flag-heading nil))) ; show the next heading
8774 (org-add-note))))
8776 (defun org-agenda-change-all-lines (newhead hdmarker
8777 &optional fixface just-this)
8778 "Change all lines in the agenda buffer which match HDMARKER.
8779 The new content of the line will be NEWHEAD (as modified by
8780 `org-agenda-format-item'). HDMARKER is checked with
8781 `equal' against all `org-hd-marker' text properties in the file.
8782 If FIXFACE is non-nil, the face of each item is modified according to
8783 the new TODO state.
8784 If JUST-THIS is non-nil, change just the current line, not all.
8785 If FORCE-TAGS is non nil, the car of it returns the new tags."
8786 (let* ((inhibit-read-only t)
8787 (line (org-current-line))
8788 (org-agenda-buffer (current-buffer))
8789 (thetags (with-current-buffer (marker-buffer hdmarker)
8790 (save-excursion (save-restriction (widen)
8791 (goto-char hdmarker)
8792 (org-get-tags-at)))))
8793 props m pl undone-face done-face finish new dotime level cat tags)
8794 (save-excursion
8795 (goto-char (point-max))
8796 (beginning-of-line 1)
8797 (while (not finish)
8798 (setq finish (bobp))
8799 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8800 (or (not just-this) (= (org-current-line) line))
8801 (equal m hdmarker))
8802 (setq props (text-properties-at (point))
8803 dotime (org-get-at-bol 'dotime)
8804 cat (org-get-at-bol 'org-category)
8805 level (org-get-at-bol 'level)
8806 tags thetags
8808 (let ((org-prefix-format-compiled
8809 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8810 org-prefix-format-compiled))
8811 (extra (org-get-at-bol 'extra)))
8812 (with-current-buffer (marker-buffer hdmarker)
8813 (save-excursion
8814 (save-restriction
8815 (widen)
8816 (org-agenda-format-item extra newhead level cat tags dotime)))))
8817 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8818 undone-face (org-get-at-bol 'undone-face)
8819 done-face (org-get-at-bol 'done-face))
8820 (beginning-of-line 1)
8821 (cond
8822 ((equal new "")
8823 (and (looking-at ".*\n?") (replace-match "")))
8824 ((looking-at ".*")
8825 (replace-match new t t)
8826 (beginning-of-line 1)
8827 (add-text-properties (point-at-bol) (point-at-eol) props)
8828 (when fixface
8829 (add-text-properties
8830 (point-at-bol) (point-at-eol)
8831 (list 'face
8832 (if org-last-todo-state-is-todo
8833 undone-face done-face))))
8834 (org-agenda-highlight-todo 'line)
8835 (beginning-of-line 1))
8836 (t (error "Line update did not work")))
8837 (save-restriction
8838 (narrow-to-region (point-at-bol) (point-at-eol))
8839 (org-agenda-finalize)))
8840 (beginning-of-line 0)))))
8842 (defun org-agenda-align-tags (&optional line)
8843 "Align all tags in agenda items to `org-agenda-tags-column'."
8844 (let ((inhibit-read-only t) l c)
8845 (save-excursion
8846 (goto-char (if line (point-at-bol) (point-min)))
8847 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8848 (if line (point-at-eol) nil) t)
8849 (add-text-properties
8850 (match-beginning 2) (match-end 2)
8851 (list 'face (delq nil (let ((prop (get-text-property
8852 (match-beginning 2) 'face)))
8853 (or (listp prop) (setq prop (list prop)))
8854 (if (memq 'org-tag prop)
8855 prop
8856 (cons 'org-tag prop))))))
8857 (setq l (- (match-end 2) (match-beginning 2))
8858 c (if (< org-agenda-tags-column 0)
8859 (- (abs org-agenda-tags-column) l)
8860 org-agenda-tags-column))
8861 (delete-region (match-beginning 1) (match-end 1))
8862 (goto-char (match-beginning 1))
8863 (insert (org-add-props
8864 (make-string (max 1 (- c (current-column))) ?\ )
8865 (plist-put (copy-sequence (text-properties-at (point)))
8866 'face nil))))
8867 (goto-char (point-min))
8868 (org-font-lock-add-tag-faces (point-max)))))
8870 (defun org-agenda-priority-up ()
8871 "Increase the priority of line at point, also in Org-mode file."
8872 (interactive)
8873 (org-agenda-priority 'up))
8875 (defun org-agenda-priority-down ()
8876 "Decrease the priority of line at point, also in Org-mode file."
8877 (interactive)
8878 (org-agenda-priority 'down))
8880 (defun org-agenda-priority (&optional force-direction)
8881 "Set the priority of line at point, also in Org-mode file.
8882 This changes the line at point, all other lines in the agenda referring to
8883 the same tree node, and the headline of the tree node in the Org-mode file.
8884 Called with a universal prefix arg, show the priority instead of setting it."
8885 (interactive "P")
8886 (if (equal force-direction '(4))
8887 (org-show-priority)
8888 (unless org-enable-priority-commands
8889 (error "Priority commands are disabled"))
8890 (org-agenda-check-no-diary)
8891 (let* ((marker (or (org-get-at-bol 'org-marker)
8892 (org-agenda-error)))
8893 (hdmarker (org-get-at-bol 'org-hd-marker))
8894 (buffer (marker-buffer hdmarker))
8895 (pos (marker-position hdmarker))
8896 (inhibit-read-only t)
8897 newhead)
8898 (org-with-remote-undo buffer
8899 (with-current-buffer buffer
8900 (widen)
8901 (goto-char pos)
8902 (org-show-context 'agenda)
8903 (save-excursion
8904 (and (outline-next-heading)
8905 (org-flag-heading nil))) ; show the next heading
8906 (funcall 'org-priority force-direction)
8907 (end-of-line 1)
8908 (setq newhead (org-get-heading)))
8909 (org-agenda-change-all-lines newhead hdmarker)
8910 (beginning-of-line 1)))))
8912 ;; FIXME: should fix the tags property of the agenda line.
8913 (defun org-agenda-set-tags (&optional tag onoff)
8914 "Set tags for the current headline."
8915 (interactive)
8916 (org-agenda-check-no-diary)
8917 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8918 (call-interactively 'org-change-tag-in-region)
8919 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8920 (org-agenda-error)))
8921 (buffer (marker-buffer hdmarker))
8922 (pos (marker-position hdmarker))
8923 (inhibit-read-only t)
8924 newhead)
8925 (org-with-remote-undo buffer
8926 (with-current-buffer buffer
8927 (widen)
8928 (goto-char pos)
8929 (save-excursion
8930 (org-show-context 'agenda))
8931 (save-excursion
8932 (and (outline-next-heading)
8933 (org-flag-heading nil))) ; show the next heading
8934 (goto-char pos)
8935 (if tag
8936 (org-toggle-tag tag onoff)
8937 (call-interactively 'org-set-tags))
8938 (end-of-line 1)
8939 (setq newhead (org-get-heading)))
8940 (org-agenda-change-all-lines newhead hdmarker)
8941 (beginning-of-line 1)))))
8943 (defun org-agenda-set-property ()
8944 "Set a property for the current headline."
8945 (interactive)
8946 (org-agenda-check-no-diary)
8947 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8948 (org-agenda-error)))
8949 (buffer (marker-buffer hdmarker))
8950 (pos (marker-position hdmarker))
8951 (inhibit-read-only t)
8952 newhead)
8953 (org-with-remote-undo buffer
8954 (with-current-buffer buffer
8955 (widen)
8956 (goto-char pos)
8957 (save-excursion
8958 (org-show-context 'agenda))
8959 (save-excursion
8960 (and (outline-next-heading)
8961 (org-flag-heading nil))) ; show the next heading
8962 (goto-char pos)
8963 (call-interactively 'org-set-property)))))
8965 (defun org-agenda-set-effort ()
8966 "Set the effort property for the current headline."
8967 (interactive)
8968 (org-agenda-check-no-diary)
8969 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8970 (org-agenda-error)))
8971 (buffer (marker-buffer hdmarker))
8972 (pos (marker-position hdmarker))
8973 (inhibit-read-only t)
8974 newhead)
8975 (org-with-remote-undo buffer
8976 (with-current-buffer buffer
8977 (widen)
8978 (goto-char pos)
8979 (save-excursion
8980 (org-show-context 'agenda))
8981 (save-excursion
8982 (and (outline-next-heading)
8983 (org-flag-heading nil))) ; show the next heading
8984 (goto-char pos)
8985 (call-interactively 'org-set-effort)
8986 (end-of-line 1)
8987 (setq newhead (org-get-heading)))
8988 (org-agenda-change-all-lines newhead hdmarker))))
8990 (defun org-agenda-toggle-archive-tag ()
8991 "Toggle the archive tag for the current entry."
8992 (interactive)
8993 (org-agenda-check-no-diary)
8994 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8995 (org-agenda-error)))
8996 (buffer (marker-buffer hdmarker))
8997 (pos (marker-position hdmarker))
8998 (inhibit-read-only t)
8999 newhead)
9000 (org-with-remote-undo buffer
9001 (with-current-buffer buffer
9002 (widen)
9003 (goto-char pos)
9004 (org-show-context 'agenda)
9005 (save-excursion
9006 (and (outline-next-heading)
9007 (org-flag-heading nil))) ; show the next heading
9008 (call-interactively 'org-toggle-archive-tag)
9009 (end-of-line 1)
9010 (setq newhead (org-get-heading)))
9011 (org-agenda-change-all-lines newhead hdmarker)
9012 (beginning-of-line 1))))
9014 (defun org-agenda-do-date-later (arg)
9015 (interactive "P")
9016 (cond
9017 ((or (equal arg '(16))
9018 (memq last-command
9019 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9020 (setq this-command 'org-agenda-date-later-minutes)
9021 (org-agenda-date-later-minutes 1))
9022 ((or (equal arg '(4))
9023 (memq last-command
9024 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9025 (setq this-command 'org-agenda-date-later-hours)
9026 (org-agenda-date-later-hours 1))
9028 (org-agenda-date-later (prefix-numeric-value arg)))))
9030 (defun org-agenda-do-date-earlier (arg)
9031 (interactive "P")
9032 (cond
9033 ((or (equal arg '(16))
9034 (memq last-command
9035 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9036 (setq this-command 'org-agenda-date-earlier-minutes)
9037 (org-agenda-date-earlier-minutes 1))
9038 ((or (equal arg '(4))
9039 (memq last-command
9040 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9041 (setq this-command 'org-agenda-date-earlier-hours)
9042 (org-agenda-date-earlier-hours 1))
9044 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9046 (defun org-agenda-date-later (arg &optional what)
9047 "Change the date of this item to ARG day(s) later."
9048 (interactive "p")
9049 (org-agenda-check-type t 'agenda 'timeline)
9050 (org-agenda-check-no-diary)
9051 (let* ((marker (or (org-get-at-bol 'org-marker)
9052 (org-agenda-error)))
9053 (buffer (marker-buffer marker))
9054 (pos (marker-position marker))
9055 cdate today)
9056 (org-with-remote-undo buffer
9057 (with-current-buffer buffer
9058 (widen)
9059 (goto-char pos)
9060 (if (not (org-at-timestamp-p))
9061 (error "Cannot find time stamp"))
9062 (when (and org-agenda-move-date-from-past-immediately-to-today
9063 (equal arg 1)
9064 (or (not what) (eq what 'day))
9065 (not (save-match-data (org-at-date-range-p))))
9066 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9067 cdate (calendar-absolute-from-gregorian
9068 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9069 today (org-today))
9070 (if (> today cdate)
9071 ;; immediately shift to today
9072 (setq arg (- today cdate))))
9073 (org-timestamp-change arg (or what 'day))
9074 (when (and (org-at-date-range-p)
9075 (re-search-backward org-tr-regexp-both (point-at-bol)))
9076 (let ((end org-last-changed-timestamp))
9077 (org-timestamp-change arg (or what 'day))
9078 (setq org-last-changed-timestamp
9079 (concat org-last-changed-timestamp "--" end)))))
9080 (org-agenda-show-new-time marker org-last-changed-timestamp))
9081 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9083 (defun org-agenda-date-earlier (arg &optional what)
9084 "Change the date of this item to ARG day(s) earlier."
9085 (interactive "p")
9086 (org-agenda-date-later (- arg) what))
9088 (defun org-agenda-date-later-minutes (arg)
9089 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9090 (interactive "p")
9091 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9092 (org-agenda-date-later arg 'minute))
9094 (defun org-agenda-date-earlier-minutes (arg)
9095 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9096 (interactive "p")
9097 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9098 (org-agenda-date-earlier arg 'minute))
9100 (defun org-agenda-date-later-hours (arg)
9101 "Change the time of this item, in hour steps."
9102 (interactive "p")
9103 (org-agenda-date-later arg 'hour))
9105 (defun org-agenda-date-earlier-hours (arg)
9106 "Change the time of this item, in hour steps."
9107 (interactive "p")
9108 (org-agenda-date-earlier arg 'hour))
9110 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9111 "Show new date stamp via text properties."
9112 ;; We use text properties to make this undoable
9113 (let ((inhibit-read-only t))
9114 (setq stamp (concat prefix " => " stamp " "))
9115 (save-excursion
9116 (goto-char (point-max))
9117 (while (not (bobp))
9118 (when (equal marker (org-get-at-bol 'org-marker))
9119 (org-move-to-column (- (window-width) (length stamp)) t)
9120 (org-agenda-fix-tags-filter-overlays-at (point))
9121 (if (featurep 'xemacs)
9122 ;; Use `duplicable' property to trigger undo recording
9123 (let ((ex (make-extent nil nil))
9124 (gl (make-glyph stamp)))
9125 (set-glyph-face gl 'secondary-selection)
9126 (set-extent-properties
9127 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9128 (insert-extent ex (1- (point)) (point-at-eol)))
9129 (add-text-properties
9130 (1- (point)) (point-at-eol)
9131 (list 'display (org-add-props stamp nil
9132 'face 'secondary-selection))))
9133 (beginning-of-line 1))
9134 (beginning-of-line 0)))))
9136 (defun org-agenda-date-prompt (arg)
9137 "Change the date of this item. Date is prompted for, with default today.
9138 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9139 be used to request time specification in the time stamp."
9140 (interactive "P")
9141 (org-agenda-check-type t 'agenda 'timeline)
9142 (org-agenda-check-no-diary)
9143 (let* ((marker (or (org-get-at-bol 'org-marker)
9144 (org-agenda-error)))
9145 (buffer (marker-buffer marker))
9146 (pos (marker-position marker)))
9147 (org-with-remote-undo buffer
9148 (with-current-buffer buffer
9149 (widen)
9150 (goto-char pos)
9151 (if (not (org-at-timestamp-p t))
9152 (error "Cannot find time stamp"))
9153 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9154 (org-agenda-show-new-time marker org-last-changed-timestamp))
9155 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9157 (defun org-agenda-schedule (arg &optional time)
9158 "Schedule the item at point.
9159 ARG is passed through to `org-schedule'."
9160 (interactive "P")
9161 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9162 (org-agenda-check-no-diary)
9163 (let* ((marker (or (org-get-at-bol 'org-marker)
9164 (org-agenda-error)))
9165 (type (marker-insertion-type marker))
9166 (buffer (marker-buffer marker))
9167 (pos (marker-position marker))
9168 (org-insert-labeled-timestamps-at-point nil)
9170 (set-marker-insertion-type marker t)
9171 (org-with-remote-undo buffer
9172 (with-current-buffer buffer
9173 (widen)
9174 (goto-char pos)
9175 (setq ts (org-schedule arg time)))
9176 (org-agenda-show-new-time marker ts " S"))
9177 (message "%s" ts)))
9179 (defun org-agenda-deadline (arg &optional time)
9180 "Schedule the item at point.
9181 ARG is passed through to `org-deadline'."
9182 (interactive "P")
9183 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9184 (org-agenda-check-no-diary)
9185 (let* ((marker (or (org-get-at-bol 'org-marker)
9186 (org-agenda-error)))
9187 (buffer (marker-buffer marker))
9188 (pos (marker-position marker))
9189 (org-insert-labeled-timestamps-at-point nil)
9191 (org-with-remote-undo buffer
9192 (with-current-buffer buffer
9193 (widen)
9194 (goto-char pos)
9195 (setq ts (org-deadline arg time)))
9196 (org-agenda-show-new-time marker ts " D"))
9197 (message "%s" ts)))
9199 (defun org-agenda-clock-in (&optional arg)
9200 "Start the clock on the currently selected item."
9201 (interactive "P")
9202 (org-agenda-check-no-diary)
9203 (if (equal arg '(4))
9204 (org-clock-in arg)
9205 (let* ((marker (or (org-get-at-bol 'org-marker)
9206 (org-agenda-error)))
9207 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9208 (pos (marker-position marker))
9209 (col (current-column))
9210 newhead)
9211 (org-with-remote-undo (marker-buffer marker)
9212 (with-current-buffer (marker-buffer marker)
9213 (widen)
9214 (goto-char pos)
9215 (org-show-context 'agenda)
9216 (org-show-entry)
9217 (org-cycle-hide-drawers 'children)
9218 (org-clock-in arg)
9219 (setq newhead (org-get-heading)))
9220 (org-agenda-change-all-lines newhead hdmarker))
9221 (org-move-to-column col))))
9223 (defun org-agenda-clock-out ()
9224 "Stop the currently running clock."
9225 (interactive)
9226 (unless (marker-buffer org-clock-marker)
9227 (error "No running clock"))
9228 (let ((marker (make-marker)) (col (current-column)) newhead)
9229 (org-with-remote-undo (marker-buffer org-clock-marker)
9230 (with-current-buffer (marker-buffer org-clock-marker)
9231 (save-excursion
9232 (save-restriction
9233 (widen)
9234 (goto-char org-clock-marker)
9235 (org-back-to-heading t)
9236 (move-marker marker (point))
9237 (org-clock-out)
9238 (setq newhead (org-get-heading))))))
9239 (org-agenda-change-all-lines newhead marker)
9240 (move-marker marker nil)
9241 (org-move-to-column col)
9242 (org-agenda-unmark-clocking-task)))
9244 (defun org-agenda-clock-cancel (&optional arg)
9245 "Cancel the currently running clock."
9246 (interactive "P")
9247 (unless (marker-buffer org-clock-marker)
9248 (user-error "No running clock"))
9249 (org-with-remote-undo (marker-buffer org-clock-marker)
9250 (org-clock-cancel)))
9252 (defun org-agenda-clock-goto ()
9253 "Jump to the currently clocked in task within the agenda.
9254 If the currently clocked in task is not listed in the agenda
9255 buffer, display it in another window."
9256 (interactive)
9257 (let (pos)
9258 (mapc (lambda (o)
9259 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9260 (setq pos (overlay-start o))))
9261 (overlays-in (point-min) (point-max)))
9262 (cond (pos (goto-char pos))
9263 ;; If the currently clocked entry is not in the agenda
9264 ;; buffer, we visit it in another window:
9265 (org-clock-current-task
9266 (org-switch-to-buffer-other-window (org-clock-goto)))
9267 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9269 (defun org-agenda-diary-entry-in-org-file ()
9270 "Make a diary entry in the file `org-agenda-diary-file'."
9271 (let (d1 d2 char (text "") dp1 dp2)
9272 (if (equal (buffer-name) "*Calendar*")
9273 (setq d1 (calendar-cursor-to-date t)
9274 d2 (car calendar-mark-ring))
9275 (setq dp1 (get-text-property (point-at-bol) 'day))
9276 (unless dp1 (user-error "No date defined in current line"))
9277 (setq d1 (calendar-gregorian-from-absolute dp1)
9278 d2 (and (ignore-errors (mark))
9279 (save-excursion
9280 (goto-char (mark))
9281 (setq dp2 (get-text-property (point-at-bol) 'day)))
9282 (calendar-gregorian-from-absolute dp2))))
9283 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9284 (setq char (read-char-exclusive))
9285 (cond
9286 ((equal char ?d)
9287 (setq text (read-string "Day entry: "))
9288 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9289 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9290 ((equal char ?a)
9291 (setq d1 (list (car d1) (nth 1 d1)
9292 (read-number (format "Reference year [%d]: " (nth 2 d1))
9293 (nth 2 d1))))
9294 (setq text (read-string "Anniversary (use %d to show years): "))
9295 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9296 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9297 ((equal char ?b)
9298 (setq text (read-string "Block entry: "))
9299 (unless (and d1 d2 (not (equal d1 d2)))
9300 (user-error "No block of days selected"))
9301 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9302 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9303 ((equal char ?j)
9304 (org-switch-to-buffer-other-window
9305 (find-file-noselect org-agenda-diary-file))
9306 (require 'org-datetree)
9307 (org-datetree-find-date-create d1)
9308 (org-reveal t))
9309 (t (user-error "Invalid selection character `%c'" char)))))
9311 (defcustom org-agenda-insert-diary-strategy 'date-tree
9312 "Where in `org-agenda-diary-file' should new entries be added?
9313 Valid values:
9315 date-tree in the date tree, as child of the date
9316 top-level as top-level entries at the end of the file."
9317 :group 'org-agenda
9318 :type '(choice
9319 (const :tag "in a date tree" date-tree)
9320 (const :tag "as top level at end of file" top-level)))
9322 (defcustom org-agenda-insert-diary-extract-time nil
9323 "Non-nil means extract any time specification from the diary entry."
9324 :group 'org-agenda
9325 :version "24.1"
9326 :type 'boolean)
9328 (defcustom org-agenda-bulk-mark-char ">"
9329 "A single-character string to be used as the bulk mark."
9330 :group 'org-agenda
9331 :version "24.1"
9332 :type 'string)
9334 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9335 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9336 If TEXT is not empty, it will become the headline of the new entry, and
9337 the resulting entry will not be shown. When TEXT is empty, switch to
9338 `org-agenda-diary-file' and let the user finish the entry there."
9339 (let ((cw (current-window-configuration)))
9340 (org-switch-to-buffer-other-window
9341 (find-file-noselect org-agenda-diary-file))
9342 (widen)
9343 (goto-char (point-min))
9344 (cond
9345 ((eq type 'anniversary)
9346 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9347 (progn
9348 (or (org-at-heading-p t)
9349 (progn
9350 (outline-next-heading)
9351 (insert "* Anniversaries\n\n")
9352 (beginning-of-line -1)))))
9353 (outline-next-heading)
9354 (org-back-over-empty-lines)
9355 (backward-char 1)
9356 (insert "\n")
9357 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9358 (nth 2 d1) (car d1) (nth 1 d1) text)))
9359 ((eq type 'day)
9360 (let ((org-prefix-has-time t)
9361 (org-agenda-time-leading-zero t)
9362 fmt time time2)
9363 (if org-agenda-insert-diary-extract-time
9364 ;; Use org-agenda-format-item to parse text for a time-range and
9365 ;; remove it. FIXME: This is a hack, we should refactor
9366 ;; that function to make time extraction available separately
9367 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9368 time (get-text-property 0 'time fmt)
9369 time2 (if (> (length time) 0)
9370 ;; split-string removes trailing ...... if
9371 ;; no end time given. First space
9372 ;; separates time from date.
9373 (concat " " (car (split-string time "\\.")))
9374 nil)
9375 text (get-text-property 0 'txt fmt)))
9376 (if (eq org-agenda-insert-diary-strategy 'top-level)
9377 (org-agenda-insert-diary-as-top-level text)
9378 (require 'org-datetree)
9379 (org-datetree-find-date-create d1)
9380 (org-agenda-insert-diary-make-new-entry text))
9381 (org-insert-time-stamp (org-time-from-absolute
9382 (calendar-absolute-from-gregorian d1))
9383 nil nil nil nil time2))
9384 (end-of-line 0))
9385 ((eq type 'block)
9386 (if (> (calendar-absolute-from-gregorian d1)
9387 (calendar-absolute-from-gregorian d2))
9388 (setq d1 (prog1 d2 (setq d2 d1))))
9389 (if (eq org-agenda-insert-diary-strategy 'top-level)
9390 (org-agenda-insert-diary-as-top-level text)
9391 (require 'org-datetree)
9392 (org-datetree-find-date-create d1)
9393 (org-agenda-insert-diary-make-new-entry text))
9394 (org-insert-time-stamp (org-time-from-absolute
9395 (calendar-absolute-from-gregorian d1)))
9396 (insert "--")
9397 (org-insert-time-stamp (org-time-from-absolute
9398 (calendar-absolute-from-gregorian d2)))
9399 (end-of-line 0)))
9400 (if (string-match "\\S-" text)
9401 (progn
9402 (set-window-configuration cw)
9403 (message "%s entry added to %s"
9404 (capitalize (symbol-name type))
9405 (abbreviate-file-name org-agenda-diary-file)))
9406 (org-reveal t)
9407 (message "Please finish entry here"))))
9409 (defun org-agenda-insert-diary-as-top-level (text)
9410 "Make new entry as a top-level entry at the end of the file.
9411 Add TEXT as headline, and position the cursor in the second line so that
9412 a timestamp can be added there."
9413 (widen)
9414 (goto-char (point-max))
9415 (or (bolp) (insert "\n"))
9416 (insert "* " text "\n")
9417 (if org-adapt-indentation (org-indent-to-column 2)))
9419 (defun org-agenda-insert-diary-make-new-entry (text)
9420 "Make new entry as last child of current entry.
9421 Add TEXT as headline, and position the cursor in the second line so that
9422 a timestamp can be added there."
9423 (let ((org-show-following-heading t)
9424 (org-show-siblings t)
9425 (org-show-hierarchy-above t)
9426 (org-show-entry-below t)
9427 col)
9428 (outline-next-heading)
9429 (org-back-over-empty-lines)
9430 (or (looking-at "[ \t]*$")
9431 (progn (insert "\n") (backward-char 1)))
9432 (org-insert-heading nil t)
9433 (org-do-demote)
9434 (setq col (current-column))
9435 (insert text "\n")
9436 (if org-adapt-indentation (org-indent-to-column col))
9437 (let ((org-show-following-heading t)
9438 (org-show-siblings t)
9439 (org-show-hierarchy-above t)
9440 (org-show-entry-below t))
9441 (org-show-context))))
9443 (defun org-agenda-diary-entry ()
9444 "Make a diary entry, like the `i' command from the calendar.
9445 All the standard commands work: block, weekly etc.
9446 When `org-agenda-diary-file' points to a file,
9447 `org-agenda-diary-entry-in-org-file' is called instead to create
9448 entries in that Org-mode file."
9449 (interactive)
9450 (if (not (eq org-agenda-diary-file 'diary-file))
9451 (org-agenda-diary-entry-in-org-file)
9452 (require 'diary-lib)
9453 (let* ((char (progn
9454 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9455 (read-char-exclusive)))
9456 (cmd (cdr (assoc char
9457 '((?d . insert-diary-entry)
9458 (?w . insert-weekly-diary-entry)
9459 (?m . insert-monthly-diary-entry)
9460 (?y . insert-yearly-diary-entry)
9461 (?a . insert-anniversary-diary-entry)
9462 (?b . insert-block-diary-entry)
9463 (?c . insert-cyclic-diary-entry)))))
9464 (oldf (symbol-function 'calendar-cursor-to-date))
9465 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9466 (point (point))
9467 (mark (or (mark t) (point))))
9468 (unless cmd
9469 (user-error "No command associated with <%c>" char))
9470 (unless (and (get-text-property point 'day)
9471 (or (not (equal ?b char))
9472 (get-text-property mark 'day)))
9473 (user-error "Don't know which date to use for diary entry"))
9474 ;; We implement this by hacking the `calendar-cursor-to-date' function
9475 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9476 (let ((calendar-mark-ring
9477 (list (calendar-gregorian-from-absolute
9478 (or (get-text-property mark 'day)
9479 (get-text-property point 'day))))))
9480 (unwind-protect
9481 (progn
9482 (fset 'calendar-cursor-to-date
9483 (lambda (&optional error dummy)
9484 (calendar-gregorian-from-absolute
9485 (get-text-property point 'day))))
9486 (call-interactively cmd))
9487 (fset 'calendar-cursor-to-date oldf))))))
9489 (defun org-agenda-execute-calendar-command (cmd)
9490 "Execute a calendar command from the agenda with date from cursor."
9491 (org-agenda-check-type t 'agenda 'timeline)
9492 (require 'diary-lib)
9493 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9494 (user-error "Don't know which date to use for the calendar command"))
9495 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9496 (point (point))
9497 (date (calendar-gregorian-from-absolute
9498 (get-text-property point 'day)))
9499 ;; the following 2 vars are needed in the calendar
9500 (displayed-month (car date))
9501 (displayed-year (nth 2 date)))
9502 (unwind-protect
9503 (progn
9504 (fset 'calendar-cursor-to-date
9505 (lambda (&optional error dummy)
9506 (calendar-gregorian-from-absolute
9507 (get-text-property point 'day))))
9508 (call-interactively cmd))
9509 (fset 'calendar-cursor-to-date oldf))))
9511 (defun org-agenda-phases-of-moon ()
9512 "Display the phases of the moon for the 3 months around the cursor date."
9513 (interactive)
9514 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9516 (defun org-agenda-holidays ()
9517 "Display the holidays for the 3 months around the cursor date."
9518 (interactive)
9519 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9521 (defvar calendar-longitude) ; defined in calendar.el
9522 (defvar calendar-latitude) ; defined in calendar.el
9523 (defvar calendar-location-name) ; defined in calendar.el
9525 (defun org-agenda-sunrise-sunset (arg)
9526 "Display sunrise and sunset for the cursor date.
9527 Latitude and longitude can be specified with the variables
9528 `calendar-latitude' and `calendar-longitude'. When called with prefix
9529 argument, latitude and longitude will be prompted for."
9530 (interactive "P")
9531 (require 'solar)
9532 (let ((calendar-longitude (if arg nil calendar-longitude))
9533 (calendar-latitude (if arg nil calendar-latitude))
9534 (calendar-location-name
9535 (if arg "the given coordinates" calendar-location-name)))
9536 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9538 (defun org-agenda-goto-calendar ()
9539 "Open the Emacs calendar with the date at the cursor."
9540 (interactive)
9541 (org-agenda-check-type t 'agenda 'timeline)
9542 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9543 (user-error "Don't know which date to open in calendar")))
9544 (date (calendar-gregorian-from-absolute day))
9545 (calendar-move-hook nil)
9546 (calendar-view-holidays-initially-flag nil)
9547 (calendar-view-diary-initially-flag nil))
9548 (calendar)
9549 (calendar-goto-date date)))
9551 ;;;###autoload
9552 (defun org-calendar-goto-agenda ()
9553 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9554 This is a command that has to be installed in `calendar-mode-map'."
9555 (interactive)
9556 (org-agenda-list nil (calendar-absolute-from-gregorian
9557 (calendar-cursor-to-date))
9558 nil))
9560 (defun org-agenda-convert-date ()
9561 (interactive)
9562 (org-agenda-check-type t 'agenda 'timeline)
9563 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9564 date s)
9565 (unless day
9566 (user-error "Don't know which date to convert"))
9567 (setq date (calendar-gregorian-from-absolute day))
9568 (setq s (concat
9569 "Gregorian: " (calendar-date-string date) "\n"
9570 "ISO: " (calendar-iso-date-string date) "\n"
9571 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9572 "Julian: " (calendar-julian-date-string date) "\n"
9573 "Astron. JD: " (calendar-astro-date-string date)
9574 " (Julian date number at noon UTC)\n"
9575 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9576 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9577 "French: " (calendar-french-date-string date) "\n"
9578 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9579 "Mayan: " (calendar-mayan-date-string date) "\n"
9580 "Coptic: " (calendar-coptic-date-string date) "\n"
9581 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9582 "Persian: " (calendar-persian-date-string date) "\n"
9583 "Chinese: " (calendar-chinese-date-string date) "\n"))
9584 (with-output-to-temp-buffer "*Dates*"
9585 (princ s))
9586 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9588 ;;; Bulk commands
9590 (defun org-agenda-bulk-marked-p ()
9591 (eq (get-char-property (point-at-bol) 'type)
9592 'org-marked-entry-overlay))
9594 (defun org-agenda-bulk-mark (&optional arg)
9595 "Mark the entry at point for future bulk action."
9596 (interactive "p")
9597 (dotimes (i (or arg 1))
9598 (unless (org-get-at-bol 'org-agenda-diary-link)
9599 (let* ((m (org-get-at-bol 'org-hd-marker))
9601 (unless (org-agenda-bulk-marked-p)
9602 (unless m (user-error "Nothing to mark at point"))
9603 (push m org-agenda-bulk-marked-entries)
9604 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9605 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9606 (org-get-todo-face "TODO")
9607 'evaporate)
9608 (overlay-put ov 'type 'org-marked-entry-overlay))
9609 (beginning-of-line 2)
9610 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9611 (beginning-of-line 2))
9612 (message "%d entries marked for bulk action"
9613 (length org-agenda-bulk-marked-entries))))))
9615 (defun org-agenda-bulk-mark-all ()
9616 "Mark all entries for future agenda bulk action."
9617 (interactive)
9618 (org-agenda-bulk-mark-regexp "."))
9620 (defun org-agenda-bulk-mark-regexp (regexp)
9621 "Mark entries matching REGEXP for future agenda bulk action."
9622 (interactive "sMark entries matching regexp: ")
9623 (let ((entries-marked 0))
9624 (save-excursion
9625 (goto-char (point-min))
9626 (goto-char (next-single-property-change (point) 'txt))
9627 (while (re-search-forward regexp nil t)
9628 (when (string-match regexp (get-text-property (point) 'txt))
9629 (setq entries-marked (1+ entries-marked))
9630 (call-interactively 'org-agenda-bulk-mark))))
9631 (if (not entries-marked)
9632 (message "No entry matching this regexp."))))
9634 (defun org-agenda-bulk-unmark (&optional arg)
9635 "Unmark the entry at point for future bulk action."
9636 (interactive "P")
9637 (if arg
9638 (org-agenda-bulk-unmark-all)
9639 (cond ((org-agenda-bulk-marked-p)
9640 (org-agenda-bulk-remove-overlays
9641 (point-at-bol) (+ 2 (point-at-bol)))
9642 (setq org-agenda-bulk-marked-entries
9643 (delete (org-get-at-bol 'org-hd-marker)
9644 org-agenda-bulk-marked-entries))
9645 (beginning-of-line 2)
9646 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9647 (beginning-of-line 2))
9648 (message "%d entries left marked for bulk action"
9649 (length org-agenda-bulk-marked-entries)))
9650 (t (message "No entry to unmark here")))))
9652 (defun org-agenda-bulk-toggle ()
9653 "Toggle marking the entry at point for bulk action."
9654 (interactive)
9655 (if (org-agenda-bulk-marked-p)
9656 (org-agenda-bulk-unmark)
9657 (org-agenda-bulk-mark)))
9659 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9660 "Remove the mark overlays between BEG and END in the agenda buffer.
9661 BEG and END default to the buffer limits.
9663 This only removes the overlays, it does not remove the markers
9664 from the list in `org-agenda-bulk-marked-entries'."
9665 (interactive)
9666 (mapc (lambda (ov)
9667 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9668 (delete-overlay ov)))
9669 (overlays-in (or beg (point-min)) (or end (point-max)))))
9671 (defun org-agenda-bulk-unmark-all ()
9672 "Remove all marks in the agenda buffer.
9673 This will remove the markers and the overlays."
9674 (interactive)
9675 (if (null org-agenda-bulk-marked-entries)
9676 (message "No entry to unmark")
9677 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9678 (setq org-agenda-bulk-marked-entries nil)
9679 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9681 (defcustom org-agenda-persistent-marks nil
9682 "Non-nil means marked items will stay marked after a bulk action.
9683 You can toggle this interactively by typing `p' when prompted for a
9684 bulk action."
9685 :group 'org-agenda
9686 :version "24.1"
9687 :type 'boolean)
9689 (defun org-agenda-bulk-action (&optional arg)
9690 "Execute an remote-editing action on all marked entries.
9691 The prefix arg is passed through to the command if possible."
9692 (interactive "P")
9693 ;; Make sure we have markers, and only valid ones
9694 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9695 (mapc
9696 (lambda (m)
9697 (unless (and (markerp m)
9698 (marker-buffer m)
9699 (buffer-live-p (marker-buffer m))
9700 (marker-position m))
9701 (user-error "Marker %s for bulk command is invalid" m)))
9702 org-agenda-bulk-marked-entries)
9704 ;; Prompt for the bulk command
9705 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9706 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9707 "[S]catter [f]unction "
9708 (when org-agenda-bulk-custom-functions
9709 (concat " Custom: ["
9710 (mapconcat (lambda(f) (char-to-string (car f)))
9711 org-agenda-bulk-custom-functions "")
9712 "]"))))
9713 (catch 'exit
9714 (let* ((action (read-char-exclusive))
9715 (org-log-refile (if org-log-refile 'time nil))
9716 (entries (reverse org-agenda-bulk-marked-entries))
9717 (org-overriding-default-time
9718 (if (get-text-property (point) 'org-agenda-date-header)
9719 (org-get-cursor-date)))
9720 redo-at-end
9721 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9722 (cond
9723 ((equal action ?p)
9724 (let ((org-agenda-persistent-marks
9725 (not org-agenda-persistent-marks)))
9726 (org-agenda-bulk-action)
9727 (throw 'exit nil)))
9729 ((equal action ?$)
9730 (setq cmd '(org-agenda-archive)))
9732 ((equal action ?A)
9733 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9735 ((member action '(?r ?w))
9736 (setq rfloc (org-refile-get-location
9737 "Refile to"
9738 (marker-buffer (car entries))
9739 org-refile-allow-creating-parent-nodes))
9740 (if (nth 3 rfloc)
9741 (setcar (nthcdr 3 rfloc)
9742 (move-marker (make-marker) (nth 3 rfloc)
9743 (or (get-file-buffer (nth 1 rfloc))
9744 (find-buffer-visiting (nth 1 rfloc))
9745 (error "This should not happen")))))
9747 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9748 redo-at-end t))
9750 ((equal action ?t)
9751 (setq state (org-icompleting-read
9752 "Todo state: "
9753 (with-current-buffer (marker-buffer (car entries))
9754 (mapcar 'list org-todo-keywords-1))))
9755 (setq cmd `(let ((org-inhibit-blocking t)
9756 (org-inhibit-logging 'note))
9757 (org-agenda-todo ,state))))
9759 ((memq action '(?- ?+))
9760 (setq tag (org-icompleting-read
9761 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9762 (with-current-buffer (marker-buffer (car entries))
9763 (delq nil
9764 (mapcar (lambda (x)
9765 (if (stringp (car x)) x)) org-tag-alist)))))
9766 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9768 ((memq action '(?s ?d))
9769 (let* ((time
9770 (unless arg
9771 (org-read-date
9772 nil nil nil
9773 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9774 org-overriding-default-time)))
9775 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9776 (setq cmd `(eval '(,c1 arg ,time)))))
9778 ((equal action ?S)
9779 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9780 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9781 (let ((days (read-number
9782 (format "Scatter tasks across how many %sdays: "
9783 (if arg "week" "")) 7)))
9784 (setq cmd
9785 `(let ((distance (1+ (random ,days))))
9786 (if arg
9787 (let ((dist distance)
9788 (day-of-week
9789 (calendar-day-of-week
9790 (calendar-gregorian-from-absolute (org-today)))))
9791 (dotimes (i (1+ dist))
9792 (while (member day-of-week org-agenda-weekend-days)
9793 (incf distance)
9794 (incf day-of-week)
9795 (if (= day-of-week 7)
9796 (setq day-of-week 0)))
9797 (incf day-of-week)
9798 (if (= day-of-week 7)
9799 (setq day-of-week 0)))))
9800 ;; silently fail when try to replan a sexp entry
9801 (condition-case nil
9802 (let* ((date (calendar-gregorian-from-absolute
9803 (+ (org-today) distance)))
9804 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9805 (nth 2 date))))
9806 (org-agenda-schedule nil time))
9807 (error nil)))))))
9809 ((assoc action org-agenda-bulk-custom-functions)
9810 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9811 redo-at-end t))
9813 ((equal action ?f)
9814 (setq cmd (list (intern
9815 (org-icompleting-read "Function: "
9816 obarray 'fboundp t nil nil)))))
9818 (t (user-error "Invalid bulk action")))
9820 ;; Sort the markers, to make sure that parents are handled before children
9821 (setq entries (sort entries
9822 (lambda (a b)
9823 (cond
9824 ((equal (marker-buffer a) (marker-buffer b))
9825 (< (marker-position a) (marker-position b)))
9827 (string< (buffer-name (marker-buffer a))
9828 (buffer-name (marker-buffer b))))))))
9830 ;; Now loop over all markers and apply cmd
9831 (while (setq e (pop entries))
9832 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9833 (if (not pos)
9834 (progn (message "Skipping removed entry at %s" e)
9835 (setq cntskip (1+ cntskip)))
9836 (goto-char pos)
9837 (let (org-loop-over-headlines-in-active-region)
9838 (eval cmd))
9839 (setq cnt (1+ cnt))))
9840 (when redo-at-end (org-agenda-redo))
9841 (unless org-agenda-persistent-marks
9842 (org-agenda-bulk-unmark-all))
9843 (message "Acted on %d entries%s%s"
9845 (if (= cntskip 0)
9847 (format ", skipped %d (disappeared before their turn)"
9848 cntskip))
9849 (if (not org-agenda-persistent-marks)
9850 "" " (kept marked)"))))))
9852 (defun org-agenda-capture (&optional with-time)
9853 "Call `org-capture' with the date at point.
9854 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9855 current HH:MM time."
9856 (interactive "P")
9857 (if (not (eq major-mode 'org-agenda-mode))
9858 (user-error "You cannot do this outside of agenda buffers")
9859 (let ((org-overriding-default-time
9860 (org-get-cursor-date (equal with-time 1))))
9861 (call-interactively 'org-capture))))
9863 ;;; Flagging notes
9865 (defun org-agenda-show-the-flagging-note ()
9866 "Display the flagging note in the other window.
9867 When called a second time in direct sequence, offer to remove the FLAGGING
9868 tag and (if present) the flagging note."
9869 (interactive)
9870 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9871 (win (selected-window))
9872 note heading newhead)
9873 (unless hdmarker
9874 (user-error "No linked entry at point"))
9875 (if (and (eq this-command last-command)
9876 (y-or-n-p "Unflag and remove any flagging note? "))
9877 (progn
9878 (org-agenda-remove-flag hdmarker)
9879 (let ((win (get-buffer-window "*Flagging Note*")))
9880 (and win (delete-window win)))
9881 (message "Entry unflagged"))
9882 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9883 (unless note
9884 (user-error "No flagging note"))
9885 (org-kill-new note)
9886 (org-switch-to-buffer-other-window "*Flagging Note*")
9887 (erase-buffer)
9888 (insert note)
9889 (goto-char (point-min))
9890 (while (re-search-forward "\\\\n" nil t)
9891 (replace-match "\n" t t))
9892 (goto-char (point-min))
9893 (select-window win)
9894 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9896 (defun org-agenda-remove-flag (marker)
9897 "Remove the FLAGGED tag and any flagging note in the entry."
9898 (let (newhead)
9899 (org-with-point-at marker
9900 (org-toggle-tag "FLAGGED" 'off)
9901 (org-entry-delete nil "THEFLAGGINGNOTE")
9902 (setq newhead (org-get-heading)))
9903 (org-agenda-change-all-lines newhead marker)
9904 (message "Entry unflagged")))
9906 (defun org-agenda-get-any-marker (&optional pos)
9907 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9908 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9910 ;;; Appointment reminders
9912 (defvar appt-time-msg-list) ; defined in appt.el
9914 ;;;###autoload
9915 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9916 "Activate appointments found in `org-agenda-files'.
9917 With a \\[universal-argument] prefix, refresh the list of
9918 appointments.
9920 If FILTER is t, interactively prompt the user for a regular
9921 expression, and filter out entries that don't match it.
9923 If FILTER is a string, use this string as a regular expression
9924 for filtering entries out.
9926 If FILTER is a function, filter out entries against which
9927 calling the function returns nil. This function takes one
9928 argument: an entry from `org-agenda-get-day-entries'.
9930 FILTER can also be an alist with the car of each cell being
9931 either 'headline or 'category. For example:
9933 '((headline \"IMPORTANT\")
9934 (category \"Work\"))
9936 will only add headlines containing IMPORTANT or headlines
9937 belonging to the \"Work\" category.
9939 ARGS are symbols indicating what kind of entries to consider.
9940 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9941 \(i.e., deadlines and scheduled items with a hh:mm specification)
9942 and :timestamp entries. See the docstring of `org-diary' for
9943 details and examples.
9945 If an entry has a APPT_WARNTIME property, its value will be used
9946 to override `appt-message-warning-time'."
9947 (interactive "P")
9948 (if refresh (setq appt-time-msg-list nil))
9949 (if (eq filter t)
9950 (setq filter (read-from-minibuffer "Regexp filter: ")))
9951 (let* ((cnt 0) ; count added events
9952 (scope (or args '(:deadline* :scheduled* :timestamp)))
9953 (org-agenda-new-buffers nil)
9954 (org-deadline-warning-days 0)
9955 ;; Do not use `org-today' here because appt only takes
9956 ;; time and without date as argument, so it may pass wrong
9957 ;; information otherwise
9958 (today (org-date-to-gregorian
9959 (time-to-days (current-time))))
9960 (org-agenda-restrict nil)
9961 (files (org-agenda-files 'unrestricted)) entries file
9962 (org-agenda-buffer nil))
9963 ;; Get all entries which may contain an appt
9964 (org-agenda-prepare-buffers files)
9965 (while (setq file (pop files))
9966 (setq entries
9967 (delq nil
9968 (append entries
9969 (apply 'org-agenda-get-day-entries
9970 file today scope)))))
9971 ;; Map thru entries and find if we should filter them out
9972 (mapc
9973 (lambda(x)
9974 (let* ((evt (org-trim
9975 (replace-regexp-in-string
9976 org-bracket-link-regexp "\\3"
9977 (or (get-text-property 1 'txt x) ""))))
9978 (cat (get-text-property 1 'org-category x))
9979 (tod (get-text-property 1 'time-of-day x))
9980 (ok (or (null filter)
9981 (and (stringp filter) (string-match filter evt))
9982 (and (functionp filter) (funcall filter x))
9983 (and (listp filter)
9984 (let ((cat-filter (cadr (assoc 'category filter)))
9985 (evt-filter (cadr (assoc 'headline filter))))
9986 (or (and (stringp cat-filter)
9987 (string-match cat-filter cat))
9988 (and (stringp evt-filter)
9989 (string-match evt-filter evt)))))))
9990 (wrn (get-text-property 1 'warntime x)))
9991 ;; FIXME: Shall we remove text-properties for the appt text?
9992 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9993 (when (and ok tod)
9994 (setq tod (concat "00" (number-to-string tod))
9995 tod (when (string-match
9996 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9997 (concat (match-string 1 tod) ":"
9998 (match-string 2 tod))))
9999 (if (version< emacs-version "23.3")
10000 (appt-add tod evt)
10001 (appt-add tod evt wrn))
10002 (setq cnt (1+ cnt))))) entries)
10003 (org-release-buffers org-agenda-new-buffers)
10004 (if (eq cnt 0)
10005 (message "No event to add")
10006 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10008 (defun org-agenda-todayp (date)
10009 "Does DATE mean today, when considering `org-extend-today-until'?"
10010 (let ((today (org-today))
10011 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10012 date)))
10013 (eq date today)))
10015 (defun org-agenda-todo-yesterday (&optional arg)
10016 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10017 (interactive "P")
10018 (let* ((hour (third (decode-time
10019 (org-current-time))))
10020 (org-extend-today-until (1+ hour)))
10021 (org-agenda-todo arg)))
10023 (provide 'org-agenda)
10025 ;;; org-agenda.el ends here