org-agenda.el (org-agenda-goto-date): Put the cursor on the agenda line with the...
[org-mode/org-tableheadings.git] / lisp / org-agenda.el
blob3abfb6b03c7b43656b9013e157ec739f0158f02a
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 (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 (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 (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 (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 (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 (error "Can only append from within agenda buffer"))
2837 (let ((org-agenda-multi t))
2838 (org-agenda)
2839 (widen)
2840 (org-agenda-finalize)
2841 (org-agenda-fit-window-to-buffer)))
2843 (defun org-agenda-normalize-custom-commands (cmds)
2844 "Normalize custom commands CMDS."
2845 (delq nil
2846 (mapcar
2847 (lambda (x)
2848 (cond ((stringp (cdr x)) nil)
2849 ((stringp (nth 1 x)) x)
2850 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2851 (t (cons (car x) (cons "" (cdr x))))))
2852 cmds)))
2854 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2855 "The user interface for selecting an agenda command."
2856 (catch 'exit
2857 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2858 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2859 (region-p (org-region-active-p))
2860 (custom org-agenda-custom-commands)
2861 (selstring "")
2862 restriction second-time
2863 c entry key type match prefixes rmheader header-end custom1 desc
2864 line lines left right n n1)
2865 (save-window-excursion
2866 (delete-other-windows)
2867 (org-switch-to-buffer-other-window " *Agenda Commands*")
2868 (erase-buffer)
2869 (insert (eval-when-compile
2870 (let ((header
2871 "Press key for an agenda command: < Buffer, subtree/region restriction
2872 -------------------------------- > Remove restriction
2873 a Agenda for current week or day e Export agenda views
2874 t List of all TODO entries T Entries with special TODO kwd
2875 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2876 s Search for keywords S Like s, but only TODO entries
2877 L Timeline for current buffer # List stuck projects (!=configure)
2878 / Multi-occur C Configure custom agenda commands
2879 ? Find :FLAGGED: entries * Toggle sticky agenda views
2881 (start 0))
2882 (while (string-match
2883 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2884 header start)
2885 (setq start (match-end 0))
2886 (add-text-properties (match-beginning 2) (match-end 2)
2887 '(face bold) header))
2888 header)))
2889 (setq header-end (point-marker))
2890 (while t
2891 (setq custom1 custom)
2892 (when (eq rmheader t)
2893 (org-goto-line 1)
2894 (re-search-forward ":" nil t)
2895 (delete-region (match-end 0) (point-at-eol))
2896 (forward-char 1)
2897 (looking-at "-+")
2898 (delete-region (match-end 0) (point-at-eol))
2899 (move-marker header-end (match-end 0)))
2900 (goto-char header-end)
2901 (delete-region (point) (point-max))
2903 ;; Produce all the lines that describe custom commands and prefixes
2904 (setq lines nil)
2905 (while (setq entry (pop custom1))
2906 (setq key (car entry) desc (nth 1 entry)
2907 type (nth 2 entry)
2908 match (nth 3 entry))
2909 (if (> (length key) 1)
2910 (add-to-list 'prefixes (string-to-char key))
2911 (setq line
2912 (format
2913 "%-4s%-14s"
2914 (org-add-props (copy-sequence key)
2915 '(face bold))
2916 (cond
2917 ((string-match "\\S-" desc) desc)
2918 ((eq type 'agenda) "Agenda for current week or day")
2919 ((eq type 'agenda*) "Appointments for current week or day")
2920 ((eq type 'alltodo) "List of all TODO entries")
2921 ((eq type 'search) "Word search")
2922 ((eq type 'stuck) "List of stuck projects")
2923 ((eq type 'todo) "TODO keyword")
2924 ((eq type 'tags) "Tags query")
2925 ((eq type 'tags-todo) "Tags (TODO)")
2926 ((eq type 'tags-tree) "Tags tree")
2927 ((eq type 'todo-tree) "TODO kwd tree")
2928 ((eq type 'occur-tree) "Occur tree")
2929 ((functionp type) (if (symbolp type)
2930 (symbol-name type)
2931 "Lambda expression"))
2932 (t "???"))))
2933 (if org-agenda-menu-show-matcher
2934 (setq line
2935 (concat line ": "
2936 (cond
2937 ((stringp match)
2938 (setq match (copy-sequence match))
2939 (org-add-props match nil 'face 'org-warning))
2940 ((listp type)
2941 (format "set of %d commands" (length type))))))
2942 (if (org-string-nw-p match)
2943 (add-text-properties
2944 0 (length line) (list 'help-echo
2945 (concat "Matcher: " match)) line)))
2946 (push line lines)))
2947 (setq lines (nreverse lines))
2948 (when prefixes
2949 (mapc (lambda (x)
2950 (push
2951 (format "%s %s"
2952 (org-add-props (char-to-string x)
2953 nil 'face 'bold)
2954 (or (cdr (assoc (concat selstring
2955 (char-to-string x))
2956 prefix-descriptions))
2957 "Prefix key"))
2958 lines))
2959 prefixes))
2961 ;; Check if we should display in two columns
2962 (if org-agenda-menu-two-columns
2963 (progn
2964 (setq n (length lines)
2965 n1 (+ (/ n 2) (mod n 2))
2966 right (nthcdr n1 lines)
2967 left (copy-sequence lines))
2968 (setcdr (nthcdr (1- n1) left) nil))
2969 (setq left lines right nil))
2970 (while left
2971 (insert "\n" (pop left))
2972 (when right
2973 (if (< (current-column) 40)
2974 (move-to-column 40 t)
2975 (insert " "))
2976 (insert (pop right))))
2978 ;; Make the window the right size
2979 (goto-char (point-min))
2980 (if second-time
2981 (if (not (pos-visible-in-window-p (point-max)))
2982 (org-fit-window-to-buffer))
2983 (setq second-time t)
2984 (org-fit-window-to-buffer))
2986 ;; Ask for selection
2987 (message "Press key for agenda command%s:"
2988 (if (or restrict-ok org-agenda-overriding-restriction)
2989 (if org-agenda-overriding-restriction
2990 " (restriction lock active)"
2991 (if restriction
2992 (format " (restricted to %s)" restriction)
2993 " (unrestricted)"))
2994 ""))
2995 (setq c (read-char-exclusive))
2996 (message "")
2997 (cond
2998 ((assoc (char-to-string c) custom)
2999 (setq selstring (concat selstring (char-to-string c)))
3000 (throw 'exit (cons selstring restriction)))
3001 ((memq c prefixes)
3002 (setq selstring (concat selstring (char-to-string c))
3003 prefixes nil
3004 rmheader (or rmheader t)
3005 custom (delq nil (mapcar
3006 (lambda (x)
3007 (if (or (= (length (car x)) 1)
3008 (/= (string-to-char (car x)) c))
3010 (cons (substring (car x) 1) (cdr x))))
3011 custom))))
3012 ((eq c ?*)
3013 (call-interactively 'org-toggle-sticky-agenda)
3014 (sit-for 2))
3015 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3016 (message "Restriction is only possible in Org-mode buffers")
3017 (ding) (sit-for 1))
3018 ((eq c ?1)
3019 (org-agenda-remove-restriction-lock 'noupdate)
3020 (setq restriction 'buffer))
3021 ((eq c ?0)
3022 (org-agenda-remove-restriction-lock 'noupdate)
3023 (setq restriction (if region-p 'region 'subtree)))
3024 ((eq c ?<)
3025 (org-agenda-remove-restriction-lock 'noupdate)
3026 (setq restriction
3027 (cond
3028 ((eq restriction 'buffer)
3029 (if region-p 'region 'subtree))
3030 ((memq restriction '(subtree region))
3031 nil)
3032 (t 'buffer))))
3033 ((eq c ?>)
3034 (org-agenda-remove-restriction-lock 'noupdate)
3035 (setq restriction nil))
3036 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3037 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3038 ((and (> (length selstring) 0) (eq c ?\d))
3039 (delete-window)
3040 (org-agenda-get-restriction-and-command prefix-descriptions))
3042 ((equal c ?q) (error "Abort"))
3043 (t (error "Invalid key %c" c))))))))
3045 (defun org-agenda-fit-window-to-buffer ()
3046 "Fit the window to the buffer size."
3047 (and (memq org-agenda-window-setup '(reorganize-frame))
3048 (fboundp 'fit-window-to-buffer)
3049 (org-fit-window-to-buffer
3051 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3052 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3054 (defvar org-cmd nil)
3055 (defvar org-agenda-overriding-cmd nil)
3056 (defvar org-agenda-overriding-arguments nil)
3057 (defvar org-agenda-overriding-cmd-arguments nil)
3058 (defun org-agenda-run-series (name series)
3059 "Run agenda NAME as a SERIES of agenda commands."
3060 (org-let (nth 1 series) '(org-agenda-prepare name))
3061 ;; We need to reset agenda markers here, because when constructing a
3062 ;; block agenda, the individual blocks do not do that.
3063 (org-agenda-reset-markers)
3064 (let* ((org-agenda-multi t)
3065 (redo (list 'org-agenda-run-series name (list 'quote series)))
3066 (cmds (car series))
3067 (gprops (nth 1 series))
3068 match ;; The byte compiler incorrectly complains about this. Keep it!
3069 org-cmd type lprops)
3070 (while (setq org-cmd (pop cmds))
3071 (setq type (car org-cmd)
3072 match (eval (nth 1 org-cmd))
3073 lprops (nth 2 org-cmd))
3074 (let ((org-agenda-overriding-arguments
3075 (if (eq org-agenda-overriding-cmd org-cmd)
3076 (or org-agenda-overriding-arguments
3077 org-agenda-overriding-cmd-arguments))))
3078 (cond
3079 ((eq type 'agenda)
3080 (org-let2 gprops lprops
3081 '(call-interactively 'org-agenda-list)))
3082 ((eq type 'agenda*)
3083 (org-let2 gprops lprops
3084 '(funcall 'org-agenda-list nil nil t)))
3085 ((eq type 'alltodo)
3086 (org-let2 gprops lprops
3087 '(call-interactively 'org-todo-list)))
3088 ((eq type 'search)
3089 (org-let2 gprops lprops
3090 '(org-search-view current-prefix-arg match nil)))
3091 ((eq type 'stuck)
3092 (org-let2 gprops lprops
3093 '(call-interactively 'org-agenda-list-stuck-projects)))
3094 ((eq type 'tags)
3095 (org-let2 gprops lprops
3096 '(org-tags-view current-prefix-arg match)))
3097 ((eq type 'tags-todo)
3098 (org-let2 gprops lprops
3099 '(org-tags-view '(4) match)))
3100 ((eq type 'todo)
3101 (org-let2 gprops lprops
3102 '(org-todo-list match)))
3103 ((fboundp type)
3104 (org-let2 gprops lprops
3105 '(funcall type match)))
3106 (t (error "Invalid type in command series")))))
3107 (widen)
3108 (let ((inhibit-read-only t))
3109 (add-text-properties (point-min) (point-max)
3110 `(org-series t org-series-redo-cmd ,redo)))
3111 (setq org-agenda-redo-command redo)
3112 (goto-char (point-min)))
3113 (org-agenda-fit-window-to-buffer)
3114 (org-let (nth 1 series) '(org-agenda-finalize)))
3116 ;;;###autoload
3117 (defmacro org-batch-agenda (cmd-key &rest parameters)
3118 "Run an agenda command in batch mode and send the result to STDOUT.
3119 If CMD-KEY is a string of length 1, it is used as a key in
3120 `org-agenda-custom-commands' and triggers this command. If it is a
3121 longer string it is used as a tags/todo match string.
3122 Parameters are alternating variable names and values that will be bound
3123 before running the agenda command."
3124 (org-eval-in-environment (org-make-parameter-alist parameters)
3125 (if (> (length cmd-key) 2)
3126 (org-tags-view nil cmd-key)
3127 (org-agenda nil cmd-key)))
3128 (set-buffer org-agenda-buffer-name)
3129 (princ (buffer-string)))
3131 (defvar org-agenda-info nil)
3133 ;;;###autoload
3134 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3135 "Run an agenda command in batch mode and send the result to STDOUT.
3136 If CMD-KEY is a string of length 1, it is used as a key in
3137 `org-agenda-custom-commands' and triggers this command. If it is a
3138 longer string it is used as a tags/todo match string.
3139 Parameters are alternating variable names and values that will be bound
3140 before running the agenda command.
3142 The output gives a line for each selected agenda item. Each
3143 item is a list of comma-separated values, like this:
3145 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3147 category The category of the item
3148 head The headline, without TODO kwd, TAGS and PRIORITY
3149 type The type of the agenda entry, can be
3150 todo selected in TODO match
3151 tagsmatch selected in tags match
3152 diary imported from diary
3153 deadline a deadline on given date
3154 scheduled scheduled on given date
3155 timestamp entry has timestamp on given date
3156 closed entry was closed on given date
3157 upcoming-deadline warning about deadline
3158 past-scheduled forwarded scheduled item
3159 block entry has date block including g. date
3160 todo The todo keyword, if any
3161 tags All tags including inherited ones, separated by colons
3162 date The relevant date, like 2007-2-14
3163 time The time, like 15:00-16:50
3164 extra Sting with extra planning info
3165 priority-l The priority letter if any was given
3166 priority-n The computed numerical priority
3167 agenda-day The day in the agenda where this is listed"
3168 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3169 (org-make-parameter-alist parameters))
3170 (if (> (length cmd-key) 2)
3171 (org-tags-view nil cmd-key)
3172 (org-agenda nil cmd-key)))
3173 (set-buffer org-agenda-buffer-name)
3174 (let* ((lines (org-split-string (buffer-string) "\n"))
3175 line)
3176 (while (setq line (pop lines))
3177 (catch 'next
3178 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3179 (setq org-agenda-info
3180 (org-fix-agenda-info (text-properties-at 0 line)))
3181 (princ
3182 (mapconcat 'org-agenda-export-csv-mapper
3183 '(org-category txt type todo tags date time extra
3184 priority-letter priority agenda-day)
3185 ","))
3186 (princ "\n")))))
3188 (defun org-fix-agenda-info (props)
3189 "Make sure all properties on an agenda item have a canonical form.
3190 This ensures the export commands can easily use it."
3191 (let (tmp re)
3192 (when (setq tmp (plist-get props 'tags))
3193 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3194 (when (setq tmp (plist-get props 'date))
3195 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3196 (let ((calendar-date-display-form '(year "-" month "-" day)))
3197 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3199 (setq tmp (calendar-date-string tmp)))
3200 (setq props (plist-put props 'date tmp)))
3201 (when (setq tmp (plist-get props 'day))
3202 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3203 (let ((calendar-date-display-form '(year "-" month "-" day)))
3204 (setq tmp (calendar-date-string tmp)))
3205 (setq props (plist-put props 'day tmp))
3206 (setq props (plist-put props 'agenda-day tmp)))
3207 (when (setq tmp (plist-get props 'txt))
3208 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3209 (plist-put props 'priority-letter (match-string 1 tmp))
3210 (setq tmp (replace-match "" t t tmp)))
3211 (when (and (setq re (plist-get props 'org-todo-regexp))
3212 (setq re (concat "\\`\\.*" re " ?"))
3213 (string-match re tmp))
3214 (plist-put props 'todo (match-string 1 tmp))
3215 (setq tmp (replace-match "" t t tmp)))
3216 (plist-put props 'txt tmp)))
3217 props)
3219 (defun org-agenda-export-csv-mapper (prop)
3220 (let ((res (plist-get org-agenda-info prop)))
3221 (setq res
3222 (cond
3223 ((not res) "")
3224 ((stringp res) res)
3225 (t (prin1-to-string res))))
3226 (while (string-match "," res)
3227 (setq res (replace-match ";" t t res)))
3228 (org-trim res)))
3230 ;;;###autoload
3231 (defun org-store-agenda-views (&rest parameters)
3232 "Store agenda views."
3233 (interactive)
3234 (eval (list 'org-batch-store-agenda-views)))
3236 ;;;###autoload
3237 (defmacro org-batch-store-agenda-views (&rest parameters)
3238 "Run all custom agenda commands that have a file argument."
3239 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3240 (pop-up-frames nil)
3241 (dir default-directory)
3242 (pars (org-make-parameter-alist parameters))
3243 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3244 (save-window-excursion
3245 (while cmds
3246 (setq cmd (pop cmds)
3247 thiscmdkey (car cmd)
3248 thiscmdcmd (cdr cmd)
3249 match (nth 2 thiscmdcmd)
3250 bufname (if org-agenda-sticky
3251 (or (and (stringp match)
3252 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3253 (format "*Org Agenda(%s)*" thiscmdkey))
3254 org-agenda-buffer-name)
3255 cmd-or-set (nth 2 cmd)
3256 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3257 files (nth (if (listp cmd-or-set) 4 5) cmd))
3258 (if (stringp files) (setq files (list files)))
3259 (when files
3260 (org-eval-in-environment (append org-agenda-exporter-settings
3261 opts pars)
3262 (org-agenda nil thiscmdkey))
3263 (set-buffer bufname)
3264 (while files
3265 (org-eval-in-environment (append org-agenda-exporter-settings
3266 opts pars)
3267 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3268 (and (get-buffer bufname)
3269 (kill-buffer bufname)))))))
3271 (defvar org-agenda-current-span nil
3272 "The current span used in the agenda view.") ; local variable in the agenda buffer
3273 (defun org-agenda-mark-header-line (pos)
3274 "Mark the line at POS as an agenda structure header."
3275 (save-excursion
3276 (goto-char pos)
3277 (put-text-property (point-at-bol) (point-at-eol)
3278 'org-agenda-structural-header t)
3279 (when org-agenda-title-append
3280 (put-text-property (point-at-bol) (point-at-eol)
3281 'org-agenda-title-append org-agenda-title-append))))
3283 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3284 (defvar org-agenda-write-buffer-name "Agenda View")
3285 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3286 "Write the current buffer (an agenda view) as a file.
3287 Depending on the extension of the file name, plain text (.txt),
3288 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3289 If the extension is .ics, run icalendar export over all files used
3290 to construct the agenda and limit the export to entries listed in the
3291 agenda now.
3292 If the extension is .org, collect all subtrees corresponding to the
3293 agenda entries and add them in an .org file.
3294 With prefix argument OPEN, open the new file immediately.
3295 If NOSETTINGS is given, do not scope the settings of
3296 `org-agenda-exporter-settings' into the export commands. This is used when
3297 the settings have already been scoped and we do not wish to overrule other,
3298 higher priority settings.
3299 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3300 (interactive "FWrite agenda to file: \nP")
3301 (if (or (not (file-writable-p file))
3302 (and (file-exists-p file)
3303 (if (called-interactively-p 'any)
3304 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3305 (error "Cannot write agenda to file %s" file))
3306 (org-let (if nosettings nil org-agenda-exporter-settings)
3307 '(save-excursion
3308 (save-window-excursion
3309 (org-agenda-mark-filtered-text)
3310 (let ((bs (copy-sequence (buffer-string))) beg content)
3311 (org-agenda-unmark-filtered-text)
3312 (with-temp-buffer
3313 (rename-buffer org-agenda-write-buffer-name t)
3314 (set-buffer-modified-p nil)
3315 (insert bs)
3316 (org-agenda-remove-marked-text 'org-filtered)
3317 (while (setq beg (text-property-any (point-min) (point-max)
3318 'org-filtered t))
3319 (delete-region
3320 beg (or (next-single-property-change beg 'org-filtered)
3321 (point-max))))
3322 (run-hooks 'org-agenda-before-write-hook)
3323 (cond
3324 ((org-bound-and-true-p org-mobile-creating-agendas)
3325 (org-mobile-write-agenda-for-mobile file))
3326 ((string-match "\\.org\\'" file)
3327 (let (content p m message-log-max)
3328 (goto-char (point-min))
3329 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3330 (goto-char p)
3331 (setq m (get-text-property (point) 'org-hd-marker))
3332 (when m
3333 (push (save-excursion
3334 (set-buffer (marker-buffer m))
3335 (goto-char m)
3336 (org-copy-subtree 1 nil t t)
3337 org-subtree-clip)
3338 content)))
3339 (find-file file)
3340 (erase-buffer)
3341 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3342 (write-file file)
3343 (kill-buffer (current-buffer))
3344 (message "Org file written to %s" file)))
3345 ((string-match "\\.html?\\'" file)
3346 (require 'htmlize)
3347 (set-buffer (htmlize-buffer (current-buffer)))
3348 (when org-agenda-export-html-style
3349 ;; replace <style> section with org-agenda-export-html-style
3350 (goto-char (point-min))
3351 (kill-region (- (search-forward "<style") 6)
3352 (search-forward "</style>"))
3353 (insert org-agenda-export-html-style))
3354 (write-file file)
3355 (kill-buffer (current-buffer))
3356 (message "HTML written to %s" file))
3357 ((string-match "\\.ps\\'" file)
3358 (require 'ps-print)
3359 (ps-print-buffer-with-faces file)
3360 (message "Postscript written to %s" file))
3361 ((string-match "\\.pdf\\'" file)
3362 (require 'ps-print)
3363 (ps-print-buffer-with-faces
3364 (concat (file-name-sans-extension file) ".ps"))
3365 (call-process "ps2pdf" nil nil nil
3366 (expand-file-name
3367 (concat (file-name-sans-extension file) ".ps"))
3368 (expand-file-name file))
3369 (delete-file (concat (file-name-sans-extension file) ".ps"))
3370 (message "PDF written to %s" file))
3371 ((string-match "\\.ics\\'" file)
3372 (require 'ox-icalendar)
3373 (org-icalendar-export-current-agenda (expand-file-name file)))
3375 (let ((bs (buffer-string)))
3376 (find-file file)
3377 (erase-buffer)
3378 (insert bs)
3379 (save-buffer 0)
3380 (kill-buffer (current-buffer))
3381 (message "Plain text written to %s" file))))))))
3382 (set-buffer (or agenda-bufname
3383 (and (org-called-interactively-p 'any) (buffer-name))
3384 org-agenda-buffer-name)))
3385 (when open (org-open-file file)))
3387 (defvar org-agenda-tag-filter-overlays nil)
3388 (defvar org-agenda-cat-filter-overlays nil)
3389 (defvar org-agenda-re-filter-overlays nil)
3391 (defun org-agenda-mark-filtered-text ()
3392 "Mark all text hidden by filtering with a text property."
3393 (let ((inhibit-read-only t))
3394 (mapc
3395 (lambda (o)
3396 (when (equal (overlay-buffer o) (current-buffer))
3397 (put-text-property
3398 (overlay-start o) (overlay-end o)
3399 'org-filtered t)))
3400 (append org-agenda-tag-filter-overlays
3401 org-agenda-cat-filter-overlays
3402 org-agenda-re-filter-overlays))))
3404 (defun org-agenda-unmark-filtered-text ()
3405 "Remove the filtering text property."
3406 (let ((inhibit-read-only t))
3407 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3409 (defun org-agenda-remove-marked-text (property &optional value)
3410 "Delete all text marked with VALUE of PROPERTY.
3411 VALUE defaults to t."
3412 (let (beg)
3413 (setq value (or value t))
3414 (while (setq beg (text-property-any (point-min) (point-max)
3415 property value))
3416 (delete-region
3417 beg (or (next-single-property-change beg 'org-filtered)
3418 (point-max))))))
3420 (defun org-agenda-add-entry-text ()
3421 "Add entry text to agenda lines.
3422 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3423 entry text following headings shown in the agenda.
3424 Drawers will be excluded, also the line with scheduling/deadline info."
3425 (when (and (> org-agenda-add-entry-text-maxlines 0)
3426 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3427 (let (m txt)
3428 (goto-char (point-min))
3429 (while (not (eobp))
3430 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3431 (beginning-of-line 2)
3432 (setq txt (org-agenda-get-some-entry-text
3433 m org-agenda-add-entry-text-maxlines " > "))
3434 (end-of-line 1)
3435 (if (string-match "\\S-" txt)
3436 (insert "\n" txt)
3437 (or (eobp) (forward-char 1))))))))
3439 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3440 &rest keep)
3441 "Extract entry text from MARKER, at most N-LINES lines.
3442 This will ignore drawers etc, just get the text.
3443 If INDENT is given, prefix every line with this string. If KEEP is
3444 given, it is a list of symbols, defining stuff that should not be
3445 removed from the entry content. Currently only `planning' is allowed here."
3446 (let (txt drawer-re kwd-time-re ind)
3447 (save-excursion
3448 (with-current-buffer (marker-buffer marker)
3449 (if (not (derived-mode-p 'org-mode))
3450 (setq txt "")
3451 (save-excursion
3452 (save-restriction
3453 (widen)
3454 (goto-char marker)
3455 (end-of-line 1)
3456 (setq txt (buffer-substring
3457 (min (1+ (point)) (point-max))
3458 (progn (outline-next-heading) (point)))
3459 drawer-re org-drawer-regexp
3460 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3461 ".*\n?"))
3462 (with-temp-buffer
3463 (insert txt)
3464 (when org-agenda-add-entry-text-descriptive-links
3465 (goto-char (point-min))
3466 (while (org-activate-bracket-links (point-max))
3467 (add-text-properties (match-beginning 0) (match-end 0)
3468 '(face org-link))))
3469 (goto-char (point-min))
3470 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3471 (set-text-properties (match-beginning 0) (match-end 0)
3472 nil))
3473 (goto-char (point-min))
3474 (while (re-search-forward drawer-re nil t)
3475 (delete-region
3476 (match-beginning 0)
3477 (progn (re-search-forward
3478 "^[ \t]*:END:.*\n?" nil 'move)
3479 (point))))
3480 (unless (member 'planning keep)
3481 (goto-char (point-min))
3482 (while (re-search-forward kwd-time-re nil t)
3483 (replace-match "")))
3484 (goto-char (point-min))
3485 (when org-agenda-entry-text-exclude-regexps
3486 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3487 (while (setq re (pop re-list))
3488 (goto-char (point-min))
3489 (while (re-search-forward re nil t)
3490 (replace-match "")))))
3491 (goto-char (point-max))
3492 (skip-chars-backward " \t\n")
3493 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3495 ;; find and remove min common indentation
3496 (goto-char (point-min))
3497 (untabify (point-min) (point-max))
3498 (setq ind (org-get-indentation))
3499 (while (not (eobp))
3500 (unless (looking-at "[ \t]*$")
3501 (setq ind (min ind (org-get-indentation))))
3502 (beginning-of-line 2))
3503 (goto-char (point-min))
3504 (while (not (eobp))
3505 (unless (looking-at "[ \t]*$")
3506 (move-to-column ind)
3507 (delete-region (point-at-bol) (point)))
3508 (beginning-of-line 2))
3510 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3512 (goto-char (point-min))
3513 (when indent
3514 (while (and (not (eobp)) (re-search-forward "^" nil t))
3515 (replace-match indent t t)))
3516 (goto-char (point-min))
3517 (while (looking-at "[ \t]*\n") (replace-match ""))
3518 (goto-char (point-max))
3519 (when (> (org-current-line)
3520 n-lines)
3521 (org-goto-line (1+ n-lines))
3522 (backward-char 1))
3523 (setq txt (buffer-substring (point-min) (point)))))))))
3524 txt))
3526 (defun org-check-for-org-mode ()
3527 "Make sure current buffer is in org-mode. Error if not."
3528 (or (derived-mode-p 'org-mode)
3529 (error "Cannot execute org-mode agenda command on buffer in %s"
3530 major-mode)))
3532 ;;; Agenda prepare and finalize
3534 (defvar org-agenda-multi nil) ; dynamically scoped
3535 (defvar org-agenda-pre-window-conf nil)
3536 (defvar org-agenda-columns-active nil)
3537 (defvar org-agenda-name nil)
3538 (defvar org-agenda-tag-filter nil)
3539 (defvar org-agenda-category-filter nil)
3540 (defvar org-agenda-regexp-filter nil)
3541 (defvar org-agenda-top-headline-filter nil)
3542 (defvar org-agenda-tag-filter-while-redo nil)
3543 (defvar org-agenda-tag-filter-preset nil
3544 "A preset of the tags filter used for secondary agenda filtering.
3545 This must be a list of strings, each string must be a single tag preceded
3546 by \"+\" or \"-\".
3547 This variable should not be set directly, but agenda custom commands can
3548 bind it in the options section. The preset filter is a global property of
3549 the entire agenda view. In a block agenda, it will not work reliably to
3550 define a filter for one of the individual blocks. You need to set it in
3551 the global options and expect it to be applied to the entire view.")
3553 (defvar org-agenda-category-filter-preset nil
3554 "A preset of the category filter used for secondary agenda filtering.
3555 This must be a list of strings, each string must be a single category
3556 preceded by \"+\" or \"-\".
3557 This variable should not be set directly, but agenda custom commands can
3558 bind it in the options section. The preset filter is a global property of
3559 the entire agenda view. In a block agenda, it will not work reliably to
3560 define a filter for one of the individual blocks. You need to set it in
3561 the global options and expect it to be applied to the entire view.")
3563 (defvar org-agenda-regexp-filter-preset nil
3564 "A preset of the regexp filter used for secondary agenda filtering.
3565 This must be a list of strings, each string must be a single category
3566 preceded by \"+\" or \"-\".
3567 This variable should not be set directly, but agenda custom commands can
3568 bind it in the options section. The preset filter is a global property of
3569 the entire agenda view. In a block agenda, it will not work reliably to
3570 define a filter for one of the individual blocks. You need to set it in
3571 the global options and expect it to be applied to the entire view.")
3573 (defun org-agenda-use-sticky-p ()
3574 "Return non-nil if an agenda buffer named
3575 `org-agenda-buffer-name' exists and should be shown instead of
3576 generating a new one."
3577 (and
3578 ;; turned off by user
3579 org-agenda-sticky
3580 ;; For multi-agenda buffer already exists
3581 (not org-agenda-multi)
3582 ;; buffer found
3583 (get-buffer org-agenda-buffer-name)
3584 ;; C-u parameter is same as last call
3585 (with-current-buffer (get-buffer org-agenda-buffer-name)
3586 (and
3587 (equal current-prefix-arg
3588 org-agenda-last-prefix-arg)
3589 ;; In case user turned stickiness on, while having existing
3590 ;; Agenda buffer active, don't reuse that buffer, because it
3591 ;; does not have org variables local
3592 org-agenda-this-buffer-is-sticky))))
3594 (defun org-agenda-prepare-window (abuf)
3595 "Setup agenda buffer in the window."
3596 (let* ((awin (get-buffer-window abuf))
3597 wconf)
3598 (cond
3599 ((equal (current-buffer) abuf) nil)
3600 (awin (select-window awin))
3601 ((not (setq wconf (current-window-configuration))))
3602 ((equal org-agenda-window-setup 'current-window)
3603 (org-pop-to-buffer-same-window abuf))
3604 ((equal org-agenda-window-setup 'other-window)
3605 (org-switch-to-buffer-other-window abuf))
3606 ((equal org-agenda-window-setup 'other-frame)
3607 (switch-to-buffer-other-frame abuf))
3608 ((equal org-agenda-window-setup 'reorganize-frame)
3609 (delete-other-windows)
3610 (org-switch-to-buffer-other-window abuf)))
3611 ;; additional test in case agenda is invoked from within agenda
3612 ;; buffer via elisp link
3613 (unless (equal (current-buffer) abuf)
3614 (org-pop-to-buffer-same-window abuf))
3615 (setq org-agenda-pre-window-conf
3616 (or org-agenda-pre-window-conf wconf))))
3618 (defun org-agenda-prepare (&optional name)
3619 (if (org-agenda-use-sticky-p)
3620 (progn
3621 ;; Popup existing buffer
3622 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3623 (message "Sticky Agenda buffer, use `r' to refresh")
3624 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3625 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3626 (setq org-todo-keywords-for-agenda nil)
3627 (setq org-drawers-for-agenda nil)
3628 (unless org-agenda-persistent-filter
3629 (setq org-agenda-tag-filter nil
3630 org-agenda-category-filter nil
3631 org-agenda-regexp-filter nil))
3632 (put 'org-agenda-tag-filter :preset-filter
3633 org-agenda-tag-filter-preset)
3634 (put 'org-agenda-category-filter :preset-filter
3635 org-agenda-category-filter-preset)
3636 (put 'org-agenda-regexp-filter :preset-filter
3637 org-agenda-regexp-filter-preset)
3638 (if org-agenda-multi
3639 (progn
3640 (setq buffer-read-only nil)
3641 (goto-char (point-max))
3642 (unless (or (bobp) org-agenda-compact-blocks
3643 (not org-agenda-block-separator))
3644 (insert "\n"
3645 (if (stringp org-agenda-block-separator)
3646 org-agenda-block-separator
3647 (make-string (window-width) org-agenda-block-separator))
3648 "\n"))
3649 (narrow-to-region (point) (point-max)))
3650 (setq org-done-keywords-for-agenda nil)
3652 ;; Setting any org variables that are in org-agenda-local-vars
3653 ;; list need to be done after the prepare call
3654 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3655 (setq buffer-read-only nil)
3656 (org-agenda-reset-markers)
3657 (let ((inhibit-read-only t)) (erase-buffer))
3658 (org-agenda-mode)
3659 (setq org-agenda-buffer (current-buffer))
3660 (setq org-agenda-contributing-files nil)
3661 (setq org-agenda-columns-active nil)
3662 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3663 (setq org-todo-keywords-for-agenda
3664 (org-uniquify org-todo-keywords-for-agenda))
3665 (setq org-done-keywords-for-agenda
3666 (org-uniquify org-done-keywords-for-agenda))
3667 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3668 (setq org-agenda-last-prefix-arg current-prefix-arg)
3669 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3670 (and name (not org-agenda-name)
3671 (org-set-local 'org-agenda-name name)))
3672 (setq buffer-read-only nil)))
3674 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3675 (defun org-agenda-finalize ()
3676 "Finishing touch for the agenda buffer, called just before displaying it."
3677 (unless org-agenda-multi
3678 (save-excursion
3679 (let ((inhibit-read-only t))
3680 (goto-char (point-min))
3681 (save-excursion
3682 (while (org-activate-bracket-links (point-max))
3683 (add-text-properties (match-beginning 0) (match-end 0)
3684 '(face org-link))))
3685 (save-excursion
3686 (while (org-activate-plain-links (point-max))
3687 (add-text-properties (match-beginning 0) (match-end 0)
3688 '(face org-link))))
3689 (unless (eq org-agenda-remove-tags t)
3690 (org-agenda-align-tags))
3691 (unless org-agenda-with-colors
3692 (remove-text-properties (point-min) (point-max) '(face nil)))
3693 (if (and (boundp 'org-agenda-overriding-columns-format)
3694 org-agenda-overriding-columns-format)
3695 (org-set-local 'org-agenda-overriding-columns-format
3696 org-agenda-overriding-columns-format))
3697 (if (and (boundp 'org-agenda-view-columns-initially)
3698 org-agenda-view-columns-initially)
3699 (org-agenda-columns))
3700 (when org-agenda-fontify-priorities
3701 (org-agenda-fontify-priorities))
3702 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3703 (org-agenda-dim-blocked-tasks))
3704 ;; We need to widen when `org-agenda-finalize' is called from
3705 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3706 (when org-clock-current-task
3707 (save-restriction
3708 (widen)
3709 (org-agenda-mark-clocking-task)))
3710 (when org-agenda-entry-text-mode
3711 (org-agenda-entry-text-hide)
3712 (org-agenda-entry-text-show))
3713 (if (and (functionp 'org-habit-insert-consistency-graphs)
3714 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3715 (org-habit-insert-consistency-graphs))
3716 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3717 (unless (or (eq org-agenda-show-inherited-tags 'always)
3718 (and (listp org-agenda-show-inherited-tags)
3719 (memq org-agenda-type org-agenda-show-inherited-tags))
3720 (and (eq org-agenda-show-inherited-tags t)
3721 (or (eq org-agenda-use-tag-inheritance t)
3722 (and (listp org-agenda-use-tag-inheritance)
3723 (not (memq org-agenda-type
3724 org-agenda-use-tag-inheritance))))))
3725 (let (mrk)
3726 (save-excursion
3727 (goto-char (point-min))
3728 (while (equal (forward-line) 0)
3729 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3730 (get-text-property (point) 'org-hd-marker)))
3731 (put-text-property (point-at-bol) (point-at-eol)
3732 'tags (org-with-point-at mrk
3733 (delete-dups
3734 (mapcar 'downcase (org-get-tags-at))))))))))
3735 (run-hooks 'org-agenda-finalize-hook)
3736 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3737 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3738 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3739 (org-agenda-filter-apply org-agenda-category-filter 'category))
3740 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3741 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3742 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3744 (defun org-agenda-mark-clocking-task ()
3745 "Mark the current clock entry in the agenda if it is present."
3746 (org-agenda-unmark-clocking-task)
3747 (when (marker-buffer org-clock-hd-marker)
3748 (save-excursion
3749 (goto-char (point-min))
3750 (let (s ov)
3751 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3752 (goto-char s)
3753 (when (equal (org-get-at-bol 'org-hd-marker)
3754 org-clock-hd-marker)
3755 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3756 (overlay-put ov 'type 'org-agenda-clocking)
3757 (overlay-put ov 'face 'org-agenda-clocking)
3758 (overlay-put ov 'help-echo
3759 "The clock is running in this item")))))))
3761 (defun org-agenda-unmark-clocking-task ()
3762 "Unmark the current clocking task."
3763 (mapc (lambda (o)
3764 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3765 (delete-overlay o)))
3766 (overlays-in (point-min) (point-max))))
3768 (defun org-agenda-fontify-priorities ()
3769 "Make highest priority lines bold, and lowest italic."
3770 (interactive)
3771 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3772 (delete-overlay o)))
3773 (overlays-in (point-min) (point-max)))
3774 (save-excursion
3775 (let (b e p ov h l)
3776 (goto-char (point-min))
3777 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3778 (setq h (or (get-char-property (point) 'org-highest-priority)
3779 org-highest-priority)
3780 l (or (get-char-property (point) 'org-lowest-priority)
3781 org-lowest-priority)
3782 p (string-to-char (match-string 1))
3783 b (match-beginning 0)
3784 e (if (eq org-agenda-fontify-priorities 'cookies)
3785 (match-end 0)
3786 (point-at-eol))
3787 ov (make-overlay b e))
3788 (overlay-put
3789 ov 'face
3790 (cons (cond ((org-face-from-face-or-color
3791 'priority nil
3792 (cdr (assoc p org-priority-faces))))
3793 ((and (listp org-agenda-fontify-priorities)
3794 (org-face-from-face-or-color
3795 'priority nil
3796 (cdr (assoc p org-agenda-fontify-priorities)))))
3797 ((equal p l) 'italic)
3798 ((equal p h) 'bold))
3799 'org-priority))
3800 (overlay-put ov 'org-type 'org-priority)))))
3802 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3803 "Dim currently blocked TODO's in the agenda display.
3804 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3805 dimming them."
3806 (interactive "P")
3807 (when (org-called-interactively-p 'interactive)
3808 (message "Dim or hide blocked tasks..."))
3809 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3810 (delete-overlay o)))
3811 (overlays-in (point-min) (point-max)))
3812 (save-excursion
3813 (let ((inhibit-read-only t)
3814 (org-depend-tag-blocked nil)
3815 (invis (or (not (null invisible))
3816 (eq org-agenda-dim-blocked-tasks 'invisible)))
3817 org-blocked-by-checkboxes
3818 invis1 b e p ov h l)
3819 (goto-char (point-min))
3820 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3821 (and pos (goto-char (1+ pos))))
3822 (setq org-blocked-by-checkboxes nil invis1 invis)
3823 (let ((marker (org-get-at-bol 'org-hd-marker)))
3824 (when (and marker
3825 (with-current-buffer (marker-buffer marker)
3826 (save-excursion (goto-char marker)
3827 (org-entry-blocked-p))))
3828 (if org-blocked-by-checkboxes (setq invis1 nil))
3829 (setq b (if invis1
3830 (max (point-min) (1- (point-at-bol)))
3831 (point-at-bol))
3832 e (point-at-eol)
3833 ov (make-overlay b e))
3834 (if invis1
3835 (overlay-put ov 'invisible t)
3836 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3837 (overlay-put ov 'org-type 'org-blocked-todo))))))
3838 (when (org-called-interactively-p 'interactive)
3839 (message "Dim or hide blocked tasks...done")))
3841 (defvar org-agenda-skip-function nil
3842 "Function to be called at each match during agenda construction.
3843 If this function returns nil, the current match should not be skipped.
3844 Otherwise, the function must return a position from where the search
3845 should be continued.
3846 This may also be a Lisp form, it will be evaluated.
3847 Never set this variable using `setq' or so, because then it will apply
3848 to all future agenda commands. If you do want a global skipping condition,
3849 use the option `org-agenda-skip-function-global' instead.
3850 The correct usage for `org-agenda-skip-function' is to bind it with
3851 `let' to scope it dynamically into the agenda-constructing command.
3852 A good way to set it is through options in `org-agenda-custom-commands'.")
3854 (defun org-agenda-skip ()
3855 "Throw to `:skip' in places that should be skipped.
3856 Also moves point to the end of the skipped region, so that search can
3857 continue from there."
3858 (let ((p (point-at-bol)) to)
3859 (when (or
3860 (save-excursion (goto-char p) (looking-at comment-start-skip))
3861 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3862 (get-text-property p :org-archived)
3863 (org-end-of-subtree t))
3864 (and org-agenda-skip-comment-trees
3865 (get-text-property p :org-comment)
3866 (org-end-of-subtree t))
3867 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3868 (org-agenda-skip-eval org-agenda-skip-function)))
3869 (goto-char to))
3870 (org-in-src-block-p t))
3871 (throw :skip t))))
3873 (defun org-agenda-skip-eval (form)
3874 "If FORM is a function or a list, call (or eval) is and return result.
3875 `save-excursion' and `save-match-data' are wrapped around the call, so point
3876 and match data are returned to the previous state no matter what these
3877 functions do."
3878 (let (fp)
3879 (and form
3880 (or (setq fp (functionp form))
3881 (consp form))
3882 (save-excursion
3883 (save-match-data
3884 (if fp
3885 (funcall form)
3886 (eval form)))))))
3888 (defvar org-agenda-markers nil
3889 "List of all currently active markers created by `org-agenda'.")
3890 (defvar org-agenda-last-marker-time (org-float-time)
3891 "Creation time of the last agenda marker.")
3893 (defun org-agenda-new-marker (&optional pos)
3894 "Return a new agenda marker.
3895 Org-mode keeps a list of these markers and resets them when they are
3896 no longer in use."
3897 (let ((m (copy-marker (or pos (point)))))
3898 (setq org-agenda-last-marker-time (org-float-time))
3899 (if org-agenda-buffer
3900 (with-current-buffer org-agenda-buffer
3901 (push m org-agenda-markers))
3902 (push m org-agenda-markers))
3905 (defun org-agenda-reset-markers ()
3906 "Reset markers created by `org-agenda'."
3907 (while org-agenda-markers
3908 (move-marker (pop org-agenda-markers) nil)))
3910 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3911 "Save relative positions of markers in region.
3912 This check for agenda markers in all agenda buffers currently active."
3913 (dolist (buf (buffer-list))
3914 (with-current-buffer buf
3915 (when (eq major-mode 'org-agenda-mode)
3916 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3917 org-agenda-markers)))))
3919 ;;; Entry text mode
3921 (defun org-agenda-entry-text-show-here ()
3922 "Add some text from the entry as context to the current line."
3923 (let (m txt o)
3924 (setq m (org-get-at-bol 'org-hd-marker))
3925 (unless (marker-buffer m)
3926 (error "No marker points to an entry here"))
3927 (setq txt (concat "\n" (org-no-properties
3928 (org-agenda-get-some-entry-text
3929 m org-agenda-entry-text-maxlines
3930 org-agenda-entry-text-leaders))))
3931 (when (string-match "\\S-" txt)
3932 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3933 (overlay-put o 'evaporate t)
3934 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3935 (overlay-put o 'after-string txt))))
3937 (defun org-agenda-entry-text-show ()
3938 "Add entry context for all agenda lines."
3939 (interactive)
3940 (save-excursion
3941 (goto-char (point-max))
3942 (beginning-of-line 1)
3943 (while (not (bobp))
3944 (when (org-get-at-bol 'org-hd-marker)
3945 (org-agenda-entry-text-show-here))
3946 (beginning-of-line 0))))
3948 (defun org-agenda-entry-text-hide ()
3949 "Remove any shown entry context."
3950 (delq nil
3951 (mapcar (lambda (o)
3952 (if (eq (overlay-get o 'org-overlay-type)
3953 'agenda-entry-content)
3954 (progn (delete-overlay o) t)))
3955 (overlays-in (point-min) (point-max)))))
3957 (defun org-agenda-get-day-face (date)
3958 "Return the face DATE should be displayed with."
3959 (or (and (functionp org-agenda-day-face-function)
3960 (funcall org-agenda-day-face-function date))
3961 (cond ((org-agenda-todayp date)
3962 'org-agenda-date-today)
3963 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3964 'org-agenda-date-weekend)
3965 (t 'org-agenda-date))))
3967 ;;; Agenda timeline
3969 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3971 (defun org-timeline (&optional dotodo)
3972 "Show a time-sorted view of the entries in the current org file.
3973 Only entries with a time stamp of today or later will be listed. With
3974 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3975 under the current date.
3976 If the buffer contains an active region, only check the region for
3977 dates."
3978 (interactive "P")
3979 (let* ((dopast t)
3980 (org-agenda-show-log-scoped org-agenda-show-log)
3981 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3982 (current-buffer))))
3983 (date (calendar-current-date))
3984 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3985 (end (if (org-region-active-p) (region-end) (point-max)))
3986 (day-numbers (org-get-all-dates
3987 beg end 'no-ranges
3988 t org-agenda-show-log-scoped ; always include today
3989 org-timeline-show-empty-dates))
3990 (org-deadline-warning-days 0)
3991 (org-agenda-only-exact-dates t)
3992 (today (org-today))
3993 (past t)
3994 args
3995 s e rtn d emptyp)
3996 (setq org-agenda-redo-command
3997 (list 'progn
3998 (list 'org-switch-to-buffer-other-window (current-buffer))
3999 (list 'org-timeline (list 'quote dotodo))))
4000 (if (not dopast)
4001 ;; Remove past dates from the list of dates.
4002 (setq day-numbers (delq nil (mapcar (lambda(x)
4003 (if (>= x today) x nil))
4004 day-numbers))))
4005 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4006 (org-compile-prefix-format 'timeline)
4007 (org-set-sorting-strategy 'timeline)
4008 (if org-agenda-show-log-scoped (push :closed args))
4009 (push :timestamp args)
4010 (push :deadline args)
4011 (push :scheduled args)
4012 (push :sexp args)
4013 (if dotodo (push :todo args))
4014 (insert "Timeline of file " entry "\n")
4015 (add-text-properties (point-min) (point)
4016 (list 'face 'org-agenda-structure))
4017 (org-agenda-mark-header-line (point-min))
4018 (while (setq d (pop day-numbers))
4019 (if (and (listp d) (eq (car d) :omitted))
4020 (progn
4021 (setq s (point))
4022 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4023 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4024 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4025 (if (and (>= d today)
4026 dopast
4027 past)
4028 (progn
4029 (setq past nil)
4030 (insert (make-string 79 ?-) "\n")))
4031 (setq date (calendar-gregorian-from-absolute d))
4032 (setq s (point))
4033 (setq rtn (and (not emptyp)
4034 (apply 'org-agenda-get-day-entries entry
4035 date args)))
4036 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4037 (progn
4038 (insert
4039 (if (stringp org-agenda-format-date)
4040 (format-time-string org-agenda-format-date
4041 (org-time-from-absolute date))
4042 (funcall org-agenda-format-date date))
4043 "\n")
4044 (put-text-property s (1- (point)) 'face
4045 (org-agenda-get-day-face date))
4046 (put-text-property s (1- (point)) 'org-date-line t)
4047 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4048 (if (equal d today)
4049 (put-text-property s (1- (point)) 'org-today t))
4050 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4051 (put-text-property s (1- (point)) 'day d)))))
4052 (goto-char (point-min))
4053 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4054 (point-min)))
4055 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
4056 (org-agenda-finalize)
4057 (setq buffer-read-only t)))
4059 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4060 "Return a list of all relevant day numbers from BEG to END buffer positions.
4061 If NO-RANGES is non-nil, include only the start and end dates of a range,
4062 not every single day in the range. If FORCE-TODAY is non-nil, make
4063 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4064 inactive time stamps (those in square brackets) are included.
4065 When EMPTY is non-nil, also include days without any entries."
4066 (let ((re (concat
4067 (if pre-re pre-re "")
4068 (if inactive org-ts-regexp-both org-ts-regexp)))
4069 dates dates1 date day day1 day2 ts1 ts2 pos)
4070 (if force-today
4071 (setq dates (list (org-today))))
4072 (save-excursion
4073 (goto-char beg)
4074 (while (re-search-forward re end t)
4075 (setq day (time-to-days (org-time-string-to-time
4076 (substring (match-string 1) 0 10)
4077 (current-buffer) (match-beginning 0))))
4078 (or (memq day dates) (push day dates)))
4079 (unless no-ranges
4080 (goto-char beg)
4081 (while (re-search-forward org-tr-regexp end t)
4082 (setq pos (match-beginning 0))
4083 (setq ts1 (substring (match-string 1) 0 10)
4084 ts2 (substring (match-string 2) 0 10)
4085 day1 (time-to-days (org-time-string-to-time
4086 ts1 (current-buffer) pos))
4087 day2 (time-to-days (org-time-string-to-time
4088 ts2 (current-buffer) pos)))
4089 (while (< (setq day1 (1+ day1)) day2)
4090 (or (memq day1 dates) (push day1 dates)))))
4091 (setq dates (sort dates '<))
4092 (when empty
4093 (while (setq day (pop dates))
4094 (setq day2 (car dates))
4095 (push day dates1)
4096 (when (and day2 empty)
4097 (if (or (eq empty t)
4098 (and (numberp empty) (<= (- day2 day) empty)))
4099 (while (< (setq day (1+ day)) day2)
4100 (push (list day) dates1))
4101 (push (cons :omitted (- day2 day)) dates1))))
4102 (setq dates (nreverse dates1)))
4103 dates)))
4105 ;;; Agenda Daily/Weekly
4107 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4108 "Start day for the agenda view.
4109 Custom commands can set this variable in the options section.
4110 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4111 input allowed when reading a date through the Org calendar.
4112 See the docstring of `org-read-date' for details.")
4113 (defvar org-starting-day nil) ; local variable in the agenda buffer
4114 (defvar org-arg-loc nil) ; local variable
4116 (defvar org-agenda-buffer-tmp-name nil)
4117 ;;;###autoload
4118 (defun org-agenda-list (&optional arg start-day span with-hour)
4119 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4120 The view will be for the current day or week, but from the overview buffer
4121 you will be able to go to other days/weeks.
4123 With a numeric prefix argument in an interactive call, the agenda will
4124 span ARG days. Lisp programs should instead specify SPAN to change
4125 the number of days. SPAN defaults to `org-agenda-span'.
4127 START-DAY defaults to TODAY, or to the most recent match for the weekday
4128 given in `org-agenda-start-on-weekday'.
4130 When WITH-HOUR is non-nil, only include scheduled and deadline
4131 items if they have an hour specification like [h]h:mm."
4132 (interactive "P")
4133 (if org-agenda-overriding-arguments
4134 (setq arg (car org-agenda-overriding-arguments)
4135 start-day (nth 1 org-agenda-overriding-arguments)
4136 span (nth 2 org-agenda-overriding-arguments)))
4137 (if (and (integerp arg) (> arg 0))
4138 (setq span arg arg nil))
4139 (catch 'exit
4140 (setq org-agenda-buffer-name
4141 (or org-agenda-buffer-tmp-name
4142 (if org-agenda-sticky
4143 (cond ((and org-keys (stringp org-match))
4144 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4145 (org-keys
4146 (format "*Org Agenda(%s)*" org-keys))
4147 (t "*Org Agenda(a)*")))
4148 org-agenda-buffer-name))
4149 (org-agenda-prepare "Day/Week")
4150 (setq start-day (or start-day org-agenda-start-day))
4151 (if (stringp start-day)
4152 ;; Convert to an absolute day number
4153 (setq start-day (time-to-days (org-read-date nil t start-day))))
4154 (org-compile-prefix-format 'agenda)
4155 (org-set-sorting-strategy 'agenda)
4156 (let* ((span (org-agenda-ndays-to-span
4157 (or span org-agenda-ndays org-agenda-span)))
4158 (today (org-today))
4159 (sd (or start-day today))
4160 (ndays (org-agenda-span-to-ndays span sd))
4161 (org-agenda-start-on-weekday
4162 (if (eq ndays 7)
4163 org-agenda-start-on-weekday))
4164 (thefiles (org-agenda-files nil 'ifmode))
4165 (files thefiles)
4166 (start (if (or (null org-agenda-start-on-weekday)
4167 (< ndays 7))
4169 (let* ((nt (calendar-day-of-week
4170 (calendar-gregorian-from-absolute sd)))
4171 (n1 org-agenda-start-on-weekday)
4172 (d (- nt n1)))
4173 (- sd (+ (if (< d 0) 7 0) d)))))
4174 (day-numbers (list start))
4175 (day-cnt 0)
4176 (inhibit-redisplay (not debug-on-error))
4177 (org-agenda-show-log-scoped org-agenda-show-log)
4178 s e rtn rtnall file date d start-pos end-pos todayp
4179 clocktable-start clocktable-end filter)
4180 (setq org-agenda-redo-command
4181 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4182 (dotimes (n (1- ndays))
4183 (push (1+ (car day-numbers)) day-numbers))
4184 (setq day-numbers (nreverse day-numbers))
4185 (setq clocktable-start (car day-numbers)
4186 clocktable-end (1+ (or (org-last day-numbers) 0)))
4187 (org-set-local 'org-starting-day (car day-numbers))
4188 (org-set-local 'org-arg-loc arg)
4189 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4190 (unless org-agenda-compact-blocks
4191 (let* ((d1 (car day-numbers))
4192 (d2 (org-last day-numbers))
4193 (w1 (org-days-to-iso-week d1))
4194 (w2 (org-days-to-iso-week d2)))
4195 (setq s (point))
4196 (if org-agenda-overriding-header
4197 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4198 nil 'face 'org-agenda-structure) "\n")
4199 (insert (org-agenda-span-name span)
4200 "-agenda"
4201 (if (< (- d2 d1) 350)
4202 (if (= w1 w2)
4203 (format " (W%02d)" w1)
4204 (format " (W%02d-W%02d)" w1 w2))
4206 ":\n")))
4207 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4208 'org-date-line t))
4209 (org-agenda-mark-header-line s))
4210 (while (setq d (pop day-numbers))
4211 (setq date (calendar-gregorian-from-absolute d)
4212 s (point))
4213 (if (or (setq todayp (= d today))
4214 (and (not start-pos) (= d sd)))
4215 (setq start-pos (point))
4216 (if (and start-pos (not end-pos))
4217 (setq end-pos (point))))
4218 (setq files thefiles
4219 rtnall nil)
4220 (while (setq file (pop files))
4221 (catch 'nextfile
4222 (org-check-agenda-file file)
4223 (let ((org-agenda-entry-types org-agenda-entry-types))
4224 ;; Starred types override non-starred equivalents
4225 (when (member :deadline* org-agenda-entry-types)
4226 (setq org-agenda-entry-types
4227 (delq :deadline org-agenda-entry-types)))
4228 (when (member :scheduled* org-agenda-entry-types)
4229 (setq org-agenda-entry-types
4230 (delq :scheduled org-agenda-entry-types)))
4231 ;; Honor with-hour
4232 (when with-hour
4233 (when (member :deadline org-agenda-entry-types)
4234 (setq org-agenda-entry-types
4235 (delq :deadline org-agenda-entry-types))
4236 (push :deadline* org-agenda-entry-types))
4237 (when (member :scheduled org-agenda-entry-types)
4238 (setq org-agenda-entry-types
4239 (delq :scheduled org-agenda-entry-types))
4240 (push :scheduled* org-agenda-entry-types)))
4241 (unless org-agenda-include-deadlines
4242 (setq org-agenda-entry-types
4243 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4244 (cond
4245 ((memq org-agenda-show-log-scoped '(only clockcheck))
4246 (setq rtn (org-agenda-get-day-entries
4247 file date :closed)))
4248 (org-agenda-show-log-scoped
4249 (setq rtn (apply 'org-agenda-get-day-entries
4250 file date
4251 (append '(:closed) org-agenda-entry-types))))
4253 (setq rtn (apply 'org-agenda-get-day-entries
4254 file date
4255 org-agenda-entry-types)))))
4256 (setq rtnall (append rtnall rtn)))) ;; all entries
4257 (if org-agenda-include-diary
4258 (let ((org-agenda-search-headline-for-time t))
4259 (require 'diary-lib)
4260 (setq rtn (org-get-entries-from-diary date))
4261 (setq rtnall (append rtnall rtn))))
4262 (if (or rtnall org-agenda-show-all-dates)
4263 (progn
4264 (setq day-cnt (1+ day-cnt))
4265 (insert
4266 (if (stringp org-agenda-format-date)
4267 (format-time-string org-agenda-format-date
4268 (org-time-from-absolute date))
4269 (funcall org-agenda-format-date date))
4270 "\n")
4271 (put-text-property s (1- (point)) 'face
4272 (org-agenda-get-day-face date))
4273 (put-text-property s (1- (point)) 'org-date-line t)
4274 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4275 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4276 (when todayp
4277 (put-text-property s (1- (point)) 'org-today t))
4278 (setq rtnall
4279 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4280 (if rtnall (insert ;; all entries
4281 (org-agenda-finalize-entries rtnall 'agenda)
4282 "\n"))
4283 (put-text-property s (1- (point)) 'day d)
4284 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4285 (when (and org-agenda-clockreport-mode clocktable-start)
4286 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4287 ;; the above line is to ensure the restricted range!
4288 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4289 tbl)
4290 (setq p (org-plist-delete p :block))
4291 (setq p (plist-put p :tstart clocktable-start))
4292 (setq p (plist-put p :tend clocktable-end))
4293 (setq p (plist-put p :scope 'agenda))
4294 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4295 (setq filter (or org-agenda-tag-filter-while-redo
4296 (get 'org-agenda-tag-filter :preset-filter))))
4297 (setq p (plist-put p :tags (mapconcat (lambda (x)
4298 (if (string-match "[<>=]" x)
4301 filter ""))))
4302 (setq tbl (apply 'org-clock-get-clocktable p))
4303 (insert tbl)))
4304 (goto-char (point-min))
4305 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4306 (unless (and (pos-visible-in-window-p (point-min))
4307 (pos-visible-in-window-p (point-max)))
4308 (goto-char (1- (point-max)))
4309 (recenter -1)
4310 (if (not (pos-visible-in-window-p (or start-pos 1)))
4311 (progn
4312 (goto-char (or start-pos 1))
4313 (recenter 1))))
4314 (goto-char (or start-pos 1))
4315 (add-text-properties (point-min) (point-max)
4316 `(org-agenda-type agenda
4317 org-last-args (,arg ,start-day ,span)
4318 org-redo-cmd ,org-agenda-redo-command
4319 org-series-cmd ,org-cmd))
4320 (if (eq org-agenda-show-log-scoped 'clockcheck)
4321 (org-agenda-show-clocking-issues))
4322 (org-agenda-finalize)
4323 (setq buffer-read-only t)
4324 (message ""))))
4326 (defun org-agenda-ndays-to-span (n)
4327 "Return a span symbol for a span of N days, or N if none matches."
4328 (cond ((symbolp n) n)
4329 ((= n 1) 'day)
4330 ((= n 7) 'week)
4331 (t n)))
4333 (defun org-agenda-span-to-ndays (span &optional start-day)
4334 "Return ndays from SPAN, possibly starting at START-DAY."
4335 (cond ((numberp span) span)
4336 ((eq span 'day) 1)
4337 ((eq span 'week) 7)
4338 ((eq span 'month)
4339 (let ((date (calendar-gregorian-from-absolute start-day)))
4340 (calendar-last-day-of-month (car date) (caddr date))))
4341 ((eq span 'year)
4342 (let ((date (calendar-gregorian-from-absolute start-day)))
4343 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4345 (defun org-agenda-span-name (span)
4346 "Return a SPAN name."
4347 (if (null span)
4349 (if (symbolp span)
4350 (capitalize (symbol-name span))
4351 (format "%d days" span))))
4353 ;;; Agenda word search
4355 (defvar org-agenda-search-history nil)
4357 (defvar org-search-syntax-table nil
4358 "Special syntax table for org-mode search.
4359 In this table, we have single quotes not as word constituents, to
4360 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4362 (defvar org-mode-syntax-table) ; From org.el
4363 (defun org-search-syntax-table ()
4364 (unless org-search-syntax-table
4365 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4366 (modify-syntax-entry ?' "." org-search-syntax-table)
4367 (modify-syntax-entry ?` "." org-search-syntax-table))
4368 org-search-syntax-table)
4370 (defvar org-agenda-last-search-view-search-was-boolean nil)
4372 ;;;###autoload
4373 (defun org-search-view (&optional todo-only string edit-at)
4374 "Show all entries that contain a phrase or words or regular expressions.
4376 With optional prefix argument TODO-ONLY, only consider entries that are
4377 TODO entries. The argument STRING can be used to pass a default search
4378 string into this function. If EDIT-AT is non-nil, it means that the
4379 user should get a chance to edit this string, with cursor at position
4380 EDIT-AT.
4382 The search string can be viewed either as a phrase that should be found as
4383 is, or it can be broken into a number of snippets, each of which must match
4384 in a Boolean way to select an entry. The default depends on the variable
4385 `org-agenda-search-view-always-boolean'.
4386 Even if this is turned off (the default) you can always switch to
4387 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4389 The default is a direct search of the whole phrase, where each space in
4390 the search string can expand to an arbitrary amount of whitespace,
4391 including newlines.
4393 If using a Boolean search, the search string is split on whitespace and
4394 each snippet is searched separately, with logical AND to select an entry.
4395 Words prefixed with a minus must *not* occur in the entry. Words without
4396 a prefix or prefixed with a plus must occur in the entry. Matching is
4397 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4398 match whole words, not parts of a word) if
4399 `org-agenda-search-view-force-full-words' is set (default is nil).
4401 Boolean search snippets enclosed by curly braces are interpreted as
4402 regular expressions that must or (when preceded with \"-\") must not
4403 match in the entry. Snippets enclosed into double quotes will be taken
4404 as a whole, to include whitespace.
4406 - If the search string starts with an asterisk, search only in headlines.
4407 - If (possibly after the leading star) the search string starts with an
4408 exclamation mark, this also means to look at TODO entries only, an effect
4409 that can also be achieved with a prefix argument.
4410 - If (possibly after star and exclamation mark) the search string starts
4411 with a colon, this will mean that the (non-regexp) snippets of the
4412 Boolean search must match as full words.
4414 This command searches the agenda files, and in addition the files listed
4415 in `org-agenda-text-search-extra-files'."
4416 (interactive "P")
4417 (if org-agenda-overriding-arguments
4418 (setq todo-only (car org-agenda-overriding-arguments)
4419 string (nth 1 org-agenda-overriding-arguments)
4420 edit-at (nth 2 org-agenda-overriding-arguments)))
4421 (let* ((props (list 'face nil
4422 'done-face 'org-agenda-done
4423 'org-not-done-regexp org-not-done-regexp
4424 'org-todo-regexp org-todo-regexp
4425 'org-complex-heading-regexp org-complex-heading-regexp
4426 'mouse-face 'highlight
4427 'help-echo (format "mouse-2 or RET jump to location")))
4428 (full-words org-agenda-search-view-force-full-words)
4429 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4430 regexp rtn rtnall files file pos inherited-tags
4431 marker category category-pos level tags c neg re boolean
4432 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4433 (unless (and (not edit-at)
4434 (stringp string)
4435 (string-match "\\S-" string))
4436 (setq string (read-string
4437 (if org-agenda-search-view-always-boolean
4438 "[+-]Word/{Regexp} ...: "
4439 "Phrase or [+-]Word/{Regexp} ...: ")
4440 (cond
4441 ((integerp edit-at) (cons string edit-at))
4442 (edit-at string))
4443 'org-agenda-search-history)))
4444 (catch 'exit
4445 (if org-agenda-sticky
4446 (setq org-agenda-buffer-name
4447 (if (stringp string)
4448 (format "*Org Agenda(%s:%s)*"
4449 (or org-keys (or (and todo-only "S") "s")) string)
4450 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4451 (org-agenda-prepare "SEARCH")
4452 (org-compile-prefix-format 'search)
4453 (org-set-sorting-strategy 'search)
4454 (setq org-agenda-redo-command
4455 (list 'org-search-view (if todo-only t nil)
4456 (list 'if 'current-prefix-arg nil string)))
4457 (setq org-agenda-query-string string)
4458 (if (equal (string-to-char string) ?*)
4459 (setq hdl-only t
4460 words (substring string 1))
4461 (setq words string))
4462 (when (equal (string-to-char words) ?!)
4463 (setq todo-only t
4464 words (substring words 1)))
4465 (when (equal (string-to-char words) ?:)
4466 (setq full-words t
4467 words (substring words 1)))
4468 (if (or org-agenda-search-view-always-boolean
4469 (member (string-to-char words) '(?- ?+ ?\{)))
4470 (setq boolean t))
4471 (setq words (org-split-string words))
4472 (let (www w)
4473 (while (setq w (pop words))
4474 (while (and (string-match "\\\\\\'" w) words)
4475 (setq w (concat (substring w 0 -1) " " (pop words))))
4476 (push w www))
4477 (setq words (nreverse www) www nil)
4478 (while (setq w (pop words))
4479 (when (and (string-match "\\`[-+]?{" w)
4480 (not (string-match "}\\'" w)))
4481 (while (and words (not (string-match "}\\'" (car words))))
4482 (setq w (concat w " " (pop words))))
4483 (setq w (concat w " " (pop words))))
4484 (push w www))
4485 (setq words (nreverse www)))
4486 (setq org-agenda-last-search-view-search-was-boolean boolean)
4487 (when boolean
4488 (let (wds w)
4489 (while (setq w (pop words))
4490 (if (or (equal (substring w 0 1) "\"")
4491 (and (> (length w) 1)
4492 (member (substring w 0 1) '("+" "-"))
4493 (equal (substring w 1 2) "\"")))
4494 (while (and words (not (equal (substring w -1) "\"")))
4495 (setq w (concat w " " (pop words)))))
4496 (and (string-match "\\`\\([-+]?\\)\"" w)
4497 (setq w (replace-match "\\1" nil nil w)))
4498 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4499 (push w wds))
4500 (setq words (nreverse wds))))
4501 (if boolean
4502 (mapc (lambda (w)
4503 (setq c (string-to-char w))
4504 (if (equal c ?-)
4505 (setq neg t w (substring w 1))
4506 (if (equal c ?+)
4507 (setq neg nil w (substring w 1))
4508 (setq neg nil)))
4509 (if (string-match "\\`{.*}\\'" w)
4510 (setq re (substring w 1 -1))
4511 (if full-words
4512 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4513 (setq re (regexp-quote (downcase w)))))
4514 (if neg (push re regexps-) (push re regexps+)))
4515 words)
4516 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4517 regexps+))
4518 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4519 (if (not regexps+)
4520 (setq regexp org-outline-regexp-bol)
4521 (setq regexp (pop regexps+))
4522 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4523 regexp))))
4524 (setq files (org-agenda-files nil 'ifmode))
4525 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4526 (pop org-agenda-text-search-extra-files)
4527 (setq files (org-add-archive-files files)))
4528 (setq files (append files org-agenda-text-search-extra-files)
4529 rtnall nil)
4530 (while (setq file (pop files))
4531 (setq ee nil)
4532 (catch 'nextfile
4533 (org-check-agenda-file file)
4534 (setq buffer (if (file-exists-p file)
4535 (org-get-agenda-file-buffer file)
4536 (error "No such file %s" file)))
4537 (if (not buffer)
4538 ;; If file does not exist, make sure an error message is sent
4539 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4540 file))))
4541 (with-current-buffer buffer
4542 (with-syntax-table (org-search-syntax-table)
4543 (unless (derived-mode-p 'org-mode)
4544 (error "Agenda file %s is not in `org-mode'" file))
4545 (let ((case-fold-search t))
4546 (save-excursion
4547 (save-restriction
4548 (if org-agenda-restrict
4549 (narrow-to-region org-agenda-restrict-begin
4550 org-agenda-restrict-end)
4551 (widen))
4552 (goto-char (point-min))
4553 (unless (or (org-at-heading-p)
4554 (outline-next-heading))
4555 (throw 'nextfile t))
4556 (goto-char (max (point-min) (1- (point))))
4557 (while (re-search-forward regexp nil t)
4558 (org-back-to-heading t)
4559 (while (and org-agenda-search-view-max-outline-level
4560 (> (org-reduced-level (org-outline-level))
4561 org-agenda-search-view-max-outline-level)
4562 (forward-line -1)
4563 (outline-back-to-heading t)))
4564 (skip-chars-forward "* ")
4565 (setq beg (point-at-bol)
4566 beg1 (point)
4567 end (progn
4568 (outline-next-heading)
4569 (while (and org-agenda-search-view-max-outline-level
4570 (> (org-reduced-level (org-outline-level))
4571 org-agenda-search-view-max-outline-level)
4572 (forward-line 1)
4573 (outline-next-heading)))
4574 (point)))
4576 (catch :skip
4577 (goto-char beg)
4578 (org-agenda-skip)
4579 (setq str (buffer-substring-no-properties
4580 (point-at-bol)
4581 (if hdl-only (point-at-eol) end)))
4582 (mapc (lambda (wr) (when (string-match wr str)
4583 (goto-char (1- end))
4584 (throw :skip t)))
4585 regexps-)
4586 (mapc (lambda (wr) (unless (string-match wr str)
4587 (goto-char (1- end))
4588 (throw :skip t)))
4589 (if todo-only
4590 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4591 regexps+)
4592 regexps+))
4593 (goto-char beg)
4594 (setq marker (org-agenda-new-marker (point))
4595 category (org-get-category)
4596 level (make-string (org-reduced-level (org-outline-level)) ? )
4597 category-pos (get-text-property (point) 'org-category-position)
4598 inherited-tags
4599 (or (eq org-agenda-show-inherited-tags 'always)
4600 (and (listp org-agenda-show-inherited-tags)
4601 (memq 'todo org-agenda-show-inherited-tags))
4602 (and (eq org-agenda-show-inherited-tags t)
4603 (or (eq org-agenda-use-tag-inheritance t)
4604 (memq 'todo org-agenda-use-tag-inheritance))))
4605 tags (org-get-tags-at nil (not inherited-tags))
4606 txt (org-agenda-format-item
4608 (buffer-substring-no-properties
4609 beg1 (point-at-eol))
4610 level category tags t))
4611 (org-add-props txt props
4612 'org-marker marker 'org-hd-marker marker
4613 'org-todo-regexp org-todo-regexp
4614 'level level
4615 'org-complex-heading-regexp org-complex-heading-regexp
4616 'priority 1000 'org-category category
4617 'org-category-position category-pos
4618 'type "search")
4619 (push txt ee)
4620 (goto-char (1- end))))))))))
4621 (setq rtn (nreverse ee))
4622 (setq rtnall (append rtnall rtn)))
4623 (if org-agenda-overriding-header
4624 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4625 nil 'face 'org-agenda-structure) "\n")
4626 (insert "Search words: ")
4627 (add-text-properties (point-min) (1- (point))
4628 (list 'face 'org-agenda-structure))
4629 (setq pos (point))
4630 (insert string "\n")
4631 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4632 (setq pos (point))
4633 (unless org-agenda-multi
4634 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4635 (add-text-properties pos (1- (point))
4636 (list 'face 'org-agenda-structure))))
4637 (org-agenda-mark-header-line (point-min))
4638 (when rtnall
4639 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4640 (goto-char (point-min))
4641 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4642 (add-text-properties (point-min) (point-max)
4643 `(org-agenda-type search
4644 org-last-args (,todo-only ,string ,edit-at)
4645 org-redo-cmd ,org-agenda-redo-command
4646 org-series-cmd ,org-cmd))
4647 (org-agenda-finalize)
4648 (setq buffer-read-only t))))
4650 ;;; Agenda TODO list
4652 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4653 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4654 (concat
4655 (if (or (equal keywords "ALL") (not keywords))
4656 (propertize "ALL" 'face 'warning)
4657 (mapconcat
4658 (lambda (kw)
4659 (propertize kw 'face (org-get-todo-face kw)))
4660 (org-split-string keywords "|")
4661 "|"))
4662 "\n"))
4664 (defvar org-select-this-todo-keyword nil)
4665 (defvar org-last-arg nil)
4667 ;;;###autoload
4668 (defun org-todo-list (&optional arg)
4669 "Show all (not done) TODO entries from all agenda file in a single list.
4670 The prefix arg can be used to select a specific TODO keyword and limit
4671 the list to these. When using \\[universal-argument], you will be prompted
4672 for a keyword. A numeric prefix directly selects the Nth keyword in
4673 `org-todo-keywords-1'."
4674 (interactive "P")
4675 (if org-agenda-overriding-arguments
4676 (setq arg org-agenda-overriding-arguments))
4677 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4678 (let* ((today (org-today))
4679 (date (calendar-gregorian-from-absolute today))
4680 (kwds org-todo-keywords-for-agenda)
4681 (completion-ignore-case t)
4682 (org-select-this-todo-keyword
4683 (if (stringp arg) arg
4684 (and arg (integerp arg) (> arg 0)
4685 (nth (1- arg) kwds))))
4686 rtn rtnall files file pos)
4687 (when (equal arg '(4))
4688 (setq org-select-this-todo-keyword
4689 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4690 (mapcar 'list kwds) nil nil)))
4691 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4692 (catch 'exit
4693 (if org-agenda-sticky
4694 (setq org-agenda-buffer-name
4695 (if (stringp org-select-this-todo-keyword)
4696 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4697 org-select-this-todo-keyword)
4698 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4699 (org-agenda-prepare "TODO")
4700 (org-compile-prefix-format 'todo)
4701 (org-set-sorting-strategy 'todo)
4702 (setq org-agenda-redo-command
4703 `(org-todo-list (or (and (numberp current-prefix-arg)
4704 current-prefix-arg)
4705 ,org-select-this-todo-keyword
4706 current-prefix-arg ,arg)))
4707 (setq files (org-agenda-files nil 'ifmode)
4708 rtnall nil)
4709 (while (setq file (pop files))
4710 (catch 'nextfile
4711 (org-check-agenda-file file)
4712 (setq rtn (org-agenda-get-day-entries file date :todo))
4713 (setq rtnall (append rtnall rtn))))
4714 (if org-agenda-overriding-header
4715 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4716 nil 'face 'org-agenda-structure) "\n")
4717 (insert "Global list of TODO items of type: ")
4718 (add-text-properties (point-min) (1- (point))
4719 (list 'face 'org-agenda-structure
4720 'short-heading
4721 (concat "ToDo: "
4722 (or org-select-this-todo-keyword "ALL"))))
4723 (org-agenda-mark-header-line (point-min))
4724 (insert (org-agenda-propertize-selected-todo-keywords
4725 org-select-this-todo-keyword))
4726 (setq pos (point))
4727 (unless org-agenda-multi
4728 (insert "Available with `N r': (0)[ALL]")
4729 (let ((n 0) s)
4730 (mapc (lambda (x)
4731 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4732 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4733 (insert "\n "))
4734 (insert " " s))
4735 kwds))
4736 (insert "\n"))
4737 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4738 (org-agenda-mark-header-line (point-min))
4739 (when rtnall
4740 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4741 (goto-char (point-min))
4742 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4743 (add-text-properties (point-min) (point-max)
4744 `(org-agenda-type todo
4745 org-last-args ,arg
4746 org-redo-cmd ,org-agenda-redo-command
4747 org-series-cmd ,org-cmd))
4748 (org-agenda-finalize)
4749 (setq buffer-read-only t))))
4751 ;;; Agenda tags match
4753 ;;;###autoload
4754 (defun org-tags-view (&optional todo-only match)
4755 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4756 The prefix arg TODO-ONLY limits the search to TODO entries."
4757 (interactive "P")
4758 (if org-agenda-overriding-arguments
4759 (setq todo-only (car org-agenda-overriding-arguments)
4760 match (nth 1 org-agenda-overriding-arguments)))
4761 (let* ((org-tags-match-list-sublevels
4762 org-tags-match-list-sublevels)
4763 (completion-ignore-case t)
4764 rtn rtnall files file pos matcher
4765 buffer)
4766 (when (and (stringp match) (not (string-match "\\S-" match)))
4767 (setq match nil))
4768 (catch 'exit
4769 (if org-agenda-sticky
4770 (setq org-agenda-buffer-name
4771 (if (stringp match)
4772 (format "*Org Agenda(%s:%s)*"
4773 (or org-keys (or (and todo-only "M") "m")) match)
4774 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4775 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4776 ;; expanding tags within `org-make-tags-matcher'
4777 (org-agenda-prepare (concat "TAGS " match))
4778 (setq matcher (org-make-tags-matcher match)
4779 match (car matcher) matcher (cdr matcher))
4780 (org-compile-prefix-format 'tags)
4781 (org-set-sorting-strategy 'tags)
4782 (setq org-agenda-query-string match)
4783 (setq org-agenda-redo-command
4784 (list 'org-tags-view `(quote ,todo-only)
4785 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4786 (setq files (org-agenda-files nil 'ifmode)
4787 rtnall nil)
4788 (while (setq file (pop files))
4789 (catch 'nextfile
4790 (org-check-agenda-file file)
4791 (setq buffer (if (file-exists-p file)
4792 (org-get-agenda-file-buffer file)
4793 (error "No such file %s" file)))
4794 (if (not buffer)
4795 ;; If file does not exist, error message to agenda
4796 (setq rtn (list
4797 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4798 rtnall (append rtnall rtn))
4799 (with-current-buffer buffer
4800 (unless (derived-mode-p 'org-mode)
4801 (error "Agenda file %s is not in `org-mode'" file))
4802 (save-excursion
4803 (save-restriction
4804 (if org-agenda-restrict
4805 (narrow-to-region org-agenda-restrict-begin
4806 org-agenda-restrict-end)
4807 (widen))
4808 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4809 (setq rtnall (append rtnall rtn))))))))
4810 (if org-agenda-overriding-header
4811 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4812 nil 'face 'org-agenda-structure) "\n")
4813 (insert "Headlines with TAGS match: ")
4814 (add-text-properties (point-min) (1- (point))
4815 (list 'face 'org-agenda-structure
4816 'short-heading
4817 (concat "Match: " match)))
4818 (setq pos (point))
4819 (insert match "\n")
4820 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4821 (setq pos (point))
4822 (unless org-agenda-multi
4823 (insert "Press `C-u r' to search again with new search string\n"))
4824 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4825 (org-agenda-mark-header-line (point-min))
4826 (when rtnall
4827 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4828 (goto-char (point-min))
4829 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4830 (add-text-properties (point-min) (point-max)
4831 `(org-agenda-type tags
4832 org-last-args (,todo-only ,match)
4833 org-redo-cmd ,org-agenda-redo-command
4834 org-series-cmd ,org-cmd))
4835 (org-agenda-finalize)
4836 (setq buffer-read-only t))))
4838 ;;; Agenda Finding stuck projects
4840 (defvar org-agenda-skip-regexp nil
4841 "Regular expression used in skipping subtrees for the agenda.
4842 This is basically a temporary global variable that can be set and then
4843 used by user-defined selections using `org-agenda-skip-function'.")
4845 (defvar org-agenda-overriding-header nil
4846 "When set during agenda, todo and tags searches it replaces the header.
4847 This variable should not be set directly, but custom commands can bind it
4848 in the options section.")
4850 (defun org-agenda-skip-entry-when-regexp-matches ()
4851 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4852 If yes, it returns the end position of this entry, causing agenda commands
4853 to skip the entry but continuing the search in the subtree. This is a
4854 function that can be put into `org-agenda-skip-function' for the duration
4855 of a command."
4856 (let ((end (save-excursion (org-end-of-subtree t)))
4857 skip)
4858 (save-excursion
4859 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4860 (and skip end)))
4862 (defun org-agenda-skip-subtree-when-regexp-matches ()
4863 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4864 If yes, it returns the end position of this tree, causing agenda commands
4865 to skip this subtree. This is a function that can be put into
4866 `org-agenda-skip-function' for the duration of a command."
4867 (let ((end (save-excursion (org-end-of-subtree t)))
4868 skip)
4869 (save-excursion
4870 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4871 (and skip end)))
4873 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4874 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4875 If yes, it returns the end position of the current entry (NOT the tree),
4876 causing agenda commands to skip the entry but continuing the search in
4877 the subtree. This is a function that can be put into
4878 `org-agenda-skip-function' for the duration of a command. An important
4879 use of this function is for the stuck project list."
4880 (let ((end (save-excursion (org-end-of-subtree t)))
4881 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4882 skip)
4883 (save-excursion
4884 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4885 (and skip entry-end)))
4887 (defun org-agenda-skip-entry-if (&rest conditions)
4888 "Skip entry if any of CONDITIONS is true.
4889 See `org-agenda-skip-if' for details."
4890 (org-agenda-skip-if nil conditions))
4892 (defun org-agenda-skip-subtree-if (&rest conditions)
4893 "Skip entry if any of CONDITIONS is true.
4894 See `org-agenda-skip-if' for details."
4895 (org-agenda-skip-if t conditions))
4897 (defun org-agenda-skip-if (subtree conditions)
4898 "Checks current entity for CONDITIONS.
4899 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4900 the entry (i.e. the text before the next heading) is checked.
4902 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4903 from different tests. Valid conditions are:
4905 scheduled Check if there is a scheduled cookie
4906 notscheduled Check if there is no scheduled cookie
4907 deadline Check if there is a deadline
4908 notdeadline Check if there is no deadline
4909 timestamp Check if there is a timestamp (also deadline or scheduled)
4910 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4911 regexp Check if regexp matches
4912 notregexp Check if regexp does not match.
4913 todo Check if TODO keyword matches
4914 nottodo Check if TODO keyword does not match
4916 The regexp is taken from the conditions list, it must come right after
4917 the `regexp' or `notregexp' element.
4919 `todo' and `nottodo' accept as an argument a list of todo
4920 keywords, which may include \"*\" to match any todo keyword.
4922 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4924 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4926 Instead of a list, a keyword class may be given. For example:
4928 (org-agenda-skip-entry-if 'nottodo 'done)
4930 would skip entries that haven't been marked with any of \"DONE\"
4931 keywords. Possible classes are: `todo', `done', `any'.
4933 If any of these conditions is met, this function returns the end point of
4934 the entity, causing the search to continue from there. This is a function
4935 that can be put into `org-agenda-skip-function' for the duration of a command."
4936 (let (beg end m)
4937 (org-back-to-heading t)
4938 (setq beg (point)
4939 end (if subtree
4940 (progn (org-end-of-subtree t) (point))
4941 (progn (outline-next-heading) (1- (point)))))
4942 (goto-char beg)
4943 (and
4945 (and (memq 'scheduled conditions)
4946 (re-search-forward org-scheduled-time-regexp end t))
4947 (and (memq 'notscheduled conditions)
4948 (not (re-search-forward org-scheduled-time-regexp end t)))
4949 (and (memq 'deadline conditions)
4950 (re-search-forward org-deadline-time-regexp end t))
4951 (and (memq 'notdeadline conditions)
4952 (not (re-search-forward org-deadline-time-regexp end t)))
4953 (and (memq 'timestamp conditions)
4954 (re-search-forward org-ts-regexp end t))
4955 (and (memq 'nottimestamp conditions)
4956 (not (re-search-forward org-ts-regexp end t)))
4957 (and (setq m (memq 'regexp conditions))
4958 (stringp (nth 1 m))
4959 (re-search-forward (nth 1 m) end t))
4960 (and (setq m (memq 'notregexp conditions))
4961 (stringp (nth 1 m))
4962 (not (re-search-forward (nth 1 m) end t)))
4963 (and (or
4964 (setq m (memq 'nottodo conditions))
4965 (setq m (memq 'todo-unblocked conditions))
4966 (setq m (memq 'nottodo-unblocked conditions))
4967 (setq m (memq 'todo conditions)))
4968 (org-agenda-skip-if-todo m end)))
4969 end)))
4971 (defun org-agenda-skip-if-todo (args end)
4972 "Helper function for `org-agenda-skip-if', do not use it directly.
4973 ARGS is a list with first element either `todo', `nottodo',
4974 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4975 a list of TODO keywords, or a state symbol `todo' or `done' or
4976 `any'."
4977 (let ((kw (car args))
4978 (arg (cadr args))
4979 todo-wds todo-re)
4980 (setq todo-wds
4981 (org-uniquify
4982 (cond
4983 ((listp arg) ;; list of keywords
4984 (if (member "*" arg)
4985 (mapcar 'substring-no-properties org-todo-keywords-1)
4986 arg))
4987 ((symbolp arg) ;; keyword class name
4988 (cond
4989 ((eq arg 'todo)
4990 (org-delete-all org-done-keywords
4991 (mapcar 'substring-no-properties
4992 org-todo-keywords-1)))
4993 ((eq arg 'done) org-done-keywords)
4994 ((eq arg 'any)
4995 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4996 (setq todo-re
4997 (concat "^\\*+[ \t]+\\<\\("
4998 (mapconcat 'identity todo-wds "\\|")
4999 "\\)\\>"))
5000 (cond
5001 ((eq kw 'todo) (re-search-forward todo-re end t))
5002 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5003 ((eq kw 'todo-unblocked)
5004 (catch 'unblocked
5005 (while (re-search-forward todo-re end t)
5006 (or (org-entry-blocked-p) (throw 'unblocked t)))
5007 nil))
5008 ((eq kw 'nottodo-unblocked)
5009 (catch 'unblocked
5010 (while (re-search-forward todo-re end t)
5011 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5015 ;;;###autoload
5016 (defun org-agenda-list-stuck-projects (&rest ignore)
5017 "Create agenda view for projects that are stuck.
5018 Stuck projects are project that have no next actions. For the definitions
5019 of what a project is and how to check if it stuck, customize the variable
5020 `org-stuck-projects'."
5021 (interactive)
5022 (let* ((org-agenda-skip-function
5023 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5024 ;; We could have used org-agenda-skip-if here.
5025 (org-agenda-overriding-header
5026 (or org-agenda-overriding-header "List of stuck projects: "))
5027 (matcher (nth 0 org-stuck-projects))
5028 (todo (nth 1 org-stuck-projects))
5029 (todo-wds (if (member "*" todo)
5030 (progn
5031 (org-agenda-prepare-buffers (org-agenda-files
5032 nil 'ifmode))
5033 (org-delete-all
5034 org-done-keywords-for-agenda
5035 (copy-sequence org-todo-keywords-for-agenda)))
5036 todo))
5037 (todo-re (concat "^\\*+[ \t]+\\("
5038 (mapconcat 'identity todo-wds "\\|")
5039 "\\)\\>"))
5040 (tags (nth 2 org-stuck-projects))
5041 (tags-re (if (member "*" tags)
5042 (concat org-outline-regexp-bol
5043 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5044 (if tags
5045 (concat org-outline-regexp-bol
5046 ".*:\\("
5047 (mapconcat 'identity tags "\\|")
5048 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5049 (gen-re (nth 3 org-stuck-projects))
5050 (re-list
5051 (delq nil
5052 (list
5053 (if todo todo-re)
5054 (if tags tags-re)
5055 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5056 gen-re)))))
5057 (setq org-agenda-skip-regexp
5058 (if re-list
5059 (mapconcat 'identity re-list "\\|")
5060 (error "No information how to identify unstuck projects")))
5061 (org-tags-view nil matcher)
5062 (with-current-buffer org-agenda-buffer-name
5063 (setq org-agenda-redo-command
5064 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5066 ;;; Diary integration
5068 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5069 (defvar diary-list-entries-hook)
5070 (defvar diary-time-regexp)
5071 (defun org-get-entries-from-diary (date)
5072 "Get the (Emacs Calendar) diary entries for DATE."
5073 (require 'diary-lib)
5074 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5075 (diary-display-hook '(fancy-diary-display))
5076 (diary-display-function 'fancy-diary-display)
5077 (pop-up-frames nil)
5078 (diary-list-entries-hook
5079 (cons 'org-diary-default-entry diary-list-entries-hook))
5080 (diary-file-name-prefix nil) ; turn this feature off
5081 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5082 entries
5083 (org-disable-agenda-to-diary t))
5084 (save-excursion
5085 (save-window-excursion
5086 (funcall (if (fboundp 'diary-list-entries)
5087 'diary-list-entries 'list-diary-entries)
5088 date 1)))
5089 (if (not (get-buffer diary-fancy-buffer))
5090 (setq entries nil)
5091 (with-current-buffer diary-fancy-buffer
5092 (setq buffer-read-only nil)
5093 (if (zerop (buffer-size))
5094 ;; No entries
5095 (setq entries nil)
5096 ;; Omit the date and other unnecessary stuff
5097 (org-agenda-cleanup-fancy-diary)
5098 ;; Add prefix to each line and extend the text properties
5099 (if (zerop (buffer-size))
5100 (setq entries nil)
5101 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5102 (setq entries
5103 (with-temp-buffer
5104 (insert entries) (goto-char (point-min))
5105 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5106 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5107 (replace-match (concat "; " (match-string 1)))))
5108 (buffer-string)))))
5109 (set-buffer-modified-p nil)
5110 (kill-buffer diary-fancy-buffer)))
5111 (when entries
5112 (setq entries (org-split-string entries "\n"))
5113 (setq entries
5114 (mapcar
5115 (lambda (x)
5116 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5117 ;; Extend the text properties to the beginning of the line
5118 (org-add-props x (text-properties-at (1- (length x)) x)
5119 'type "diary" 'date date 'face 'org-agenda-diary))
5120 entries)))))
5122 (defvar org-agenda-cleanup-fancy-diary-hook nil
5123 "Hook run when the fancy diary buffer is cleaned up.")
5125 (defun org-agenda-cleanup-fancy-diary ()
5126 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5127 This gets rid of the date, the underline under the date, and
5128 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5129 date. It also removes lines that contain only whitespace."
5130 (goto-char (point-min))
5131 (if (looking-at ".*?:[ \t]*")
5132 (progn
5133 (replace-match "")
5134 (re-search-forward "\n=+$" nil t)
5135 (replace-match "")
5136 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5137 (re-search-forward "\n=+$" nil t)
5138 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5139 (goto-char (point-min))
5140 (while (re-search-forward "^ +\n" nil t)
5141 (replace-match ""))
5142 (goto-char (point-min))
5143 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5144 (replace-match ""))
5145 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5147 ;; Make sure entries from the diary have the right text properties.
5148 (eval-after-load "diary-lib"
5149 '(if (boundp 'diary-modify-entry-list-string-function)
5150 ;; We can rely on the hook, nothing to do
5152 ;; Hook not available, must use advice to make this work
5153 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5154 "Make the position visible."
5155 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5156 (stringp string)
5157 buffer-file-name)
5158 (setq string (org-modify-diary-entry-string string))))))
5160 (defun org-modify-diary-entry-string (string)
5161 "Add text properties to string, allowing org-mode to act on it."
5162 (org-add-props string nil
5163 'mouse-face 'highlight
5164 'help-echo (if buffer-file-name
5165 (format "mouse-2 or RET jump to diary file %s"
5166 (abbreviate-file-name buffer-file-name))
5168 'org-agenda-diary-link t
5169 'org-marker (org-agenda-new-marker (point-at-bol))))
5171 (defun org-diary-default-entry ()
5172 "Add a dummy entry to the diary.
5173 Needed to avoid empty dates which mess up holiday display."
5174 ;; Catch the error if dealing with the new add-to-diary-alist
5175 (when org-disable-agenda-to-diary
5176 (condition-case nil
5177 (org-add-to-diary-list original-date "Org-mode dummy" "")
5178 (error
5179 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5181 (defun org-add-to-diary-list (&rest args)
5182 (if (fboundp 'diary-add-to-list)
5183 (apply 'diary-add-to-list args)
5184 (apply 'add-to-diary-list args)))
5186 (defvar org-diary-last-run-time nil)
5188 ;;;###autoload
5189 (defun org-diary (&rest args)
5190 "Return diary information from org files.
5191 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5192 It accesses org files and extracts information from those files to be
5193 listed in the diary. The function accepts arguments specifying what
5194 items should be listed. For a list of arguments allowed here, see the
5195 variable `org-agenda-entry-types'.
5197 The call in the diary file should look like this:
5199 &%%(org-diary) ~/path/to/some/orgfile.org
5201 Use a separate line for each org file to check. Or, if you omit the file name,
5202 all files listed in `org-agenda-files' will be checked automatically:
5204 &%%(org-diary)
5206 If you don't give any arguments (as in the example above), the default value
5207 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5208 So the example above may also be written as
5210 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5212 The function expects the lisp variables `entry' and `date' to be provided
5213 by the caller, because this is how the calendar works. Don't use this
5214 function from a program - use `org-agenda-get-day-entries' instead."
5215 (when (> (- (org-float-time)
5216 org-agenda-last-marker-time)
5218 ;; I am not sure if this works with sticky agendas, because the marker
5219 ;; list is then no longer a global variable.
5220 (org-agenda-reset-markers))
5221 (org-compile-prefix-format 'agenda)
5222 (org-set-sorting-strategy 'agenda)
5223 (setq args (or args org-agenda-entry-types))
5224 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5225 (list entry)
5226 (org-agenda-files t)))
5227 (time (org-float-time))
5228 file rtn results)
5229 (when (or (not org-diary-last-run-time)
5230 (> (- time
5231 org-diary-last-run-time)
5233 (org-agenda-prepare-buffers files))
5234 (setq org-diary-last-run-time time)
5235 ;; If this is called during org-agenda, don't return any entries to
5236 ;; the calendar. Org Agenda will list these entries itself.
5237 (if org-disable-agenda-to-diary (setq files nil))
5238 (while (setq file (pop files))
5239 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5240 (setq results (append results rtn)))
5241 (when results
5242 (setq results
5243 (mapcar (lambda (i) (replace-regexp-in-string
5244 org-bracket-link-regexp "\\3" i)) results))
5245 (concat (org-agenda-finalize-entries results) "\n"))))
5247 ;;; Agenda entry finders
5249 (defun org-agenda-get-day-entries (file date &rest args)
5250 "Does the work for `org-diary' and `org-agenda'.
5251 FILE is the path to a file to be checked for entries. DATE is date like
5252 the one returned by `calendar-current-date'. ARGS are symbols indicating
5253 which kind of entries should be extracted. For details about these, see
5254 the documentation of `org-diary'."
5255 (setq args (or args org-agenda-entry-types))
5256 (let* ((org-startup-folded nil)
5257 (org-startup-align-all-tables nil)
5258 (buffer (if (file-exists-p file)
5259 (org-get-agenda-file-buffer file)
5260 (error "No such file %s" file)))
5261 arg results rtn deadline-results)
5262 (if (not buffer)
5263 ;; If file does not exist, make sure an error message ends up in diary
5264 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5265 (with-current-buffer buffer
5266 (unless (derived-mode-p 'org-mode)
5267 (error "Agenda file %s is not in `org-mode'" file))
5268 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5269 (let ((case-fold-search nil))
5270 (save-excursion
5271 (save-restriction
5272 (if org-agenda-restrict
5273 (narrow-to-region org-agenda-restrict-begin
5274 org-agenda-restrict-end)
5275 (widen))
5276 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5277 (while (setq arg (pop args))
5278 (cond
5279 ((and (eq arg :todo)
5280 (equal date (calendar-gregorian-from-absolute
5281 (org-today))))
5282 (setq rtn (org-agenda-get-todos))
5283 (setq results (append results rtn)))
5284 ((eq arg :timestamp)
5285 (setq rtn (org-agenda-get-blocks))
5286 (setq results (append results rtn))
5287 (setq rtn (org-agenda-get-timestamps deadline-results))
5288 (setq results (append results rtn)))
5289 ((eq arg :sexp)
5290 (setq rtn (org-agenda-get-sexps))
5291 (setq results (append results rtn)))
5292 ((eq arg :scheduled)
5293 (setq rtn (org-agenda-get-scheduled deadline-results))
5294 (setq results (append results rtn)))
5295 ((eq arg :scheduled*)
5296 (setq rtn (org-agenda-get-scheduled deadline-results t))
5297 (setq results (append results rtn)))
5298 ((eq arg :closed)
5299 (setq rtn (org-agenda-get-progress))
5300 (setq results (append results rtn)))
5301 ((eq arg :deadline)
5302 (setq rtn (org-agenda-get-deadlines))
5303 (setq deadline-results (copy-sequence rtn))
5304 (setq results (append results rtn)))
5305 ((eq arg :deadline*)
5306 (setq rtn (org-agenda-get-deadlines t))
5307 (setq deadline-results (copy-sequence rtn))
5308 (setq results (append results rtn))))))))
5309 results))))
5311 (defsubst org-em (x y list)
5312 "Is X or Y a member of LIST?"
5313 (or (memq x list) (memq y list)))
5315 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5316 (defvar org-agenda-sorting-strategy-selected nil)
5318 (defun org-agenda-get-todos ()
5319 "Return the TODO information for agenda display."
5320 (let* ((props (list 'face nil
5321 'done-face 'org-agenda-done
5322 'org-not-done-regexp org-not-done-regexp
5323 'org-todo-regexp org-todo-regexp
5324 'org-complex-heading-regexp org-complex-heading-regexp
5325 'mouse-face 'highlight
5326 'help-echo
5327 (format "mouse-2 or RET jump to org file %s"
5328 (abbreviate-file-name buffer-file-name))))
5329 (regexp (format org-heading-keyword-regexp-format
5330 (cond
5331 ((and org-select-this-todo-keyword
5332 (equal org-select-this-todo-keyword "*"))
5333 org-todo-regexp)
5334 (org-select-this-todo-keyword
5335 (concat "\\("
5336 (mapconcat 'identity
5337 (org-split-string
5338 org-select-this-todo-keyword
5339 "|")
5340 "\\|") "\\)"))
5341 (t org-not-done-regexp))))
5342 marker priority category category-pos level tags todo-state ts-date ts-date-type
5343 ee txt beg end inherited-tags todo-state-end-pos)
5344 (goto-char (point-min))
5345 (while (re-search-forward regexp nil t)
5346 (catch :skip
5347 (save-match-data
5348 (beginning-of-line)
5349 (org-agenda-skip)
5350 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5351 (unless (and (setq todo-state (org-get-todo-state))
5352 (setq todo-state-end-pos (match-end 2)))
5353 (goto-char end)
5354 (throw :skip nil))
5355 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5356 (goto-char (1+ beg))
5357 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5358 (throw :skip nil)))
5359 (goto-char (match-beginning 2))
5360 (setq marker (org-agenda-new-marker (match-beginning 0))
5361 category (org-get-category)
5362 ts-date (let (ts)
5363 (save-match-data
5364 (cond ((org-em 'scheduled-up 'scheduled-down
5365 org-agenda-sorting-strategy-selected)
5366 (setq ts (org-entry-get (point) "SCHEDULED")
5367 ts-date-type " scheduled"))
5368 ((org-em 'deadline-up 'deadline-down
5369 org-agenda-sorting-strategy-selected)
5370 (setq ts (org-entry-get (point) "DEADLINE")
5371 ts-date-type " deadline"))
5372 ((org-em 'ts-up 'ts-down
5373 org-agenda-sorting-strategy-selected)
5374 (setq ts (org-entry-get (point) "TIMESTAMP")
5375 ts-date-type " timestamp"))
5376 ((org-em 'tsia-up 'tsia-down
5377 org-agenda-sorting-strategy-selected)
5378 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5379 ts-date-type " timestamp_ia"))
5380 ((org-em 'timestamp-up 'timestamp-down
5381 org-agenda-sorting-strategy-selected)
5382 (setq ts (or (org-entry-get (point) "SCHEDULED")
5383 (org-entry-get (point) "DEADLINE")
5384 (org-entry-get (point) "TIMESTAMP")
5385 (org-entry-get (point) "TIMESTAMP_IA"))
5386 ts-date-type ""))
5387 (t (setq ts-date-type "")))
5388 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5389 category-pos (get-text-property (point) 'org-category-position)
5390 txt (org-trim
5391 (buffer-substring (match-beginning 2) (match-end 0)))
5392 inherited-tags
5393 (or (eq org-agenda-show-inherited-tags 'always)
5394 (and (listp org-agenda-show-inherited-tags)
5395 (memq 'todo org-agenda-show-inherited-tags))
5396 (and (eq org-agenda-show-inherited-tags t)
5397 (or (eq org-agenda-use-tag-inheritance t)
5398 (memq 'todo org-agenda-use-tag-inheritance))))
5399 tags (org-get-tags-at nil (not inherited-tags))
5400 level (make-string (org-reduced-level (org-outline-level)) ? )
5401 txt (org-agenda-format-item "" txt level category tags t)
5402 priority (1+ (org-get-priority txt)))
5403 (org-add-props txt props
5404 'org-marker marker 'org-hd-marker marker
5405 'priority priority 'org-category category
5406 'level level
5407 'ts-date ts-date
5408 'org-category-position category-pos
5409 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5410 (push txt ee)
5411 (if org-agenda-todo-list-sublevels
5412 (goto-char todo-state-end-pos)
5413 (org-end-of-subtree 'invisible))))
5414 (nreverse ee)))
5416 (defun org-agenda-todo-custom-ignore-p (time n)
5417 "Check whether timestamp is farther away than n number of days.
5418 This function is invoked if `org-agenda-todo-ignore-deadlines',
5419 `org-agenda-todo-ignore-scheduled' or
5420 `org-agenda-todo-ignore-timestamp' is set to an integer."
5421 (let ((days (org-time-stamp-to-now
5422 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5423 (if (>= n 0)
5424 (>= days n)
5425 (<= days n))))
5427 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5428 (&optional end)
5429 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5430 (when (or org-agenda-todo-ignore-with-date
5431 org-agenda-todo-ignore-scheduled
5432 org-agenda-todo-ignore-deadlines
5433 org-agenda-todo-ignore-timestamp)
5434 (setq end (or end (save-excursion (outline-next-heading) (point))))
5435 (save-excursion
5436 (or (and org-agenda-todo-ignore-with-date
5437 (re-search-forward org-ts-regexp end t))
5438 (and org-agenda-todo-ignore-scheduled
5439 (re-search-forward org-scheduled-time-regexp end t)
5440 (cond
5441 ((eq org-agenda-todo-ignore-scheduled 'future)
5442 (> (org-time-stamp-to-now
5443 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5444 ((eq org-agenda-todo-ignore-scheduled 'past)
5445 (<= (org-time-stamp-to-now
5446 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5447 ((numberp org-agenda-todo-ignore-scheduled)
5448 (org-agenda-todo-custom-ignore-p
5449 (match-string 1) org-agenda-todo-ignore-scheduled))
5450 (t)))
5451 (and org-agenda-todo-ignore-deadlines
5452 (re-search-forward org-deadline-time-regexp end t)
5453 (cond
5454 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5455 ((eq org-agenda-todo-ignore-deadlines 'far)
5456 (not (org-deadline-close (match-string 1))))
5457 ((eq org-agenda-todo-ignore-deadlines 'future)
5458 (> (org-time-stamp-to-now
5459 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5460 ((eq org-agenda-todo-ignore-deadlines 'past)
5461 (<= (org-time-stamp-to-now
5462 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5463 ((numberp org-agenda-todo-ignore-deadlines)
5464 (org-agenda-todo-custom-ignore-p
5465 (match-string 1) org-agenda-todo-ignore-deadlines))
5466 (t (org-deadline-close (match-string 1)))))
5467 (and org-agenda-todo-ignore-timestamp
5468 (let ((buffer (current-buffer))
5469 (regexp
5470 (concat
5471 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5472 (start (point)))
5473 ;; Copy current buffer into a temporary one
5474 (with-temp-buffer
5475 (insert-buffer-substring buffer start end)
5476 (goto-char (point-min))
5477 ;; Delete SCHEDULED and DEADLINE items
5478 (while (re-search-forward regexp end t)
5479 (delete-region (match-beginning 0) (match-end 0)))
5480 (goto-char (point-min))
5481 ;; No search for timestamp left
5482 (when (re-search-forward org-ts-regexp nil t)
5483 (cond
5484 ((eq org-agenda-todo-ignore-timestamp 'future)
5485 (> (org-time-stamp-to-now
5486 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5487 ((eq org-agenda-todo-ignore-timestamp 'past)
5488 (<= (org-time-stamp-to-now
5489 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5490 ((numberp org-agenda-todo-ignore-timestamp)
5491 (org-agenda-todo-custom-ignore-p
5492 (match-string 1) org-agenda-todo-ignore-timestamp))
5493 (t))))))))))
5495 (defun org-agenda-get-timestamps (&optional deadline-results)
5496 "Return the date stamp information for agenda display."
5497 (let* ((props (list 'face 'org-agenda-calendar-event
5498 'org-not-done-regexp org-not-done-regexp
5499 'org-todo-regexp org-todo-regexp
5500 'org-complex-heading-regexp org-complex-heading-regexp
5501 'mouse-face 'highlight
5502 'help-echo
5503 (format "mouse-2 or RET jump to org file %s"
5504 (abbreviate-file-name buffer-file-name))))
5505 (d1 (calendar-absolute-from-gregorian date))
5507 (deadline-position-alist
5508 (mapcar (lambda (a) (and (setq mm (get-text-property
5509 0 'org-hd-marker a))
5510 (cons (marker-position mm) a)))
5511 deadline-results))
5512 (remove-re org-ts-regexp)
5513 (regexp
5514 (concat
5515 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5516 (regexp-quote
5517 (substring
5518 (format-time-string
5519 (car org-time-stamp-formats)
5520 (apply 'encode-time ; DATE bound by calendar
5521 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5522 1 11))
5523 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5524 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5525 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5526 donep tmp priority category category-pos level ee txt timestr tags
5527 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5528 inherited-tags ts-date)
5529 (goto-char (point-min))
5530 (while (setq end-of-match (re-search-forward regexp nil t))
5531 (setq b0 (match-beginning 0)
5532 b3 (match-beginning 3) e3 (match-end 3)
5533 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5534 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5535 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5536 (member todo-state
5537 org-agenda-repeating-timestamp-show-all)))
5538 (catch :skip
5539 (and (org-at-date-range-p) (throw :skip nil))
5540 (org-agenda-skip)
5541 (if (and (match-end 1)
5542 (not (= d1 (org-time-string-to-absolute
5543 (match-string 1) d1 nil show-all
5544 (current-buffer) b0))))
5545 (throw :skip nil))
5546 (if (and e3
5547 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5548 (throw :skip nil))
5549 (setq tmp (buffer-substring (max (point-min)
5550 (- b0 org-ds-keyword-length))
5552 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5553 inactivep (= (char-after b0) ?\[)
5554 deadlinep (string-match org-deadline-regexp tmp)
5555 scheduledp (string-match org-scheduled-regexp tmp)
5556 closedp (and org-agenda-include-inactive-timestamps
5557 (string-match org-closed-string tmp))
5558 clockp (and org-agenda-include-inactive-timestamps
5559 (or (string-match org-clock-string tmp)
5560 (string-match "]-+\\'" tmp)))
5561 warntime (get-text-property (point) 'org-appt-warntime)
5562 donep (member todo-state org-done-keywords))
5563 (if (or scheduledp deadlinep closedp clockp
5564 (and donep org-agenda-skip-timestamp-if-done))
5565 (throw :skip t))
5566 (if (string-match ">" timestr)
5567 ;; substring should only run to end of time stamp
5568 (setq timestr (substring timestr 0 (match-end 0))))
5569 (setq marker (org-agenda-new-marker b0)
5570 category (org-get-category b0)
5571 category-pos (get-text-property b0 'org-category-position))
5572 (save-excursion
5573 (if (not (re-search-backward org-outline-regexp-bol nil t))
5574 (throw :skip nil)
5575 (goto-char (match-beginning 0))
5576 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5577 (assoc (point) deadline-position-alist))
5578 (throw :skip nil))
5579 (setq hdmarker (org-agenda-new-marker)
5580 inherited-tags
5581 (or (eq org-agenda-show-inherited-tags 'always)
5582 (and (listp org-agenda-show-inherited-tags)
5583 (memq 'agenda org-agenda-show-inherited-tags))
5584 (and (eq org-agenda-show-inherited-tags t)
5585 (or (eq org-agenda-use-tag-inheritance t)
5586 (memq 'agenda org-agenda-use-tag-inheritance))))
5587 tags (org-get-tags-at nil (not inherited-tags))
5588 level (make-string (org-reduced-level (org-outline-level)) ? ))
5589 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5590 (setq head (or (match-string 1) ""))
5591 (setq txt (org-agenda-format-item
5592 (if inactivep org-agenda-inactive-leader nil)
5593 head level category tags timestr
5594 remove-re habitp)))
5595 (setq priority (org-get-priority txt))
5596 (org-add-props txt props 'priority priority
5597 'org-marker marker 'org-hd-marker hdmarker
5598 'org-category category 'date date
5599 'level level
5600 'ts-date
5601 (ignore-errors (org-time-string-to-absolute timestr))
5602 'org-category-position category-pos
5603 'todo-state todo-state
5604 'warntime warntime
5605 'type "timestamp")
5606 (push txt ee))
5607 (if org-agenda-skip-additional-timestamps-same-entry
5608 (outline-next-heading)
5609 (goto-char end-of-match))))
5610 (nreverse ee)))
5612 (defun org-agenda-get-sexps ()
5613 "Return the sexp information for agenda display."
5614 (require 'diary-lib)
5615 (let* ((props (list 'face 'org-agenda-calendar-sexp
5616 'mouse-face 'highlight
5617 'help-echo
5618 (format "mouse-2 or RET jump to org file %s"
5619 (abbreviate-file-name buffer-file-name))))
5620 (regexp "^&?%%(")
5621 marker category extra category-pos level ee txt tags entry
5622 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5623 (goto-char (point-min))
5624 (while (re-search-forward regexp nil t)
5625 (catch :skip
5626 (org-agenda-skip)
5627 (setq beg (match-beginning 0))
5628 (goto-char (1- (match-end 0)))
5629 (setq b (point))
5630 (forward-sexp 1)
5631 (setq sexp (buffer-substring b (point)))
5632 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5633 (org-trim (match-string 1))
5634 ""))
5635 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5636 (when result
5637 (setq marker (org-agenda-new-marker beg)
5638 level (make-string (org-reduced-level (org-outline-level)) ? )
5639 category (org-get-category beg)
5640 category-pos (get-text-property beg 'org-category-position)
5641 inherited-tags
5642 (or (eq org-agenda-show-inherited-tags 'always)
5643 (and (listp org-agenda-show-inherited-tags)
5644 (memq 'agenda org-agenda-show-inherited-tags))
5645 (and (eq org-agenda-show-inherited-tags t)
5646 (or (eq org-agenda-use-tag-inheritance t)
5647 (memq 'agenda org-agenda-use-tag-inheritance))))
5648 tags (org-get-tags-at nil (not inherited-tags))
5649 todo-state (org-get-todo-state)
5650 warntime (get-text-property (point) 'org-appt-warntime)
5651 extra nil)
5653 (dolist (r (if (stringp result)
5654 (list result)
5655 result)) ;; we expect a list here
5656 (when (and org-agenda-diary-sexp-prefix
5657 (string-match org-agenda-diary-sexp-prefix r))
5658 (setq extra (match-string 0 r)
5659 r (replace-match "" nil nil r)))
5660 (if (string-match "\\S-" r)
5661 (setq txt r)
5662 (setq txt "SEXP entry returned empty string"))
5663 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5664 (org-add-props txt props 'org-marker marker
5665 'org-category category 'date date 'todo-state todo-state
5666 'org-category-position category-pos 'tags tags
5667 'level level
5668 'type "sexp" 'warntime warntime)
5669 (push txt ee)))))
5670 (nreverse ee)))
5672 ;; Calendar sanity: define some functions that are independent of
5673 ;; `calendar-date-style'.
5674 ;; Normally I would like to use ISO format when calling the diary functions,
5675 ;; but to make sure we still have Emacs 22 compatibility we bind
5676 ;; also `european-calendar-style' and use european format
5677 (defun org-anniversary (year month day &optional mark)
5678 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5679 (org-no-warnings
5680 (let ((calendar-date-style 'european) (european-calendar-style t))
5681 (diary-anniversary day month year mark))))
5682 (defun org-cyclic (N year month day &optional mark)
5683 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5684 (org-no-warnings
5685 (let ((calendar-date-style 'european) (european-calendar-style t))
5686 (diary-cyclic N day month year mark))))
5687 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5688 "Like `diary-block', but with fixed (ISO) order of arguments."
5689 (org-no-warnings
5690 (let ((calendar-date-style 'european) (european-calendar-style t))
5691 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5692 (defun org-date (year month day &optional mark)
5693 "Like `diary-date', but with fixed (ISO) order of arguments."
5694 (org-no-warnings
5695 (let ((calendar-date-style 'european) (european-calendar-style t))
5696 (diary-date day month year mark))))
5698 ;; Define the` org-class' function
5699 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5700 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5701 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5702 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5703 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5704 `holidays', then any date that is known by the Emacs calendar to be a
5705 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5706 then those holidays will be skipped."
5707 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5708 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5709 (d (calendar-absolute-from-gregorian date))
5710 (h (when skip-weeks (calendar-check-holidays date))))
5711 (and
5712 (<= date1 d)
5713 (<= d date2)
5714 (= (calendar-day-of-week date) dayname)
5715 (or (not skip-weeks)
5716 (progn
5717 (require 'cal-iso)
5718 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5719 (not (or (and h (memq 'holidays skip-weeks))
5720 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5721 entry)))
5723 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5724 "Like `org-class', but honor `calendar-date-style'.
5725 The order of the first 2 times 3 arguments depends on the variable
5726 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5727 So for American calendars, give this as MONTH DAY YEAR, for European as
5728 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5729 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5730 is any number of ISO weeks in the block period for which the item should
5731 be skipped.
5733 This function is here only for backward compatibility and it is deprecated,
5734 please use `org-class' instead."
5735 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5736 (date2 (org-order-calendar-date-args m2 d2 y2)))
5737 (org-class
5738 (nth 2 date1) (car date1) (nth 1 date1)
5739 (nth 2 date2) (car date2) (nth 1 date2)
5740 dayname skip-weeks)))
5741 (make-obsolete 'org-diary-class 'org-class "")
5743 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5744 (defalias 'org-get-closed 'org-agenda-get-progress)
5745 (defun org-agenda-get-progress ()
5746 "Return the logged TODO entries for agenda display."
5747 (let* ((props (list 'mouse-face 'highlight
5748 'org-not-done-regexp org-not-done-regexp
5749 'org-todo-regexp org-todo-regexp
5750 'org-complex-heading-regexp org-complex-heading-regexp
5751 'help-echo
5752 (format "mouse-2 or RET jump to org file %s"
5753 (abbreviate-file-name buffer-file-name))))
5754 (items (if (consp org-agenda-show-log-scoped)
5755 org-agenda-show-log-scoped
5756 (if (eq org-agenda-show-log-scoped 'clockcheck)
5757 '(clock)
5758 org-agenda-log-mode-items)))
5759 (parts
5760 (delq nil
5761 (list
5762 (if (memq 'closed items) (concat "\\<" org-closed-string))
5763 (if (memq 'clock items) (concat "\\<" org-clock-string))
5764 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5765 (parts-re (if parts (mapconcat 'identity parts "\\|")
5766 (error "`org-agenda-log-mode-items' is empty")))
5767 (regexp (concat
5768 "\\(" parts-re "\\)"
5769 " *\\["
5770 (regexp-quote
5771 (substring
5772 (format-time-string
5773 (car org-time-stamp-formats)
5774 (apply 'encode-time ; DATE bound by calendar
5775 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5776 1 11))))
5777 (org-agenda-search-headline-for-time nil)
5778 marker hdmarker priority category category-pos level tags closedp
5779 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5780 (goto-char (point-min))
5781 (while (re-search-forward regexp nil t)
5782 (catch :skip
5783 (org-agenda-skip)
5784 (setq marker (org-agenda-new-marker (match-beginning 0))
5785 closedp (equal (match-string 1) org-closed-string)
5786 statep (equal (string-to-char (match-string 1)) ?-)
5787 clockp (not (or closedp statep))
5788 state (and statep (match-string 2))
5789 category (org-get-category (match-beginning 0))
5790 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5791 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5792 (when (string-match "\\]" timestr)
5793 ;; substring should only run to end of time stamp
5794 (setq rest (substring timestr (match-end 0))
5795 timestr (substring timestr 0 (match-end 0)))
5796 (if (and (not closedp) (not statep)
5797 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5798 rest))
5799 (progn (setq timestr (concat (substring timestr 0 -1)
5800 "-" (match-string 1 rest) "]"))
5801 (setq clocked (match-string 2 rest)))
5802 (setq clocked "-")))
5803 (save-excursion
5804 (setq extra
5805 (cond
5806 ((not org-agenda-log-mode-add-notes) nil)
5807 (statep
5808 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5809 (match-string 1)))
5810 (clockp
5811 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5812 (match-string 1)))))
5813 (if (not (re-search-backward org-outline-regexp-bol nil t))
5814 (throw :skip nil)
5815 (goto-char (match-beginning 0))
5816 (setq hdmarker (org-agenda-new-marker)
5817 inherited-tags
5818 (or (eq org-agenda-show-inherited-tags 'always)
5819 (and (listp org-agenda-show-inherited-tags)
5820 (memq 'todo org-agenda-show-inherited-tags))
5821 (and (eq org-agenda-show-inherited-tags t)
5822 (or (eq org-agenda-use-tag-inheritance t)
5823 (memq 'todo org-agenda-use-tag-inheritance))))
5824 tags (org-get-tags-at nil (not inherited-tags))
5825 level (make-string (org-reduced-level (org-outline-level)) ? ))
5826 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5827 (setq txt (match-string 1))
5828 (when extra
5829 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5830 (setq txt (concat (substring txt 0 (match-beginning 1))
5831 " - " extra " " (match-string 2 txt)))
5832 (setq txt (concat txt " - " extra))))
5833 (setq txt (org-agenda-format-item
5834 (cond
5835 (closedp "Closed: ")
5836 (statep (concat "State: (" state ")"))
5837 (t (concat "Clocked: (" clocked ")")))
5838 txt level category tags timestr)))
5839 (setq priority 100000)
5840 (org-add-props txt props
5841 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5842 'priority priority 'org-category category
5843 'org-category-position category-pos
5844 'level level
5845 'type "closed" 'date date
5846 'undone-face 'org-warning 'done-face 'org-agenda-done)
5847 (push txt ee))
5848 (goto-char (point-at-eol))))
5849 (nreverse ee)))
5851 (defun org-agenda-show-clocking-issues ()
5852 "Add overlays, showing issues with clocking.
5853 See also the user option `org-agenda-clock-consistency-checks'."
5854 (interactive)
5855 (let* ((org-time-clocksum-use-effort-durations nil)
5856 (pl org-agenda-clock-consistency-checks)
5857 (re (concat "^[ \t]*"
5858 org-clock-string
5859 "[ \t]+"
5860 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5861 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5862 (tlstart 0.)
5863 (tlend 0.)
5864 (maxtime (org-hh:mm-string-to-minutes
5865 (or (plist-get pl :max-duration) "24:00")))
5866 (mintime (org-hh:mm-string-to-minutes
5867 (or (plist-get pl :min-duration) 0)))
5868 (maxgap (org-hh:mm-string-to-minutes
5869 ;; default 30:00 means never complain
5870 (or (plist-get pl :max-gap) "30:00")))
5871 (gapok (mapcar 'org-hh:mm-string-to-minutes
5872 (plist-get pl :gap-ok-around)))
5873 (def-face (or (plist-get pl :default-face)
5874 '((:background "DarkRed") (:foreground "white"))))
5875 issue face m te ts dt ov)
5876 (goto-char (point-min))
5877 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5878 (setq issue nil face def-face)
5879 (catch 'next
5880 (setq m (org-get-at-bol 'org-marker)
5881 te nil ts nil)
5882 (unless (and m (markerp m))
5883 (setq issue "No valid clock line") (throw 'next t))
5884 (org-with-point-at m
5885 (save-excursion
5886 (goto-char (point-at-bol))
5887 (unless (looking-at re)
5888 (error "No valid Clock line")
5889 (throw 'next t))
5890 (unless (match-end 3)
5891 (setq issue "No end time"
5892 face (or (plist-get pl :no-end-time-face) face))
5893 (throw 'next t))
5894 (setq ts (match-string 1)
5895 te (match-string 3)
5896 ts (org-float-time
5897 (apply 'encode-time (org-parse-time-string ts)))
5898 te (org-float-time
5899 (apply 'encode-time (org-parse-time-string te)))
5900 dt (- te ts))))
5901 (cond
5902 ((> dt (* 60 maxtime))
5903 ;; a very long clocking chunk
5904 (setq issue (format "Clocking interval is very long: %s"
5905 (org-minutes-to-clocksum-string
5906 (floor (/ (float dt) 60.))))
5907 face (or (plist-get pl :long-face) face)))
5908 ((< dt (* 60 mintime))
5909 ;; a very short clocking chunk
5910 (setq issue (format "Clocking interval is very short: %s"
5911 (org-minutes-to-clocksum-string
5912 (floor (/ (float dt) 60.))))
5913 face (or (plist-get pl :short-face) face)))
5914 ((and (> tlend 0) (< ts tlend))
5915 ;; Two clock entries are overlapping
5916 (setq issue (format "Clocking overlap: %d minutes"
5917 (/ (- tlend ts) 60))
5918 face (or (plist-get pl :overlap-face) face)))
5919 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5920 ;; There is a gap, lets see if we need to report it
5921 (unless (org-agenda-check-clock-gap tlend ts gapok)
5922 (setq issue (format "Clocking gap: %d minutes"
5923 (/ (- ts tlend) 60))
5924 face (or (plist-get pl :gap-face) face))))
5925 (t nil)))
5926 (setq tlend (or te tlend) tlstart (or ts tlstart))
5927 (when issue
5928 ;; OK, there was some issue, add an overlay to show the issue
5929 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5930 (overlay-put ov 'before-string
5931 (concat
5932 (org-add-props
5933 (format "%-43s" (concat " " issue))
5935 'face face)
5936 "\n"))
5937 (overlay-put ov 'evaporate t)))))
5939 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5940 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5941 (catch 'exit
5942 (unless ok-list
5943 ;; there are no OK times for gaps...
5944 (throw 'exit nil))
5945 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5946 ;; This is more than 24 hours, so it is OK.
5947 ;; because we have at least one OK time, that must be in the
5948 ;; 24 hour interval.
5949 (throw 'exit t))
5950 ;; We have a shorter gap.
5951 ;; Now we have to get the minute of the day when these times are
5952 (let* ((t1dec (decode-time (seconds-to-time t1)))
5953 (t2dec (decode-time (seconds-to-time t2)))
5954 ;; compute the minute on the day
5955 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5956 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5957 (when (< min2 min1)
5958 ;; if min2 is smaller than min1, this means it is on the next day.
5959 ;; Wrap it to after midnight.
5960 (setq min2 (+ min2 1440)))
5961 ;; Now check if any of the OK times is in the gap
5962 (mapc (lambda (x)
5963 ;; Wrap the time to after midnight if necessary
5964 (if (< x min1) (setq x (+ x 1440)))
5965 ;; Check if in interval
5966 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5967 ok-list)
5968 ;; Nope, this gap is not OK
5969 nil)))
5971 (defun org-agenda-get-deadlines (&optional with-hour)
5972 "Return the deadline information for agenda display.
5973 When WITH-HOUR is non-nil, only return deadlines with an hour
5974 specification like [h]h:mm."
5975 (let* ((props (list 'mouse-face 'highlight
5976 'org-not-done-regexp org-not-done-regexp
5977 'org-todo-regexp org-todo-regexp
5978 'org-complex-heading-regexp org-complex-heading-regexp
5979 'help-echo
5980 (format "mouse-2 or RET jump to org file %s"
5981 (abbreviate-file-name buffer-file-name))))
5982 (regexp (if with-hour
5983 org-deadline-time-hour-regexp
5984 org-deadline-time-regexp))
5985 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5986 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5987 (dl0 (car org-agenda-deadline-leaders))
5988 (dl1 (nth 1 org-agenda-deadline-leaders))
5989 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
5990 d2 diff dfrac wdays pos pos1 category category-pos level
5991 tags suppress-prewarning ee txt head face s todo-state
5992 show-all upcomingp donep timestr warntime inherited-tags ts-date)
5993 (goto-char (point-min))
5994 (while (re-search-forward regexp nil t)
5995 (catch :skip
5996 (org-agenda-skip)
5997 (setq s (match-string 1)
5998 txt nil
5999 pos (1- (match-beginning 1))
6000 todo-state (save-match-data (org-get-todo-state))
6001 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6002 (member todo-state
6003 org-agenda-repeating-timestamp-show-all))
6004 d2 (org-time-string-to-absolute
6005 s d1 'past show-all (current-buffer) pos)
6006 diff (- d2 d1))
6007 (setq suppress-prewarning
6008 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6009 (let ((item (buffer-substring (point-at-bol)
6010 (point-at-eol))))
6011 (save-match-data
6012 (and (string-match
6013 org-scheduled-time-regexp item)
6014 (match-string 1 item)))))))
6015 (cond
6016 ((not ds) nil)
6017 ;; The current item has a scheduled date (in ds), so
6018 ;; evaluate its prewarning lead time.
6019 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6020 ;; Use global prewarning-restart lead time.
6021 org-agenda-skip-deadline-prewarning-if-scheduled)
6022 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6023 'pre-scheduled)
6024 ;; Set prewarning to no earlier than scheduled.
6025 (min (- d2 (org-time-string-to-absolute
6026 ds d1 'past show-all (current-buffer) pos))
6027 org-deadline-warning-days))
6028 ;; Set prewarning to deadline.
6029 (t 0))))
6030 (setq wdays (if suppress-prewarning
6031 (let ((org-deadline-warning-days suppress-prewarning))
6032 (org-get-wdays s))
6033 (org-get-wdays s))
6034 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6035 upcomingp (and todayp (> diff 0)))
6036 ;; When to show a deadline in the calendar:
6037 ;; If the expiration is within wdays warning time.
6038 ;; Past-due deadlines are only shown on the current date
6039 (if (and (or (and (<= diff wdays)
6040 (and todayp (not org-agenda-only-exact-dates)))
6041 (= diff 0)))
6042 (save-excursion
6043 ;; (setq todo-state (org-get-todo-state))
6044 (setq donep (member todo-state org-done-keywords))
6045 (if (and donep
6046 (or org-agenda-skip-deadline-if-done
6047 (not (= diff 0))))
6048 (setq txt nil)
6049 (setq category (org-get-category)
6050 warntime (get-text-property (point) 'org-appt-warntime)
6051 category-pos (get-text-property (point) 'org-category-position))
6052 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6053 (throw :skip nil)
6054 (goto-char (match-end 0))
6055 (setq pos1 (match-beginning 0))
6056 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6057 (setq inherited-tags
6058 (or (eq org-agenda-show-inherited-tags 'always)
6059 (and (listp org-agenda-show-inherited-tags)
6060 (memq 'agenda org-agenda-show-inherited-tags))
6061 (and (eq org-agenda-show-inherited-tags t)
6062 (or (eq org-agenda-use-tag-inheritance t)
6063 (memq 'agenda org-agenda-use-tag-inheritance))))
6064 tags (org-get-tags-at pos1 (not inherited-tags)))
6065 (setq head (buffer-substring
6066 (point)
6067 (progn (skip-chars-forward "^\r\n")
6068 (point))))
6069 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6070 (setq timestr
6071 (concat (substring s (match-beginning 1)) " "))
6072 (setq timestr 'time))
6073 (setq txt (org-agenda-format-item
6074 (cond ((= diff 0) dl0)
6075 ((> diff 0)
6076 (if (functionp dl1)
6077 (funcall dl1 diff date)
6078 (format dl1 diff)))
6080 (if (functionp dl2)
6081 (funcall dl2 diff date)
6082 (format dl2 (if (string= dl2 dl1)
6083 diff (abs diff))))))
6084 head level category tags
6085 (if (not (= diff 0)) nil timestr)))))
6086 (when txt
6087 (setq face (org-agenda-deadline-face dfrac))
6088 (org-add-props txt props
6089 'org-marker (org-agenda-new-marker pos)
6090 'warntime warntime
6091 'level level
6092 'ts-date d2
6093 'org-hd-marker (org-agenda-new-marker pos1)
6094 'priority (+ (- diff)
6095 (org-get-priority txt))
6096 'org-category category
6097 'org-category-position category-pos
6098 'todo-state todo-state
6099 'type (if upcomingp "upcoming-deadline" "deadline")
6100 'date (if upcomingp date d2)
6101 'face (if donep 'org-agenda-done face)
6102 'undone-face face 'done-face 'org-agenda-done)
6103 (push txt ee))))))
6104 (nreverse ee)))
6106 (defun org-agenda-deadline-face (fraction)
6107 "Return the face to displaying a deadline item.
6108 FRACTION is what fraction of the head-warning time has passed."
6109 (let ((faces org-agenda-deadline-faces) f)
6110 (catch 'exit
6111 (while (setq f (pop faces))
6112 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6114 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6115 "Return the scheduled information for agenda display.
6116 When WITH-HOUR is non-nil, only return scheduled items with
6117 an hour specification like [h]h:mm."
6118 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6119 'org-todo-regexp org-todo-regexp
6120 'org-complex-heading-regexp org-complex-heading-regexp
6121 'done-face 'org-agenda-done
6122 'mouse-face 'highlight
6123 'help-echo
6124 (format "mouse-2 or RET jump to org file %s"
6125 (abbreviate-file-name buffer-file-name))))
6126 (regexp (if with-hour
6127 org-scheduled-time-hour-regexp
6128 org-scheduled-time-regexp))
6129 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6130 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6132 (deadline-position-alist
6133 (mapcar (lambda (a) (and (setq mm (get-text-property
6134 0 'org-hd-marker a))
6135 (cons (marker-position mm) a)))
6136 deadline-results))
6137 d2 diff pos pos1 category category-pos level tags donep
6138 ee txt head pastschedp todo-state face timestr s habitp show-all
6139 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6140 ddays)
6141 (goto-char (point-min))
6142 (while (re-search-forward regexp nil t)
6143 (catch :skip
6144 (org-agenda-skip)
6145 (setq s (match-string 1)
6146 txt nil
6147 pos (1- (match-beginning 1))
6148 todo-state (save-match-data (org-get-todo-state))
6149 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6150 (member todo-state
6151 org-agenda-repeating-timestamp-show-all))
6152 d2 (org-time-string-to-absolute
6153 s d1 'past show-all (current-buffer) pos)
6154 diff (- d2 d1)
6155 warntime (get-text-property (point) 'org-appt-warntime))
6156 (setq pastschedp (and todayp (< diff 0)))
6157 (setq did-habit-check-p nil)
6158 (setq suppress-delay
6159 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6160 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6161 (save-match-data
6162 (and (string-match
6163 org-deadline-time-regexp item)
6164 (match-string 1 item)))))))
6165 (cond
6166 ((not ds) nil)
6167 ;; The current item has a deadline date (in ds), so
6168 ;; evaluate its delay time.
6169 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6170 ;; Use global delay time.
6171 (- org-agenda-skip-scheduled-delay-if-deadline))
6172 ((eq org-agenda-skip-scheduled-delay-if-deadline
6173 'post-deadline)
6174 ;; Set delay to no later than deadline.
6175 (min (- d2 (org-time-string-to-absolute
6176 ds d1 'past show-all (current-buffer) pos))
6177 org-scheduled-delay-days))
6178 (t 0))))
6179 (setq ddays (if suppress-delay
6180 (let ((org-scheduled-delay-days suppress-delay))
6181 (org-get-wdays s t t))
6182 (org-get-wdays s t)))
6183 ;; Use a delay of 0 when there is a repeater and the delay is
6184 ;; of the form --3d
6185 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6186 (< (org-time-string-to-absolute s)
6187 (org-time-string-to-absolute
6188 s d2 'past nil (current-buffer) pos)))
6189 (setq ddays 0))
6190 ;; When to show a scheduled item in the calendar:
6191 ;; If it is on or past the date.
6192 (when (or (and (> ddays 0) (= diff (- ddays)))
6193 (and (zerop ddays) (= diff 0))
6194 (and (< (+ diff ddays) 0)
6195 (< (abs diff) org-scheduled-past-days)
6196 (and todayp (not org-agenda-only-exact-dates)))
6197 ;; org-is-habit-p uses org-entry-get, which is expansive
6198 ;; so we go extra mile to only call it once
6199 (and todayp
6200 (boundp 'org-habit-show-all-today)
6201 org-habit-show-all-today
6202 (setq did-habit-check-p t)
6203 (setq habitp (and (functionp 'org-is-habit-p)
6204 (org-is-habit-p)))))
6205 (save-excursion
6206 (setq donep (member todo-state org-done-keywords))
6207 (if (and donep
6208 (or org-agenda-skip-scheduled-if-done
6209 (not (= diff 0))
6210 (and (functionp 'org-is-habit-p)
6211 (org-is-habit-p))))
6212 (setq txt nil)
6213 (setq habitp (if did-habit-check-p habitp
6214 (and (functionp 'org-is-habit-p)
6215 (org-is-habit-p))))
6216 (setq category (org-get-category)
6217 category-pos (get-text-property (point) 'org-category-position))
6218 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6219 'repeated-after-deadline)
6220 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6221 (throw :skip nil))
6222 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6223 (throw :skip nil)
6224 (goto-char (match-end 0))
6225 (setq pos1 (match-beginning 0))
6226 (if habitp
6227 (if (or (not org-habit-show-habits)
6228 (and (not todayp)
6229 (boundp 'org-habit-show-habits-only-for-today)
6230 org-habit-show-habits-only-for-today))
6231 (throw :skip nil))
6232 (if (and
6233 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6234 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6235 pastschedp))
6236 (setq mm (assoc pos1 deadline-position-alist)))
6237 (throw :skip nil)))
6238 (setq inherited-tags
6239 (or (eq org-agenda-show-inherited-tags 'always)
6240 (and (listp org-agenda-show-inherited-tags)
6241 (memq 'agenda org-agenda-show-inherited-tags))
6242 (and (eq org-agenda-show-inherited-tags t)
6243 (or (eq org-agenda-use-tag-inheritance t)
6244 (memq 'agenda org-agenda-use-tag-inheritance))))
6246 tags (org-get-tags-at nil (not inherited-tags)))
6247 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6248 (setq head (buffer-substring
6249 (point)
6250 (progn (skip-chars-forward "^\r\n") (point))))
6251 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6252 (setq timestr
6253 (concat (substring s (match-beginning 1)) " "))
6254 (setq timestr 'time))
6255 (setq txt (org-agenda-format-item
6256 (if (= diff 0)
6257 (car org-agenda-scheduled-leaders)
6258 (format (nth 1 org-agenda-scheduled-leaders)
6259 (- 1 diff)))
6260 head level category tags
6261 (if (not (= diff 0)) nil timestr)
6262 nil habitp))))
6263 (when txt
6264 (setq face
6265 (cond
6266 ((and (not habitp) pastschedp)
6267 'org-scheduled-previously)
6268 (todayp 'org-scheduled-today)
6269 (t 'org-scheduled))
6270 habitp (and habitp (org-habit-parse-todo)))
6271 (org-add-props txt props
6272 'undone-face face
6273 'face (if donep 'org-agenda-done face)
6274 'org-marker (org-agenda-new-marker pos)
6275 'org-hd-marker (org-agenda-new-marker pos1)
6276 'type (if pastschedp "past-scheduled" "scheduled")
6277 'date (if pastschedp d2 date)
6278 'ts-date d2
6279 'warntime warntime
6280 'level level
6281 'priority (if habitp
6282 (org-habit-get-priority habitp)
6283 (+ 94 (- 5 diff) (org-get-priority txt)))
6284 'org-category category
6285 'category-position category-pos
6286 'org-habit-p habitp
6287 'todo-state todo-state)
6288 (push txt ee))))))
6289 (nreverse ee)))
6291 (defun org-agenda-get-blocks ()
6292 "Return the date-range information for agenda display."
6293 (let* ((props (list 'face nil
6294 'org-not-done-regexp org-not-done-regexp
6295 'org-todo-regexp org-todo-regexp
6296 'org-complex-heading-regexp org-complex-heading-regexp
6297 'mouse-face 'highlight
6298 'help-echo
6299 (format "mouse-2 or RET jump to org file %s"
6300 (abbreviate-file-name buffer-file-name))))
6301 (regexp org-tr-regexp)
6302 (d0 (calendar-absolute-from-gregorian date))
6303 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6304 level todo-state tags pos head donep inherited-tags)
6305 (goto-char (point-min))
6306 (while (re-search-forward regexp nil t)
6307 (catch :skip
6308 (org-agenda-skip)
6309 (setq pos (point))
6310 (let ((start-time (match-string 1))
6311 (end-time (match-string 2)))
6312 (setq s1 (match-string 1)
6313 s2 (match-string 2)
6314 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6315 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6316 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6317 ;; Only allow days between the limits, because the normal
6318 ;; date stamps will catch the limits.
6319 (save-excursion
6320 (setq todo-state (org-get-todo-state))
6321 (setq donep (member todo-state org-done-keywords))
6322 (if (and donep org-agenda-skip-timestamp-if-done)
6323 (throw :skip t))
6324 (setq marker (org-agenda-new-marker (point)))
6325 (setq category (org-get-category)
6326 category-pos (get-text-property (point) 'org-category-position))
6327 (if (not (re-search-backward org-outline-regexp-bol nil t))
6328 (throw :skip nil)
6329 (goto-char (match-beginning 0))
6330 (setq hdmarker (org-agenda-new-marker (point))
6331 inherited-tags
6332 (or (eq org-agenda-show-inherited-tags 'always)
6333 (and (listp org-agenda-show-inherited-tags)
6334 (memq 'agenda org-agenda-show-inherited-tags))
6335 (and (eq org-agenda-show-inherited-tags t)
6336 (or (eq org-agenda-use-tag-inheritance t)
6337 (memq 'agenda org-agenda-use-tag-inheritance))))
6339 tags (org-get-tags-at nil (not inherited-tags)))
6340 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6341 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6342 (setq head (match-string 1))
6343 (let ((remove-re
6344 (if org-agenda-remove-timeranges-from-blocks
6345 (concat
6346 "<" (regexp-quote s1) ".*?>"
6347 "--"
6348 "<" (regexp-quote s2) ".*?>")
6349 nil)))
6350 (setq txt (org-agenda-format-item
6351 (format
6352 (nth (if (= d1 d2) 0 1)
6353 org-agenda-timerange-leaders)
6354 (1+ (- d0 d1)) (1+ (- d2 d1)))
6355 head level category tags
6356 (cond ((and (= d1 d0) (= d2 d0))
6357 (concat "<" start-time ">--<" end-time ">"))
6358 ((= d1 d0)
6359 (concat "<" start-time ">"))
6360 ((= d2 d0)
6361 (concat "<" end-time ">")))
6362 remove-re))))
6363 (org-add-props txt props
6364 'org-marker marker 'org-hd-marker hdmarker
6365 'type "block" 'date date
6366 'level level
6367 'todo-state todo-state
6368 'priority (org-get-priority txt) 'org-category category
6369 'org-category-position category-pos)
6370 (push txt ee))))
6371 (goto-char pos)))
6372 ;; Sort the entries by expiration date.
6373 (nreverse ee)))
6375 ;;; Agenda presentation and sorting
6377 (defvar org-prefix-has-time nil
6378 "A flag, set by `org-compile-prefix-format'.
6379 The flag is set if the currently compiled format contains a `%t'.")
6380 (defvar org-prefix-has-tag nil
6381 "A flag, set by `org-compile-prefix-format'.
6382 The flag is set if the currently compiled format contains a `%T'.")
6383 (defvar org-prefix-has-effort nil
6384 "A flag, set by `org-compile-prefix-format'.
6385 The flag is set if the currently compiled format contains a `%e'.")
6386 (defvar org-prefix-has-breadcrumbs nil
6387 "A flag, set by `org-compile-prefix-format'.
6388 The flag is set if the currently compiled format contains a `%b'.")
6389 (defvar org-prefix-category-length nil
6390 "Used by `org-compile-prefix-format' to remember the category field width.")
6391 (defvar org-prefix-category-max-length nil
6392 "Used by `org-compile-prefix-format' to remember the category field width.")
6394 (defun org-agenda-get-category-icon (category)
6395 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6396 (dolist (entry org-agenda-category-icon-alist)
6397 (when (org-string-match-p (car entry) category)
6398 (if (listp (cadr entry))
6399 (return (cadr entry))
6400 (return (apply 'create-image (cdr entry)))))))
6402 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6403 remove-re habitp)
6404 "Format TXT to be inserted into the agenda buffer.
6405 In particular, add the prefix and corresponding text properties.
6407 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6408 LEVEL may be a string to replace the `%l' specifier.
6409 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6410 category taken from local variable or file name. It will replace the `%c'
6411 specifier in the format.
6412 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6413 TXT for sorting of this entry, and for the `%t' specifier in the format.
6414 When DOTIME is a string, this string is searched for a time before TXT is.
6415 TAGS can be the tags of the headline.
6416 Any match of REMOVE-RE will be removed from TXT."
6417 ;; We keep the org-prefix-* variable values along with a compiled
6418 ;; formatter, so that multiple agendas existing at the same time do
6419 ;; not step on each other toes.
6421 ;; It was inconvenient to make these variables buffer local in
6422 ;; Agenda buffers, because this function expects to be called with
6423 ;; the buffer where item comes from being current, and not agenda
6424 ;; buffer
6425 (let* ((bindings (car org-prefix-format-compiled))
6426 (formatter (cadr org-prefix-format-compiled)))
6427 (loop for (var value) in bindings
6428 do (set var value))
6429 (save-match-data
6430 ;; Diary entries sometimes have extra whitespace at the beginning
6431 (setq txt (org-trim txt))
6433 ;; Fix the tags part in txt
6434 (setq txt (org-agenda-fix-displayed-tags
6435 txt tags
6436 org-agenda-show-inherited-tags
6437 org-agenda-hide-tags-regexp))
6439 (let* ((category (or category
6440 (if (stringp org-category)
6441 org-category
6442 (and org-category (symbol-name org-category)))
6443 (if buffer-file-name
6444 (file-name-sans-extension
6445 (file-name-nondirectory buffer-file-name))
6446 "")))
6447 (category-icon (org-agenda-get-category-icon category))
6448 (category-icon (if category-icon
6449 (propertize " " 'display category-icon)
6450 ""))
6451 ;; time, tag, effort are needed for the eval of the prefix format
6452 (tag (if tags (nth (1- (length tags)) tags) ""))
6453 time effort neffort
6454 (ts (if dotime (concat
6455 (if (stringp dotime) dotime "")
6456 (and org-agenda-search-headline-for-time txt))))
6457 (time-of-day (and dotime (org-get-time-of-day ts)))
6458 stamp plain s0 s1 s2 rtn srp l
6459 duration thecategory breadcrumbs)
6460 (and (derived-mode-p 'org-mode) buffer-file-name
6461 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6462 (when (and dotime time-of-day)
6463 ;; Extract starting and ending time and move them to prefix
6464 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6465 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6466 (setq s0 (match-string 0 ts)
6467 srp (and stamp (match-end 3))
6468 s1 (match-string (if plain 1 2) ts)
6469 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6471 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6472 ;; them, we might want to remove them there to avoid duplication.
6473 ;; The user can turn this off with a variable.
6474 (if (and org-prefix-has-time
6475 org-agenda-remove-times-when-in-prefix (or stamp plain)
6476 (string-match (concat (regexp-quote s0) " *") txt)
6477 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6478 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6479 (= (match-beginning 0) 0)
6481 (setq txt (replace-match "" nil nil txt))))
6482 ;; Normalize the time(s) to 24 hour
6483 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6484 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6486 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6487 (let (org-time-clocksum-use-effort-durations)
6488 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6489 (setq s2
6490 (org-minutes-to-clocksum-string
6491 (+ (org-hh:mm-string-to-minutes s1)
6492 org-agenda-default-appointment-duration)))))
6494 ;; Compute the duration
6495 (when s2
6496 (setq duration (- (org-hh:mm-string-to-minutes s2)
6497 (org-hh:mm-string-to-minutes s1)))))
6499 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6500 txt)
6501 ;; Tags are in the string
6502 (if (or (eq org-agenda-remove-tags t)
6503 (and org-agenda-remove-tags
6504 org-prefix-has-tag))
6505 (setq txt (replace-match "" t t txt))
6506 (setq txt (replace-match
6507 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6508 (match-string 2 txt))
6509 t t txt))))
6510 (when (derived-mode-p 'org-mode)
6511 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6513 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6514 ;; current buffer, so move this check outside of above
6515 (if effort
6516 (setq neffort (org-duration-string-to-minutes effort)
6517 effort (setq effort (concat "[" effort "]")))
6518 ;; prevent erroring out with %e format when there is no effort
6519 (setq effort ""))
6521 (when remove-re
6522 (while (string-match remove-re txt)
6523 (setq txt (replace-match "" t t txt))))
6525 ;; Set org-heading property on `txt' to mark the start of the
6526 ;; heading.
6527 (add-text-properties 0 (length txt) '(org-heading t) txt)
6529 ;; Prepare the variables needed in the eval of the compiled format
6530 (if org-prefix-has-breadcrumbs
6531 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6532 (let ((s (org-display-outline-path nil nil "->" t)))
6533 (if (eq "" s) "" (concat s "->"))))))
6534 (setq time (cond (s2 (concat
6535 (org-agenda-time-of-day-to-ampm-maybe s1)
6536 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6537 (if org-agenda-timegrid-use-ampm " ")))
6538 (s1 (concat
6539 (org-agenda-time-of-day-to-ampm-maybe s1)
6540 (if org-agenda-timegrid-use-ampm
6541 "........ "
6542 "......")))
6543 (t ""))
6544 extra (or (and (not habitp) extra) "")
6545 category (if (symbolp category) (symbol-name category) category)
6546 thecategory (copy-sequence category)
6547 level (or level ""))
6548 (if (string-match org-bracket-link-regexp category)
6549 (progn
6550 (setq l (if (match-end 3)
6551 (- (match-end 3) (match-beginning 3))
6552 (- (match-end 1) (match-beginning 1))))
6553 (when (< l (or org-prefix-category-length 0))
6554 (setq category (copy-sequence category))
6555 (org-add-props category nil
6556 'extra-space (make-string
6557 (- org-prefix-category-length l 1) ?\ ))))
6558 (if (and org-prefix-category-max-length
6559 (>= (length category) org-prefix-category-max-length))
6560 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6561 ;; Evaluate the compiled format
6562 (setq rtn (concat (eval formatter) txt))
6564 ;; And finally add the text properties
6565 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6566 (org-add-props rtn nil
6567 'org-category (if thecategory (downcase thecategory) category)
6568 'tags (mapcar 'org-downcase-keep-props tags)
6569 'org-highest-priority org-highest-priority
6570 'org-lowest-priority org-lowest-priority
6571 'time-of-day time-of-day
6572 'duration duration
6573 'effort effort
6574 'effort-minutes neffort
6575 'breadcrumbs breadcrumbs
6576 'txt txt
6577 'level level
6578 'time time
6579 'extra extra
6580 'format org-prefix-format-compiled
6581 'dotime dotime)))))
6583 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6584 "Remove tags string from TXT, and add a modified list of tags.
6585 The modified list may contain inherited tags, and tags matched by
6586 `org-agenda-hide-tags-regexp' will be removed."
6587 (when (or add-inherited hide-re)
6588 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6589 (setq txt (substring txt 0 (match-beginning 0))))
6590 (setq tags
6591 (delq nil
6592 (mapcar (lambda (tg)
6593 (if (or (and hide-re (string-match hide-re tg))
6594 (and (not add-inherited)
6595 (get-text-property 0 'inherited tg)))
6597 tg))
6598 tags)))
6599 (when tags
6600 (let ((have-i (get-text-property 0 'inherited (car tags)))
6602 (setq txt (concat txt " :"
6603 (mapconcat
6604 (lambda (x)
6605 (setq i (get-text-property 0 'inherited x))
6606 (if (and have-i (not i))
6607 (progn
6608 (setq have-i nil)
6609 (concat ":" x))
6611 tags ":")
6612 (if have-i "::" ":"))))))
6613 txt)
6615 (defun org-downcase-keep-props (s)
6616 (let ((props (text-properties-at 0 s)))
6617 (setq s (downcase s))
6618 (add-text-properties 0 (length s) props s)
6621 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6623 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6624 "Add a time-grid for agenda items which need it.
6626 LIST is the list of agenda items formatted by `org-agenda-list'.
6627 NDAYS is the span of the current agenda view.
6628 TODAYP is `t' when the current agenda view is on today."
6629 (catch 'exit
6630 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6631 ((and todayp (member 'today (car org-agenda-time-grid))))
6632 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6633 ((member 'weekly (car org-agenda-time-grid)))
6634 (t (throw 'exit list)))
6635 (let* ((have (delq nil (mapcar
6636 (lambda (x) (get-text-property 1 'time-of-day x))
6637 list)))
6638 (string (nth 1 org-agenda-time-grid))
6639 (gridtimes (nth 2 org-agenda-time-grid))
6640 (req (car org-agenda-time-grid))
6641 (remove (member 'remove-match req))
6642 new time)
6643 (if (and (member 'require-timed req) (not have))
6644 ;; don't show empty grid
6645 (throw 'exit list))
6646 (while (setq time (pop gridtimes))
6647 (unless (and remove (member time have))
6648 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6649 (push (org-agenda-format-item
6650 nil string nil "" nil
6651 (concat (substring time 0 -2) ":" (substring time -2)))
6652 new)
6653 (put-text-property
6654 2 (length (car new)) 'face 'org-time-grid (car new))))
6655 (when (and todayp org-agenda-show-current-time-in-grid)
6656 (push (org-agenda-format-item
6657 nil org-agenda-current-time-string nil "" nil
6658 (format-time-string "%H:%M "))
6659 new)
6660 (put-text-property
6661 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6663 (if (member 'time-up org-agenda-sorting-strategy-selected)
6664 (append new list)
6665 (append list new)))))
6667 (defun org-compile-prefix-format (key)
6668 "Compile the prefix format into a Lisp form that can be evaluated.
6669 The resulting form and associated variable bindings is returned
6670 and stored in the variable `org-prefix-format-compiled'."
6671 (setq org-prefix-has-time nil
6672 org-prefix-has-tag nil
6673 org-prefix-category-length nil
6674 org-prefix-has-effort nil
6675 org-prefix-has-breadcrumbs nil)
6676 (let ((s (cond
6677 ((stringp org-agenda-prefix-format)
6678 org-agenda-prefix-format)
6679 ((assq key org-agenda-prefix-format)
6680 (cdr (assq key org-agenda-prefix-format)))
6681 (t " %-12:c%?-12t% s")))
6682 (start 0)
6683 varform vars var e c f opt)
6684 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6685 s start)
6686 (setq var (or (cdr (assoc (match-string 4 s)
6687 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6688 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6689 'eval)
6690 c (or (match-string 3 s) "")
6691 opt (match-beginning 1)
6692 start (1+ (match-beginning 0)))
6693 (if (equal var 'time) (setq org-prefix-has-time t))
6694 (if (equal var 'tag) (setq org-prefix-has-tag t))
6695 (if (equal var 'effort) (setq org-prefix-has-effort t))
6696 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6697 (setq f (concat "%" (match-string 2 s) "s"))
6698 (when (equal var 'category)
6699 (setq org-prefix-category-length
6700 (floor (abs (string-to-number (match-string 2 s)))))
6701 (setq org-prefix-category-max-length
6702 (let ((x (match-string 2 s)))
6703 (save-match-data
6704 (if (string-match "\\.[0-9]+" x)
6705 (string-to-number (substring (match-string 0 x) 1)))))))
6706 (if (eq var 'eval)
6707 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6708 (if opt
6709 (setq varform
6710 `(if (equal "" ,var)
6712 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6713 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6714 (setq s (replace-match "%s" t nil s))
6715 (push varform vars))
6716 (setq vars (nreverse vars))
6717 (with-current-buffer (or org-agenda-buffer (current-buffer))
6718 (setq org-prefix-format-compiled
6719 (list
6720 `((org-prefix-has-time ,org-prefix-has-time)
6721 (org-prefix-has-tag ,org-prefix-has-tag)
6722 (org-prefix-category-length ,org-prefix-category-length)
6723 (org-prefix-has-effort ,org-prefix-has-effort)
6724 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6725 `(format ,s ,@vars))))))
6727 (defun org-set-sorting-strategy (key)
6728 (if (symbolp (car org-agenda-sorting-strategy))
6729 ;; the old format
6730 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6731 (setq org-agenda-sorting-strategy-selected
6732 (or (cdr (assq key org-agenda-sorting-strategy))
6733 (cdr (assq 'agenda org-agenda-sorting-strategy))
6734 '(time-up category-keep priority-down)))))
6736 (defun org-get-time-of-day (s &optional string mod24)
6737 "Check string S for a time of day.
6738 If found, return it as a military time number between 0 and 2400.
6739 If not found, return nil.
6740 The optional STRING argument forces conversion into a 5 character wide string
6741 HH:MM."
6742 (save-match-data
6743 (when
6744 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6745 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6746 (let* ((h (string-to-number (match-string 1 s)))
6747 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6748 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6749 (am-p (equal ampm "am"))
6750 (h1 (cond ((not ampm) h)
6751 ((= h 12) (if am-p 0 12))
6752 (t (+ h (if am-p 0 12)))))
6753 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6754 (mod h1 24) h1))
6755 (t0 (+ (* 100 h2) m))
6756 (t1 (concat (if (>= h1 24) "+" " ")
6757 (if (and org-agenda-time-leading-zero
6758 (< t0 1000)) "0" "")
6759 (if (< t0 100) "0" "")
6760 (if (< t0 10) "0" "")
6761 (int-to-string t0))))
6762 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6764 (defvar org-agenda-before-sorting-filter-function nil
6765 "Function to be applied to agenda items prior to sorting.
6766 Prior to sorting also means just before they are inserted into the agenda.
6768 To aid sorting, you may revisit the original entries and add more text
6769 properties which will later be used by the sorting functions.
6771 The function should take a string argument, an agenda line.
6772 It has access to the text properties in that line, which contain among
6773 other things, the property `org-hd-marker' that points to the entry
6774 where the line comes from. Note that not all lines going into the agenda
6775 have this property, only most.
6777 The function should return the modified string. It is probably best
6778 to ONLY change text properties.
6780 You can also use this function as a filter, by returning nil for lines
6781 you don't want to have in the agenda at all. For this application, you
6782 could bind the variable in the options section of a custom command.")
6784 (defun org-agenda-finalize-entries (list &optional type)
6785 "Sort, limit and concatenate the LIST of agenda items.
6786 The optional argument TYPE tells the agenda type."
6787 (let ((max-effort (cond ((listp org-agenda-max-effort)
6788 (cdr (assoc type org-agenda-max-effort)))
6789 (t org-agenda-max-effort)))
6790 (max-todo (cond ((listp org-agenda-max-todos)
6791 (cdr (assoc type org-agenda-max-todos)))
6792 (t org-agenda-max-todos)))
6793 (max-tags (cond ((listp org-agenda-max-tags)
6794 (cdr (assoc type org-agenda-max-tags)))
6795 (t org-agenda-max-tags)))
6796 (max-entries (cond ((listp org-agenda-max-entries)
6797 (cdr (assoc type org-agenda-max-entries)))
6798 (t org-agenda-max-entries))) l)
6799 (when org-agenda-before-sorting-filter-function
6800 (setq list
6801 (delq nil
6802 (mapcar
6803 org-agenda-before-sorting-filter-function list))))
6804 (setq list (mapcar 'org-agenda-highlight-todo list)
6805 list (mapcar 'identity (sort list 'org-entries-lessp)))
6806 (when max-effort
6807 (setq list (org-agenda-limit-entries
6808 list 'effort-minutes max-effort 'identity)))
6809 (when max-todo
6810 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6811 (when max-tags
6812 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6813 (when max-entries
6814 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6815 (mapconcat 'identity list "\n")))
6817 (defun org-agenda-limit-entries (list prop limit &optional fn)
6818 "Limit the number of agenda entries."
6819 (let ((include (and limit (< limit 0))))
6820 (if limit
6821 (let ((fun (or fn (lambda (p) (if p 1))))
6822 (lim 0))
6823 (delq nil
6824 (mapcar
6825 (lambda (e)
6826 (let ((pval (funcall fun (get-text-property 1 prop e))))
6827 (if pval (setq lim (+ lim pval)))
6828 (cond ((and pval (<= lim (abs limit))) e)
6829 ((and include (not pval)) e))))
6830 list)))
6831 list)))
6833 (defun org-agenda-limit-interactively ()
6834 "In agenda, interactively limit entries to various maximums."
6835 (interactive)
6836 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6837 (num (string-to-number (read-from-minibuffer "How many? "))))
6838 (cond ((equal max ?e)
6839 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6840 ((equal max ?t)
6841 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6842 ((equal max ?T)
6843 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6844 ((equal max ?E)
6845 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6846 (org-agenda-fit-window-to-buffer))
6848 (defun org-agenda-highlight-todo (x)
6849 (let ((org-done-keywords org-done-keywords-for-agenda)
6850 (case-fold-search nil)
6852 (if (eq x 'line)
6853 (save-excursion
6854 (beginning-of-line 1)
6855 (setq re (org-get-at-bol 'org-todo-regexp))
6856 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6857 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6858 (add-text-properties (match-beginning 0) (match-end 1)
6859 (list 'face (org-get-todo-face 1)))
6860 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6861 (delete-region (match-beginning 1) (1- (match-end 0)))
6862 (goto-char (match-beginning 1))
6863 (insert (format org-agenda-todo-keyword-format s)))))
6864 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6865 (setq re (get-text-property 0 'org-todo-regexp x))
6866 (when (and re
6867 ;; Test `pl' because if there's no heading content,
6868 ;; there's no point matching to highlight. Note
6869 ;; that if we didn't test `pl' first, and there
6870 ;; happened to be no keyword from `org-todo-regexp'
6871 ;; on this heading line, then the `equal' comparison
6872 ;; afterwards would spuriously succeed in the case
6873 ;; where `pl' is nil -- causing an args-out-of-range
6874 ;; error when we try to add text properties to text
6875 ;; that isn't there.
6877 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6878 x pl) pl))
6879 (add-text-properties
6880 (or (match-end 1) (match-end 0)) (match-end 0)
6881 (list 'face (org-get-todo-face (match-string 2 x)))
6883 (when (match-end 1)
6884 (setq x (concat (substring x 0 (match-end 1))
6885 (format org-agenda-todo-keyword-format
6886 (match-string 2 x))
6887 (org-add-props " " (text-properties-at 0 x))
6888 (substring x (match-end 3)))))))
6889 x)))
6891 (defsubst org-cmp-priority (a b)
6892 "Compare the priorities of string A and B."
6893 (let ((pa (or (get-text-property 1 'priority a) 0))
6894 (pb (or (get-text-property 1 'priority b) 0)))
6895 (cond ((> pa pb) +1)
6896 ((< pa pb) -1))))
6898 (defsubst org-cmp-effort (a b)
6899 "Compare the effort values of string A and B."
6900 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6901 (ea (or (get-text-property 1 'effort-minutes a) def))
6902 (eb (or (get-text-property 1 'effort-minutes b) def)))
6903 (cond ((> ea eb) +1)
6904 ((< ea eb) -1))))
6906 (defsubst org-cmp-category (a b)
6907 "Compare the string values of categories of strings A and B."
6908 (let ((ca (or (get-text-property 1 'org-category a) ""))
6909 (cb (or (get-text-property 1 'org-category b) "")))
6910 (cond ((string-lessp ca cb) -1)
6911 ((string-lessp cb ca) +1))))
6913 (defsubst org-cmp-todo-state (a b)
6914 "Compare the todo states of strings A and B."
6915 (let* ((ma (or (get-text-property 1 'org-marker a)
6916 (get-text-property 1 'org-hd-marker a)))
6917 (mb (or (get-text-property 1 'org-marker b)
6918 (get-text-property 1 'org-hd-marker b)))
6919 (fa (and ma (marker-buffer ma)))
6920 (fb (and mb (marker-buffer mb)))
6921 (todo-kwds
6922 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6923 (and fb (with-current-buffer fb org-todo-keywords-1))))
6924 (ta (or (get-text-property 1 'todo-state a) ""))
6925 (tb (or (get-text-property 1 'todo-state b) ""))
6926 (la (- (length (member ta todo-kwds))))
6927 (lb (- (length (member tb todo-kwds))))
6928 (donepa (member ta org-done-keywords-for-agenda))
6929 (donepb (member tb org-done-keywords-for-agenda)))
6930 (cond ((and donepa (not donepb)) -1)
6931 ((and (not donepa) donepb) +1)
6932 ((< la lb) -1)
6933 ((< lb la) +1))))
6935 (defsubst org-cmp-alpha (a b)
6936 "Compare the headlines, alphabetically."
6937 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6938 (plb (text-property-any 0 (length b) 'org-heading t b))
6939 (ta (and pla (substring a pla)))
6940 (tb (and plb (substring b plb))))
6941 (when pla
6942 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6943 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6944 (setq ta (substring ta (match-end 0))))
6945 (setq ta (downcase ta)))
6946 (when plb
6947 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6948 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6949 (setq tb (substring tb (match-end 0))))
6950 (setq tb (downcase tb)))
6951 (cond ((not ta) +1)
6952 ((not tb) -1)
6953 ((string-lessp ta tb) -1)
6954 ((string-lessp tb ta) +1))))
6956 (defsubst org-cmp-tag (a b)
6957 "Compare the string values of the first tags of A and B."
6958 (let ((ta (car (last (get-text-property 1 'tags a))))
6959 (tb (car (last (get-text-property 1 'tags b)))))
6960 (cond ((not ta) +1)
6961 ((not tb) -1)
6962 ((string-lessp ta tb) -1)
6963 ((string-lessp tb ta) +1))))
6965 (defsubst org-cmp-time (a b)
6966 "Compare the time-of-day values of strings A and B."
6967 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6968 (ta (or (get-text-property 1 'time-of-day a) def))
6969 (tb (or (get-text-property 1 'time-of-day b) def)))
6970 (cond ((< ta tb) -1)
6971 ((< tb ta) +1))))
6973 (defsubst org-cmp-ts (a b &optional type)
6974 "Compare the timestamps values of entries A and B.
6975 When TYPE is \"scheduled\", \"deadline\", \"timestamp\"
6976 or \"timestamp_ia\", compare within each of these type.
6977 When TYPE is the empty string, compare all timestamps
6978 without respect of their type."
6979 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6980 (ta (or (and (string-match type (get-text-property 1 'type a))
6981 (get-text-property 1 'ts-date a)) def))
6982 (tb (or (and (string-match type (get-text-property 1 'type b))
6983 (get-text-property 1 'ts-date b)) def)))
6984 (cond ((< ta tb) -1)
6985 ((< tb ta) +1))))
6987 (defsubst org-cmp-habit-p (a b)
6988 "Compare the todo states of strings A and B."
6989 (let ((ha (get-text-property 1 'org-habit-p a))
6990 (hb (get-text-property 1 'org-habit-p b)))
6991 (cond ((and ha (not hb)) -1)
6992 ((and (not ha) hb) +1))))
6994 (defun org-entries-lessp (a b)
6995 "Predicate for sorting agenda entries."
6996 ;; The following variables will be used when the form is evaluated.
6997 ;; So even though the compiler complains, keep them.
6998 (let* ((ss org-agenda-sorting-strategy-selected)
6999 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7000 (org-cmp-ts a b "")))
7001 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7002 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7003 (org-cmp-ts a b "scheduled")))
7004 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7005 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7006 (org-cmp-ts a b "deadline")))
7007 (deadline-down (if deadline-up (- deadline-up) nil))
7008 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7009 (org-cmp-ts a b "iatimestamp_ia")))
7010 (tsia-down (if tsia-up (- tsia-up) nil))
7011 (ts-up (and (org-em 'ts-up 'ts-down ss)
7012 (org-cmp-ts a b "timestamp")))
7013 (ts-down (if ts-up (- ts-up) nil))
7014 (time-up (and (org-em 'time-up 'time-down ss)
7015 (org-cmp-time a b)))
7016 (time-down (if time-up (- time-up) nil))
7017 (priority-up (and (org-em 'priority-up 'priority-down ss)
7018 (org-cmp-priority a b)))
7019 (priority-down (if priority-up (- priority-up) nil))
7020 (effort-up (and (org-em 'effort-up 'effort-down ss)
7021 (org-cmp-effort a b)))
7022 (effort-down (if effort-up (- effort-up) nil))
7023 (category-up (and (or (org-em 'category-up 'category-down ss)
7024 (memq 'category-keep ss))
7025 (org-cmp-category a b)))
7026 (category-down (if category-up (- category-up) nil))
7027 (category-keep (if category-up +1 nil))
7028 (tag-up (and (org-em 'tag-up 'tag-down ss)
7029 (org-cmp-tag a b)))
7030 (tag-down (if tag-up (- tag-up) nil))
7031 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7032 (org-cmp-todo-state a b)))
7033 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7034 (habit-up (and (org-em 'habit-up 'habit-down ss)
7035 (org-cmp-habit-p a b)))
7036 (habit-down (if habit-up (- habit-up) nil))
7037 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7038 (org-cmp-alpha a b)))
7039 (alpha-down (if alpha-up (- alpha-up) nil))
7040 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7041 user-defined-up user-defined-down)
7042 (if (and need-user-cmp org-agenda-cmp-user-defined
7043 (functionp org-agenda-cmp-user-defined))
7044 (setq user-defined-up
7045 (funcall org-agenda-cmp-user-defined a b)
7046 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7047 (cdr (assoc
7048 (eval (cons 'or org-agenda-sorting-strategy-selected))
7049 '((-1 . t) (1 . nil) (nil . nil))))))
7051 ;;; Agenda restriction lock
7053 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7054 "Overlay to mark the headline to which agenda commands are restricted.")
7055 (overlay-put org-agenda-restriction-lock-overlay
7056 'face 'org-agenda-restriction-lock)
7057 (overlay-put org-agenda-restriction-lock-overlay
7058 'help-echo "Agendas are currently limited to this subtree.")
7059 (org-detach-overlay org-agenda-restriction-lock-overlay)
7061 (defun org-agenda-set-restriction-lock (&optional type)
7062 "Set restriction lock for agenda, to current subtree or file.
7063 Restriction will be the file if TYPE is `file', or if type is the
7064 universal prefix '(4), or if the cursor is before the first headline
7065 in the file. Otherwise, restriction will be to the current subtree."
7066 (interactive "P")
7067 (and (equal type '(4)) (setq type 'file))
7068 (setq type (cond
7069 (type type)
7070 ((org-at-heading-p) 'subtree)
7071 ((condition-case nil (org-back-to-heading t) (error nil))
7072 'subtree)
7073 (t 'file)))
7074 (if (eq type 'subtree)
7075 (progn
7076 (setq org-agenda-restrict t)
7077 (setq org-agenda-overriding-restriction 'subtree)
7078 (put 'org-agenda-files 'org-restrict
7079 (list (buffer-file-name (buffer-base-buffer))))
7080 (org-back-to-heading t)
7081 (move-overlay org-agenda-restriction-lock-overlay
7082 (point)
7083 (if org-agenda-restriction-lock-highlight-subtree
7084 (save-excursion (org-end-of-subtree t t) (point))
7085 (point-at-eol)))
7086 (move-marker org-agenda-restrict-begin (point))
7087 (move-marker org-agenda-restrict-end
7088 (save-excursion (org-end-of-subtree t t)))
7089 (message "Locking agenda restriction to subtree"))
7090 (put 'org-agenda-files 'org-restrict
7091 (list (buffer-file-name (buffer-base-buffer))))
7092 (setq org-agenda-restrict nil)
7093 (setq org-agenda-overriding-restriction 'file)
7094 (move-marker org-agenda-restrict-begin nil)
7095 (move-marker org-agenda-restrict-end nil)
7096 (message "Locking agenda restriction to file"))
7097 (setq current-prefix-arg nil)
7098 (org-agenda-maybe-redo))
7100 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7101 "Remove the agenda restriction lock."
7102 (interactive "P")
7103 (org-detach-overlay org-agenda-restriction-lock-overlay)
7104 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7105 (setq org-agenda-overriding-restriction nil)
7106 (setq org-agenda-restrict nil)
7107 (put 'org-agenda-files 'org-restrict nil)
7108 (move-marker org-agenda-restrict-begin nil)
7109 (move-marker org-agenda-restrict-end nil)
7110 (setq current-prefix-arg nil)
7111 (message "Agenda restriction lock removed")
7112 (or noupdate (org-agenda-maybe-redo)))
7114 (defun org-agenda-maybe-redo ()
7115 "If there is any window showing the agenda view, update it."
7116 (let ((w (get-buffer-window org-agenda-buffer-name t))
7117 (w0 (selected-window)))
7118 (when w
7119 (select-window w)
7120 (org-agenda-redo)
7121 (select-window w0)
7122 (if org-agenda-overriding-restriction
7123 (message "Agenda view shifted to new %s restriction"
7124 org-agenda-overriding-restriction)
7125 (message "Agenda restriction lock removed")))))
7127 ;;; Agenda commands
7129 (defun org-agenda-check-type (error &rest types)
7130 "Check if agenda buffer is of allowed type.
7131 If ERROR is non-nil, throw an error, otherwise just return nil.
7132 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7133 (if (not org-agenda-type)
7134 (error "No Org agenda currently displayed")
7135 (if (memq org-agenda-type types)
7137 (if error
7138 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7139 nil))))
7141 (defun org-agenda-Quit ()
7142 "Exit the agenda and kill buffers loaded by `org-agenda'.
7143 Also restore the window configuration."
7144 (interactive)
7145 (if org-agenda-columns-active
7146 (org-columns-quit)
7147 (let ((buf (current-buffer)))
7148 (if (eq org-agenda-window-setup 'other-frame)
7149 (progn
7150 (org-agenda-reset-markers)
7151 (kill-buffer buf)
7152 (org-columns-remove-overlays)
7153 (setq org-agenda-archives-mode nil)
7154 (delete-frame))
7155 (and (not (eq org-agenda-window-setup 'current-window))
7156 (not (one-window-p))
7157 (delete-window))
7158 (org-agenda-reset-markers)
7159 (kill-buffer buf)
7160 (org-columns-remove-overlays)
7161 (setq org-agenda-archives-mode nil)))
7162 (setq org-agenda-buffer nil)
7163 ;; Maybe restore the pre-agenda window configuration.
7164 (and org-agenda-restore-windows-after-quit
7165 (not (eq org-agenda-window-setup 'other-frame))
7166 org-agenda-pre-window-conf
7167 (set-window-configuration org-agenda-pre-window-conf)
7168 (setq org-agenda-pre-window-conf nil))))
7170 (defun org-agenda-quit ()
7171 "Exit the agenda and restore the window configuration.
7172 When `org-agenda-sticky' is non-nil, only bury the agenda."
7173 (interactive)
7174 (if (and (eq org-indirect-buffer-display 'other-window)
7175 org-last-indirect-buffer)
7176 (let ((org-last-indirect-window
7177 (get-buffer-window org-last-indirect-buffer)))
7178 (if org-last-indirect-window
7179 (delete-window org-last-indirect-window))))
7180 (if org-agenda-columns-active
7181 (org-columns-quit)
7182 (if org-agenda-sticky
7183 (let ((buf (current-buffer)))
7184 (if (eq org-agenda-window-setup 'other-frame)
7185 (progn
7186 (delete-frame))
7187 (and (not (eq org-agenda-window-setup 'current-window))
7188 (not (one-window-p))
7189 (delete-window)))
7190 (with-current-buffer buf
7191 (bury-buffer)
7192 ;; Maybe restore the pre-agenda window configuration.
7193 (and org-agenda-restore-windows-after-quit
7194 (not (eq org-agenda-window-setup 'other-frame))
7195 org-agenda-pre-window-conf
7196 (set-window-configuration org-agenda-pre-window-conf)
7197 (setq org-agenda-pre-window-conf nil))))
7198 (org-agenda-Quit))))
7200 (defun org-agenda-exit ()
7201 "Exit the agenda and restore the window configuration.
7202 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7203 buffers visited directly by the user will not be touched."
7204 (interactive)
7205 (org-release-buffers org-agenda-new-buffers)
7206 (setq org-agenda-new-buffers nil)
7207 (org-agenda-Quit))
7209 (defun org-agenda-kill-all-agenda-buffers ()
7210 "Kill all buffers in `org-agenda-mode'.
7211 This is used when toggling sticky agendas.
7212 You can also explicitly invoke it with `C-c a C-k'."
7213 (interactive)
7214 (let (blist)
7215 (dolist (buf (buffer-list))
7216 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7217 (push buf blist)))
7218 (mapc 'kill-buffer blist)))
7220 (defun org-agenda-execute (arg)
7221 "Execute another agenda command, keeping same window.
7222 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7223 in the agenda."
7224 (interactive "P")
7225 (let ((org-agenda-window-setup 'current-window))
7226 (org-agenda arg)))
7228 (defun org-agenda-redo (&optional all)
7229 "Rebuild possibly ALL agenda view(s) in the current buffer."
7230 (interactive "P")
7231 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7232 (cpa (unless (eq all t) current-prefix-arg))
7233 (org-agenda-doing-sticky-redo org-agenda-sticky)
7234 (org-agenda-sticky nil)
7235 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7236 org-agenda-buffer-name))
7237 (org-agenda-keep-modes t)
7238 (tag-filter org-agenda-tag-filter)
7239 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7240 (top-hl-filter org-agenda-top-headline-filter)
7241 (cat-filter org-agenda-category-filter)
7242 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7243 (re-filter org-agenda-regexp-filter)
7244 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7245 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7246 (cols org-agenda-columns-active)
7247 (line (org-current-line))
7248 (window-line (- line (org-current-line (window-start))))
7249 (lprops (get 'org-agenda-redo-command 'org-lprops))
7250 (redo-cmd (get-text-property p 'org-redo-cmd))
7251 (last-args (get-text-property p 'org-last-args))
7252 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7253 (org-agenda-overriding-cmd-arguments
7254 (unless (eq all t)
7255 (cond ((listp last-args)
7256 (cons (or cpa (car last-args)) (cdr last-args)))
7257 ((stringp last-args)
7258 last-args))))
7259 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7260 (put 'org-agenda-tag-filter :preset-filter nil)
7261 (put 'org-agenda-category-filter :preset-filter nil)
7262 (put 'org-agenda-regexp-filter :preset-filter nil)
7263 (and cols (org-columns-quit))
7264 (message "Rebuilding agenda buffer...")
7265 (if series-redo-cmd
7266 (eval series-redo-cmd)
7267 (org-let lprops redo-cmd))
7268 (setq org-agenda-undo-list nil
7269 org-agenda-pending-undo-list nil
7270 org-agenda-tag-filter tag-filter
7271 org-agenda-category-filter cat-filter
7272 org-agenda-regexp-filter re-filter
7273 org-agenda-top-headline-filter top-hl-filter)
7274 (message "Rebuilding agenda buffer...done")
7275 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7276 (put 'org-agenda-category-filter :preset-filter cat-preset)
7277 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7278 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7279 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7280 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7281 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7282 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7283 (org-goto-line line)
7284 (recenter window-line)))
7286 (defvar org-global-tags-completion-table nil)
7287 (defvar org-agenda-filter-form nil)
7288 (defvar org-agenda-filtered-by-category nil)
7290 (defun org-agenda-filter-by-category (strip)
7291 "Keep only those lines in the agenda buffer that have a specific category.
7292 The category is that of the current line."
7293 (interactive "P")
7294 (if (and org-agenda-filtered-by-category
7295 org-agenda-category-filter)
7296 (org-agenda-filter-show-all-cat)
7297 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7298 (if (and cat (not (string= "" cat)))
7299 (org-agenda-filter-apply
7300 (setq org-agenda-category-filter
7301 (list (concat (if strip "-" "+") cat)))
7302 'category)
7303 (error "No category at point")))))
7305 (defun org-find-top-headline (&optional pos)
7306 "Find the topmost parent headline and return it."
7307 (save-excursion
7308 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7309 (if pos (goto-char pos))
7310 ;; Skip up to the topmost parent
7311 (while (ignore-errors (outline-up-heading 1) t))
7312 (ignore-errors
7313 (nth 4 (org-heading-components))))))
7315 (defvar org-agenda-filtered-by-top-headline nil)
7316 (defun org-agenda-filter-by-top-headline (strip)
7317 "Keep only those lines that are descendants from the same top headline.
7318 The top headline is that of the current line."
7319 (interactive "P")
7320 (if org-agenda-filtered-by-top-headline
7321 (progn
7322 (setq org-agenda-filtered-by-top-headline nil
7323 org-agenda-top-headline-filter nil)
7324 (org-agenda-filter-show-all-cat))
7325 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7326 (if cat (org-agenda-filter-top-headline-apply cat strip)
7327 (error "No top-level category at point")))))
7329 (defvar org-agenda-regexp-filter nil)
7330 (defun org-agenda-filter-by-regexp (strip)
7331 "Filter agenda entries by a regular expression.
7332 Regexp filters are cumulative.
7333 With no prefix argument, keep entries matching the regexp.
7334 With one prefix argument, filter out entries matching the regexp.
7335 With two prefix arguments, remove the regexp filters."
7336 (interactive "P")
7337 (if (not (equal strip '(16)))
7338 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7339 (read-from-minibuffer
7340 (if (equal strip '(4))
7341 "Filter out entries matching regexp: "
7342 "Narrow to entries matching regexp: ")))))
7343 (push flt org-agenda-regexp-filter)
7344 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7345 (org-agenda-filter-show-all-re)
7346 (message "Regexp filter removed")))
7348 (defun org-agenda-filter-remove-all ()
7349 "Remove all filters from the current agenda buffer."
7350 (interactive)
7351 (when org-agenda-tag-filter
7352 (org-agenda-filter-show-all-tag))
7353 (when org-agenda-category-filter
7354 (org-agenda-filter-show-all-cat))
7355 (when org-agenda-regexp-filter
7356 (org-agenda-filter-show-all-re)))
7358 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7359 "Keep only those lines in the agenda buffer that have a specific tag.
7360 The tag is selected with its fast selection letter, as configured.
7361 With prefix argument STRIP, remove all lines that do have the tag.
7362 A lisp caller can specify CHAR. NARROW means that the new tag should be
7363 used to narrow the search - the interactive user can also press `-' or `+'
7364 to switch to narrowing."
7365 (interactive "P")
7366 (let* ((alist org-tag-alist-for-agenda)
7367 (tag-chars (mapconcat
7368 (lambda (x) (if (and (not (symbolp (car x)))
7369 (cdr x))
7370 (char-to-string (cdr x))
7371 ""))
7372 alist ""))
7373 (efforts (org-split-string
7374 (or (cdr (assoc (concat org-effort-property "_ALL")
7375 org-global-properties))
7376 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7377 "")))
7378 (effort-op org-agenda-filter-effort-default-operator)
7379 (effort-prompt "")
7380 (inhibit-read-only t)
7381 (current org-agenda-tag-filter)
7382 maybe-refresh a n tag)
7383 (unless char
7384 (message
7385 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7386 (if narrow "Narrow" "Filter") tag-chars
7387 (if org-agenda-auto-exclude-function "[RET], " ""))
7388 (setq char (read-char-exclusive)))
7389 (when (member char '(?+ ?-))
7390 ;; Narrowing down
7391 (cond ((equal char ?-) (setq strip t narrow t))
7392 ((equal char ?+) (setq strip nil narrow t)))
7393 (message
7394 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7395 (setq char (read-char-exclusive)))
7396 (when (member char '(?< ?> ?= ??))
7397 ;; An effort operator
7398 (setq effort-op (char-to-string char))
7399 (setq alist nil) ; to make sure it will be interpreted as effort.
7400 (unless (equal char ??)
7401 (loop for i from 0 to 9 do
7402 (setq effort-prompt
7403 (concat
7404 effort-prompt " ["
7405 (if (= i 9) "0" (int-to-string (1+ i)))
7406 "]" (nth i efforts))))
7407 (message "Effort%s: %s " effort-op effort-prompt)
7408 (setq char (read-char-exclusive))
7409 (when (or (< char ?0) (> char ?9))
7410 (error "Need 1-9,0 to select effort"))))
7411 (when (equal char ?\t)
7412 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7413 (org-set-local 'org-global-tags-completion-table
7414 (org-global-tags-completion-table)))
7415 (let ((completion-ignore-case t))
7416 (setq tag (org-icompleting-read
7417 "Tag: " org-global-tags-completion-table))))
7418 (cond
7419 ((equal char ?\r)
7420 (org-agenda-filter-show-all-tag)
7421 (when org-agenda-auto-exclude-function
7422 (setq org-agenda-tag-filter nil)
7423 (dolist (tag (org-agenda-get-represented-tags))
7424 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7425 (if modifier
7426 (push modifier org-agenda-tag-filter))))
7427 (if (not (null org-agenda-tag-filter))
7428 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7429 (setq maybe-refresh t))
7430 ((equal char ?/)
7431 (org-agenda-filter-show-all-tag)
7432 (when (get 'org-agenda-tag-filter :preset-filter)
7433 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7434 (setq maybe-refresh t))
7435 ((equal char ?. )
7436 (setq org-agenda-tag-filter
7437 (mapcar (lambda(tag) (concat "+" tag))
7438 (org-get-at-bol 'tags)))
7439 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7440 (setq maybe-refresh t))
7441 ((or (equal char ?\ )
7442 (setq a (rassoc char alist))
7443 (and (>= char ?0) (<= char ?9)
7444 (setq n (if (= char ?0) 9 (- char ?0 1))
7445 tag (concat effort-op (nth n efforts))
7446 a (cons tag nil)))
7447 (and (= char ??)
7448 (setq tag "?eff")
7449 a (cons tag nil))
7450 (and tag (setq a (cons tag nil))))
7451 (org-agenda-filter-show-all-tag)
7452 (setq tag (car a))
7453 (setq org-agenda-tag-filter
7454 (cons (concat (if strip "-" "+") tag)
7455 (if narrow current nil)))
7456 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7457 (setq maybe-refresh t))
7458 (t (error "Invalid tag selection character %c" char)))
7459 (when (and maybe-refresh
7460 (eq org-agenda-clockreport-mode 'with-filter))
7461 (org-agenda-redo))))
7463 (defun org-agenda-get-represented-tags ()
7464 "Get a list of all tags currently represented in the agenda."
7465 (let (p tags)
7466 (save-excursion
7467 (goto-char (point-min))
7468 (while (setq p (next-single-property-change (point) 'tags))
7469 (goto-char p)
7470 (mapc (lambda (x) (add-to-list 'tags x))
7471 (get-text-property (point) 'tags))))
7472 tags))
7474 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7475 "Refine the current filter. See `org-agenda-filter-by-tag'."
7476 (interactive "P")
7477 (org-agenda-filter-by-tag strip char 'refine))
7479 (defun org-agenda-filter-make-matcher (filter type)
7480 "Create the form that tests a line for agenda filter."
7481 (let (f f1)
7482 (cond
7483 ;; Tag filter
7484 ((eq type 'tag)
7485 (setq filter
7486 (delete-dups
7487 (append (get 'org-agenda-tag-filter :preset-filter)
7488 filter)))
7489 (dolist (x filter)
7490 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7491 (ffunc
7492 (lambda (nf0 nf01 fltr notgroup op)
7493 (dolist (x fltr)
7494 (if (member x '("-" "+"))
7495 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7496 (if (string-match "[<=>?]" x)
7497 (setq nf01 (org-agenda-filter-effort-form x))
7498 (setq nf01 (list 'member (downcase (substring x 1))
7499 'tags)))
7500 (when (equal (string-to-char x) ?-)
7501 (setq nf01 (list 'not nf01))
7502 (when (not notgroup) (setq op 'and))))
7503 (push nf01 nf0))
7504 (if notgroup
7505 (push (cons 'and nf0) f)
7506 (push (cons (or op 'or) nf0) f)))))
7507 (if (equal nfilter filter)
7508 (funcall ffunc f1 f filter t nil)
7509 (funcall ffunc nf1 nf nfilter nil nil)))))
7510 ;; Category filter
7511 ((eq type 'category)
7512 (setq filter
7513 (delete-dups
7514 (append (get 'org-agenda-category-filter :preset-filter)
7515 filter)))
7516 (dolist (x filter)
7517 (if (equal "-" (substring x 0 1))
7518 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7519 (setq f1 (list 'equal (substring x 1) 'cat)))
7520 (push f1 f)))
7521 ;; Regexp filter
7522 ((eq type 'regexp)
7523 (setq filter
7524 (delete-dups
7525 (append (get 'org-agenda-regexp-filter :preset-filter)
7526 filter)))
7527 (dolist (x filter)
7528 (if (equal "-" (substring x 0 1))
7529 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7530 (setq f1 (list 'string-match (substring x 1) 'txt)))
7531 (push f1 f))))
7532 (cons 'and (nreverse f))))
7534 (defun org-agenda-filter-effort-form (e)
7535 "Return the form to compare the effort of the current line with what E says.
7536 E looks like \"+<2:25\"."
7537 (let (op)
7538 (setq e (substring e 1))
7539 (setq op (string-to-char e) e (substring e 1))
7540 (setq op (cond ((equal op ?<) '<=)
7541 ((equal op ?>) '>=)
7542 ((equal op ??) op)
7543 (t '=)))
7544 (list 'org-agenda-compare-effort (list 'quote op)
7545 (org-duration-string-to-minutes e))))
7547 (defun org-agenda-compare-effort (op value)
7548 "Compare the effort of the current line with VALUE, using OP.
7549 If the line does not have an effort defined, return nil."
7550 (let ((eff (org-get-at-bol 'effort-minutes)))
7551 (if (equal op ??)
7552 (not eff)
7553 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7554 value))))
7556 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7557 "Expand group tags in FILTER for the agenda.
7558 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7559 (if org-group-tags
7560 (let ((case-fold-search t) rtn)
7561 (mapc
7562 (lambda (f)
7563 (let (f0 dir)
7564 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7565 (setq dir (match-string 1 f) f0 (match-string 2 f))
7566 (setq dir (if no-operator "" "+") f0 f))
7567 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7568 (org-tags-expand f0 t t))
7569 rtn))))
7570 filter)
7571 (reverse rtn))
7572 filter))
7574 (defun org-agenda-filter-apply (filter type)
7575 "Set FILTER as the new agenda filter and apply it."
7576 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7577 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7578 (let (tags cat txt)
7579 (setq org-agenda-filter-form
7580 (org-agenda-filter-make-matcher filter type))
7581 (if (and (eq type 'category)
7582 (not (equal (substring (car filter) 0 1) "-")))
7583 ;; Only set `org-agenda-filtered-by-category' to t
7584 ;; when a unique category is used as the filter
7585 (setq org-agenda-filtered-by-category t))
7586 (org-agenda-set-mode-name)
7587 (save-excursion
7588 (goto-char (point-min))
7589 (while (not (eobp))
7590 (if (org-get-at-bol 'org-marker)
7591 (progn
7592 (setq tags ; used in eval
7593 (apply 'append
7594 (mapcar (lambda (f)
7595 (org-agenda-filter-expand-tags (list f) t))
7596 (org-get-at-bol 'tags)))
7597 cat (get-text-property (point) 'org-category)
7598 txt (get-text-property (point) 'txt))
7599 (if (not (eval org-agenda-filter-form))
7600 (org-agenda-filter-hide-line type))
7601 (beginning-of-line 2))
7602 (beginning-of-line 2))))
7603 (if (get-char-property (point) 'invisible)
7604 (ignore-errors (org-agenda-previous-line)))))
7606 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7607 "Filter by top headline HL."
7608 (org-agenda-set-mode-name)
7609 (save-excursion
7610 (goto-char (point-min))
7611 (while (not (eobp))
7612 (let* ((pos (org-get-at-bol 'org-hd-marker))
7613 (tophl (and pos (org-find-top-headline pos))))
7614 (if (and tophl (funcall (if negative 'identity 'not)
7615 (string= hl tophl)))
7616 (org-agenda-filter-hide-line 'category)))
7617 (beginning-of-line 2)))
7618 (if (get-char-property (point) 'invisible)
7619 (org-agenda-previous-line))
7620 (setq org-agenda-top-headline-filter hl
7621 org-agenda-filtered-by-top-headline t))
7623 (defun org-agenda-filter-hide-line (type)
7624 "Hide lines with TYPE in the agenda buffer."
7625 (let (ov)
7626 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7627 (point-at-eol)))
7628 (overlay-put ov 'invisible t)
7629 (overlay-put ov 'type type)
7630 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7631 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7632 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7634 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7635 (setq pos (or pos (point)))
7636 (save-excursion
7637 (dolist (ov (overlays-at pos))
7638 (when (and (overlay-get ov 'invisible)
7639 (eq (overlay-get ov 'type) 'tag))
7640 (goto-char pos)
7641 (if (< (overlay-start ov) (point-at-eol))
7642 (move-overlay ov (point-at-eol)
7643 (overlay-end ov)))))))
7645 (defun org-agenda-filter-show-all-tag nil
7646 "Remove tag filter overlays from the agenda buffer."
7647 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7648 (setq org-agenda-tag-filter-overlays nil
7649 org-agenda-tag-filter nil
7650 org-agenda-filter-form nil)
7651 (org-agenda-set-mode-name))
7653 (defun org-agenda-filter-show-all-re nil
7654 "Remove regexp filter overlays from the agenda buffer."
7655 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7656 (setq org-agenda-re-filter-overlays nil
7657 org-agenda-regexp-filter nil
7658 org-agenda-filter-form nil)
7659 (org-agenda-set-mode-name))
7661 (defun org-agenda-filter-show-all-cat nil
7662 "Remove category filter overlays from the agenda buffer."
7663 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7664 (setq org-agenda-cat-filter-overlays nil
7665 org-agenda-filtered-by-category nil
7666 org-agenda-category-filter nil
7667 org-agenda-filter-form nil)
7668 (org-agenda-set-mode-name))
7670 (defun org-agenda-manipulate-query-add ()
7671 "Manipulate the query by adding a search term with positive selection.
7672 Positive selection means the term must be matched for selection of an entry."
7673 (interactive)
7674 (org-agenda-manipulate-query ?\[))
7675 (defun org-agenda-manipulate-query-subtract ()
7676 "Manipulate the query by adding a search term with negative selection.
7677 Negative selection means term must not be matched for selection of an entry."
7678 (interactive)
7679 (org-agenda-manipulate-query ?\]))
7680 (defun org-agenda-manipulate-query-add-re ()
7681 "Manipulate the query by adding a search regexp with positive selection.
7682 Positive selection means the regexp must match for selection of an entry."
7683 (interactive)
7684 (org-agenda-manipulate-query ?\{))
7685 (defun org-agenda-manipulate-query-subtract-re ()
7686 "Manipulate the query by adding a search regexp with negative selection.
7687 Negative selection means regexp must not match for selection of an entry."
7688 (interactive)
7689 (org-agenda-manipulate-query ?\}))
7690 (defun org-agenda-manipulate-query (char)
7691 (cond
7692 ((memq org-agenda-type '(timeline agenda))
7693 (let ((org-agenda-include-inactive-timestamps t))
7694 (org-agenda-redo))
7695 (message "Display now includes inactive timestamps as well"))
7696 ((eq org-agenda-type 'search)
7697 (org-add-to-string
7698 'org-agenda-query-string
7699 (if org-agenda-last-search-view-search-was-boolean
7700 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7701 (?\{ . " +{}") (?\} . " -{}"))))
7702 " "))
7703 (setq org-agenda-redo-command
7704 (list 'org-search-view
7705 (car (get-text-property (min (1- (point-max)) (point))
7706 'org-last-args))
7707 org-agenda-query-string
7708 (+ (length org-agenda-query-string)
7709 (if (member char '(?\{ ?\})) 0 1))))
7710 (set-register org-agenda-query-register org-agenda-query-string)
7711 (let ((org-agenda-overriding-arguments
7712 (cdr org-agenda-redo-command)))
7713 (org-agenda-redo)))
7714 (t (error "Cannot manipulate query for %s-type agenda buffers"
7715 org-agenda-type))))
7717 (defun org-add-to-string (var string)
7718 (set var (concat (symbol-value var) string)))
7720 (defun org-agenda-goto-date (span)
7721 "Jump to DATE in agenda."
7722 (interactive "P")
7723 (let* ((org-read-date-prefer-future
7724 (eval org-agenda-jump-prefer-future))
7725 (date (org-read-date))
7726 (day (time-to-days (org-time-string-to-time date)))
7727 (org-agenda-sticky-orig org-agenda-sticky)
7728 (org-agenda-buffer-tmp-name (buffer-name))
7729 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7730 (0-arg (or current-prefix-arg (car args)))
7731 (2-arg (nth 2 args))
7732 (newcmd (list 'org-agenda-list 0-arg date
7733 (org-agenda-span-to-ndays 2-arg)))
7734 (newargs (cdr newcmd))
7735 (inhibit-read-only t)
7736 org-agenda-sticky)
7737 (if (not (org-agenda-check-type t 'agenda))
7738 (error "Not available in non-agenda views")
7739 (add-text-properties (point-min) (point-max)
7740 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7741 (org-agenda-redo)
7742 (goto-char (point-min))
7743 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7744 (save-excursion (move-beginning-of-line 2) (eobp))))
7745 (move-beginning-of-line 2))
7746 (setq org-agenda-sticky org-agenda-sticky-orig
7747 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7749 (defun org-agenda-goto-today ()
7750 "Go to today."
7751 (interactive)
7752 (org-agenda-check-type t 'timeline 'agenda)
7753 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7754 (curspan (nth 2 args))
7755 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7756 (cond
7757 (tdpos (goto-char tdpos))
7758 ((eq org-agenda-type 'agenda)
7759 (let* ((sd (org-agenda-compute-starting-span
7760 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7761 (org-agenda-overriding-arguments args))
7762 (setf (nth 1 org-agenda-overriding-arguments) sd)
7763 (org-agenda-redo)
7764 (org-agenda-find-same-or-today-or-agenda)))
7765 (t (error "Cannot find today")))))
7767 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7768 (goto-char
7769 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7770 (text-property-any (point-min) (point-max) 'org-today t)
7771 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7772 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7773 (org-agenda-goto-block-beginning))
7774 (point-min))))
7776 (defun org-agenda-goto-block-beginning ()
7777 "Go the agenda block beginning."
7778 (interactive)
7779 (if (not (derived-mode-p 'org-agenda-mode))
7780 (error "Cannot execute this command outside of org-agenda-mode buffers")
7781 (let (dest)
7782 (save-excursion
7783 (unless (looking-at "\\'")
7784 (forward-char))
7785 (let* ((prop 'org-agenda-structural-header)
7786 (p (previous-single-property-change (point) prop))
7787 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7788 (1- (point))) prop)))
7789 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7790 (if (not dest)
7791 (error "Cannot find the beginning of the blog")
7792 (goto-char dest)
7793 (move-beginning-of-line 1)))))
7795 (defun org-agenda-later (arg)
7796 "Go forward in time by the current span.
7797 With prefix ARG, go forward that many times the current span."
7798 (interactive "p")
7799 (org-agenda-check-type t 'agenda)
7800 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7801 (span (or (nth 2 args) org-agenda-current-span))
7802 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7803 (greg (calendar-gregorian-from-absolute sd))
7804 (cnt (org-get-at-bol 'org-day-cnt))
7805 greg2)
7806 (cond
7807 ((numberp span)
7808 (setq sd (+ (* span arg) sd)))
7809 ((eq span 'day)
7810 (setq sd (+ arg sd)))
7811 ((eq span 'week)
7812 (setq sd (+ (* 7 arg) sd)))
7813 ((eq span 'month)
7814 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7815 sd (calendar-absolute-from-gregorian greg2))
7816 (setcar greg2 (1+ (car greg2))))
7817 ((eq span 'year)
7818 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7819 sd (calendar-absolute-from-gregorian greg2))
7820 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7822 (setq sd (+ (* span arg) sd))))
7823 (let ((org-agenda-overriding-cmd
7824 ;; `cmd' may have been set by `org-agenda-run-series' which
7825 ;; uses `org-agenda-overriding-cmd' to decide whether
7826 ;; overriding is allowed for `cmd'
7827 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7828 (org-agenda-overriding-arguments
7829 (list (car args) sd span)))
7830 (org-agenda-redo)
7831 (org-agenda-find-same-or-today-or-agenda cnt))))
7833 (defun org-agenda-earlier (arg)
7834 "Go backward in time by the current span.
7835 With prefix ARG, go backward that many times the current span."
7836 (interactive "p")
7837 (org-agenda-later (- arg)))
7839 (defun org-agenda-view-mode-dispatch ()
7840 "Call one of the view mode commands."
7841 (interactive)
7842 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7843 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7844 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7845 (let ((a (read-char-exclusive)))
7846 (case a
7847 (?\ (call-interactively 'org-agenda-reset-view))
7848 (?d (call-interactively 'org-agenda-day-view))
7849 (?w (call-interactively 'org-agenda-week-view))
7850 (?m (call-interactively 'org-agenda-month-view))
7851 (?y (call-interactively 'org-agenda-year-view))
7852 (?l (call-interactively 'org-agenda-log-mode))
7853 (?L (org-agenda-log-mode '(4)))
7854 (?c (org-agenda-log-mode 'clockcheck))
7855 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7856 (?a (call-interactively 'org-agenda-archives-mode))
7857 (?A (org-agenda-archives-mode 'files))
7858 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7859 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7860 (?G (call-interactively 'org-agenda-toggle-time-grid))
7861 (?D (call-interactively 'org-agenda-toggle-diary))
7862 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7863 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7864 (org-agenda-check-type t 'timeline 'agenda)
7865 (org-agenda-redo))
7866 (message "Display now includes inactive timestamps as well"))
7867 (?q (message "Abort"))
7868 (otherwise (error "Invalid key" )))))
7870 (defun org-agenda-reset-view ()
7871 "Switch to default view for agenda."
7872 (interactive)
7873 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7874 (defun org-agenda-day-view (&optional day-of-month)
7875 "Switch to daily view for agenda.
7876 With argument DAY-OF-MONTH, switch to that day of the month."
7877 (interactive "P")
7878 (org-agenda-change-time-span 'day day-of-month))
7879 (defun org-agenda-week-view (&optional iso-week)
7880 "Switch to daily view for agenda.
7881 With argument ISO-WEEK, switch to the corresponding ISO week.
7882 If ISO-WEEK has more then 2 digits, only the last two encode the
7883 week. Any digits before this encode a year. So 200712 means
7884 week 12 of year 2007. Years in the range 1938-2037 can also be
7885 written as 2-digit years."
7886 (interactive "P")
7887 (org-agenda-change-time-span 'week iso-week))
7888 (defun org-agenda-month-view (&optional month)
7889 "Switch to monthly view for agenda.
7890 With argument MONTH, switch to that month."
7891 (interactive "P")
7892 (org-agenda-change-time-span 'month month))
7893 (defun org-agenda-year-view (&optional year)
7894 "Switch to yearly view for agenda.
7895 With argument YEAR, switch to that year.
7896 If MONTH has more then 2 digits, only the last two encode the
7897 month. Any digits before this encode a year. So 200712 means
7898 December year 2007. Years in the range 1938-2037 can also be
7899 written as 2-digit years."
7900 (interactive "P")
7901 (when year
7902 (setq year (org-small-year-to-year year)))
7903 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7904 (org-agenda-change-time-span 'year year)
7905 (error "Abort")))
7907 (defun org-agenda-change-time-span (span &optional n)
7908 "Change the agenda view to SPAN.
7909 SPAN may be `day', `week', `month', `year'."
7910 (org-agenda-check-type t 'agenda)
7911 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7912 (curspan (nth 2 args)))
7913 (if (and (not n) (equal curspan span))
7914 (error "Viewing span is already \"%s\"" span))
7915 (let* ((sd (or (org-get-at-bol 'day)
7916 (nth 1 args)
7917 org-starting-day))
7918 (sd (org-agenda-compute-starting-span sd span n))
7919 (org-agenda-overriding-cmd
7920 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7921 (org-agenda-overriding-arguments
7922 (list (car args) sd span)))
7923 (org-agenda-redo)
7924 (org-agenda-find-same-or-today-or-agenda))
7925 (org-agenda-set-mode-name)
7926 (message "Switched to %s view" span)))
7928 (defun org-agenda-compute-starting-span (sd span &optional n)
7929 "Compute starting date for agenda.
7930 SPAN may be `day', `week', `month', `year'. The return value
7931 is a cons cell with the starting date and the number of days,
7932 so that the date SD will be in that range."
7933 (let* ((greg (calendar-gregorian-from-absolute sd))
7934 (dg (nth 1 greg))
7935 (mg (car greg))
7936 (yg (nth 2 greg)))
7937 (cond
7938 ((eq span 'day)
7939 (when n
7940 (setq sd (+ (calendar-absolute-from-gregorian
7941 (list mg 1 yg))
7942 n -1))))
7943 ((eq span 'week)
7944 (let* ((nt (calendar-day-of-week
7945 (calendar-gregorian-from-absolute sd)))
7946 (d (if org-agenda-start-on-weekday
7947 (- nt org-agenda-start-on-weekday)
7950 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7951 (when n
7952 (require 'cal-iso)
7953 (when (> n 99)
7954 (setq y1 (org-small-year-to-year (/ n 100))
7955 n (mod n 100)))
7956 (setq sd
7957 (calendar-absolute-from-iso
7958 (list n 1
7959 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7960 ((eq span 'month)
7961 (let (y1)
7962 (when (and n (> n 99))
7963 (setq y1 (org-small-year-to-year (/ n 100))
7964 n (mod n 100)))
7965 (setq sd (calendar-absolute-from-gregorian
7966 (list (or n mg) 1 (or y1 yg))))))
7967 ((eq span 'year)
7968 (setq sd (calendar-absolute-from-gregorian
7969 (list 1 1 (or n yg))))))
7970 sd))
7972 (defun org-agenda-next-date-line (&optional arg)
7973 "Jump to the next line indicating a date in agenda buffer."
7974 (interactive "p")
7975 (org-agenda-check-type t 'agenda 'timeline)
7976 (beginning-of-line 1)
7977 ;; This does not work if user makes date format that starts with a blank
7978 (if (looking-at "^\\S-") (forward-char 1))
7979 (if (not (re-search-forward "^\\S-" nil t arg))
7980 (progn
7981 (backward-char 1)
7982 (error "No next date after this line in this buffer")))
7983 (goto-char (match-beginning 0)))
7985 (defun org-agenda-previous-date-line (&optional arg)
7986 "Jump to the previous line indicating a date in agenda buffer."
7987 (interactive "p")
7988 (org-agenda-check-type t 'agenda 'timeline)
7989 (beginning-of-line 1)
7990 (if (not (re-search-backward "^\\S-" nil t arg))
7991 (error "No previous date before this line in this buffer")))
7993 ;; Initialize the highlight
7994 (defvar org-hl (make-overlay 1 1))
7995 (overlay-put org-hl 'face 'highlight)
7997 (defun org-highlight (begin end &optional buffer)
7998 "Highlight a region with overlay."
7999 (move-overlay org-hl begin end (or buffer (current-buffer))))
8001 (defun org-unhighlight ()
8002 "Detach overlay INDEX."
8003 (org-detach-overlay org-hl))
8005 (defun org-unhighlight-once ()
8006 "Remove the highlight from its position, and this function from the hook."
8007 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8008 (org-unhighlight))
8010 (defvar org-agenda-pre-follow-window-conf nil)
8011 (defun org-agenda-follow-mode ()
8012 "Toggle follow mode in an agenda buffer."
8013 (interactive)
8014 (unless org-agenda-follow-mode
8015 (setq org-agenda-pre-follow-window-conf
8016 (current-window-configuration)))
8017 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8018 (unless org-agenda-follow-mode
8019 (set-window-configuration org-agenda-pre-follow-window-conf))
8020 (org-agenda-set-mode-name)
8021 (org-agenda-do-context-action)
8022 (message "Follow mode is %s"
8023 (if org-agenda-follow-mode "on" "off")))
8025 (defun org-agenda-entry-text-mode (&optional arg)
8026 "Toggle entry text mode in an agenda buffer."
8027 (interactive "P")
8028 (if (or org-agenda-tag-filter
8029 org-agenda-category-filter
8030 org-agenda-top-headline-filter)
8031 (user-error "Can't show entry text in filtered views")
8032 (setq org-agenda-entry-text-mode (or (integerp arg)
8033 (not org-agenda-entry-text-mode)))
8034 (org-agenda-entry-text-hide)
8035 (and org-agenda-entry-text-mode
8036 (let ((org-agenda-entry-text-maxlines
8037 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8038 (org-agenda-entry-text-show)))
8039 (org-agenda-set-mode-name)
8040 (message "Entry text mode is %s%s"
8041 (if org-agenda-entry-text-mode "on" "off")
8042 (if (not org-agenda-entry-text-mode) ""
8043 (format " (maximum number of lines is %d)"
8044 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8046 (defun org-agenda-clockreport-mode (&optional with-filter)
8047 "Toggle clocktable mode in an agenda buffer.
8048 With prefix arg WITH-FILTER, make the clocktable respect the current
8049 agenda filter."
8050 (interactive "P")
8051 (org-agenda-check-type t 'agenda)
8052 (if with-filter
8053 (setq org-agenda-clockreport-mode 'with-filter)
8054 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8055 (org-agenda-set-mode-name)
8056 (org-agenda-redo)
8057 (message "Clocktable mode is %s"
8058 (if org-agenda-clockreport-mode "on" "off")))
8060 (defun org-agenda-log-mode (&optional special)
8061 "Toggle log mode in an agenda buffer.
8062 With argument SPECIAL, show all possible log items, not only the ones
8063 configured in `org-agenda-log-mode-items'.
8064 With a double `C-u' prefix arg, show *only* log items, nothing else."
8065 (interactive "P")
8066 (org-agenda-check-type t 'agenda 'timeline)
8067 (setq org-agenda-show-log
8068 (cond
8069 ((equal special '(16)) 'only)
8070 ((eq special 'clockcheck)
8071 (if (eq org-agenda-show-log 'clockcheck)
8072 nil 'clockcheck))
8073 (special '(closed clock state))
8074 (t (not org-agenda-show-log))))
8075 (org-agenda-set-mode-name)
8076 (org-agenda-redo)
8077 (message "Log mode is %s"
8078 (if org-agenda-show-log "on" "off")))
8080 (defun org-agenda-archives-mode (&optional with-files)
8081 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8082 When called with a prefix argument, include all archive files as well."
8083 (interactive "P")
8084 (setq org-agenda-archives-mode
8085 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8086 (org-agenda-set-mode-name)
8087 (org-agenda-redo)
8088 (message
8089 "%s"
8090 (cond
8091 ((eq org-agenda-archives-mode nil)
8092 "No archives are included")
8093 ((eq org-agenda-archives-mode 'trees)
8094 (format "Trees with :%s: tag are included" org-archive-tag))
8095 ((eq org-agenda-archives-mode t)
8096 (format "Trees with :%s: tag and all active archive files are included"
8097 org-archive-tag)))))
8099 (defun org-agenda-toggle-diary ()
8100 "Toggle diary inclusion in an agenda buffer."
8101 (interactive)
8102 (org-agenda-check-type t 'agenda)
8103 (setq org-agenda-include-diary (not org-agenda-include-diary))
8104 (org-agenda-redo)
8105 (org-agenda-set-mode-name)
8106 (message "Diary inclusion turned %s"
8107 (if org-agenda-include-diary "on" "off")))
8109 (defun org-agenda-toggle-deadlines ()
8110 "Toggle inclusion of entries with a deadline in an agenda buffer."
8111 (interactive)
8112 (org-agenda-check-type t 'agenda)
8113 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8114 (org-agenda-redo)
8115 (org-agenda-set-mode-name)
8116 (message "Deadlines inclusion turned %s"
8117 (if org-agenda-include-deadlines "on" "off")))
8119 (defun org-agenda-toggle-time-grid ()
8120 "Toggle time grid in an agenda buffer."
8121 (interactive)
8122 (org-agenda-check-type t 'agenda)
8123 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8124 (org-agenda-redo)
8125 (org-agenda-set-mode-name)
8126 (message "Time-grid turned %s"
8127 (if org-agenda-use-time-grid "on" "off")))
8129 (defun org-agenda-set-mode-name ()
8130 "Set the mode name to indicate all the small mode settings."
8131 (setq mode-name
8132 (list "Org-Agenda"
8133 (if (get 'org-agenda-files 'org-restrict) " []" "")
8135 '(:eval (org-agenda-span-name org-agenda-current-span))
8136 (if org-agenda-follow-mode " Follow" "")
8137 (if org-agenda-entry-text-mode " ETxt" "")
8138 (if org-agenda-include-diary " Diary" "")
8139 (if org-agenda-include-deadlines " Ddl" "")
8140 (if org-agenda-use-time-grid " Grid" "")
8141 (if (and (boundp 'org-habit-show-habits)
8142 org-habit-show-habits) " Habit" "")
8143 (cond
8144 ((consp org-agenda-show-log) " LogAll")
8145 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8146 (org-agenda-show-log " Log")
8147 (t ""))
8148 (if (or org-agenda-category-filter
8149 (get 'org-agenda-category-filter :preset-filter))
8150 '(:eval (org-propertize
8151 (concat " <"
8152 (mapconcat
8153 'identity
8154 (append
8155 (get 'org-agenda-category-filter :preset-filter)
8156 org-agenda-category-filter)
8158 ">")
8159 'face 'org-agenda-filter-category
8160 'help-echo "Category used in filtering")) "")
8161 (if (or org-agenda-tag-filter
8162 (get 'org-agenda-tag-filter :preset-filter))
8163 '(:eval (org-propertize
8164 (concat " {"
8165 (mapconcat
8166 'identity
8167 (append
8168 (get 'org-agenda-tag-filter :preset-filter)
8169 org-agenda-tag-filter)
8171 "}")
8172 'face 'org-agenda-filter-tags
8173 'help-echo "Tags used in filtering")) "")
8174 (if (or org-agenda-regexp-filter
8175 (get 'org-agenda-regexp-filter :preset-filter))
8176 '(:eval (org-propertize
8177 (concat " ["
8178 (mapconcat
8179 'identity
8180 (append
8181 (get 'org-agenda-regexp-filter :preset-filter)
8182 org-agenda-regexp-filter)
8184 "]")
8185 'face 'org-agenda-filter-regexp
8186 'help-echo "Regexp used in filtering")) "")
8187 (if org-agenda-archives-mode
8188 (if (eq org-agenda-archives-mode t)
8189 " Archives"
8190 (format " :%s:" org-archive-tag))
8192 (if org-agenda-clockreport-mode
8193 (if (eq org-agenda-clockreport-mode 'with-filter)
8194 " Clock{}" " Clock")
8195 "")))
8196 (force-mode-line-update))
8198 (define-obsolete-function-alias
8199 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8201 (defun org-agenda-update-agenda-type ()
8202 "Update the agenda type after each command."
8203 (setq org-agenda-type
8204 (or (get-text-property (point) 'org-agenda-type)
8205 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8207 (defun org-agenda-next-line ()
8208 "Move cursor to the next line, and show if follow mode is active."
8209 (interactive)
8210 (call-interactively 'next-line)
8211 (org-agenda-do-context-action))
8213 (defun org-agenda-previous-line ()
8214 "Move cursor to the previous line, and show if follow-mode is active."
8215 (interactive)
8216 (call-interactively 'previous-line)
8217 (org-agenda-do-context-action))
8219 (defun org-agenda-next-item (n)
8220 "Move cursor to next agenda item."
8221 (interactive "p")
8222 (let ((col (current-column)))
8223 (dotimes (c n)
8224 (when (next-single-property-change (point-at-eol) 'org-marker)
8225 (move-end-of-line 1)
8226 (goto-char (next-single-property-change (point) 'org-marker))))
8227 (org-move-to-column col))
8228 (org-agenda-do-context-action))
8230 (defun org-agenda-previous-item (n)
8231 "Move cursor to next agenda item."
8232 (interactive "p")
8233 (dotimes (c n)
8234 (let ((col (current-column))
8235 (goto (save-excursion
8236 (move-end-of-line 0)
8237 (previous-single-property-change (point) 'org-marker))))
8238 (if goto (goto-char goto))
8239 (org-move-to-column col)))
8240 (org-agenda-do-context-action))
8242 (defun org-agenda-do-context-action ()
8243 "Show outline path and, maybe, follow mode window."
8244 (let ((m (org-get-at-bol 'org-marker)))
8245 (when (and (markerp m) (marker-buffer m))
8246 (and org-agenda-follow-mode
8247 (if org-agenda-follow-indirect
8248 (org-agenda-tree-to-indirect-buffer nil)
8249 (org-agenda-show)))
8250 (and org-agenda-show-outline-path
8251 (org-with-point-at m (org-display-outline-path t))))))
8253 (defun org-agenda-show-tags ()
8254 "Show the tags applicable to the current item."
8255 (interactive)
8256 (let* ((tags (org-get-at-bol 'tags)))
8257 (if tags
8258 (message "Tags are :%s:"
8259 (org-no-properties (mapconcat 'identity tags ":")))
8260 (message "No tags associated with this line"))))
8262 (defun org-agenda-goto (&optional highlight)
8263 "Go to the Org-mode file which contains the item at point."
8264 (interactive)
8265 (let* ((marker (or (org-get-at-bol 'org-marker)
8266 (org-agenda-error)))
8267 (buffer (marker-buffer marker))
8268 (pos (marker-position marker)))
8269 (switch-to-buffer-other-window buffer)
8270 (widen)
8271 (push-mark)
8272 (goto-char pos)
8273 (when (derived-mode-p 'org-mode)
8274 (org-show-context 'agenda)
8275 (save-excursion
8276 (and (outline-next-heading)
8277 (org-flag-heading nil)))) ; show the next heading
8278 (when (outline-invisible-p)
8279 (show-entry)) ; display invisible text
8280 (recenter (/ (window-height) 2))
8281 (run-hooks 'org-agenda-after-show-hook)
8282 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8284 (defvar org-agenda-after-show-hook nil
8285 "Normal hook run after an item has been shown from the agenda.
8286 Point is in the buffer where the item originated.")
8288 (defun org-agenda-kill ()
8289 "Kill the entry or subtree belonging to the current agenda entry."
8290 (interactive)
8291 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8292 (let* ((bufname-orig (buffer-name))
8293 (marker (or (org-get-at-bol 'org-marker)
8294 (org-agenda-error)))
8295 (buffer (marker-buffer marker))
8296 (pos (marker-position marker))
8297 (type (org-get-at-bol 'type))
8298 dbeg dend (n 0) conf)
8299 (org-with-remote-undo buffer
8300 (with-current-buffer buffer
8301 (save-excursion
8302 (goto-char pos)
8303 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8304 (setq dbeg (progn (org-back-to-heading t) (point))
8305 dend (org-end-of-subtree t t))
8306 (setq dbeg (point-at-bol)
8307 dend (min (point-max) (1+ (point-at-eol)))))
8308 (goto-char dbeg)
8309 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8310 (setq conf (or (eq t org-agenda-confirm-kill)
8311 (and (numberp org-agenda-confirm-kill)
8312 (> n org-agenda-confirm-kill))))
8313 (and conf
8314 (not (y-or-n-p
8315 (format "Delete entry with %d lines in buffer \"%s\"? "
8316 n (buffer-name buffer))))
8317 (error "Abort"))
8318 (let ((org-agenda-buffer-name bufname-orig))
8319 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8320 (with-current-buffer buffer (delete-region dbeg dend))
8321 (message "Agenda item and source killed"))))
8323 (defvar org-archive-default-command) ; defined in org-archive.el
8324 (defun org-agenda-archive-default ()
8325 "Archive the entry or subtree belonging to the current agenda entry."
8326 (interactive)
8327 (require 'org-archive)
8328 (org-agenda-archive-with org-archive-default-command))
8330 (defun org-agenda-archive-default-with-confirmation ()
8331 "Archive the entry or subtree belonging to the current agenda entry."
8332 (interactive)
8333 (require 'org-archive)
8334 (org-agenda-archive-with org-archive-default-command 'confirm))
8336 (defun org-agenda-archive ()
8337 "Archive the entry or subtree belonging to the current agenda entry."
8338 (interactive)
8339 (org-agenda-archive-with 'org-archive-subtree))
8341 (defun org-agenda-archive-to-archive-sibling ()
8342 "Move the entry to the archive sibling."
8343 (interactive)
8344 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8346 (defun org-agenda-archive-with (cmd &optional confirm)
8347 "Move the entry to the archive sibling."
8348 (interactive)
8349 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8350 (let* ((bufname-orig (buffer-name))
8351 (marker (or (org-get-at-bol 'org-marker)
8352 (org-agenda-error)))
8353 (buffer (marker-buffer marker))
8354 (pos (marker-position marker)))
8355 (org-with-remote-undo buffer
8356 (with-current-buffer buffer
8357 (if (derived-mode-p 'org-mode)
8358 (if (and confirm
8359 (not (y-or-n-p "Archive this subtree or entry? ")))
8360 (error "Abort")
8361 (save-excursion
8362 (goto-char pos)
8363 (let ((org-agenda-buffer-name bufname-orig))
8364 (org-remove-subtree-entries-from-agenda))
8365 (org-back-to-heading t)
8366 (funcall cmd)))
8367 (error "Archiving works only in Org-mode files"))))))
8369 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8370 "Remove all lines in the agenda that correspond to a given subtree.
8371 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8372 If this information is not given, the function uses the tree at point."
8373 (let ((buf (or buf (current-buffer))) m p)
8374 (save-excursion
8375 (unless (and beg end)
8376 (org-back-to-heading t)
8377 (setq beg (point))
8378 (org-end-of-subtree t)
8379 (setq end (point)))
8380 (set-buffer (get-buffer org-agenda-buffer-name))
8381 (save-excursion
8382 (goto-char (point-max))
8383 (beginning-of-line 1)
8384 (while (not (bobp))
8385 (when (and (setq m (org-get-at-bol 'org-marker))
8386 (equal buf (marker-buffer m))
8387 (setq p (marker-position m))
8388 (>= p beg)
8389 (< p end))
8390 (let ((inhibit-read-only t))
8391 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8392 (beginning-of-line 0))))))
8394 (defun org-agenda-refile (&optional goto rfloc no-update)
8395 "Refile the item at point.
8397 When GOTO is 0 or '(64), clear the refile cache.
8398 When GOTO is '(16), go to the location of the last refiled item.
8399 RFLOC can be a refile location obtained in a different way.
8400 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8401 (interactive "P")
8402 (cond
8403 ((member goto '(0 (64)))
8404 (org-refile-cache-clear))
8405 ((equal goto '(16))
8406 (org-refile-goto-last-stored))
8408 (let* ((buffer-orig (buffer-name))
8409 (marker (or (org-get-at-bol 'org-hd-marker)
8410 (org-agenda-error)))
8411 (buffer (marker-buffer marker))
8412 (pos (marker-position marker))
8413 (rfloc (or rfloc
8414 (org-refile-get-location
8415 (if goto "Goto" "Refile to") buffer
8416 org-refile-allow-creating-parent-nodes))))
8417 (with-current-buffer buffer
8418 (save-excursion
8419 (save-restriction
8420 (widen)
8421 (goto-char marker)
8422 (let ((org-agenda-buffer-name buffer-orig))
8423 (org-remove-subtree-entries-from-agenda))
8424 (org-refile goto buffer rfloc)))))
8425 (unless no-update (org-agenda-redo)))))
8427 (defun org-agenda-open-link (&optional arg)
8428 "Open the link(s) in the current entry, if any.
8429 This looks for a link in the displayed line in the agenda.
8430 It also looks at the text of the entry itself."
8431 (interactive "P")
8432 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8433 (org-get-at-bol 'org-marker)))
8434 (buffer (and marker (marker-buffer marker)))
8435 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8436 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8437 (lk0 (car lkall))
8438 (lk (if (stringp lk0) (list lk0) lk0))
8439 (lkend (cdr lkall))
8440 trg)
8441 (cond
8442 ((and buffer lk)
8443 (mapcar (lambda(l)
8444 (with-current-buffer buffer
8445 (setq trg (and (string-match org-bracket-link-regexp l)
8446 (match-string 1 l)))
8447 (if (or (not trg) (string-match org-any-link-re trg))
8448 (save-excursion
8449 (save-restriction
8450 (widen)
8451 (goto-char marker)
8452 (when (search-forward l nil lkend)
8453 (goto-char (match-beginning 0))
8454 (org-open-at-point))))
8455 ;; This is an internal link, widen the buffer
8456 (switch-to-buffer-other-window buffer)
8457 (widen)
8458 (goto-char marker)
8459 (when (search-forward l nil lkend)
8460 (goto-char (match-beginning 0))
8461 (org-open-at-point)))))
8462 lk))
8463 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8464 (save-excursion
8465 (beginning-of-line 1)
8466 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8467 (org-open-link-from-string (match-string 1)))
8468 (t (message "No link to open here")))))
8470 (defun org-agenda-copy-local-variable (var)
8471 "Get a variable from a referenced buffer and install it here."
8472 (let ((m (org-get-at-bol 'org-marker)))
8473 (when (and m (buffer-live-p (marker-buffer m)))
8474 (org-set-local var (with-current-buffer (marker-buffer m)
8475 (symbol-value var))))))
8477 (defun org-agenda-switch-to (&optional delete-other-windows)
8478 "Go to the Org-mode file which contains the item at point."
8479 (interactive)
8480 (if (and org-return-follows-link
8481 (not (org-get-at-bol 'org-marker))
8482 (org-in-regexp org-bracket-link-regexp))
8483 (org-open-link-from-string (match-string 0))
8484 (let* ((marker (or (org-get-at-bol 'org-marker)
8485 (org-agenda-error)))
8486 (buffer (marker-buffer marker))
8487 (pos (marker-position marker)))
8488 (org-pop-to-buffer-same-window buffer)
8489 (and delete-other-windows (delete-other-windows))
8490 (widen)
8491 (goto-char pos)
8492 (when (derived-mode-p 'org-mode)
8493 (org-show-context 'agenda)
8494 (save-excursion
8495 (and (outline-next-heading)
8496 (org-flag-heading nil))) ; show the next heading
8497 (when (outline-invisible-p)
8498 (show-entry)) ; display invisible text
8499 (run-hooks 'org-agenda-after-show-hook)))))
8501 (defun org-agenda-goto-mouse (ev)
8502 "Go to the Org-mode file which contains the item at the mouse click."
8503 (interactive "e")
8504 (mouse-set-point ev)
8505 (org-agenda-goto))
8507 (defun org-agenda-show (&optional full-entry)
8508 "Display the Org-mode file which contains the item at point.
8509 With prefix argument FULL-ENTRY, make the entire entry visible
8510 if it was hidden in the outline."
8511 (interactive "P")
8512 (let ((win (selected-window)))
8513 (if full-entry
8514 (let ((org-show-entry-below t))
8515 (org-agenda-goto t))
8516 (org-agenda-goto t))
8517 (select-window win)))
8519 (defvar org-agenda-show-window nil)
8520 (defun org-agenda-show-and-scroll-up (&optional arg)
8521 "Display the Org-mode file which contains the item at point.
8522 When called repeatedly, scroll the window that is displaying the buffer.
8523 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8524 `show-subtree' to display the item, so that drawers and logbooks stay
8525 folded."
8526 (interactive "P")
8527 (let ((win (selected-window)))
8528 (if (and (window-live-p org-agenda-show-window)
8529 (eq this-command last-command))
8530 (progn
8531 (select-window org-agenda-show-window)
8532 (ignore-errors (scroll-up)))
8533 (org-agenda-goto t)
8534 (if arg (org-show-entry) (show-subtree))
8535 (setq org-agenda-show-window (selected-window)))
8536 (select-window win)))
8538 (defun org-agenda-show-scroll-down ()
8539 "Scroll down the window showing the agenda."
8540 (interactive)
8541 (let ((win (selected-window)))
8542 (when (window-live-p org-agenda-show-window)
8543 (select-window org-agenda-show-window)
8544 (ignore-errors (scroll-down))
8545 (select-window win))))
8547 (defun org-agenda-show-1 (&optional more)
8548 "Display the Org-mode file which contains the item at point.
8549 The prefix arg selects the amount of information to display:
8551 0 hide the subtree
8552 1 just show the entry according to defaults.
8553 2 show the children view
8554 3 show the subtree view
8555 4 show the entire subtree and any LOGBOOK drawers
8556 5 show the entire subtree and any drawers
8557 With prefix argument FULL-ENTRY, make the entire entry visible
8558 if it was hidden in the outline."
8559 (interactive "p")
8560 (let ((win (selected-window)))
8561 (org-agenda-goto t)
8562 (org-recenter-heading 1)
8563 (cond
8564 ((= more 0)
8565 (hide-subtree)
8566 (save-excursion
8567 (org-back-to-heading)
8568 (run-hook-with-args 'org-cycle-hook 'folded))
8569 (message "Remote: FOLDED"))
8570 ((and (org-called-interactively-p 'any) (= more 1))
8571 (message "Remote: show with default settings"))
8572 ((= more 2)
8573 (show-entry)
8574 (show-children)
8575 (save-excursion
8576 (org-back-to-heading)
8577 (run-hook-with-args 'org-cycle-hook 'children))
8578 (message "Remote: CHILDREN"))
8579 ((= more 3)
8580 (show-subtree)
8581 (save-excursion
8582 (org-back-to-heading)
8583 (run-hook-with-args 'org-cycle-hook 'subtree))
8584 (message "Remote: SUBTREE"))
8585 ((= more 4)
8586 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8587 (org-drawer-regexp
8588 (concat "^[ \t]*:\\("
8589 (mapconcat 'regexp-quote org-drawers "\\|")
8590 "\\):[ \t]*$")))
8591 (show-subtree)
8592 (save-excursion
8593 (org-back-to-heading)
8594 (org-cycle-hide-drawers 'subtree)))
8595 (message "Remote: SUBTREE AND LOGBOOK"))
8596 ((> more 4)
8597 (show-subtree)
8598 (message "Remote: SUBTREE AND ALL DRAWERS")))
8599 (select-window win)))
8601 (defun org-recenter-heading (n)
8602 (save-excursion
8603 (org-back-to-heading)
8604 (recenter n)))
8606 (defvar org-agenda-cycle-counter nil)
8607 (defun org-agenda-cycle-show (&optional n)
8608 "Show the current entry in another window, with default settings.
8609 Default settings are taken from `org-show-hierarchy-above' and siblings.
8610 When use repeatedly in immediate succession, the remote entry will cycle
8611 through visibility
8613 children -> subtree -> folded
8615 When called with a numeric prefix arg, that arg will be passed through to
8616 `org-agenda-show-1'. For the interpretation of that argument, see the
8617 docstring of `org-agenda-show-1'."
8618 (interactive "P")
8619 (if (integerp n)
8620 (setq org-agenda-cycle-counter n)
8621 (if (not (eq last-command this-command))
8622 (setq org-agenda-cycle-counter 1)
8623 (if (equal org-agenda-cycle-counter 0)
8624 (setq org-agenda-cycle-counter 2)
8625 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8626 (if (> org-agenda-cycle-counter 3)
8627 (setq org-agenda-cycle-counter 0)))))
8628 (org-agenda-show-1 org-agenda-cycle-counter))
8630 (defun org-agenda-recenter (arg)
8631 "Display the Org-mode file which contains the item at point and recenter."
8632 (interactive "P")
8633 (let ((win (selected-window)))
8634 (org-agenda-goto t)
8635 (recenter arg)
8636 (select-window win)))
8638 (defun org-agenda-show-mouse (ev)
8639 "Display the Org-mode file which contains the item at the mouse click."
8640 (interactive "e")
8641 (mouse-set-point ev)
8642 (org-agenda-show))
8644 (defun org-agenda-check-no-diary ()
8645 "Check if the entry is a diary link and abort if yes."
8646 (if (org-get-at-bol 'org-agenda-diary-link)
8647 (org-agenda-error)))
8649 (defun org-agenda-error ()
8650 (error "Command not allowed in this line"))
8652 (defun org-agenda-tree-to-indirect-buffer (arg)
8653 "Show the subtree corresponding to the current entry in an indirect buffer.
8654 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8656 With a numerical prefix ARG, go up to this level and then take that tree.
8657 With a negative numeric ARG, go up by this number of levels.
8658 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8659 use the dedicated frame)."
8660 (interactive "P")
8661 (if current-prefix-arg
8662 (org-agenda-do-tree-to-indirect-buffer arg)
8663 (let ((agenda-buffer (buffer-name))
8664 (agenda-window (selected-window))
8665 (indirect-window
8666 (and org-last-indirect-buffer
8667 (get-buffer-window org-last-indirect-buffer))))
8668 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8669 (unless (or (eq org-indirect-buffer-display 'new-frame)
8670 (eq org-indirect-buffer-display 'dedicated-frame))
8671 (unwind-protect
8672 (unless (and indirect-window (window-live-p indirect-window))
8673 (setq indirect-window (split-window agenda-window)))
8674 (and indirect-window (select-window indirect-window))
8675 (switch-to-buffer org-last-indirect-buffer :norecord)
8676 (fit-window-to-buffer indirect-window)))
8677 (select-window (get-buffer-window agenda-buffer)))))
8679 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8680 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8681 (org-agenda-check-no-diary)
8682 (let* ((marker (or (org-get-at-bol 'org-marker)
8683 (org-agenda-error)))
8684 (buffer (marker-buffer marker))
8685 (pos (marker-position marker)))
8686 (with-current-buffer buffer
8687 (save-excursion
8688 (goto-char pos)
8689 (funcall 'org-tree-to-indirect-buffer arg)))))
8691 (defvar org-last-heading-marker (make-marker)
8692 "Marker pointing to the headline that last changed its TODO state
8693 by a remote command from the agenda.")
8695 (defun org-agenda-todo-nextset ()
8696 "Switch TODO entry to next sequence."
8697 (interactive)
8698 (org-agenda-todo 'nextset))
8700 (defun org-agenda-todo-previousset ()
8701 "Switch TODO entry to previous sequence."
8702 (interactive)
8703 (org-agenda-todo 'previousset))
8705 (defun org-agenda-todo (&optional arg)
8706 "Cycle TODO state of line at point, also in Org-mode file.
8707 This changes the line at point, all other lines in the agenda referring to
8708 the same tree node, and the headline of the tree node in the Org-mode file."
8709 (interactive "P")
8710 (org-agenda-check-no-diary)
8711 (let* ((col (current-column))
8712 (marker (or (org-get-at-bol 'org-marker)
8713 (org-agenda-error)))
8714 (buffer (marker-buffer marker))
8715 (pos (marker-position marker))
8716 (hdmarker (org-get-at-bol 'org-hd-marker))
8717 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8718 (inhibit-read-only t)
8719 org-agenda-headline-snapshot-before-repeat newhead just-one)
8720 (org-with-remote-undo buffer
8721 (with-current-buffer buffer
8722 (widen)
8723 (goto-char pos)
8724 (org-show-context 'agenda)
8725 (save-excursion
8726 (and (outline-next-heading)
8727 (org-flag-heading nil))) ; show the next heading
8728 (let ((current-prefix-arg arg))
8729 (call-interactively 'org-todo))
8730 (and (bolp) (forward-char 1))
8731 (setq newhead (org-get-heading))
8732 (when (and (org-bound-and-true-p
8733 org-agenda-headline-snapshot-before-repeat)
8734 (not (equal org-agenda-headline-snapshot-before-repeat
8735 newhead))
8736 todayp)
8737 (setq newhead org-agenda-headline-snapshot-before-repeat
8738 just-one t))
8739 (save-excursion
8740 (org-back-to-heading)
8741 (move-marker org-last-heading-marker (point))))
8742 (beginning-of-line 1)
8743 (save-excursion
8744 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8745 (org-move-to-column col))))
8747 (defun org-agenda-add-note (&optional arg)
8748 "Add a time-stamped note to the entry at point."
8749 (interactive "P")
8750 (org-agenda-check-no-diary)
8751 (let* ((marker (or (org-get-at-bol 'org-marker)
8752 (org-agenda-error)))
8753 (buffer (marker-buffer marker))
8754 (pos (marker-position marker))
8755 (hdmarker (org-get-at-bol 'org-hd-marker))
8756 (inhibit-read-only t))
8757 (with-current-buffer buffer
8758 (widen)
8759 (goto-char pos)
8760 (org-show-context 'agenda)
8761 (save-excursion
8762 (and (outline-next-heading)
8763 (org-flag-heading nil))) ; show the next heading
8764 (org-add-note))))
8766 (defun org-agenda-change-all-lines (newhead hdmarker
8767 &optional fixface just-this)
8768 "Change all lines in the agenda buffer which match HDMARKER.
8769 The new content of the line will be NEWHEAD (as modified by
8770 `org-agenda-format-item'). HDMARKER is checked with
8771 `equal' against all `org-hd-marker' text properties in the file.
8772 If FIXFACE is non-nil, the face of each item is modified according to
8773 the new TODO state.
8774 If JUST-THIS is non-nil, change just the current line, not all.
8775 If FORCE-TAGS is non nil, the car of it returns the new tags."
8776 (let* ((inhibit-read-only t)
8777 (line (org-current-line))
8778 (org-agenda-buffer (current-buffer))
8779 (thetags (with-current-buffer (marker-buffer hdmarker)
8780 (save-excursion (save-restriction (widen)
8781 (goto-char hdmarker)
8782 (org-get-tags-at)))))
8783 props m pl undone-face done-face finish new dotime level cat tags)
8784 (save-excursion
8785 (goto-char (point-max))
8786 (beginning-of-line 1)
8787 (while (not finish)
8788 (setq finish (bobp))
8789 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8790 (or (not just-this) (= (org-current-line) line))
8791 (equal m hdmarker))
8792 (setq props (text-properties-at (point))
8793 dotime (org-get-at-bol 'dotime)
8794 cat (org-get-at-bol 'org-category)
8795 level (org-get-at-bol 'level)
8796 tags thetags
8798 (let ((org-prefix-format-compiled
8799 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8800 org-prefix-format-compiled))
8801 (extra (org-get-at-bol 'extra)))
8802 (with-current-buffer (marker-buffer hdmarker)
8803 (save-excursion
8804 (save-restriction
8805 (widen)
8806 (org-agenda-format-item extra newhead level cat tags dotime)))))
8807 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8808 undone-face (org-get-at-bol 'undone-face)
8809 done-face (org-get-at-bol 'done-face))
8810 (beginning-of-line 1)
8811 (cond
8812 ((equal new "")
8813 (and (looking-at ".*\n?") (replace-match "")))
8814 ((looking-at ".*")
8815 (replace-match new t t)
8816 (beginning-of-line 1)
8817 (add-text-properties (point-at-bol) (point-at-eol) props)
8818 (when fixface
8819 (add-text-properties
8820 (point-at-bol) (point-at-eol)
8821 (list 'face
8822 (if org-last-todo-state-is-todo
8823 undone-face done-face))))
8824 (org-agenda-highlight-todo 'line)
8825 (beginning-of-line 1))
8826 (t (error "Line update did not work")))
8827 (save-restriction
8828 (narrow-to-region (point-at-bol) (point-at-eol))
8829 (org-agenda-finalize)))
8830 (beginning-of-line 0)))))
8832 (defun org-agenda-align-tags (&optional line)
8833 "Align all tags in agenda items to `org-agenda-tags-column'."
8834 (let ((inhibit-read-only t) l c)
8835 (save-excursion
8836 (goto-char (if line (point-at-bol) (point-min)))
8837 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8838 (if line (point-at-eol) nil) t)
8839 (add-text-properties
8840 (match-beginning 2) (match-end 2)
8841 (list 'face (delq nil (let ((prop (get-text-property
8842 (match-beginning 2) 'face)))
8843 (or (listp prop) (setq prop (list prop)))
8844 (if (memq 'org-tag prop)
8845 prop
8846 (cons 'org-tag prop))))))
8847 (setq l (- (match-end 2) (match-beginning 2))
8848 c (if (< org-agenda-tags-column 0)
8849 (- (abs org-agenda-tags-column) l)
8850 org-agenda-tags-column))
8851 (delete-region (match-beginning 1) (match-end 1))
8852 (goto-char (match-beginning 1))
8853 (insert (org-add-props
8854 (make-string (max 1 (- c (current-column))) ?\ )
8855 (plist-put (copy-sequence (text-properties-at (point)))
8856 'face nil))))
8857 (goto-char (point-min))
8858 (org-font-lock-add-tag-faces (point-max)))))
8860 (defun org-agenda-priority-up ()
8861 "Increase the priority of line at point, also in Org-mode file."
8862 (interactive)
8863 (org-agenda-priority 'up))
8865 (defun org-agenda-priority-down ()
8866 "Decrease the priority of line at point, also in Org-mode file."
8867 (interactive)
8868 (org-agenda-priority 'down))
8870 (defun org-agenda-priority (&optional force-direction)
8871 "Set the priority of line at point, also in Org-mode file.
8872 This changes the line at point, all other lines in the agenda referring to
8873 the same tree node, and the headline of the tree node in the Org-mode file.
8874 Called with a universal prefix arg, show the priority instead of setting it."
8875 (interactive "P")
8876 (if (equal force-direction '(4))
8877 (org-show-priority)
8878 (unless org-enable-priority-commands
8879 (error "Priority commands are disabled"))
8880 (org-agenda-check-no-diary)
8881 (let* ((marker (or (org-get-at-bol 'org-marker)
8882 (org-agenda-error)))
8883 (hdmarker (org-get-at-bol 'org-hd-marker))
8884 (buffer (marker-buffer hdmarker))
8885 (pos (marker-position hdmarker))
8886 (inhibit-read-only t)
8887 newhead)
8888 (org-with-remote-undo buffer
8889 (with-current-buffer buffer
8890 (widen)
8891 (goto-char pos)
8892 (org-show-context 'agenda)
8893 (save-excursion
8894 (and (outline-next-heading)
8895 (org-flag-heading nil))) ; show the next heading
8896 (funcall 'org-priority force-direction)
8897 (end-of-line 1)
8898 (setq newhead (org-get-heading)))
8899 (org-agenda-change-all-lines newhead hdmarker)
8900 (beginning-of-line 1)))))
8902 ;; FIXME: should fix the tags property of the agenda line.
8903 (defun org-agenda-set-tags (&optional tag onoff)
8904 "Set tags for the current headline."
8905 (interactive)
8906 (org-agenda-check-no-diary)
8907 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8908 (call-interactively 'org-change-tag-in-region)
8909 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8910 (org-agenda-error)))
8911 (buffer (marker-buffer hdmarker))
8912 (pos (marker-position hdmarker))
8913 (inhibit-read-only t)
8914 newhead)
8915 (org-with-remote-undo buffer
8916 (with-current-buffer buffer
8917 (widen)
8918 (goto-char pos)
8919 (save-excursion
8920 (org-show-context 'agenda))
8921 (save-excursion
8922 (and (outline-next-heading)
8923 (org-flag-heading nil))) ; show the next heading
8924 (goto-char pos)
8925 (if tag
8926 (org-toggle-tag tag onoff)
8927 (call-interactively 'org-set-tags))
8928 (end-of-line 1)
8929 (setq newhead (org-get-heading)))
8930 (org-agenda-change-all-lines newhead hdmarker)
8931 (beginning-of-line 1)))))
8933 (defun org-agenda-set-property ()
8934 "Set a property for the current headline."
8935 (interactive)
8936 (org-agenda-check-no-diary)
8937 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8938 (org-agenda-error)))
8939 (buffer (marker-buffer hdmarker))
8940 (pos (marker-position hdmarker))
8941 (inhibit-read-only t)
8942 newhead)
8943 (org-with-remote-undo buffer
8944 (with-current-buffer buffer
8945 (widen)
8946 (goto-char pos)
8947 (save-excursion
8948 (org-show-context 'agenda))
8949 (save-excursion
8950 (and (outline-next-heading)
8951 (org-flag-heading nil))) ; show the next heading
8952 (goto-char pos)
8953 (call-interactively 'org-set-property)))))
8955 (defun org-agenda-set-effort ()
8956 "Set the effort property for the current headline."
8957 (interactive)
8958 (org-agenda-check-no-diary)
8959 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8960 (org-agenda-error)))
8961 (buffer (marker-buffer hdmarker))
8962 (pos (marker-position hdmarker))
8963 (inhibit-read-only t)
8964 newhead)
8965 (org-with-remote-undo buffer
8966 (with-current-buffer buffer
8967 (widen)
8968 (goto-char pos)
8969 (save-excursion
8970 (org-show-context 'agenda))
8971 (save-excursion
8972 (and (outline-next-heading)
8973 (org-flag-heading nil))) ; show the next heading
8974 (goto-char pos)
8975 (call-interactively 'org-set-effort)
8976 (end-of-line 1)
8977 (setq newhead (org-get-heading)))
8978 (org-agenda-change-all-lines newhead hdmarker))))
8980 (defun org-agenda-toggle-archive-tag ()
8981 "Toggle the archive tag for the current entry."
8982 (interactive)
8983 (org-agenda-check-no-diary)
8984 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8985 (org-agenda-error)))
8986 (buffer (marker-buffer hdmarker))
8987 (pos (marker-position hdmarker))
8988 (inhibit-read-only t)
8989 newhead)
8990 (org-with-remote-undo buffer
8991 (with-current-buffer buffer
8992 (widen)
8993 (goto-char pos)
8994 (org-show-context 'agenda)
8995 (save-excursion
8996 (and (outline-next-heading)
8997 (org-flag-heading nil))) ; show the next heading
8998 (call-interactively 'org-toggle-archive-tag)
8999 (end-of-line 1)
9000 (setq newhead (org-get-heading)))
9001 (org-agenda-change-all-lines newhead hdmarker)
9002 (beginning-of-line 1))))
9004 (defun org-agenda-do-date-later (arg)
9005 (interactive "P")
9006 (cond
9007 ((or (equal arg '(16))
9008 (memq last-command
9009 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9010 (setq this-command 'org-agenda-date-later-minutes)
9011 (org-agenda-date-later-minutes 1))
9012 ((or (equal arg '(4))
9013 (memq last-command
9014 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9015 (setq this-command 'org-agenda-date-later-hours)
9016 (org-agenda-date-later-hours 1))
9018 (org-agenda-date-later (prefix-numeric-value arg)))))
9020 (defun org-agenda-do-date-earlier (arg)
9021 (interactive "P")
9022 (cond
9023 ((or (equal arg '(16))
9024 (memq last-command
9025 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9026 (setq this-command 'org-agenda-date-earlier-minutes)
9027 (org-agenda-date-earlier-minutes 1))
9028 ((or (equal arg '(4))
9029 (memq last-command
9030 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9031 (setq this-command 'org-agenda-date-earlier-hours)
9032 (org-agenda-date-earlier-hours 1))
9034 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9036 (defun org-agenda-date-later (arg &optional what)
9037 "Change the date of this item to ARG day(s) later."
9038 (interactive "p")
9039 (org-agenda-check-type t 'agenda 'timeline)
9040 (org-agenda-check-no-diary)
9041 (let* ((marker (or (org-get-at-bol 'org-marker)
9042 (org-agenda-error)))
9043 (buffer (marker-buffer marker))
9044 (pos (marker-position marker))
9045 cdate today)
9046 (org-with-remote-undo buffer
9047 (with-current-buffer buffer
9048 (widen)
9049 (goto-char pos)
9050 (if (not (org-at-timestamp-p))
9051 (error "Cannot find time stamp"))
9052 (when (and org-agenda-move-date-from-past-immediately-to-today
9053 (equal arg 1)
9054 (or (not what) (eq what 'day))
9055 (not (save-match-data (org-at-date-range-p))))
9056 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9057 cdate (calendar-absolute-from-gregorian
9058 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9059 today (org-today))
9060 (if (> today cdate)
9061 ;; immediately shift to today
9062 (setq arg (- today cdate))))
9063 (org-timestamp-change arg (or what 'day))
9064 (when (and (org-at-date-range-p)
9065 (re-search-backward org-tr-regexp-both (point-at-bol)))
9066 (let ((end org-last-changed-timestamp))
9067 (org-timestamp-change arg (or what 'day))
9068 (setq org-last-changed-timestamp
9069 (concat org-last-changed-timestamp "--" end)))))
9070 (org-agenda-show-new-time marker org-last-changed-timestamp))
9071 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9073 (defun org-agenda-date-earlier (arg &optional what)
9074 "Change the date of this item to ARG day(s) earlier."
9075 (interactive "p")
9076 (org-agenda-date-later (- arg) what))
9078 (defun org-agenda-date-later-minutes (arg)
9079 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9080 (interactive "p")
9081 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9082 (org-agenda-date-later arg 'minute))
9084 (defun org-agenda-date-earlier-minutes (arg)
9085 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9086 (interactive "p")
9087 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9088 (org-agenda-date-earlier arg 'minute))
9090 (defun org-agenda-date-later-hours (arg)
9091 "Change the time of this item, in hour steps."
9092 (interactive "p")
9093 (org-agenda-date-later arg 'hour))
9095 (defun org-agenda-date-earlier-hours (arg)
9096 "Change the time of this item, in hour steps."
9097 (interactive "p")
9098 (org-agenda-date-earlier arg 'hour))
9100 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9101 "Show new date stamp via text properties."
9102 ;; We use text properties to make this undoable
9103 (let ((inhibit-read-only t))
9104 (setq stamp (concat prefix " => " stamp " "))
9105 (save-excursion
9106 (goto-char (point-max))
9107 (while (not (bobp))
9108 (when (equal marker (org-get-at-bol 'org-marker))
9109 (org-move-to-column (- (window-width) (length stamp)) t)
9110 (org-agenda-fix-tags-filter-overlays-at (point))
9111 (if (featurep 'xemacs)
9112 ;; Use `duplicable' property to trigger undo recording
9113 (let ((ex (make-extent nil nil))
9114 (gl (make-glyph stamp)))
9115 (set-glyph-face gl 'secondary-selection)
9116 (set-extent-properties
9117 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9118 (insert-extent ex (1- (point)) (point-at-eol)))
9119 (add-text-properties
9120 (1- (point)) (point-at-eol)
9121 (list 'display (org-add-props stamp nil
9122 'face 'secondary-selection))))
9123 (beginning-of-line 1))
9124 (beginning-of-line 0)))))
9126 (defun org-agenda-date-prompt (arg)
9127 "Change the date of this item. Date is prompted for, with default today.
9128 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9129 be used to request time specification in the time stamp."
9130 (interactive "P")
9131 (org-agenda-check-type t 'agenda 'timeline)
9132 (org-agenda-check-no-diary)
9133 (let* ((marker (or (org-get-at-bol 'org-marker)
9134 (org-agenda-error)))
9135 (buffer (marker-buffer marker))
9136 (pos (marker-position marker)))
9137 (org-with-remote-undo buffer
9138 (with-current-buffer buffer
9139 (widen)
9140 (goto-char pos)
9141 (if (not (org-at-timestamp-p t))
9142 (error "Cannot find time stamp"))
9143 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9144 (org-agenda-show-new-time marker org-last-changed-timestamp))
9145 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9147 (defun org-agenda-schedule (arg &optional time)
9148 "Schedule the item at point.
9149 ARG is passed through to `org-schedule'."
9150 (interactive "P")
9151 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9152 (org-agenda-check-no-diary)
9153 (let* ((marker (or (org-get-at-bol 'org-marker)
9154 (org-agenda-error)))
9155 (type (marker-insertion-type marker))
9156 (buffer (marker-buffer marker))
9157 (pos (marker-position marker))
9158 (org-insert-labeled-timestamps-at-point nil)
9160 (set-marker-insertion-type marker t)
9161 (org-with-remote-undo buffer
9162 (with-current-buffer buffer
9163 (widen)
9164 (goto-char pos)
9165 (setq ts (org-schedule arg time)))
9166 (org-agenda-show-new-time marker ts " S"))
9167 (message "%s" ts)))
9169 (defun org-agenda-deadline (arg &optional time)
9170 "Schedule the item at point.
9171 ARG is passed through to `org-deadline'."
9172 (interactive "P")
9173 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9174 (org-agenda-check-no-diary)
9175 (let* ((marker (or (org-get-at-bol 'org-marker)
9176 (org-agenda-error)))
9177 (buffer (marker-buffer marker))
9178 (pos (marker-position marker))
9179 (org-insert-labeled-timestamps-at-point nil)
9181 (org-with-remote-undo buffer
9182 (with-current-buffer buffer
9183 (widen)
9184 (goto-char pos)
9185 (setq ts (org-deadline arg time)))
9186 (org-agenda-show-new-time marker ts " D"))
9187 (message "%s" ts)))
9189 (defun org-agenda-clock-in (&optional arg)
9190 "Start the clock on the currently selected item."
9191 (interactive "P")
9192 (org-agenda-check-no-diary)
9193 (if (equal arg '(4))
9194 (org-clock-in arg)
9195 (let* ((marker (or (org-get-at-bol 'org-marker)
9196 (org-agenda-error)))
9197 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9198 (pos (marker-position marker))
9199 (col (current-column))
9200 newhead)
9201 (org-with-remote-undo (marker-buffer marker)
9202 (with-current-buffer (marker-buffer marker)
9203 (widen)
9204 (goto-char pos)
9205 (org-show-context 'agenda)
9206 (org-show-entry)
9207 (org-cycle-hide-drawers 'children)
9208 (org-clock-in arg)
9209 (setq newhead (org-get-heading)))
9210 (org-agenda-change-all-lines newhead hdmarker))
9211 (org-move-to-column col))))
9213 (defun org-agenda-clock-out ()
9214 "Stop the currently running clock."
9215 (interactive)
9216 (unless (marker-buffer org-clock-marker)
9217 (error "No running clock"))
9218 (let ((marker (make-marker)) (col (current-column)) newhead)
9219 (org-with-remote-undo (marker-buffer org-clock-marker)
9220 (with-current-buffer (marker-buffer org-clock-marker)
9221 (save-excursion
9222 (save-restriction
9223 (widen)
9224 (goto-char org-clock-marker)
9225 (org-back-to-heading t)
9226 (move-marker marker (point))
9227 (org-clock-out)
9228 (setq newhead (org-get-heading))))))
9229 (org-agenda-change-all-lines newhead marker)
9230 (move-marker marker nil)
9231 (org-move-to-column col)
9232 (org-agenda-unmark-clocking-task)))
9234 (defun org-agenda-clock-cancel (&optional arg)
9235 "Cancel the currently running clock."
9236 (interactive "P")
9237 (unless (marker-buffer org-clock-marker)
9238 (error "No running clock"))
9239 (org-with-remote-undo (marker-buffer org-clock-marker)
9240 (org-clock-cancel)))
9242 (defun org-agenda-clock-goto ()
9243 "Jump to the currently clocked in task within the agenda.
9244 If the currently clocked in task is not listed in the agenda
9245 buffer, display it in another window."
9246 (interactive)
9247 (let (pos)
9248 (mapc (lambda (o)
9249 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9250 (setq pos (overlay-start o))))
9251 (overlays-in (point-min) (point-max)))
9252 (cond (pos (goto-char pos))
9253 ;; If the currently clocked entry is not in the agenda
9254 ;; buffer, we visit it in another window:
9255 (org-clock-current-task
9256 (org-switch-to-buffer-other-window (org-clock-goto)))
9257 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9259 (defun org-agenda-diary-entry-in-org-file ()
9260 "Make a diary entry in the file `org-agenda-diary-file'."
9261 (let (d1 d2 char (text "") dp1 dp2)
9262 (if (equal (buffer-name) "*Calendar*")
9263 (setq d1 (calendar-cursor-to-date t)
9264 d2 (car calendar-mark-ring))
9265 (setq dp1 (get-text-property (point-at-bol) 'day))
9266 (unless dp1 (error "No date defined in current line"))
9267 (setq d1 (calendar-gregorian-from-absolute dp1)
9268 d2 (and (ignore-errors (mark))
9269 (save-excursion
9270 (goto-char (mark))
9271 (setq dp2 (get-text-property (point-at-bol) 'day)))
9272 (calendar-gregorian-from-absolute dp2))))
9273 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9274 (setq char (read-char-exclusive))
9275 (cond
9276 ((equal char ?d)
9277 (setq text (read-string "Day entry: "))
9278 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9279 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9280 ((equal char ?a)
9281 (setq d1 (list (car d1) (nth 1 d1)
9282 (read-number (format "Reference year [%d]: " (nth 2 d1))
9283 (nth 2 d1))))
9284 (setq text (read-string "Anniversary (use %d to show years): "))
9285 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9286 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9287 ((equal char ?b)
9288 (setq text (read-string "Block entry: "))
9289 (unless (and d1 d2 (not (equal d1 d2)))
9290 (error "No block of days selected"))
9291 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9292 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9293 ((equal char ?j)
9294 (org-switch-to-buffer-other-window
9295 (find-file-noselect org-agenda-diary-file))
9296 (require 'org-datetree)
9297 (org-datetree-find-date-create d1)
9298 (org-reveal t))
9299 (t (error "Invalid selection character `%c'" char)))))
9301 (defcustom org-agenda-insert-diary-strategy 'date-tree
9302 "Where in `org-agenda-diary-file' should new entries be added?
9303 Valid values:
9305 date-tree in the date tree, as child of the date
9306 top-level as top-level entries at the end of the file."
9307 :group 'org-agenda
9308 :type '(choice
9309 (const :tag "in a date tree" date-tree)
9310 (const :tag "as top level at end of file" top-level)))
9312 (defcustom org-agenda-insert-diary-extract-time nil
9313 "Non-nil means extract any time specification from the diary entry."
9314 :group 'org-agenda
9315 :version "24.1"
9316 :type 'boolean)
9318 (defcustom org-agenda-bulk-mark-char ">"
9319 "A single-character string to be used as the bulk mark."
9320 :group 'org-agenda
9321 :version "24.1"
9322 :type 'string)
9324 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9325 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9326 If TEXT is not empty, it will become the headline of the new entry, and
9327 the resulting entry will not be shown. When TEXT is empty, switch to
9328 `org-agenda-diary-file' and let the user finish the entry there."
9329 (let ((cw (current-window-configuration)))
9330 (org-switch-to-buffer-other-window
9331 (find-file-noselect org-agenda-diary-file))
9332 (widen)
9333 (goto-char (point-min))
9334 (cond
9335 ((eq type 'anniversary)
9336 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9337 (progn
9338 (or (org-at-heading-p t)
9339 (progn
9340 (outline-next-heading)
9341 (insert "* Anniversaries\n\n")
9342 (beginning-of-line -1)))))
9343 (outline-next-heading)
9344 (org-back-over-empty-lines)
9345 (backward-char 1)
9346 (insert "\n")
9347 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9348 (nth 2 d1) (car d1) (nth 1 d1) text)))
9349 ((eq type 'day)
9350 (let ((org-prefix-has-time t)
9351 (org-agenda-time-leading-zero t)
9352 fmt time time2)
9353 (if org-agenda-insert-diary-extract-time
9354 ;; Use org-agenda-format-item to parse text for a time-range and
9355 ;; remove it. FIXME: This is a hack, we should refactor
9356 ;; that function to make time extraction available separately
9357 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9358 time (get-text-property 0 'time fmt)
9359 time2 (if (> (length time) 0)
9360 ;; split-string removes trailing ...... if
9361 ;; no end time given. First space
9362 ;; separates time from date.
9363 (concat " " (car (split-string time "\\.")))
9364 nil)
9365 text (get-text-property 0 'txt fmt)))
9366 (if (eq org-agenda-insert-diary-strategy 'top-level)
9367 (org-agenda-insert-diary-as-top-level text)
9368 (require 'org-datetree)
9369 (org-datetree-find-date-create d1)
9370 (org-agenda-insert-diary-make-new-entry text))
9371 (org-insert-time-stamp (org-time-from-absolute
9372 (calendar-absolute-from-gregorian d1))
9373 nil nil nil nil time2))
9374 (end-of-line 0))
9375 ((eq type 'block)
9376 (if (> (calendar-absolute-from-gregorian d1)
9377 (calendar-absolute-from-gregorian d2))
9378 (setq d1 (prog1 d2 (setq d2 d1))))
9379 (if (eq org-agenda-insert-diary-strategy 'top-level)
9380 (org-agenda-insert-diary-as-top-level text)
9381 (require 'org-datetree)
9382 (org-datetree-find-date-create d1)
9383 (org-agenda-insert-diary-make-new-entry text))
9384 (org-insert-time-stamp (org-time-from-absolute
9385 (calendar-absolute-from-gregorian d1)))
9386 (insert "--")
9387 (org-insert-time-stamp (org-time-from-absolute
9388 (calendar-absolute-from-gregorian d2)))
9389 (end-of-line 0)))
9390 (if (string-match "\\S-" text)
9391 (progn
9392 (set-window-configuration cw)
9393 (message "%s entry added to %s"
9394 (capitalize (symbol-name type))
9395 (abbreviate-file-name org-agenda-diary-file)))
9396 (org-reveal t)
9397 (message "Please finish entry here"))))
9399 (defun org-agenda-insert-diary-as-top-level (text)
9400 "Make new entry as a top-level entry at the end of the file.
9401 Add TEXT as headline, and position the cursor in the second line so that
9402 a timestamp can be added there."
9403 (widen)
9404 (goto-char (point-max))
9405 (or (bolp) (insert "\n"))
9406 (insert "* " text "\n")
9407 (if org-adapt-indentation (org-indent-to-column 2)))
9409 (defun org-agenda-insert-diary-make-new-entry (text)
9410 "Make new entry as last child of current entry.
9411 Add TEXT as headline, and position the cursor in the second line so that
9412 a timestamp can be added there."
9413 (let ((org-show-following-heading t)
9414 (org-show-siblings t)
9415 (org-show-hierarchy-above t)
9416 (org-show-entry-below t)
9417 col)
9418 (outline-next-heading)
9419 (org-back-over-empty-lines)
9420 (or (looking-at "[ \t]*$")
9421 (progn (insert "\n") (backward-char 1)))
9422 (org-insert-heading nil t)
9423 (org-do-demote)
9424 (setq col (current-column))
9425 (insert text "\n")
9426 (if org-adapt-indentation (org-indent-to-column col))
9427 (let ((org-show-following-heading t)
9428 (org-show-siblings t)
9429 (org-show-hierarchy-above t)
9430 (org-show-entry-below t))
9431 (org-show-context))))
9433 (defun org-agenda-diary-entry ()
9434 "Make a diary entry, like the `i' command from the calendar.
9435 All the standard commands work: block, weekly etc.
9436 When `org-agenda-diary-file' points to a file,
9437 `org-agenda-diary-entry-in-org-file' is called instead to create
9438 entries in that Org-mode file."
9439 (interactive)
9440 (if (not (eq org-agenda-diary-file 'diary-file))
9441 (org-agenda-diary-entry-in-org-file)
9442 (require 'diary-lib)
9443 (let* ((char (progn
9444 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9445 (read-char-exclusive)))
9446 (cmd (cdr (assoc char
9447 '((?d . insert-diary-entry)
9448 (?w . insert-weekly-diary-entry)
9449 (?m . insert-monthly-diary-entry)
9450 (?y . insert-yearly-diary-entry)
9451 (?a . insert-anniversary-diary-entry)
9452 (?b . insert-block-diary-entry)
9453 (?c . insert-cyclic-diary-entry)))))
9454 (oldf (symbol-function 'calendar-cursor-to-date))
9455 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9456 (point (point))
9457 (mark (or (mark t) (point))))
9458 (unless cmd
9459 (error "No command associated with <%c>" char))
9460 (unless (and (get-text-property point 'day)
9461 (or (not (equal ?b char))
9462 (get-text-property mark 'day)))
9463 (error "Don't know which date to use for diary entry"))
9464 ;; We implement this by hacking the `calendar-cursor-to-date' function
9465 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9466 (let ((calendar-mark-ring
9467 (list (calendar-gregorian-from-absolute
9468 (or (get-text-property mark 'day)
9469 (get-text-property point 'day))))))
9470 (unwind-protect
9471 (progn
9472 (fset 'calendar-cursor-to-date
9473 (lambda (&optional error dummy)
9474 (calendar-gregorian-from-absolute
9475 (get-text-property point 'day))))
9476 (call-interactively cmd))
9477 (fset 'calendar-cursor-to-date oldf))))))
9479 (defun org-agenda-execute-calendar-command (cmd)
9480 "Execute a calendar command from the agenda with date from cursor."
9481 (org-agenda-check-type t 'agenda 'timeline)
9482 (require 'diary-lib)
9483 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9484 (error "Don't know which date to use for the calendar command"))
9485 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9486 (point (point))
9487 (date (calendar-gregorian-from-absolute
9488 (get-text-property point 'day)))
9489 ;; the following 2 vars are needed in the calendar
9490 (displayed-month (car date))
9491 (displayed-year (nth 2 date)))
9492 (unwind-protect
9493 (progn
9494 (fset 'calendar-cursor-to-date
9495 (lambda (&optional error dummy)
9496 (calendar-gregorian-from-absolute
9497 (get-text-property point 'day))))
9498 (call-interactively cmd))
9499 (fset 'calendar-cursor-to-date oldf))))
9501 (defun org-agenda-phases-of-moon ()
9502 "Display the phases of the moon for the 3 months around the cursor date."
9503 (interactive)
9504 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9506 (defun org-agenda-holidays ()
9507 "Display the holidays for the 3 months around the cursor date."
9508 (interactive)
9509 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9511 (defvar calendar-longitude) ; defined in calendar.el
9512 (defvar calendar-latitude) ; defined in calendar.el
9513 (defvar calendar-location-name) ; defined in calendar.el
9515 (defun org-agenda-sunrise-sunset (arg)
9516 "Display sunrise and sunset for the cursor date.
9517 Latitude and longitude can be specified with the variables
9518 `calendar-latitude' and `calendar-longitude'. When called with prefix
9519 argument, latitude and longitude will be prompted for."
9520 (interactive "P")
9521 (require 'solar)
9522 (let ((calendar-longitude (if arg nil calendar-longitude))
9523 (calendar-latitude (if arg nil calendar-latitude))
9524 (calendar-location-name
9525 (if arg "the given coordinates" calendar-location-name)))
9526 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9528 (defun org-agenda-goto-calendar ()
9529 "Open the Emacs calendar with the date at the cursor."
9530 (interactive)
9531 (org-agenda-check-type t 'agenda 'timeline)
9532 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9533 (error "Don't know which date to open in calendar")))
9534 (date (calendar-gregorian-from-absolute day))
9535 (calendar-move-hook nil)
9536 (calendar-view-holidays-initially-flag nil)
9537 (calendar-view-diary-initially-flag nil))
9538 (calendar)
9539 (calendar-goto-date date)))
9541 ;;;###autoload
9542 (defun org-calendar-goto-agenda ()
9543 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9544 This is a command that has to be installed in `calendar-mode-map'."
9545 (interactive)
9546 (org-agenda-list nil (calendar-absolute-from-gregorian
9547 (calendar-cursor-to-date))
9548 nil))
9550 (defun org-agenda-convert-date ()
9551 (interactive)
9552 (org-agenda-check-type t 'agenda 'timeline)
9553 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9554 date s)
9555 (unless day
9556 (error "Don't know which date to convert"))
9557 (setq date (calendar-gregorian-from-absolute day))
9558 (setq s (concat
9559 "Gregorian: " (calendar-date-string date) "\n"
9560 "ISO: " (calendar-iso-date-string date) "\n"
9561 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9562 "Julian: " (calendar-julian-date-string date) "\n"
9563 "Astron. JD: " (calendar-astro-date-string date)
9564 " (Julian date number at noon UTC)\n"
9565 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9566 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9567 "French: " (calendar-french-date-string date) "\n"
9568 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9569 "Mayan: " (calendar-mayan-date-string date) "\n"
9570 "Coptic: " (calendar-coptic-date-string date) "\n"
9571 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9572 "Persian: " (calendar-persian-date-string date) "\n"
9573 "Chinese: " (calendar-chinese-date-string date) "\n"))
9574 (with-output-to-temp-buffer "*Dates*"
9575 (princ s))
9576 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9578 ;;; Bulk commands
9580 (defun org-agenda-bulk-marked-p ()
9581 (eq (get-char-property (point-at-bol) 'type)
9582 'org-marked-entry-overlay))
9584 (defun org-agenda-bulk-mark (&optional arg)
9585 "Mark the entry at point for future bulk action."
9586 (interactive "p")
9587 (dotimes (i (or arg 1))
9588 (unless (org-get-at-bol 'org-agenda-diary-link)
9589 (let* ((m (org-get-at-bol 'org-hd-marker))
9591 (unless (org-agenda-bulk-marked-p)
9592 (unless m (error "Nothing to mark at point"))
9593 (push m org-agenda-bulk-marked-entries)
9594 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9595 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9596 (org-get-todo-face "TODO")
9597 'evaporate)
9598 (overlay-put ov 'type 'org-marked-entry-overlay))
9599 (beginning-of-line 2)
9600 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9601 (beginning-of-line 2))
9602 (message "%d entries marked for bulk action"
9603 (length org-agenda-bulk-marked-entries))))))
9605 (defun org-agenda-bulk-mark-all ()
9606 "Mark all entries for future agenda bulk action."
9607 (interactive)
9608 (org-agenda-bulk-mark-regexp "."))
9610 (defun org-agenda-bulk-mark-regexp (regexp)
9611 "Mark entries matching REGEXP for future agenda bulk action."
9612 (interactive "sMark entries matching regexp: ")
9613 (let ((entries-marked 0))
9614 (save-excursion
9615 (goto-char (point-min))
9616 (goto-char (next-single-property-change (point) 'txt))
9617 (while (re-search-forward regexp nil t)
9618 (when (string-match regexp (get-text-property (point) 'txt))
9619 (setq entries-marked (1+ entries-marked))
9620 (call-interactively 'org-agenda-bulk-mark))))
9621 (if (not entries-marked)
9622 (message "No entry matching this regexp."))))
9624 (defun org-agenda-bulk-unmark (&optional arg)
9625 "Unmark the entry at point for future bulk action."
9626 (interactive "P")
9627 (if arg
9628 (org-agenda-bulk-unmark-all)
9629 (cond ((org-agenda-bulk-marked-p)
9630 (org-agenda-bulk-remove-overlays
9631 (point-at-bol) (+ 2 (point-at-bol)))
9632 (setq org-agenda-bulk-marked-entries
9633 (delete (org-get-at-bol 'org-hd-marker)
9634 org-agenda-bulk-marked-entries))
9635 (beginning-of-line 2)
9636 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9637 (beginning-of-line 2))
9638 (message "%d entries left marked for bulk action"
9639 (length org-agenda-bulk-marked-entries)))
9640 (t (message "No entry to unmark here")))))
9642 (defun org-agenda-bulk-toggle ()
9643 "Toggle marking the entry at point for bulk action."
9644 (interactive)
9645 (if (org-agenda-bulk-marked-p)
9646 (org-agenda-bulk-unmark)
9647 (org-agenda-bulk-mark)))
9649 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9650 "Remove the mark overlays between BEG and END in the agenda buffer.
9651 BEG and END default to the buffer limits.
9653 This only removes the overlays, it does not remove the markers
9654 from the list in `org-agenda-bulk-marked-entries'."
9655 (interactive)
9656 (mapc (lambda (ov)
9657 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9658 (delete-overlay ov)))
9659 (overlays-in (or beg (point-min)) (or end (point-max)))))
9661 (defun org-agenda-bulk-unmark-all ()
9662 "Remove all marks in the agenda buffer.
9663 This will remove the markers and the overlays."
9664 (interactive)
9665 (if (null org-agenda-bulk-marked-entries)
9666 (message "No entry to unmark")
9667 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9668 (setq org-agenda-bulk-marked-entries nil)
9669 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9671 (defcustom org-agenda-persistent-marks nil
9672 "Non-nil means marked items will stay marked after a bulk action.
9673 You can toggle this interactively by typing `p' when prompted for a
9674 bulk action."
9675 :group 'org-agenda
9676 :version "24.1"
9677 :type 'boolean)
9679 (defun org-agenda-bulk-action (&optional arg)
9680 "Execute an remote-editing action on all marked entries.
9681 The prefix arg is passed through to the command if possible."
9682 (interactive "P")
9683 ;; Make sure we have markers, and only valid ones
9684 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
9685 (mapc
9686 (lambda (m)
9687 (unless (and (markerp m)
9688 (marker-buffer m)
9689 (buffer-live-p (marker-buffer m))
9690 (marker-position m))
9691 (error "Marker %s for bulk command is invalid" m)))
9692 org-agenda-bulk-marked-entries)
9694 ;; Prompt for the bulk command
9695 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9696 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9697 "[S]catter [f]unction "
9698 (when org-agenda-bulk-custom-functions
9699 (concat " Custom: ["
9700 (mapconcat (lambda(f) (char-to-string (car f)))
9701 org-agenda-bulk-custom-functions "")
9702 "]"))))
9703 (catch 'exit
9704 (let* ((action (read-char-exclusive))
9705 (org-log-refile (if org-log-refile 'time nil))
9706 (entries (reverse org-agenda-bulk-marked-entries))
9707 (org-overriding-default-time
9708 (if (get-text-property (point) 'org-agenda-date-header)
9709 (org-get-cursor-date)))
9710 redo-at-end
9711 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9712 (cond
9713 ((equal action ?p)
9714 (let ((org-agenda-persistent-marks
9715 (not org-agenda-persistent-marks)))
9716 (org-agenda-bulk-action)
9717 (throw 'exit nil)))
9719 ((equal action ?$)
9720 (setq cmd '(org-agenda-archive)))
9722 ((equal action ?A)
9723 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9725 ((member action '(?r ?w))
9726 (setq rfloc (org-refile-get-location
9727 "Refile to"
9728 (marker-buffer (car entries))
9729 org-refile-allow-creating-parent-nodes))
9730 (if (nth 3 rfloc)
9731 (setcar (nthcdr 3 rfloc)
9732 (move-marker (make-marker) (nth 3 rfloc)
9733 (or (get-file-buffer (nth 1 rfloc))
9734 (find-buffer-visiting (nth 1 rfloc))
9735 (error "This should not happen")))))
9737 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9738 redo-at-end t))
9740 ((equal action ?t)
9741 (setq state (org-icompleting-read
9742 "Todo state: "
9743 (with-current-buffer (marker-buffer (car entries))
9744 (mapcar 'list org-todo-keywords-1))))
9745 (setq cmd `(let ((org-inhibit-blocking t)
9746 (org-inhibit-logging 'note))
9747 (org-agenda-todo ,state))))
9749 ((memq action '(?- ?+))
9750 (setq tag (org-icompleting-read
9751 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9752 (with-current-buffer (marker-buffer (car entries))
9753 (delq nil
9754 (mapcar (lambda (x)
9755 (if (stringp (car x)) x)) org-tag-alist)))))
9756 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9758 ((memq action '(?s ?d))
9759 (let* ((time
9760 (unless arg
9761 (org-read-date
9762 nil nil nil
9763 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9764 org-overriding-default-time)))
9765 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9766 (setq cmd `(eval '(,c1 arg ,time)))))
9768 ((equal action ?S)
9769 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9770 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9771 (let ((days (read-number
9772 (format "Scatter tasks across how many %sdays: "
9773 (if arg "week" "")) 7)))
9774 (setq cmd
9775 `(let ((distance (1+ (random ,days))))
9776 (if arg
9777 (let ((dist distance)
9778 (day-of-week
9779 (calendar-day-of-week
9780 (calendar-gregorian-from-absolute (org-today)))))
9781 (dotimes (i (1+ dist))
9782 (while (member day-of-week org-agenda-weekend-days)
9783 (incf distance)
9784 (incf day-of-week)
9785 (if (= day-of-week 7)
9786 (setq day-of-week 0)))
9787 (incf day-of-week)
9788 (if (= day-of-week 7)
9789 (setq day-of-week 0)))))
9790 ;; silently fail when try to replan a sexp entry
9791 (condition-case nil
9792 (let* ((date (calendar-gregorian-from-absolute
9793 (+ (org-today) distance)))
9794 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9795 (nth 2 date))))
9796 (org-agenda-schedule nil time))
9797 (error nil)))))))
9799 ((assoc action org-agenda-bulk-custom-functions)
9800 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9801 redo-at-end t))
9803 ((equal action ?f)
9804 (setq cmd (list (intern
9805 (org-icompleting-read "Function: "
9806 obarray 'fboundp t nil nil)))))
9808 (t (error "Invalid bulk action")))
9810 ;; Sort the markers, to make sure that parents are handled before children
9811 (setq entries (sort entries
9812 (lambda (a b)
9813 (cond
9814 ((equal (marker-buffer a) (marker-buffer b))
9815 (< (marker-position a) (marker-position b)))
9817 (string< (buffer-name (marker-buffer a))
9818 (buffer-name (marker-buffer b))))))))
9820 ;; Now loop over all markers and apply cmd
9821 (while (setq e (pop entries))
9822 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9823 (if (not pos)
9824 (progn (message "Skipping removed entry at %s" e)
9825 (setq cntskip (1+ cntskip)))
9826 (goto-char pos)
9827 (let (org-loop-over-headlines-in-active-region)
9828 (eval cmd))
9829 (setq cnt (1+ cnt))))
9830 (when redo-at-end (org-agenda-redo))
9831 (unless org-agenda-persistent-marks
9832 (org-agenda-bulk-unmark-all))
9833 (message "Acted on %d entries%s%s"
9835 (if (= cntskip 0)
9837 (format ", skipped %d (disappeared before their turn)"
9838 cntskip))
9839 (if (not org-agenda-persistent-marks)
9840 "" " (kept marked)"))))))
9842 (defun org-agenda-capture (&optional with-time)
9843 "Call `org-capture' with the date at point.
9844 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9845 current HH:MM time."
9846 (interactive "P")
9847 (if (not (eq major-mode 'org-agenda-mode))
9848 (user-error "You cannot do this outside of agenda buffers")
9849 (let ((org-overriding-default-time
9850 (org-get-cursor-date (equal with-time 1))))
9851 (call-interactively 'org-capture))))
9853 ;;; Flagging notes
9855 (defun org-agenda-show-the-flagging-note ()
9856 "Display the flagging note in the other window.
9857 When called a second time in direct sequence, offer to remove the FLAGGING
9858 tag and (if present) the flagging note."
9859 (interactive)
9860 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9861 (win (selected-window))
9862 note heading newhead)
9863 (unless hdmarker
9864 (error "No linked entry at point"))
9865 (if (and (eq this-command last-command)
9866 (y-or-n-p "Unflag and remove any flagging note? "))
9867 (progn
9868 (org-agenda-remove-flag hdmarker)
9869 (let ((win (get-buffer-window "*Flagging Note*")))
9870 (and win (delete-window win)))
9871 (message "Entry unflagged"))
9872 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9873 (unless note
9874 (error "No flagging note"))
9875 (org-kill-new note)
9876 (org-switch-to-buffer-other-window "*Flagging Note*")
9877 (erase-buffer)
9878 (insert note)
9879 (goto-char (point-min))
9880 (while (re-search-forward "\\\\n" nil t)
9881 (replace-match "\n" t t))
9882 (goto-char (point-min))
9883 (select-window win)
9884 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9886 (defun org-agenda-remove-flag (marker)
9887 "Remove the FLAGGED tag and any flagging note in the entry."
9888 (let (newhead)
9889 (org-with-point-at marker
9890 (org-toggle-tag "FLAGGED" 'off)
9891 (org-entry-delete nil "THEFLAGGINGNOTE")
9892 (setq newhead (org-get-heading)))
9893 (org-agenda-change-all-lines newhead marker)
9894 (message "Entry unflagged")))
9896 (defun org-agenda-get-any-marker (&optional pos)
9897 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9898 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9900 ;;; Appointment reminders
9902 (defvar appt-time-msg-list) ; defined in appt.el
9904 ;;;###autoload
9905 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9906 "Activate appointments found in `org-agenda-files'.
9907 With a \\[universal-argument] prefix, refresh the list of
9908 appointments.
9910 If FILTER is t, interactively prompt the user for a regular
9911 expression, and filter out entries that don't match it.
9913 If FILTER is a string, use this string as a regular expression
9914 for filtering entries out.
9916 If FILTER is a function, filter out entries against which
9917 calling the function returns nil. This function takes one
9918 argument: an entry from `org-agenda-get-day-entries'.
9920 FILTER can also be an alist with the car of each cell being
9921 either 'headline or 'category. For example:
9923 '((headline \"IMPORTANT\")
9924 (category \"Work\"))
9926 will only add headlines containing IMPORTANT or headlines
9927 belonging to the \"Work\" category.
9929 ARGS are symbols indicating what kind of entries to consider.
9930 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9931 \(i.e., deadlines and scheduled items with a hh:mm specification)
9932 and :timestamp entries. See the docstring of `org-diary' for
9933 details and examples.
9935 If an entry has a APPT_WARNTIME property, its value will be used
9936 to override `appt-message-warning-time'."
9937 (interactive "P")
9938 (if refresh (setq appt-time-msg-list nil))
9939 (if (eq filter t)
9940 (setq filter (read-from-minibuffer "Regexp filter: ")))
9941 (let* ((cnt 0) ; count added events
9942 (scope (or args '(:deadline* :scheduled* :timestamp)))
9943 (org-agenda-new-buffers nil)
9944 (org-deadline-warning-days 0)
9945 ;; Do not use `org-today' here because appt only takes
9946 ;; time and without date as argument, so it may pass wrong
9947 ;; information otherwise
9948 (today (org-date-to-gregorian
9949 (time-to-days (current-time))))
9950 (org-agenda-restrict nil)
9951 (files (org-agenda-files 'unrestricted)) entries file
9952 (org-agenda-buffer nil))
9953 ;; Get all entries which may contain an appt
9954 (org-agenda-prepare-buffers files)
9955 (while (setq file (pop files))
9956 (setq entries
9957 (delq nil
9958 (append entries
9959 (apply 'org-agenda-get-day-entries
9960 file today scope)))))
9961 ;; Map thru entries and find if we should filter them out
9962 (mapc
9963 (lambda(x)
9964 (let* ((evt (org-trim
9965 (replace-regexp-in-string
9966 org-bracket-link-regexp "\\3"
9967 (or (get-text-property 1 'txt x) ""))))
9968 (cat (get-text-property 1 'org-category x))
9969 (tod (get-text-property 1 'time-of-day x))
9970 (ok (or (null filter)
9971 (and (stringp filter) (string-match filter evt))
9972 (and (functionp filter) (funcall filter x))
9973 (and (listp filter)
9974 (let ((cat-filter (cadr (assoc 'category filter)))
9975 (evt-filter (cadr (assoc 'headline filter))))
9976 (or (and (stringp cat-filter)
9977 (string-match cat-filter cat))
9978 (and (stringp evt-filter)
9979 (string-match evt-filter evt)))))))
9980 (wrn (get-text-property 1 'warntime x)))
9981 ;; FIXME: Shall we remove text-properties for the appt text?
9982 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
9983 (when (and ok tod)
9984 (setq tod (concat "00" (number-to-string tod))
9985 tod (when (string-match
9986 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
9987 (concat (match-string 1 tod) ":"
9988 (match-string 2 tod))))
9989 (if (version< emacs-version "23.3")
9990 (appt-add tod evt)
9991 (appt-add tod evt wrn))
9992 (setq cnt (1+ cnt))))) entries)
9993 (org-release-buffers org-agenda-new-buffers)
9994 (if (eq cnt 0)
9995 (message "No event to add")
9996 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
9998 (defun org-agenda-todayp (date)
9999 "Does DATE mean today, when considering `org-extend-today-until'?"
10000 (let ((today (org-today))
10001 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10002 date)))
10003 (eq date today)))
10005 (defun org-agenda-todo-yesterday (&optional arg)
10006 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10007 (interactive "P")
10008 (let* ((hour (third (decode-time
10009 (org-current-time))))
10010 (org-extend-today-until (1+ hour)))
10011 (org-agenda-todo arg)))
10013 (provide 'org-agenda)
10015 ;;; org-agenda.el ends here