Merge branch 'maint'
[org-mode.git] / lisp / org-agenda.el
blobc48da911c135f3e727f4c9fdf2bbac58934b7277
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 '(choice
231 (const nil)
232 (string)))
234 (defcustom org-agenda-persistent-filter nil
235 "When set, keep filters from one agenda view to the next."
236 :group 'org-agenda
237 :type 'boolean)
239 (defgroup org-agenda-custom-commands nil
240 "Options concerning agenda views in Org-mode."
241 :tag "Org Agenda Custom Commands"
242 :group 'org-agenda)
244 (defconst org-sorting-choice
245 '(choice
246 (const time-up) (const time-down)
247 (const timestamp-up) (const timestamp-down)
248 (const scheduled-up) (const scheduled-down)
249 (const deadline-up) (const deadline-down)
250 (const ts-up) (const ts-down)
251 (const tsia-up) (const tsia-down)
252 (const category-keep) (const category-up) (const category-down)
253 (const tag-down) (const tag-up)
254 (const priority-up) (const priority-down)
255 (const todo-state-up) (const todo-state-down)
256 (const effort-up) (const effort-down)
257 (const habit-up) (const habit-down)
258 (const alpha-up) (const alpha-down)
259 (const user-defined-up) (const user-defined-down))
260 "Sorting choices.")
262 ;; Keep custom values for `org-agenda-filter-preset' compatible with
263 ;; the new variable `org-agenda-tag-filter-preset'.
264 (org-defvaralias 'org-agenda-filter-preset 'org-agenda-tag-filter-preset)
265 (org-defvaralias 'org-agenda-filter 'org-agenda-tag-filter)
267 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
268 "List of types searched for when creating the daily/weekly agenda.
269 This variable is a list of symbols that controls the types of
270 items that appear in the daily/weekly agenda. Allowed symbols in this
271 list are are
273 :timestamp List items containing a date stamp or date range matching
274 the selected date. This includes sexp entries in angular
275 brackets.
277 :sexp List entries resulting from plain diary-like sexps.
279 :deadline List deadline due on that date. When the date is today,
280 also list any deadlines past due, or due within
281 `org-deadline-warning-days'. `:deadline' must appear before
282 `:scheduled' if the setting of
283 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
284 any effect.
286 :deadline* Same as above, but only include the deadline if it has an
287 hour specification as [h]h:mm.
289 :scheduled List all items which are scheduled for the given date.
290 The diary for *today* also contains items which were
291 scheduled earlier and are not yet marked DONE.
293 :scheduled* Same as above, but only include the scheduled item if it
294 has an hour specification as [h]h:mm.
296 By default, all four non-starred types are turned on.
298 When :scheduled* or :deadline* are included, :schedule or :deadline
299 will be ignored.
301 Never set this variable globally using `setq', because then it
302 will apply to all future agenda commands. Instead, bind it with
303 `let' to scope it dynamically into the agenda-constructing
304 command. A good way to set it is through options in
305 `org-agenda-custom-commands'. For a more flexible (though
306 somewhat less efficient) way of determining what is included in
307 the daily/weekly agenda, see `org-agenda-skip-function'.")
309 (defconst org-agenda-custom-commands-local-options
310 `(repeat :tag "Local settings for this command. Remember to quote values"
311 (choice :tag "Setting"
312 (list :tag "Heading for this block"
313 (const org-agenda-overriding-header)
314 (string :tag "Headline"))
315 (list :tag "Files to be searched"
316 (const org-agenda-files)
317 (list
318 (const :format "" quote)
319 (repeat (file))))
320 (list :tag "Sorting strategy"
321 (const org-agenda-sorting-strategy)
322 (list
323 (const :format "" quote)
324 (repeat
325 ,org-sorting-choice)))
326 (list :tag "Prefix format"
327 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
328 (string))
329 (list :tag "Number of days in agenda"
330 (const org-agenda-span)
331 (choice (const :tag "Day" 'day)
332 (const :tag "Week" 'week)
333 (const :tag "Month" 'month)
334 (const :tag "Year" 'year)
335 (integer :tag "Custom")))
336 (list :tag "Fixed starting date"
337 (const org-agenda-start-day)
338 (string :value "2007-11-01"))
339 (list :tag "Start on day of week"
340 (const org-agenda-start-on-weekday)
341 (choice :value 1
342 (const :tag "Today" nil)
343 (integer :tag "Weekday No.")))
344 (list :tag "Include data from diary"
345 (const org-agenda-include-diary)
346 (boolean))
347 (list :tag "Deadline Warning days"
348 (const org-deadline-warning-days)
349 (integer :value 1))
350 (list :tag "Category filter preset"
351 (const org-agenda-category-filter-preset)
352 (list
353 (const :format "" quote)
354 (repeat
355 (string :tag "+category or -category"))))
356 (list :tag "Tags filter preset"
357 (const org-agenda-tag-filter-preset)
358 (list
359 (const :format "" quote)
360 (repeat
361 (string :tag "+tag or -tag"))))
362 (list :tag "Regexp filter preset"
363 (const org-agenda-regexp-filter-preset)
364 (list
365 (const :format "" quote)
366 (repeat
367 (string :tag "+regexp or -regexp"))))
368 (list :tag "Set daily/weekly entry types"
369 (const org-agenda-entry-types)
370 (list
371 (const :format "" quote)
372 (set :greedy t :value ,org-agenda-entry-types
373 (const :deadline)
374 (const :scheduled)
375 (const :deadline*)
376 (const :scheduled*)
377 (const :timestamp)
378 (const :sexp))))
379 (list :tag "Standard skipping condition"
380 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
381 (const org-agenda-skip-function)
382 (list
383 (const :format "" quote)
384 (list
385 (choice
386 :tag "Skipping range"
387 (const :tag "Skip entry" org-agenda-skip-entry-if)
388 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
389 (repeat :inline t :tag "Conditions for skipping"
390 (choice
391 :tag "Condition type"
392 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
393 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
394 (list :tag "TODO state is" :inline t
395 (const 'todo)
396 (choice
397 (const :tag "any not-done state" 'todo)
398 (const :tag "any done state" 'done)
399 (const :tag "any state" 'any)
400 (list :tag "Keyword list"
401 (const :format "" quote)
402 (repeat (string :tag "Keyword")))))
403 (list :tag "TODO state is not" :inline t
404 (const 'nottodo)
405 (choice
406 (const :tag "any not-done state" 'todo)
407 (const :tag "any done state" 'done)
408 (const :tag "any state" 'any)
409 (list :tag "Keyword list"
410 (const :format "" quote)
411 (repeat (string :tag "Keyword")))))
412 (const :tag "scheduled" 'scheduled)
413 (const :tag "not scheduled" 'notscheduled)
414 (const :tag "deadline" 'deadline)
415 (const :tag "no deadline" 'notdeadline)
416 (const :tag "timestamp" 'timestamp)
417 (const :tag "no timestamp" 'nottimestamp))))))
418 (list :tag "Non-standard skipping condition"
419 :value (org-agenda-skip-function)
420 (const org-agenda-skip-function)
421 (sexp :tag "Function or form (quoted!)"))
422 (list :tag "Any variable"
423 (variable :tag "Variable")
424 (sexp :tag "Value (sexp)"))))
425 "Selection of examples for agenda command settings.
426 This will be spliced into the custom type of
427 `org-agenda-custom-commands'.")
430 (defcustom org-agenda-custom-commands
431 '(("n" "Agenda and all TODO's" ((agenda "") (alltodo ""))))
432 "Custom commands for the agenda.
433 These commands will be offered on the splash screen displayed by the
434 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
436 (key desc type match settings files)
438 key The key (one or more characters as a string) to be associated
439 with the command.
440 desc A description of the command, when omitted or nil, a default
441 description is built using MATCH.
442 type The command type, any of the following symbols:
443 agenda The daily/weekly agenda.
444 todo Entries with a specific TODO keyword, in all agenda files.
445 search Entries containing search words entry or headline.
446 tags Tags/Property/TODO match in all agenda files.
447 tags-todo Tags/P/T match in all agenda files, TODO entries only.
448 todo-tree Sparse tree of specific TODO keyword in *current* file.
449 tags-tree Sparse tree with all tags matches in *current* file.
450 occur-tree Occur sparse tree for *current* file.
451 ... A user-defined function.
452 match What to search for:
453 - a single keyword for TODO keyword searches
454 - a tags match expression for tags searches
455 - a word search expression for text searches.
456 - a regular expression for occur searches
457 For all other commands, this should be the empty string.
458 settings A list of option settings, similar to that in a let form, so like
459 this: ((opt1 val1) (opt2 val2) ...). The values will be
460 evaluated at the moment of execution, so quote them when needed.
461 files A list of files file to write the produced agenda buffer to
462 with the command `org-store-agenda-views'.
463 If a file name ends in \".html\", an HTML version of the buffer
464 is written out. If it ends in \".ps\", a postscript version is
465 produced. Otherwise, only the plain text is written to the file.
467 You can also define a set of commands, to create a composite agenda buffer.
468 In this case, an entry looks like this:
470 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
472 where
474 desc A description string to be displayed in the dispatcher menu.
475 cmd An agenda command, similar to the above. However, tree commands
476 are not allowed, but instead you can get agenda and global todo list.
477 So valid commands for a set are:
478 (agenda \"\" settings)
479 (alltodo \"\" settings)
480 (stuck \"\" settings)
481 (todo \"match\" settings files)
482 (search \"match\" settings files)
483 (tags \"match\" settings files)
484 (tags-todo \"match\" settings files)
486 Each command can carry a list of options, and another set of options can be
487 given for the whole set of commands. Individual command options take
488 precedence over the general options.
490 When using several characters as key to a command, the first characters
491 are prefix commands. For the dispatcher to display useful information, you
492 should provide a description for the prefix, like
494 (setq org-agenda-custom-commands
495 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
496 (\"hl\" tags \"+HOME+Lisa\")
497 (\"hp\" tags \"+HOME+Peter\")
498 (\"hk\" tags \"+HOME+Kim\")))"
499 :group 'org-agenda-custom-commands
500 :type `(repeat
501 (choice :value ("x" "Describe command here" tags "" nil)
502 (list :tag "Single command"
503 (string :tag "Access Key(s) ")
504 (option (string :tag "Description"))
505 (choice
506 (const :tag "Agenda" agenda)
507 (const :tag "TODO list" alltodo)
508 (const :tag "Search words" search)
509 (const :tag "Stuck projects" stuck)
510 (const :tag "Tags/Property match (all agenda files)" tags)
511 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
512 (const :tag "TODO keyword search (all agenda files)" todo)
513 (const :tag "Tags sparse tree (current buffer)" tags-tree)
514 (const :tag "TODO keyword tree (current buffer)" todo-tree)
515 (const :tag "Occur tree (current buffer)" occur-tree)
516 (sexp :tag "Other, user-defined function"))
517 (string :tag "Match (only for some commands)")
518 ,org-agenda-custom-commands-local-options
519 (option (repeat :tag "Export" (file :tag "Export to"))))
520 (list :tag "Command series, all agenda files"
521 (string :tag "Access Key(s)")
522 (string :tag "Description ")
523 (repeat :tag "Component"
524 (choice
525 (list :tag "Agenda"
526 (const :format "" agenda)
527 (const :tag "" :format "" "")
528 ,org-agenda-custom-commands-local-options)
529 (list :tag "TODO list (all keywords)"
530 (const :format "" alltodo)
531 (const :tag "" :format "" "")
532 ,org-agenda-custom-commands-local-options)
533 (list :tag "Search words"
534 (const :format "" search)
535 (string :tag "Match")
536 ,org-agenda-custom-commands-local-options)
537 (list :tag "Stuck projects"
538 (const :format "" stuck)
539 (const :tag "" :format "" "")
540 ,org-agenda-custom-commands-local-options)
541 (list :tag "Tags search"
542 (const :format "" tags)
543 (string :tag "Match")
544 ,org-agenda-custom-commands-local-options)
545 (list :tag "Tags search, TODO entries only"
546 (const :format "" tags-todo)
547 (string :tag "Match")
548 ,org-agenda-custom-commands-local-options)
549 (list :tag "TODO keyword search"
550 (const :format "" todo)
551 (string :tag "Match")
552 ,org-agenda-custom-commands-local-options)
553 (list :tag "Other, user-defined function"
554 (symbol :tag "function")
555 (string :tag "Match")
556 ,org-agenda-custom-commands-local-options)))
558 (repeat :tag "Settings for entire command set"
559 (list (variable :tag "Any variable")
560 (sexp :tag "Value")))
561 (option (repeat :tag "Export" (file :tag "Export to"))))
562 (cons :tag "Prefix key documentation"
563 (string :tag "Access Key(s)")
564 (string :tag "Description ")))))
566 (defcustom org-agenda-query-register ?o
567 "The register holding the current query string.
568 The purpose of this is that if you construct a query string interactively,
569 you can then use it to define a custom command."
570 :group 'org-agenda-custom-commands
571 :type 'character)
573 (defcustom org-stuck-projects
574 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
575 "How to identify stuck projects.
576 This is a list of four items:
577 1. A tags/todo/property matcher string that is used to identify a project.
578 See the manual for a description of tag and property searches.
579 The entire tree below a headline matched by this is considered one project.
580 2. A list of TODO keywords identifying non-stuck projects.
581 If the project subtree contains any headline with one of these todo
582 keywords, the project is considered to be not stuck. If you specify
583 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
584 3. A list of tags identifying non-stuck projects.
585 If the project subtree contains any headline with one of these tags,
586 the project is considered to be not stuck. If you specify \"*\" as
587 a tag, any tag will mark the project unstuck. Note that this is about
588 the explicit presence of a tag somewhere in the subtree, inherited
589 tags do not count here. If inherited tags make a project not stuck,
590 use \"-TAG\" in the tags part of the matcher under (1.) above.
591 4. An arbitrary regular expression matching non-stuck projects.
593 If the project turns out to be not stuck, search continues also in the
594 subtree to see if any of the subtasks have project status.
596 See also the variable `org-tags-match-list-sublevels' which applies
597 to projects matched by this search as well.
599 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
600 or `C-c a #' to produce the list."
601 :group 'org-agenda-custom-commands
602 :type '(list
603 (string :tag "Tags/TODO match to identify a project")
604 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
605 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
606 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
608 (defcustom org-agenda-filter-effort-default-operator "<"
609 "The default operator for effort estimate filtering.
610 If you select an effort estimate limit without first pressing an operator,
611 this one will be used."
612 :group 'org-agenda-custom-commands
613 :type '(choice (const :tag "less or equal" "<")
614 (const :tag "greater or equal"">")
615 (const :tag "equal" "=")))
617 (defgroup org-agenda-skip nil
618 "Options concerning skipping parts of agenda files."
619 :tag "Org Agenda Skip"
620 :group 'org-agenda)
622 (defcustom org-agenda-skip-function-global nil
623 "Function to be called at each match during agenda construction.
624 If this function returns nil, the current match should not be skipped.
625 If the function decided to skip an agenda match, is must return the
626 buffer position from which the search should be continued.
627 This may also be a Lisp form, which will be evaluated.
629 This variable will be applied to every agenda match, including
630 tags/property searches and TODO lists. So try to make the test function
631 do its checking as efficiently as possible. To implement a skipping
632 condition just for specific agenda commands, use the variable
633 `org-agenda-skip-function' which can be set in the options section
634 of custom agenda commands."
635 :group 'org-agenda-skip
636 :type 'sexp)
638 (defgroup org-agenda-daily/weekly nil
639 "Options concerning the daily/weekly agenda."
640 :tag "Org Agenda Daily/Weekly"
641 :group 'org-agenda)
642 (defgroup org-agenda-todo-list nil
643 "Options concerning the global todo list agenda view."
644 :tag "Org Agenda Todo List"
645 :group 'org-agenda)
646 (defgroup org-agenda-match-view nil
647 "Options concerning the general tags/property/todo match agenda view."
648 :tag "Org Agenda Match View"
649 :group 'org-agenda)
650 (defgroup org-agenda-search-view nil
651 "Options concerning the general tags/property/todo match agenda view."
652 :tag "Org Agenda Match View"
653 :group 'org-agenda)
655 (defvar org-agenda-archives-mode nil
656 "Non-nil means the agenda will include archived items.
657 If this is the symbol `trees', trees in the selected agenda scope
658 that are marked with the ARCHIVE tag will be included anyway. When this is
659 t, also all archive files associated with the current selection of agenda
660 files will be included.")
662 (defcustom org-agenda-restriction-lock-highlight-subtree t
663 "Non-nil means highlight the whole subtree when restriction is active.
664 Otherwise only highlight the headline. Highlighting the whole subtree is
665 useful to ensure no edits happen beyond the restricted region."
666 :group 'org-agenda
667 :type 'boolean)
669 (defcustom org-agenda-skip-comment-trees t
670 "Non-nil means skip trees that start with the COMMENT keyword.
671 When nil, these trees are also scanned by agenda commands."
672 :group 'org-agenda-skip
673 :type 'boolean)
675 (defcustom org-agenda-todo-list-sublevels t
676 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
677 When nil, the sublevels of a TODO entry are not checked, resulting in
678 potentially much shorter TODO lists."
679 :group 'org-agenda-skip
680 :group 'org-agenda-todo-list
681 :type 'boolean)
683 (defcustom org-agenda-todo-ignore-with-date nil
684 "Non-nil means don't show entries with a date in the global todo list.
685 You can use this if you prefer to mark mere appointments with a TODO keyword,
686 but don't want them to show up in the TODO list.
687 When this is set, it also covers deadlines and scheduled items, the settings
688 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
689 will be ignored.
690 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
691 :group 'org-agenda-skip
692 :group 'org-agenda-todo-list
693 :type 'boolean)
695 (defcustom org-agenda-todo-ignore-timestamp nil
696 "Non-nil means don't show entries with a timestamp.
697 This applies when creating the global todo list.
698 Valid values are:
700 past Don't show entries for today or in the past.
702 future Don't show entries with a timestamp in the future.
703 The idea behind this is that if it has a future
704 timestamp, you don't want to think about it until the
705 date.
707 all Don't show any entries with a timestamp in the global todo list.
708 The idea behind this is that by setting a timestamp, you
709 have already \"taken care\" of this item.
711 This variable can also have an integer as a value. If positive (N),
712 todos with a timestamp N or more days in the future will be ignored. If
713 negative (-N), todos with a timestamp N or more days in the past will be
714 ignored. If 0, todos with a timestamp either today or in the future will
715 be ignored. For example, a value of -1 will exclude todos with a
716 timestamp in the past (yesterday or earlier), while a value of 7 will
717 exclude todos with a timestamp a week or more in the future.
719 See also `org-agenda-todo-ignore-with-date'.
720 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
721 to make his option also apply to the tags-todo list."
722 :group 'org-agenda-skip
723 :group 'org-agenda-todo-list
724 :version "24.1"
725 :type '(choice
726 (const :tag "Ignore future timestamp todos" future)
727 (const :tag "Ignore past or present timestamp todos" past)
728 (const :tag "Ignore all timestamp todos" all)
729 (const :tag "Show timestamp todos" nil)
730 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
732 (defcustom org-agenda-todo-ignore-scheduled nil
733 "Non-nil means, ignore some scheduled TODO items when making TODO list.
734 This applies when creating the global todo list.
735 Valid values are:
737 past Don't show entries scheduled today or in the past.
739 future Don't show entries scheduled in the future.
740 The idea behind this is that by scheduling it, you don't want to
741 think about it until the scheduled date.
743 all Don't show any scheduled entries in the global todo list.
744 The idea behind this is that by scheduling it, you have already
745 \"taken care\" of this item.
747 t Same as `all', for backward compatibility.
749 This variable can also have an integer as a value. See
750 `org-agenda-todo-ignore-timestamp' for more details.
752 See also `org-agenda-todo-ignore-with-date'.
753 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
754 to make his option also apply to the tags-todo list."
755 :group 'org-agenda-skip
756 :group 'org-agenda-todo-list
757 :type '(choice
758 (const :tag "Ignore future-scheduled todos" future)
759 (const :tag "Ignore past- or present-scheduled todos" past)
760 (const :tag "Ignore all scheduled todos" all)
761 (const :tag "Ignore all scheduled todos (compatibility)" t)
762 (const :tag "Show scheduled todos" nil)
763 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
765 (defcustom org-agenda-todo-ignore-deadlines nil
766 "Non-nil means ignore some deadlined TODO items when making TODO list.
767 There are different motivations for using different values, please think
768 carefully when configuring this variable.
770 This applies when creating the global todo list.
771 Valid values are:
773 near Don't show near deadline entries. A deadline is near when it is
774 closer than `org-deadline-warning-days' days. The idea behind this
775 is that such items will appear in the agenda anyway.
777 far Don't show TODO entries where a deadline has been defined, but
778 the deadline is not near. This is useful if you don't want to
779 use the todo list to figure out what to do now.
781 past Don't show entries with a deadline timestamp for today or in the past.
783 future Don't show entries with a deadline timestamp in the future, not even
784 when they become `near' ones. Use it with caution.
786 all Ignore all TODO entries that do have a deadline.
788 t Same as `near', for backward compatibility.
790 This variable can also have an integer as a value. See
791 `org-agenda-todo-ignore-timestamp' for more details.
793 See also `org-agenda-todo-ignore-with-date'.
794 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
795 to make his option also apply to the tags-todo list."
796 :group 'org-agenda-skip
797 :group 'org-agenda-todo-list
798 :type '(choice
799 (const :tag "Ignore near deadlines" near)
800 (const :tag "Ignore near deadlines (compatibility)" t)
801 (const :tag "Ignore far deadlines" far)
802 (const :tag "Ignore all TODOs with a deadlines" all)
803 (const :tag "Show all TODOs, even if they have a deadline" nil)
804 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
806 (defcustom org-agenda-todo-ignore-time-comparison-use-seconds nil
807 "Time unit to use when possibly ignoring an agenda item.
809 See the docstring of various `org-agenda-todo-ignore-*' options.
810 The default is to compare time stamps using days. An item is thus
811 considered to be in the future if it is at least one day after today.
812 Non-nil means to compare time stamps using seconds. An item is then
813 considered future if it has a time value later than current time."
814 :group 'org-agenda-skip
815 :group 'org-agenda-todo-list
816 :version "24.4"
817 :package-version '(Org . "8.0")
818 :type '(choice
819 (const :tag "Compare time with days" nil)
820 (const :tag "Compare time with seconds" t)))
822 (defcustom org-agenda-tags-todo-honor-ignore-options nil
823 "Non-nil means honor todo-list ignores options also in tags-todo search.
824 The variables
825 `org-agenda-todo-ignore-with-date',
826 `org-agenda-todo-ignore-timestamp',
827 `org-agenda-todo-ignore-scheduled',
828 `org-agenda-todo-ignore-deadlines'
829 make the global TODO list skip entries that have time stamps of certain
830 kinds. If this option is set, the same options will also apply for the
831 tags-todo search, which is the general tags/property matcher
832 restricted to unfinished TODO entries only."
833 :group 'org-agenda-skip
834 :group 'org-agenda-todo-list
835 :group 'org-agenda-match-view
836 :type 'boolean)
838 (defcustom org-agenda-skip-scheduled-if-done nil
839 "Non-nil means don't show scheduled items in agenda when they are done.
840 This is relevant for the daily/weekly agenda, not for the TODO list. And
841 it applies only to the actual date of the scheduling. Warnings about
842 an item with a past scheduling dates are always turned off when the item
843 is DONE."
844 :group 'org-agenda-skip
845 :group 'org-agenda-daily/weekly
846 :type 'boolean)
848 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
849 "Non-nil means skip scheduling line if same entry shows because of deadline.
851 In the agenda of today, an entry can show up multiple times
852 because it is both scheduled and has a nearby deadline, and maybe
853 a plain time stamp as well.
855 When this variable is nil, the entry will be shown several times.
857 When set to t, then only the deadline is shown and the fact that
858 the entry is scheduled today or was scheduled previously is not
859 shown.
861 When set to the symbol `not-today', skip scheduled previously,
862 but not scheduled today.
864 When set to the symbol `repeated-after-deadline', skip scheduled
865 items if they are repeated beyond the current dealine."
866 :group 'org-agenda-skip
867 :group 'org-agenda-daily/weekly
868 :type '(choice
869 (const :tag "Never" nil)
870 (const :tag "Always" t)
871 (const :tag "Not when scheduled today" not-today)
872 (const :tag "When repeated past deadline" repeated-after-deadline)))
874 (defcustom org-agenda-skip-timestamp-if-deadline-is-shown nil
875 "Non-nil means skip timestamp line if same entry shows because of deadline.
876 In the agenda of today, an entry can show up multiple times
877 because it has both a plain timestamp and has a nearby deadline.
878 When this variable is t, then only the deadline is shown and the
879 fact that the entry has a timestamp for or including today is not
880 shown. When this variable is nil, the entry will be shown
881 several times."
882 :group 'org-agenda-skip
883 :group 'org-agenda-daily/weekly
884 :version "24.1"
885 :type '(choice
886 (const :tag "Never" nil)
887 (const :tag "Always" t)))
889 (defcustom org-agenda-skip-deadline-if-done nil
890 "Non-nil means don't show deadlines when the corresponding item is done.
891 When nil, the deadline is still shown and should give you a happy feeling.
892 This is relevant for the daily/weekly agenda. And it applied only to the
893 actually date of the deadline. Warnings about approaching and past-due
894 deadlines are always turned off when the item is DONE."
895 :group 'org-agenda-skip
896 :group 'org-agenda-daily/weekly
897 :type 'boolean)
899 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
900 "Non-nil means skip deadline prewarning when entry is also scheduled.
901 This will apply on all days where a prewarning for the deadline would
902 be shown, but not at the day when the entry is actually due. On that day,
903 the deadline will be shown anyway.
904 This variable may be set to nil, t, the symbol `pre-scheduled',
905 or a number which will then give the number of days before the actual
906 deadline when the prewarnings should resume. The symbol `pre-scheduled'
907 eliminates the deadline prewarning only prior to the scheduled date.
908 This can be used in a workflow where the first showing of the deadline will
909 trigger you to schedule it, and then you don't want to be reminded of it
910 because you will take care of it on the day when scheduled."
911 :group 'org-agenda-skip
912 :group 'org-agenda-daily/weekly
913 :version "24.1"
914 :type '(choice
915 (const :tag "Always show prewarning" nil)
916 (const :tag "Remove prewarning prior to scheduled date" pre-scheduled)
917 (const :tag "Remove prewarning if entry is scheduled" t)
918 (integer :tag "Restart prewarning N days before deadline")))
920 (defcustom org-agenda-skip-scheduled-delay-if-deadline nil
921 "Non-nil means skip scheduled delay when entry also has a deadline.
922 This variable may be set to nil, t, the symbol `post-deadline',
923 or a number which will then give the number of days after the actual
924 scheduled date when the delay should expire. The symbol `post-deadline'
925 eliminates the schedule delay when the date is posterior to the deadline."
926 :group 'org-agenda-skip
927 :group 'org-agenda-daily/weekly
928 :version "24.4"
929 :package-version '(Org . "8.0")
930 :type '(choice
931 (const :tag "Always honor delay" nil)
932 (const :tag "Ignore delay if posterior to the deadline" post-deadline)
933 (const :tag "Ignore delay if entry has a deadline" t)
934 (integer :tag "Honor delay up until N days after the scheduled date")))
936 (defcustom org-agenda-skip-additional-timestamps-same-entry nil
937 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
938 When non-nil, after the search for timestamps has matched once in an
939 entry, the rest of the entry will not be searched."
940 :group 'org-agenda-skip
941 :type 'boolean)
943 (defcustom org-agenda-skip-timestamp-if-done nil
944 "Non-nil means don't select item by timestamp or -range if it is DONE."
945 :group 'org-agenda-skip
946 :group 'org-agenda-daily/weekly
947 :type 'boolean)
949 (defcustom org-agenda-dim-blocked-tasks t
950 "Non-nil means dim blocked tasks in the agenda display.
951 This causes some overhead during agenda construction, but if you
952 have turned on `org-enforce-todo-dependencies',
953 `org-enforce-todo-checkbox-dependencies', or any other blocking
954 mechanism, this will create useful feedback in the agenda.
956 Instead of t, this variable can also have the value `invisible'.
957 Then blocked tasks will be invisible and only become visible when
958 they become unblocked. An exemption to this behavior is when a task is
959 blocked because of unchecked checkboxes below it. Since checkboxes do
960 not show up in the agenda views, making this task invisible you remove any
961 trace from agenda views that there is something to do. Therefore, a task
962 that is blocked because of checkboxes will never be made invisible, it
963 will only be dimmed."
964 :group 'org-agenda-daily/weekly
965 :group 'org-agenda-todo-list
966 :version "24.3"
967 :type '(choice
968 (const :tag "Do not dim" nil)
969 (const :tag "Dim to a gray face" t)
970 (const :tag "Make invisible" invisible)))
972 (defcustom org-timeline-show-empty-dates 3
973 "Non-nil means `org-timeline' also shows dates without an entry.
974 When nil, only the days which actually have entries are shown.
975 When t, all days between the first and the last date are shown.
976 When an integer, show also empty dates, but if there is a gap of more than
977 N days, just insert a special line indicating the size of the gap."
978 :group 'org-agenda-skip
979 :type '(choice
980 (const :tag "None" nil)
981 (const :tag "All" t)
982 (integer :tag "at most")))
984 (defgroup org-agenda-startup nil
985 "Options concerning initial settings in the Agenda in Org Mode."
986 :tag "Org Agenda Startup"
987 :group 'org-agenda)
989 (defcustom org-agenda-menu-show-matcher t
990 "Non-nil means show the match string in the agenda dispatcher menu.
991 When nil, the matcher string is not shown, but is put into the help-echo
992 property so than moving the mouse over the command shows it.
993 Setting it to nil is good if matcher strings are very long and/or if
994 you want to use two-columns display (see `org-agenda-menu-two-columns')."
995 :group 'org-agenda
996 :version "24.1"
997 :type 'boolean)
999 (define-obsolete-variable-alias 'org-agenda-menu-two-column 'org-agenda-menu-two-columns "24.3")
1001 (defcustom org-agenda-menu-two-columns nil
1002 "Non-nil means, use two columns to show custom commands in the dispatcher.
1003 If you use this, you probably want to set `org-agenda-menu-show-matcher'
1004 to nil."
1005 :group 'org-agenda
1006 :version "24.1"
1007 :type 'boolean)
1009 (define-obsolete-variable-alias 'org-finalize-agenda-hook 'org-agenda-finalize-hook "24.3")
1010 (defcustom org-agenda-finalize-hook nil
1011 "Hook run just before displaying an agenda buffer.
1012 The buffer is still writable when the hook is called.
1014 You can modify some of the buffer substrings but you should be
1015 extra careful not to modify the text properties of the agenda
1016 headlines as the agenda display heavily relies on them."
1017 :group 'org-agenda-startup
1018 :type 'hook)
1020 (defcustom org-agenda-mouse-1-follows-link nil
1021 "Non-nil means mouse-1 on a link will follow the link in the agenda.
1022 A longer mouse click will still set point. Does not work on XEmacs.
1023 Needs to be set before org.el is loaded."
1024 :group 'org-agenda-startup
1025 :type 'boolean)
1027 (defcustom org-agenda-start-with-follow-mode nil
1028 "The initial value of follow mode in a newly created agenda window."
1029 :group 'org-agenda-startup
1030 :type 'boolean)
1032 (defcustom org-agenda-follow-indirect nil
1033 "Non-nil means `org-agenda-follow-mode' displays only the
1034 current item's tree, in an indirect buffer."
1035 :group 'org-agenda
1036 :version "24.1"
1037 :type 'boolean)
1039 (defcustom org-agenda-show-outline-path t
1040 "Non-nil means show outline path in echo area after line motion."
1041 :group 'org-agenda-startup
1042 :type 'boolean)
1044 (defcustom org-agenda-start-with-entry-text-mode nil
1045 "The initial value of entry-text-mode in a newly created agenda window."
1046 :group 'org-agenda-startup
1047 :type 'boolean)
1049 (defcustom org-agenda-entry-text-maxlines 5
1050 "Number of text lines to be added when `E' is pressed in the agenda.
1052 Note that this variable only used during agenda display. Add add entry text
1053 when exporting the agenda, configure the variable
1054 `org-agenda-add-entry-ext-maxlines'."
1055 :group 'org-agenda
1056 :type 'integer)
1058 (defcustom org-agenda-entry-text-exclude-regexps nil
1059 "List of regular expressions to clean up entry text.
1060 The complete matches of all regular expressions in this list will be
1061 removed from entry text before it is shown in the agenda."
1062 :group 'org-agenda
1063 :type '(repeat (regexp)))
1065 (defcustom org-agenda-entry-text-leaders " > "
1066 "Text prepended to the entry text in agenda buffers."
1067 :version "24.4"
1068 :package-version '(Org . "8.0")
1069 :group 'org-agenda
1070 :type 'string)
1072 (defvar org-agenda-entry-text-cleanup-hook nil
1073 "Hook that is run after basic cleanup of entry text to be shown in agenda.
1074 This cleanup is done in a temporary buffer, so the function may inspect and
1075 change the entire buffer.
1076 Some default stuff like drawers and scheduling/deadline dates will already
1077 have been removed when this is called, as will any matches for regular
1078 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
1080 (defvar org-agenda-include-inactive-timestamps nil
1081 "Non-nil means include inactive time stamps in agenda and timeline.
1082 Dynamically scoped.")
1084 (defgroup org-agenda-windows nil
1085 "Options concerning the windows used by the Agenda in Org Mode."
1086 :tag "Org Agenda Windows"
1087 :group 'org-agenda)
1089 (defcustom org-agenda-window-setup 'reorganize-frame
1090 "How the agenda buffer should be displayed.
1091 Possible values for this option are:
1093 current-window Show agenda in the current window, keeping all other windows.
1094 other-window Use `switch-to-buffer-other-window' to display agenda.
1095 reorganize-frame Show only two windows on the current frame, the current
1096 window and the agenda.
1097 other-frame Use `switch-to-buffer-other-frame' to display agenda.
1098 Also, when exiting the agenda, kill that frame.
1099 See also the variable `org-agenda-restore-windows-after-quit'."
1100 :group 'org-agenda-windows
1101 :type '(choice
1102 (const current-window)
1103 (const other-frame)
1104 (const other-window)
1105 (const reorganize-frame)))
1107 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
1108 "The min and max height of the agenda window as a fraction of frame height.
1109 The value of the variable is a cons cell with two numbers between 0 and 1.
1110 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
1111 :group 'org-agenda-windows
1112 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
1114 (defcustom org-agenda-restore-windows-after-quit nil
1115 "Non-nil means restore window configuration upon exiting agenda.
1116 Before the window configuration is changed for displaying the agenda,
1117 the current status is recorded. When the agenda is exited with
1118 `q' or `x' and this option is set, the old state is restored. If
1119 `org-agenda-window-setup' is `other-frame', the value of this
1120 option will be ignored."
1121 :group 'org-agenda-windows
1122 :type 'boolean)
1124 (defcustom org-agenda-ndays nil
1125 "Number of days to include in overview display.
1126 Should be 1 or 7.
1127 Obsolete, see `org-agenda-span'."
1128 :group 'org-agenda-daily/weekly
1129 :type '(choice (const nil)
1130 (integer)))
1132 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
1134 (defcustom org-agenda-span 'week
1135 "Number of days to include in overview display.
1136 Can be day, week, month, year, or any number of days.
1137 Custom commands can set this variable in the options section."
1138 :group 'org-agenda-daily/weekly
1139 :type '(choice (const :tag "Day" day)
1140 (const :tag "Week" week)
1141 (const :tag "Month" month)
1142 (const :tag "Year" year)
1143 (integer :tag "Custom")))
1145 (defcustom org-agenda-start-on-weekday 1
1146 "Non-nil means start the overview always on the specified weekday.
1147 0 denotes Sunday, 1 denotes Monday, etc.
1148 When nil, always start on the current day.
1149 Custom commands can set this variable in the options section."
1150 :group 'org-agenda-daily/weekly
1151 :type '(choice (const :tag "Today" nil)
1152 (integer :tag "Weekday No.")))
1154 (defcustom org-agenda-show-all-dates t
1155 "Non-nil means `org-agenda' shows every day in the selected range.
1156 When nil, only the days which actually have entries are shown."
1157 :group 'org-agenda-daily/weekly
1158 :type 'boolean)
1160 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
1161 "Format string for displaying dates in the agenda.
1162 Used by the daily/weekly agenda and by the timeline. This should be
1163 a format string understood by `format-time-string', or a function returning
1164 the formatted date as a string. The function must take a single argument,
1165 a calendar-style date list like (month day year)."
1166 :group 'org-agenda-daily/weekly
1167 :type '(choice
1168 (string :tag "Format string")
1169 (function :tag "Function")))
1171 (defun org-agenda-format-date-aligned (date)
1172 "Format a DATE string for display in the daily/weekly agenda, or timeline.
1173 This function makes sure that dates are aligned for easy reading."
1174 (require 'cal-iso)
1175 (let* ((dayname (calendar-day-name date))
1176 (day (cadr date))
1177 (day-of-week (calendar-day-of-week date))
1178 (month (car date))
1179 (monthname (calendar-month-name month))
1180 (year (nth 2 date))
1181 (iso-week (org-days-to-iso-week
1182 (calendar-absolute-from-gregorian date)))
1183 (weekyear (cond ((and (= month 1) (>= iso-week 52))
1184 (1- year))
1185 ((and (= month 12) (<= iso-week 1))
1186 (1+ year))
1187 (t year)))
1188 (weekstring (if (= day-of-week 1)
1189 (format " W%02d" iso-week)
1190 "")))
1191 (format "%-10s %2d %s %4d%s"
1192 dayname day monthname year weekstring)))
1194 (defcustom org-agenda-time-leading-zero nil
1195 "Non-nil means use leading zero for military times in agenda.
1196 For example, 9:30am would become 09:30 rather than 9:30."
1197 :group 'org-agenda-daily/weekly
1198 :version "24.1"
1199 :type 'boolean)
1201 (defcustom org-agenda-timegrid-use-ampm nil
1202 "When set, show AM/PM style timestamps on the timegrid."
1203 :group 'org-agenda
1204 :version "24.1"
1205 :type 'boolean)
1207 (defun org-agenda-time-of-day-to-ampm (time)
1208 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1209 (let* ((hour-number (string-to-number (substring time 0 -3)))
1210 (minute (substring time -2))
1211 (ampm "am"))
1212 (cond
1213 ((equal hour-number 12)
1214 (setq ampm "pm"))
1215 ((> hour-number 12)
1216 (setq ampm "pm")
1217 (setq hour-number (- hour-number 12))))
1218 (concat
1219 (if org-agenda-time-leading-zero
1220 (format "%02d" hour-number)
1221 (format "%02s" (number-to-string hour-number)))
1222 ":" minute ampm)))
1224 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1225 "Conditionally convert TIME to AM/PM format based on `org-agenda-timegrid-use-ampm'."
1226 (if org-agenda-timegrid-use-ampm
1227 (org-agenda-time-of-day-to-ampm time)
1228 time))
1230 (defcustom org-agenda-weekend-days '(6 0)
1231 "Which days are weekend?
1232 These days get the special face `org-agenda-date-weekend' in the agenda
1233 and timeline buffers."
1234 :group 'org-agenda-daily/weekly
1235 :type '(set :greedy t
1236 (const :tag "Monday" 1)
1237 (const :tag "Tuesday" 2)
1238 (const :tag "Wednesday" 3)
1239 (const :tag "Thursday" 4)
1240 (const :tag "Friday" 5)
1241 (const :tag "Saturday" 6)
1242 (const :tag "Sunday" 0)))
1244 (defcustom org-agenda-move-date-from-past-immediately-to-today t
1245 "Non-nil means jump to today when moving a past date forward in time.
1246 When using S-right in the agenda to move a a date forward, and the date
1247 stamp currently points to the past, the first key press will move it
1248 to today. WHen nil, just move one day forward even if the date stays
1249 in the past."
1250 :group 'org-agenda-daily/weekly
1251 :version "24.1"
1252 :type 'boolean)
1254 (defcustom org-agenda-include-diary nil
1255 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1256 Custom commands can set this variable in the options section."
1257 :group 'org-agenda-daily/weekly
1258 :type 'boolean)
1260 (defcustom org-agenda-include-deadlines t
1261 "If non-nil, include entries within their deadline warning period.
1262 Custom commands can set this variable in the options section."
1263 :group 'org-agenda-daily/weekly
1264 :version "24.1"
1265 :type 'boolean)
1267 (defcustom org-agenda-repeating-timestamp-show-all t
1268 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1269 When set to a list of strings, only show occurrences of repeating
1270 stamps for these TODO keywords. When nil, only one occurrence is
1271 shown, either today or the nearest into the future."
1272 :group 'org-agenda-daily/weekly
1273 :type '(choice
1274 (const :tag "Show repeating stamps" t)
1275 (repeat :tag "Show repeating stamps for these TODO keywords"
1276 (string :tag "TODO Keyword"))
1277 (const :tag "Don't show repeating stamps" nil)))
1279 (defcustom org-scheduled-past-days 10000
1280 "Number of days to continue listing scheduled items not marked DONE.
1281 When an item is scheduled on a date, it shows up in the agenda on this
1282 day and will be listed until it is marked done for the number of days
1283 given here."
1284 :group 'org-agenda-daily/weekly
1285 :type 'integer)
1287 (defcustom org-agenda-log-mode-items '(closed clock)
1288 "List of items that should be shown in agenda log mode.
1289 This list may contain the following symbols:
1291 closed Show entries that have been closed on that day.
1292 clock Show entries that have received clocked time on that day.
1293 state Show all logged state changes.
1294 Note that instead of changing this variable, you can also press `C-u l' in
1295 the agenda to display all available LOG items temporarily."
1296 :group 'org-agenda-daily/weekly
1297 :type '(set :greedy t (const closed) (const clock) (const state)))
1299 (defcustom org-agenda-clock-consistency-checks
1300 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1301 :gap-ok-around ("4:00")
1302 :default-face ((:background "DarkRed") (:foreground "white"))
1303 :overlap-face nil :gap-face nil :no-end-time-face nil
1304 :long-face nil :short-face nil)
1305 "This is a property list, with the following keys:
1307 :max-duration Mark clocking chunks that are longer than this time.
1308 This is a time string like \"HH:MM\", or the number
1309 of minutes as an integer.
1311 :min-duration Mark clocking chunks that are shorter that this.
1312 This is a time string like \"HH:MM\", or the number
1313 of minutes as an integer.
1315 :max-gap Mark gaps between clocking chunks that are longer than
1316 this duration. A number of minutes, or a string
1317 like \"HH:MM\".
1319 :gap-ok-around List of times during the day which are usually not working
1320 times. When a gap is detected, but the gap contains any
1321 of these times, the gap is *not* reported. For example,
1322 if this is (\"4:00\" \"13:00\") then gaps that contain
1323 4:00 in the morning (i.e. the night) and 13:00
1324 (i.e. a typical lunch time) do not cause a warning.
1325 You should have at least one time during the night in this
1326 list, or otherwise the first task each morning will trigger
1327 a warning because it follows a long gap.
1329 Furthermore, the following properties can be used to define faces for
1330 issue display.
1332 :default-face the default face, if the specific face is undefined
1333 :overlap-face face for overlapping clocks
1334 :gap-face face for gaps between clocks
1335 :no-end-time-face face for incomplete clocks
1336 :long-face face for clock intervals that are too long
1337 :short-face face for clock intervals that are too short"
1338 :group 'org-agenda-daily/weekly
1339 :group 'org-clock
1340 :version "24.1"
1341 :type 'plist)
1343 (defcustom org-agenda-log-mode-add-notes t
1344 "Non-nil means add first line of notes to log entries in agenda views.
1345 If a log item like a state change or a clock entry is associated with
1346 notes, the first line of these notes will be added to the entry in the
1347 agenda display."
1348 :group 'org-agenda-daily/weekly
1349 :type 'boolean)
1351 (defcustom org-agenda-start-with-log-mode nil
1352 "The initial value of log-mode in a newly created agenda window.
1353 See `org-agenda-log-mode' and `org-agenda-log-mode-items' for further
1354 explanations on the possible values."
1355 :group 'org-agenda-startup
1356 :group 'org-agenda-daily/weekly
1357 :type '(choice (const :tag "Don't show log items" nil)
1358 (const :tag "Show only log items" 'only)
1359 (const :tag "Show all possible log items" 'clockcheck)
1360 (repeat :tag "Choose among possible values for `org-agenda-log-mode-items'"
1361 (choice (const :tag "Show closed log items" 'closed)
1362 (const :tag "Show clocked log items" 'clock)
1363 (const :tag "Show all logged state changes" 'state)))))
1365 (defcustom org-agenda-start-with-clockreport-mode nil
1366 "The initial value of clockreport-mode in a newly created agenda window."
1367 :group 'org-agenda-startup
1368 :group 'org-agenda-daily/weekly
1369 :type 'boolean)
1371 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1372 "Property list with parameters for the clocktable in clockreport mode.
1373 This is the display mode that shows a clock table in the daily/weekly
1374 agenda, the properties for this dynamic block can be set here.
1375 The usual clocktable parameters are allowed here, but you cannot set
1376 the properties :name, :tstart, :tend, :block, and :scope - these will
1377 be overwritten to make sure the content accurately reflects the
1378 current display in the agenda."
1379 :group 'org-agenda-daily/weekly
1380 :type 'plist)
1382 (defcustom org-agenda-search-view-always-boolean nil
1383 "Non-nil means the search string is interpreted as individual parts.
1385 The search string for search view can either be interpreted as a phrase,
1386 or as a list of snippets that define a boolean search for a number of
1387 strings.
1389 When this is non-nil, the string will be split on whitespace, and each
1390 snippet will be searched individually, and all must match in order to
1391 select an entry. A snippet is then a single string of non-white
1392 characters, or a string in double quotes, or a regexp in {} braces.
1393 If a snippet is preceded by \"-\", the snippet must *not* match.
1394 \"+\" is syntactic sugar for positive selection. Each snippet may
1395 be found as a full word or a partial word, but see the variable
1396 `org-agenda-search-view-force-full-words'.
1398 When this is nil, search will look for the entire search phrase as one,
1399 with each space character matching any amount of whitespace, including
1400 line breaks.
1402 Even when this is nil, you can still switch to Boolean search dynamically
1403 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1404 is a regexp marked with braces like \"{abc}\", this will also switch to
1405 boolean search."
1406 :group 'org-agenda-search-view
1407 :version "24.1"
1408 :type 'boolean)
1410 (org-defvaralias 'org-agenda-search-view-search-words-only
1411 'org-agenda-search-view-always-boolean)
1413 (defcustom org-agenda-search-view-force-full-words nil
1414 "Non-nil means, search words must be matches as complete words.
1415 When nil, they may also match part of a word."
1416 :group 'org-agenda-search-view
1417 :version "24.1"
1418 :type 'boolean)
1420 (defcustom org-agenda-search-view-max-outline-level nil
1421 "Maximum outline level to display in search view.
1422 E.g. when this is set to 1, the search view will only
1423 show headlines of level 1."
1424 :group 'org-agenda-search-view
1425 :version "24.4"
1426 :package-version '(Org . "8.0")
1427 :type 'integer)
1429 (defgroup org-agenda-time-grid nil
1430 "Options concerning the time grid in the Org-mode Agenda."
1431 :tag "Org Agenda Time Grid"
1432 :group 'org-agenda)
1434 (defcustom org-agenda-search-headline-for-time t
1435 "Non-nil means search headline for a time-of-day.
1436 If the headline contains a time-of-day in one format or another, it will
1437 be used to sort the entry into the time sequence of items for a day.
1438 Some people have time stamps in the headline that refer to the creation
1439 time or so, and then this produces an unwanted side effect. If this is
1440 the case for your, use this variable to turn off searching the headline
1441 for a time."
1442 :group 'org-agenda-time-grid
1443 :type 'boolean)
1445 (defcustom org-agenda-use-time-grid t
1446 "Non-nil means show a time grid in the agenda schedule.
1447 A time grid is a set of lines for specific times (like every two hours between
1448 8:00 and 20:00). The items scheduled for a day at specific times are
1449 sorted in between these lines.
1450 For details about when the grid will be shown, and what it will look like, see
1451 the variable `org-agenda-time-grid'."
1452 :group 'org-agenda-time-grid
1453 :type 'boolean)
1455 (defcustom org-agenda-time-grid
1456 '((daily today require-timed)
1457 "----------------"
1458 (800 1000 1200 1400 1600 1800 2000))
1460 "The settings for time grid for agenda display.
1461 This is a list of three items. The first item is again a list. It contains
1462 symbols specifying conditions when the grid should be displayed:
1464 daily if the agenda shows a single day
1465 weekly if the agenda shows an entire week
1466 today show grid on current date, independent of daily/weekly display
1467 require-timed show grid only if at least one item has a time specification
1469 The second item is a string which will be placed behind the grid time.
1471 The third item is a list of integers, indicating the times that should have
1472 a grid line."
1473 :group 'org-agenda-time-grid
1474 :type
1475 '(list
1476 (set :greedy t :tag "Grid Display Options"
1477 (const :tag "Show grid in single day agenda display" daily)
1478 (const :tag "Show grid in weekly agenda display" weekly)
1479 (const :tag "Always show grid for today" today)
1480 (const :tag "Show grid only if any timed entries are present"
1481 require-timed)
1482 (const :tag "Skip grid times already present in an entry"
1483 remove-match))
1484 (string :tag "Grid String")
1485 (repeat :tag "Grid Times" (integer :tag "Time"))))
1487 (defcustom org-agenda-show-current-time-in-grid t
1488 "Non-nil means show the current time in the time grid."
1489 :group 'org-agenda-time-grid
1490 :version "24.1"
1491 :type 'boolean)
1493 (defcustom org-agenda-current-time-string
1494 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1495 "The string for the current time marker in the agenda."
1496 :group 'org-agenda-time-grid
1497 :version "24.1"
1498 :type 'string)
1500 (defgroup org-agenda-sorting nil
1501 "Options concerning sorting in the Org-mode Agenda."
1502 :tag "Org Agenda Sorting"
1503 :group 'org-agenda)
1505 (defcustom org-agenda-sorting-strategy
1506 '((agenda habit-down time-up priority-down category-keep)
1507 (todo priority-down category-keep)
1508 (tags priority-down category-keep)
1509 (search category-keep))
1510 "Sorting structure for the agenda items of a single day.
1511 This is a list of symbols which will be used in sequence to determine
1512 if an entry should be listed before another entry. The following
1513 symbols are recognized:
1515 time-up Put entries with time-of-day indications first, early first
1516 time-down Put entries with time-of-day indications first, late first
1517 timestamp-up Sort by any timestamp, early first
1518 timestamp-down Sort by any timestamp, late first
1519 scheduled-up Sort by scheduled timestamp, early first
1520 scheduled-down Sort by scheduled timestamp, late first
1521 deadline-up Sort by deadline timestamp, early first
1522 deadline-down Sort by deadline timestamp, late first
1523 ts-up Sort by active timestamp, early first
1524 ts-down Sort by active timestamp, late first
1525 tsia-up Sort by inactive timestamp, early first
1526 tsia-down Sort by inactive timestamp, late first
1527 category-keep Keep the default order of categories, corresponding to the
1528 sequence in `org-agenda-files'.
1529 category-up Sort alphabetically by category, A-Z.
1530 category-down Sort alphabetically by category, Z-A.
1531 tag-up Sort alphabetically by last tag, A-Z.
1532 tag-down Sort alphabetically by last tag, Z-A.
1533 priority-up Sort numerically by priority, high priority last.
1534 priority-down Sort numerically by priority, high priority first.
1535 todo-state-up Sort by todo state, tasks that are done last.
1536 todo-state-down Sort by todo state, tasks that are done first.
1537 effort-up Sort numerically by estimated effort, high effort last.
1538 effort-down Sort numerically by estimated effort, high effort first.
1539 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1540 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1541 habit-up Put entries that are habits first
1542 habit-down Put entries that are habits last
1543 alpha-up Sort headlines alphabetically
1544 alpha-down Sort headlines alphabetically, reversed
1546 The different possibilities will be tried in sequence, and testing stops
1547 if one comparison returns a \"not-equal\". For example, the default
1548 '(time-up category-keep priority-down)
1549 means: Pull out all entries having a specified time of day and sort them,
1550 in order to make a time schedule for the current day the first thing in the
1551 agenda listing for the day. Of the entries without a time indication, keep
1552 the grouped in categories, don't sort the categories, but keep them in
1553 the sequence given in `org-agenda-files'. Within each category sort by
1554 priority.
1556 Leaving out `category-keep' would mean that items will be sorted across
1557 categories by priority.
1559 Instead of a single list, this can also be a set of list for specific
1560 contents, with a context symbol in the car of the list, any of
1561 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1563 Custom commands can bind this variable in the options section."
1564 :group 'org-agenda-sorting
1565 :type `(choice
1566 (repeat :tag "General" ,org-sorting-choice)
1567 (list :tag "Individually"
1568 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1569 (repeat ,org-sorting-choice))
1570 (cons (const :tag "Strategy for TODO lists" todo)
1571 (repeat ,org-sorting-choice))
1572 (cons (const :tag "Strategy for Tags matches" tags)
1573 (repeat ,org-sorting-choice))
1574 (cons (const :tag "Strategy for search matches" search)
1575 (repeat ,org-sorting-choice)))))
1577 (defcustom org-agenda-cmp-user-defined nil
1578 "A function to define the comparison `user-defined'.
1579 This function must receive two arguments, agenda entry a and b.
1580 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1581 the user comparison, return nil.
1582 When this is defined, you can make `user-defined-up' and `user-defined-down'
1583 part of an agenda sorting strategy."
1584 :group 'org-agenda-sorting
1585 :type 'symbol)
1587 (defcustom org-sort-agenda-notime-is-late t
1588 "Non-nil means items without time are considered late.
1589 This is only relevant for sorting. When t, items which have no explicit
1590 time like 15:30 will be considered as 99:01, i.e. later than any items which
1591 do have a time. When nil, the default time is before 0:00. You can use this
1592 option to decide if the schedule for today should come before or after timeless
1593 agenda entries."
1594 :group 'org-agenda-sorting
1595 :type 'boolean)
1597 (defcustom org-sort-agenda-noeffort-is-high t
1598 "Non-nil means items without effort estimate are sorted as high effort.
1599 This also applies when filtering an agenda view with respect to the
1600 < or > effort operator. Then, tasks with no effort defined will be treated
1601 as tasks with high effort.
1602 When nil, such items are sorted as 0 minutes effort."
1603 :group 'org-agenda-sorting
1604 :type 'boolean)
1606 (defgroup org-agenda-line-format nil
1607 "Options concerning the entry prefix in the Org-mode agenda display."
1608 :tag "Org Agenda Line Format"
1609 :group 'org-agenda)
1611 (defcustom org-agenda-prefix-format
1612 '((agenda . " %i %-12:c%?-12t% s")
1613 (timeline . " % s")
1614 (todo . " %i %-12:c")
1615 (tags . " %i %-12:c")
1616 (search . " %i %-12:c"))
1617 "Format specifications for the prefix of items in the agenda views.
1618 An alist with five entries, each for the different agenda types. The
1619 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1620 The values are format strings.
1622 This format works similar to a printf format, with the following meaning:
1624 %c the category of the item, \"Diary\" for entries from the diary,
1625 or as given by the CATEGORY keyword or derived from the file name
1626 %e the effort required by the item
1627 %l the level of the item (insert X space(s) if item is of level X)
1628 %i the icon category of the item, see `org-agenda-category-icon-alist'
1629 %T the last tag of the item (ignore inherited tags, which come first)
1630 %t the HH:MM time-of-day specification if one applies to the entry
1631 %s Scheduling/Deadline information, a short string
1632 %b show breadcrumbs, i.e., the names of the higher levels
1633 %(expression) Eval EXPRESSION and replace the control string
1634 by the result
1636 All specifiers work basically like the standard `%s' of printf, but may
1637 contain two additional characters: a question mark just after the `%'
1638 and a whitespace/punctuation character just before the final letter.
1640 If the first character after `%' is a question mark, the entire field
1641 will only be included if the corresponding value applies to the current
1642 entry. This is useful for fields which should have fixed width when
1643 present, but zero width when absent. For example, \"%?-12t\" will
1644 result in a 12 character time field if a time of the day is specified,
1645 but will completely disappear in entries which do not contain a time.
1647 If there is punctuation or whitespace character just before the
1648 final format letter, this character will be appended to the field
1649 value if the value is not empty. For example, the format
1650 \"%-12:c\" leads to \"Diary: \" if the category is \"Diary\". If
1651 the category is empty, no additional colon is inserted.
1653 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1654 which means:
1656 - Indent the line with two space characters
1657 - Give the category a 12 chars wide field, padded with whitespace on
1658 the right (because of `-'). Append a colon if there is a category
1659 (because of `:').
1660 - If there is a time-of-day, put it into a 12 chars wide field. If no
1661 time, don't put in an empty field, just skip it (because of '?').
1662 - Finally, put the scheduling information.
1664 See also the variables `org-agenda-remove-times-when-in-prefix' and
1665 `org-agenda-remove-tags'.
1667 Custom commands can set this variable in the options section."
1668 :type '(choice
1669 (string :tag "General format")
1670 (list :greedy t :tag "View dependent"
1671 (cons (const agenda) (string :tag "Format"))
1672 (cons (const timeline) (string :tag "Format"))
1673 (cons (const todo) (string :tag "Format"))
1674 (cons (const tags) (string :tag "Format"))
1675 (cons (const search) (string :tag "Format"))))
1676 :group 'org-agenda-line-format)
1678 (defvar org-prefix-format-compiled nil
1679 "The compiled prefix format and associated variables.
1680 This is a list where first element is a list of variable bindings, and second
1681 element is the compiled format expression. See the variable
1682 `org-agenda-prefix-format'.")
1684 (defcustom org-agenda-todo-keyword-format "%-1s"
1685 "Format for the TODO keyword in agenda lines.
1686 Set this to something like \"%-12s\" if you want all TODO keywords
1687 to occupy a fixed space in the agenda display."
1688 :group 'org-agenda-line-format
1689 :type 'string)
1691 (defcustom org-agenda-diary-sexp-prefix nil
1692 "A regexp that matches part of a diary sexp entry
1693 which should be treated as scheduling/deadline information in
1694 `org-agenda'.
1696 For example, you can use this to extract the `diary-remind-message' from
1697 `diary-remind' entries."
1698 :group 'org-agenda-line-format
1699 :type '(choice (const :tag "None" nil) (regexp :tag "Regexp")))
1701 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1702 "Text preceding timerange entries in the agenda view.
1703 This is a list with two strings. The first applies when the range
1704 is entirely on one day. The second applies if the range spans several days.
1705 The strings may have two \"%d\" format specifiers which will be filled
1706 with the sequence number of the days, and the total number of days in the
1707 range, respectively."
1708 :group 'org-agenda-line-format
1709 :type '(list
1710 (string :tag "Deadline today ")
1711 (choice :tag "Deadline relative"
1712 (string :tag "Format string")
1713 (function))))
1715 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1716 "Text preceding scheduled items in the agenda view.
1717 This is a list with two strings. The first applies when the item is
1718 scheduled on the current day. The second applies when it has been scheduled
1719 previously, it may contain a %d indicating that this is the nth time that
1720 this item is scheduled, due to automatic rescheduling of unfinished items
1721 for the following day. So this number is one larger than the number of days
1722 that passed since this item was scheduled first."
1723 :group 'org-agenda-line-format
1724 :version "24.4"
1725 :package-version '(Org . "8.0")
1726 :type '(list
1727 (string :tag "Scheduled today ")
1728 (string :tag "Scheduled previously")))
1730 (defcustom org-agenda-inactive-leader "["
1731 "Text preceding item pulled into the agenda by inactive time stamps.
1732 These entries are added to the agenda when pressing \"[\"."
1733 :group 'org-agenda-line-format
1734 :version "24.1"
1735 :type 'string)
1737 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: " "%2d d. ago: ")
1738 "Text preceding deadline items in the agenda view.
1739 This is a list with three strings. The first applies when the item has its
1740 deadline on the current day. The second applies when the deadline is in the
1741 future, the third one when it is in the past. The strings may contain %d
1742 to capture the number of days."
1743 :group 'org-agenda-line-format
1744 :version "24.4"
1745 :package-version '(Org . "8.0")
1746 :type '(list
1747 (string :tag "Deadline today ")
1748 (choice :tag "Deadline relative"
1749 (string :tag "Format string")
1750 (function))))
1752 (defcustom org-agenda-remove-times-when-in-prefix t
1753 "Non-nil means remove duplicate time specifications in agenda items.
1754 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1755 time-of-day specification in a headline or diary entry is extracted and
1756 placed into the prefix. If this option is non-nil, the original specification
1757 \(a timestamp or -range, or just a plain time(range) specification like
1758 11:30-4pm) will be removed for agenda display. This makes the agenda less
1759 cluttered.
1760 The option can be t or nil. It may also be the symbol `beg', indicating
1761 that the time should only be removed when it is located at the beginning of
1762 the headline/diary entry."
1763 :group 'org-agenda-line-format
1764 :type '(choice
1765 (const :tag "Always" t)
1766 (const :tag "Never" nil)
1767 (const :tag "When at beginning of entry" beg)))
1769 (defcustom org-agenda-remove-timeranges-from-blocks nil
1770 "Non-nil means remove time ranges specifications in agenda
1771 items that span on several days."
1772 :group 'org-agenda-line-format
1773 :version "24.1"
1774 :type 'boolean)
1776 (defcustom org-agenda-default-appointment-duration nil
1777 "Default duration for appointments that only have a starting time.
1778 When nil, no duration is specified in such cases.
1779 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1780 :group 'org-agenda-line-format
1781 :type '(choice
1782 (integer :tag "Minutes")
1783 (const :tag "No default duration")))
1785 (defcustom org-agenda-show-inherited-tags t
1786 "Non-nil means show inherited tags in each agenda line.
1788 When this option is set to 'always, it take precedences over
1789 `org-agenda-use-tag-inheritance' and inherited tags are shown
1790 in every agenda.
1792 When this option is set to t (the default), inherited tags are
1793 shown when they are available, i.e. when the value of
1794 `org-agenda-use-tag-inheritance' has been taken into account.
1796 This can be set to a list of agenda types in which the agenda
1797 must display the inherited tags. Available types are 'todo,
1798 'agenda, 'search and 'timeline.
1800 When set to nil, never show inherited tags in agenda lines."
1801 :group 'org-agenda-line-format
1802 :group 'org-agenda
1803 :version "24.3"
1804 :type '(choice
1805 (const :tag "Show inherited tags when available" t)
1806 (const :tag "Always show inherited tags" 'always)
1807 (repeat :tag "Show inherited tags only in selected agenda types"
1808 (symbol :tag "Agenda type"))))
1810 (defcustom org-agenda-use-tag-inheritance '(todo search timeline agenda)
1811 "List of agenda view types where to use tag inheritance.
1813 In tags/tags-todo/tags-tree agenda views, tag inheritance is
1814 controlled by `org-use-tag-inheritance'. In other agenda types,
1815 `org-use-tag-inheritance' is not used for the selection of the
1816 agenda entries. Still, you may want the agenda to be aware of
1817 the inherited tags anyway, e.g. for later tag filtering.
1819 Allowed value are 'todo, 'search, 'timeline and 'agenda.
1821 This variable has no effect if `org-agenda-show-inherited-tags'
1822 is set to 'always. In that case, the agenda is aware of those
1823 tags.
1825 The default value sets tags in every agenda type. Setting this
1826 option to nil will speed up non-tags agenda view a lot."
1827 :group 'org-agenda
1828 :version "24.3"
1829 :type '(choice
1830 (const :tag "Use tag inheritance in all agenda types" t)
1831 (repeat :tag "Use tag inheritance in selected agenda types"
1832 (symbol :tag "Agenda type"))))
1834 (defcustom org-agenda-hide-tags-regexp nil
1835 "Regular expression used to filter away specific tags in agenda views.
1836 This means that these tags will be present, but not be shown in the agenda
1837 line. Secondary filtering will still work on the hidden tags.
1838 Nil means don't hide any tags."
1839 :group 'org-agenda-line-format
1840 :type '(choice
1841 (const :tag "Hide none" nil)
1842 (string :tag "Regexp ")))
1844 (defcustom org-agenda-remove-tags nil
1845 "Non-nil means remove the tags from the headline copy in the agenda.
1846 When this is the symbol `prefix', only remove tags when
1847 `org-agenda-prefix-format' contains a `%T' specifier."
1848 :group 'org-agenda-line-format
1849 :type '(choice
1850 (const :tag "Always" t)
1851 (const :tag "Never" nil)
1852 (const :tag "When prefix format contains %T" prefix)))
1854 (org-defvaralias 'org-agenda-remove-tags-when-in-prefix
1855 'org-agenda-remove-tags)
1857 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1858 "Shift tags in agenda items to this column.
1859 If this number is positive, it specifies the column. If it is negative,
1860 it means that the tags should be flushright to that column. For example,
1861 -80 works well for a normal 80 character screen."
1862 :group 'org-agenda-line-format
1863 :type 'integer)
1865 (org-defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column)
1867 (defcustom org-agenda-fontify-priorities 'cookies
1868 "Non-nil means highlight low and high priorities in agenda.
1869 When t, the highest priority entries are bold, lowest priority italic.
1870 However, settings in `org-priority-faces' will overrule these faces.
1871 When this variable is the symbol `cookies', only fontify the
1872 cookies, not the entire task.
1873 This may also be an association list of priority faces, whose
1874 keys are the character values of `org-highest-priority',
1875 `org-default-priority', and `org-lowest-priority' (the default values
1876 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1877 color as a string, or a list like `(:background \"Red\")'.
1878 If it is a color, the variable `org-faces-easy-properties'
1879 determines if it is a foreground or a background color."
1880 :group 'org-agenda-line-format
1881 :type '(choice
1882 (const :tag "Never" nil)
1883 (const :tag "Defaults" t)
1884 (const :tag "Cookies only" cookies)
1885 (repeat :tag "Specify"
1886 (list (character :tag "Priority" :value ?A)
1887 (choice :tag "Face "
1888 (string :tag "Color")
1889 (sexp :tag "Face"))))))
1891 (defcustom org-agenda-day-face-function nil
1892 "Function called to determine what face should be used to display a day.
1893 The only argument passed to that function is the day. It should
1894 returns a face, or nil if does not want to specify a face and let
1895 the normal rules apply."
1896 :group 'org-agenda-line-format
1897 :version "24.1"
1898 :type '(choice (const nil) (function)))
1900 (defcustom org-agenda-category-icon-alist nil
1901 "Alist of category icon to be displayed in agenda views.
1903 Each entry should have the following format:
1905 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1907 Where CATEGORY-REGEXP is a regexp matching the categories where
1908 the icon should be displayed.
1909 FILE-OR-DATA either a file path or a string containing image data.
1911 The other fields can be omitted safely if not needed:
1912 TYPE indicates the image type.
1913 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1914 image data.
1915 PROPS are additional image attributes to assign to the image,
1916 like, e.g. `:ascent center'.
1918 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1920 If you want to set the display properties yourself, just put a
1921 list as second element:
1923 (CATEGORY-REGEXP (MY PROPERTY LIST))
1925 For example, to display a 16px horizontal space for Emacs
1926 category, you can use:
1928 (\"Emacs\" '(space . (:width (16))))"
1929 :group 'org-agenda-line-format
1930 :version "24.1"
1931 :type '(alist :key-type (string :tag "Regexp matching category")
1932 :value-type (choice (list :tag "Icon"
1933 (string :tag "File or data")
1934 (symbol :tag "Type")
1935 (boolean :tag "Data?")
1936 (repeat :tag "Extra image properties" :inline t symbol))
1937 (list :tag "Display properties" sexp))))
1939 (defgroup org-agenda-column-view nil
1940 "Options concerning column view in the agenda."
1941 :tag "Org Agenda Column View"
1942 :group 'org-agenda)
1944 (defcustom org-agenda-columns-show-summaries t
1945 "Non-nil means show summaries for columns displayed in the agenda view."
1946 :group 'org-agenda-column-view
1947 :type 'boolean)
1949 (defcustom org-agenda-columns-compute-summary-properties t
1950 "Non-nil means recompute all summary properties before column view.
1951 When column view in the agenda is listing properties that have a summary
1952 operator, it can go to all relevant buffers and recompute the summaries
1953 there. This can mean overhead for the agenda column view, but is necessary
1954 to have thing up to date.
1955 As a special case, a CLOCKSUM property also makes sure that the clock
1956 computations are current."
1957 :group 'org-agenda-column-view
1958 :type 'boolean)
1960 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1961 "Non-nil means the duration of an appointment will add to day effort.
1962 The property to which appointment durations will be added is the one given
1963 in the option `org-effort-property'. If an appointment does not have
1964 an end time, `org-agenda-default-appointment-duration' will be used. If that
1965 is not set, an appointment without end time will not contribute to the time
1966 estimate."
1967 :group 'org-agenda-column-view
1968 :type 'boolean)
1970 (defcustom org-agenda-auto-exclude-function nil
1971 "A function called with a tag to decide if it is filtered on '/ RET'.
1972 The sole argument to the function, which is called once for each
1973 possible tag, is a string giving the name of the tag. The
1974 function should return either nil if the tag should be included
1975 as normal, or \"-<TAG>\" to exclude the tag.
1976 Note that for the purpose of tag filtering, only the lower-case version of
1977 all tags will be considered, so that this function will only ever see
1978 the lower-case version of all tags."
1979 :group 'org-agenda
1980 :type '(choice (const nil) (function)))
1982 (defcustom org-agenda-bulk-custom-functions nil
1983 "Alist of characters and custom functions for bulk actions.
1984 For example, this value makes those two functions available:
1986 '((?R set-category)
1987 (?C bulk-cut))
1989 With selected entries in an agenda buffer, `B R' will call
1990 the custom function `set-category' on the selected entries.
1991 Note that functions in this alist don't need to be quoted."
1992 :type 'alist
1993 :version "24.1"
1994 :group 'org-agenda)
1996 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1997 "Execute BODY with point at location given by `org-hd-marker' property.
1998 If STRING is non-nil, the text property will be fetched from position 0
1999 in that string. If STRING is nil, it will be fetched from the beginning
2000 of the current line."
2001 (org-with-gensyms (marker)
2002 `(let ((,marker (get-text-property (if string 0 (point-at-bol))
2003 'org-hd-marker ,string)))
2004 (with-current-buffer (marker-buffer ,marker)
2005 (save-excursion
2006 (goto-char ,marker)
2007 ,@body)))))
2008 (def-edebug-spec org-agenda-with-point-at-orig-entry (form body))
2010 (defun org-add-agenda-custom-command (entry)
2011 "Replace or add a command in `org-agenda-custom-commands'.
2012 This is mostly for hacking and trying a new command - once the command
2013 works you probably want to add it to `org-agenda-custom-commands' for good."
2014 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
2015 (if ass
2016 (setcdr ass (cdr entry))
2017 (push entry org-agenda-custom-commands))))
2019 ;;; Define the org-agenda-mode
2021 (defvar org-agenda-mode-map (make-sparse-keymap)
2022 "Keymap for `org-agenda-mode'.")
2023 (org-defvaralias 'org-agenda-keymap 'org-agenda-mode-map)
2025 (defvar org-agenda-menu) ; defined later in this file.
2026 (defvar org-agenda-restrict nil) ; defined later in this file.
2027 (defvar org-agenda-follow-mode nil)
2028 (defvar org-agenda-entry-text-mode nil)
2029 (defvar org-agenda-clockreport-mode nil)
2030 (defvar org-agenda-show-log nil)
2031 (defvar org-agenda-redo-command nil)
2032 (defvar org-agenda-query-string nil)
2033 (defvar org-agenda-mode-hook nil
2034 "Hook run after `org-agenda-mode' is turned on.
2035 The buffer is still writable when this hook is called.")
2036 (defvar org-agenda-type nil)
2037 (defvar org-agenda-force-single-file nil)
2038 (defvar org-agenda-bulk-marked-entries nil
2039 "List of markers that refer to marked entries in the agenda.")
2041 ;;; Multiple agenda buffers support
2043 (defcustom org-agenda-sticky nil
2044 "Non-nil means agenda q key will bury agenda buffers.
2045 Agenda commands will then show existing buffer instead of generating new ones.
2046 When nil, `q' will kill the single agenda buffer."
2047 :group 'org-agenda
2048 :version "24.3"
2049 :type 'boolean)
2052 ;;;###autoload
2053 (defun org-toggle-sticky-agenda (&optional arg)
2054 "Toggle `org-agenda-sticky'."
2055 (interactive "P")
2056 (let ((new-value (if arg
2057 (> (prefix-numeric-value arg) 0)
2058 (not org-agenda-sticky))))
2059 (if (equal new-value org-agenda-sticky)
2060 (and (org-called-interactively-p 'interactive)
2061 (message "Sticky agenda was already %s"
2062 (if org-agenda-sticky "enabled" "disabled")))
2063 (setq org-agenda-sticky new-value)
2064 (org-agenda-kill-all-agenda-buffers)
2065 (and (org-called-interactively-p 'interactive)
2066 (message "Sticky agenda was %s"
2067 (if org-agenda-sticky "enabled" "disabled"))))))
2069 (defvar org-agenda-buffer nil
2070 "Agenda buffer currently being generated.")
2072 (defvar org-agenda-last-prefix-arg nil)
2073 (defvar org-agenda-this-buffer-name nil)
2074 (defvar org-agenda-doing-sticky-redo nil)
2075 (defvar org-agenda-this-buffer-is-sticky nil)
2077 (defconst org-agenda-local-vars
2078 '(org-agenda-this-buffer-name
2079 org-agenda-undo-list
2080 org-agenda-pending-undo-list
2081 org-agenda-follow-mode
2082 org-agenda-entry-text-mode
2083 org-agenda-clockreport-mode
2084 org-agenda-show-log
2085 org-agenda-redo-command
2086 org-agenda-query-string
2087 org-agenda-type
2088 org-agenda-bulk-marked-entries
2089 org-agenda-undo-has-started-in
2090 org-agenda-info
2091 org-agenda-pre-window-conf
2092 org-agenda-columns-active
2093 org-agenda-tag-filter-overlays
2094 org-agenda-tag-filter
2095 org-agenda-cat-filter-overlays
2096 org-agenda-category-filter
2097 org-agenda-re-filter-overlays
2098 org-agenda-regexp-filter
2099 org-agenda-markers
2100 org-agenda-last-search-view-search-was-boolean
2101 org-agenda-filtered-by-category
2102 org-agenda-filter-form
2103 org-agenda-cycle-counter
2104 org-agenda-last-prefix-arg)
2105 "Variables that must be local in agenda buffers to allow multiple buffers.")
2107 (defun org-agenda-mode ()
2108 "Mode for time-sorted view on action items in Org-mode files.
2110 The following commands are available:
2112 \\{org-agenda-mode-map}"
2113 (interactive)
2114 (cond (org-agenda-doing-sticky-redo
2115 ;; Refreshing sticky agenda-buffer
2117 ;; Preserve the value of `org-agenda-local-vars' variables,
2118 ;; while letting `kill-all-local-variables' kill the rest
2119 (let ((save (buffer-local-variables)))
2120 (kill-all-local-variables)
2121 (mapc 'make-local-variable org-agenda-local-vars)
2122 (dolist (elem save)
2123 (let ((var (car elem))
2124 (val (cdr elem)))
2125 (when (and val
2126 (member var org-agenda-local-vars))
2127 (set var val)))))
2128 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2129 (org-agenda-sticky
2130 ;; Creating a sticky Agenda buffer for the first time
2131 (kill-all-local-variables)
2132 (mapc 'make-local-variable org-agenda-local-vars)
2133 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) t))
2135 ;; Creating a non-sticky agenda buffer
2136 (kill-all-local-variables)
2137 (set (make-local-variable 'org-agenda-this-buffer-is-sticky) nil)))
2138 (setq org-agenda-undo-list nil
2139 org-agenda-pending-undo-list nil
2140 org-agenda-bulk-marked-entries nil)
2141 (setq major-mode 'org-agenda-mode)
2142 ;; Keep global-font-lock-mode from turning on font-lock-mode
2143 (org-set-local 'font-lock-global-modes (list 'not major-mode))
2144 (setq mode-name "Org-Agenda")
2145 (use-local-map org-agenda-mode-map)
2146 (easy-menu-add org-agenda-menu)
2147 (if org-startup-truncated (setq truncate-lines t))
2148 (org-set-local 'line-move-visual nil)
2149 (org-add-hook 'post-command-hook 'org-agenda-update-agenda-type nil 'local)
2150 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
2151 ;; Make sure properties are removed when copying text
2152 (org-add-hook 'filter-buffer-substring-functions
2153 (lambda (fun start end delete)
2154 (substring-no-properties (funcall fun start end delete)))
2155 nil t)
2156 (unless org-agenda-keep-modes
2157 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
2158 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
2159 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
2160 org-agenda-show-log org-agenda-start-with-log-mode))
2162 (easy-menu-change
2163 '("Agenda") "Agenda Files"
2164 (append
2165 (list
2166 (vector
2167 (if (get 'org-agenda-files 'org-restrict)
2168 "Restricted to single file"
2169 "Edit File List")
2170 '(org-edit-agenda-file-list)
2171 (not (get 'org-agenda-files 'org-restrict)))
2172 "--")
2173 (mapcar 'org-file-menu-entry (org-agenda-files))))
2174 (org-agenda-set-mode-name)
2175 (apply
2176 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
2177 (list 'org-agenda-mode-hook)))
2179 (substitute-key-definition 'undo 'org-agenda-undo
2180 org-agenda-mode-map global-map)
2181 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
2182 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
2183 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
2184 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
2185 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
2186 (org-defkey org-agenda-mode-map [(meta down)] 'org-agenda-drag-line-forward)
2187 (org-defkey org-agenda-mode-map [(meta up)] 'org-agenda-drag-line-backward)
2188 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
2189 (org-defkey org-agenda-mode-map "\M-m" 'org-agenda-bulk-toggle)
2190 (org-defkey org-agenda-mode-map "*" 'org-agenda-bulk-mark-all)
2191 (org-defkey org-agenda-mode-map "\M-*" 'org-agenda-bulk-toggle-all)
2192 (org-defkey org-agenda-mode-map "#" 'org-agenda-dim-blocked-tasks)
2193 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
2194 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
2195 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-unmark-all)
2196 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
2197 (org-defkey org-agenda-mode-map "k" 'org-agenda-capture)
2198 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
2199 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
2200 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
2201 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
2202 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
2203 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
2204 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
2205 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
2206 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
2207 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
2208 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
2209 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
2210 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
2211 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
2212 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
2213 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
2214 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
2215 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
2216 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
2217 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
2218 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
2219 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
2220 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
2221 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
2222 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
2223 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
2224 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
2225 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
2226 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
2227 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
2228 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
2229 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
2230 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
2232 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
2233 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
2234 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
2235 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
2236 (while l (org-defkey org-agenda-mode-map
2237 (int-to-string (pop l)) 'digit-argument)))
2239 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
2240 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
2241 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
2242 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
2243 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
2244 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
2245 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
2246 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
2247 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
2248 (org-defkey org-agenda-mode-map "g" (lambda () (interactive) (org-agenda-redo t)))
2249 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
2250 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
2251 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
2252 'org-clock-modify-effort-estimate)
2253 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
2254 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
2255 (org-defkey org-agenda-mode-map "Q" 'org-agenda-Quit)
2256 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
2257 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-agenda-write)
2258 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
2259 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
2260 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
2261 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
2262 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
2263 (org-defkey org-agenda-mode-map "N" 'org-agenda-next-item)
2264 (org-defkey org-agenda-mode-map "P" 'org-agenda-previous-item)
2265 (substitute-key-definition 'next-line 'org-agenda-next-line
2266 org-agenda-mode-map global-map)
2267 (substitute-key-definition 'previous-line 'org-agenda-previous-line
2268 org-agenda-mode-map global-map)
2269 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
2270 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
2271 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
2272 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
2273 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
2274 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
2275 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
2276 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
2277 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
2278 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
2279 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
2280 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
2281 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
2282 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
2283 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
2284 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
2285 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
2286 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
2287 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
2288 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
2289 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
2290 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
2291 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
2292 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
2293 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
2294 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
2295 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
2296 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
2297 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
2298 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
2300 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
2301 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
2302 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
2303 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
2304 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
2305 (org-defkey org-agenda-mode-map "=" 'org-agenda-filter-by-regexp)
2306 (org-defkey org-agenda-mode-map "|" 'org-agenda-filter-remove-all)
2307 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
2308 (org-defkey org-agenda-mode-map "~" 'org-agenda-limit-interactively)
2309 (org-defkey org-agenda-mode-map "<" 'org-agenda-filter-by-category)
2310 (org-defkey org-agenda-mode-map "^" 'org-agenda-filter-by-top-headline)
2311 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
2312 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
2313 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
2314 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
2316 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
2317 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
2318 (when org-agenda-mouse-1-follows-link
2319 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
2320 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
2321 '("Agenda"
2322 ("Agenda Files")
2323 "--"
2324 ("Agenda Dates"
2325 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
2326 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
2327 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
2328 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
2329 "--"
2330 ("View"
2331 ["Day View" org-agenda-day-view
2332 :active (org-agenda-check-type nil 'agenda)
2333 :style radio :selected (eq org-agenda-current-span 'day)
2334 :keys "v d (or just d)"]
2335 ["Week View" org-agenda-week-view
2336 :active (org-agenda-check-type nil 'agenda)
2337 :style radio :selected (eq org-agenda-current-span 'week)
2338 :keys "v w (or just w)"]
2339 ["Month View" org-agenda-month-view
2340 :active (org-agenda-check-type nil 'agenda)
2341 :style radio :selected (eq org-agenda-current-span 'month)
2342 :keys "v m"]
2343 ["Year View" org-agenda-year-view
2344 :active (org-agenda-check-type nil 'agenda)
2345 :style radio :selected (eq org-agenda-current-span 'year)
2346 :keys "v y"]
2347 "--"
2348 ["Include Diary" org-agenda-toggle-diary
2349 :style toggle :selected org-agenda-include-diary
2350 :active (org-agenda-check-type nil 'agenda)]
2351 ["Include Deadlines" org-agenda-toggle-deadlines
2352 :style toggle :selected org-agenda-include-deadlines
2353 :active (org-agenda-check-type nil 'agenda)]
2354 ["Use Time Grid" org-agenda-toggle-time-grid
2355 :style toggle :selected org-agenda-use-time-grid
2356 :active (org-agenda-check-type nil 'agenda)]
2357 "--"
2358 ["Show clock report" org-agenda-clockreport-mode
2359 :style toggle :selected org-agenda-clockreport-mode
2360 :active (org-agenda-check-type nil 'agenda)]
2361 ["Show some entry text" org-agenda-entry-text-mode
2362 :style toggle :selected org-agenda-entry-text-mode
2363 :active t]
2364 "--"
2365 ["Show Logbook entries" org-agenda-log-mode
2366 :style toggle :selected org-agenda-show-log
2367 :active (org-agenda-check-type nil 'agenda 'timeline)
2368 :keys "v l (or just l)"]
2369 ["Include archived trees" org-agenda-archives-mode
2370 :style toggle :selected org-agenda-archives-mode :active t
2371 :keys "v a"]
2372 ["Include archive files" (org-agenda-archives-mode t)
2373 :style toggle :selected (eq org-agenda-archives-mode t) :active t
2374 :keys "v A"]
2375 "--"
2376 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
2377 ["Write view to file" org-agenda-write t]
2378 ["Rebuild buffer" org-agenda-redo t]
2379 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
2380 "--"
2381 ["Show original entry" org-agenda-show t]
2382 ["Go To (other window)" org-agenda-goto t]
2383 ["Go To (this window)" org-agenda-switch-to t]
2384 ["Capture with cursor date" org-agenda-capture t]
2385 ["Follow Mode" org-agenda-follow-mode
2386 :style toggle :selected org-agenda-follow-mode :active t]
2387 ;; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2388 "--"
2389 ("TODO"
2390 ["Cycle TODO" org-agenda-todo t]
2391 ["Next TODO set" org-agenda-todo-nextset t]
2392 ["Previous TODO set" org-agenda-todo-previousset t]
2393 ["Add note" org-agenda-add-note t])
2394 ("Archive/Refile/Delete"
2395 ["Archive default" org-agenda-archive-default t]
2396 ["Archive default" org-agenda-archive-default-with-confirmation t]
2397 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2398 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2399 ["Archive subtree" org-agenda-archive t]
2400 "--"
2401 ["Refile" org-agenda-refile t]
2402 "--"
2403 ["Delete subtree" org-agenda-kill t])
2404 ("Bulk action"
2405 ["Mark entry" org-agenda-bulk-mark t]
2406 ["Mark all" org-agenda-bulk-mark-all t]
2407 ["Unmark entry" org-agenda-bulk-unmark t]
2408 ["Unmark all" org-agenda-bulk-unmark-all :active t :keys "U"]
2409 ["Toggle mark" org-agenda-bulk-toggle t]
2410 ["Toggle all" org-agenda-bulk-toggle-all t]
2411 ["Mark regexp" org-agenda-bulk-mark-regexp t])
2412 ["Act on all marked" org-agenda-bulk-action t]
2413 "--"
2414 ("Tags and Properties"
2415 ["Show all Tags" org-agenda-show-tags t]
2416 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2417 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2418 "--"
2419 ["Column View" org-columns t])
2420 ("Deadline/Schedule"
2421 ["Schedule" org-agenda-schedule t]
2422 ["Set Deadline" org-agenda-deadline t]
2423 "--"
2424 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2425 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2426 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2427 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2428 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2429 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2430 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2431 ("Clock and Effort"
2432 ["Clock in" org-agenda-clock-in t]
2433 ["Clock out" org-agenda-clock-out t]
2434 ["Clock cancel" org-agenda-clock-cancel t]
2435 ["Goto running clock" org-clock-goto t]
2436 "--"
2437 ["Set Effort" org-agenda-set-effort t]
2438 ["Change clocked effort" org-clock-modify-effort-estimate
2439 (org-clock-is-active)])
2440 ("Priority"
2441 ["Set Priority" org-agenda-priority t]
2442 ["Increase Priority" org-agenda-priority-up t]
2443 ["Decrease Priority" org-agenda-priority-down t]
2444 ["Show Priority" org-show-priority t])
2445 ("Calendar/Diary"
2446 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2447 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2448 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2449 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2450 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2451 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2452 "--"
2453 ["Create iCalendar File" org-icalendar-combine-agenda-files t])
2454 "--"
2455 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2456 "--"
2457 ("MobileOrg"
2458 ["Push Files and Views" org-mobile-push t]
2459 ["Get Captured and Flagged" org-mobile-pull t]
2460 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2461 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2462 "--"
2463 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2464 "--"
2465 ["Quit" org-agenda-quit t]
2466 ["Exit and Release Buffers" org-agenda-exit t]
2469 ;;; Agenda undo
2471 (defvar org-agenda-allow-remote-undo t
2472 "Non-nil means allow remote undo from the agenda buffer.")
2473 (defvar org-agenda-undo-has-started-in nil
2474 "Buffers that have already seen `undo-start' in the current undo sequence.")
2476 (defun org-agenda-undo ()
2477 "Undo a remote editing step in the agenda.
2478 This undoes changes both in the agenda buffer and in the remote buffer
2479 that have been changed along."
2480 (interactive)
2481 (or org-agenda-allow-remote-undo
2482 (user-error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2483 (if (not (eq this-command last-command))
2484 (setq org-agenda-undo-has-started-in nil
2485 org-agenda-pending-undo-list org-agenda-undo-list))
2486 (if (not org-agenda-pending-undo-list)
2487 (user-error "No further undo information"))
2488 (let* ((entry (pop org-agenda-pending-undo-list))
2489 buf line cmd rembuf)
2490 (setq cmd (pop entry) line (pop entry))
2491 (setq rembuf (nth 2 entry))
2492 (org-with-remote-undo rembuf
2493 (while (bufferp (setq buf (pop entry)))
2494 (if (pop entry)
2495 (with-current-buffer buf
2496 (let ((last-undo-buffer buf)
2497 (inhibit-read-only t))
2498 (unless (memq buf org-agenda-undo-has-started-in)
2499 (push buf org-agenda-undo-has-started-in)
2500 (make-local-variable 'pending-undo-list)
2501 (undo-start))
2502 (while (and pending-undo-list
2503 (listp pending-undo-list)
2504 (not (car pending-undo-list)))
2505 (pop pending-undo-list))
2506 (undo-more 1))))))
2507 (org-goto-line line)
2508 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2510 (defun org-verify-change-for-undo (l1 l2)
2511 "Verify that a real change occurred between the undo lists L1 and L2."
2512 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2513 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2514 (not (eq l1 l2)))
2516 ;;; Agenda dispatch
2518 (defvar org-agenda-restrict-begin (make-marker))
2519 (defvar org-agenda-restrict-end (make-marker))
2520 (defvar org-agenda-last-dispatch-buffer nil)
2521 (defvar org-agenda-overriding-restriction nil)
2523 (defcustom org-agenda-custom-commands-contexts nil
2524 "Alist of custom agenda keys and contextual rules.
2526 For example, if you have a custom agenda command \"p\" and you
2527 want this command to be accessible only from plain text files,
2528 use this:
2530 '((\"p\" ((in-file . \"\\.txt\"))))
2532 Here are the available contexts definitions:
2534 in-file: command displayed only in matching files
2535 in-mode: command displayed only in matching modes
2536 not-in-file: command not displayed in matching files
2537 not-in-mode: command not displayed in matching modes
2538 in-buffer: command displayed only in matching buffers
2539 not-in-buffer: command not displayed in matching buffers
2540 [function]: a custom function taking no argument
2542 If you define several checks, the agenda command will be
2543 accessible if there is at least one valid check.
2545 You can also bind a key to another agenda custom command
2546 depending on contextual rules.
2548 '((\"p\" \"q\" ((in-file . \"\\.txt\"))))
2550 Here it means: in .txt files, use \"p\" as the key for the
2551 agenda command otherwise associated with \"q\". (The command
2552 originally associated with \"q\" is not displayed to avoid
2553 duplicates.)"
2554 :version "24.3"
2555 :group 'org-agenda-custom-commands
2556 :type '(repeat (list :tag "Rule"
2557 (string :tag " Agenda key")
2558 (string :tag "Replace by command")
2559 (repeat :tag "Available when"
2560 (choice
2561 (cons :tag "Condition"
2562 (choice
2563 (const :tag "In file" in-file)
2564 (const :tag "Not in file" not-in-file)
2565 (const :tag "In buffer" in-buffer)
2566 (const :tag "Not in buffer" not-in-buffer)
2567 (const :tag "In mode" in-mode)
2568 (const :tag "Not in mode" not-in-mode))
2569 (regexp))
2570 (function :tag "Custom function"))))))
2572 (defcustom org-agenda-max-entries nil
2573 "Maximum number of entries to display in an agenda.
2574 This can be nil (no limit) or an integer or an alist of agenda
2575 types with an associated number of entries to display in this
2576 type."
2577 :version "24.4"
2578 :package-version '(Org . "8.0")
2579 :group 'org-agenda-custom-commands
2580 :type '(choice (symbol :tag "No limit" nil)
2581 (integer :tag "Max number of entries")
2582 (repeat
2583 (cons (choice :tag "Agenda type"
2584 (const agenda)
2585 (const todo)
2586 (const tags)
2587 (const search)
2588 (const timeline))
2589 (integer :tag "Max number of entries")))))
2591 (defcustom org-agenda-max-todos nil
2592 "Maximum number of TODOs to display in an agenda.
2593 This can be nil (no limit) or an integer or an alist of agenda
2594 types with an associated number of entries to display in this
2595 type."
2596 :version "24.4"
2597 :package-version '(Org . "8.0")
2598 :group 'org-agenda-custom-commands
2599 :type '(choice (symbol :tag "No limit" nil)
2600 (integer :tag "Max number of entries")
2601 (repeat
2602 (cons (choice :tag "Agenda type"
2603 (const agenda)
2604 (const todo)
2605 (const tags)
2606 (const search)
2607 (const timeline))
2608 (integer :tag "Max number of entries")))))
2610 (defcustom org-agenda-max-tags nil
2611 "Maximum number of tagged entries to display in an agenda.
2612 This can be nil (no limit) or an integer or an alist of agenda
2613 types with an associated number of entries to display in this
2614 type."
2615 :version "24.4"
2616 :package-version '(Org . "8.0")
2617 :group 'org-agenda-custom-commands
2618 :type '(choice (symbol :tag "No limit" nil)
2619 (integer :tag "Max number of entries")
2620 (repeat
2621 (cons (choice :tag "Agenda type"
2622 (const agenda)
2623 (const todo)
2624 (const tags)
2625 (const search)
2626 (const timeline))
2627 (integer :tag "Max number of entries")))))
2629 (defcustom org-agenda-max-effort nil
2630 "Maximum cumulated effort duration for the agenda.
2631 This can be nil (no limit) or a number of minutes (as an integer)
2632 or an alist of agenda types with an associated number of minutes
2633 to limit entries to in this type."
2634 :version "24.4"
2635 :package-version '(Org . "8.0")
2636 :group 'org-agenda-custom-commands
2637 :type '(choice (symbol :tag "No limit" nil)
2638 (integer :tag "Max number of entries")
2639 (repeat
2640 (cons (choice :tag "Agenda type"
2641 (const agenda)
2642 (const todo)
2643 (const tags)
2644 (const search)
2645 (const timeline))
2646 (integer :tag "Max number of entries")))))
2648 (defvar org-keys nil)
2649 (defvar org-match nil)
2650 ;;;###autoload
2651 (defun org-agenda (&optional arg org-keys restriction)
2652 "Dispatch agenda commands to collect entries to the agenda buffer.
2653 Prompts for a command to execute. Any prefix arg will be passed
2654 on to the selected command. The default selections are:
2656 a Call `org-agenda-list' to display the agenda for current day or week.
2657 t Call `org-todo-list' to display the global todo list.
2658 T Call `org-todo-list' to display the global todo list, select only
2659 entries with a specific TODO keyword (the user gets a prompt).
2660 m Call `org-tags-view' to display headlines with tags matching
2661 a condition (the user is prompted for the condition).
2662 M Like `m', but select only TODO entries, no ordinary headlines.
2663 L Create a timeline for the current buffer.
2664 e Export views to associated files.
2665 s Search entries for keywords.
2666 S Search entries for keywords, only with TODO keywords.
2667 / Multi occur across all agenda files and also files listed
2668 in `org-agenda-text-search-extra-files'.
2669 < Restrict agenda commands to buffer, subtree, or region.
2670 Press several times to get the desired effect.
2671 > Remove a previous restriction.
2672 # List \"stuck\" projects.
2673 ! Configure what \"stuck\" means.
2674 C Configure custom agenda commands.
2676 More commands can be added by configuring the variable
2677 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2678 searches can be pre-defined in this way.
2680 If the current buffer is in Org-mode and visiting a file, you can also
2681 first press `<' once to indicate that the agenda should be temporarily
2682 \(until the next use of \\[org-agenda]) restricted to the current file.
2683 Pressing `<' twice means to restrict to the current subtree or region
2684 \(if active)."
2685 (interactive "P")
2686 (catch 'exit
2687 (let* ((prefix-descriptions nil)
2688 (org-agenda-buffer-name org-agenda-buffer-name)
2689 (org-agenda-window-setup (if (equal (buffer-name)
2690 org-agenda-buffer-name)
2691 'current-window
2692 org-agenda-window-setup))
2693 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2694 (org-agenda-custom-commands
2695 ;; normalize different versions
2696 (delq nil
2697 (mapcar
2698 (lambda (x)
2699 (cond ((stringp (cdr x))
2700 (push x prefix-descriptions)
2701 nil)
2702 ((stringp (nth 1 x)) x)
2703 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2704 (t (cons (car x) (cons "" (cdr x))))))
2705 org-agenda-custom-commands)))
2706 (org-agenda-custom-commands
2707 (org-contextualize-keys
2708 org-agenda-custom-commands org-agenda-custom-commands-contexts))
2709 (buf (current-buffer))
2710 (bfn (buffer-file-name (buffer-base-buffer)))
2711 entry key type org-match lprops ans)
2712 ;; Turn off restriction unless there is an overriding one,
2713 (unless org-agenda-overriding-restriction
2714 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2715 ;; There is a request to keep the file list in place
2716 (put 'org-agenda-files 'org-restrict nil))
2717 (setq org-agenda-restrict nil)
2718 (move-marker org-agenda-restrict-begin nil)
2719 (move-marker org-agenda-restrict-end nil))
2720 ;; Delete old local properties
2721 (put 'org-agenda-redo-command 'org-lprops nil)
2722 ;; Delete previously set last-arguments
2723 (put 'org-agenda-redo-command 'last-args nil)
2724 ;; Remember where this call originated
2725 (setq org-agenda-last-dispatch-buffer (current-buffer))
2726 (unless org-keys
2727 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2728 org-keys (car ans)
2729 restriction (cdr ans)))
2730 ;; If we have sticky agenda buffers, set a name for the buffer,
2731 ;; depending on the invoking keys. The user may still set this
2732 ;; as a command option, which will overwrite what we do here.
2733 (if org-agenda-sticky
2734 (setq org-agenda-buffer-name
2735 (format "*Org Agenda(%s)*" org-keys)))
2736 ;; Establish the restriction, if any
2737 (when (and (not org-agenda-overriding-restriction) restriction)
2738 (put 'org-agenda-files 'org-restrict (list bfn))
2739 (cond
2740 ((eq restriction 'region)
2741 (setq org-agenda-restrict (current-buffer))
2742 (move-marker org-agenda-restrict-begin (region-beginning))
2743 (move-marker org-agenda-restrict-end (region-end)))
2744 ((eq restriction 'subtree)
2745 (save-excursion
2746 (setq org-agenda-restrict (current-buffer))
2747 (org-back-to-heading t)
2748 (move-marker org-agenda-restrict-begin (point))
2749 (move-marker org-agenda-restrict-end
2750 (progn (org-end-of-subtree t)))))))
2752 ;; For example the todo list should not need it (but does...)
2753 (cond
2754 ((setq entry (assoc org-keys org-agenda-custom-commands))
2755 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2756 (progn
2757 (setq type (nth 2 entry) org-match (eval (nth 3 entry))
2758 lprops (nth 4 entry))
2759 (if org-agenda-sticky
2760 (setq org-agenda-buffer-name
2761 (or (and (stringp org-match) (format "*Org Agenda(%s:%s)*" org-keys org-match))
2762 (format "*Org Agenda(%s)*" org-keys))))
2763 (put 'org-agenda-redo-command 'org-lprops lprops)
2764 (cond
2765 ((eq type 'agenda)
2766 (org-let lprops '(org-agenda-list current-prefix-arg)))
2767 ((eq type 'agenda*)
2768 (org-let lprops '(org-agenda-list current-prefix-arg nil nil t)))
2769 ((eq type 'alltodo)
2770 (org-let lprops '(org-todo-list current-prefix-arg)))
2771 ((eq type 'search)
2772 (org-let lprops '(org-search-view current-prefix-arg org-match nil)))
2773 ((eq type 'stuck)
2774 (org-let lprops '(org-agenda-list-stuck-projects
2775 current-prefix-arg)))
2776 ((eq type 'tags)
2777 (org-let lprops '(org-tags-view current-prefix-arg org-match)))
2778 ((eq type 'tags-todo)
2779 (org-let lprops '(org-tags-view '(4) org-match)))
2780 ((eq type 'todo)
2781 (org-let lprops '(org-todo-list org-match)))
2782 ((eq type 'tags-tree)
2783 (org-check-for-org-mode)
2784 (org-let lprops '(org-match-sparse-tree current-prefix-arg org-match)))
2785 ((eq type 'todo-tree)
2786 (org-check-for-org-mode)
2787 (org-let lprops
2788 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2789 (regexp-quote org-match) "\\>"))))
2790 ((eq type 'occur-tree)
2791 (org-check-for-org-mode)
2792 (org-let lprops '(org-occur org-match)))
2793 ((functionp type)
2794 (org-let lprops '(funcall type org-match)))
2795 ((fboundp type)
2796 (org-let lprops '(funcall type org-match)))
2797 (t (user-error "Invalid custom agenda command type %s" type))))
2798 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2799 ((equal org-keys "C")
2800 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2801 (customize-variable 'org-agenda-custom-commands))
2802 ((equal org-keys "a") (call-interactively 'org-agenda-list))
2803 ((equal org-keys "s") (call-interactively 'org-search-view))
2804 ((equal org-keys "S") (org-call-with-arg 'org-search-view (or arg '(4))))
2805 ((equal org-keys "t") (call-interactively 'org-todo-list))
2806 ((equal org-keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2807 ((equal org-keys "m") (call-interactively 'org-tags-view))
2808 ((equal org-keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2809 ((equal org-keys "e") (call-interactively 'org-store-agenda-views))
2810 ((equal org-keys "?") (org-tags-view nil "+FLAGGED")
2811 (org-add-hook
2812 'post-command-hook
2813 (lambda ()
2814 (unless (current-message)
2815 (let* ((m (org-agenda-get-any-marker))
2816 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2817 (when note
2818 (message (concat
2819 "FLAGGING-NOTE ([?] for more info): "
2820 (org-add-props
2821 (replace-regexp-in-string
2822 "\\\\n" "//"
2823 (copy-sequence note))
2824 nil 'face 'org-warning)))))))
2825 t t))
2826 ((equal org-keys "L")
2827 (unless (derived-mode-p 'org-mode)
2828 (user-error "This is not an Org-mode file"))
2829 (unless restriction
2830 (put 'org-agenda-files 'org-restrict (list bfn))
2831 (org-call-with-arg 'org-timeline arg)))
2832 ((equal org-keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2833 ((equal org-keys "/") (call-interactively 'org-occur-in-agenda-files))
2834 ((equal org-keys "!") (customize-variable 'org-stuck-projects))
2835 (t (user-error "Invalid agenda key"))))))
2837 (defun org-agenda-append-agenda ()
2838 "Append another agenda view to the current one.
2839 This function allows interactive building of block agendas.
2840 Agenda views are separated by `org-agenda-block-separator'."
2841 (interactive)
2842 (unless (derived-mode-p 'org-agenda-mode)
2843 (user-error "Can only append from within agenda buffer"))
2844 (let ((org-agenda-multi t))
2845 (org-agenda)
2846 (widen)
2847 (org-agenda-finalize)
2848 (setq buffer-read-only t)
2849 (org-agenda-fit-window-to-buffer)))
2851 (defun org-agenda-normalize-custom-commands (cmds)
2852 "Normalize custom commands CMDS."
2853 (delq nil
2854 (mapcar
2855 (lambda (x)
2856 (cond ((stringp (cdr x)) nil)
2857 ((stringp (nth 1 x)) x)
2858 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2859 (t (cons (car x) (cons "" (cdr x))))))
2860 cmds)))
2862 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2863 "The user interface for selecting an agenda command."
2864 (catch 'exit
2865 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2866 (restrict-ok (and bfn (derived-mode-p 'org-mode)))
2867 (region-p (org-region-active-p))
2868 (custom org-agenda-custom-commands)
2869 (selstring "")
2870 restriction second-time
2871 c entry key type match prefixes rmheader header-end custom1 desc
2872 line lines left right n n1)
2873 (save-window-excursion
2874 (delete-other-windows)
2875 (org-switch-to-buffer-other-window " *Agenda Commands*")
2876 (erase-buffer)
2877 (insert (eval-when-compile
2878 (let ((header
2879 "Press key for an agenda command: < Buffer, subtree/region restriction
2880 -------------------------------- > Remove restriction
2881 a Agenda for current week or day e Export agenda views
2882 t List of all TODO entries T Entries with special TODO kwd
2883 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2884 s Search for keywords S Like s, but only TODO entries
2885 L Timeline for current buffer # List stuck projects (!=configure)
2886 / Multi-occur C Configure custom agenda commands
2887 ? Find :FLAGGED: entries * Toggle sticky agenda views
2889 (start 0))
2890 (while (string-match
2891 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2892 header start)
2893 (setq start (match-end 0))
2894 (add-text-properties (match-beginning 2) (match-end 2)
2895 '(face bold) header))
2896 header)))
2897 (setq header-end (point-marker))
2898 (while t
2899 (setq custom1 custom)
2900 (when (eq rmheader t)
2901 (org-goto-line 1)
2902 (re-search-forward ":" nil t)
2903 (delete-region (match-end 0) (point-at-eol))
2904 (forward-char 1)
2905 (looking-at "-+")
2906 (delete-region (match-end 0) (point-at-eol))
2907 (move-marker header-end (match-end 0)))
2908 (goto-char header-end)
2909 (delete-region (point) (point-max))
2911 ;; Produce all the lines that describe custom commands and prefixes
2912 (setq lines nil)
2913 (while (setq entry (pop custom1))
2914 (setq key (car entry) desc (nth 1 entry)
2915 type (nth 2 entry)
2916 match (nth 3 entry))
2917 (if (> (length key) 1)
2918 (add-to-list 'prefixes (string-to-char key))
2919 (setq line
2920 (format
2921 "%-4s%-14s"
2922 (org-add-props (copy-sequence key)
2923 '(face bold))
2924 (cond
2925 ((string-match "\\S-" desc) desc)
2926 ((eq type 'agenda) "Agenda for current week or day")
2927 ((eq type 'agenda*) "Appointments for current week or day")
2928 ((eq type 'alltodo) "List of all TODO entries")
2929 ((eq type 'search) "Word search")
2930 ((eq type 'stuck) "List of stuck projects")
2931 ((eq type 'todo) "TODO keyword")
2932 ((eq type 'tags) "Tags query")
2933 ((eq type 'tags-todo) "Tags (TODO)")
2934 ((eq type 'tags-tree) "Tags tree")
2935 ((eq type 'todo-tree) "TODO kwd tree")
2936 ((eq type 'occur-tree) "Occur tree")
2937 ((functionp type) (if (symbolp type)
2938 (symbol-name type)
2939 "Lambda expression"))
2940 (t "???"))))
2941 (if org-agenda-menu-show-matcher
2942 (setq line
2943 (concat line ": "
2944 (cond
2945 ((stringp match)
2946 (setq match (copy-sequence match))
2947 (org-add-props match nil 'face 'org-warning))
2948 ((listp type)
2949 (format "set of %d commands" (length type))))))
2950 (if (org-string-nw-p match)
2951 (add-text-properties
2952 0 (length line) (list 'help-echo
2953 (concat "Matcher: " match)) line)))
2954 (push line lines)))
2955 (setq lines (nreverse lines))
2956 (when prefixes
2957 (mapc (lambda (x)
2958 (push
2959 (format "%s %s"
2960 (org-add-props (char-to-string x)
2961 nil 'face 'bold)
2962 (or (cdr (assoc (concat selstring
2963 (char-to-string x))
2964 prefix-descriptions))
2965 "Prefix key"))
2966 lines))
2967 prefixes))
2969 ;; Check if we should display in two columns
2970 (if org-agenda-menu-two-columns
2971 (progn
2972 (setq n (length lines)
2973 n1 (+ (/ n 2) (mod n 2))
2974 right (nthcdr n1 lines)
2975 left (copy-sequence lines))
2976 (setcdr (nthcdr (1- n1) left) nil))
2977 (setq left lines right nil))
2978 (while left
2979 (insert "\n" (pop left))
2980 (when right
2981 (if (< (current-column) 40)
2982 (move-to-column 40 t)
2983 (insert " "))
2984 (insert (pop right))))
2986 ;; Make the window the right size
2987 (goto-char (point-min))
2988 (if second-time
2989 (if (not (pos-visible-in-window-p (point-max)))
2990 (org-fit-window-to-buffer))
2991 (setq second-time t)
2992 (org-fit-window-to-buffer))
2994 ;; Ask for selection
2995 (message "Press key for agenda command%s:"
2996 (if (or restrict-ok org-agenda-overriding-restriction)
2997 (if org-agenda-overriding-restriction
2998 " (restriction lock active)"
2999 (if restriction
3000 (format " (restricted to %s)" restriction)
3001 " (unrestricted)"))
3002 ""))
3003 (setq c (read-char-exclusive))
3004 (message "")
3005 (cond
3006 ((assoc (char-to-string c) custom)
3007 (setq selstring (concat selstring (char-to-string c)))
3008 (throw 'exit (cons selstring restriction)))
3009 ((memq c prefixes)
3010 (setq selstring (concat selstring (char-to-string c))
3011 prefixes nil
3012 rmheader (or rmheader t)
3013 custom (delq nil (mapcar
3014 (lambda (x)
3015 (if (or (= (length (car x)) 1)
3016 (/= (string-to-char (car x)) c))
3018 (cons (substring (car x) 1) (cdr x))))
3019 custom))))
3020 ((eq c ?*)
3021 (call-interactively 'org-toggle-sticky-agenda)
3022 (sit-for 2))
3023 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
3024 (message "Restriction is only possible in Org-mode buffers")
3025 (ding) (sit-for 1))
3026 ((eq c ?1)
3027 (org-agenda-remove-restriction-lock 'noupdate)
3028 (setq restriction 'buffer))
3029 ((eq c ?0)
3030 (org-agenda-remove-restriction-lock 'noupdate)
3031 (setq restriction (if region-p 'region 'subtree)))
3032 ((eq c ?<)
3033 (org-agenda-remove-restriction-lock 'noupdate)
3034 (setq restriction
3035 (cond
3036 ((eq restriction 'buffer)
3037 (if region-p 'region 'subtree))
3038 ((memq restriction '(subtree region))
3039 nil)
3040 (t 'buffer))))
3041 ((eq c ?>)
3042 (org-agenda-remove-restriction-lock 'noupdate)
3043 (setq restriction nil))
3044 ((and (equal selstring "") (memq c '(?s ?S ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
3045 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
3046 ((and (> (length selstring) 0) (eq c ?\d))
3047 (delete-window)
3048 (org-agenda-get-restriction-and-command prefix-descriptions))
3050 ((equal c ?q) (error "Abort"))
3051 (t (user-error "Invalid key %c" c))))))))
3053 (defun org-agenda-fit-window-to-buffer ()
3054 "Fit the window to the buffer size."
3055 (and (memq org-agenda-window-setup '(reorganize-frame))
3056 (fboundp 'fit-window-to-buffer)
3057 (org-fit-window-to-buffer
3059 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3060 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3062 (defvar org-cmd nil)
3063 (defvar org-agenda-overriding-cmd nil)
3064 (defvar org-agenda-overriding-arguments nil)
3065 (defvar org-agenda-overriding-cmd-arguments nil)
3066 (defun org-agenda-run-series (name series)
3067 "Run agenda NAME as a SERIES of agenda commands."
3068 (org-let (nth 1 series) '(org-agenda-prepare name))
3069 ;; We need to reset agenda markers here, because when constructing a
3070 ;; block agenda, the individual blocks do not do that.
3071 (org-agenda-reset-markers)
3072 (let* ((org-agenda-multi t)
3073 (redo (list 'org-agenda-run-series name (list 'quote series)))
3074 (cmds (car series))
3075 (gprops (nth 1 series))
3076 match ;; The byte compiler incorrectly complains about this. Keep it!
3077 org-cmd type lprops)
3078 (while (setq org-cmd (pop cmds))
3079 (setq type (car org-cmd)
3080 match (eval (nth 1 org-cmd))
3081 lprops (nth 2 org-cmd))
3082 (let ((org-agenda-overriding-arguments
3083 (if (eq org-agenda-overriding-cmd org-cmd)
3084 (or org-agenda-overriding-arguments
3085 org-agenda-overriding-cmd-arguments))))
3086 (cond
3087 ((eq type 'agenda)
3088 (org-let2 gprops lprops
3089 '(call-interactively 'org-agenda-list)))
3090 ((eq type 'agenda*)
3091 (org-let2 gprops lprops
3092 '(funcall 'org-agenda-list nil nil t)))
3093 ((eq type 'alltodo)
3094 (org-let2 gprops lprops
3095 '(call-interactively 'org-todo-list)))
3096 ((eq type 'search)
3097 (org-let2 gprops lprops
3098 '(org-search-view current-prefix-arg match nil)))
3099 ((eq type 'stuck)
3100 (org-let2 gprops lprops
3101 '(call-interactively 'org-agenda-list-stuck-projects)))
3102 ((eq type 'tags)
3103 (org-let2 gprops lprops
3104 '(org-tags-view current-prefix-arg match)))
3105 ((eq type 'tags-todo)
3106 (org-let2 gprops lprops
3107 '(org-tags-view '(4) match)))
3108 ((eq type 'todo)
3109 (org-let2 gprops lprops
3110 '(org-todo-list match)))
3111 ((fboundp type)
3112 (org-let2 gprops lprops
3113 '(funcall type match)))
3114 (t (error "Invalid type in command series")))))
3115 (widen)
3116 (let ((inhibit-read-only t))
3117 (add-text-properties (point-min) (point-max)
3118 `(org-series t org-series-redo-cmd ,redo)))
3119 (setq org-agenda-redo-command redo)
3120 (goto-char (point-min)))
3121 (org-agenda-fit-window-to-buffer)
3122 (org-let (nth 1 series) '(org-agenda-finalize)))
3124 ;;;###autoload
3125 (defmacro org-batch-agenda (cmd-key &rest parameters)
3126 "Run an agenda command in batch mode and send the result to STDOUT.
3127 If CMD-KEY is a string of length 1, it is used as a key in
3128 `org-agenda-custom-commands' and triggers this command. If it is a
3129 longer string it is used as a tags/todo match string.
3130 Parameters are alternating variable names and values that will be bound
3131 before running the agenda command."
3132 (org-eval-in-environment (org-make-parameter-alist parameters)
3133 (let (org-agenda-sticky)
3134 (if (> (length cmd-key) 2)
3135 (org-tags-view nil cmd-key)
3136 (org-agenda nil cmd-key))))
3137 (set-buffer org-agenda-buffer-name)
3138 (princ (buffer-string)))
3140 (defvar org-agenda-info nil)
3142 ;;;###autoload
3143 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
3144 "Run an agenda command in batch mode and send the result to STDOUT.
3145 If CMD-KEY is a string of length 1, it is used as a key in
3146 `org-agenda-custom-commands' and triggers this command. If it is a
3147 longer string it is used as a tags/todo match string.
3148 Parameters are alternating variable names and values that will be bound
3149 before running the agenda command.
3151 The output gives a line for each selected agenda item. Each
3152 item is a list of comma-separated values, like this:
3154 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
3156 category The category of the item
3157 head The headline, without TODO kwd, TAGS and PRIORITY
3158 type The type of the agenda entry, can be
3159 todo selected in TODO match
3160 tagsmatch selected in tags match
3161 diary imported from diary
3162 deadline a deadline on given date
3163 scheduled scheduled on given date
3164 timestamp entry has timestamp on given date
3165 closed entry was closed on given date
3166 upcoming-deadline warning about deadline
3167 past-scheduled forwarded scheduled item
3168 block entry has date block including g. date
3169 todo The todo keyword, if any
3170 tags All tags including inherited ones, separated by colons
3171 date The relevant date, like 2007-2-14
3172 time The time, like 15:00-16:50
3173 extra Sting with extra planning info
3174 priority-l The priority letter if any was given
3175 priority-n The computed numerical priority
3176 agenda-day The day in the agenda where this is listed"
3177 (org-eval-in-environment (append '((org-agenda-remove-tags t))
3178 (org-make-parameter-alist parameters))
3179 (if (> (length cmd-key) 2)
3180 (org-tags-view nil cmd-key)
3181 (org-agenda nil cmd-key)))
3182 (set-buffer org-agenda-buffer-name)
3183 (let* ((lines (org-split-string (buffer-string) "\n"))
3184 line)
3185 (while (setq line (pop lines))
3186 (catch 'next
3187 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
3188 (setq org-agenda-info
3189 (org-fix-agenda-info (text-properties-at 0 line)))
3190 (princ
3191 (mapconcat 'org-agenda-export-csv-mapper
3192 '(org-category txt type todo tags date time extra
3193 priority-letter priority agenda-day)
3194 ","))
3195 (princ "\n")))))
3197 (defun org-fix-agenda-info (props)
3198 "Make sure all properties on an agenda item have a canonical form.
3199 This ensures the export commands can easily use it."
3200 (let (tmp re)
3201 (when (setq tmp (plist-get props 'tags))
3202 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
3203 (when (setq tmp (plist-get props 'date))
3204 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3205 (let ((calendar-date-display-form '(year "-" month "-" day)))
3206 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
3208 (setq tmp (calendar-date-string tmp)))
3209 (setq props (plist-put props 'date tmp)))
3210 (when (setq tmp (plist-get props 'day))
3211 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
3212 (let ((calendar-date-display-form '(year "-" month "-" day)))
3213 (setq tmp (calendar-date-string tmp)))
3214 (setq props (plist-put props 'day tmp))
3215 (setq props (plist-put props 'agenda-day tmp)))
3216 (when (setq tmp (plist-get props 'txt))
3217 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
3218 (plist-put props 'priority-letter (match-string 1 tmp))
3219 (setq tmp (replace-match "" t t tmp)))
3220 (when (and (setq re (plist-get props 'org-todo-regexp))
3221 (setq re (concat "\\`\\.*" re " ?"))
3222 (string-match re tmp))
3223 (plist-put props 'todo (match-string 1 tmp))
3224 (setq tmp (replace-match "" t t tmp)))
3225 (plist-put props 'txt tmp)))
3226 props)
3228 (defun org-agenda-export-csv-mapper (prop)
3229 (let ((res (plist-get org-agenda-info prop)))
3230 (setq res
3231 (cond
3232 ((not res) "")
3233 ((stringp res) res)
3234 (t (prin1-to-string res))))
3235 (while (string-match "," res)
3236 (setq res (replace-match ";" t t res)))
3237 (org-trim res)))
3239 ;;;###autoload
3240 (defun org-store-agenda-views (&rest parameters)
3241 "Store agenda views."
3242 (interactive)
3243 (eval (list 'org-batch-store-agenda-views)))
3245 ;;;###autoload
3246 (defmacro org-batch-store-agenda-views (&rest parameters)
3247 "Run all custom agenda commands that have a file argument."
3248 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
3249 (pop-up-frames nil)
3250 (dir default-directory)
3251 (pars (org-make-parameter-alist parameters))
3252 cmd thiscmdkey thiscmdcmd match files opts cmd-or-set bufname)
3253 (save-window-excursion
3254 (while cmds
3255 (setq cmd (pop cmds)
3256 thiscmdkey (car cmd)
3257 thiscmdcmd (cdr cmd)
3258 match (nth 2 thiscmdcmd)
3259 bufname (if org-agenda-sticky
3260 (or (and (stringp match)
3261 (format "*Org Agenda(%s:%s)*" thiscmdkey match))
3262 (format "*Org Agenda(%s)*" thiscmdkey))
3263 org-agenda-buffer-name)
3264 cmd-or-set (nth 2 cmd)
3265 opts (nth (if (listp cmd-or-set) 3 4) cmd)
3266 files (nth (if (listp cmd-or-set) 4 5) cmd))
3267 (if (stringp files) (setq files (list files)))
3268 (when files
3269 (org-eval-in-environment (append org-agenda-exporter-settings
3270 opts pars)
3271 (org-agenda nil thiscmdkey))
3272 (set-buffer bufname)
3273 (while files
3274 (org-eval-in-environment (append org-agenda-exporter-settings
3275 opts pars)
3276 (org-agenda-write (expand-file-name (pop files) dir) nil t bufname)))
3277 (and (get-buffer bufname)
3278 (kill-buffer bufname)))))))
3280 (defvar org-agenda-current-span nil
3281 "The current span used in the agenda view.") ; local variable in the agenda buffer
3282 (defun org-agenda-mark-header-line (pos)
3283 "Mark the line at POS as an agenda structure header."
3284 (save-excursion
3285 (goto-char pos)
3286 (put-text-property (point-at-bol) (point-at-eol)
3287 'org-agenda-structural-header t)
3288 (when org-agenda-title-append
3289 (put-text-property (point-at-bol) (point-at-eol)
3290 'org-agenda-title-append org-agenda-title-append))))
3292 (defvar org-mobile-creating-agendas) ; defined in org-mobile.el
3293 (defvar org-agenda-write-buffer-name "Agenda View")
3294 (defun org-agenda-write (file &optional open nosettings agenda-bufname)
3295 "Write the current buffer (an agenda view) as a file.
3296 Depending on the extension of the file name, plain text (.txt),
3297 HTML (.html or .htm), PDF (.pdf) or Postscript (.ps) is produced.
3298 If the extension is .ics, run icalendar export over all files used
3299 to construct the agenda and limit the export to entries listed in the
3300 agenda now.
3301 If the extension is .org, collect all subtrees corresponding to the
3302 agenda entries and add them in an .org file.
3303 With prefix argument OPEN, open the new file immediately.
3304 If NOSETTINGS is given, do not scope the settings of
3305 `org-agenda-exporter-settings' into the export commands. This is used when
3306 the settings have already been scoped and we do not wish to overrule other,
3307 higher priority settings.
3308 If AGENDA-BUFFER-NAME, use this as the buffer name for the agenda to write."
3309 (interactive "FWrite agenda to file: \nP")
3310 (if (or (not (file-writable-p file))
3311 (and (file-exists-p file)
3312 (if (org-called-interactively-p 'any)
3313 (not (y-or-n-p (format "Overwrite existing file %s? " file))))))
3314 (user-error "Cannot write agenda to file %s" file))
3315 (org-let (if nosettings nil org-agenda-exporter-settings)
3316 '(save-excursion
3317 (save-window-excursion
3318 (org-agenda-mark-filtered-text)
3319 (let ((bs (copy-sequence (buffer-string))) beg content)
3320 (org-agenda-unmark-filtered-text)
3321 (with-temp-buffer
3322 (rename-buffer org-agenda-write-buffer-name t)
3323 (set-buffer-modified-p nil)
3324 (insert bs)
3325 (org-agenda-remove-marked-text 'org-filtered)
3326 (while (setq beg (text-property-any (point-min) (point-max)
3327 'org-filtered t))
3328 (delete-region
3329 beg (or (next-single-property-change beg 'org-filtered)
3330 (point-max))))
3331 (run-hooks 'org-agenda-before-write-hook)
3332 (cond
3333 ((org-bound-and-true-p org-mobile-creating-agendas)
3334 (org-mobile-write-agenda-for-mobile file))
3335 ((string-match "\\.org\\'" file)
3336 (let (content p m message-log-max)
3337 (goto-char (point-min))
3338 (while (setq p (next-single-property-change (point) 'org-hd-marker nil))
3339 (goto-char p)
3340 (setq m (get-text-property (point) 'org-hd-marker))
3341 (when m
3342 (push (save-excursion
3343 (set-buffer (marker-buffer m))
3344 (goto-char m)
3345 (org-copy-subtree 1 nil t t)
3346 org-subtree-clip)
3347 content)))
3348 (find-file file)
3349 (erase-buffer)
3350 (mapcar (lambda (s) (org-paste-subtree 1 s)) (reverse content))
3351 (write-file file)
3352 (kill-buffer (current-buffer))
3353 (message "Org file written to %s" file)))
3354 ((string-match "\\.html?\\'" file)
3355 (require 'htmlize)
3356 (set-buffer (htmlize-buffer (current-buffer)))
3357 (when org-agenda-export-html-style
3358 ;; replace <style> section with org-agenda-export-html-style
3359 (goto-char (point-min))
3360 (kill-region (- (search-forward "<style") 6)
3361 (search-forward "</style>"))
3362 (insert org-agenda-export-html-style))
3363 (write-file file)
3364 (kill-buffer (current-buffer))
3365 (message "HTML written to %s" file))
3366 ((string-match "\\.ps\\'" file)
3367 (require 'ps-print)
3368 (ps-print-buffer-with-faces file)
3369 (message "Postscript written to %s" file))
3370 ((string-match "\\.pdf\\'" file)
3371 (require 'ps-print)
3372 (ps-print-buffer-with-faces
3373 (concat (file-name-sans-extension file) ".ps"))
3374 (call-process "ps2pdf" nil nil nil
3375 (expand-file-name
3376 (concat (file-name-sans-extension file) ".ps"))
3377 (expand-file-name file))
3378 (delete-file (concat (file-name-sans-extension file) ".ps"))
3379 (message "PDF written to %s" file))
3380 ((string-match "\\.ics\\'" file)
3381 (require 'ox-icalendar)
3382 (org-icalendar-export-current-agenda (expand-file-name file)))
3384 (let ((bs (buffer-string)))
3385 (find-file file)
3386 (erase-buffer)
3387 (insert bs)
3388 (save-buffer 0)
3389 (kill-buffer (current-buffer))
3390 (message "Plain text written to %s" file))))))))
3391 (set-buffer (or agenda-bufname
3392 (and (org-called-interactively-p 'any) (buffer-name))
3393 org-agenda-buffer-name)))
3394 (when open (org-open-file file)))
3396 (defvar org-agenda-tag-filter-overlays nil)
3397 (defvar org-agenda-cat-filter-overlays nil)
3398 (defvar org-agenda-re-filter-overlays nil)
3400 (defun org-agenda-mark-filtered-text ()
3401 "Mark all text hidden by filtering with a text property."
3402 (let ((inhibit-read-only t))
3403 (mapc
3404 (lambda (o)
3405 (when (equal (overlay-buffer o) (current-buffer))
3406 (put-text-property
3407 (overlay-start o) (overlay-end o)
3408 'org-filtered t)))
3409 (append org-agenda-tag-filter-overlays
3410 org-agenda-cat-filter-overlays
3411 org-agenda-re-filter-overlays))))
3413 (defun org-agenda-unmark-filtered-text ()
3414 "Remove the filtering text property."
3415 (let ((inhibit-read-only t))
3416 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
3418 (defun org-agenda-remove-marked-text (property &optional value)
3419 "Delete all text marked with VALUE of PROPERTY.
3420 VALUE defaults to t."
3421 (let (beg)
3422 (setq value (or value t))
3423 (while (setq beg (text-property-any (point-min) (point-max)
3424 property value))
3425 (delete-region
3426 beg (or (next-single-property-change beg 'org-filtered)
3427 (point-max))))))
3429 (defun org-agenda-add-entry-text ()
3430 "Add entry text to agenda lines.
3431 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
3432 entry text following headings shown in the agenda.
3433 Drawers will be excluded, also the line with scheduling/deadline info."
3434 (when (and (> org-agenda-add-entry-text-maxlines 0)
3435 (not (org-bound-and-true-p org-mobile-creating-agendas)))
3436 (let (m txt)
3437 (goto-char (point-min))
3438 (while (not (eobp))
3439 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
3440 (beginning-of-line 2)
3441 (setq txt (org-agenda-get-some-entry-text
3442 m org-agenda-add-entry-text-maxlines " > "))
3443 (end-of-line 1)
3444 (if (string-match "\\S-" txt)
3445 (insert "\n" txt)
3446 (or (eobp) (forward-char 1))))))))
3448 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
3449 &rest keep)
3450 "Extract entry text from MARKER, at most N-LINES lines.
3451 This will ignore drawers etc, just get the text.
3452 If INDENT is given, prefix every line with this string. If KEEP is
3453 given, it is a list of symbols, defining stuff that should not be
3454 removed from the entry content. Currently only `planning' is allowed here."
3455 (let (txt drawer-re kwd-time-re ind)
3456 (save-excursion
3457 (with-current-buffer (marker-buffer marker)
3458 (if (not (derived-mode-p 'org-mode))
3459 (setq txt "")
3460 (save-excursion
3461 (save-restriction
3462 (widen)
3463 (goto-char marker)
3464 (end-of-line 1)
3465 (setq txt (buffer-substring
3466 (min (1+ (point)) (point-max))
3467 (progn (outline-next-heading) (point)))
3468 drawer-re org-drawer-regexp
3469 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
3470 ".*\n?"))
3471 (with-temp-buffer
3472 (insert txt)
3473 (when org-agenda-add-entry-text-descriptive-links
3474 (goto-char (point-min))
3475 (while (org-activate-bracket-links (point-max))
3476 (add-text-properties (match-beginning 0) (match-end 0)
3477 '(face org-link))))
3478 (goto-char (point-min))
3479 (while (re-search-forward org-bracket-link-regexp (point-max) t)
3480 (set-text-properties (match-beginning 0) (match-end 0)
3481 nil))
3482 (goto-char (point-min))
3483 (while (re-search-forward drawer-re nil t)
3484 (delete-region
3485 (match-beginning 0)
3486 (progn (re-search-forward
3487 "^[ \t]*:END:.*\n?" nil 'move)
3488 (point))))
3489 (unless (member 'planning keep)
3490 (goto-char (point-min))
3491 (while (re-search-forward kwd-time-re nil t)
3492 (replace-match "")))
3493 (goto-char (point-min))
3494 (when org-agenda-entry-text-exclude-regexps
3495 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
3496 (while (setq re (pop re-list))
3497 (goto-char (point-min))
3498 (while (re-search-forward re nil t)
3499 (replace-match "")))))
3500 (goto-char (point-max))
3501 (skip-chars-backward " \t\n")
3502 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
3504 ;; find and remove min common indentation
3505 (goto-char (point-min))
3506 (untabify (point-min) (point-max))
3507 (setq ind (org-get-indentation))
3508 (while (not (eobp))
3509 (unless (looking-at "[ \t]*$")
3510 (setq ind (min ind (org-get-indentation))))
3511 (beginning-of-line 2))
3512 (goto-char (point-min))
3513 (while (not (eobp))
3514 (unless (looking-at "[ \t]*$")
3515 (move-to-column ind)
3516 (delete-region (point-at-bol) (point)))
3517 (beginning-of-line 2))
3519 (run-hooks 'org-agenda-entry-text-cleanup-hook)
3521 (goto-char (point-min))
3522 (when indent
3523 (while (and (not (eobp)) (re-search-forward "^" nil t))
3524 (replace-match indent t t)))
3525 (goto-char (point-min))
3526 (while (looking-at "[ \t]*\n") (replace-match ""))
3527 (goto-char (point-max))
3528 (when (> (org-current-line)
3529 n-lines)
3530 (org-goto-line (1+ n-lines))
3531 (backward-char 1))
3532 (setq txt (buffer-substring (point-min) (point)))))))))
3533 txt))
3535 (defun org-check-for-org-mode ()
3536 "Make sure current buffer is in org-mode. Error if not."
3537 (or (derived-mode-p 'org-mode)
3538 (error "Cannot execute org-mode agenda command on buffer in %s"
3539 major-mode)))
3541 ;;; Agenda prepare and finalize
3543 (defvar org-agenda-multi nil) ; dynamically scoped
3544 (defvar org-agenda-pre-window-conf nil)
3545 (defvar org-agenda-columns-active nil)
3546 (defvar org-agenda-name nil)
3547 (defvar org-agenda-tag-filter nil)
3548 (defvar org-agenda-category-filter nil)
3549 (defvar org-agenda-regexp-filter nil)
3550 (defvar org-agenda-top-headline-filter nil)
3551 (defvar org-agenda-tag-filter-while-redo nil)
3552 (defvar org-agenda-tag-filter-preset nil
3553 "A preset of the tags filter used for secondary agenda filtering.
3554 This must be a list of strings, each string must be a single tag preceded
3555 by \"+\" or \"-\".
3556 This variable should not be set directly, but agenda custom commands can
3557 bind it in the options section. The preset filter is a global property of
3558 the entire agenda view. In a block agenda, it will not work reliably to
3559 define a filter for one of the individual blocks. You need to set it in
3560 the global options and expect it to be applied to the entire view.")
3562 (defvar org-agenda-category-filter-preset nil
3563 "A preset of the category filter used for secondary agenda filtering.
3564 This must be a list of strings, each string must be a single category
3565 preceded by \"+\" or \"-\".
3566 This variable should not be set directly, but agenda custom commands can
3567 bind it in the options section. The preset filter is a global property of
3568 the entire agenda view. In a block agenda, it will not work reliably to
3569 define a filter for one of the individual blocks. You need to set it in
3570 the global options and expect it to be applied to the entire view.")
3572 (defvar org-agenda-regexp-filter-preset nil
3573 "A preset of the regexp filter used for secondary agenda filtering.
3574 This must be a list of strings, each string must be a single category
3575 preceded by \"+\" or \"-\".
3576 This variable should not be set directly, but agenda custom commands can
3577 bind it in the options section. The preset filter is a global property of
3578 the entire agenda view. In a block agenda, it will not work reliably to
3579 define a filter for one of the individual blocks. You need to set it in
3580 the global options and expect it to be applied to the entire view.")
3582 (defun org-agenda-use-sticky-p ()
3583 "Return non-nil if an agenda buffer named
3584 `org-agenda-buffer-name' exists and should be shown instead of
3585 generating a new one."
3586 (and
3587 ;; turned off by user
3588 org-agenda-sticky
3589 ;; For multi-agenda buffer already exists
3590 (not org-agenda-multi)
3591 ;; buffer found
3592 (get-buffer org-agenda-buffer-name)
3593 ;; C-u parameter is same as last call
3594 (with-current-buffer (get-buffer org-agenda-buffer-name)
3595 (and
3596 (equal current-prefix-arg
3597 org-agenda-last-prefix-arg)
3598 ;; In case user turned stickiness on, while having existing
3599 ;; Agenda buffer active, don't reuse that buffer, because it
3600 ;; does not have org variables local
3601 org-agenda-this-buffer-is-sticky))))
3603 (defun org-agenda-prepare-window (abuf)
3604 "Setup agenda buffer in the window."
3605 (let* ((awin (get-buffer-window abuf))
3606 wconf)
3607 (cond
3608 ((equal (current-buffer) abuf) nil)
3609 (awin (select-window awin))
3610 ((not (setq wconf (current-window-configuration))))
3611 ((equal org-agenda-window-setup 'current-window)
3612 (org-pop-to-buffer-same-window abuf))
3613 ((equal org-agenda-window-setup 'other-window)
3614 (org-switch-to-buffer-other-window abuf))
3615 ((equal org-agenda-window-setup 'other-frame)
3616 (switch-to-buffer-other-frame abuf))
3617 ((equal org-agenda-window-setup 'reorganize-frame)
3618 (delete-other-windows)
3619 (org-switch-to-buffer-other-window abuf)))
3620 ;; additional test in case agenda is invoked from within agenda
3621 ;; buffer via elisp link
3622 (unless (equal (current-buffer) abuf)
3623 (org-pop-to-buffer-same-window abuf))
3624 (setq org-agenda-pre-window-conf
3625 (or org-agenda-pre-window-conf wconf))))
3627 (defun org-agenda-prepare (&optional name)
3628 (if (org-agenda-use-sticky-p)
3629 (progn
3630 ;; Popup existing buffer
3631 (org-agenda-prepare-window (get-buffer org-agenda-buffer-name))
3632 (message "Sticky Agenda buffer, use `r' to refresh")
3633 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
3634 (throw 'exit "Sticky Agenda buffer, use `r' to refresh"))
3635 (setq org-todo-keywords-for-agenda nil)
3636 (setq org-drawers-for-agenda nil)
3637 (unless org-agenda-persistent-filter
3638 (setq org-agenda-tag-filter nil
3639 org-agenda-category-filter nil
3640 org-agenda-regexp-filter nil))
3641 (put 'org-agenda-tag-filter :preset-filter
3642 org-agenda-tag-filter-preset)
3643 (put 'org-agenda-category-filter :preset-filter
3644 org-agenda-category-filter-preset)
3645 (put 'org-agenda-regexp-filter :preset-filter
3646 org-agenda-regexp-filter-preset)
3647 (if org-agenda-multi
3648 (progn
3649 (setq buffer-read-only nil)
3650 (goto-char (point-max))
3651 (unless (or (bobp) org-agenda-compact-blocks
3652 (not org-agenda-block-separator))
3653 (insert "\n"
3654 (if (stringp org-agenda-block-separator)
3655 org-agenda-block-separator
3656 (make-string (window-width) org-agenda-block-separator))
3657 "\n"))
3658 (narrow-to-region (point) (point-max)))
3659 (setq org-done-keywords-for-agenda nil)
3661 ;; Setting any org variables that are in org-agenda-local-vars
3662 ;; list need to be done after the prepare call
3663 (org-agenda-prepare-window (get-buffer-create org-agenda-buffer-name))
3664 (setq buffer-read-only nil)
3665 (org-agenda-reset-markers)
3666 (let ((inhibit-read-only t)) (erase-buffer))
3667 (org-agenda-mode)
3668 (setq org-agenda-buffer (current-buffer))
3669 (setq org-agenda-contributing-files nil)
3670 (setq org-agenda-columns-active nil)
3671 (org-agenda-prepare-buffers (org-agenda-files nil 'ifmode))
3672 (setq org-todo-keywords-for-agenda
3673 (org-uniquify org-todo-keywords-for-agenda))
3674 (setq org-done-keywords-for-agenda
3675 (org-uniquify org-done-keywords-for-agenda))
3676 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3677 (setq org-agenda-last-prefix-arg current-prefix-arg)
3678 (setq org-agenda-this-buffer-name org-agenda-buffer-name)
3679 (and name (not org-agenda-name)
3680 (org-set-local 'org-agenda-name name)))
3681 (setq buffer-read-only nil)))
3683 (defvar org-agenda-overriding-columns-format) ; From org-colview.el
3684 (defun org-agenda-finalize ()
3685 "Finishing touch for the agenda buffer, called just before displaying it."
3686 (unless org-agenda-multi
3687 (save-excursion
3688 (let ((inhibit-read-only t))
3689 (goto-char (point-min))
3690 (save-excursion
3691 (while (org-activate-bracket-links (point-max))
3692 (add-text-properties (match-beginning 0) (match-end 0)
3693 '(face org-link))))
3694 (save-excursion
3695 (while (org-activate-plain-links (point-max))
3696 (add-text-properties (match-beginning 0) (match-end 0)
3697 '(face org-link))))
3698 (unless (eq org-agenda-remove-tags t)
3699 (org-agenda-align-tags))
3700 (unless org-agenda-with-colors
3701 (remove-text-properties (point-min) (point-max) '(face nil)))
3702 (if (and (boundp 'org-agenda-overriding-columns-format)
3703 org-agenda-overriding-columns-format)
3704 (org-set-local 'org-agenda-overriding-columns-format
3705 org-agenda-overriding-columns-format))
3706 (if (and (boundp 'org-agenda-view-columns-initially)
3707 org-agenda-view-columns-initially)
3708 (org-agenda-columns))
3709 (when org-agenda-fontify-priorities
3710 (org-agenda-fontify-priorities))
3711 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3712 (org-agenda-dim-blocked-tasks))
3713 ;; We need to widen when `org-agenda-finalize' is called from
3714 ;; `org-agenda-change-all-lines' (e.g. in `org-agenda-clock-in')
3715 (when org-clock-current-task
3716 (save-restriction
3717 (widen)
3718 (org-agenda-mark-clocking-task)))
3719 (when org-agenda-entry-text-mode
3720 (org-agenda-entry-text-hide)
3721 (org-agenda-entry-text-show))
3722 (if (and (functionp 'org-habit-insert-consistency-graphs)
3723 (save-excursion (next-single-property-change (point-min) 'org-habit-p)))
3724 (org-habit-insert-consistency-graphs))
3725 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3726 (unless (or (eq org-agenda-show-inherited-tags 'always)
3727 (and (listp org-agenda-show-inherited-tags)
3728 (memq org-agenda-type org-agenda-show-inherited-tags))
3729 (and (eq org-agenda-show-inherited-tags t)
3730 (or (eq org-agenda-use-tag-inheritance t)
3731 (and (listp org-agenda-use-tag-inheritance)
3732 (not (memq org-agenda-type
3733 org-agenda-use-tag-inheritance))))))
3734 (let (mrk)
3735 (save-excursion
3736 (goto-char (point-min))
3737 (while (equal (forward-line) 0)
3738 (when (setq mrk (or (get-text-property (point) 'org-hd-marker)
3739 (get-text-property (point) 'org-hd-marker)))
3740 (put-text-property (point-at-bol) (point-at-eol)
3741 'tags (org-with-point-at mrk
3742 (delete-dups
3743 (mapcar 'downcase (org-get-tags-at))))))))))
3744 (run-hooks 'org-agenda-finalize-hook)
3745 (when (or org-agenda-tag-filter (get 'org-agenda-tag-filter :preset-filter))
3746 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
3747 (when (or org-agenda-category-filter (get 'org-agenda-category-filter :preset-filter))
3748 (org-agenda-filter-apply org-agenda-category-filter 'category))
3749 (when (or org-agenda-regexp-filter (get 'org-agenda-regexp-filter :preset-filter))
3750 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
3751 (org-add-hook 'kill-buffer-hook 'org-agenda-reset-markers 'append 'local)))))
3753 (defun org-agenda-mark-clocking-task ()
3754 "Mark the current clock entry in the agenda if it is present."
3755 (org-agenda-unmark-clocking-task)
3756 (when (marker-buffer org-clock-hd-marker)
3757 (save-excursion
3758 (goto-char (point-min))
3759 (let (s ov)
3760 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3761 (goto-char s)
3762 (when (equal (org-get-at-bol 'org-hd-marker)
3763 org-clock-hd-marker)
3764 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3765 (overlay-put ov 'type 'org-agenda-clocking)
3766 (overlay-put ov 'face 'org-agenda-clocking)
3767 (overlay-put ov 'help-echo
3768 "The clock is running in this item")))))))
3770 (defun org-agenda-unmark-clocking-task ()
3771 "Unmark the current clocking task."
3772 (mapc (lambda (o)
3773 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3774 (delete-overlay o)))
3775 (overlays-in (point-min) (point-max))))
3777 (defun org-agenda-fontify-priorities ()
3778 "Make highest priority lines bold, and lowest italic."
3779 (interactive)
3780 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3781 (delete-overlay o)))
3782 (overlays-in (point-min) (point-max)))
3783 (save-excursion
3784 (let (b e p ov h l)
3785 (goto-char (point-min))
3786 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3787 (setq h (or (get-char-property (point) 'org-highest-priority)
3788 org-highest-priority)
3789 l (or (get-char-property (point) 'org-lowest-priority)
3790 org-lowest-priority)
3791 p (string-to-char (match-string 1))
3792 b (match-beginning 0)
3793 e (if (eq org-agenda-fontify-priorities 'cookies)
3794 (match-end 0)
3795 (point-at-eol))
3796 ov (make-overlay b e))
3797 (overlay-put
3798 ov 'face
3799 (cons (cond ((org-face-from-face-or-color
3800 'priority nil
3801 (cdr (assoc p org-priority-faces))))
3802 ((and (listp org-agenda-fontify-priorities)
3803 (org-face-from-face-or-color
3804 'priority nil
3805 (cdr (assoc p org-agenda-fontify-priorities)))))
3806 ((equal p l) 'italic)
3807 ((equal p h) 'bold))
3808 'org-priority))
3809 (overlay-put ov 'org-type 'org-priority)))))
3811 (defun org-agenda-dim-blocked-tasks (&optional invisible)
3812 "Dim currently blocked TODO's in the agenda display.
3813 When INVISIBLE is non-nil, hide currently blocked TODO instead of
3814 dimming them."
3815 (interactive "P")
3816 (when (org-called-interactively-p 'interactive)
3817 (message "Dim or hide blocked tasks..."))
3818 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3819 (delete-overlay o)))
3820 (overlays-in (point-min) (point-max)))
3821 (save-excursion
3822 (let ((inhibit-read-only t)
3823 (org-depend-tag-blocked nil)
3824 (invis (or (not (null invisible))
3825 (eq org-agenda-dim-blocked-tasks 'invisible)))
3826 org-blocked-by-checkboxes
3827 invis1 b e p ov h l)
3828 (goto-char (point-min))
3829 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3830 (and pos (goto-char (1+ pos))))
3831 (setq org-blocked-by-checkboxes nil invis1 invis)
3832 (let ((marker (org-get-at-bol 'org-hd-marker)))
3833 (when (and marker
3834 (with-current-buffer (marker-buffer marker)
3835 (save-excursion (goto-char marker)
3836 (org-entry-blocked-p))))
3837 (if org-blocked-by-checkboxes (setq invis1 nil))
3838 (setq b (if invis1
3839 (max (point-min) (1- (point-at-bol)))
3840 (point-at-bol))
3841 e (point-at-eol)
3842 ov (make-overlay b e))
3843 (if invis1
3844 (overlay-put ov 'invisible t)
3845 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3846 (overlay-put ov 'org-type 'org-blocked-todo))))))
3847 (when (org-called-interactively-p 'interactive)
3848 (message "Dim or hide blocked tasks...done")))
3850 (defvar org-agenda-skip-function nil
3851 "Function to be called at each match during agenda construction.
3852 If this function returns nil, the current match should not be skipped.
3853 Otherwise, the function must return a position from where the search
3854 should be continued.
3855 This may also be a Lisp form, it will be evaluated.
3856 Never set this variable using `setq' or so, because then it will apply
3857 to all future agenda commands. If you do want a global skipping condition,
3858 use the option `org-agenda-skip-function-global' instead.
3859 The correct usage for `org-agenda-skip-function' is to bind it with
3860 `let' to scope it dynamically into the agenda-constructing command.
3861 A good way to set it is through options in `org-agenda-custom-commands'.")
3863 (defun org-agenda-skip ()
3864 "Throw to `:skip' in places that should be skipped.
3865 Also moves point to the end of the skipped region, so that search can
3866 continue from there."
3867 (let ((p (point-at-bol)) to)
3868 (when (or
3869 (save-excursion (goto-char p) (looking-at comment-start-skip))
3870 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3871 (get-text-property p :org-archived)
3872 (org-end-of-subtree t))
3873 (and org-agenda-skip-comment-trees
3874 (get-text-property p :org-comment)
3875 (org-end-of-subtree t))
3876 (and (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3877 (org-agenda-skip-eval org-agenda-skip-function)))
3878 (goto-char to))
3879 (org-in-src-block-p t))
3880 (throw :skip t))))
3882 (defun org-agenda-skip-eval (form)
3883 "If FORM is a function or a list, call (or eval) it and return the result.
3884 `save-excursion' and `save-match-data' are wrapped around the call, so point
3885 and match data are returned to the previous state no matter what these
3886 functions do."
3887 (let (fp)
3888 (and form
3889 (or (setq fp (functionp form))
3890 (consp form))
3891 (save-excursion
3892 (save-match-data
3893 (if fp
3894 (funcall form)
3895 (eval form)))))))
3897 (defvar org-agenda-markers nil
3898 "List of all currently active markers created by `org-agenda'.")
3899 (defvar org-agenda-last-marker-time (org-float-time)
3900 "Creation time of the last agenda marker.")
3902 (defun org-agenda-new-marker (&optional pos)
3903 "Return a new agenda marker.
3904 Org-mode keeps a list of these markers and resets them when they are
3905 no longer in use."
3906 (let ((m (copy-marker (or pos (point)))))
3907 (setq org-agenda-last-marker-time (org-float-time))
3908 (if org-agenda-buffer
3909 (with-current-buffer org-agenda-buffer
3910 (push m org-agenda-markers))
3911 (push m org-agenda-markers))
3914 (defun org-agenda-reset-markers ()
3915 "Reset markers created by `org-agenda'."
3916 (while org-agenda-markers
3917 (move-marker (pop org-agenda-markers) nil)))
3919 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3920 "Save relative positions of markers in region.
3921 This check for agenda markers in all agenda buffers currently active."
3922 (dolist (buf (buffer-list))
3923 (with-current-buffer buf
3924 (when (eq major-mode 'org-agenda-mode)
3925 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3926 org-agenda-markers)))))
3928 ;;; Entry text mode
3930 (defun org-agenda-entry-text-show-here ()
3931 "Add some text from the entry as context to the current line."
3932 (let (m txt o)
3933 (setq m (org-get-at-bol 'org-hd-marker))
3934 (unless (marker-buffer m)
3935 (error "No marker points to an entry here"))
3936 (setq txt (concat "\n" (org-no-properties
3937 (org-agenda-get-some-entry-text
3938 m org-agenda-entry-text-maxlines
3939 org-agenda-entry-text-leaders))))
3940 (when (string-match "\\S-" txt)
3941 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3942 (overlay-put o 'evaporate t)
3943 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3944 (overlay-put o 'after-string txt))))
3946 (defun org-agenda-entry-text-show ()
3947 "Add entry context for all agenda lines."
3948 (interactive)
3949 (save-excursion
3950 (goto-char (point-max))
3951 (beginning-of-line 1)
3952 (while (not (bobp))
3953 (when (org-get-at-bol 'org-hd-marker)
3954 (org-agenda-entry-text-show-here))
3955 (beginning-of-line 0))))
3957 (defun org-agenda-entry-text-hide ()
3958 "Remove any shown entry context."
3959 (delq nil
3960 (mapcar (lambda (o)
3961 (if (eq (overlay-get o 'org-overlay-type)
3962 'agenda-entry-content)
3963 (progn (delete-overlay o) t)))
3964 (overlays-in (point-min) (point-max)))))
3966 (defun org-agenda-get-day-face (date)
3967 "Return the face DATE should be displayed with."
3968 (or (and (functionp org-agenda-day-face-function)
3969 (funcall org-agenda-day-face-function date))
3970 (cond ((org-agenda-todayp date)
3971 'org-agenda-date-today)
3972 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3973 'org-agenda-date-weekend)
3974 (t 'org-agenda-date))))
3976 ;;; Agenda timeline
3978 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3980 (defun org-timeline (&optional dotodo)
3981 "Show a time-sorted view of the entries in the current org file.
3982 Only entries with a time stamp of today or later will be listed. With
3983 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3984 under the current date.
3985 If the buffer contains an active region, only check the region for
3986 dates."
3987 (interactive "P")
3988 (let* ((dopast t)
3989 (org-agenda-show-log-scoped org-agenda-show-log)
3990 (org-agenda-show-log org-agenda-show-log-scoped)
3991 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3992 (current-buffer))))
3993 (date (calendar-current-date))
3994 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3995 (end (if (org-region-active-p) (region-end) (point-max)))
3996 (day-numbers (org-get-all-dates
3997 beg end 'no-ranges
3998 t org-agenda-show-log-scoped ; always include today
3999 org-timeline-show-empty-dates))
4000 (org-deadline-warning-days 0)
4001 (org-agenda-only-exact-dates t)
4002 (today (org-today))
4003 (past t)
4004 args
4005 s e rtn d emptyp)
4006 (setq org-agenda-redo-command
4007 (list 'let
4008 (list (list 'org-agenda-show-log 'org-agenda-show-log))
4009 (list 'org-switch-to-buffer-other-window (current-buffer))
4010 (list 'org-timeline (list 'quote dotodo))))
4011 (put 'org-agenda-redo-command 'org-lprops nil)
4012 (if (not dopast)
4013 ;; Remove past dates from the list of dates.
4014 (setq day-numbers (delq nil (mapcar (lambda(x)
4015 (if (>= x today) x nil))
4016 day-numbers))))
4017 (org-agenda-prepare (concat "Timeline " (file-name-nondirectory entry)))
4018 (org-compile-prefix-format 'timeline)
4019 (org-set-sorting-strategy 'timeline)
4020 (if org-agenda-show-log-scoped (push :closed args))
4021 (push :timestamp args)
4022 (push :deadline args)
4023 (push :scheduled args)
4024 (push :sexp args)
4025 (if dotodo (push :todo args))
4026 (insert "Timeline of file " entry "\n")
4027 (add-text-properties (point-min) (point)
4028 (list 'face 'org-agenda-structure))
4029 (org-agenda-mark-header-line (point-min))
4030 (while (setq d (pop day-numbers))
4031 (if (and (listp d) (eq (car d) :omitted))
4032 (progn
4033 (setq s (point))
4034 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
4035 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
4036 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
4037 (if (and (>= d today)
4038 dopast
4039 past)
4040 (progn
4041 (setq past nil)
4042 (insert (make-string 79 ?-) "\n")))
4043 (setq date (calendar-gregorian-from-absolute d))
4044 (setq s (point))
4045 (setq rtn (and (not emptyp)
4046 (apply 'org-agenda-get-day-entries entry
4047 date args)))
4048 (if (or rtn (equal d today) org-timeline-show-empty-dates)
4049 (progn
4050 (insert
4051 (if (stringp org-agenda-format-date)
4052 (format-time-string org-agenda-format-date
4053 (org-time-from-absolute date))
4054 (funcall org-agenda-format-date date))
4055 "\n")
4056 (put-text-property s (1- (point)) 'face
4057 (org-agenda-get-day-face date))
4058 (put-text-property s (1- (point)) 'org-date-line t)
4059 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4060 (if (equal d today)
4061 (put-text-property s (1- (point)) 'org-today t))
4062 (and rtn (insert (org-agenda-finalize-entries rtn 'timeline) "\n"))
4063 (put-text-property s (1- (point)) 'day d)))))
4064 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
4065 (point-min)))
4066 (add-text-properties
4067 (point-min) (point-max)
4068 `(org-agenda-type timeline org-redo-cmd ,org-agenda-redo-command))
4069 (org-agenda-finalize)
4070 (setq buffer-read-only t)))
4072 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
4073 "Return a list of all relevant day numbers from BEG to END buffer positions.
4074 If NO-RANGES is non-nil, include only the start and end dates of a range,
4075 not every single day in the range. If FORCE-TODAY is non-nil, make
4076 sure that TODAY is included in the list. If INACTIVE is non-nil, also
4077 inactive time stamps (those in square brackets) are included.
4078 When EMPTY is non-nil, also include days without any entries."
4079 (let ((re (concat
4080 (if pre-re pre-re "")
4081 (if inactive org-ts-regexp-both org-ts-regexp)))
4082 dates dates1 date day day1 day2 ts1 ts2 pos)
4083 (if force-today
4084 (setq dates (list (org-today))))
4085 (save-excursion
4086 (goto-char beg)
4087 (while (re-search-forward re end t)
4088 (setq day (time-to-days (org-time-string-to-time
4089 (substring (match-string 1) 0 10)
4090 (current-buffer) (match-beginning 0))))
4091 (or (memq day dates) (push day dates)))
4092 (unless no-ranges
4093 (goto-char beg)
4094 (while (re-search-forward org-tr-regexp end t)
4095 (setq pos (match-beginning 0))
4096 (setq ts1 (substring (match-string 1) 0 10)
4097 ts2 (substring (match-string 2) 0 10)
4098 day1 (time-to-days (org-time-string-to-time
4099 ts1 (current-buffer) pos))
4100 day2 (time-to-days (org-time-string-to-time
4101 ts2 (current-buffer) pos)))
4102 (while (< (setq day1 (1+ day1)) day2)
4103 (or (memq day1 dates) (push day1 dates)))))
4104 (setq dates (sort dates '<))
4105 (when empty
4106 (while (setq day (pop dates))
4107 (setq day2 (car dates))
4108 (push day dates1)
4109 (when (and day2 empty)
4110 (if (or (eq empty t)
4111 (and (numberp empty) (<= (- day2 day) empty)))
4112 (while (< (setq day (1+ day)) day2)
4113 (push (list day) dates1))
4114 (push (cons :omitted (- day2 day)) dates1))))
4115 (setq dates (nreverse dates1)))
4116 dates)))
4118 ;;; Agenda Daily/Weekly
4120 (defvar org-agenda-start-day nil ; dynamically scoped parameter
4121 "Start day for the agenda view.
4122 Custom commands can set this variable in the options section.
4123 This is usually a string like \"2007-11-01\", \"+2d\" or any other
4124 input allowed when reading a date through the Org calendar.
4125 See the docstring of `org-read-date' for details.")
4126 (defvar org-starting-day nil) ; local variable in the agenda buffer
4127 (defvar org-arg-loc nil) ; local variable
4129 (defvar org-agenda-buffer-tmp-name nil)
4130 ;;;###autoload
4131 (defun org-agenda-list (&optional arg start-day span with-hour)
4132 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
4133 The view will be for the current day or week, but from the overview buffer
4134 you will be able to go to other days/weeks.
4136 With a numeric prefix argument in an interactive call, the agenda will
4137 span ARG days. Lisp programs should instead specify SPAN to change
4138 the number of days. SPAN defaults to `org-agenda-span'.
4140 START-DAY defaults to TODAY, or to the most recent match for the weekday
4141 given in `org-agenda-start-on-weekday'.
4143 When WITH-HOUR is non-nil, only include scheduled and deadline
4144 items if they have an hour specification like [h]h:mm."
4145 (interactive "P")
4146 (if org-agenda-overriding-arguments
4147 (setq arg (car org-agenda-overriding-arguments)
4148 start-day (nth 1 org-agenda-overriding-arguments)
4149 span (nth 2 org-agenda-overriding-arguments)))
4150 (if (and (integerp arg) (> arg 0))
4151 (setq span arg arg nil))
4152 (catch 'exit
4153 (setq org-agenda-buffer-name
4154 (or org-agenda-buffer-tmp-name
4155 (if org-agenda-sticky
4156 (cond ((and org-keys (stringp org-match))
4157 (format "*Org Agenda(%s:%s)*" org-keys org-match))
4158 (org-keys
4159 (format "*Org Agenda(%s)*" org-keys))
4160 (t "*Org Agenda(a)*")))
4161 org-agenda-buffer-name))
4162 (org-agenda-prepare "Day/Week")
4163 (setq start-day (or start-day org-agenda-start-day))
4164 (if (stringp start-day)
4165 ;; Convert to an absolute day number
4166 (setq start-day (time-to-days (org-read-date nil t start-day))))
4167 (org-compile-prefix-format 'agenda)
4168 (org-set-sorting-strategy 'agenda)
4169 (let* ((span (org-agenda-ndays-to-span
4170 (or span org-agenda-ndays org-agenda-span)))
4171 (today (org-today))
4172 (sd (or start-day today))
4173 (ndays (org-agenda-span-to-ndays span sd))
4174 (org-agenda-start-on-weekday
4175 (if (eq ndays 7)
4176 org-agenda-start-on-weekday))
4177 (thefiles (org-agenda-files nil 'ifmode))
4178 (files thefiles)
4179 (start (if (or (null org-agenda-start-on-weekday)
4180 (< ndays 7))
4182 (let* ((nt (calendar-day-of-week
4183 (calendar-gregorian-from-absolute sd)))
4184 (n1 org-agenda-start-on-weekday)
4185 (d (- nt n1)))
4186 (- sd (+ (if (< d 0) 7 0) d)))))
4187 (day-numbers (list start))
4188 (day-cnt 0)
4189 (inhibit-redisplay (not debug-on-error))
4190 (org-agenda-show-log-scoped org-agenda-show-log)
4191 s e rtn rtnall file date d start-pos end-pos todayp
4192 clocktable-start clocktable-end filter)
4193 (setq org-agenda-redo-command
4194 (list 'org-agenda-list (list 'quote arg) start-day (list 'quote span) with-hour))
4195 (dotimes (n (1- ndays))
4196 (push (1+ (car day-numbers)) day-numbers))
4197 (setq day-numbers (nreverse day-numbers))
4198 (setq clocktable-start (car day-numbers)
4199 clocktable-end (1+ (or (org-last day-numbers) 0)))
4200 (org-set-local 'org-starting-day (car day-numbers))
4201 (org-set-local 'org-arg-loc arg)
4202 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
4203 (unless org-agenda-compact-blocks
4204 (let* ((d1 (car day-numbers))
4205 (d2 (org-last day-numbers))
4206 (w1 (org-days-to-iso-week d1))
4207 (w2 (org-days-to-iso-week d2)))
4208 (setq s (point))
4209 (if org-agenda-overriding-header
4210 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4211 nil 'face 'org-agenda-structure) "\n")
4212 (insert (org-agenda-span-name span)
4213 "-agenda"
4214 (if (< (- d2 d1) 350)
4215 (if (= w1 w2)
4216 (format " (W%02d)" w1)
4217 (format " (W%02d-W%02d)" w1 w2))
4219 ":\n")))
4220 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
4221 'org-date-line t))
4222 (org-agenda-mark-header-line s))
4223 (while (setq d (pop day-numbers))
4224 (setq date (calendar-gregorian-from-absolute d)
4225 s (point))
4226 (if (or (setq todayp (= d today))
4227 (and (not start-pos) (= d sd)))
4228 (setq start-pos (point))
4229 (if (and start-pos (not end-pos))
4230 (setq end-pos (point))))
4231 (setq files thefiles
4232 rtnall nil)
4233 (while (setq file (pop files))
4234 (catch 'nextfile
4235 (org-check-agenda-file file)
4236 (let ((org-agenda-entry-types org-agenda-entry-types))
4237 ;; Starred types override non-starred equivalents
4238 (when (member :deadline* org-agenda-entry-types)
4239 (setq org-agenda-entry-types
4240 (delq :deadline org-agenda-entry-types)))
4241 (when (member :scheduled* org-agenda-entry-types)
4242 (setq org-agenda-entry-types
4243 (delq :scheduled org-agenda-entry-types)))
4244 ;; Honor with-hour
4245 (when with-hour
4246 (when (member :deadline org-agenda-entry-types)
4247 (setq org-agenda-entry-types
4248 (delq :deadline org-agenda-entry-types))
4249 (push :deadline* org-agenda-entry-types))
4250 (when (member :scheduled org-agenda-entry-types)
4251 (setq org-agenda-entry-types
4252 (delq :scheduled org-agenda-entry-types))
4253 (push :scheduled* org-agenda-entry-types)))
4254 (unless org-agenda-include-deadlines
4255 (setq org-agenda-entry-types
4256 (delq :deadline* (delq :deadline org-agenda-entry-types))))
4257 (cond
4258 ((memq org-agenda-show-log-scoped '(only clockcheck))
4259 (setq rtn (org-agenda-get-day-entries
4260 file date :closed)))
4261 (org-agenda-show-log-scoped
4262 (setq rtn (apply 'org-agenda-get-day-entries
4263 file date
4264 (append '(:closed) org-agenda-entry-types))))
4266 (setq rtn (apply 'org-agenda-get-day-entries
4267 file date
4268 org-agenda-entry-types)))))
4269 (setq rtnall (append rtnall rtn)))) ;; all entries
4270 (if org-agenda-include-diary
4271 (let ((org-agenda-search-headline-for-time t))
4272 (require 'diary-lib)
4273 (setq rtn (org-get-entries-from-diary date))
4274 (setq rtnall (append rtnall rtn))))
4275 (if (or rtnall org-agenda-show-all-dates)
4276 (progn
4277 (setq day-cnt (1+ day-cnt))
4278 (insert
4279 (if (stringp org-agenda-format-date)
4280 (format-time-string org-agenda-format-date
4281 (org-time-from-absolute date))
4282 (funcall org-agenda-format-date date))
4283 "\n")
4284 (put-text-property s (1- (point)) 'face
4285 (org-agenda-get-day-face date))
4286 (put-text-property s (1- (point)) 'org-date-line t)
4287 (put-text-property s (1- (point)) 'org-agenda-date-header t)
4288 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
4289 (when todayp
4290 (put-text-property s (1- (point)) 'org-today t))
4291 (setq rtnall
4292 (org-agenda-add-time-grid-maybe rtnall ndays todayp))
4293 (if rtnall (insert ;; all entries
4294 (org-agenda-finalize-entries rtnall 'agenda)
4295 "\n"))
4296 (put-text-property s (1- (point)) 'day d)
4297 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
4298 (when (and org-agenda-clockreport-mode clocktable-start)
4299 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
4300 ;; the above line is to ensure the restricted range!
4301 (p (copy-sequence org-agenda-clockreport-parameter-plist))
4302 tbl)
4303 (setq p (org-plist-delete p :block))
4304 (setq p (plist-put p :tstart clocktable-start))
4305 (setq p (plist-put p :tend clocktable-end))
4306 (setq p (plist-put p :scope 'agenda))
4307 (when (and (eq org-agenda-clockreport-mode 'with-filter)
4308 (setq filter (or org-agenda-tag-filter-while-redo
4309 (get 'org-agenda-tag-filter :preset-filter))))
4310 (setq p (plist-put p :tags (mapconcat (lambda (x)
4311 (if (string-match "[<>=]" x)
4314 filter ""))))
4315 (setq tbl (apply 'org-clock-get-clocktable p))
4316 (insert tbl)))
4317 (goto-char (point-min))
4318 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4319 (unless (and (pos-visible-in-window-p (point-min))
4320 (pos-visible-in-window-p (point-max)))
4321 (goto-char (1- (point-max)))
4322 (recenter -1)
4323 (if (not (pos-visible-in-window-p (or start-pos 1)))
4324 (progn
4325 (goto-char (or start-pos 1))
4326 (recenter 1))))
4327 (goto-char (or start-pos 1))
4328 (add-text-properties (point-min) (point-max)
4329 `(org-agenda-type agenda
4330 org-last-args (,arg ,start-day ,span)
4331 org-redo-cmd ,org-agenda-redo-command
4332 org-series-cmd ,org-cmd))
4333 (if (eq org-agenda-show-log-scoped 'clockcheck)
4334 (org-agenda-show-clocking-issues))
4335 (org-agenda-finalize)
4336 (setq buffer-read-only t)
4337 (message ""))))
4339 (defun org-agenda-ndays-to-span (n)
4340 "Return a span symbol for a span of N days, or N if none matches."
4341 (cond ((symbolp n) n)
4342 ((= n 1) 'day)
4343 ((= n 7) 'week)
4344 (t n)))
4346 (defun org-agenda-span-to-ndays (span &optional start-day)
4347 "Return ndays from SPAN, possibly starting at START-DAY.
4348 START-DAY is an absolute time value."
4349 (cond ((numberp span) span)
4350 ((eq span 'day) 1)
4351 ((eq span 'week) 7)
4352 ((eq span 'month)
4353 (let ((date (calendar-gregorian-from-absolute start-day)))
4354 (calendar-last-day-of-month (car date) (caddr date))))
4355 ((eq span 'year)
4356 (let ((date (calendar-gregorian-from-absolute start-day)))
4357 (if (calendar-leap-year-p (caddr date)) 366 365)))))
4359 (defun org-agenda-span-name (span)
4360 "Return a SPAN name."
4361 (if (null span)
4363 (if (symbolp span)
4364 (capitalize (symbol-name span))
4365 (format "%d days" span))))
4367 ;;; Agenda word search
4369 (defvar org-agenda-search-history nil)
4371 (defvar org-search-syntax-table nil
4372 "Special syntax table for org-mode search.
4373 In this table, we have single quotes not as word constituents, to
4374 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
4376 (defvar org-mode-syntax-table) ; From org.el
4377 (defun org-search-syntax-table ()
4378 (unless org-search-syntax-table
4379 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
4380 (modify-syntax-entry ?' "." org-search-syntax-table)
4381 (modify-syntax-entry ?` "." org-search-syntax-table))
4382 org-search-syntax-table)
4384 (defvar org-agenda-last-search-view-search-was-boolean nil)
4386 ;;;###autoload
4387 (defun org-search-view (&optional todo-only string edit-at)
4388 "Show all entries that contain a phrase or words or regular expressions.
4390 With optional prefix argument TODO-ONLY, only consider entries that are
4391 TODO entries. The argument STRING can be used to pass a default search
4392 string into this function. If EDIT-AT is non-nil, it means that the
4393 user should get a chance to edit this string, with cursor at position
4394 EDIT-AT.
4396 The search string can be viewed either as a phrase that should be found as
4397 is, or it can be broken into a number of snippets, each of which must match
4398 in a Boolean way to select an entry. The default depends on the variable
4399 `org-agenda-search-view-always-boolean'.
4400 Even if this is turned off (the default) you can always switch to
4401 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
4403 The default is a direct search of the whole phrase, where each space in
4404 the search string can expand to an arbitrary amount of whitespace,
4405 including newlines.
4407 If using a Boolean search, the search string is split on whitespace and
4408 each snippet is searched separately, with logical AND to select an entry.
4409 Words prefixed with a minus must *not* occur in the entry. Words without
4410 a prefix or prefixed with a plus must occur in the entry. Matching is
4411 case-insensitive. Words are enclosed by word delimiters (i.e. they must
4412 match whole words, not parts of a word) if
4413 `org-agenda-search-view-force-full-words' is set (default is nil).
4415 Boolean search snippets enclosed by curly braces are interpreted as
4416 regular expressions that must or (when preceded with \"-\") must not
4417 match in the entry. Snippets enclosed into double quotes will be taken
4418 as a whole, to include whitespace.
4420 - If the search string starts with an asterisk, search only in headlines.
4421 - If (possibly after the leading star) the search string starts with an
4422 exclamation mark, this also means to look at TODO entries only, an effect
4423 that can also be achieved with a prefix argument.
4424 - If (possibly after star and exclamation mark) the search string starts
4425 with a colon, this will mean that the (non-regexp) snippets of the
4426 Boolean search must match as full words.
4428 This command searches the agenda files, and in addition the files listed
4429 in `org-agenda-text-search-extra-files'."
4430 (interactive "P")
4431 (if org-agenda-overriding-arguments
4432 (setq todo-only (car org-agenda-overriding-arguments)
4433 string (nth 1 org-agenda-overriding-arguments)
4434 edit-at (nth 2 org-agenda-overriding-arguments)))
4435 (let* ((props (list 'face nil
4436 'done-face 'org-agenda-done
4437 'org-not-done-regexp org-not-done-regexp
4438 'org-todo-regexp org-todo-regexp
4439 'org-complex-heading-regexp org-complex-heading-regexp
4440 'mouse-face 'highlight
4441 'help-echo (format "mouse-2 or RET jump to location")))
4442 (full-words org-agenda-search-view-force-full-words)
4443 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
4444 regexp rtn rtnall files file pos inherited-tags
4445 marker category category-pos level tags c neg re boolean
4446 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
4447 (unless (and (not edit-at)
4448 (stringp string)
4449 (string-match "\\S-" string))
4450 (setq string (read-string
4451 (if org-agenda-search-view-always-boolean
4452 "[+-]Word/{Regexp} ...: "
4453 "Phrase or [+-]Word/{Regexp} ...: ")
4454 (cond
4455 ((integerp edit-at) (cons string edit-at))
4456 (edit-at string))
4457 'org-agenda-search-history)))
4458 (catch 'exit
4459 (if org-agenda-sticky
4460 (setq org-agenda-buffer-name
4461 (if (stringp string)
4462 (format "*Org Agenda(%s:%s)*"
4463 (or org-keys (or (and todo-only "S") "s")) string)
4464 (format "*Org Agenda(%s)*" (or (and todo-only "S") "s")))))
4465 (org-agenda-prepare "SEARCH")
4466 (org-compile-prefix-format 'search)
4467 (org-set-sorting-strategy 'search)
4468 (setq org-agenda-redo-command
4469 (list 'org-search-view (if todo-only t nil)
4470 (list 'if 'current-prefix-arg nil string)))
4471 (setq org-agenda-query-string string)
4472 (if (equal (string-to-char string) ?*)
4473 (setq hdl-only t
4474 words (substring string 1))
4475 (setq words string))
4476 (when (equal (string-to-char words) ?!)
4477 (setq todo-only t
4478 words (substring words 1)))
4479 (when (equal (string-to-char words) ?:)
4480 (setq full-words t
4481 words (substring words 1)))
4482 (if (or org-agenda-search-view-always-boolean
4483 (member (string-to-char words) '(?- ?+ ?\{)))
4484 (setq boolean t))
4485 (setq words (org-split-string words))
4486 (let (www w)
4487 (while (setq w (pop words))
4488 (while (and (string-match "\\\\\\'" w) words)
4489 (setq w (concat (substring w 0 -1) " " (pop words))))
4490 (push w www))
4491 (setq words (nreverse www) www nil)
4492 (while (setq w (pop words))
4493 (when (and (string-match "\\`[-+]?{" w)
4494 (not (string-match "}\\'" w)))
4495 (while (and words (not (string-match "}\\'" (car words))))
4496 (setq w (concat w " " (pop words))))
4497 (setq w (concat w " " (pop words))))
4498 (push w www))
4499 (setq words (nreverse www)))
4500 (setq org-agenda-last-search-view-search-was-boolean boolean)
4501 (when boolean
4502 (let (wds w)
4503 (while (setq w (pop words))
4504 (if (or (equal (substring w 0 1) "\"")
4505 (and (> (length w) 1)
4506 (member (substring w 0 1) '("+" "-"))
4507 (equal (substring w 1 2) "\"")))
4508 (while (and words (not (equal (substring w -1) "\"")))
4509 (setq w (concat w " " (pop words)))))
4510 (and (string-match "\\`\\([-+]?\\)\"" w)
4511 (setq w (replace-match "\\1" nil nil w)))
4512 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
4513 (push w wds))
4514 (setq words (nreverse wds))))
4515 (if boolean
4516 (mapc (lambda (w)
4517 (setq c (string-to-char w))
4518 (if (equal c ?-)
4519 (setq neg t w (substring w 1))
4520 (if (equal c ?+)
4521 (setq neg nil w (substring w 1))
4522 (setq neg nil)))
4523 (if (string-match "\\`{.*}\\'" w)
4524 (setq re (substring w 1 -1))
4525 (if full-words
4526 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
4527 (setq re (regexp-quote (downcase w)))))
4528 (if neg (push re regexps-) (push re regexps+)))
4529 words)
4530 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
4531 regexps+))
4532 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
4533 (if (not regexps+)
4534 (setq regexp org-outline-regexp-bol)
4535 (setq regexp (pop regexps+))
4536 (if hdl-only (setq regexp (concat org-outline-regexp-bol ".*?"
4537 regexp))))
4538 (setq files (org-agenda-files nil 'ifmode))
4539 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
4540 (pop org-agenda-text-search-extra-files)
4541 (setq files (org-add-archive-files files)))
4542 (setq files (append files org-agenda-text-search-extra-files)
4543 rtnall nil)
4544 (while (setq file (pop files))
4545 (setq ee nil)
4546 (catch 'nextfile
4547 (org-check-agenda-file file)
4548 (setq buffer (if (file-exists-p file)
4549 (org-get-agenda-file-buffer file)
4550 (error "No such file %s" file)))
4551 (if (not buffer)
4552 ;; If file does not exist, make sure an error message is sent
4553 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
4554 file))))
4555 (with-current-buffer buffer
4556 (with-syntax-table (org-search-syntax-table)
4557 (unless (derived-mode-p 'org-mode)
4558 (error "Agenda file %s is not in `org-mode'" file))
4559 (let ((case-fold-search t))
4560 (save-excursion
4561 (save-restriction
4562 (if (eq buffer org-agenda-restrict)
4563 (narrow-to-region org-agenda-restrict-begin
4564 org-agenda-restrict-end)
4565 (widen))
4566 (goto-char (point-min))
4567 (unless (or (org-at-heading-p)
4568 (outline-next-heading))
4569 (throw 'nextfile t))
4570 (goto-char (max (point-min) (1- (point))))
4571 (while (re-search-forward regexp nil t)
4572 (org-back-to-heading t)
4573 (while (and org-agenda-search-view-max-outline-level
4574 (> (org-reduced-level (org-outline-level))
4575 org-agenda-search-view-max-outline-level)
4576 (forward-line -1)
4577 (outline-back-to-heading t)))
4578 (skip-chars-forward "* ")
4579 (setq beg (point-at-bol)
4580 beg1 (point)
4581 end (progn
4582 (outline-next-heading)
4583 (while (and org-agenda-search-view-max-outline-level
4584 (> (org-reduced-level (org-outline-level))
4585 org-agenda-search-view-max-outline-level)
4586 (forward-line 1)
4587 (outline-next-heading)))
4588 (point)))
4590 (catch :skip
4591 (goto-char beg)
4592 (org-agenda-skip)
4593 (setq str (buffer-substring-no-properties
4594 (point-at-bol)
4595 (if hdl-only (point-at-eol) end)))
4596 (mapc (lambda (wr) (when (string-match wr str)
4597 (goto-char (1- end))
4598 (throw :skip t)))
4599 regexps-)
4600 (mapc (lambda (wr) (unless (string-match wr str)
4601 (goto-char (1- end))
4602 (throw :skip t)))
4603 (if todo-only
4604 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
4605 regexps+)
4606 regexps+))
4607 (goto-char beg)
4608 (setq marker (org-agenda-new-marker (point))
4609 category (org-get-category)
4610 level (make-string (org-reduced-level (org-outline-level)) ? )
4611 category-pos (get-text-property (point) 'org-category-position)
4612 inherited-tags
4613 (or (eq org-agenda-show-inherited-tags 'always)
4614 (and (listp org-agenda-show-inherited-tags)
4615 (memq 'todo org-agenda-show-inherited-tags))
4616 (and (eq org-agenda-show-inherited-tags t)
4617 (or (eq org-agenda-use-tag-inheritance t)
4618 (memq 'todo org-agenda-use-tag-inheritance))))
4619 tags (org-get-tags-at nil (not inherited-tags))
4620 txt (org-agenda-format-item
4622 (buffer-substring-no-properties
4623 beg1 (point-at-eol))
4624 level category tags t))
4625 (org-add-props txt props
4626 'org-marker marker 'org-hd-marker marker
4627 'org-todo-regexp org-todo-regexp
4628 'level level
4629 'org-complex-heading-regexp org-complex-heading-regexp
4630 'priority 1000 'org-category category
4631 'org-category-position category-pos
4632 'type "search")
4633 (push txt ee)
4634 (goto-char (1- end))))))))))
4635 (setq rtn (nreverse ee))
4636 (setq rtnall (append rtnall rtn)))
4637 (if org-agenda-overriding-header
4638 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4639 nil 'face 'org-agenda-structure) "\n")
4640 (insert "Search words: ")
4641 (add-text-properties (point-min) (1- (point))
4642 (list 'face 'org-agenda-structure))
4643 (setq pos (point))
4644 (insert string "\n")
4645 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4646 (setq pos (point))
4647 (unless org-agenda-multi
4648 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
4649 (add-text-properties pos (1- (point))
4650 (list 'face 'org-agenda-structure))))
4651 (org-agenda-mark-header-line (point-min))
4652 (when rtnall
4653 (insert (org-agenda-finalize-entries rtnall 'search) "\n"))
4654 (goto-char (point-min))
4655 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4656 (add-text-properties (point-min) (point-max)
4657 `(org-agenda-type search
4658 org-last-args (,todo-only ,string ,edit-at)
4659 org-redo-cmd ,org-agenda-redo-command
4660 org-series-cmd ,org-cmd))
4661 (org-agenda-finalize)
4662 (setq buffer-read-only t))))
4664 ;;; Agenda TODO list
4666 (defun org-agenda-propertize-selected-todo-keywords (keywords)
4667 "Use `org-todo-keyword-faces' for the selected todo KEYWORDS."
4668 (concat
4669 (if (or (equal keywords "ALL") (not keywords))
4670 (propertize "ALL" 'face 'warning)
4671 (mapconcat
4672 (lambda (kw)
4673 (propertize kw 'face (org-get-todo-face kw)))
4674 (org-split-string keywords "|")
4675 "|"))
4676 "\n"))
4678 (defvar org-select-this-todo-keyword nil)
4679 (defvar org-last-arg nil)
4681 ;;;###autoload
4682 (defun org-todo-list (&optional arg)
4683 "Show all (not done) TODO entries from all agenda file in a single list.
4684 The prefix arg can be used to select a specific TODO keyword and limit
4685 the list to these. When using \\[universal-argument], you will be prompted
4686 for a keyword. A numeric prefix directly selects the Nth keyword in
4687 `org-todo-keywords-1'."
4688 (interactive "P")
4689 (if org-agenda-overriding-arguments
4690 (setq arg org-agenda-overriding-arguments))
4691 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
4692 (let* ((today (org-today))
4693 (date (calendar-gregorian-from-absolute today))
4694 (kwds org-todo-keywords-for-agenda)
4695 (completion-ignore-case t)
4696 (org-select-this-todo-keyword
4697 (if (stringp arg) arg
4698 (and arg (integerp arg) (> arg 0)
4699 (nth (1- arg) kwds))))
4700 rtn rtnall files file pos)
4701 (when (equal arg '(4))
4702 (setq org-select-this-todo-keyword
4703 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4704 (mapcar 'list kwds) nil nil)))
4705 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4706 (catch 'exit
4707 (if org-agenda-sticky
4708 (setq org-agenda-buffer-name
4709 (if (stringp org-select-this-todo-keyword)
4710 (format "*Org Agenda(%s:%s)*" (or org-keys "t")
4711 org-select-this-todo-keyword)
4712 (format "*Org Agenda(%s)*" (or org-keys "t")))))
4713 (org-agenda-prepare "TODO")
4714 (org-compile-prefix-format 'todo)
4715 (org-set-sorting-strategy 'todo)
4716 (setq org-agenda-redo-command
4717 `(org-todo-list (or (and (numberp current-prefix-arg)
4718 current-prefix-arg)
4719 ,org-select-this-todo-keyword
4720 current-prefix-arg ,arg)))
4721 (setq files (org-agenda-files nil 'ifmode)
4722 rtnall nil)
4723 (while (setq file (pop files))
4724 (catch 'nextfile
4725 (org-check-agenda-file file)
4726 (setq rtn (org-agenda-get-day-entries file date :todo))
4727 (setq rtnall (append rtnall rtn))))
4728 (if org-agenda-overriding-header
4729 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4730 nil 'face 'org-agenda-structure) "\n")
4731 (insert "Global list of TODO items of type: ")
4732 (add-text-properties (point-min) (1- (point))
4733 (list 'face 'org-agenda-structure
4734 'short-heading
4735 (concat "ToDo: "
4736 (or org-select-this-todo-keyword "ALL"))))
4737 (org-agenda-mark-header-line (point-min))
4738 (insert (org-agenda-propertize-selected-todo-keywords
4739 org-select-this-todo-keyword))
4740 (setq pos (point))
4741 (unless org-agenda-multi
4742 (insert "Available with `N r': (0)[ALL]")
4743 (let ((n 0) s)
4744 (mapc (lambda (x)
4745 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4746 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4747 (insert "\n "))
4748 (insert " " s))
4749 kwds))
4750 (insert "\n"))
4751 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4752 (org-agenda-mark-header-line (point-min))
4753 (when rtnall
4754 (insert (org-agenda-finalize-entries rtnall 'todo) "\n"))
4755 (goto-char (point-min))
4756 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4757 (add-text-properties (point-min) (point-max)
4758 `(org-agenda-type todo
4759 org-last-args ,arg
4760 org-redo-cmd ,org-agenda-redo-command
4761 org-series-cmd ,org-cmd))
4762 (org-agenda-finalize)
4763 (setq buffer-read-only t))))
4765 ;;; Agenda tags match
4767 ;;;###autoload
4768 (defun org-tags-view (&optional todo-only match)
4769 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4770 The prefix arg TODO-ONLY limits the search to TODO entries."
4771 (interactive "P")
4772 (if org-agenda-overriding-arguments
4773 (setq todo-only (car org-agenda-overriding-arguments)
4774 match (nth 1 org-agenda-overriding-arguments)))
4775 (let* ((org-tags-match-list-sublevels
4776 org-tags-match-list-sublevels)
4777 (completion-ignore-case t)
4778 rtn rtnall files file pos matcher
4779 buffer)
4780 (when (and (stringp match) (not (string-match "\\S-" match)))
4781 (setq match nil))
4782 (catch 'exit
4783 (if org-agenda-sticky
4784 (setq org-agenda-buffer-name
4785 (if (stringp match)
4786 (format "*Org Agenda(%s:%s)*"
4787 (or org-keys (or (and todo-only "M") "m")) match)
4788 (format "*Org Agenda(%s)*" (or (and todo-only "M") "m")))))
4789 ;; Prepare agendas (and `org-tag-alist-for-agenda') before
4790 ;; expanding tags within `org-make-tags-matcher'
4791 (org-agenda-prepare (concat "TAGS " match))
4792 (setq matcher (org-make-tags-matcher match)
4793 match (car matcher) matcher (cdr matcher))
4794 (org-compile-prefix-format 'tags)
4795 (org-set-sorting-strategy 'tags)
4796 (setq org-agenda-query-string match)
4797 (setq org-agenda-redo-command
4798 (list 'org-tags-view `(quote ,todo-only)
4799 (list 'if 'current-prefix-arg nil `(quote ,org-agenda-query-string))))
4800 (setq files (org-agenda-files nil 'ifmode)
4801 rtnall nil)
4802 (while (setq file (pop files))
4803 (catch 'nextfile
4804 (org-check-agenda-file file)
4805 (setq buffer (if (file-exists-p file)
4806 (org-get-agenda-file-buffer file)
4807 (error "No such file %s" file)))
4808 (if (not buffer)
4809 ;; If file does not exist, error message to agenda
4810 (setq rtn (list
4811 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4812 rtnall (append rtnall rtn))
4813 (with-current-buffer buffer
4814 (unless (derived-mode-p 'org-mode)
4815 (error "Agenda file %s is not in `org-mode'" file))
4816 (save-excursion
4817 (save-restriction
4818 (if (eq buffer org-agenda-restrict)
4819 (narrow-to-region org-agenda-restrict-begin
4820 org-agenda-restrict-end)
4821 (widen))
4822 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4823 (setq rtnall (append rtnall rtn))))))))
4824 (if org-agenda-overriding-header
4825 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4826 nil 'face 'org-agenda-structure) "\n")
4827 (insert "Headlines with TAGS match: ")
4828 (add-text-properties (point-min) (1- (point))
4829 (list 'face 'org-agenda-structure
4830 'short-heading
4831 (concat "Match: " match)))
4832 (setq pos (point))
4833 (insert match "\n")
4834 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4835 (setq pos (point))
4836 (unless org-agenda-multi
4837 (insert "Press `C-u r' to search again with new search string\n"))
4838 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4839 (org-agenda-mark-header-line (point-min))
4840 (when rtnall
4841 (insert (org-agenda-finalize-entries rtnall 'tags) "\n"))
4842 (goto-char (point-min))
4843 (or org-agenda-multi (org-agenda-fit-window-to-buffer))
4844 (add-text-properties (point-min) (point-max)
4845 `(org-agenda-type tags
4846 org-last-args (,todo-only ,match)
4847 org-redo-cmd ,org-agenda-redo-command
4848 org-series-cmd ,org-cmd))
4849 (org-agenda-finalize)
4850 (setq buffer-read-only t))))
4852 ;;; Agenda Finding stuck projects
4854 (defvar org-agenda-skip-regexp nil
4855 "Regular expression used in skipping subtrees for the agenda.
4856 This is basically a temporary global variable that can be set and then
4857 used by user-defined selections using `org-agenda-skip-function'.")
4859 (defvar org-agenda-overriding-header nil
4860 "When set during agenda, todo and tags searches it replaces the header.
4861 This variable should not be set directly, but custom commands can bind it
4862 in the options section.")
4864 (defun org-agenda-skip-entry-when-regexp-matches ()
4865 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4866 If yes, it returns the end position of this entry, causing agenda commands
4867 to skip the entry but continuing the search in the subtree. This is a
4868 function that can be put into `org-agenda-skip-function' for the duration
4869 of a command."
4870 (let ((end (save-excursion (org-end-of-subtree t)))
4871 skip)
4872 (save-excursion
4873 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4874 (and skip end)))
4876 (defun org-agenda-skip-subtree-when-regexp-matches ()
4877 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4878 If yes, it returns the end position of this tree, causing agenda commands
4879 to skip this subtree. This is a function that can be put into
4880 `org-agenda-skip-function' for the duration of a command."
4881 (let ((end (save-excursion (org-end-of-subtree t)))
4882 skip)
4883 (save-excursion
4884 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4885 (and skip end)))
4887 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4888 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4889 If yes, it returns the end position of the current entry (NOT the tree),
4890 causing agenda commands to skip the entry but continuing the search in
4891 the subtree. This is a function that can be put into
4892 `org-agenda-skip-function' for the duration of a command. An important
4893 use of this function is for the stuck project list."
4894 (let ((end (save-excursion (org-end-of-subtree t)))
4895 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4896 skip)
4897 (save-excursion
4898 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4899 (and skip entry-end)))
4901 (defun org-agenda-skip-entry-if (&rest conditions)
4902 "Skip entry if any of CONDITIONS is true.
4903 See `org-agenda-skip-if' for details."
4904 (org-agenda-skip-if nil conditions))
4906 (defun org-agenda-skip-subtree-if (&rest conditions)
4907 "Skip entry if any of CONDITIONS is true.
4908 See `org-agenda-skip-if' for details."
4909 (org-agenda-skip-if t conditions))
4911 (defun org-agenda-skip-if (subtree conditions)
4912 "Checks current entity for CONDITIONS.
4913 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4914 the entry (i.e. the text before the next heading) is checked.
4916 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4917 from different tests. Valid conditions are:
4919 scheduled Check if there is a scheduled cookie
4920 notscheduled Check if there is no scheduled cookie
4921 deadline Check if there is a deadline
4922 notdeadline Check if there is no deadline
4923 timestamp Check if there is a timestamp (also deadline or scheduled)
4924 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4925 regexp Check if regexp matches
4926 notregexp Check if regexp does not match.
4927 todo Check if TODO keyword matches
4928 nottodo Check if TODO keyword does not match
4930 The regexp is taken from the conditions list, it must come right after
4931 the `regexp' or `notregexp' element.
4933 `todo' and `nottodo' accept as an argument a list of todo
4934 keywords, which may include \"*\" to match any todo keyword.
4936 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4938 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4940 Instead of a list, a keyword class may be given. For example:
4942 (org-agenda-skip-entry-if 'nottodo 'done)
4944 would skip entries that haven't been marked with any of \"DONE\"
4945 keywords. Possible classes are: `todo', `done', `any'.
4947 If any of these conditions is met, this function returns the end point of
4948 the entity, causing the search to continue from there. This is a function
4949 that can be put into `org-agenda-skip-function' for the duration of a command."
4950 (let (beg end m)
4951 (org-back-to-heading t)
4952 (setq beg (point)
4953 end (if subtree
4954 (progn (org-end-of-subtree t) (point))
4955 (progn (outline-next-heading) (1- (point)))))
4956 (goto-char beg)
4957 (and
4959 (and (memq 'scheduled conditions)
4960 (re-search-forward org-scheduled-time-regexp end t))
4961 (and (memq 'notscheduled conditions)
4962 (not (re-search-forward org-scheduled-time-regexp end t)))
4963 (and (memq 'deadline conditions)
4964 (re-search-forward org-deadline-time-regexp end t))
4965 (and (memq 'notdeadline conditions)
4966 (not (re-search-forward org-deadline-time-regexp end t)))
4967 (and (memq 'timestamp conditions)
4968 (re-search-forward org-ts-regexp end t))
4969 (and (memq 'nottimestamp conditions)
4970 (not (re-search-forward org-ts-regexp end t)))
4971 (and (setq m (memq 'regexp conditions))
4972 (stringp (nth 1 m))
4973 (re-search-forward (nth 1 m) end t))
4974 (and (setq m (memq 'notregexp conditions))
4975 (stringp (nth 1 m))
4976 (not (re-search-forward (nth 1 m) end t)))
4977 (and (or
4978 (setq m (memq 'nottodo conditions))
4979 (setq m (memq 'todo-unblocked conditions))
4980 (setq m (memq 'nottodo-unblocked conditions))
4981 (setq m (memq 'todo conditions)))
4982 (org-agenda-skip-if-todo m end)))
4983 end)))
4985 (defun org-agenda-skip-if-todo (args end)
4986 "Helper function for `org-agenda-skip-if', do not use it directly.
4987 ARGS is a list with first element either `todo', `nottodo',
4988 `todo-unblocked' or `nottodo-unblocked'. The remainder is either
4989 a list of TODO keywords, or a state symbol `todo' or `done' or
4990 `any'."
4991 (let ((kw (car args))
4992 (arg (cadr args))
4993 todo-wds todo-re)
4994 (setq todo-wds
4995 (org-uniquify
4996 (cond
4997 ((listp arg) ;; list of keywords
4998 (if (member "*" arg)
4999 (mapcar 'substring-no-properties org-todo-keywords-1)
5000 arg))
5001 ((symbolp arg) ;; keyword class name
5002 (cond
5003 ((eq arg 'todo)
5004 (org-delete-all org-done-keywords
5005 (mapcar 'substring-no-properties
5006 org-todo-keywords-1)))
5007 ((eq arg 'done) org-done-keywords)
5008 ((eq arg 'any)
5009 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
5010 (setq todo-re
5011 (concat "^\\*+[ \t]+\\<\\("
5012 (mapconcat 'identity todo-wds "\\|")
5013 "\\)\\>"))
5014 (cond
5015 ((eq kw 'todo) (re-search-forward todo-re end t))
5016 ((eq kw 'nottodo) (not (re-search-forward todo-re end t)))
5017 ((eq kw 'todo-unblocked)
5018 (catch 'unblocked
5019 (while (re-search-forward todo-re end t)
5020 (or (org-entry-blocked-p) (throw 'unblocked t)))
5021 nil))
5022 ((eq kw 'nottodo-unblocked)
5023 (catch 'unblocked
5024 (while (re-search-forward todo-re end t)
5025 (or (org-entry-blocked-p) (throw 'unblocked nil)))
5029 ;;;###autoload
5030 (defun org-agenda-list-stuck-projects (&rest ignore)
5031 "Create agenda view for projects that are stuck.
5032 Stuck projects are project that have no next actions. For the definitions
5033 of what a project is and how to check if it stuck, customize the variable
5034 `org-stuck-projects'."
5035 (interactive)
5036 (let* ((org-agenda-skip-function
5037 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
5038 ;; We could have used org-agenda-skip-if here.
5039 (org-agenda-overriding-header
5040 (or org-agenda-overriding-header "List of stuck projects: "))
5041 (matcher (nth 0 org-stuck-projects))
5042 (todo (nth 1 org-stuck-projects))
5043 (todo-wds (if (member "*" todo)
5044 (progn
5045 (org-agenda-prepare-buffers (org-agenda-files
5046 nil 'ifmode))
5047 (org-delete-all
5048 org-done-keywords-for-agenda
5049 (copy-sequence org-todo-keywords-for-agenda)))
5050 todo))
5051 (todo-re (concat "^\\*+[ \t]+\\("
5052 (mapconcat 'identity todo-wds "\\|")
5053 "\\)\\>"))
5054 (tags (nth 2 org-stuck-projects))
5055 (tags-re (if (member "*" tags)
5056 (concat org-outline-regexp-bol
5057 (org-re ".*:[[:alnum:]_@#%]+:[ \t]*$"))
5058 (if tags
5059 (concat org-outline-regexp-bol
5060 ".*:\\("
5061 (mapconcat 'identity tags "\\|")
5062 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
5063 (gen-re (nth 3 org-stuck-projects))
5064 (re-list
5065 (delq nil
5066 (list
5067 (if todo todo-re)
5068 (if tags tags-re)
5069 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
5070 gen-re)))))
5071 (setq org-agenda-skip-regexp
5072 (if re-list
5073 (mapconcat 'identity re-list "\\|")
5074 (error "No information how to identify unstuck projects")))
5075 (org-tags-view nil matcher)
5076 (with-current-buffer org-agenda-buffer-name
5077 (setq org-agenda-redo-command
5078 `(org-agenda-list-stuck-projects ,current-prefix-arg)))))
5080 ;;; Diary integration
5082 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
5083 (defvar diary-list-entries-hook)
5084 (defvar diary-time-regexp)
5085 (defun org-get-entries-from-diary (date)
5086 "Get the (Emacs Calendar) diary entries for DATE."
5087 (require 'diary-lib)
5088 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
5089 (diary-display-hook '(fancy-diary-display))
5090 (diary-display-function 'fancy-diary-display)
5091 (pop-up-frames nil)
5092 (diary-list-entries-hook
5093 (cons 'org-diary-default-entry diary-list-entries-hook))
5094 (diary-file-name-prefix nil) ; turn this feature off
5095 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
5096 entries
5097 (org-disable-agenda-to-diary t))
5098 (save-excursion
5099 (save-window-excursion
5100 (funcall (if (fboundp 'diary-list-entries)
5101 'diary-list-entries 'list-diary-entries)
5102 date 1)))
5103 (if (not (get-buffer diary-fancy-buffer))
5104 (setq entries nil)
5105 (with-current-buffer diary-fancy-buffer
5106 (setq buffer-read-only nil)
5107 (if (zerop (buffer-size))
5108 ;; No entries
5109 (setq entries nil)
5110 ;; Omit the date and other unnecessary stuff
5111 (org-agenda-cleanup-fancy-diary)
5112 ;; Add prefix to each line and extend the text properties
5113 (if (zerop (buffer-size))
5114 (setq entries nil)
5115 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
5116 (setq entries
5117 (with-temp-buffer
5118 (insert entries) (goto-char (point-min))
5119 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
5120 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
5121 (replace-match (concat "; " (match-string 1)))))
5122 (buffer-string)))))
5123 (set-buffer-modified-p nil)
5124 (kill-buffer diary-fancy-buffer)))
5125 (when entries
5126 (setq entries (org-split-string entries "\n"))
5127 (setq entries
5128 (mapcar
5129 (lambda (x)
5130 (setq x (org-agenda-format-item "" x nil "Diary" nil 'time))
5131 ;; Extend the text properties to the beginning of the line
5132 (org-add-props x (text-properties-at (1- (length x)) x)
5133 'type "diary" 'date date 'face 'org-agenda-diary))
5134 entries)))))
5136 (defvar org-agenda-cleanup-fancy-diary-hook nil
5137 "Hook run when the fancy diary buffer is cleaned up.")
5139 (defun org-agenda-cleanup-fancy-diary ()
5140 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
5141 This gets rid of the date, the underline under the date, and
5142 the dummy entry installed by `org-mode' to ensure non-empty diary for each
5143 date. It also removes lines that contain only whitespace."
5144 (goto-char (point-min))
5145 (if (looking-at ".*?:[ \t]*")
5146 (progn
5147 (replace-match "")
5148 (re-search-forward "\n=+$" nil t)
5149 (replace-match "")
5150 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
5151 (re-search-forward "\n=+$" nil t)
5152 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
5153 (goto-char (point-min))
5154 (while (re-search-forward "^ +\n" nil t)
5155 (replace-match ""))
5156 (goto-char (point-min))
5157 (if (re-search-forward "^Org-mode dummy\n?" nil t)
5158 (replace-match ""))
5159 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
5161 ;; Make sure entries from the diary have the right text properties.
5162 (eval-after-load "diary-lib"
5163 '(if (boundp 'diary-modify-entry-list-string-function)
5164 ;; We can rely on the hook, nothing to do
5166 ;; Hook not available, must use advice to make this work
5167 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
5168 "Make the position visible."
5169 (if (and org-disable-agenda-to-diary ;; called from org-agenda
5170 (stringp string)
5171 buffer-file-name)
5172 (setq string (org-modify-diary-entry-string string))))))
5174 (defun org-modify-diary-entry-string (string)
5175 "Add text properties to string, allowing org-mode to act on it."
5176 (org-add-props string nil
5177 'mouse-face 'highlight
5178 'help-echo (if buffer-file-name
5179 (format "mouse-2 or RET jump to diary file %s"
5180 (abbreviate-file-name buffer-file-name))
5182 'org-agenda-diary-link t
5183 'org-marker (org-agenda-new-marker (point-at-bol))))
5185 (defun org-diary-default-entry ()
5186 "Add a dummy entry to the diary.
5187 Needed to avoid empty dates which mess up holiday display."
5188 ;; Catch the error if dealing with the new add-to-diary-alist
5189 (when org-disable-agenda-to-diary
5190 (condition-case nil
5191 (org-add-to-diary-list original-date "Org-mode dummy" "")
5192 (error
5193 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
5195 (defun org-add-to-diary-list (&rest args)
5196 (if (fboundp 'diary-add-to-list)
5197 (apply 'diary-add-to-list args)
5198 (apply 'add-to-diary-list args)))
5200 (defvar org-diary-last-run-time nil)
5202 ;;;###autoload
5203 (defun org-diary (&rest args)
5204 "Return diary information from org files.
5205 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
5206 It accesses org files and extracts information from those files to be
5207 listed in the diary. The function accepts arguments specifying what
5208 items should be listed. For a list of arguments allowed here, see the
5209 variable `org-agenda-entry-types'.
5211 The call in the diary file should look like this:
5213 &%%(org-diary) ~/path/to/some/orgfile.org
5215 Use a separate line for each org file to check. Or, if you omit the file name,
5216 all files listed in `org-agenda-files' will be checked automatically:
5218 &%%(org-diary)
5220 If you don't give any arguments (as in the example above), the default value
5221 of `org-agenda-entry-types' is used: (:deadline :scheduled :timestamp :sexp).
5222 So the example above may also be written as
5224 &%%(org-diary :deadline :timestamp :sexp :scheduled)
5226 The function expects the lisp variables `entry' and `date' to be provided
5227 by the caller, because this is how the calendar works. Don't use this
5228 function from a program - use `org-agenda-get-day-entries' instead."
5229 (when (> (- (org-float-time)
5230 org-agenda-last-marker-time)
5232 ;; I am not sure if this works with sticky agendas, because the marker
5233 ;; list is then no longer a global variable.
5234 (org-agenda-reset-markers))
5235 (org-compile-prefix-format 'agenda)
5236 (org-set-sorting-strategy 'agenda)
5237 (setq args (or args org-agenda-entry-types))
5238 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
5239 (list entry)
5240 (org-agenda-files t)))
5241 (time (org-float-time))
5242 file rtn results)
5243 (when (or (not org-diary-last-run-time)
5244 (> (- time
5245 org-diary-last-run-time)
5247 (org-agenda-prepare-buffers files))
5248 (setq org-diary-last-run-time time)
5249 ;; If this is called during org-agenda, don't return any entries to
5250 ;; the calendar. Org Agenda will list these entries itself.
5251 (if org-disable-agenda-to-diary (setq files nil))
5252 (while (setq file (pop files))
5253 (setq rtn (apply 'org-agenda-get-day-entries file date args))
5254 (setq results (append results rtn)))
5255 (when results
5256 (setq results
5257 (mapcar (lambda (i) (replace-regexp-in-string
5258 org-bracket-link-regexp "\\3" i)) results))
5259 (concat (org-agenda-finalize-entries results) "\n"))))
5261 ;;; Agenda entry finders
5263 (defun org-agenda-get-day-entries (file date &rest args)
5264 "Does the work for `org-diary' and `org-agenda'.
5265 FILE is the path to a file to be checked for entries. DATE is date like
5266 the one returned by `calendar-current-date'. ARGS are symbols indicating
5267 which kind of entries should be extracted. For details about these, see
5268 the documentation of `org-diary'."
5269 (setq args (or args org-agenda-entry-types))
5270 (let* ((org-startup-folded nil)
5271 (org-startup-align-all-tables nil)
5272 (buffer (if (file-exists-p file)
5273 (org-get-agenda-file-buffer file)
5274 (error "No such file %s" file)))
5275 arg results rtn deadline-results)
5276 (if (not buffer)
5277 ;; If file does not exist, make sure an error message ends up in diary
5278 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
5279 (with-current-buffer buffer
5280 (unless (derived-mode-p 'org-mode)
5281 (error "Agenda file %s is not in `org-mode'" file))
5282 (setq org-agenda-buffer (or org-agenda-buffer buffer))
5283 (let ((case-fold-search nil))
5284 (save-excursion
5285 (save-restriction
5286 (if (eq buffer org-agenda-restrict)
5287 (narrow-to-region org-agenda-restrict-begin
5288 org-agenda-restrict-end)
5289 (widen))
5290 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
5291 (while (setq arg (pop args))
5292 (cond
5293 ((and (eq arg :todo)
5294 (equal date (calendar-gregorian-from-absolute
5295 (org-today))))
5296 (setq rtn (org-agenda-get-todos))
5297 (setq results (append results rtn)))
5298 ((eq arg :timestamp)
5299 (setq rtn (org-agenda-get-blocks))
5300 (setq results (append results rtn))
5301 (setq rtn (org-agenda-get-timestamps deadline-results))
5302 (setq results (append results rtn)))
5303 ((eq arg :sexp)
5304 (setq rtn (org-agenda-get-sexps))
5305 (setq results (append results rtn)))
5306 ((eq arg :scheduled)
5307 (setq rtn (org-agenda-get-scheduled deadline-results))
5308 (setq results (append results rtn)))
5309 ((eq arg :scheduled*)
5310 (setq rtn (org-agenda-get-scheduled deadline-results t))
5311 (setq results (append results rtn)))
5312 ((eq arg :closed)
5313 (setq rtn (org-agenda-get-progress))
5314 (setq results (append results rtn)))
5315 ((eq arg :deadline)
5316 (setq rtn (org-agenda-get-deadlines))
5317 (setq deadline-results (copy-sequence rtn))
5318 (setq results (append results rtn)))
5319 ((eq arg :deadline*)
5320 (setq rtn (org-agenda-get-deadlines t))
5321 (setq deadline-results (copy-sequence rtn))
5322 (setq results (append results rtn))))))))
5323 results))))
5325 (defsubst org-em (x y list)
5326 "Is X or Y a member of LIST?"
5327 (or (memq x list) (memq y list)))
5329 (defvar org-heading-keyword-regexp-format) ; defined in org.el
5330 (defvar org-agenda-sorting-strategy-selected nil)
5332 (defun org-agenda-get-todos ()
5333 "Return the TODO information for agenda display."
5334 (let* ((props (list 'face nil
5335 'done-face 'org-agenda-done
5336 'org-not-done-regexp org-not-done-regexp
5337 'org-todo-regexp org-todo-regexp
5338 'org-complex-heading-regexp org-complex-heading-regexp
5339 'mouse-face 'highlight
5340 'help-echo
5341 (format "mouse-2 or RET jump to org file %s"
5342 (abbreviate-file-name buffer-file-name))))
5343 (regexp (format org-heading-keyword-regexp-format
5344 (cond
5345 ((and org-select-this-todo-keyword
5346 (equal org-select-this-todo-keyword "*"))
5347 org-todo-regexp)
5348 (org-select-this-todo-keyword
5349 (concat "\\("
5350 (mapconcat 'identity
5351 (org-split-string
5352 org-select-this-todo-keyword
5353 "|")
5354 "\\|") "\\)"))
5355 (t org-not-done-regexp))))
5356 marker priority category category-pos level tags todo-state ts-date ts-date-type
5357 ee txt beg end inherited-tags todo-state-end-pos)
5358 (goto-char (point-min))
5359 (while (re-search-forward regexp nil t)
5360 (catch :skip
5361 (save-match-data
5362 (beginning-of-line)
5363 (org-agenda-skip)
5364 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
5365 (unless (and (setq todo-state (org-get-todo-state))
5366 (setq todo-state-end-pos (match-end 2)))
5367 (goto-char end)
5368 (throw :skip nil))
5369 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
5370 (goto-char (1+ beg))
5371 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
5372 (throw :skip nil)))
5373 (goto-char (match-beginning 2))
5374 (setq marker (org-agenda-new-marker (match-beginning 0))
5375 category (org-get-category)
5376 ts-date (let (ts)
5377 (save-match-data
5378 (cond ((org-em 'scheduled-up 'scheduled-down
5379 org-agenda-sorting-strategy-selected)
5380 (setq ts (org-entry-get (point) "SCHEDULED")
5381 ts-date-type " scheduled"))
5382 ((org-em 'deadline-up 'deadline-down
5383 org-agenda-sorting-strategy-selected)
5384 (setq ts (org-entry-get (point) "DEADLINE")
5385 ts-date-type " deadline"))
5386 ((org-em 'ts-up 'ts-down
5387 org-agenda-sorting-strategy-selected)
5388 (setq ts (org-entry-get (point) "TIMESTAMP")
5389 ts-date-type " timestamp"))
5390 ((org-em 'tsia-up 'tsia-down
5391 org-agenda-sorting-strategy-selected)
5392 (setq ts (org-entry-get (point) "TIMESTAMP_IA")
5393 ts-date-type " timestamp_ia"))
5394 ((org-em 'timestamp-up 'timestamp-down
5395 org-agenda-sorting-strategy-selected)
5396 (setq ts (or (org-entry-get (point) "SCHEDULED")
5397 (org-entry-get (point) "DEADLINE")
5398 (org-entry-get (point) "TIMESTAMP")
5399 (org-entry-get (point) "TIMESTAMP_IA"))
5400 ts-date-type ""))
5401 (t (setq ts-date-type "")))
5402 (when ts (ignore-errors (org-time-string-to-absolute ts)))))
5403 category-pos (get-text-property (point) 'org-category-position)
5404 txt (org-trim
5405 (buffer-substring (match-beginning 2) (match-end 0)))
5406 inherited-tags
5407 (or (eq org-agenda-show-inherited-tags 'always)
5408 (and (listp org-agenda-show-inherited-tags)
5409 (memq 'todo org-agenda-show-inherited-tags))
5410 (and (eq org-agenda-show-inherited-tags t)
5411 (or (eq org-agenda-use-tag-inheritance t)
5412 (memq 'todo org-agenda-use-tag-inheritance))))
5413 tags (org-get-tags-at nil (not inherited-tags))
5414 level (make-string (org-reduced-level (org-outline-level)) ? )
5415 txt (org-agenda-format-item "" txt level category tags t)
5416 priority (1+ (org-get-priority txt)))
5417 (org-add-props txt props
5418 'org-marker marker 'org-hd-marker marker
5419 'priority priority 'org-category category
5420 'level level
5421 'ts-date ts-date
5422 'org-category-position category-pos
5423 'type (concat "todo" ts-date-type) 'todo-state todo-state)
5424 (push txt ee)
5425 (if org-agenda-todo-list-sublevels
5426 (goto-char todo-state-end-pos)
5427 (org-end-of-subtree 'invisible))))
5428 (nreverse ee)))
5430 (defun org-agenda-todo-custom-ignore-p (time n)
5431 "Check whether timestamp is farther away than n number of days.
5432 This function is invoked if `org-agenda-todo-ignore-deadlines',
5433 `org-agenda-todo-ignore-scheduled' or
5434 `org-agenda-todo-ignore-timestamp' is set to an integer."
5435 (let ((days (org-time-stamp-to-now
5436 time org-agenda-todo-ignore-time-comparison-use-seconds)))
5437 (if (>= n 0)
5438 (>= days n)
5439 (<= days n))))
5441 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
5442 (&optional end)
5443 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
5444 (when (or org-agenda-todo-ignore-with-date
5445 org-agenda-todo-ignore-scheduled
5446 org-agenda-todo-ignore-deadlines
5447 org-agenda-todo-ignore-timestamp)
5448 (setq end (or end (save-excursion (outline-next-heading) (point))))
5449 (save-excursion
5450 (or (and org-agenda-todo-ignore-with-date
5451 (re-search-forward org-ts-regexp end t))
5452 (and org-agenda-todo-ignore-scheduled
5453 (re-search-forward org-scheduled-time-regexp end t)
5454 (cond
5455 ((eq org-agenda-todo-ignore-scheduled 'future)
5456 (> (org-time-stamp-to-now
5457 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5458 ((eq org-agenda-todo-ignore-scheduled 'past)
5459 (<= (org-time-stamp-to-now
5460 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5461 ((numberp org-agenda-todo-ignore-scheduled)
5462 (org-agenda-todo-custom-ignore-p
5463 (match-string 1) org-agenda-todo-ignore-scheduled))
5464 (t)))
5465 (and org-agenda-todo-ignore-deadlines
5466 (re-search-forward org-deadline-time-regexp end t)
5467 (cond
5468 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
5469 ((eq org-agenda-todo-ignore-deadlines 'far)
5470 (not (org-deadline-close (match-string 1))))
5471 ((eq org-agenda-todo-ignore-deadlines 'future)
5472 (> (org-time-stamp-to-now
5473 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5474 ((eq org-agenda-todo-ignore-deadlines 'past)
5475 (<= (org-time-stamp-to-now
5476 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5477 ((numberp org-agenda-todo-ignore-deadlines)
5478 (org-agenda-todo-custom-ignore-p
5479 (match-string 1) org-agenda-todo-ignore-deadlines))
5480 (t (org-deadline-close (match-string 1)))))
5481 (and org-agenda-todo-ignore-timestamp
5482 (let ((buffer (current-buffer))
5483 (regexp
5484 (concat
5485 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
5486 (start (point)))
5487 ;; Copy current buffer into a temporary one
5488 (with-temp-buffer
5489 (insert-buffer-substring buffer start end)
5490 (goto-char (point-min))
5491 ;; Delete SCHEDULED and DEADLINE items
5492 (while (re-search-forward regexp end t)
5493 (delete-region (match-beginning 0) (match-end 0)))
5494 (goto-char (point-min))
5495 ;; No search for timestamp left
5496 (when (re-search-forward org-ts-regexp nil t)
5497 (cond
5498 ((eq org-agenda-todo-ignore-timestamp 'future)
5499 (> (org-time-stamp-to-now
5500 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5501 ((eq org-agenda-todo-ignore-timestamp 'past)
5502 (<= (org-time-stamp-to-now
5503 (match-string 1) org-agenda-todo-ignore-time-comparison-use-seconds) 0))
5504 ((numberp org-agenda-todo-ignore-timestamp)
5505 (org-agenda-todo-custom-ignore-p
5506 (match-string 1) org-agenda-todo-ignore-timestamp))
5507 (t))))))))))
5509 (defun org-agenda-get-timestamps (&optional deadline-results)
5510 "Return the date stamp information for agenda display."
5511 (let* ((props (list 'face 'org-agenda-calendar-event
5512 'org-not-done-regexp org-not-done-regexp
5513 'org-todo-regexp org-todo-regexp
5514 'org-complex-heading-regexp org-complex-heading-regexp
5515 'mouse-face 'highlight
5516 'help-echo
5517 (format "mouse-2 or RET jump to org file %s"
5518 (abbreviate-file-name buffer-file-name))))
5519 (d1 (calendar-absolute-from-gregorian date))
5521 (deadline-position-alist
5522 (mapcar (lambda (a) (and (setq mm (get-text-property
5523 0 'org-hd-marker a))
5524 (cons (marker-position mm) a)))
5525 deadline-results))
5526 (remove-re org-ts-regexp)
5527 (regexp
5528 (concat
5529 (if org-agenda-include-inactive-timestamps "[[<]" "<")
5530 (regexp-quote
5531 (substring
5532 (format-time-string
5533 (car org-time-stamp-formats)
5534 (apply 'encode-time ; DATE bound by calendar
5535 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5536 1 11))
5537 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[hdwmy]>\\)"
5538 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
5539 marker hdmarker deadlinep scheduledp clockp closedp inactivep
5540 donep tmp priority category category-pos level ee txt timestr tags
5541 b0 b3 e3 head todo-state end-of-match show-all warntime habitp
5542 inherited-tags ts-date)
5543 (goto-char (point-min))
5544 (while (setq end-of-match (re-search-forward regexp nil t))
5545 (setq b0 (match-beginning 0)
5546 b3 (match-beginning 3) e3 (match-end 3)
5547 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
5548 habitp (and (functionp 'org-is-habit-p) (save-match-data (org-is-habit-p)))
5549 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5550 (member todo-state
5551 org-agenda-repeating-timestamp-show-all)))
5552 (catch :skip
5553 (and (org-at-date-range-p) (throw :skip nil))
5554 (org-agenda-skip)
5555 (if (and (match-end 1)
5556 (not (= d1 (org-time-string-to-absolute
5557 (match-string 1) d1 nil show-all
5558 (current-buffer) b0))))
5559 (throw :skip nil))
5560 (if (and e3
5561 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
5562 (throw :skip nil))
5563 (setq tmp (buffer-substring (max (point-min)
5564 (- b0 org-ds-keyword-length))
5566 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
5567 inactivep (= (char-after b0) ?\[)
5568 deadlinep (string-match org-deadline-regexp tmp)
5569 scheduledp (string-match org-scheduled-regexp tmp)
5570 closedp (and org-agenda-include-inactive-timestamps
5571 (string-match org-closed-string tmp))
5572 clockp (and org-agenda-include-inactive-timestamps
5573 (or (string-match org-clock-string tmp)
5574 (string-match "]-+\\'" tmp)))
5575 warntime (get-text-property (point) 'org-appt-warntime)
5576 donep (member todo-state org-done-keywords))
5577 (if (or scheduledp deadlinep closedp clockp
5578 (and donep org-agenda-skip-timestamp-if-done))
5579 (throw :skip t))
5580 (if (string-match ">" timestr)
5581 ;; substring should only run to end of time stamp
5582 (setq timestr (substring timestr 0 (match-end 0))))
5583 (setq marker (org-agenda-new-marker b0)
5584 category (org-get-category b0)
5585 category-pos (get-text-property b0 'org-category-position))
5586 (save-excursion
5587 (if (not (re-search-backward org-outline-regexp-bol nil t))
5588 (throw :skip nil)
5589 (goto-char (match-beginning 0))
5590 (if (and (eq t org-agenda-skip-timestamp-if-deadline-is-shown)
5591 (assoc (point) deadline-position-alist))
5592 (throw :skip nil))
5593 (setq hdmarker (org-agenda-new-marker)
5594 inherited-tags
5595 (or (eq org-agenda-show-inherited-tags 'always)
5596 (and (listp org-agenda-show-inherited-tags)
5597 (memq 'agenda org-agenda-show-inherited-tags))
5598 (and (eq org-agenda-show-inherited-tags t)
5599 (or (eq org-agenda-use-tag-inheritance t)
5600 (memq 'agenda org-agenda-use-tag-inheritance))))
5601 tags (org-get-tags-at nil (not inherited-tags))
5602 level (make-string (org-reduced-level (org-outline-level)) ? ))
5603 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5604 (setq head (or (match-string 1) ""))
5605 (setq txt (org-agenda-format-item
5606 (if inactivep org-agenda-inactive-leader nil)
5607 head level category tags timestr
5608 remove-re habitp)))
5609 (setq priority (org-get-priority txt))
5610 (org-add-props txt props 'priority priority
5611 'org-marker marker 'org-hd-marker hdmarker
5612 'org-category category 'date date
5613 'level level
5614 'ts-date
5615 (ignore-errors (org-time-string-to-absolute timestr))
5616 'org-category-position category-pos
5617 'todo-state todo-state
5618 'warntime warntime
5619 'type "timestamp")
5620 (push txt ee))
5621 (if org-agenda-skip-additional-timestamps-same-entry
5622 (outline-next-heading)
5623 (goto-char end-of-match))))
5624 (nreverse ee)))
5626 (defun org-agenda-get-sexps ()
5627 "Return the sexp information for agenda display."
5628 (require 'diary-lib)
5629 (let* ((props (list 'face 'org-agenda-calendar-sexp
5630 'mouse-face 'highlight
5631 'help-echo
5632 (format "mouse-2 or RET jump to org file %s"
5633 (abbreviate-file-name buffer-file-name))))
5634 (regexp "^&?%%(")
5635 marker category extra category-pos level ee txt tags entry
5636 result beg b sexp sexp-entry todo-state warntime inherited-tags)
5637 (goto-char (point-min))
5638 (while (re-search-forward regexp nil t)
5639 (catch :skip
5640 (org-agenda-skip)
5641 (setq beg (match-beginning 0))
5642 (goto-char (1- (match-end 0)))
5643 (setq b (point))
5644 (forward-sexp 1)
5645 (setq sexp (buffer-substring b (point)))
5646 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
5647 (org-trim (match-string 1))
5648 ""))
5649 (setq result (org-diary-sexp-entry sexp sexp-entry date))
5650 (when result
5651 (setq marker (org-agenda-new-marker beg)
5652 level (make-string (org-reduced-level (org-outline-level)) ? )
5653 category (org-get-category beg)
5654 category-pos (get-text-property beg 'org-category-position)
5655 inherited-tags
5656 (or (eq org-agenda-show-inherited-tags 'always)
5657 (and (listp org-agenda-show-inherited-tags)
5658 (memq 'agenda org-agenda-show-inherited-tags))
5659 (and (eq org-agenda-show-inherited-tags t)
5660 (or (eq org-agenda-use-tag-inheritance t)
5661 (memq 'agenda org-agenda-use-tag-inheritance))))
5662 tags (org-get-tags-at nil (not inherited-tags))
5663 todo-state (org-get-todo-state)
5664 warntime (get-text-property (point) 'org-appt-warntime)
5665 extra nil)
5667 (dolist (r (if (stringp result)
5668 (list result)
5669 result)) ;; we expect a list here
5670 (when (and org-agenda-diary-sexp-prefix
5671 (string-match org-agenda-diary-sexp-prefix r))
5672 (setq extra (match-string 0 r)
5673 r (replace-match "" nil nil r)))
5674 (if (string-match "\\S-" r)
5675 (setq txt r)
5676 (setq txt "SEXP entry returned empty string"))
5677 (setq txt (org-agenda-format-item extra txt level category tags 'time))
5678 (org-add-props txt props 'org-marker marker
5679 'org-category category 'date date 'todo-state todo-state
5680 'org-category-position category-pos 'tags tags
5681 'level level
5682 'type "sexp" 'warntime warntime)
5683 (push txt ee)))))
5684 (nreverse ee)))
5686 ;; Calendar sanity: define some functions that are independent of
5687 ;; `calendar-date-style'.
5688 ;; Normally I would like to use ISO format when calling the diary functions,
5689 ;; but to make sure we still have Emacs 22 compatibility we bind
5690 ;; also `european-calendar-style' and use european format
5691 (defun org-anniversary (year month day &optional mark)
5692 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
5693 (org-no-warnings
5694 (let ((calendar-date-style 'european) (european-calendar-style t))
5695 (diary-anniversary day month year mark))))
5696 (defun org-cyclic (N year month day &optional mark)
5697 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
5698 (org-no-warnings
5699 (let ((calendar-date-style 'european) (european-calendar-style t))
5700 (diary-cyclic N day month year mark))))
5701 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
5702 "Like `diary-block', but with fixed (ISO) order of arguments."
5703 (org-no-warnings
5704 (let ((calendar-date-style 'european) (european-calendar-style t))
5705 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
5706 (defun org-date (year month day &optional mark)
5707 "Like `diary-date', but with fixed (ISO) order of arguments."
5708 (org-no-warnings
5709 (let ((calendar-date-style 'european) (european-calendar-style t))
5710 (diary-date day month year mark))))
5712 ;; Define the` org-class' function
5713 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
5714 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
5715 DAYNAME is a number between 0 (Sunday) and 6 (Saturday).
5716 SKIP-WEEKS is any number of ISO weeks in the block period for which the
5717 item should be skipped. If any of the SKIP-WEEKS arguments is the symbol
5718 `holidays', then any date that is known by the Emacs calendar to be a
5719 holiday will also be skipped. If SKIP-WEEKS arguments are holiday strings,
5720 then those holidays will be skipped."
5721 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
5722 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
5723 (d (calendar-absolute-from-gregorian date))
5724 (h (when skip-weeks (calendar-check-holidays date))))
5725 (and
5726 (<= date1 d)
5727 (<= d date2)
5728 (= (calendar-day-of-week date) dayname)
5729 (or (not skip-weeks)
5730 (progn
5731 (require 'cal-iso)
5732 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
5733 (not (or (and h (memq 'holidays skip-weeks))
5734 (delq nil (mapcar (lambda(g) (member g skip-weeks)) h))))
5735 entry)))
5737 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
5738 "Like `org-class', but honor `calendar-date-style'.
5739 The order of the first 2 times 3 arguments depends on the variable
5740 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
5741 So for American calendars, give this as MONTH DAY YEAR, for European as
5742 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
5743 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
5744 is any number of ISO weeks in the block period for which the item should
5745 be skipped.
5747 This function is here only for backward compatibility and it is deprecated,
5748 please use `org-class' instead."
5749 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
5750 (date2 (org-order-calendar-date-args m2 d2 y2)))
5751 (org-class
5752 (nth 2 date1) (car date1) (nth 1 date1)
5753 (nth 2 date2) (car date2) (nth 1 date2)
5754 dayname skip-weeks)))
5755 (make-obsolete 'org-diary-class 'org-class "")
5757 (defvar org-agenda-show-log-scoped) ;; dynamically scope in `org-timeline' or `org-agenda-list'
5758 (defalias 'org-get-closed 'org-agenda-get-progress)
5759 (defun org-agenda-get-progress ()
5760 "Return the logged TODO entries for agenda display."
5761 (let* ((props (list 'mouse-face 'highlight
5762 'org-not-done-regexp org-not-done-regexp
5763 'org-todo-regexp org-todo-regexp
5764 'org-complex-heading-regexp org-complex-heading-regexp
5765 'help-echo
5766 (format "mouse-2 or RET jump to org file %s"
5767 (abbreviate-file-name buffer-file-name))))
5768 (items (if (consp org-agenda-show-log-scoped)
5769 org-agenda-show-log-scoped
5770 (if (eq org-agenda-show-log-scoped 'clockcheck)
5771 '(clock)
5772 org-agenda-log-mode-items)))
5773 (parts
5774 (delq nil
5775 (list
5776 (if (memq 'closed items) (concat "\\<" org-closed-string))
5777 (if (memq 'clock items) (concat "\\<" org-clock-string))
5778 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
5779 (parts-re (if parts (mapconcat 'identity parts "\\|")
5780 (error "`org-agenda-log-mode-items' is empty")))
5781 (regexp (concat
5782 "\\(" parts-re "\\)"
5783 " *\\["
5784 (regexp-quote
5785 (substring
5786 (format-time-string
5787 (car org-time-stamp-formats)
5788 (apply 'encode-time ; DATE bound by calendar
5789 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
5790 1 11))))
5791 (org-agenda-search-headline-for-time nil)
5792 marker hdmarker priority category category-pos level tags closedp
5793 statep clockp state ee txt extra timestr rest clocked inherited-tags)
5794 (goto-char (point-min))
5795 (while (re-search-forward regexp nil t)
5796 (catch :skip
5797 (org-agenda-skip)
5798 (setq marker (org-agenda-new-marker (match-beginning 0))
5799 closedp (equal (match-string 1) org-closed-string)
5800 statep (equal (string-to-char (match-string 1)) ?-)
5801 clockp (not (or closedp statep))
5802 state (and statep (match-string 2))
5803 category (org-get-category (match-beginning 0))
5804 category-pos (get-text-property (match-beginning 0) 'org-category-position)
5805 timestr (buffer-substring (match-beginning 0) (point-at-eol)))
5806 (when (string-match "\\]" timestr)
5807 ;; substring should only run to end of time stamp
5808 (setq rest (substring timestr (match-end 0))
5809 timestr (substring timestr 0 (match-end 0)))
5810 (if (and (not closedp) (not statep)
5811 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)"
5812 rest))
5813 (progn (setq timestr (concat (substring timestr 0 -1)
5814 "-" (match-string 1 rest) "]"))
5815 (setq clocked (match-string 2 rest)))
5816 (setq clocked "-")))
5817 (save-excursion
5818 (setq extra
5819 (cond
5820 ((not org-agenda-log-mode-add-notes) nil)
5821 (statep
5822 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
5823 (match-string 1)))
5824 (clockp
5825 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
5826 (match-string 1)))))
5827 (if (not (re-search-backward org-outline-regexp-bol nil t))
5828 (throw :skip nil)
5829 (goto-char (match-beginning 0))
5830 (setq hdmarker (org-agenda-new-marker)
5831 inherited-tags
5832 (or (eq org-agenda-show-inherited-tags 'always)
5833 (and (listp org-agenda-show-inherited-tags)
5834 (memq 'todo org-agenda-show-inherited-tags))
5835 (and (eq org-agenda-show-inherited-tags t)
5836 (or (eq org-agenda-use-tag-inheritance t)
5837 (memq 'todo org-agenda-use-tag-inheritance))))
5838 tags (org-get-tags-at nil (not inherited-tags))
5839 level (make-string (org-reduced-level (org-outline-level)) ? ))
5840 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5841 (setq txt (match-string 1))
5842 (when extra
5843 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
5844 (setq txt (concat (substring txt 0 (match-beginning 1))
5845 " - " extra " " (match-string 2 txt)))
5846 (setq txt (concat txt " - " extra))))
5847 (setq txt (org-agenda-format-item
5848 (cond
5849 (closedp "Closed: ")
5850 (statep (concat "State: (" state ")"))
5851 (t (concat "Clocked: (" clocked ")")))
5852 txt level category tags timestr)))
5853 (setq priority 100000)
5854 (org-add-props txt props
5855 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
5856 'priority priority 'org-category category
5857 'org-category-position category-pos
5858 'level level
5859 'type "closed" 'date date
5860 'undone-face 'org-warning 'done-face 'org-agenda-done)
5861 (push txt ee))
5862 (goto-char (point-at-eol))))
5863 (nreverse ee)))
5865 (defun org-agenda-show-clocking-issues ()
5866 "Add overlays, showing issues with clocking.
5867 See also the user option `org-agenda-clock-consistency-checks'."
5868 (interactive)
5869 (let* ((org-time-clocksum-use-effort-durations nil)
5870 (pl org-agenda-clock-consistency-checks)
5871 (re (concat "^[ \t]*"
5872 org-clock-string
5873 "[ \t]+"
5874 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5875 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5876 (tlstart 0.)
5877 (tlend 0.)
5878 (maxtime (org-hh:mm-string-to-minutes
5879 (or (plist-get pl :max-duration) "24:00")))
5880 (mintime (org-hh:mm-string-to-minutes
5881 (or (plist-get pl :min-duration) 0)))
5882 (maxgap (org-hh:mm-string-to-minutes
5883 ;; default 30:00 means never complain
5884 (or (plist-get pl :max-gap) "30:00")))
5885 (gapok (mapcar 'org-hh:mm-string-to-minutes
5886 (plist-get pl :gap-ok-around)))
5887 (def-face (or (plist-get pl :default-face)
5888 '((:background "DarkRed") (:foreground "white"))))
5889 issue face m te ts dt ov)
5890 (goto-char (point-min))
5891 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5892 (setq issue nil face def-face)
5893 (catch 'next
5894 (setq m (org-get-at-bol 'org-marker)
5895 te nil ts nil)
5896 (unless (and m (markerp m))
5897 (setq issue "No valid clock line") (throw 'next t))
5898 (org-with-point-at m
5899 (save-excursion
5900 (goto-char (point-at-bol))
5901 (unless (looking-at re)
5902 (error "No valid Clock line")
5903 (throw 'next t))
5904 (unless (match-end 3)
5905 (setq issue "No end time"
5906 face (or (plist-get pl :no-end-time-face) face))
5907 (throw 'next t))
5908 (setq ts (match-string 1)
5909 te (match-string 3)
5910 ts (org-float-time
5911 (apply 'encode-time (org-parse-time-string ts)))
5912 te (org-float-time
5913 (apply 'encode-time (org-parse-time-string te)))
5914 dt (- te ts))))
5915 (cond
5916 ((> dt (* 60 maxtime))
5917 ;; a very long clocking chunk
5918 (setq issue (format "Clocking interval is very long: %s"
5919 (org-minutes-to-clocksum-string
5920 (floor (/ (float dt) 60.))))
5921 face (or (plist-get pl :long-face) face)))
5922 ((< dt (* 60 mintime))
5923 ;; a very short clocking chunk
5924 (setq issue (format "Clocking interval is very short: %s"
5925 (org-minutes-to-clocksum-string
5926 (floor (/ (float dt) 60.))))
5927 face (or (plist-get pl :short-face) face)))
5928 ((and (> tlend 0) (< ts tlend))
5929 ;; Two clock entries are overlapping
5930 (setq issue (format "Clocking overlap: %d minutes"
5931 (/ (- tlend ts) 60))
5932 face (or (plist-get pl :overlap-face) face)))
5933 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5934 ;; There is a gap, lets see if we need to report it
5935 (unless (org-agenda-check-clock-gap tlend ts gapok)
5936 (setq issue (format "Clocking gap: %d minutes"
5937 (/ (- ts tlend) 60))
5938 face (or (plist-get pl :gap-face) face))))
5939 (t nil)))
5940 (setq tlend (or te tlend) tlstart (or ts tlstart))
5941 (when issue
5942 ;; OK, there was some issue, add an overlay to show the issue
5943 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5944 (overlay-put ov 'before-string
5945 (concat
5946 (org-add-props
5947 (format "%-43s" (concat " " issue))
5949 'face face)
5950 "\n"))
5951 (overlay-put ov 'evaporate t)))))
5953 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5954 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5955 (catch 'exit
5956 (unless ok-list
5957 ;; there are no OK times for gaps...
5958 (throw 'exit nil))
5959 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5960 ;; This is more than 24 hours, so it is OK.
5961 ;; because we have at least one OK time, that must be in the
5962 ;; 24 hour interval.
5963 (throw 'exit t))
5964 ;; We have a shorter gap.
5965 ;; Now we have to get the minute of the day when these times are
5966 (let* ((t1dec (decode-time (seconds-to-time t1)))
5967 (t2dec (decode-time (seconds-to-time t2)))
5968 ;; compute the minute on the day
5969 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5970 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5971 (when (< min2 min1)
5972 ;; if min2 is smaller than min1, this means it is on the next day.
5973 ;; Wrap it to after midnight.
5974 (setq min2 (+ min2 1440)))
5975 ;; Now check if any of the OK times is in the gap
5976 (mapc (lambda (x)
5977 ;; Wrap the time to after midnight if necessary
5978 (if (< x min1) (setq x (+ x 1440)))
5979 ;; Check if in interval
5980 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5981 ok-list)
5982 ;; Nope, this gap is not OK
5983 nil)))
5985 (defun org-agenda-get-deadlines (&optional with-hour)
5986 "Return the deadline information for agenda display.
5987 When WITH-HOUR is non-nil, only return deadlines with an hour
5988 specification like [h]h:mm."
5989 (let* ((props (list 'mouse-face 'highlight
5990 'org-not-done-regexp org-not-done-regexp
5991 'org-todo-regexp org-todo-regexp
5992 'org-complex-heading-regexp org-complex-heading-regexp
5993 'help-echo
5994 (format "mouse-2 or RET jump to org file %s"
5995 (abbreviate-file-name buffer-file-name))))
5996 (regexp (if with-hour
5997 org-deadline-time-hour-regexp
5998 org-deadline-time-regexp))
5999 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6000 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6001 (dl0 (car org-agenda-deadline-leaders))
6002 (dl1 (nth 1 org-agenda-deadline-leaders))
6003 (dl2 (or (nth 2 org-agenda-deadline-leaders) dl1))
6004 d2 diff dfrac wdays pos pos1 category category-pos level
6005 tags suppress-prewarning ee txt head face s todo-state
6006 show-all upcomingp donep timestr warntime inherited-tags ts-date)
6007 (goto-char (point-min))
6008 (while (re-search-forward regexp nil t)
6009 (catch :skip
6010 (org-agenda-skip)
6011 (setq s (match-string 1)
6012 txt nil
6013 pos (1- (match-beginning 1))
6014 todo-state (save-match-data (org-get-todo-state))
6015 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6016 (member todo-state
6017 org-agenda-repeating-timestamp-show-all))
6018 d2 (org-time-string-to-absolute
6019 s d1 'past show-all (current-buffer) pos)
6020 diff (- d2 d1))
6021 (setq suppress-prewarning
6022 (let ((ds (and org-agenda-skip-deadline-prewarning-if-scheduled
6023 (let ((item (buffer-substring (point-at-bol)
6024 (point-at-eol))))
6025 (save-match-data
6026 (and (string-match
6027 org-scheduled-time-regexp item)
6028 (match-string 1 item)))))))
6029 (cond
6030 ((not ds) nil)
6031 ;; The current item has a scheduled date (in ds), so
6032 ;; evaluate its prewarning lead time.
6033 ((integerp org-agenda-skip-deadline-prewarning-if-scheduled)
6034 ;; Use global prewarning-restart lead time.
6035 org-agenda-skip-deadline-prewarning-if-scheduled)
6036 ((eq org-agenda-skip-deadline-prewarning-if-scheduled
6037 'pre-scheduled)
6038 ;; Set prewarning to no earlier than scheduled.
6039 (min (- d2 (org-time-string-to-absolute
6040 ds d1 'past show-all (current-buffer) pos))
6041 org-deadline-warning-days))
6042 ;; Set prewarning to deadline.
6043 (t 0))))
6044 (setq wdays (if suppress-prewarning
6045 (let ((org-deadline-warning-days suppress-prewarning))
6046 (org-get-wdays s))
6047 (org-get-wdays s))
6048 dfrac (- 1 (/ (* 1.0 diff) (max wdays 1)))
6049 upcomingp (and todayp (> diff 0)))
6050 ;; When to show a deadline in the calendar:
6051 ;; If the expiration is within wdays warning time.
6052 ;; Past-due deadlines are only shown on the current date
6053 (if (and (or (and (<= diff wdays)
6054 (and todayp (not org-agenda-only-exact-dates)))
6055 (= diff 0)))
6056 (save-excursion
6057 ;; (setq todo-state (org-get-todo-state))
6058 (setq donep (member todo-state org-done-keywords))
6059 (if (and donep
6060 (or org-agenda-skip-deadline-if-done
6061 (not (= diff 0))))
6062 (setq txt nil)
6063 (setq category (org-get-category)
6064 warntime (get-text-property (point) 'org-appt-warntime)
6065 category-pos (get-text-property (point) 'org-category-position))
6066 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6067 (throw :skip nil)
6068 (goto-char (match-end 0))
6069 (setq pos1 (match-beginning 0))
6070 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6071 (setq inherited-tags
6072 (or (eq org-agenda-show-inherited-tags 'always)
6073 (and (listp org-agenda-show-inherited-tags)
6074 (memq 'agenda org-agenda-show-inherited-tags))
6075 (and (eq org-agenda-show-inherited-tags t)
6076 (or (eq org-agenda-use-tag-inheritance t)
6077 (memq 'agenda org-agenda-use-tag-inheritance))))
6078 tags (org-get-tags-at pos1 (not inherited-tags)))
6079 (setq head (buffer-substring
6080 (point)
6081 (progn (skip-chars-forward "^\r\n")
6082 (point))))
6083 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6084 (setq timestr
6085 (concat (substring s (match-beginning 1)) " "))
6086 (setq timestr 'time))
6087 (setq txt (org-agenda-format-item
6088 (cond ((= diff 0) dl0)
6089 ((> diff 0)
6090 (if (functionp dl1)
6091 (funcall dl1 diff date)
6092 (format dl1 diff)))
6094 (if (functionp dl2)
6095 (funcall dl2 diff date)
6096 (format dl2 (if (string= dl2 dl1)
6097 diff (abs diff))))))
6098 head level category tags
6099 (if (not (= diff 0)) nil timestr)))))
6100 (when txt
6101 (setq face (org-agenda-deadline-face dfrac))
6102 (org-add-props txt props
6103 'org-marker (org-agenda-new-marker pos)
6104 'warntime warntime
6105 'level level
6106 'ts-date d2
6107 'org-hd-marker (org-agenda-new-marker pos1)
6108 'priority (+ (- diff)
6109 (org-get-priority txt))
6110 'org-category category
6111 'org-category-position category-pos
6112 'todo-state todo-state
6113 'type (if upcomingp "upcoming-deadline" "deadline")
6114 'date (if upcomingp date d2)
6115 'face (if donep 'org-agenda-done face)
6116 'undone-face face 'done-face 'org-agenda-done)
6117 (push txt ee))))))
6118 (nreverse ee)))
6120 (defun org-agenda-deadline-face (fraction)
6121 "Return the face to displaying a deadline item.
6122 FRACTION is what fraction of the head-warning time has passed."
6123 (let ((faces org-agenda-deadline-faces) f)
6124 (catch 'exit
6125 (while (setq f (pop faces))
6126 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
6128 (defun org-agenda-get-scheduled (&optional deadline-results with-hour)
6129 "Return the scheduled information for agenda display.
6130 When WITH-HOUR is non-nil, only return scheduled items with
6131 an hour specification like [h]h:mm."
6132 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
6133 'org-todo-regexp org-todo-regexp
6134 'org-complex-heading-regexp org-complex-heading-regexp
6135 'done-face 'org-agenda-done
6136 'mouse-face 'highlight
6137 'help-echo
6138 (format "mouse-2 or RET jump to org file %s"
6139 (abbreviate-file-name buffer-file-name))))
6140 (regexp (if with-hour
6141 org-scheduled-time-hour-regexp
6142 org-scheduled-time-regexp))
6143 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
6144 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
6146 (deadline-position-alist
6147 (mapcar (lambda (a) (and (setq mm (get-text-property
6148 0 'org-hd-marker a))
6149 (cons (marker-position mm) a)))
6150 deadline-results))
6151 d2 diff pos pos1 category category-pos level tags donep
6152 ee txt head pastschedp todo-state face timestr s habitp show-all
6153 did-habit-check-p warntime inherited-tags ts-date suppress-delay
6154 ddays)
6155 (goto-char (point-min))
6156 (while (re-search-forward regexp nil t)
6157 (catch :skip
6158 (org-agenda-skip)
6159 (setq s (match-string 1)
6160 txt nil
6161 pos (1- (match-beginning 1))
6162 todo-state (save-match-data (org-get-todo-state))
6163 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
6164 (member todo-state
6165 org-agenda-repeating-timestamp-show-all))
6166 d2 (org-time-string-to-absolute
6167 s d1 'past show-all (current-buffer) pos)
6168 diff (- d2 d1)
6169 warntime (get-text-property (point) 'org-appt-warntime))
6170 (setq pastschedp (and todayp (< diff 0)))
6171 (setq did-habit-check-p nil)
6172 (setq suppress-delay
6173 (let ((ds (and org-agenda-skip-scheduled-delay-if-deadline
6174 (let ((item (buffer-substring (point-at-bol) (point-at-eol))))
6175 (save-match-data
6176 (and (string-match
6177 org-deadline-time-regexp item)
6178 (match-string 1 item)))))))
6179 (cond
6180 ((not ds) nil)
6181 ;; The current item has a deadline date (in ds), so
6182 ;; evaluate its delay time.
6183 ((integerp org-agenda-skip-scheduled-delay-if-deadline)
6184 ;; Use global delay time.
6185 (- org-agenda-skip-scheduled-delay-if-deadline))
6186 ((eq org-agenda-skip-scheduled-delay-if-deadline
6187 'post-deadline)
6188 ;; Set delay to no later than deadline.
6189 (min (- d2 (org-time-string-to-absolute
6190 ds d1 'past show-all (current-buffer) pos))
6191 org-scheduled-delay-days))
6192 (t 0))))
6193 (setq ddays (if suppress-delay
6194 (let ((org-scheduled-delay-days suppress-delay))
6195 (org-get-wdays s t t))
6196 (org-get-wdays s t)))
6197 ;; Use a delay of 0 when there is a repeater and the delay is
6198 ;; of the form --3d
6199 (when (and (save-match-data (string-match "--[0-9]+[hdwmy]" s))
6200 (< (org-time-string-to-absolute s)
6201 (org-time-string-to-absolute
6202 s d2 'past nil (current-buffer) pos)))
6203 (setq ddays 0))
6204 ;; When to show a scheduled item in the calendar:
6205 ;; If it is on or past the date.
6206 (when (or (and (> ddays 0) (= diff (- ddays)))
6207 (and (zerop ddays) (= diff 0))
6208 (and (< (+ diff ddays) 0)
6209 (< (abs diff) org-scheduled-past-days)
6210 (and todayp (not org-agenda-only-exact-dates)))
6211 ;; org-is-habit-p uses org-entry-get, which is expansive
6212 ;; so we go extra mile to only call it once
6213 (and todayp
6214 (boundp 'org-habit-show-all-today)
6215 org-habit-show-all-today
6216 (setq did-habit-check-p t)
6217 (setq habitp (and (functionp 'org-is-habit-p)
6218 (org-is-habit-p)))))
6219 (save-excursion
6220 (setq donep (member todo-state org-done-keywords))
6221 (if (and donep
6222 (or org-agenda-skip-scheduled-if-done
6223 (not (= diff 0))
6224 (and (functionp 'org-is-habit-p)
6225 (org-is-habit-p))))
6226 (setq txt nil)
6227 (setq habitp (if did-habit-check-p habitp
6228 (and (functionp 'org-is-habit-p)
6229 (org-is-habit-p))))
6230 (setq category (org-get-category)
6231 category-pos (get-text-property (point) 'org-category-position))
6232 (if (and (eq org-agenda-skip-scheduled-if-deadline-is-shown
6233 'repeated-after-deadline)
6234 (<= 0 (- d2 (time-to-days (org-get-deadline-time (point))))))
6235 (throw :skip nil))
6236 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
6237 (throw :skip nil)
6238 (goto-char (match-end 0))
6239 (setq pos1 (match-beginning 0))
6240 (if habitp
6241 (if (or (not org-habit-show-habits)
6242 (and (not todayp)
6243 (boundp 'org-habit-show-habits-only-for-today)
6244 org-habit-show-habits-only-for-today))
6245 (throw :skip nil))
6246 (if (and
6247 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
6248 (and (eq org-agenda-skip-scheduled-if-deadline-is-shown 'not-today)
6249 pastschedp))
6250 (setq mm (assoc pos1 deadline-position-alist)))
6251 (throw :skip nil)))
6252 (setq inherited-tags
6253 (or (eq org-agenda-show-inherited-tags 'always)
6254 (and (listp org-agenda-show-inherited-tags)
6255 (memq 'agenda org-agenda-show-inherited-tags))
6256 (and (eq org-agenda-show-inherited-tags t)
6257 (or (eq org-agenda-use-tag-inheritance t)
6258 (memq 'agenda org-agenda-use-tag-inheritance))))
6260 tags (org-get-tags-at nil (not inherited-tags)))
6261 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6262 (setq head (buffer-substring
6263 (point)
6264 (progn (skip-chars-forward "^\r\n") (point))))
6265 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
6266 (setq timestr
6267 (concat (substring s (match-beginning 1)) " "))
6268 (setq timestr 'time))
6269 (setq txt (org-agenda-format-item
6270 (if (= diff 0)
6271 (car org-agenda-scheduled-leaders)
6272 (format (nth 1 org-agenda-scheduled-leaders)
6273 (- 1 diff)))
6274 head level category tags
6275 (if (not (= diff 0)) nil timestr)
6276 nil habitp))))
6277 (when txt
6278 (setq face
6279 (cond
6280 ((and (not habitp) pastschedp)
6281 'org-scheduled-previously)
6282 (todayp 'org-scheduled-today)
6283 (t 'org-scheduled))
6284 habitp (and habitp (org-habit-parse-todo)))
6285 (org-add-props txt props
6286 'undone-face face
6287 'face (if donep 'org-agenda-done face)
6288 'org-marker (org-agenda-new-marker pos)
6289 'org-hd-marker (org-agenda-new-marker pos1)
6290 'type (if pastschedp "past-scheduled" "scheduled")
6291 'date (if pastschedp d2 date)
6292 'ts-date d2
6293 'warntime warntime
6294 'level level
6295 'priority (if habitp
6296 (org-habit-get-priority habitp)
6297 (+ 94 (- 5 diff) (org-get-priority txt)))
6298 'org-category category
6299 'category-position category-pos
6300 'org-habit-p habitp
6301 'todo-state todo-state)
6302 (push txt ee))))))
6303 (nreverse ee)))
6305 (defun org-agenda-get-blocks ()
6306 "Return the date-range information for agenda display."
6307 (let* ((props (list 'face nil
6308 'org-not-done-regexp org-not-done-regexp
6309 'org-todo-regexp org-todo-regexp
6310 'org-complex-heading-regexp org-complex-heading-regexp
6311 'mouse-face 'highlight
6312 'help-echo
6313 (format "mouse-2 or RET jump to org file %s"
6314 (abbreviate-file-name buffer-file-name))))
6315 (regexp org-tr-regexp)
6316 (d0 (calendar-absolute-from-gregorian date))
6317 marker hdmarker ee txt d1 d2 s1 s2 category category-pos
6318 level todo-state tags pos head donep inherited-tags)
6319 (goto-char (point-min))
6320 (while (re-search-forward regexp nil t)
6321 (catch :skip
6322 (org-agenda-skip)
6323 (setq pos (point))
6324 (let ((start-time (match-string 1))
6325 (end-time (match-string 2)))
6326 (setq s1 (match-string 1)
6327 s2 (match-string 2)
6328 d1 (time-to-days (org-time-string-to-time s1 (current-buffer) pos))
6329 d2 (time-to-days (org-time-string-to-time s2 (current-buffer) pos)))
6330 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
6331 ;; Only allow days between the limits, because the normal
6332 ;; date stamps will catch the limits.
6333 (save-excursion
6334 (setq todo-state (org-get-todo-state))
6335 (setq donep (member todo-state org-done-keywords))
6336 (if (and donep org-agenda-skip-timestamp-if-done)
6337 (throw :skip t))
6338 (setq marker (org-agenda-new-marker (point)))
6339 (setq category (org-get-category)
6340 category-pos (get-text-property (point) 'org-category-position))
6341 (if (not (re-search-backward org-outline-regexp-bol nil t))
6342 (throw :skip nil)
6343 (goto-char (match-beginning 0))
6344 (setq hdmarker (org-agenda-new-marker (point))
6345 inherited-tags
6346 (or (eq org-agenda-show-inherited-tags 'always)
6347 (and (listp org-agenda-show-inherited-tags)
6348 (memq 'agenda org-agenda-show-inherited-tags))
6349 (and (eq org-agenda-show-inherited-tags t)
6350 (or (eq org-agenda-use-tag-inheritance t)
6351 (memq 'agenda org-agenda-use-tag-inheritance))))
6353 tags (org-get-tags-at nil (not inherited-tags)))
6354 (setq level (make-string (org-reduced-level (org-outline-level)) ? ))
6355 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
6356 (setq head (match-string 1))
6357 (let ((remove-re
6358 (if org-agenda-remove-timeranges-from-blocks
6359 (concat
6360 "<" (regexp-quote s1) ".*?>"
6361 "--"
6362 "<" (regexp-quote s2) ".*?>")
6363 nil)))
6364 (setq txt (org-agenda-format-item
6365 (format
6366 (nth (if (= d1 d2) 0 1)
6367 org-agenda-timerange-leaders)
6368 (1+ (- d0 d1)) (1+ (- d2 d1)))
6369 head level category tags
6370 (cond ((and (= d1 d0) (= d2 d0))
6371 (concat "<" start-time ">--<" end-time ">"))
6372 ((= d1 d0)
6373 (concat "<" start-time ">"))
6374 ((= d2 d0)
6375 (concat "<" end-time ">")))
6376 remove-re))))
6377 (org-add-props txt props
6378 'org-marker marker 'org-hd-marker hdmarker
6379 'type "block" 'date date
6380 'level level
6381 'todo-state todo-state
6382 'priority (org-get-priority txt) 'org-category category
6383 'org-category-position category-pos)
6384 (push txt ee))))
6385 (goto-char pos)))
6386 ;; Sort the entries by expiration date.
6387 (nreverse ee)))
6389 ;;; Agenda presentation and sorting
6391 (defvar org-prefix-has-time nil
6392 "A flag, set by `org-compile-prefix-format'.
6393 The flag is set if the currently compiled format contains a `%t'.")
6394 (defvar org-prefix-has-tag nil
6395 "A flag, set by `org-compile-prefix-format'.
6396 The flag is set if the currently compiled format contains a `%T'.")
6397 (defvar org-prefix-has-effort nil
6398 "A flag, set by `org-compile-prefix-format'.
6399 The flag is set if the currently compiled format contains a `%e'.")
6400 (defvar org-prefix-has-breadcrumbs nil
6401 "A flag, set by `org-compile-prefix-format'.
6402 The flag is set if the currently compiled format contains a `%b'.")
6403 (defvar org-prefix-category-length nil
6404 "Used by `org-compile-prefix-format' to remember the category field width.")
6405 (defvar org-prefix-category-max-length nil
6406 "Used by `org-compile-prefix-format' to remember the category field width.")
6408 (defun org-agenda-get-category-icon (category)
6409 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
6410 (dolist (entry org-agenda-category-icon-alist)
6411 (when (org-string-match-p (car entry) category)
6412 (if (listp (cadr entry))
6413 (return (cadr entry))
6414 (return (apply 'create-image (cdr entry)))))))
6416 (defun org-agenda-format-item (extra txt &optional level category tags dotime
6417 remove-re habitp)
6418 "Format TXT to be inserted into the agenda buffer.
6419 In particular, add the prefix and corresponding text properties.
6421 EXTRA must be a string to replace the `%s' specifier in the prefix format.
6422 LEVEL may be a string to replace the `%l' specifier.
6423 CATEGORY (a string, a symbol or nil) may be used to overrule the default
6424 category taken from local variable or file name. It will replace the `%c'
6425 specifier in the format.
6426 DOTIME, when non-nil, indicates that a time-of-day should be extracted from
6427 TXT for sorting of this entry, and for the `%t' specifier in the format.
6428 When DOTIME is a string, this string is searched for a time before TXT is.
6429 TAGS can be the tags of the headline.
6430 Any match of REMOVE-RE will be removed from TXT."
6431 ;; We keep the org-prefix-* variable values along with a compiled
6432 ;; formatter, so that multiple agendas existing at the same time do
6433 ;; not step on each other toes.
6435 ;; It was inconvenient to make these variables buffer local in
6436 ;; Agenda buffers, because this function expects to be called with
6437 ;; the buffer where item comes from being current, and not agenda
6438 ;; buffer
6439 (let* ((bindings (car org-prefix-format-compiled))
6440 (formatter (cadr org-prefix-format-compiled)))
6441 (loop for (var value) in bindings
6442 do (set var value))
6443 (save-match-data
6444 ;; Diary entries sometimes have extra whitespace at the beginning
6445 (setq txt (org-trim txt))
6447 ;; Fix the tags part in txt
6448 (setq txt (org-agenda-fix-displayed-tags
6449 txt tags
6450 org-agenda-show-inherited-tags
6451 org-agenda-hide-tags-regexp))
6453 (let* ((category (or category
6454 (if (stringp org-category)
6455 org-category
6456 (and org-category (symbol-name org-category)))
6457 (if buffer-file-name
6458 (file-name-sans-extension
6459 (file-name-nondirectory buffer-file-name))
6460 "")))
6461 (category-icon (org-agenda-get-category-icon category))
6462 (category-icon (if category-icon
6463 (propertize " " 'display category-icon)
6464 ""))
6465 ;; time, tag, effort are needed for the eval of the prefix format
6466 (tag (if tags (nth (1- (length tags)) tags) ""))
6467 time effort neffort
6468 (ts (if dotime (concat
6469 (if (stringp dotime) dotime "")
6470 (and org-agenda-search-headline-for-time txt))))
6471 (time-of-day (and dotime (org-get-time-of-day ts)))
6472 stamp plain s0 s1 s2 rtn srp l
6473 duration thecategory breadcrumbs)
6474 (and (derived-mode-p 'org-mode) buffer-file-name
6475 (add-to-list 'org-agenda-contributing-files buffer-file-name))
6476 (when (and dotime time-of-day)
6477 ;; Extract starting and ending time and move them to prefix
6478 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
6479 (setq plain (string-match org-plain-time-of-day-regexp ts)))
6480 (setq s0 (match-string 0 ts)
6481 srp (and stamp (match-end 3))
6482 s1 (match-string (if plain 1 2) ts)
6483 s2 (match-string (if plain 8 (if srp 4 6)) ts))
6485 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
6486 ;; them, we might want to remove them there to avoid duplication.
6487 ;; The user can turn this off with a variable.
6488 (if (and org-prefix-has-time
6489 org-agenda-remove-times-when-in-prefix (or stamp plain)
6490 (string-match (concat (regexp-quote s0) " *") txt)
6491 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
6492 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
6493 (= (match-beginning 0) 0)
6495 (setq txt (replace-match "" nil nil txt))))
6496 ;; Normalize the time(s) to 24 hour
6497 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
6498 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
6500 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
6501 (let (org-time-clocksum-use-effort-durations)
6502 (when (and s1 (not s2) org-agenda-default-appointment-duration)
6503 (setq s2
6504 (org-minutes-to-clocksum-string
6505 (+ (org-hh:mm-string-to-minutes s1)
6506 org-agenda-default-appointment-duration)))))
6508 ;; Compute the duration
6509 (when s2
6510 (setq duration (- (org-hh:mm-string-to-minutes s2)
6511 (org-hh:mm-string-to-minutes s1)))))
6513 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
6514 txt)
6515 ;; Tags are in the string
6516 (if (or (eq org-agenda-remove-tags t)
6517 (and org-agenda-remove-tags
6518 org-prefix-has-tag))
6519 (setq txt (replace-match "" t t txt))
6520 (setq txt (replace-match
6521 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
6522 (match-string 2 txt))
6523 t t txt))))
6524 (when (derived-mode-p 'org-mode)
6525 (setq effort (ignore-errors (get-text-property 0 'org-effort txt))))
6527 ;; org-agenda-add-time-grid-maybe calls us with *Agenda* as
6528 ;; current buffer, so move this check outside of above
6529 (if effort
6530 (setq neffort (org-duration-string-to-minutes effort)
6531 effort (setq effort (concat "[" effort "]")))
6532 ;; prevent erroring out with %e format when there is no effort
6533 (setq effort ""))
6535 (when remove-re
6536 (while (string-match remove-re txt)
6537 (setq txt (replace-match "" t t txt))))
6539 ;; Set org-heading property on `txt' to mark the start of the
6540 ;; heading.
6541 (add-text-properties 0 (length txt) '(org-heading t) txt)
6543 ;; Prepare the variables needed in the eval of the compiled format
6544 (if org-prefix-has-breadcrumbs
6545 (setq breadcrumbs (org-with-point-at (org-get-at-bol 'org-marker)
6546 (let ((s (org-display-outline-path nil nil "->" t)))
6547 (if (eq "" s) "" (concat s "->"))))))
6548 (setq time (cond (s2 (concat
6549 (org-agenda-time-of-day-to-ampm-maybe s1)
6550 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
6551 (if org-agenda-timegrid-use-ampm " ")))
6552 (s1 (concat
6553 (org-agenda-time-of-day-to-ampm-maybe s1)
6554 (if org-agenda-timegrid-use-ampm
6555 "........ "
6556 "......")))
6557 (t ""))
6558 extra (or (and (not habitp) extra) "")
6559 category (if (symbolp category) (symbol-name category) category)
6560 thecategory (copy-sequence category)
6561 level (or level ""))
6562 (if (string-match org-bracket-link-regexp category)
6563 (progn
6564 (setq l (if (match-end 3)
6565 (- (match-end 3) (match-beginning 3))
6566 (- (match-end 1) (match-beginning 1))))
6567 (when (< l (or org-prefix-category-length 0))
6568 (setq category (copy-sequence category))
6569 (org-add-props category nil
6570 'extra-space (make-string
6571 (- org-prefix-category-length l 1) ?\ ))))
6572 (if (and org-prefix-category-max-length
6573 (>= (length category) org-prefix-category-max-length))
6574 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
6575 ;; Evaluate the compiled format
6576 (setq rtn (concat (eval formatter) txt))
6578 ;; And finally add the text properties
6579 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
6580 (org-add-props rtn nil
6581 'org-category (if thecategory (downcase thecategory) category)
6582 'tags (mapcar 'org-downcase-keep-props tags)
6583 'org-highest-priority org-highest-priority
6584 'org-lowest-priority org-lowest-priority
6585 'time-of-day time-of-day
6586 'duration duration
6587 'effort effort
6588 'effort-minutes neffort
6589 'breadcrumbs breadcrumbs
6590 'txt txt
6591 'level level
6592 'time time
6593 'extra extra
6594 'format org-prefix-format-compiled
6595 'dotime dotime)))))
6597 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
6598 "Remove tags string from TXT, and add a modified list of tags.
6599 The modified list may contain inherited tags, and tags matched by
6600 `org-agenda-hide-tags-regexp' will be removed."
6601 (when (or add-inherited hide-re)
6602 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
6603 (setq txt (substring txt 0 (match-beginning 0))))
6604 (setq tags
6605 (delq nil
6606 (mapcar (lambda (tg)
6607 (if (or (and hide-re (string-match hide-re tg))
6608 (and (not add-inherited)
6609 (get-text-property 0 'inherited tg)))
6611 tg))
6612 tags)))
6613 (when tags
6614 (let ((have-i (get-text-property 0 'inherited (car tags)))
6616 (setq txt (concat txt " :"
6617 (mapconcat
6618 (lambda (x)
6619 (setq i (get-text-property 0 'inherited x))
6620 (if (and have-i (not i))
6621 (progn
6622 (setq have-i nil)
6623 (concat ":" x))
6625 tags ":")
6626 (if have-i "::" ":"))))))
6627 txt)
6629 (defun org-downcase-keep-props (s)
6630 (let ((props (text-properties-at 0 s)))
6631 (setq s (downcase s))
6632 (add-text-properties 0 (length s) props s)
6635 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
6637 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
6638 "Add a time-grid for agenda items which need it.
6640 LIST is the list of agenda items formatted by `org-agenda-list'.
6641 NDAYS is the span of the current agenda view.
6642 TODAYP is `t' when the current agenda view is on today."
6643 (catch 'exit
6644 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
6645 ((and todayp (member 'today (car org-agenda-time-grid))))
6646 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
6647 ((member 'weekly (car org-agenda-time-grid)))
6648 (t (throw 'exit list)))
6649 (let* ((have (delq nil (mapcar
6650 (lambda (x) (get-text-property 1 'time-of-day x))
6651 list)))
6652 (string (nth 1 org-agenda-time-grid))
6653 (gridtimes (nth 2 org-agenda-time-grid))
6654 (req (car org-agenda-time-grid))
6655 (remove (member 'remove-match req))
6656 new time)
6657 (if (and (member 'require-timed req) (not have))
6658 ;; don't show empty grid
6659 (throw 'exit list))
6660 (while (setq time (pop gridtimes))
6661 (unless (and remove (member time have))
6662 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
6663 (push (org-agenda-format-item
6664 nil string nil "" nil
6665 (concat (substring time 0 -2) ":" (substring time -2)))
6666 new)
6667 (put-text-property
6668 2 (length (car new)) 'face 'org-time-grid (car new))))
6669 (when (and todayp org-agenda-show-current-time-in-grid)
6670 (push (org-agenda-format-item
6671 nil org-agenda-current-time-string nil "" nil
6672 (format-time-string "%H:%M "))
6673 new)
6674 (put-text-property
6675 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
6677 (if (member 'time-up org-agenda-sorting-strategy-selected)
6678 (append new list)
6679 (append list new)))))
6681 (defun org-compile-prefix-format (key)
6682 "Compile the prefix format into a Lisp form that can be evaluated.
6683 The resulting form and associated variable bindings is returned
6684 and stored in the variable `org-prefix-format-compiled'."
6685 (setq org-prefix-has-time nil
6686 org-prefix-has-tag nil
6687 org-prefix-category-length nil
6688 org-prefix-has-effort nil
6689 org-prefix-has-breadcrumbs nil)
6690 (let ((s (cond
6691 ((stringp org-agenda-prefix-format)
6692 org-agenda-prefix-format)
6693 ((assq key org-agenda-prefix-format)
6694 (cdr (assq key org-agenda-prefix-format)))
6695 (t " %-12:c%?-12t% s")))
6696 (start 0)
6697 varform vars var e c f opt)
6698 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cltseib]\\|(.+)\\)"
6699 s start)
6700 (setq var (or (cdr (assoc (match-string 4 s)
6701 '(("c" . category) ("t" . time) ("l" . level) ("s" . extra)
6702 ("i" . category-icon) ("T" . tag) ("e" . effort) ("b" . breadcrumbs))))
6703 'eval)
6704 c (or (match-string 3 s) "")
6705 opt (match-beginning 1)
6706 start (1+ (match-beginning 0)))
6707 (if (equal var 'time) (setq org-prefix-has-time t))
6708 (if (equal var 'tag) (setq org-prefix-has-tag t))
6709 (if (equal var 'effort) (setq org-prefix-has-effort t))
6710 (if (equal var 'breadcrumbs) (setq org-prefix-has-breadcrumbs t))
6711 (setq f (concat "%" (match-string 2 s) "s"))
6712 (when (equal var 'category)
6713 (setq org-prefix-category-length
6714 (floor (abs (string-to-number (match-string 2 s)))))
6715 (setq org-prefix-category-max-length
6716 (let ((x (match-string 2 s)))
6717 (save-match-data
6718 (if (string-match "\\.[0-9]+" x)
6719 (string-to-number (substring (match-string 0 x) 1)))))))
6720 (if (eq var 'eval)
6721 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
6722 (if opt
6723 (setq varform
6724 `(if (equal "" ,var)
6726 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
6727 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
6728 (setq s (replace-match "%s" t nil s))
6729 (push varform vars))
6730 (setq vars (nreverse vars))
6731 (with-current-buffer (or org-agenda-buffer (current-buffer))
6732 (setq org-prefix-format-compiled
6733 (list
6734 `((org-prefix-has-time ,org-prefix-has-time)
6735 (org-prefix-has-tag ,org-prefix-has-tag)
6736 (org-prefix-category-length ,org-prefix-category-length)
6737 (org-prefix-has-effort ,org-prefix-has-effort)
6738 (org-prefix-has-breadcrumbs ,org-prefix-has-breadcrumbs))
6739 `(format ,s ,@vars))))))
6741 (defun org-set-sorting-strategy (key)
6742 (if (symbolp (car org-agenda-sorting-strategy))
6743 ;; the old format
6744 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
6745 (setq org-agenda-sorting-strategy-selected
6746 (or (cdr (assq key org-agenda-sorting-strategy))
6747 (cdr (assq 'agenda org-agenda-sorting-strategy))
6748 '(time-up category-keep priority-down)))))
6750 (defun org-get-time-of-day (s &optional string mod24)
6751 "Check string S for a time of day.
6752 If found, return it as a military time number between 0 and 2400.
6753 If not found, return nil.
6754 The optional STRING argument forces conversion into a 5 character wide string
6755 HH:MM."
6756 (save-match-data
6757 (when
6758 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
6759 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
6760 (let* ((h (string-to-number (match-string 1 s)))
6761 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
6762 (ampm (if (match-end 4) (downcase (match-string 4 s))))
6763 (am-p (equal ampm "am"))
6764 (h1 (cond ((not ampm) h)
6765 ((= h 12) (if am-p 0 12))
6766 (t (+ h (if am-p 0 12)))))
6767 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
6768 (mod h1 24) h1))
6769 (t0 (+ (* 100 h2) m))
6770 (t1 (concat (if (>= h1 24) "+" " ")
6771 (if (and org-agenda-time-leading-zero
6772 (< t0 1000)) "0" "")
6773 (if (< t0 100) "0" "")
6774 (if (< t0 10) "0" "")
6775 (int-to-string t0))))
6776 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
6778 (defvar org-agenda-before-sorting-filter-function nil
6779 "Function to be applied to agenda items prior to sorting.
6780 Prior to sorting also means just before they are inserted into the agenda.
6782 To aid sorting, you may revisit the original entries and add more text
6783 properties which will later be used by the sorting functions.
6785 The function should take a string argument, an agenda line.
6786 It has access to the text properties in that line, which contain among
6787 other things, the property `org-hd-marker' that points to the entry
6788 where the line comes from. Note that not all lines going into the agenda
6789 have this property, only most.
6791 The function should return the modified string. It is probably best
6792 to ONLY change text properties.
6794 You can also use this function as a filter, by returning nil for lines
6795 you don't want to have in the agenda at all. For this application, you
6796 could bind the variable in the options section of a custom command.")
6798 (defun org-agenda-finalize-entries (list &optional type)
6799 "Sort, limit and concatenate the LIST of agenda items.
6800 The optional argument TYPE tells the agenda type."
6801 (let ((max-effort (cond ((listp org-agenda-max-effort)
6802 (cdr (assoc type org-agenda-max-effort)))
6803 (t org-agenda-max-effort)))
6804 (max-todo (cond ((listp org-agenda-max-todos)
6805 (cdr (assoc type org-agenda-max-todos)))
6806 (t org-agenda-max-todos)))
6807 (max-tags (cond ((listp org-agenda-max-tags)
6808 (cdr (assoc type org-agenda-max-tags)))
6809 (t org-agenda-max-tags)))
6810 (max-entries (cond ((listp org-agenda-max-entries)
6811 (cdr (assoc type org-agenda-max-entries)))
6812 (t org-agenda-max-entries))) l)
6813 (when org-agenda-before-sorting-filter-function
6814 (setq list
6815 (delq nil
6816 (mapcar
6817 org-agenda-before-sorting-filter-function list))))
6818 (setq list (mapcar 'org-agenda-highlight-todo list)
6819 list (mapcar 'identity (sort list 'org-entries-lessp)))
6820 (when max-effort
6821 (setq list (org-agenda-limit-entries
6822 list 'effort-minutes max-effort 'identity)))
6823 (when max-todo
6824 (setq list (org-agenda-limit-entries list 'todo-state max-todo)))
6825 (when max-tags
6826 (setq list (org-agenda-limit-entries list 'tags max-tags)))
6827 (when max-entries
6828 (setq list (org-agenda-limit-entries list 'org-hd-marker max-entries)))
6829 (mapconcat 'identity list "\n")))
6831 (defun org-agenda-limit-entries (list prop limit &optional fn)
6832 "Limit the number of agenda entries."
6833 (let ((include (and limit (< limit 0))))
6834 (if limit
6835 (let ((fun (or fn (lambda (p) (if p 1))))
6836 (lim 0))
6837 (delq nil
6838 (mapcar
6839 (lambda (e)
6840 (let ((pval (funcall fun (get-text-property 1 prop e))))
6841 (if pval (setq lim (+ lim pval)))
6842 (cond ((and pval (<= lim (abs limit))) e)
6843 ((and include (not pval)) e))))
6844 list)))
6845 list)))
6847 (defun org-agenda-limit-interactively ()
6848 "In agenda, interactively limit entries to various maximums."
6849 (interactive)
6850 (let* ((max (read-char "Number of [e]ntries [t]odos [T]ags [E]ffort? "))
6851 (num (string-to-number (read-from-minibuffer "How many? "))))
6852 (cond ((equal max ?e)
6853 (let ((org-agenda-max-entries num)) (org-agenda-redo)))
6854 ((equal max ?t)
6855 (let ((org-agenda-max-todos num)) (org-agenda-redo)))
6856 ((equal max ?T)
6857 (let ((org-agenda-max-tags num)) (org-agenda-redo)))
6858 ((equal max ?E)
6859 (let ((org-agenda-max-effort num)) (org-agenda-redo)))))
6860 (org-agenda-fit-window-to-buffer))
6862 (defun org-agenda-highlight-todo (x)
6863 (let ((org-done-keywords org-done-keywords-for-agenda)
6864 (case-fold-search nil)
6866 (if (eq x 'line)
6867 (save-excursion
6868 (beginning-of-line 1)
6869 (setq re (org-get-at-bol 'org-todo-regexp))
6870 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
6871 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
6872 (add-text-properties (match-beginning 0) (match-end 1)
6873 (list 'face (org-get-todo-face 1)))
6874 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
6875 (delete-region (match-beginning 1) (1- (match-end 0)))
6876 (goto-char (match-beginning 1))
6877 (insert (format org-agenda-todo-keyword-format s)))))
6878 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
6879 (setq re (get-text-property 0 'org-todo-regexp x))
6880 (when (and re
6881 ;; Test `pl' because if there's no heading content,
6882 ;; there's no point matching to highlight. Note
6883 ;; that if we didn't test `pl' first, and there
6884 ;; happened to be no keyword from `org-todo-regexp'
6885 ;; on this heading line, then the `equal' comparison
6886 ;; afterwards would spuriously succeed in the case
6887 ;; where `pl' is nil -- causing an args-out-of-range
6888 ;; error when we try to add text properties to text
6889 ;; that isn't there.
6891 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
6892 x pl) pl))
6893 (add-text-properties
6894 (or (match-end 1) (match-end 0)) (match-end 0)
6895 (list 'face (org-get-todo-face (match-string 2 x)))
6897 (when (match-end 1)
6898 (setq x (concat (substring x 0 (match-end 1))
6899 (format org-agenda-todo-keyword-format
6900 (match-string 2 x))
6901 (org-add-props " " (text-properties-at 0 x))
6902 (substring x (match-end 3)))))))
6903 x)))
6905 (defsubst org-cmp-priority (a b)
6906 "Compare the priorities of string A and B."
6907 (let ((pa (or (get-text-property 1 'priority a) 0))
6908 (pb (or (get-text-property 1 'priority b) 0)))
6909 (cond ((> pa pb) +1)
6910 ((< pa pb) -1))))
6912 (defsubst org-cmp-effort (a b)
6913 "Compare the effort values of string A and B."
6914 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
6915 (ea (or (get-text-property 1 'effort-minutes a) def))
6916 (eb (or (get-text-property 1 'effort-minutes b) def)))
6917 (cond ((> ea eb) +1)
6918 ((< ea eb) -1))))
6920 (defsubst org-cmp-category (a b)
6921 "Compare the string values of categories of strings A and B."
6922 (let ((ca (or (get-text-property 1 'org-category a) ""))
6923 (cb (or (get-text-property 1 'org-category b) "")))
6924 (cond ((string-lessp ca cb) -1)
6925 ((string-lessp cb ca) +1))))
6927 (defsubst org-cmp-todo-state (a b)
6928 "Compare the todo states of strings A and B."
6929 (let* ((ma (or (get-text-property 1 'org-marker a)
6930 (get-text-property 1 'org-hd-marker a)))
6931 (mb (or (get-text-property 1 'org-marker b)
6932 (get-text-property 1 'org-hd-marker b)))
6933 (fa (and ma (marker-buffer ma)))
6934 (fb (and mb (marker-buffer mb)))
6935 (todo-kwds
6936 (or (and fa (with-current-buffer fa org-todo-keywords-1))
6937 (and fb (with-current-buffer fb org-todo-keywords-1))))
6938 (ta (or (get-text-property 1 'todo-state a) ""))
6939 (tb (or (get-text-property 1 'todo-state b) ""))
6940 (la (- (length (member ta todo-kwds))))
6941 (lb (- (length (member tb todo-kwds))))
6942 (donepa (member ta org-done-keywords-for-agenda))
6943 (donepb (member tb org-done-keywords-for-agenda)))
6944 (cond ((and donepa (not donepb)) -1)
6945 ((and (not donepa) donepb) +1)
6946 ((< la lb) -1)
6947 ((< lb la) +1))))
6949 (defsubst org-cmp-alpha (a b)
6950 "Compare the headlines, alphabetically."
6951 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
6952 (plb (text-property-any 0 (length b) 'org-heading t b))
6953 (ta (and pla (substring a pla)))
6954 (tb (and plb (substring b plb))))
6955 (when pla
6956 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
6957 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
6958 (setq ta (substring ta (match-end 0))))
6959 (setq ta (downcase ta)))
6960 (when plb
6961 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
6962 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
6963 (setq tb (substring tb (match-end 0))))
6964 (setq tb (downcase tb)))
6965 (cond ((not ta) +1)
6966 ((not tb) -1)
6967 ((string-lessp ta tb) -1)
6968 ((string-lessp tb ta) +1))))
6970 (defsubst org-cmp-tag (a b)
6971 "Compare the string values of the first tags of A and B."
6972 (let ((ta (car (last (get-text-property 1 'tags a))))
6973 (tb (car (last (get-text-property 1 'tags b)))))
6974 (cond ((not ta) +1)
6975 ((not tb) -1)
6976 ((string-lessp ta tb) -1)
6977 ((string-lessp tb ta) +1))))
6979 (defsubst org-cmp-time (a b)
6980 "Compare the time-of-day values of strings A and B."
6981 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6982 (ta (or (get-text-property 1 'time-of-day a) def))
6983 (tb (or (get-text-property 1 'time-of-day b) def)))
6984 (cond ((< ta tb) -1)
6985 ((< tb ta) +1))))
6987 (defsubst org-cmp-ts (a b &optional type)
6988 "Compare the timestamps values of entries A and B.
6989 When TYPE is \"scheduled\", \"deadline\", \"timestamp\" or
6990 \"timestamp_ia\", compare within each of these type. When TYPE
6991 is the empty string, compare all timestamps without respect of
6992 their type."
6993 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
6994 (ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
6995 (get-text-property 1 'ts-date a)) def))
6996 (tb (or (and (string-match type (or (get-text-property 1 'type b) ""))
6997 (get-text-property 1 'ts-date b)) def)))
6998 (cond ((< ta tb) -1)
6999 ((< tb ta) +1))))
7001 (defsubst org-cmp-habit-p (a b)
7002 "Compare the todo states of strings A and B."
7003 (let ((ha (get-text-property 1 'org-habit-p a))
7004 (hb (get-text-property 1 'org-habit-p b)))
7005 (cond ((and ha (not hb)) -1)
7006 ((and (not ha) hb) +1))))
7008 (defun org-entries-lessp (a b)
7009 "Predicate for sorting agenda entries."
7010 ;; The following variables will be used when the form is evaluated.
7011 ;; So even though the compiler complains, keep them.
7012 (let* ((ss org-agenda-sorting-strategy-selected)
7013 (timestamp-up (and (org-em 'timestamp-up 'timestamp-down ss)
7014 (org-cmp-ts a b "")))
7015 (timestamp-down (if timestamp-up (- timestamp-up) nil))
7016 (scheduled-up (and (org-em 'scheduled-up 'scheduled-down ss)
7017 (org-cmp-ts a b "scheduled")))
7018 (scheduled-down (if scheduled-up (- scheduled-up) nil))
7019 (deadline-up (and (org-em 'deadline-up 'deadline-down ss)
7020 (org-cmp-ts a b "deadline")))
7021 (deadline-down (if deadline-up (- deadline-up) nil))
7022 (tsia-up (and (org-em 'tsia-up 'tsia-down ss)
7023 (org-cmp-ts a b "iatimestamp_ia")))
7024 (tsia-down (if tsia-up (- tsia-up) nil))
7025 (ts-up (and (org-em 'ts-up 'ts-down ss)
7026 (org-cmp-ts a b "timestamp")))
7027 (ts-down (if ts-up (- ts-up) nil))
7028 (time-up (and (org-em 'time-up 'time-down ss)
7029 (org-cmp-time a b)))
7030 (time-down (if time-up (- time-up) nil))
7031 (priority-up (and (org-em 'priority-up 'priority-down ss)
7032 (org-cmp-priority a b)))
7033 (priority-down (if priority-up (- priority-up) nil))
7034 (effort-up (and (org-em 'effort-up 'effort-down ss)
7035 (org-cmp-effort a b)))
7036 (effort-down (if effort-up (- effort-up) nil))
7037 (category-up (and (or (org-em 'category-up 'category-down ss)
7038 (memq 'category-keep ss))
7039 (org-cmp-category a b)))
7040 (category-down (if category-up (- category-up) nil))
7041 (category-keep (if category-up +1 nil))
7042 (tag-up (and (org-em 'tag-up 'tag-down ss)
7043 (org-cmp-tag a b)))
7044 (tag-down (if tag-up (- tag-up) nil))
7045 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
7046 (org-cmp-todo-state a b)))
7047 (todo-state-down (if todo-state-up (- todo-state-up) nil))
7048 (habit-up (and (org-em 'habit-up 'habit-down ss)
7049 (org-cmp-habit-p a b)))
7050 (habit-down (if habit-up (- habit-up) nil))
7051 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
7052 (org-cmp-alpha a b)))
7053 (alpha-down (if alpha-up (- alpha-up) nil))
7054 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
7055 user-defined-up user-defined-down)
7056 (if (and need-user-cmp org-agenda-cmp-user-defined
7057 (functionp org-agenda-cmp-user-defined))
7058 (setq user-defined-up
7059 (funcall org-agenda-cmp-user-defined a b)
7060 user-defined-down (if user-defined-up (- user-defined-up) nil)))
7061 (cdr (assoc
7062 (eval (cons 'or org-agenda-sorting-strategy-selected))
7063 '((-1 . t) (1 . nil) (nil . nil))))))
7065 ;;; Agenda restriction lock
7067 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
7068 "Overlay to mark the headline to which agenda commands are restricted.")
7069 (overlay-put org-agenda-restriction-lock-overlay
7070 'face 'org-agenda-restriction-lock)
7071 (overlay-put org-agenda-restriction-lock-overlay
7072 'help-echo "Agendas are currently limited to this subtree.")
7073 (org-detach-overlay org-agenda-restriction-lock-overlay)
7075 (defun org-agenda-set-restriction-lock (&optional type)
7076 "Set restriction lock for agenda, to current subtree or file.
7077 Restriction will be the file if TYPE is `file', or if type is the
7078 universal prefix '(4), or if the cursor is before the first headline
7079 in the file. Otherwise, restriction will be to the current subtree."
7080 (interactive "P")
7081 (and (equal type '(4)) (setq type 'file))
7082 (setq type (cond
7083 (type type)
7084 ((org-at-heading-p) 'subtree)
7085 ((condition-case nil (org-back-to-heading t) (error nil))
7086 'subtree)
7087 (t 'file)))
7088 (if (eq type 'subtree)
7089 (progn
7090 (setq org-agenda-restrict (current-buffer))
7091 (setq org-agenda-overriding-restriction 'subtree)
7092 (put 'org-agenda-files 'org-restrict
7093 (list (buffer-file-name (buffer-base-buffer))))
7094 (org-back-to-heading t)
7095 (move-overlay org-agenda-restriction-lock-overlay
7096 (point)
7097 (if org-agenda-restriction-lock-highlight-subtree
7098 (save-excursion (org-end-of-subtree t t) (point))
7099 (point-at-eol)))
7100 (move-marker org-agenda-restrict-begin (point))
7101 (move-marker org-agenda-restrict-end
7102 (save-excursion (org-end-of-subtree t t)))
7103 (message "Locking agenda restriction to subtree"))
7104 (put 'org-agenda-files 'org-restrict
7105 (list (buffer-file-name (buffer-base-buffer))))
7106 (setq org-agenda-restrict nil)
7107 (setq org-agenda-overriding-restriction 'file)
7108 (move-marker org-agenda-restrict-begin nil)
7109 (move-marker org-agenda-restrict-end nil)
7110 (message "Locking agenda restriction to file"))
7111 (setq current-prefix-arg nil)
7112 (org-agenda-maybe-redo))
7114 (defun org-agenda-remove-restriction-lock (&optional noupdate)
7115 "Remove the agenda restriction lock."
7116 (interactive "P")
7117 (org-detach-overlay org-agenda-restriction-lock-overlay)
7118 (org-detach-overlay org-speedbar-restriction-lock-overlay)
7119 (setq org-agenda-overriding-restriction nil)
7120 (setq org-agenda-restrict nil)
7121 (put 'org-agenda-files 'org-restrict nil)
7122 (move-marker org-agenda-restrict-begin nil)
7123 (move-marker org-agenda-restrict-end nil)
7124 (setq current-prefix-arg nil)
7125 (message "Agenda restriction lock removed")
7126 (or noupdate (org-agenda-maybe-redo)))
7128 (defun org-agenda-maybe-redo ()
7129 "If there is any window showing the agenda view, update it."
7130 (let ((w (get-buffer-window org-agenda-buffer-name t))
7131 (w0 (selected-window)))
7132 (when w
7133 (select-window w)
7134 (org-agenda-redo)
7135 (select-window w0)
7136 (if org-agenda-overriding-restriction
7137 (message "Agenda view shifted to new %s restriction"
7138 org-agenda-overriding-restriction)
7139 (message "Agenda restriction lock removed")))))
7141 ;;; Agenda commands
7143 (defun org-agenda-check-type (error &rest types)
7144 "Check if agenda buffer is of allowed type.
7145 If ERROR is non-nil, throw an error, otherwise just return nil.
7146 Allowed types are 'agenda 'timeline 'todo 'tags 'search."
7147 (if (not org-agenda-type)
7148 (error "No Org agenda currently displayed")
7149 (if (memq org-agenda-type types)
7151 (if error
7152 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
7153 nil))))
7155 (defun org-agenda-Quit ()
7156 "Exit the agenda and kill buffers loaded by `org-agenda'.
7157 Also restore the window configuration."
7158 (interactive)
7159 (if org-agenda-columns-active
7160 (org-columns-quit)
7161 (let ((buf (current-buffer)))
7162 (if (eq org-agenda-window-setup 'other-frame)
7163 (progn
7164 (org-agenda-reset-markers)
7165 (kill-buffer buf)
7166 (org-columns-remove-overlays)
7167 (setq org-agenda-archives-mode nil)
7168 (delete-frame))
7169 (and (not (eq org-agenda-window-setup 'current-window))
7170 (not (one-window-p))
7171 (delete-window))
7172 (org-agenda-reset-markers)
7173 (kill-buffer buf)
7174 (org-columns-remove-overlays)
7175 (setq org-agenda-archives-mode nil)))
7176 (setq org-agenda-buffer nil)
7177 ;; Maybe restore the pre-agenda window configuration.
7178 (and org-agenda-restore-windows-after-quit
7179 (not (eq org-agenda-window-setup 'other-frame))
7180 org-agenda-pre-window-conf
7181 (set-window-configuration org-agenda-pre-window-conf)
7182 (setq org-agenda-pre-window-conf nil))))
7184 (defun org-agenda-quit ()
7185 "Exit the agenda and restore the window configuration.
7186 When `org-agenda-sticky' is non-nil, only bury the agenda."
7187 (interactive)
7188 (if (and (eq org-indirect-buffer-display 'other-window)
7189 org-last-indirect-buffer)
7190 (let ((org-last-indirect-window
7191 (get-buffer-window org-last-indirect-buffer)))
7192 (if org-last-indirect-window
7193 (delete-window org-last-indirect-window))))
7194 (if org-agenda-columns-active
7195 (org-columns-quit)
7196 (if org-agenda-sticky
7197 (let ((buf (current-buffer)))
7198 (if (eq org-agenda-window-setup 'other-frame)
7199 (progn
7200 (delete-frame))
7201 (and (not (eq org-agenda-window-setup 'current-window))
7202 (not (one-window-p))
7203 (delete-window)))
7204 (with-current-buffer buf
7205 (bury-buffer)
7206 ;; Maybe restore the pre-agenda window configuration.
7207 (and org-agenda-restore-windows-after-quit
7208 (not (eq org-agenda-window-setup 'other-frame))
7209 org-agenda-pre-window-conf
7210 (set-window-configuration org-agenda-pre-window-conf)
7211 (setq org-agenda-pre-window-conf nil))))
7212 (org-agenda-Quit))))
7214 (defun org-agenda-exit ()
7215 "Exit the agenda and restore the window configuration.
7216 Also kill Org-mode buffers loaded by `org-agenda'. Org-mode
7217 buffers visited directly by the user will not be touched."
7218 (interactive)
7219 (org-release-buffers org-agenda-new-buffers)
7220 (setq org-agenda-new-buffers nil)
7221 (org-agenda-Quit))
7223 (defun org-agenda-kill-all-agenda-buffers ()
7224 "Kill all buffers in `org-agenda-mode'.
7225 This is used when toggling sticky agendas.
7226 You can also explicitly invoke it with `C-c a C-k'."
7227 (interactive)
7228 (let (blist)
7229 (dolist (buf (buffer-list))
7230 (when (with-current-buffer buf (eq major-mode 'org-agenda-mode))
7231 (push buf blist)))
7232 (mapc 'kill-buffer blist)))
7234 (defun org-agenda-execute (arg)
7235 "Execute another agenda command, keeping same window.
7236 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
7237 in the agenda."
7238 (interactive "P")
7239 (let ((org-agenda-window-setup 'current-window))
7240 (org-agenda arg)))
7242 (defun org-agenda-redo (&optional all)
7243 "Rebuild possibly ALL agenda view(s) in the current buffer."
7244 (interactive "P")
7245 (let* ((p (or (and (looking-at "\\'") (1- (point))) (point)))
7246 (cpa (unless (eq all t) current-prefix-arg))
7247 (org-agenda-doing-sticky-redo org-agenda-sticky)
7248 (org-agenda-sticky nil)
7249 (org-agenda-buffer-name (or org-agenda-this-buffer-name
7250 org-agenda-buffer-name))
7251 (org-agenda-keep-modes t)
7252 (tag-filter org-agenda-tag-filter)
7253 (tag-preset (get 'org-agenda-tag-filter :preset-filter))
7254 (top-hl-filter org-agenda-top-headline-filter)
7255 (cat-filter org-agenda-category-filter)
7256 (cat-preset (get 'org-agenda-category-filter :preset-filter))
7257 (re-filter org-agenda-regexp-filter)
7258 (re-preset (get 'org-agenda-regexp-filter :preset-filter))
7259 (org-agenda-tag-filter-while-redo (or tag-filter tag-preset))
7260 (cols org-agenda-columns-active)
7261 (line (org-current-line))
7262 (window-line (- line (org-current-line (window-start))))
7263 (lprops (get 'org-agenda-redo-command 'org-lprops))
7264 (redo-cmd (get-text-property p 'org-redo-cmd))
7265 (last-args (get-text-property p 'org-last-args))
7266 (org-agenda-overriding-cmd (get-text-property p 'org-series-cmd))
7267 (org-agenda-overriding-cmd-arguments
7268 (unless (eq all t)
7269 (cond ((listp last-args)
7270 (cons (or cpa (car last-args)) (cdr last-args)))
7271 ((stringp last-args)
7272 last-args))))
7273 (series-redo-cmd (get-text-property p 'org-series-redo-cmd)))
7274 (put 'org-agenda-tag-filter :preset-filter nil)
7275 (put 'org-agenda-category-filter :preset-filter nil)
7276 (put 'org-agenda-regexp-filter :preset-filter nil)
7277 (and cols (org-columns-quit))
7278 (message "Rebuilding agenda buffer...")
7279 (if series-redo-cmd
7280 (eval series-redo-cmd)
7281 (org-let lprops redo-cmd))
7282 (setq org-agenda-undo-list nil
7283 org-agenda-pending-undo-list nil
7284 org-agenda-tag-filter tag-filter
7285 org-agenda-category-filter cat-filter
7286 org-agenda-regexp-filter re-filter
7287 org-agenda-top-headline-filter top-hl-filter)
7288 (message "Rebuilding agenda buffer...done")
7289 (put 'org-agenda-tag-filter :preset-filter tag-preset)
7290 (put 'org-agenda-category-filter :preset-filter cat-preset)
7291 (put 'org-agenda-regexp-filter :preset-filter re-preset)
7292 (and (or tag-filter tag-preset) (org-agenda-filter-apply tag-filter 'tag))
7293 (and (or cat-filter cat-preset) (org-agenda-filter-apply cat-filter 'category))
7294 (and (or re-filter re-preset) (org-agenda-filter-apply re-filter 'regexp))
7295 (and top-hl-filter (org-agenda-filter-top-headline-apply top-hl-filter))
7296 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
7297 (org-goto-line line)
7298 (recenter window-line)))
7300 (defvar org-global-tags-completion-table nil)
7301 (defvar org-agenda-filter-form nil)
7302 (defvar org-agenda-filtered-by-category nil)
7304 (defun org-agenda-filter-by-category (strip)
7305 "Keep only those lines in the agenda buffer that have a specific category.
7306 The category is that of the current line."
7307 (interactive "P")
7308 (if (and org-agenda-filtered-by-category
7309 org-agenda-category-filter)
7310 (org-agenda-filter-show-all-cat)
7311 (let ((cat (org-no-properties (get-text-property (point) 'org-category))))
7312 (cond
7313 ((and cat strip)
7314 (org-agenda-filter-apply
7315 (push (concat "-" cat) org-agenda-category-filter) 'category))
7316 ((and cat)
7317 (org-agenda-filter-apply
7318 (setq org-agenda-category-filter
7319 (list (concat "+" cat))) 'category))
7320 ((error "No category at point"))))))
7322 (defun org-find-top-headline (&optional pos)
7323 "Find the topmost parent headline and return it."
7324 (save-excursion
7325 (with-current-buffer (if pos (marker-buffer pos) (current-buffer))
7326 (if pos (goto-char pos))
7327 ;; Skip up to the topmost parent
7328 (while (ignore-errors (outline-up-heading 1) t))
7329 (ignore-errors
7330 (nth 4 (org-heading-components))))))
7332 (defvar org-agenda-filtered-by-top-headline nil)
7333 (defun org-agenda-filter-by-top-headline (strip)
7334 "Keep only those lines that are descendants from the same top headline.
7335 The top headline is that of the current line."
7336 (interactive "P")
7337 (if org-agenda-filtered-by-top-headline
7338 (progn
7339 (setq org-agenda-filtered-by-top-headline nil
7340 org-agenda-top-headline-filter nil)
7341 (org-agenda-filter-show-all-cat))
7342 (let ((cat (org-find-top-headline (org-get-at-bol 'org-hd-marker))))
7343 (if cat (org-agenda-filter-top-headline-apply cat strip)
7344 (error "No top-level category at point")))))
7346 (defvar org-agenda-regexp-filter nil)
7347 (defun org-agenda-filter-by-regexp (strip)
7348 "Filter agenda entries by a regular expression.
7349 Regexp filters are cumulative.
7350 With no prefix argument, keep entries matching the regexp.
7351 With one prefix argument, filter out entries matching the regexp.
7352 With two prefix arguments, remove the regexp filters."
7353 (interactive "P")
7354 (if (not (equal strip '(16)))
7355 (let ((flt (concat (if (equal strip '(4)) "-" "+")
7356 (read-from-minibuffer
7357 (if (equal strip '(4))
7358 "Filter out entries matching regexp: "
7359 "Narrow to entries matching regexp: ")))))
7360 (push flt org-agenda-regexp-filter)
7361 (org-agenda-filter-apply org-agenda-regexp-filter 'regexp))
7362 (org-agenda-filter-show-all-re)
7363 (message "Regexp filter removed")))
7365 (defun org-agenda-filter-remove-all ()
7366 "Remove all filters from the current agenda buffer."
7367 (interactive)
7368 (when org-agenda-tag-filter
7369 (org-agenda-filter-show-all-tag))
7370 (when org-agenda-category-filter
7371 (org-agenda-filter-show-all-cat))
7372 (when org-agenda-regexp-filter
7373 (org-agenda-filter-show-all-re)))
7375 (defun org-agenda-filter-by-tag (strip &optional char narrow)
7376 "Keep only those lines in the agenda buffer that have a specific tag.
7377 The tag is selected with its fast selection letter, as configured.
7378 With prefix argument STRIP, remove all lines that do have the tag.
7379 A lisp caller can specify CHAR. NARROW means that the new tag should be
7380 used to narrow the search - the interactive user can also press `-' or `+'
7381 to switch to narrowing."
7382 (interactive "P")
7383 (let* ((alist org-tag-alist-for-agenda)
7384 (tag-chars (mapconcat
7385 (lambda (x) (if (and (not (symbolp (car x)))
7386 (cdr x))
7387 (char-to-string (cdr x))
7388 ""))
7389 alist ""))
7390 (efforts (org-split-string
7391 (or (cdr (assoc (concat org-effort-property "_ALL")
7392 org-global-properties))
7393 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
7394 "")))
7395 (effort-op org-agenda-filter-effort-default-operator)
7396 (effort-prompt "")
7397 (inhibit-read-only t)
7398 (current org-agenda-tag-filter)
7399 maybe-refresh a n tag)
7400 (unless char
7401 (message
7402 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
7403 (if narrow "Narrow" "Filter") tag-chars
7404 (if org-agenda-auto-exclude-function "[RET], " ""))
7405 (setq char (read-char-exclusive)))
7406 (when (member char '(?+ ?-))
7407 ;; Narrowing down
7408 (cond ((equal char ?-) (setq strip t narrow t))
7409 ((equal char ?+) (setq strip nil narrow t)))
7410 (message
7411 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
7412 (setq char (read-char-exclusive)))
7413 (when (member char '(?< ?> ?= ??))
7414 ;; An effort operator
7415 (setq effort-op (char-to-string char))
7416 (setq alist nil) ; to make sure it will be interpreted as effort.
7417 (unless (equal char ??)
7418 (loop for i from 0 to 9 do
7419 (setq effort-prompt
7420 (concat
7421 effort-prompt " ["
7422 (if (= i 9) "0" (int-to-string (1+ i)))
7423 "]" (nth i efforts))))
7424 (message "Effort%s: %s " effort-op effort-prompt)
7425 (setq char (read-char-exclusive))
7426 (when (or (< char ?0) (> char ?9))
7427 (error "Need 1-9,0 to select effort"))))
7428 (when (equal char ?\t)
7429 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
7430 (org-set-local 'org-global-tags-completion-table
7431 (org-global-tags-completion-table)))
7432 (let ((completion-ignore-case t))
7433 (setq tag (org-icompleting-read
7434 "Tag: " org-global-tags-completion-table))))
7435 (cond
7436 ((equal char ?\r)
7437 (org-agenda-filter-show-all-tag)
7438 (when org-agenda-auto-exclude-function
7439 (setq org-agenda-tag-filter nil)
7440 (dolist (tag (org-agenda-get-represented-tags))
7441 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
7442 (if modifier
7443 (push modifier org-agenda-tag-filter))))
7444 (if (not (null org-agenda-tag-filter))
7445 (org-agenda-filter-apply org-agenda-tag-filter 'tag)))
7446 (setq maybe-refresh t))
7447 ((equal char ?/)
7448 (org-agenda-filter-show-all-tag)
7449 (when (get 'org-agenda-tag-filter :preset-filter)
7450 (org-agenda-filter-apply org-agenda-tag-filter 'tag))
7451 (setq maybe-refresh t))
7452 ((equal char ?. )
7453 (setq org-agenda-tag-filter
7454 (mapcar (lambda(tag) (concat "+" tag))
7455 (org-get-at-bol 'tags)))
7456 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7457 (setq maybe-refresh t))
7458 ((or (equal char ?\ )
7459 (setq a (rassoc char alist))
7460 (and (>= char ?0) (<= char ?9)
7461 (setq n (if (= char ?0) 9 (- char ?0 1))
7462 tag (concat effort-op (nth n efforts))
7463 a (cons tag nil)))
7464 (and (= char ??)
7465 (setq tag "?eff")
7466 a (cons tag nil))
7467 (and tag (setq a (cons tag nil))))
7468 (org-agenda-filter-show-all-tag)
7469 (setq tag (car a))
7470 (setq org-agenda-tag-filter
7471 (cons (concat (if strip "-" "+") tag)
7472 (if narrow current nil)))
7473 (org-agenda-filter-apply org-agenda-tag-filter 'tag)
7474 (setq maybe-refresh t))
7475 (t (error "Invalid tag selection character %c" char)))
7476 (when (and maybe-refresh
7477 (eq org-agenda-clockreport-mode 'with-filter))
7478 (org-agenda-redo))))
7480 (defun org-agenda-get-represented-tags ()
7481 "Get a list of all tags currently represented in the agenda."
7482 (let (p tags)
7483 (save-excursion
7484 (goto-char (point-min))
7485 (while (setq p (next-single-property-change (point) 'tags))
7486 (goto-char p)
7487 (mapc (lambda (x) (add-to-list 'tags x))
7488 (get-text-property (point) 'tags))))
7489 tags))
7491 (defun org-agenda-filter-by-tag-refine (strip &optional char)
7492 "Refine the current filter. See `org-agenda-filter-by-tag'."
7493 (interactive "P")
7494 (org-agenda-filter-by-tag strip char 'refine))
7496 (defun org-agenda-filter-make-matcher (filter type)
7497 "Create the form that tests a line for agenda filter."
7498 (let (f f1)
7499 (cond
7500 ;; Tag filter
7501 ((eq type 'tag)
7502 (setq filter
7503 (delete-dups
7504 (append (get 'org-agenda-tag-filter :preset-filter)
7505 filter)))
7506 (dolist (x filter)
7507 (let ((nfilter (org-agenda-filter-expand-tags filter)) nf nf1
7508 (ffunc
7509 (lambda (nf0 nf01 fltr notgroup op)
7510 (dolist (x fltr)
7511 (if (member x '("-" "+"))
7512 (setq nf01 (if (equal x "-") 'tags '(not tags)))
7513 (if (string-match "[<=>?]" x)
7514 (setq nf01 (org-agenda-filter-effort-form x))
7515 (setq nf01 (list 'member (downcase (substring x 1))
7516 'tags)))
7517 (when (equal (string-to-char x) ?-)
7518 (setq nf01 (list 'not nf01))
7519 (when (not notgroup) (setq op 'and))))
7520 (push nf01 nf0))
7521 (if notgroup
7522 (push (cons 'and nf0) f)
7523 (push (cons (or op 'or) nf0) f)))))
7524 (if (equal nfilter filter)
7525 (funcall ffunc f1 f filter t nil)
7526 (funcall ffunc nf1 nf nfilter nil nil)))))
7527 ;; Category filter
7528 ((eq type 'category)
7529 (setq filter
7530 (delete-dups
7531 (append (get 'org-agenda-category-filter :preset-filter)
7532 filter)))
7533 (dolist (x filter)
7534 (if (equal "-" (substring x 0 1))
7535 (setq f1 (list 'not (list 'equal (substring x 1) 'cat)))
7536 (setq f1 (list 'equal (substring x 1) 'cat)))
7537 (push f1 f)))
7538 ;; Regexp filter
7539 ((eq type 'regexp)
7540 (setq filter
7541 (delete-dups
7542 (append (get 'org-agenda-regexp-filter :preset-filter)
7543 filter)))
7544 (dolist (x filter)
7545 (if (equal "-" (substring x 0 1))
7546 (setq f1 (list 'not (list 'string-match (substring x 1) 'txt)))
7547 (setq f1 (list 'string-match (substring x 1) 'txt)))
7548 (push f1 f))))
7549 (cons 'and (nreverse f))))
7551 (defun org-agenda-filter-effort-form (e)
7552 "Return the form to compare the effort of the current line with what E says.
7553 E looks like \"+<2:25\"."
7554 (let (op)
7555 (setq e (substring e 1))
7556 (setq op (string-to-char e) e (substring e 1))
7557 (setq op (cond ((equal op ?<) '<=)
7558 ((equal op ?>) '>=)
7559 ((equal op ??) op)
7560 (t '=)))
7561 (list 'org-agenda-compare-effort (list 'quote op)
7562 (org-duration-string-to-minutes e))))
7564 (defun org-agenda-compare-effort (op value)
7565 "Compare the effort of the current line with VALUE, using OP.
7566 If the line does not have an effort defined, return nil."
7567 (let ((eff (org-get-at-bol 'effort-minutes)))
7568 (if (equal op ??)
7569 (not eff)
7570 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
7571 value))))
7573 (defun org-agenda-filter-expand-tags (filter &optional no-operator)
7574 "Expand group tags in FILTER for the agenda.
7575 When NO-OPERATOR is non-nil, do not add the + operator to returned tags."
7576 (if org-group-tags
7577 (let ((case-fold-search t) rtn)
7578 (mapc
7579 (lambda (f)
7580 (let (f0 dir)
7581 (if (string-match "^\\([+-]\\)\\(.+\\)" f)
7582 (setq dir (match-string 1 f) f0 (match-string 2 f))
7583 (setq dir (if no-operator "" "+") f0 f))
7584 (setq rtn (append (mapcar (lambda(f1) (concat dir f1))
7585 (org-tags-expand f0 t t))
7586 rtn))))
7587 filter)
7588 (reverse rtn))
7589 filter))
7591 (defun org-agenda-filter-apply (filter type)
7592 "Set FILTER as the new agenda filter and apply it."
7593 ;; Deactivate `org-agenda-entry-text-mode' when filtering
7594 (if org-agenda-entry-text-mode (org-agenda-entry-text-mode))
7595 (let (tags cat txt)
7596 (setq org-agenda-filter-form
7597 (org-agenda-filter-make-matcher filter type))
7598 (if (and (eq type 'category)
7599 (not (equal (substring (car filter) 0 1) "-")))
7600 ;; Only set `org-agenda-filtered-by-category' to t
7601 ;; when a unique category is used as the filter
7602 (setq org-agenda-filtered-by-category t))
7603 (org-agenda-set-mode-name)
7604 (save-excursion
7605 (goto-char (point-min))
7606 (while (not (eobp))
7607 (if (org-get-at-bol 'org-marker)
7608 (progn
7609 (setq tags ; used in eval
7610 (apply 'append
7611 (mapcar (lambda (f)
7612 (org-agenda-filter-expand-tags (list f) t))
7613 (org-get-at-bol 'tags)))
7614 cat (get-text-property (point) 'org-category)
7615 txt (get-text-property (point) 'txt))
7616 (if (not (eval org-agenda-filter-form))
7617 (org-agenda-filter-hide-line type))
7618 (beginning-of-line 2))
7619 (beginning-of-line 2))))
7620 (if (get-char-property (point) 'invisible)
7621 (ignore-errors (org-agenda-previous-line)))))
7623 (defun org-agenda-filter-top-headline-apply (hl &optional negative)
7624 "Filter by top headline HL."
7625 (org-agenda-set-mode-name)
7626 (save-excursion
7627 (goto-char (point-min))
7628 (while (not (eobp))
7629 (let* ((pos (org-get-at-bol 'org-hd-marker))
7630 (tophl (and pos (org-find-top-headline pos))))
7631 (if (and tophl (funcall (if negative 'identity 'not)
7632 (string= hl tophl)))
7633 (org-agenda-filter-hide-line 'category)))
7634 (beginning-of-line 2)))
7635 (if (get-char-property (point) 'invisible)
7636 (org-agenda-previous-line))
7637 (setq org-agenda-top-headline-filter hl
7638 org-agenda-filtered-by-top-headline t))
7640 (defun org-agenda-filter-hide-line (type)
7641 "Hide lines with TYPE in the agenda buffer."
7642 (let (ov)
7643 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
7644 (point-at-eol)))
7645 (overlay-put ov 'invisible t)
7646 (overlay-put ov 'type type)
7647 (cond ((eq type 'tag) (push ov org-agenda-tag-filter-overlays))
7648 ((eq type 'category) (push ov org-agenda-cat-filter-overlays))
7649 ((eq type 'regexp) (push ov org-agenda-re-filter-overlays)))))
7651 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
7652 (setq pos (or pos (point)))
7653 (save-excursion
7654 (dolist (ov (overlays-at pos))
7655 (when (and (overlay-get ov 'invisible)
7656 (eq (overlay-get ov 'type) 'tag))
7657 (goto-char pos)
7658 (if (< (overlay-start ov) (point-at-eol))
7659 (move-overlay ov (point-at-eol)
7660 (overlay-end ov)))))))
7662 (defun org-agenda-filter-show-all-tag nil
7663 "Remove tag filter overlays from the agenda buffer."
7664 (mapc 'delete-overlay org-agenda-tag-filter-overlays)
7665 (setq org-agenda-tag-filter-overlays nil
7666 org-agenda-tag-filter nil
7667 org-agenda-filter-form nil)
7668 (org-agenda-set-mode-name))
7670 (defun org-agenda-filter-show-all-re nil
7671 "Remove regexp filter overlays from the agenda buffer."
7672 (mapc 'delete-overlay org-agenda-re-filter-overlays)
7673 (setq org-agenda-re-filter-overlays nil
7674 org-agenda-regexp-filter nil
7675 org-agenda-filter-form nil)
7676 (org-agenda-set-mode-name))
7678 (defun org-agenda-filter-show-all-cat nil
7679 "Remove category filter overlays from the agenda buffer."
7680 (mapc 'delete-overlay org-agenda-cat-filter-overlays)
7681 (setq org-agenda-cat-filter-overlays nil
7682 org-agenda-filtered-by-category nil
7683 org-agenda-category-filter nil
7684 org-agenda-filter-form nil)
7685 (org-agenda-set-mode-name))
7687 (defun org-agenda-manipulate-query-add ()
7688 "Manipulate the query by adding a search term with positive selection.
7689 Positive selection means the term must be matched for selection of an entry."
7690 (interactive)
7691 (org-agenda-manipulate-query ?\[))
7692 (defun org-agenda-manipulate-query-subtract ()
7693 "Manipulate the query by adding a search term with negative selection.
7694 Negative selection means term must not be matched for selection of an entry."
7695 (interactive)
7696 (org-agenda-manipulate-query ?\]))
7697 (defun org-agenda-manipulate-query-add-re ()
7698 "Manipulate the query by adding a search regexp with positive selection.
7699 Positive selection means the regexp must match for selection of an entry."
7700 (interactive)
7701 (org-agenda-manipulate-query ?\{))
7702 (defun org-agenda-manipulate-query-subtract-re ()
7703 "Manipulate the query by adding a search regexp with negative selection.
7704 Negative selection means regexp must not match for selection of an entry."
7705 (interactive)
7706 (org-agenda-manipulate-query ?\}))
7707 (defun org-agenda-manipulate-query (char)
7708 (cond
7709 ((memq org-agenda-type '(timeline agenda))
7710 (let ((org-agenda-include-inactive-timestamps t))
7711 (org-agenda-redo))
7712 (message "Display now includes inactive timestamps as well"))
7713 ((eq org-agenda-type 'search)
7714 (org-add-to-string
7715 'org-agenda-query-string
7716 (if org-agenda-last-search-view-search-was-boolean
7717 (cdr (assoc char '((?\[ . " +") (?\] . " -")
7718 (?\{ . " +{}") (?\} . " -{}"))))
7719 " "))
7720 (setq org-agenda-redo-command
7721 (list 'org-search-view
7722 (car (get-text-property (min (1- (point-max)) (point))
7723 'org-last-args))
7724 org-agenda-query-string
7725 (+ (length org-agenda-query-string)
7726 (if (member char '(?\{ ?\})) 0 1))))
7727 (set-register org-agenda-query-register org-agenda-query-string)
7728 (let ((org-agenda-overriding-arguments
7729 (cdr org-agenda-redo-command)))
7730 (org-agenda-redo)))
7731 (t (error "Cannot manipulate query for %s-type agenda buffers"
7732 org-agenda-type))))
7734 (defun org-add-to-string (var string)
7735 (set var (concat (symbol-value var) string)))
7737 (defun org-agenda-goto-date (span)
7738 "Jump to DATE in agenda."
7739 (interactive "P")
7740 (let* ((org-read-date-prefer-future
7741 (eval org-agenda-jump-prefer-future))
7742 (date (org-read-date))
7743 (day (time-to-days (org-time-string-to-time date)))
7744 (org-agenda-sticky-orig org-agenda-sticky)
7745 (org-agenda-buffer-tmp-name (buffer-name))
7746 (args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7747 (0-arg (or current-prefix-arg (car args)))
7748 (2-arg (nth 2 args))
7749 (with-hour-p (nth 4 org-agenda-redo-command))
7750 (newcmd (list 'org-agenda-list 0-arg date
7751 (org-agenda-span-to-ndays
7752 2-arg (org-time-string-to-absolute date))
7753 with-hour-p))
7754 (newargs (cdr newcmd))
7755 (inhibit-read-only t)
7756 org-agenda-sticky)
7757 (if (not (org-agenda-check-type t 'agenda))
7758 (error "Not available in non-agenda views")
7759 (add-text-properties (point-min) (point-max)
7760 `(org-redo-cmd ,newcmd org-last-args ,newargs))
7761 (org-agenda-redo)
7762 (goto-char (point-min))
7763 (while (not (or (= (or (get-text-property (point) 'day) 0) day)
7764 (save-excursion (move-beginning-of-line 2) (eobp))))
7765 (move-beginning-of-line 2))
7766 (setq org-agenda-sticky org-agenda-sticky-orig
7767 org-agenda-this-buffer-is-sticky org-agenda-sticky))))
7769 (defun org-agenda-goto-today ()
7770 "Go to today."
7771 (interactive)
7772 (org-agenda-check-type t 'timeline 'agenda)
7773 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7774 (curspan (nth 2 args))
7775 (tdpos (text-property-any (point-min) (point-max) 'org-today t)))
7776 (cond
7777 (tdpos (goto-char tdpos))
7778 ((eq org-agenda-type 'agenda)
7779 (let* ((sd (org-agenda-compute-starting-span
7780 (org-today) (or curspan org-agenda-ndays org-agenda-span)))
7781 (org-agenda-overriding-arguments args))
7782 (setf (nth 1 org-agenda-overriding-arguments) sd)
7783 (org-agenda-redo)
7784 (org-agenda-find-same-or-today-or-agenda)))
7785 (t (error "Cannot find today")))))
7787 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
7788 (goto-char
7789 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
7790 (text-property-any (point-min) (point-max) 'org-today t)
7791 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
7792 (and (get-text-property (min (1- (point-max)) (point)) 'org-series)
7793 (org-agenda-goto-block-beginning))
7794 (point-min))))
7796 (defun org-agenda-goto-block-beginning ()
7797 "Go the agenda block beginning."
7798 (interactive)
7799 (if (not (derived-mode-p 'org-agenda-mode))
7800 (error "Cannot execute this command outside of org-agenda-mode buffers")
7801 (let (dest)
7802 (save-excursion
7803 (unless (looking-at "\\'")
7804 (forward-char))
7805 (let* ((prop 'org-agenda-structural-header)
7806 (p (previous-single-property-change (point) prop))
7807 (n (next-single-property-change (or (and (looking-at "\\`") 1)
7808 (1- (point))) prop)))
7809 (setq dest (cond ((eq n (point-at-eol)) (1- n)) (p (1- p))))))
7810 (if (not dest)
7811 (error "Cannot find the beginning of the blog")
7812 (goto-char dest)
7813 (move-beginning-of-line 1)))))
7815 (defun org-agenda-later (arg)
7816 "Go forward in time by the current span.
7817 With prefix ARG, go forward that many times the current span."
7818 (interactive "p")
7819 (org-agenda-check-type t 'agenda)
7820 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7821 (span (or (nth 2 args) org-agenda-current-span))
7822 (sd (or (nth 1 args) (org-get-at-bol 'day) org-starting-day))
7823 (greg (calendar-gregorian-from-absolute sd))
7824 (cnt (org-get-at-bol 'org-day-cnt))
7825 greg2)
7826 (cond
7827 ((numberp span)
7828 (setq sd (+ (* span arg) sd)))
7829 ((eq span 'day)
7830 (setq sd (+ arg sd)))
7831 ((eq span 'week)
7832 (setq sd (+ (* 7 arg) sd)))
7833 ((eq span 'month)
7834 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
7835 sd (calendar-absolute-from-gregorian greg2))
7836 (setcar greg2 (1+ (car greg2))))
7837 ((eq span 'year)
7838 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
7839 sd (calendar-absolute-from-gregorian greg2))
7840 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
7842 (setq sd (+ (* span arg) sd))))
7843 (let ((org-agenda-overriding-cmd
7844 ;; `cmd' may have been set by `org-agenda-run-series' which
7845 ;; uses `org-agenda-overriding-cmd' to decide whether
7846 ;; overriding is allowed for `cmd'
7847 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7848 (org-agenda-overriding-arguments
7849 (list (car args) sd span)))
7850 (org-agenda-redo)
7851 (org-agenda-find-same-or-today-or-agenda cnt))))
7853 (defun org-agenda-earlier (arg)
7854 "Go backward in time by the current span.
7855 With prefix ARG, go backward that many times the current span."
7856 (interactive "p")
7857 (org-agenda-later (- arg)))
7859 (defun org-agenda-view-mode-dispatch ()
7860 "Call one of the view mode commands."
7861 (interactive)
7862 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
7863 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
7864 [a]rch-trees [A]rch-files clock[R]eport include[D]iary [E]ntryText")
7865 (let ((a (read-char-exclusive)))
7866 (case a
7867 (?\ (call-interactively 'org-agenda-reset-view))
7868 (?d (call-interactively 'org-agenda-day-view))
7869 (?w (call-interactively 'org-agenda-week-view))
7870 (?m (call-interactively 'org-agenda-month-view))
7871 (?y (call-interactively 'org-agenda-year-view))
7872 (?l (call-interactively 'org-agenda-log-mode))
7873 (?L (org-agenda-log-mode '(4)))
7874 (?c (org-agenda-log-mode 'clockcheck))
7875 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
7876 (?a (call-interactively 'org-agenda-archives-mode))
7877 (?A (org-agenda-archives-mode 'files))
7878 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
7879 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
7880 (?G (call-interactively 'org-agenda-toggle-time-grid))
7881 (?D (call-interactively 'org-agenda-toggle-diary))
7882 (?\! (call-interactively 'org-agenda-toggle-deadlines))
7883 (?\[ (let ((org-agenda-include-inactive-timestamps t))
7884 (org-agenda-check-type t 'timeline 'agenda)
7885 (org-agenda-redo))
7886 (message "Display now includes inactive timestamps as well"))
7887 (?q (message "Abort"))
7888 (otherwise (error "Invalid key" )))))
7890 (defun org-agenda-reset-view ()
7891 "Switch to default view for agenda."
7892 (interactive)
7893 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
7894 (defun org-agenda-day-view (&optional day-of-month)
7895 "Switch to daily view for agenda.
7896 With argument DAY-OF-MONTH, switch to that day of the month."
7897 (interactive "P")
7898 (org-agenda-change-time-span 'day day-of-month))
7899 (defun org-agenda-week-view (&optional iso-week)
7900 "Switch to daily view for agenda.
7901 With argument ISO-WEEK, switch to the corresponding ISO week.
7902 If ISO-WEEK has more then 2 digits, only the last two encode the
7903 week. Any digits before this encode a year. So 200712 means
7904 week 12 of year 2007. Years in the range 1938-2037 can also be
7905 written as 2-digit years."
7906 (interactive "P")
7907 (org-agenda-change-time-span 'week iso-week))
7908 (defun org-agenda-month-view (&optional month)
7909 "Switch to monthly view for agenda.
7910 With argument MONTH, switch to that month."
7911 (interactive "P")
7912 (org-agenda-change-time-span 'month month))
7913 (defun org-agenda-year-view (&optional year)
7914 "Switch to yearly view for agenda.
7915 With argument YEAR, switch to that year.
7916 If MONTH has more then 2 digits, only the last two encode the
7917 month. Any digits before this encode a year. So 200712 means
7918 December year 2007. Years in the range 1938-2037 can also be
7919 written as 2-digit years."
7920 (interactive "P")
7921 (when year
7922 (setq year (org-small-year-to-year year)))
7923 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
7924 (org-agenda-change-time-span 'year year)
7925 (error "Abort")))
7927 (defun org-agenda-change-time-span (span &optional n)
7928 "Change the agenda view to SPAN.
7929 SPAN may be `day', `week', `month', `year'."
7930 (org-agenda-check-type t 'agenda)
7931 (let* ((args (get-text-property (min (1- (point-max)) (point)) 'org-last-args))
7932 (curspan (nth 2 args)))
7933 (if (and (not n) (equal curspan span))
7934 (error "Viewing span is already \"%s\"" span))
7935 (let* ((sd (or (org-get-at-bol 'day)
7936 (nth 1 args)
7937 org-starting-day))
7938 (sd (org-agenda-compute-starting-span sd span n))
7939 (org-agenda-overriding-cmd
7940 (get-text-property (min (1- (point-max)) (point)) 'org-series-cmd))
7941 (org-agenda-overriding-arguments
7942 (list (car args) sd span)))
7943 (org-agenda-redo)
7944 (org-agenda-find-same-or-today-or-agenda))
7945 (org-agenda-set-mode-name)
7946 (message "Switched to %s view" span)))
7948 (defun org-agenda-compute-starting-span (sd span &optional n)
7949 "Compute starting date for agenda.
7950 SPAN may be `day', `week', `month', `year'. The return value
7951 is a cons cell with the starting date and the number of days,
7952 so that the date SD will be in that range."
7953 (let* ((greg (calendar-gregorian-from-absolute sd))
7954 (dg (nth 1 greg))
7955 (mg (car greg))
7956 (yg (nth 2 greg)))
7957 (cond
7958 ((eq span 'day)
7959 (when n
7960 (setq sd (+ (calendar-absolute-from-gregorian
7961 (list mg 1 yg))
7962 n -1))))
7963 ((eq span 'week)
7964 (let* ((nt (calendar-day-of-week
7965 (calendar-gregorian-from-absolute sd)))
7966 (d (if org-agenda-start-on-weekday
7967 (- nt org-agenda-start-on-weekday)
7970 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
7971 (when n
7972 (require 'cal-iso)
7973 (when (> n 99)
7974 (setq y1 (org-small-year-to-year (/ n 100))
7975 n (mod n 100)))
7976 (setq sd
7977 (calendar-absolute-from-iso
7978 (list n 1
7979 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
7980 ((eq span 'month)
7981 (let (y1)
7982 (when (and n (> n 99))
7983 (setq y1 (org-small-year-to-year (/ n 100))
7984 n (mod n 100)))
7985 (setq sd (calendar-absolute-from-gregorian
7986 (list (or n mg) 1 (or y1 yg))))))
7987 ((eq span 'year)
7988 (setq sd (calendar-absolute-from-gregorian
7989 (list 1 1 (or n yg))))))
7990 sd))
7992 (defun org-agenda-next-date-line (&optional arg)
7993 "Jump to the next line indicating a date in agenda buffer."
7994 (interactive "p")
7995 (org-agenda-check-type t 'agenda 'timeline)
7996 (beginning-of-line 1)
7997 ;; This does not work if user makes date format that starts with a blank
7998 (if (looking-at "^\\S-") (forward-char 1))
7999 (if (not (re-search-forward "^\\S-" nil t arg))
8000 (progn
8001 (backward-char 1)
8002 (error "No next date after this line in this buffer")))
8003 (goto-char (match-beginning 0)))
8005 (defun org-agenda-previous-date-line (&optional arg)
8006 "Jump to the previous line indicating a date in agenda buffer."
8007 (interactive "p")
8008 (org-agenda-check-type t 'agenda 'timeline)
8009 (beginning-of-line 1)
8010 (if (not (re-search-backward "^\\S-" nil t arg))
8011 (error "No previous date before this line in this buffer")))
8013 ;; Initialize the highlight
8014 (defvar org-hl (make-overlay 1 1))
8015 (overlay-put org-hl 'face 'highlight)
8017 (defun org-highlight (begin end &optional buffer)
8018 "Highlight a region with overlay."
8019 (move-overlay org-hl begin end (or buffer (current-buffer))))
8021 (defun org-unhighlight ()
8022 "Detach overlay INDEX."
8023 (org-detach-overlay org-hl))
8025 (defun org-unhighlight-once ()
8026 "Remove the highlight from its position, and this function from the hook."
8027 (remove-hook 'pre-command-hook 'org-unhighlight-once)
8028 (org-unhighlight))
8030 (defvar org-agenda-pre-follow-window-conf nil)
8031 (defun org-agenda-follow-mode ()
8032 "Toggle follow mode in an agenda buffer."
8033 (interactive)
8034 (unless org-agenda-follow-mode
8035 (setq org-agenda-pre-follow-window-conf
8036 (current-window-configuration)))
8037 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
8038 (unless org-agenda-follow-mode
8039 (set-window-configuration org-agenda-pre-follow-window-conf))
8040 (org-agenda-set-mode-name)
8041 (org-agenda-do-context-action)
8042 (message "Follow mode is %s"
8043 (if org-agenda-follow-mode "on" "off")))
8045 (defun org-agenda-entry-text-mode (&optional arg)
8046 "Toggle entry text mode in an agenda buffer."
8047 (interactive "P")
8048 (if (or org-agenda-tag-filter
8049 org-agenda-category-filter
8050 org-agenda-regexp-filter
8051 org-agenda-top-headline-filter)
8052 (user-error "Can't show entry text in filtered views")
8053 (setq org-agenda-entry-text-mode (or (integerp arg)
8054 (not org-agenda-entry-text-mode)))
8055 (org-agenda-entry-text-hide)
8056 (and org-agenda-entry-text-mode
8057 (let ((org-agenda-entry-text-maxlines
8058 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
8059 (org-agenda-entry-text-show)))
8060 (org-agenda-set-mode-name)
8061 (message "Entry text mode is %s%s"
8062 (if org-agenda-entry-text-mode "on" "off")
8063 (if (not org-agenda-entry-text-mode) ""
8064 (format " (maximum number of lines is %d)"
8065 (if (integerp arg) arg org-agenda-entry-text-maxlines))))))
8067 (defun org-agenda-clockreport-mode (&optional with-filter)
8068 "Toggle clocktable mode in an agenda buffer.
8069 With prefix arg WITH-FILTER, make the clocktable respect the current
8070 agenda filter."
8071 (interactive "P")
8072 (org-agenda-check-type t 'agenda)
8073 (if with-filter
8074 (setq org-agenda-clockreport-mode 'with-filter)
8075 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
8076 (org-agenda-set-mode-name)
8077 (org-agenda-redo)
8078 (message "Clocktable mode is %s"
8079 (if org-agenda-clockreport-mode "on" "off")))
8081 (defun org-agenda-log-mode (&optional special)
8082 "Toggle log mode in an agenda buffer.
8083 With argument SPECIAL, show all possible log items, not only the ones
8084 configured in `org-agenda-log-mode-items'.
8085 With a double `C-u' prefix arg, show *only* log items, nothing else."
8086 (interactive "P")
8087 (org-agenda-check-type t 'agenda 'timeline)
8088 (setq org-agenda-show-log
8089 (cond
8090 ((equal special '(16)) 'only)
8091 ((eq special 'clockcheck)
8092 (if (eq org-agenda-show-log 'clockcheck)
8093 nil 'clockcheck))
8094 (special '(closed clock state))
8095 (t (not org-agenda-show-log))))
8096 (org-agenda-set-mode-name)
8097 (org-agenda-redo)
8098 (message "Log mode is %s"
8099 (if org-agenda-show-log "on" "off")))
8101 (defun org-agenda-archives-mode (&optional with-files)
8102 "Toggle inclusion of items in trees marked with :ARCHIVE:.
8103 When called with a prefix argument, include all archive files as well."
8104 (interactive "P")
8105 (setq org-agenda-archives-mode
8106 (if with-files t (if org-agenda-archives-mode nil 'trees)))
8107 (org-agenda-set-mode-name)
8108 (org-agenda-redo)
8109 (message
8110 "%s"
8111 (cond
8112 ((eq org-agenda-archives-mode nil)
8113 "No archives are included")
8114 ((eq org-agenda-archives-mode 'trees)
8115 (format "Trees with :%s: tag are included" org-archive-tag))
8116 ((eq org-agenda-archives-mode t)
8117 (format "Trees with :%s: tag and all active archive files are included"
8118 org-archive-tag)))))
8120 (defun org-agenda-toggle-diary ()
8121 "Toggle diary inclusion in an agenda buffer."
8122 (interactive)
8123 (org-agenda-check-type t 'agenda)
8124 (setq org-agenda-include-diary (not org-agenda-include-diary))
8125 (org-agenda-redo)
8126 (org-agenda-set-mode-name)
8127 (message "Diary inclusion turned %s"
8128 (if org-agenda-include-diary "on" "off")))
8130 (defun org-agenda-toggle-deadlines ()
8131 "Toggle inclusion of entries with a deadline in an agenda buffer."
8132 (interactive)
8133 (org-agenda-check-type t 'agenda)
8134 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
8135 (org-agenda-redo)
8136 (org-agenda-set-mode-name)
8137 (message "Deadlines inclusion turned %s"
8138 (if org-agenda-include-deadlines "on" "off")))
8140 (defun org-agenda-toggle-time-grid ()
8141 "Toggle time grid in an agenda buffer."
8142 (interactive)
8143 (org-agenda-check-type t 'agenda)
8144 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
8145 (org-agenda-redo)
8146 (org-agenda-set-mode-name)
8147 (message "Time-grid turned %s"
8148 (if org-agenda-use-time-grid "on" "off")))
8150 (defun org-agenda-set-mode-name ()
8151 "Set the mode name to indicate all the small mode settings."
8152 (setq mode-name
8153 (list "Org-Agenda"
8154 (if (get 'org-agenda-files 'org-restrict) " []" "")
8156 '(:eval (org-agenda-span-name org-agenda-current-span))
8157 (if org-agenda-follow-mode " Follow" "")
8158 (if org-agenda-entry-text-mode " ETxt" "")
8159 (if org-agenda-include-diary " Diary" "")
8160 (if org-agenda-include-deadlines " Ddl" "")
8161 (if org-agenda-use-time-grid " Grid" "")
8162 (if (and (boundp 'org-habit-show-habits)
8163 org-habit-show-habits) " Habit" "")
8164 (cond
8165 ((consp org-agenda-show-log) " LogAll")
8166 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
8167 (org-agenda-show-log " Log")
8168 (t ""))
8169 (if (or org-agenda-category-filter
8170 (get 'org-agenda-category-filter :preset-filter))
8171 '(:eval (org-propertize
8172 (concat " <"
8173 (mapconcat
8174 'identity
8175 (append
8176 (get 'org-agenda-category-filter :preset-filter)
8177 org-agenda-category-filter)
8179 ">")
8180 'face 'org-agenda-filter-category
8181 'help-echo "Category used in filtering")) "")
8182 (if (or org-agenda-tag-filter
8183 (get 'org-agenda-tag-filter :preset-filter))
8184 '(:eval (org-propertize
8185 (concat " {"
8186 (mapconcat
8187 'identity
8188 (append
8189 (get 'org-agenda-tag-filter :preset-filter)
8190 org-agenda-tag-filter)
8192 "}")
8193 'face 'org-agenda-filter-tags
8194 'help-echo "Tags used in filtering")) "")
8195 (if (or org-agenda-regexp-filter
8196 (get 'org-agenda-regexp-filter :preset-filter))
8197 '(:eval (org-propertize
8198 (concat " ["
8199 (mapconcat
8200 'identity
8201 (append
8202 (get 'org-agenda-regexp-filter :preset-filter)
8203 org-agenda-regexp-filter)
8205 "]")
8206 'face 'org-agenda-filter-regexp
8207 'help-echo "Regexp used in filtering")) "")
8208 (if org-agenda-archives-mode
8209 (if (eq org-agenda-archives-mode t)
8210 " Archives"
8211 (format " :%s:" org-archive-tag))
8213 (if org-agenda-clockreport-mode
8214 (if (eq org-agenda-clockreport-mode 'with-filter)
8215 " Clock{}" " Clock")
8216 "")))
8217 (force-mode-line-update))
8219 (define-obsolete-function-alias
8220 'org-agenda-post-command-hook 'org-agenda-update-agenda-type "24.3")
8222 (defun org-agenda-update-agenda-type ()
8223 "Update the agenda type after each command."
8224 (setq org-agenda-type
8225 (or (get-text-property (point) 'org-agenda-type)
8226 (get-text-property (max (point-min) (1- (point))) 'org-agenda-type))))
8228 (defun org-agenda-next-line ()
8229 "Move cursor to the next line, and show if follow mode is active."
8230 (interactive)
8231 (call-interactively 'next-line)
8232 (org-agenda-do-context-action))
8234 (defun org-agenda-previous-line ()
8235 "Move cursor to the previous line, and show if follow-mode is active."
8236 (interactive)
8237 (call-interactively 'previous-line)
8238 (org-agenda-do-context-action))
8240 (defun org-agenda-next-item (n)
8241 "Move cursor to next agenda item."
8242 (interactive "p")
8243 (let ((col (current-column)))
8244 (dotimes (c n)
8245 (when (next-single-property-change (point-at-eol) 'org-marker)
8246 (move-end-of-line 1)
8247 (goto-char (next-single-property-change (point) 'org-marker))))
8248 (org-move-to-column col))
8249 (org-agenda-do-context-action))
8251 (defun org-agenda-previous-item (n)
8252 "Move cursor to next agenda item."
8253 (interactive "p")
8254 (dotimes (c n)
8255 (let ((col (current-column))
8256 (goto (save-excursion
8257 (move-end-of-line 0)
8258 (previous-single-property-change (point) 'org-marker))))
8259 (if goto (goto-char goto))
8260 (org-move-to-column col)))
8261 (org-agenda-do-context-action))
8263 (defun org-agenda-do-context-action ()
8264 "Show outline path and, maybe, follow mode window."
8265 (let ((m (org-get-at-bol 'org-marker)))
8266 (when (and (markerp m) (marker-buffer m))
8267 (and org-agenda-follow-mode
8268 (if org-agenda-follow-indirect
8269 (org-agenda-tree-to-indirect-buffer nil)
8270 (org-agenda-show)))
8271 (and org-agenda-show-outline-path
8272 (org-with-point-at m (org-display-outline-path t))))))
8274 (defun org-agenda-show-tags ()
8275 "Show the tags applicable to the current item."
8276 (interactive)
8277 (let* ((tags (org-get-at-bol 'tags)))
8278 (if tags
8279 (message "Tags are :%s:"
8280 (org-no-properties (mapconcat 'identity tags ":")))
8281 (message "No tags associated with this line"))))
8283 (defun org-agenda-goto (&optional highlight)
8284 "Go to the Org-mode file which contains the item at point."
8285 (interactive)
8286 (let* ((marker (or (org-get-at-bol 'org-marker)
8287 (org-agenda-error)))
8288 (buffer (marker-buffer marker))
8289 (pos (marker-position marker)))
8290 (switch-to-buffer-other-window buffer)
8291 (widen)
8292 (push-mark)
8293 (goto-char pos)
8294 (when (derived-mode-p 'org-mode)
8295 (org-show-context 'agenda)
8296 (save-excursion
8297 (and (outline-next-heading)
8298 (org-flag-heading nil)))) ; show the next heading
8299 (when (outline-invisible-p)
8300 (show-entry)) ; display invisible text
8301 (recenter (/ (window-height) 2))
8302 (run-hooks 'org-agenda-after-show-hook)
8303 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
8305 (defvar org-agenda-after-show-hook nil
8306 "Normal hook run after an item has been shown from the agenda.
8307 Point is in the buffer where the item originated.")
8309 (defun org-agenda-kill ()
8310 "Kill the entry or subtree belonging to the current agenda entry."
8311 (interactive)
8312 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8313 (let* ((bufname-orig (buffer-name))
8314 (marker (or (org-get-at-bol 'org-marker)
8315 (org-agenda-error)))
8316 (buffer (marker-buffer marker))
8317 (pos (marker-position marker))
8318 (type (org-get-at-bol 'type))
8319 dbeg dend (n 0) conf)
8320 (org-with-remote-undo buffer
8321 (with-current-buffer buffer
8322 (save-excursion
8323 (goto-char pos)
8324 (if (and (derived-mode-p 'org-mode) (not (member type '("sexp"))))
8325 (setq dbeg (progn (org-back-to-heading t) (point))
8326 dend (org-end-of-subtree t t))
8327 (setq dbeg (point-at-bol)
8328 dend (min (point-max) (1+ (point-at-eol)))))
8329 (goto-char dbeg)
8330 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
8331 (setq conf (or (eq t org-agenda-confirm-kill)
8332 (and (numberp org-agenda-confirm-kill)
8333 (> n org-agenda-confirm-kill))))
8334 (and conf
8335 (not (y-or-n-p
8336 (format "Delete entry with %d lines in buffer \"%s\"? "
8337 n (buffer-name buffer))))
8338 (error "Abort"))
8339 (let ((org-agenda-buffer-name bufname-orig))
8340 (org-remove-subtree-entries-from-agenda buffer dbeg dend))
8341 (with-current-buffer buffer (delete-region dbeg dend))
8342 (message "Agenda item and source killed"))))
8344 (defvar org-archive-default-command) ; defined in org-archive.el
8345 (defun org-agenda-archive-default ()
8346 "Archive the entry or subtree belonging to the current agenda entry."
8347 (interactive)
8348 (require 'org-archive)
8349 (org-agenda-archive-with org-archive-default-command))
8351 (defun org-agenda-archive-default-with-confirmation ()
8352 "Archive the entry or subtree belonging to the current agenda entry."
8353 (interactive)
8354 (require 'org-archive)
8355 (org-agenda-archive-with org-archive-default-command 'confirm))
8357 (defun org-agenda-archive ()
8358 "Archive the entry or subtree belonging to the current agenda entry."
8359 (interactive)
8360 (org-agenda-archive-with 'org-archive-subtree))
8362 (defun org-agenda-archive-to-archive-sibling ()
8363 "Move the entry to the archive sibling."
8364 (interactive)
8365 (org-agenda-archive-with 'org-archive-to-archive-sibling))
8367 (defun org-agenda-archive-with (cmd &optional confirm)
8368 "Move the entry to the archive sibling."
8369 (interactive)
8370 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
8371 (let* ((bufname-orig (buffer-name))
8372 (marker (or (org-get-at-bol 'org-marker)
8373 (org-agenda-error)))
8374 (buffer (marker-buffer marker))
8375 (pos (marker-position marker)))
8376 (org-with-remote-undo buffer
8377 (with-current-buffer buffer
8378 (if (derived-mode-p 'org-mode)
8379 (if (and confirm
8380 (not (y-or-n-p "Archive this subtree or entry? ")))
8381 (error "Abort")
8382 (save-window-excursion
8383 (goto-char pos)
8384 (let ((org-agenda-buffer-name bufname-orig))
8385 (org-remove-subtree-entries-from-agenda))
8386 (org-back-to-heading t)
8387 (funcall cmd)))
8388 (error "Archiving works only in Org-mode files"))))))
8390 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
8391 "Remove all lines in the agenda that correspond to a given subtree.
8392 The subtree is the one in buffer BUF, starting at BEG and ending at END.
8393 If this information is not given, the function uses the tree at point."
8394 (let ((buf (or buf (current-buffer))) m p)
8395 (save-excursion
8396 (unless (and beg end)
8397 (org-back-to-heading t)
8398 (setq beg (point))
8399 (org-end-of-subtree t)
8400 (setq end (point)))
8401 (set-buffer (get-buffer org-agenda-buffer-name))
8402 (save-excursion
8403 (goto-char (point-max))
8404 (beginning-of-line 1)
8405 (while (not (bobp))
8406 (when (and (setq m (org-get-at-bol 'org-marker))
8407 (equal buf (marker-buffer m))
8408 (setq p (marker-position m))
8409 (>= p beg)
8410 (< p end))
8411 (let ((inhibit-read-only t))
8412 (delete-region (point-at-bol) (1+ (point-at-eol)))))
8413 (beginning-of-line 0))))))
8415 (defun org-agenda-refile (&optional goto rfloc no-update)
8416 "Refile the item at point.
8418 When GOTO is 0 or '(64), clear the refile cache.
8419 When GOTO is '(16), go to the location of the last refiled item.
8420 RFLOC can be a refile location obtained in a different way.
8421 When NO-UPDATE is non-nil, don't redo the agenda buffer."
8422 (interactive "P")
8423 (cond
8424 ((member goto '(0 (64)))
8425 (org-refile-cache-clear))
8426 ((equal goto '(16))
8427 (org-refile-goto-last-stored))
8429 (let* ((buffer-orig (buffer-name))
8430 (marker (or (org-get-at-bol 'org-hd-marker)
8431 (org-agenda-error)))
8432 (buffer (marker-buffer marker))
8433 (pos (marker-position marker))
8434 (rfloc (or rfloc
8435 (org-refile-get-location
8436 (if goto "Goto" "Refile to") buffer
8437 org-refile-allow-creating-parent-nodes))))
8438 (with-current-buffer buffer
8439 (save-excursion
8440 (save-restriction
8441 (widen)
8442 (goto-char marker)
8443 (let ((org-agenda-buffer-name buffer-orig))
8444 (org-remove-subtree-entries-from-agenda))
8445 (org-refile goto buffer rfloc)))))
8446 (unless no-update (org-agenda-redo)))))
8448 (defun org-agenda-open-link (&optional arg)
8449 "Open the link(s) in the current entry, if any.
8450 This looks for a link in the displayed line in the agenda.
8451 It also looks at the text of the entry itself."
8452 (interactive "P")
8453 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
8454 (org-get-at-bol 'org-marker)))
8455 (buffer (and marker (marker-buffer marker)))
8456 (prefix (buffer-substring (point-at-bol) (point-at-eol)))
8457 (lkall (org-offer-links-in-entry buffer marker arg prefix))
8458 (lk0 (car lkall))
8459 (lk (if (stringp lk0) (list lk0) lk0))
8460 (lkend (cdr lkall))
8461 trg)
8462 (cond
8463 ((and buffer lk)
8464 (mapcar (lambda(l)
8465 (with-current-buffer buffer
8466 (setq trg (and (string-match org-bracket-link-regexp l)
8467 (match-string 1 l)))
8468 (if (or (not trg) (string-match org-any-link-re trg))
8469 (save-excursion
8470 (save-restriction
8471 (widen)
8472 (goto-char marker)
8473 (when (search-forward l nil lkend)
8474 (goto-char (match-beginning 0))
8475 (org-open-at-point))))
8476 ;; This is an internal link, widen the buffer
8477 (switch-to-buffer-other-window buffer)
8478 (widen)
8479 (goto-char marker)
8480 (when (search-forward l nil lkend)
8481 (goto-char (match-beginning 0))
8482 (org-open-at-point)))))
8483 lk))
8484 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
8485 (save-excursion
8486 (beginning-of-line 1)
8487 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
8488 (org-open-link-from-string (match-string 1)))
8489 (t (message "No link to open here")))))
8491 (defun org-agenda-copy-local-variable (var)
8492 "Get a variable from a referenced buffer and install it here."
8493 (let ((m (org-get-at-bol 'org-marker)))
8494 (when (and m (buffer-live-p (marker-buffer m)))
8495 (org-set-local var (with-current-buffer (marker-buffer m)
8496 (symbol-value var))))))
8498 (defun org-agenda-switch-to (&optional delete-other-windows)
8499 "Go to the Org-mode file which contains the item at point."
8500 (interactive)
8501 (if (and org-return-follows-link
8502 (not (org-get-at-bol 'org-marker))
8503 (org-in-regexp org-bracket-link-regexp))
8504 (org-open-link-from-string (match-string 0))
8505 (let* ((marker (or (org-get-at-bol 'org-marker)
8506 (org-agenda-error)))
8507 (buffer (marker-buffer marker))
8508 (pos (marker-position marker)))
8509 (org-pop-to-buffer-same-window buffer)
8510 (and delete-other-windows (delete-other-windows))
8511 (widen)
8512 (goto-char pos)
8513 (when (derived-mode-p 'org-mode)
8514 (org-show-context 'agenda)
8515 (save-excursion
8516 (and (outline-next-heading)
8517 (org-flag-heading nil))) ; show the next heading
8518 (when (outline-invisible-p)
8519 (show-entry)) ; display invisible text
8520 (run-hooks 'org-agenda-after-show-hook)))))
8522 (defun org-agenda-goto-mouse (ev)
8523 "Go to the Org-mode file which contains the item at the mouse click."
8524 (interactive "e")
8525 (mouse-set-point ev)
8526 (org-agenda-goto))
8528 (defun org-agenda-show (&optional full-entry)
8529 "Display the Org-mode file which contains the item at point.
8530 With prefix argument FULL-ENTRY, make the entire entry visible
8531 if it was hidden in the outline."
8532 (interactive "P")
8533 (let ((win (selected-window)))
8534 (if full-entry
8535 (let ((org-show-entry-below t))
8536 (org-agenda-goto t))
8537 (org-agenda-goto t))
8538 (select-window win)))
8540 (defvar org-agenda-show-window nil)
8541 (defun org-agenda-show-and-scroll-up (&optional arg)
8542 "Display the Org-mode file which contains the item at point.
8543 When called repeatedly, scroll the window that is displaying the buffer.
8544 With a \\[universal-argument] prefix, use `org-show-entry' instead of
8545 `show-subtree' to display the item, so that drawers and logbooks stay
8546 folded."
8547 (interactive "P")
8548 (let ((win (selected-window)))
8549 (if (and (window-live-p org-agenda-show-window)
8550 (eq this-command last-command))
8551 (progn
8552 (select-window org-agenda-show-window)
8553 (ignore-errors (scroll-up)))
8554 (org-agenda-goto t)
8555 (if arg (org-show-entry) (show-subtree))
8556 (setq org-agenda-show-window (selected-window)))
8557 (select-window win)))
8559 (defun org-agenda-show-scroll-down ()
8560 "Scroll down the window showing the agenda."
8561 (interactive)
8562 (let ((win (selected-window)))
8563 (when (window-live-p org-agenda-show-window)
8564 (select-window org-agenda-show-window)
8565 (ignore-errors (scroll-down))
8566 (select-window win))))
8568 (defun org-agenda-show-1 (&optional more)
8569 "Display the Org-mode file which contains the item at point.
8570 The prefix arg selects the amount of information to display:
8572 0 hide the subtree
8573 1 just show the entry according to defaults.
8574 2 show the children view
8575 3 show the subtree view
8576 4 show the entire subtree and any LOGBOOK drawers
8577 5 show the entire subtree and any drawers
8578 With prefix argument FULL-ENTRY, make the entire entry visible
8579 if it was hidden in the outline."
8580 (interactive "p")
8581 (let ((win (selected-window)))
8582 (org-agenda-goto t)
8583 (org-recenter-heading 1)
8584 (cond
8585 ((= more 0)
8586 (hide-subtree)
8587 (save-excursion
8588 (org-back-to-heading)
8589 (run-hook-with-args 'org-cycle-hook 'folded))
8590 (message "Remote: FOLDED"))
8591 ((and (org-called-interactively-p 'any) (= more 1))
8592 (message "Remote: show with default settings"))
8593 ((= more 2)
8594 (show-entry)
8595 (show-children)
8596 (save-excursion
8597 (org-back-to-heading)
8598 (run-hook-with-args 'org-cycle-hook 'children))
8599 (message "Remote: CHILDREN"))
8600 ((= more 3)
8601 (show-subtree)
8602 (save-excursion
8603 (org-back-to-heading)
8604 (run-hook-with-args 'org-cycle-hook 'subtree))
8605 (message "Remote: SUBTREE"))
8606 ((= more 4)
8607 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
8608 (org-drawer-regexp
8609 (concat "^[ \t]*:\\("
8610 (mapconcat 'regexp-quote org-drawers "\\|")
8611 "\\):[ \t]*$")))
8612 (show-subtree)
8613 (save-excursion
8614 (org-back-to-heading)
8615 (org-cycle-hide-drawers 'subtree)))
8616 (message "Remote: SUBTREE AND LOGBOOK"))
8617 ((> more 4)
8618 (show-subtree)
8619 (message "Remote: SUBTREE AND ALL DRAWERS")))
8620 (select-window win)))
8622 (defun org-recenter-heading (n)
8623 (save-excursion
8624 (org-back-to-heading)
8625 (recenter n)))
8627 (defvar org-agenda-cycle-counter nil)
8628 (defun org-agenda-cycle-show (&optional n)
8629 "Show the current entry in another window, with default settings.
8630 Default settings are taken from `org-show-hierarchy-above' and siblings.
8631 When use repeatedly in immediate succession, the remote entry will cycle
8632 through visibility
8634 children -> subtree -> folded
8636 When called with a numeric prefix arg, that arg will be passed through to
8637 `org-agenda-show-1'. For the interpretation of that argument, see the
8638 docstring of `org-agenda-show-1'."
8639 (interactive "P")
8640 (if (integerp n)
8641 (setq org-agenda-cycle-counter n)
8642 (if (not (eq last-command this-command))
8643 (setq org-agenda-cycle-counter 1)
8644 (if (equal org-agenda-cycle-counter 0)
8645 (setq org-agenda-cycle-counter 2)
8646 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
8647 (if (> org-agenda-cycle-counter 3)
8648 (setq org-agenda-cycle-counter 0)))))
8649 (org-agenda-show-1 org-agenda-cycle-counter))
8651 (defun org-agenda-recenter (arg)
8652 "Display the Org-mode file which contains the item at point and recenter."
8653 (interactive "P")
8654 (let ((win (selected-window)))
8655 (org-agenda-goto t)
8656 (recenter arg)
8657 (select-window win)))
8659 (defun org-agenda-show-mouse (ev)
8660 "Display the Org-mode file which contains the item at the mouse click."
8661 (interactive "e")
8662 (mouse-set-point ev)
8663 (org-agenda-show))
8665 (defun org-agenda-check-no-diary ()
8666 "Check if the entry is a diary link and abort if yes."
8667 (if (org-get-at-bol 'org-agenda-diary-link)
8668 (org-agenda-error)))
8670 (defun org-agenda-error ()
8671 (error "Command not allowed in this line"))
8673 (defun org-agenda-tree-to-indirect-buffer (arg)
8674 "Show the subtree corresponding to the current entry in an indirect buffer.
8675 This calls the command `org-tree-to-indirect-buffer' from the original buffer.
8677 With a numerical prefix ARG, go up to this level and then take that tree.
8678 With a negative numeric ARG, go up by this number of levels.
8679 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
8680 use the dedicated frame)."
8681 (interactive "P")
8682 (if current-prefix-arg
8683 (org-agenda-do-tree-to-indirect-buffer arg)
8684 (let ((agenda-buffer (buffer-name))
8685 (agenda-window (selected-window))
8686 (indirect-window
8687 (and org-last-indirect-buffer
8688 (get-buffer-window org-last-indirect-buffer))))
8689 (save-window-excursion (org-agenda-do-tree-to-indirect-buffer arg))
8690 (unless (or (eq org-indirect-buffer-display 'new-frame)
8691 (eq org-indirect-buffer-display 'dedicated-frame))
8692 (unwind-protect
8693 (unless (and indirect-window (window-live-p indirect-window))
8694 (setq indirect-window (split-window agenda-window)))
8695 (and indirect-window (select-window indirect-window))
8696 (switch-to-buffer org-last-indirect-buffer :norecord)
8697 (fit-window-to-buffer indirect-window)))
8698 (select-window (get-buffer-window agenda-buffer)))))
8700 (defun org-agenda-do-tree-to-indirect-buffer (arg)
8701 "Same as `org-agenda-tree-to-indirect-buffer' without saving window."
8702 (org-agenda-check-no-diary)
8703 (let* ((marker (or (org-get-at-bol 'org-marker)
8704 (org-agenda-error)))
8705 (buffer (marker-buffer marker))
8706 (pos (marker-position marker)))
8707 (with-current-buffer buffer
8708 (save-excursion
8709 (goto-char pos)
8710 (funcall 'org-tree-to-indirect-buffer arg)))))
8712 (defvar org-last-heading-marker (make-marker)
8713 "Marker pointing to the headline that last changed its TODO state
8714 by a remote command from the agenda.")
8716 (defun org-agenda-todo-nextset ()
8717 "Switch TODO entry to next sequence."
8718 (interactive)
8719 (org-agenda-todo 'nextset))
8721 (defun org-agenda-todo-previousset ()
8722 "Switch TODO entry to previous sequence."
8723 (interactive)
8724 (org-agenda-todo 'previousset))
8726 (defun org-agenda-todo (&optional arg)
8727 "Cycle TODO state of line at point, also in Org-mode file.
8728 This changes the line at point, all other lines in the agenda referring to
8729 the same tree node, and the headline of the tree node in the Org-mode file."
8730 (interactive "P")
8731 (org-agenda-check-no-diary)
8732 (let* ((col (current-column))
8733 (marker (or (org-get-at-bol 'org-marker)
8734 (org-agenda-error)))
8735 (buffer (marker-buffer marker))
8736 (pos (marker-position marker))
8737 (hdmarker (org-get-at-bol 'org-hd-marker))
8738 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
8739 (inhibit-read-only t)
8740 org-agenda-headline-snapshot-before-repeat newhead just-one)
8741 (org-with-remote-undo buffer
8742 (with-current-buffer buffer
8743 (widen)
8744 (goto-char pos)
8745 (org-show-context 'agenda)
8746 (save-excursion
8747 (and (outline-next-heading)
8748 (org-flag-heading nil))) ; show the next heading
8749 (let ((current-prefix-arg arg))
8750 (call-interactively 'org-todo))
8751 (and (bolp) (forward-char 1))
8752 (setq newhead (org-get-heading))
8753 (when (and (org-bound-and-true-p
8754 org-agenda-headline-snapshot-before-repeat)
8755 (not (equal org-agenda-headline-snapshot-before-repeat
8756 newhead))
8757 todayp)
8758 (setq newhead org-agenda-headline-snapshot-before-repeat
8759 just-one t))
8760 (save-excursion
8761 (org-back-to-heading)
8762 (move-marker org-last-heading-marker (point))))
8763 (beginning-of-line 1)
8764 (save-excursion
8765 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
8766 (org-move-to-column col))))
8768 (defun org-agenda-add-note (&optional arg)
8769 "Add a time-stamped note to the entry at point."
8770 (interactive "P")
8771 (org-agenda-check-no-diary)
8772 (let* ((marker (or (org-get-at-bol 'org-marker)
8773 (org-agenda-error)))
8774 (buffer (marker-buffer marker))
8775 (pos (marker-position marker))
8776 (hdmarker (org-get-at-bol 'org-hd-marker))
8777 (inhibit-read-only t))
8778 (with-current-buffer buffer
8779 (widen)
8780 (goto-char pos)
8781 (org-show-context 'agenda)
8782 (save-excursion
8783 (and (outline-next-heading)
8784 (org-flag-heading nil))) ; show the next heading
8785 (org-add-note))))
8787 (defun org-agenda-change-all-lines (newhead hdmarker
8788 &optional fixface just-this)
8789 "Change all lines in the agenda buffer which match HDMARKER.
8790 The new content of the line will be NEWHEAD (as modified by
8791 `org-agenda-format-item'). HDMARKER is checked with
8792 `equal' against all `org-hd-marker' text properties in the file.
8793 If FIXFACE is non-nil, the face of each item is modified according to
8794 the new TODO state.
8795 If JUST-THIS is non-nil, change just the current line, not all.
8796 If FORCE-TAGS is non nil, the car of it returns the new tags."
8797 (let* ((inhibit-read-only t)
8798 (line (org-current-line))
8799 (org-agenda-buffer (current-buffer))
8800 (thetags (with-current-buffer (marker-buffer hdmarker)
8801 (save-excursion (save-restriction (widen)
8802 (goto-char hdmarker)
8803 (org-get-tags-at)))))
8804 props m pl undone-face done-face finish new dotime level cat tags)
8805 (save-excursion
8806 (goto-char (point-max))
8807 (beginning-of-line 1)
8808 (while (not finish)
8809 (setq finish (bobp))
8810 (when (and (setq m (org-get-at-bol 'org-hd-marker))
8811 (or (not just-this) (= (org-current-line) line))
8812 (equal m hdmarker))
8813 (setq props (text-properties-at (point))
8814 dotime (org-get-at-bol 'dotime)
8815 cat (org-get-at-bol 'org-category)
8816 level (org-get-at-bol 'level)
8817 tags thetags
8819 (let ((org-prefix-format-compiled
8820 (or (get-text-property (min (1- (point-max)) (point)) 'format)
8821 org-prefix-format-compiled))
8822 (extra (org-get-at-bol 'extra)))
8823 (with-current-buffer (marker-buffer hdmarker)
8824 (save-excursion
8825 (save-restriction
8826 (widen)
8827 (org-agenda-format-item extra newhead level cat tags dotime)))))
8828 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
8829 undone-face (org-get-at-bol 'undone-face)
8830 done-face (org-get-at-bol 'done-face))
8831 (beginning-of-line 1)
8832 (cond
8833 ((equal new "")
8834 (and (looking-at ".*\n?") (replace-match "")))
8835 ((looking-at ".*")
8836 (replace-match new t t)
8837 (beginning-of-line 1)
8838 (add-text-properties (point-at-bol) (point-at-eol) props)
8839 (when fixface
8840 (add-text-properties
8841 (point-at-bol) (point-at-eol)
8842 (list 'face
8843 (if org-last-todo-state-is-todo
8844 undone-face done-face))))
8845 (org-agenda-highlight-todo 'line)
8846 (beginning-of-line 1))
8847 (t (error "Line update did not work")))
8848 (save-restriction
8849 (narrow-to-region (point-at-bol) (point-at-eol))
8850 (org-agenda-finalize)))
8851 (beginning-of-line 0)))))
8853 (defun org-agenda-align-tags (&optional line)
8854 "Align all tags in agenda items to `org-agenda-tags-column'."
8855 (let ((inhibit-read-only t) l c)
8856 (save-excursion
8857 (goto-char (if line (point-at-bol) (point-min)))
8858 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
8859 (if line (point-at-eol) nil) t)
8860 (add-text-properties
8861 (match-beginning 2) (match-end 2)
8862 (list 'face (delq nil (let ((prop (get-text-property
8863 (match-beginning 2) 'face)))
8864 (or (listp prop) (setq prop (list prop)))
8865 (if (memq 'org-tag prop)
8866 prop
8867 (cons 'org-tag prop))))))
8868 (setq l (- (match-end 2) (match-beginning 2))
8869 c (if (< org-agenda-tags-column 0)
8870 (- (abs org-agenda-tags-column) l)
8871 org-agenda-tags-column))
8872 (delete-region (match-beginning 1) (match-end 1))
8873 (goto-char (match-beginning 1))
8874 (insert (org-add-props
8875 (make-string (max 1 (- c (current-column))) ?\ )
8876 (plist-put (copy-sequence (text-properties-at (point)))
8877 'face nil))))
8878 (goto-char (point-min))
8879 (org-font-lock-add-tag-faces (point-max)))))
8881 (defun org-agenda-priority-up ()
8882 "Increase the priority of line at point, also in Org-mode file."
8883 (interactive)
8884 (org-agenda-priority 'up))
8886 (defun org-agenda-priority-down ()
8887 "Decrease the priority of line at point, also in Org-mode file."
8888 (interactive)
8889 (org-agenda-priority 'down))
8891 (defun org-agenda-priority (&optional force-direction)
8892 "Set the priority of line at point, also in Org-mode file.
8893 This changes the line at point, all other lines in the agenda referring to
8894 the same tree node, and the headline of the tree node in the Org-mode file.
8895 Called with a universal prefix arg, show the priority instead of setting it."
8896 (interactive "P")
8897 (if (equal force-direction '(4))
8898 (org-show-priority)
8899 (unless org-enable-priority-commands
8900 (error "Priority commands are disabled"))
8901 (org-agenda-check-no-diary)
8902 (let* ((marker (or (org-get-at-bol 'org-marker)
8903 (org-agenda-error)))
8904 (hdmarker (org-get-at-bol 'org-hd-marker))
8905 (buffer (marker-buffer hdmarker))
8906 (pos (marker-position hdmarker))
8907 (inhibit-read-only t)
8908 newhead)
8909 (org-with-remote-undo buffer
8910 (with-current-buffer buffer
8911 (widen)
8912 (goto-char pos)
8913 (org-show-context 'agenda)
8914 (save-excursion
8915 (and (outline-next-heading)
8916 (org-flag-heading nil))) ; show the next heading
8917 (funcall 'org-priority force-direction)
8918 (end-of-line 1)
8919 (setq newhead (org-get-heading)))
8920 (org-agenda-change-all-lines newhead hdmarker)
8921 (beginning-of-line 1)))))
8923 ;; FIXME: should fix the tags property of the agenda line.
8924 (defun org-agenda-set-tags (&optional tag onoff)
8925 "Set tags for the current headline."
8926 (interactive)
8927 (org-agenda-check-no-diary)
8928 (if (and (org-region-active-p) (org-called-interactively-p 'any))
8929 (call-interactively 'org-change-tag-in-region)
8930 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8931 (org-agenda-error)))
8932 (buffer (marker-buffer hdmarker))
8933 (pos (marker-position hdmarker))
8934 (inhibit-read-only t)
8935 newhead)
8936 (org-with-remote-undo buffer
8937 (with-current-buffer buffer
8938 (widen)
8939 (goto-char pos)
8940 (save-excursion
8941 (org-show-context 'agenda))
8942 (save-excursion
8943 (and (outline-next-heading)
8944 (org-flag-heading nil))) ; show the next heading
8945 (goto-char pos)
8946 (if tag
8947 (org-toggle-tag tag onoff)
8948 (call-interactively 'org-set-tags))
8949 (end-of-line 1)
8950 (setq newhead (org-get-heading)))
8951 (org-agenda-change-all-lines newhead hdmarker)
8952 (beginning-of-line 1)))))
8954 (defun org-agenda-set-property ()
8955 "Set a property for the current headline."
8956 (interactive)
8957 (org-agenda-check-no-diary)
8958 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8959 (org-agenda-error)))
8960 (buffer (marker-buffer hdmarker))
8961 (pos (marker-position hdmarker))
8962 (inhibit-read-only t)
8963 newhead)
8964 (org-with-remote-undo buffer
8965 (with-current-buffer buffer
8966 (widen)
8967 (goto-char pos)
8968 (save-excursion
8969 (org-show-context 'agenda))
8970 (save-excursion
8971 (and (outline-next-heading)
8972 (org-flag-heading nil))) ; show the next heading
8973 (goto-char pos)
8974 (call-interactively 'org-set-property)))))
8976 (defun org-agenda-set-effort ()
8977 "Set the effort property for the current headline."
8978 (interactive)
8979 (org-agenda-check-no-diary)
8980 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
8981 (org-agenda-error)))
8982 (buffer (marker-buffer hdmarker))
8983 (pos (marker-position hdmarker))
8984 (inhibit-read-only t)
8985 newhead)
8986 (org-with-remote-undo buffer
8987 (with-current-buffer buffer
8988 (widen)
8989 (goto-char pos)
8990 (save-excursion
8991 (org-show-context 'agenda))
8992 (save-excursion
8993 (and (outline-next-heading)
8994 (org-flag-heading nil))) ; show the next heading
8995 (goto-char pos)
8996 (call-interactively 'org-set-effort)
8997 (end-of-line 1)
8998 (setq newhead (org-get-heading)))
8999 (org-agenda-change-all-lines newhead hdmarker))))
9001 (defun org-agenda-toggle-archive-tag ()
9002 "Toggle the archive tag for the current entry."
9003 (interactive)
9004 (org-agenda-check-no-diary)
9005 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
9006 (org-agenda-error)))
9007 (buffer (marker-buffer hdmarker))
9008 (pos (marker-position hdmarker))
9009 (inhibit-read-only t)
9010 newhead)
9011 (org-with-remote-undo buffer
9012 (with-current-buffer buffer
9013 (widen)
9014 (goto-char pos)
9015 (org-show-context 'agenda)
9016 (save-excursion
9017 (and (outline-next-heading)
9018 (org-flag-heading nil))) ; show the next heading
9019 (call-interactively 'org-toggle-archive-tag)
9020 (end-of-line 1)
9021 (setq newhead (org-get-heading)))
9022 (org-agenda-change-all-lines newhead hdmarker)
9023 (beginning-of-line 1))))
9025 (defun org-agenda-do-date-later (arg)
9026 (interactive "P")
9027 (cond
9028 ((or (equal arg '(16))
9029 (memq last-command
9030 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9031 (setq this-command 'org-agenda-date-later-minutes)
9032 (org-agenda-date-later-minutes 1))
9033 ((or (equal arg '(4))
9034 (memq last-command
9035 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9036 (setq this-command 'org-agenda-date-later-hours)
9037 (org-agenda-date-later-hours 1))
9039 (org-agenda-date-later (prefix-numeric-value arg)))))
9041 (defun org-agenda-do-date-earlier (arg)
9042 (interactive "P")
9043 (cond
9044 ((or (equal arg '(16))
9045 (memq last-command
9046 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
9047 (setq this-command 'org-agenda-date-earlier-minutes)
9048 (org-agenda-date-earlier-minutes 1))
9049 ((or (equal arg '(4))
9050 (memq last-command
9051 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
9052 (setq this-command 'org-agenda-date-earlier-hours)
9053 (org-agenda-date-earlier-hours 1))
9055 (org-agenda-date-earlier (prefix-numeric-value arg)))))
9057 (defun org-agenda-date-later (arg &optional what)
9058 "Change the date of this item to ARG day(s) later."
9059 (interactive "p")
9060 (org-agenda-check-type t 'agenda 'timeline)
9061 (org-agenda-check-no-diary)
9062 (let* ((marker (or (org-get-at-bol 'org-marker)
9063 (org-agenda-error)))
9064 (buffer (marker-buffer marker))
9065 (pos (marker-position marker))
9066 cdate today)
9067 (org-with-remote-undo buffer
9068 (with-current-buffer buffer
9069 (widen)
9070 (goto-char pos)
9071 (if (not (org-at-timestamp-p))
9072 (error "Cannot find time stamp"))
9073 (when (and org-agenda-move-date-from-past-immediately-to-today
9074 (equal arg 1)
9075 (or (not what) (eq what 'day))
9076 (not (save-match-data (org-at-date-range-p))))
9077 (setq cdate (org-parse-time-string (match-string 0) 'nodefault)
9078 cdate (calendar-absolute-from-gregorian
9079 (list (nth 4 cdate) (nth 3 cdate) (nth 5 cdate)))
9080 today (org-today))
9081 (if (> today cdate)
9082 ;; immediately shift to today
9083 (setq arg (- today cdate))))
9084 (org-timestamp-change arg (or what 'day))
9085 (when (and (org-at-date-range-p)
9086 (re-search-backward org-tr-regexp-both (point-at-bol)))
9087 (let ((end org-last-changed-timestamp))
9088 (org-timestamp-change arg (or what 'day))
9089 (setq org-last-changed-timestamp
9090 (concat org-last-changed-timestamp "--" end)))))
9091 (org-agenda-show-new-time marker org-last-changed-timestamp))
9092 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9094 (defun org-agenda-date-earlier (arg &optional what)
9095 "Change the date of this item to ARG day(s) earlier."
9096 (interactive "p")
9097 (org-agenda-date-later (- arg) what))
9099 (defun org-agenda-date-later-minutes (arg)
9100 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9101 (interactive "p")
9102 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9103 (org-agenda-date-later arg 'minute))
9105 (defun org-agenda-date-earlier-minutes (arg)
9106 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
9107 (interactive "p")
9108 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
9109 (org-agenda-date-earlier arg 'minute))
9111 (defun org-agenda-date-later-hours (arg)
9112 "Change the time of this item, in hour steps."
9113 (interactive "p")
9114 (org-agenda-date-later arg 'hour))
9116 (defun org-agenda-date-earlier-hours (arg)
9117 "Change the time of this item, in hour steps."
9118 (interactive "p")
9119 (org-agenda-date-earlier arg 'hour))
9121 (defun org-agenda-show-new-time (marker stamp &optional prefix)
9122 "Show new date stamp via text properties."
9123 ;; We use text properties to make this undoable
9124 (let ((inhibit-read-only t))
9125 (setq stamp (concat prefix " => " stamp " "))
9126 (save-excursion
9127 (goto-char (point-max))
9128 (while (not (bobp))
9129 (when (equal marker (org-get-at-bol 'org-marker))
9130 (org-move-to-column (- (window-width) (length stamp)) t)
9131 (org-agenda-fix-tags-filter-overlays-at (point))
9132 (if (featurep 'xemacs)
9133 ;; Use `duplicable' property to trigger undo recording
9134 (let ((ex (make-extent nil nil))
9135 (gl (make-glyph stamp)))
9136 (set-glyph-face gl 'secondary-selection)
9137 (set-extent-properties
9138 ex (list 'invisible t 'end-glyph gl 'duplicable t))
9139 (insert-extent ex (1- (point)) (point-at-eol)))
9140 (add-text-properties
9141 (1- (point)) (point-at-eol)
9142 (list 'display (org-add-props stamp nil
9143 'face 'secondary-selection))))
9144 (beginning-of-line 1))
9145 (beginning-of-line 0)))))
9147 (defun org-agenda-date-prompt (arg)
9148 "Change the date of this item. Date is prompted for, with default today.
9149 The prefix ARG is passed to the `org-time-stamp' command and can therefore
9150 be used to request time specification in the time stamp."
9151 (interactive "P")
9152 (org-agenda-check-type t 'agenda 'timeline)
9153 (org-agenda-check-no-diary)
9154 (let* ((marker (or (org-get-at-bol 'org-marker)
9155 (org-agenda-error)))
9156 (buffer (marker-buffer marker))
9157 (pos (marker-position marker)))
9158 (org-with-remote-undo buffer
9159 (with-current-buffer buffer
9160 (widen)
9161 (goto-char pos)
9162 (if (not (org-at-timestamp-p t))
9163 (error "Cannot find time stamp"))
9164 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
9165 (org-agenda-show-new-time marker org-last-changed-timestamp))
9166 (message "Time stamp changed to %s" org-last-changed-timestamp)))
9168 (defun org-agenda-schedule (arg &optional time)
9169 "Schedule the item at point.
9170 ARG is passed through to `org-schedule'."
9171 (interactive "P")
9172 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9173 (org-agenda-check-no-diary)
9174 (let* ((marker (or (org-get-at-bol 'org-marker)
9175 (org-agenda-error)))
9176 (type (marker-insertion-type marker))
9177 (buffer (marker-buffer marker))
9178 (pos (marker-position marker))
9179 (org-insert-labeled-timestamps-at-point nil)
9181 (set-marker-insertion-type marker t)
9182 (org-with-remote-undo buffer
9183 (with-current-buffer buffer
9184 (widen)
9185 (goto-char pos)
9186 (setq ts (org-schedule arg time)))
9187 (org-agenda-show-new-time marker ts " S"))
9188 (message "%s" ts)))
9190 (defun org-agenda-deadline (arg &optional time)
9191 "Schedule the item at point.
9192 ARG is passed through to `org-deadline'."
9193 (interactive "P")
9194 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
9195 (org-agenda-check-no-diary)
9196 (let* ((marker (or (org-get-at-bol 'org-marker)
9197 (org-agenda-error)))
9198 (buffer (marker-buffer marker))
9199 (pos (marker-position marker))
9200 (org-insert-labeled-timestamps-at-point nil)
9202 (org-with-remote-undo buffer
9203 (with-current-buffer buffer
9204 (widen)
9205 (goto-char pos)
9206 (setq ts (org-deadline arg time)))
9207 (org-agenda-show-new-time marker ts " D"))
9208 (message "%s" ts)))
9210 (defun org-agenda-clock-in (&optional arg)
9211 "Start the clock on the currently selected item."
9212 (interactive "P")
9213 (org-agenda-check-no-diary)
9214 (if (equal arg '(4))
9215 (org-clock-in arg)
9216 (let* ((marker (or (org-get-at-bol 'org-marker)
9217 (org-agenda-error)))
9218 (hdmarker (or (org-get-at-bol 'org-hd-marker) marker))
9219 (pos (marker-position marker))
9220 (col (current-column))
9221 newhead)
9222 (org-with-remote-undo (marker-buffer marker)
9223 (with-current-buffer (marker-buffer marker)
9224 (widen)
9225 (goto-char pos)
9226 (org-show-context 'agenda)
9227 (org-show-entry)
9228 (org-cycle-hide-drawers 'children)
9229 (org-clock-in arg)
9230 (setq newhead (org-get-heading)))
9231 (org-agenda-change-all-lines newhead hdmarker))
9232 (org-move-to-column col))))
9234 (defun org-agenda-clock-out ()
9235 "Stop the currently running clock."
9236 (interactive)
9237 (unless (marker-buffer org-clock-marker)
9238 (error "No running clock"))
9239 (let ((marker (make-marker)) (col (current-column)) newhead)
9240 (org-with-remote-undo (marker-buffer org-clock-marker)
9241 (with-current-buffer (marker-buffer org-clock-marker)
9242 (save-excursion
9243 (save-restriction
9244 (widen)
9245 (goto-char org-clock-marker)
9246 (org-back-to-heading t)
9247 (move-marker marker (point))
9248 (org-clock-out)
9249 (setq newhead (org-get-heading))))))
9250 (org-agenda-change-all-lines newhead marker)
9251 (move-marker marker nil)
9252 (org-move-to-column col)
9253 (org-agenda-unmark-clocking-task)))
9255 (defun org-agenda-clock-cancel (&optional arg)
9256 "Cancel the currently running clock."
9257 (interactive "P")
9258 (unless (marker-buffer org-clock-marker)
9259 (user-error "No running clock"))
9260 (org-with-remote-undo (marker-buffer org-clock-marker)
9261 (org-clock-cancel)))
9263 (defun org-agenda-clock-goto ()
9264 "Jump to the currently clocked in task within the agenda.
9265 If the currently clocked in task is not listed in the agenda
9266 buffer, display it in another window."
9267 (interactive)
9268 (let (pos)
9269 (mapc (lambda (o)
9270 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
9271 (setq pos (overlay-start o))))
9272 (overlays-in (point-min) (point-max)))
9273 (cond (pos (goto-char pos))
9274 ;; If the currently clocked entry is not in the agenda
9275 ;; buffer, we visit it in another window:
9276 (org-clock-current-task
9277 (org-switch-to-buffer-other-window (org-clock-goto)))
9278 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
9280 (defun org-agenda-diary-entry-in-org-file ()
9281 "Make a diary entry in the file `org-agenda-diary-file'."
9282 (let (d1 d2 char (text "") dp1 dp2)
9283 (if (equal (buffer-name) "*Calendar*")
9284 (setq d1 (calendar-cursor-to-date t)
9285 d2 (car calendar-mark-ring))
9286 (setq dp1 (get-text-property (point-at-bol) 'day))
9287 (unless dp1 (user-error "No date defined in current line"))
9288 (setq d1 (calendar-gregorian-from-absolute dp1)
9289 d2 (and (ignore-errors (mark))
9290 (save-excursion
9291 (goto-char (mark))
9292 (setq dp2 (get-text-property (point-at-bol) 'day)))
9293 (calendar-gregorian-from-absolute dp2))))
9294 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
9295 (setq char (read-char-exclusive))
9296 (cond
9297 ((equal char ?d)
9298 (setq text (read-string "Day entry: "))
9299 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
9300 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9301 ((equal char ?a)
9302 (setq d1 (list (car d1) (nth 1 d1)
9303 (read-number (format "Reference year [%d]: " (nth 2 d1))
9304 (nth 2 d1))))
9305 (setq text (read-string "Anniversary (use %d to show years): "))
9306 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
9307 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9308 ((equal char ?b)
9309 (setq text (read-string "Block entry: "))
9310 (unless (and d1 d2 (not (equal d1 d2)))
9311 (user-error "No block of days selected"))
9312 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
9313 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
9314 ((equal char ?j)
9315 (org-switch-to-buffer-other-window
9316 (find-file-noselect org-agenda-diary-file))
9317 (require 'org-datetree)
9318 (org-datetree-find-date-create d1)
9319 (org-reveal t))
9320 (t (user-error "Invalid selection character `%c'" char)))))
9322 (defcustom org-agenda-insert-diary-strategy 'date-tree
9323 "Where in `org-agenda-diary-file' should new entries be added?
9324 Valid values:
9326 date-tree in the date tree, as child of the date
9327 top-level as top-level entries at the end of the file."
9328 :group 'org-agenda
9329 :type '(choice
9330 (const :tag "in a date tree" date-tree)
9331 (const :tag "as top level at end of file" top-level)))
9333 (defcustom org-agenda-insert-diary-extract-time nil
9334 "Non-nil means extract any time specification from the diary entry."
9335 :group 'org-agenda
9336 :version "24.1"
9337 :type 'boolean)
9339 (defcustom org-agenda-bulk-mark-char ">"
9340 "A single-character string to be used as the bulk mark."
9341 :group 'org-agenda
9342 :version "24.1"
9343 :type 'string)
9345 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
9346 "Add a diary entry with TYPE to `org-agenda-diary-file'.
9347 If TEXT is not empty, it will become the headline of the new entry, and
9348 the resulting entry will not be shown. When TEXT is empty, switch to
9349 `org-agenda-diary-file' and let the user finish the entry there."
9350 (let ((cw (current-window-configuration)))
9351 (org-switch-to-buffer-other-window
9352 (find-file-noselect org-agenda-diary-file))
9353 (widen)
9354 (goto-char (point-min))
9355 (cond
9356 ((eq type 'anniversary)
9357 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
9358 (progn
9359 (or (org-at-heading-p t)
9360 (progn
9361 (outline-next-heading)
9362 (insert "* Anniversaries\n\n")
9363 (beginning-of-line -1)))))
9364 (outline-next-heading)
9365 (org-back-over-empty-lines)
9366 (backward-char 1)
9367 (insert "\n")
9368 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
9369 (nth 2 d1) (car d1) (nth 1 d1) text)))
9370 ((eq type 'day)
9371 (let ((org-prefix-has-time t)
9372 (org-agenda-time-leading-zero t)
9373 fmt time time2)
9374 (if org-agenda-insert-diary-extract-time
9375 ;; Use org-agenda-format-item to parse text for a time-range and
9376 ;; remove it. FIXME: This is a hack, we should refactor
9377 ;; that function to make time extraction available separately
9378 (setq fmt (org-agenda-format-item nil text nil nil nil t)
9379 time (get-text-property 0 'time fmt)
9380 time2 (if (> (length time) 0)
9381 ;; split-string removes trailing ...... if
9382 ;; no end time given. First space
9383 ;; separates time from date.
9384 (concat " " (car (split-string time "\\.")))
9385 nil)
9386 text (get-text-property 0 'txt fmt)))
9387 (if (eq org-agenda-insert-diary-strategy 'top-level)
9388 (org-agenda-insert-diary-as-top-level text)
9389 (require 'org-datetree)
9390 (org-datetree-find-date-create d1)
9391 (org-agenda-insert-diary-make-new-entry text))
9392 (org-insert-time-stamp (org-time-from-absolute
9393 (calendar-absolute-from-gregorian d1))
9394 nil nil nil nil time2))
9395 (end-of-line 0))
9396 ((eq type 'block)
9397 (if (> (calendar-absolute-from-gregorian d1)
9398 (calendar-absolute-from-gregorian d2))
9399 (setq d1 (prog1 d2 (setq d2 d1))))
9400 (if (eq org-agenda-insert-diary-strategy 'top-level)
9401 (org-agenda-insert-diary-as-top-level text)
9402 (require 'org-datetree)
9403 (org-datetree-find-date-create d1)
9404 (org-agenda-insert-diary-make-new-entry text))
9405 (org-insert-time-stamp (org-time-from-absolute
9406 (calendar-absolute-from-gregorian d1)))
9407 (insert "--")
9408 (org-insert-time-stamp (org-time-from-absolute
9409 (calendar-absolute-from-gregorian d2)))
9410 (end-of-line 0)))
9411 (if (string-match "\\S-" text)
9412 (progn
9413 (set-window-configuration cw)
9414 (message "%s entry added to %s"
9415 (capitalize (symbol-name type))
9416 (abbreviate-file-name org-agenda-diary-file)))
9417 (org-reveal t)
9418 (message "Please finish entry here"))))
9420 (defun org-agenda-insert-diary-as-top-level (text)
9421 "Make new entry as a top-level entry at the end of the file.
9422 Add TEXT as headline, and position the cursor in the second line so that
9423 a timestamp can be added there."
9424 (widen)
9425 (goto-char (point-max))
9426 (or (bolp) (insert "\n"))
9427 (insert "* " text "\n")
9428 (if org-adapt-indentation (org-indent-to-column 2)))
9430 (defun org-agenda-insert-diary-make-new-entry (text)
9431 "Make new entry as last child of current entry.
9432 Add TEXT as headline, and position the cursor in the second line so that
9433 a timestamp can be added there."
9434 (let ((org-show-following-heading t)
9435 (org-show-siblings t)
9436 (org-show-hierarchy-above t)
9437 (org-show-entry-below t)
9438 col)
9439 (outline-next-heading)
9440 (org-back-over-empty-lines)
9441 (or (looking-at "[ \t]*$")
9442 (progn (insert "\n") (backward-char 1)))
9443 (org-insert-heading nil t)
9444 (org-do-demote)
9445 (setq col (current-column))
9446 (insert text "\n")
9447 (if org-adapt-indentation (org-indent-to-column col))
9448 (let ((org-show-following-heading t)
9449 (org-show-siblings t)
9450 (org-show-hierarchy-above t)
9451 (org-show-entry-below t))
9452 (org-show-context))))
9454 (defun org-agenda-diary-entry ()
9455 "Make a diary entry, like the `i' command from the calendar.
9456 All the standard commands work: block, weekly etc.
9457 When `org-agenda-diary-file' points to a file,
9458 `org-agenda-diary-entry-in-org-file' is called instead to create
9459 entries in that Org-mode file."
9460 (interactive)
9461 (if (not (eq org-agenda-diary-file 'diary-file))
9462 (org-agenda-diary-entry-in-org-file)
9463 (require 'diary-lib)
9464 (let* ((char (progn
9465 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
9466 (read-char-exclusive)))
9467 (cmd (cdr (assoc char
9468 '((?d . insert-diary-entry)
9469 (?w . insert-weekly-diary-entry)
9470 (?m . insert-monthly-diary-entry)
9471 (?y . insert-yearly-diary-entry)
9472 (?a . insert-anniversary-diary-entry)
9473 (?b . insert-block-diary-entry)
9474 (?c . insert-cyclic-diary-entry)))))
9475 (oldf (symbol-function 'calendar-cursor-to-date))
9476 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
9477 (point (point))
9478 (mark (or (mark t) (point))))
9479 (unless cmd
9480 (user-error "No command associated with <%c>" char))
9481 (unless (and (get-text-property point 'day)
9482 (or (not (equal ?b char))
9483 (get-text-property mark 'day)))
9484 (user-error "Don't know which date to use for diary entry"))
9485 ;; We implement this by hacking the `calendar-cursor-to-date' function
9486 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
9487 (let ((calendar-mark-ring
9488 (list (calendar-gregorian-from-absolute
9489 (or (get-text-property mark 'day)
9490 (get-text-property point 'day))))))
9491 (unwind-protect
9492 (progn
9493 (fset 'calendar-cursor-to-date
9494 (lambda (&optional error dummy)
9495 (calendar-gregorian-from-absolute
9496 (get-text-property point 'day))))
9497 (call-interactively cmd))
9498 (fset 'calendar-cursor-to-date oldf))))))
9500 (defun org-agenda-execute-calendar-command (cmd)
9501 "Execute a calendar command from the agenda with date from cursor."
9502 (org-agenda-check-type t 'agenda 'timeline)
9503 (require 'diary-lib)
9504 (unless (get-text-property (min (1- (point-max)) (point)) 'day)
9505 (user-error "Don't know which date to use for the calendar command"))
9506 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
9507 (point (point))
9508 (date (calendar-gregorian-from-absolute
9509 (get-text-property point 'day)))
9510 ;; the following 2 vars are needed in the calendar
9511 (displayed-month (car date))
9512 (displayed-year (nth 2 date)))
9513 (unwind-protect
9514 (progn
9515 (fset 'calendar-cursor-to-date
9516 (lambda (&optional error dummy)
9517 (calendar-gregorian-from-absolute
9518 (get-text-property point 'day))))
9519 (call-interactively cmd))
9520 (fset 'calendar-cursor-to-date oldf))))
9522 (defun org-agenda-phases-of-moon ()
9523 "Display the phases of the moon for the 3 months around the cursor date."
9524 (interactive)
9525 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
9527 (defun org-agenda-holidays ()
9528 "Display the holidays for the 3 months around the cursor date."
9529 (interactive)
9530 (org-agenda-execute-calendar-command 'list-calendar-holidays))
9532 (defvar calendar-longitude) ; defined in calendar.el
9533 (defvar calendar-latitude) ; defined in calendar.el
9534 (defvar calendar-location-name) ; defined in calendar.el
9536 (defun org-agenda-sunrise-sunset (arg)
9537 "Display sunrise and sunset for the cursor date.
9538 Latitude and longitude can be specified with the variables
9539 `calendar-latitude' and `calendar-longitude'. When called with prefix
9540 argument, latitude and longitude will be prompted for."
9541 (interactive "P")
9542 (require 'solar)
9543 (let ((calendar-longitude (if arg nil calendar-longitude))
9544 (calendar-latitude (if arg nil calendar-latitude))
9545 (calendar-location-name
9546 (if arg "the given coordinates" calendar-location-name)))
9547 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
9549 (defun org-agenda-goto-calendar ()
9550 "Open the Emacs calendar with the date at the cursor."
9551 (interactive)
9552 (org-agenda-check-type t 'agenda 'timeline)
9553 (let* ((day (or (get-text-property (min (1- (point-max)) (point)) 'day)
9554 (user-error "Don't know which date to open in calendar")))
9555 (date (calendar-gregorian-from-absolute day))
9556 (calendar-move-hook nil)
9557 (calendar-view-holidays-initially-flag nil)
9558 (calendar-view-diary-initially-flag nil))
9559 (calendar)
9560 (calendar-goto-date date)))
9562 ;;;###autoload
9563 (defun org-calendar-goto-agenda ()
9564 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
9565 This is a command that has to be installed in `calendar-mode-map'."
9566 (interactive)
9567 (org-agenda-list nil (calendar-absolute-from-gregorian
9568 (calendar-cursor-to-date))
9569 nil))
9571 (defun org-agenda-convert-date ()
9572 (interactive)
9573 (org-agenda-check-type t 'agenda 'timeline)
9574 (let ((day (get-text-property (min (1- (point-max)) (point)) 'day))
9575 date s)
9576 (unless day
9577 (user-error "Don't know which date to convert"))
9578 (setq date (calendar-gregorian-from-absolute day))
9579 (setq s (concat
9580 "Gregorian: " (calendar-date-string date) "\n"
9581 "ISO: " (calendar-iso-date-string date) "\n"
9582 "Day of Yr: " (calendar-day-of-year-string date) "\n"
9583 "Julian: " (calendar-julian-date-string date) "\n"
9584 "Astron. JD: " (calendar-astro-date-string date)
9585 " (Julian date number at noon UTC)\n"
9586 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
9587 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
9588 "French: " (calendar-french-date-string date) "\n"
9589 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
9590 "Mayan: " (calendar-mayan-date-string date) "\n"
9591 "Coptic: " (calendar-coptic-date-string date) "\n"
9592 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
9593 "Persian: " (calendar-persian-date-string date) "\n"
9594 "Chinese: " (calendar-chinese-date-string date) "\n"))
9595 (with-output-to-temp-buffer "*Dates*"
9596 (princ s))
9597 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
9599 ;;; Bulk commands
9601 (defun org-agenda-bulk-marked-p ()
9602 (eq (get-char-property (point-at-bol) 'type)
9603 'org-marked-entry-overlay))
9605 (defun org-agenda-bulk-mark (&optional arg)
9606 "Mark the entry at point for future bulk action."
9607 (interactive "p")
9608 (dotimes (i (or arg 1))
9609 (unless (org-get-at-bol 'org-agenda-diary-link)
9610 (let* ((m (org-get-at-bol 'org-hd-marker))
9612 (unless (org-agenda-bulk-marked-p)
9613 (unless m (user-error "Nothing to mark at point"))
9614 (push m org-agenda-bulk-marked-entries)
9615 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
9616 (org-overlay-display ov (concat org-agenda-bulk-mark-char " ")
9617 (org-get-todo-face "TODO")
9618 'evaporate)
9619 (overlay-put ov 'type 'org-marked-entry-overlay))
9620 (end-of-line 1)
9621 (or (ignore-errors
9622 (goto-char (next-single-property-change (point) 'txt)))
9623 (beginning-of-line 2))
9624 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9625 (beginning-of-line 2))
9626 (message "%d entries marked for bulk action"
9627 (length org-agenda-bulk-marked-entries))))))
9629 (defun org-agenda-bulk-mark-all ()
9630 "Mark all entries for future agenda bulk action."
9631 (interactive)
9632 (org-agenda-bulk-mark-regexp "."))
9634 (defun org-agenda-bulk-mark-regexp (regexp)
9635 "Mark entries matching REGEXP for future agenda bulk action."
9636 (interactive "sMark entries matching regexp: ")
9637 (let ((entries-marked 0) txt-at-point)
9638 (save-excursion
9639 (goto-char (point-min))
9640 (goto-char (next-single-property-change (point) 'txt))
9641 (while (and (re-search-forward regexp nil t)
9642 (setq txt-at-point (get-text-property (point) 'txt)))
9643 (when (string-match regexp txt-at-point)
9644 (setq entries-marked (1+ entries-marked))
9645 (call-interactively 'org-agenda-bulk-mark))))
9646 (if (not entries-marked)
9647 (message "No entry matching this regexp."))))
9649 (defun org-agenda-bulk-unmark (&optional arg)
9650 "Unmark the entry at point for future bulk action."
9651 (interactive "P")
9652 (if arg
9653 (org-agenda-bulk-unmark-all)
9654 (cond ((org-agenda-bulk-marked-p)
9655 (org-agenda-bulk-remove-overlays
9656 (point-at-bol) (+ 2 (point-at-bol)))
9657 (setq org-agenda-bulk-marked-entries
9658 (delete (org-get-at-bol 'org-hd-marker)
9659 org-agenda-bulk-marked-entries))
9660 (end-of-line 1)
9661 (or (ignore-errors
9662 (goto-char (next-single-property-change (point) 'txt)))
9663 (beginning-of-line 2))
9664 (while (and (get-char-property (point) 'invisible) (not (eobp)))
9665 (beginning-of-line 2))
9666 (message "%d entries left marked for bulk action"
9667 (length org-agenda-bulk-marked-entries)))
9668 (t (message "No entry to unmark here")))))
9670 (defun org-agenda-bulk-toggle-all ()
9671 "Toggle all marks for bulk action."
9672 (interactive)
9673 (save-excursion
9674 (goto-char (point-min))
9675 (while (ignore-errors
9676 (goto-char (next-single-property-change (point) 'txt)))
9677 (org-agenda-bulk-toggle))))
9679 (defun org-agenda-bulk-toggle ()
9680 "Toggle the mark at point for bulk action."
9681 (interactive)
9682 (if (org-agenda-bulk-marked-p)
9683 (org-agenda-bulk-unmark)
9684 (org-agenda-bulk-mark)))
9686 (defun org-agenda-bulk-remove-overlays (&optional beg end)
9687 "Remove the mark overlays between BEG and END in the agenda buffer.
9688 BEG and END default to the buffer limits.
9690 This only removes the overlays, it does not remove the markers
9691 from the list in `org-agenda-bulk-marked-entries'."
9692 (interactive)
9693 (mapc (lambda (ov)
9694 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
9695 (delete-overlay ov)))
9696 (overlays-in (or beg (point-min)) (or end (point-max)))))
9698 (defun org-agenda-bulk-unmark-all ()
9699 "Remove all marks in the agenda buffer.
9700 This will remove the markers and the overlays."
9701 (interactive)
9702 (if (null org-agenda-bulk-marked-entries)
9703 (message "No entry to unmark")
9704 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
9705 (setq org-agenda-bulk-marked-entries nil)
9706 (org-agenda-bulk-remove-overlays (point-min) (point-max))))
9708 (defcustom org-agenda-persistent-marks nil
9709 "Non-nil means marked items will stay marked after a bulk action.
9710 You can toggle this interactively by typing `p' when prompted for a
9711 bulk action."
9712 :group 'org-agenda
9713 :version "24.1"
9714 :type 'boolean)
9716 (defun org-agenda-bulk-action (&optional arg)
9717 "Execute an remote-editing action on all marked entries.
9718 The prefix arg is passed through to the command if possible."
9719 (interactive "P")
9720 ;; Make sure we have markers, and only valid ones
9721 (unless org-agenda-bulk-marked-entries (user-error "No entries are marked"))
9722 (mapc
9723 (lambda (m)
9724 (unless (and (markerp m)
9725 (marker-buffer m)
9726 (buffer-live-p (marker-buffer m))
9727 (marker-position m))
9728 (user-error "Marker %s for bulk command is invalid" m)))
9729 org-agenda-bulk-marked-entries)
9731 ;; Prompt for the bulk command
9732 (let* ((msg (if org-agenda-persistent-marks "Bulk (persistent): " "Bulk: ")))
9733 (message (concat msg "[$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [d]eadline [r]efile "
9734 "[S]catter [f]unction "
9735 (when org-agenda-bulk-custom-functions
9736 (concat " Custom: ["
9737 (mapconcat (lambda(f) (char-to-string (car f)))
9738 org-agenda-bulk-custom-functions "")
9739 "]"))))
9740 (catch 'exit
9741 (let* ((action (read-char-exclusive))
9742 (org-log-refile (if org-log-refile 'time nil))
9743 (entries (reverse org-agenda-bulk-marked-entries))
9744 (org-overriding-default-time
9745 (if (get-text-property (point) 'org-agenda-date-header)
9746 (org-get-cursor-date)))
9747 redo-at-end
9748 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
9749 (cond
9750 ((equal action ?p)
9751 (let ((org-agenda-persistent-marks
9752 (not org-agenda-persistent-marks)))
9753 (org-agenda-bulk-action)
9754 (throw 'exit nil)))
9756 ((equal action ?$)
9757 (setq cmd '(org-agenda-archive)))
9759 ((equal action ?A)
9760 (setq cmd '(org-agenda-archive-to-archive-sibling)))
9762 ((member action '(?r ?w))
9763 (setq rfloc (org-refile-get-location
9764 "Refile to"
9765 (marker-buffer (car entries))
9766 org-refile-allow-creating-parent-nodes))
9767 (if (nth 3 rfloc)
9768 (setcar (nthcdr 3 rfloc)
9769 (move-marker (make-marker) (nth 3 rfloc)
9770 (or (get-file-buffer (nth 1 rfloc))
9771 (find-buffer-visiting (nth 1 rfloc))
9772 (error "This should not happen")))))
9774 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
9775 redo-at-end t))
9777 ((equal action ?t)
9778 (setq state (org-icompleting-read
9779 "Todo state: "
9780 (with-current-buffer (marker-buffer (car entries))
9781 (mapcar 'list org-todo-keywords-1))))
9782 (setq cmd `(let ((org-inhibit-blocking t)
9783 (org-inhibit-logging 'note))
9784 (org-agenda-todo ,state))))
9786 ((memq action '(?- ?+))
9787 (setq tag (org-icompleting-read
9788 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
9789 (with-current-buffer (marker-buffer (car entries))
9790 (delq nil
9791 (mapcar (lambda (x)
9792 (if (stringp (car x)) x)) org-tag-alist)))))
9793 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
9795 ((memq action '(?s ?d))
9796 (let* ((time
9797 (unless arg
9798 (org-read-date
9799 nil nil nil
9800 (if (eq action ?s) "(Re)Schedule to" "(Re)Set Deadline to")
9801 org-overriding-default-time)))
9802 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
9803 (setq cmd `(eval '(,c1 arg ,time)))))
9805 ((equal action ?S)
9806 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
9807 (user-error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
9808 (let ((days (read-number
9809 (format "Scatter tasks across how many %sdays: "
9810 (if arg "week" "")) 7)))
9811 (setq cmd
9812 `(let ((distance (1+ (random ,days))))
9813 (if arg
9814 (let ((dist distance)
9815 (day-of-week
9816 (calendar-day-of-week
9817 (calendar-gregorian-from-absolute (org-today)))))
9818 (dotimes (i (1+ dist))
9819 (while (member day-of-week org-agenda-weekend-days)
9820 (incf distance)
9821 (incf day-of-week)
9822 (if (= day-of-week 7)
9823 (setq day-of-week 0)))
9824 (incf day-of-week)
9825 (if (= day-of-week 7)
9826 (setq day-of-week 0)))))
9827 ;; silently fail when try to replan a sexp entry
9828 (condition-case nil
9829 (let* ((date (calendar-gregorian-from-absolute
9830 (+ (org-today) distance)))
9831 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
9832 (nth 2 date))))
9833 (org-agenda-schedule nil time))
9834 (error nil)))))))
9836 ((assoc action org-agenda-bulk-custom-functions)
9837 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
9838 redo-at-end t))
9840 ((equal action ?f)
9841 (setq cmd (list (intern
9842 (org-icompleting-read "Function: "
9843 obarray 'fboundp t nil nil)))))
9845 (t (user-error "Invalid bulk action")))
9847 ;; Sort the markers, to make sure that parents are handled before children
9848 (setq entries (sort entries
9849 (lambda (a b)
9850 (cond
9851 ((equal (marker-buffer a) (marker-buffer b))
9852 (< (marker-position a) (marker-position b)))
9854 (string< (buffer-name (marker-buffer a))
9855 (buffer-name (marker-buffer b))))))))
9857 ;; Now loop over all markers and apply cmd
9858 (while (setq e (pop entries))
9859 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
9860 (if (not pos)
9861 (progn (message "Skipping removed entry at %s" e)
9862 (setq cntskip (1+ cntskip)))
9863 (goto-char pos)
9864 (let (org-loop-over-headlines-in-active-region)
9865 (eval cmd))
9866 (setq cnt (1+ cnt))))
9867 (when redo-at-end (org-agenda-redo))
9868 (unless org-agenda-persistent-marks
9869 (org-agenda-bulk-unmark-all))
9870 (message "Acted on %d entries%s%s"
9872 (if (= cntskip 0)
9874 (format ", skipped %d (disappeared before their turn)"
9875 cntskip))
9876 (if (not org-agenda-persistent-marks)
9877 "" " (kept marked)"))))))
9879 (defun org-agenda-capture (&optional with-time)
9880 "Call `org-capture' with the date at point.
9881 With a `C-1' prefix, use the HH:MM value at point (if any) or the
9882 current HH:MM time."
9883 (interactive "P")
9884 (if (not (eq major-mode 'org-agenda-mode))
9885 (user-error "You cannot do this outside of agenda buffers")
9886 (let ((org-overriding-default-time
9887 (org-get-cursor-date (equal with-time 1))))
9888 (call-interactively 'org-capture))))
9890 ;;; Dragging agenda lines forward/backward
9892 (defun org-agenda-drag-line-forward (arg)
9893 "Drag an agenda line forward by ARG lines."
9894 (interactive "p")
9895 (let ((inhibit-read-only t) lst)
9896 (if (save-excursion
9897 (dotimes (n arg)
9898 (beginning-of-line 2)
9899 (push (not (get-text-property (point) 'txt)) lst))
9900 (delq nil lst))
9901 (message "Cannot move line forward")
9902 (org-drag-line-forward arg))))
9904 (defun org-agenda-drag-line-backward (arg)
9905 "Drag an agenda line backward by ARG lines."
9906 (interactive "p")
9907 (let ((inhibit-read-only t) lst)
9908 (if (save-excursion
9909 (dotimes (n arg)
9910 (beginning-of-line 0)
9911 (push (not (get-text-property (point) 'txt)) lst))
9912 (delq nil lst))
9913 (message "Cannot move line backward")
9914 (org-drag-line-backward arg))))
9916 ;;; Flagging notes
9918 (defun org-agenda-show-the-flagging-note ()
9919 "Display the flagging note in the other window.
9920 When called a second time in direct sequence, offer to remove the FLAGGING
9921 tag and (if present) the flagging note."
9922 (interactive)
9923 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
9924 (win (selected-window))
9925 note heading newhead)
9926 (unless hdmarker
9927 (user-error "No linked entry at point"))
9928 (if (and (eq this-command last-command)
9929 (y-or-n-p "Unflag and remove any flagging note? "))
9930 (progn
9931 (org-agenda-remove-flag hdmarker)
9932 (let ((win (get-buffer-window "*Flagging Note*")))
9933 (and win (delete-window win)))
9934 (message "Entry unflagged"))
9935 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
9936 (unless note
9937 (user-error "No flagging note"))
9938 (org-kill-new note)
9939 (org-switch-to-buffer-other-window "*Flagging Note*")
9940 (erase-buffer)
9941 (insert note)
9942 (goto-char (point-min))
9943 (while (re-search-forward "\\\\n" nil t)
9944 (replace-match "\n" t t))
9945 (goto-char (point-min))
9946 (select-window win)
9947 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
9949 (defun org-agenda-remove-flag (marker)
9950 "Remove the FLAGGED tag and any flagging note in the entry."
9951 (let (newhead)
9952 (org-with-point-at marker
9953 (org-toggle-tag "FLAGGED" 'off)
9954 (org-entry-delete nil "THEFLAGGINGNOTE")
9955 (setq newhead (org-get-heading)))
9956 (org-agenda-change-all-lines newhead marker)
9957 (message "Entry unflagged")))
9959 (defun org-agenda-get-any-marker (&optional pos)
9960 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
9961 (get-text-property (or pos (point-at-bol)) 'org-marker)))
9963 ;;; Appointment reminders
9965 (defvar appt-time-msg-list) ; defined in appt.el
9967 ;;;###autoload
9968 (defun org-agenda-to-appt (&optional refresh filter &rest args)
9969 "Activate appointments found in `org-agenda-files'.
9970 With a \\[universal-argument] prefix, refresh the list of
9971 appointments.
9973 If FILTER is t, interactively prompt the user for a regular
9974 expression, and filter out entries that don't match it.
9976 If FILTER is a string, use this string as a regular expression
9977 for filtering entries out.
9979 If FILTER is a function, filter out entries against which
9980 calling the function returns nil. This function takes one
9981 argument: an entry from `org-agenda-get-day-entries'.
9983 FILTER can also be an alist with the car of each cell being
9984 either 'headline or 'category. For example:
9986 '((headline \"IMPORTANT\")
9987 (category \"Work\"))
9989 will only add headlines containing IMPORTANT or headlines
9990 belonging to the \"Work\" category.
9992 ARGS are symbols indicating what kind of entries to consider.
9993 By default `org-agenda-to-appt' will use :deadline*, :scheduled*
9994 \(i.e., deadlines and scheduled items with a hh:mm specification)
9995 and :timestamp entries. See the docstring of `org-diary' for
9996 details and examples.
9998 If an entry has a APPT_WARNTIME property, its value will be used
9999 to override `appt-message-warning-time'."
10000 (interactive "P")
10001 (if refresh (setq appt-time-msg-list nil))
10002 (if (eq filter t)
10003 (setq filter (read-from-minibuffer "Regexp filter: ")))
10004 (let* ((cnt 0) ; count added events
10005 (scope (or args '(:deadline* :scheduled* :timestamp)))
10006 (org-agenda-new-buffers nil)
10007 (org-deadline-warning-days 0)
10008 ;; Do not use `org-today' here because appt only takes
10009 ;; time and without date as argument, so it may pass wrong
10010 ;; information otherwise
10011 (today (org-date-to-gregorian
10012 (time-to-days (current-time))))
10013 (org-agenda-restrict nil)
10014 (files (org-agenda-files 'unrestricted)) entries file
10015 (org-agenda-buffer nil))
10016 ;; Get all entries which may contain an appt
10017 (org-agenda-prepare-buffers files)
10018 (while (setq file (pop files))
10019 (setq entries
10020 (delq nil
10021 (append entries
10022 (apply 'org-agenda-get-day-entries
10023 file today scope)))))
10024 ;; Map thru entries and find if we should filter them out
10025 (mapc
10026 (lambda(x)
10027 (let* ((evt (org-trim
10028 (replace-regexp-in-string
10029 org-bracket-link-regexp "\\3"
10030 (or (get-text-property 1 'txt x) ""))))
10031 (cat (get-text-property 1 'org-category x))
10032 (tod (get-text-property 1 'time-of-day x))
10033 (ok (or (null filter)
10034 (and (stringp filter) (string-match filter evt))
10035 (and (functionp filter) (funcall filter x))
10036 (and (listp filter)
10037 (let ((cat-filter (cadr (assoc 'category filter)))
10038 (evt-filter (cadr (assoc 'headline filter))))
10039 (or (and (stringp cat-filter)
10040 (string-match cat-filter cat))
10041 (and (stringp evt-filter)
10042 (string-match evt-filter evt)))))))
10043 (wrn (get-text-property 1 'warntime x)))
10044 ;; FIXME: Shall we remove text-properties for the appt text?
10045 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
10046 (when (and ok tod)
10047 (setq tod (concat "00" (number-to-string tod))
10048 tod (when (string-match
10049 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
10050 (concat (match-string 1 tod) ":"
10051 (match-string 2 tod))))
10052 (if (version< emacs-version "23.3")
10053 (appt-add tod evt)
10054 (appt-add tod evt wrn))
10055 (setq cnt (1+ cnt))))) entries)
10056 (org-release-buffers org-agenda-new-buffers)
10057 (if (eq cnt 0)
10058 (message "No event to add")
10059 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
10061 (defun org-agenda-todayp (date)
10062 "Does DATE mean today, when considering `org-extend-today-until'?"
10063 (let ((today (org-today))
10064 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
10065 date)))
10066 (eq date today)))
10068 (defun org-agenda-todo-yesterday (&optional arg)
10069 "Like `org-agenda-todo' but the time of change will be 23:59 of yesterday."
10070 (interactive "P")
10071 (let* ((hour (third (decode-time
10072 (org-current-time))))
10073 (org-extend-today-until (1+ hour)))
10074 (org-agenda-todo arg)))
10076 (provide 'org-agenda)
10078 ;;; org-agenda.el ends here